Mercurial > hg > orthanc-stone
annotate UnitTestsSources/TestMessageBroker.cpp @ 1075:81b29bc7c3d4 broker
cont
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 17 Oct 2019 14:39:51 +0200 |
parents | 05b2e71ed145 |
children | 008dbc4ceb62 |
rev | line source |
---|---|
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
1 /** |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
2 * Stone of Orthanc |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
440 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
6 * |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
10 * the License, or (at your option) any later version. |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
11 * |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
15 * Affero General Public License for more details. |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
16 * |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
19 **/ |
251 | 20 |
21 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
22 #include "gtest/gtest.h" |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
23 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
24 #include "Framework/Messages/IObservable.h" |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
25 #include "Framework/Messages/ObserverBase.h" |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
26 |
251 | 27 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
28 int testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
29 namespace { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
30 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
31 using namespace OrthancStone; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
32 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
33 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
34 class MyObservable : public IObservable |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
35 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
36 public: |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
37 struct MyCustomMessage : public IMessage |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
38 { |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
39 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__); |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
40 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
41 int payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
42 |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
43 MyCustomMessage(int payload) : |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
44 payload_(payload) |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
45 { |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
46 } |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
47 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
48 }; |
267 | 49 |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
50 class MyObserver : public ObserverBase<MyObserver> |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
51 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
52 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
53 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
54 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
55 testCounter += message.payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
56 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
57 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
58 } |
251 | 59 |
60 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
61 TEST(MessageBroker, TestPermanentConnectionSimpleUseCase) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
62 { |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
63 MyObservable observable; |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
64 boost::shared_ptr<MyObserver> observer(new MyObserver); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
65 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
66 // create a permanent connection between an observable and an observer |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
67 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
68 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
69 testCounter = 0; |
624 | 70 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
71 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
72 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
73 // the connection is permanent; if we emit the same message again, the observer will be notified again |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
74 testCounter = 0; |
624 | 75 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
76 ASSERT_EQ(20, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
77 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
78 // Unregister the observer; make sure it's not called anymore |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
79 observer.reset(); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
80 testCounter = 0; |
624 | 81 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
82 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
83 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
84 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
85 TEST(MessageBroker, TestPermanentConnectionDeleteObserver) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
86 { |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
87 MyObservable observable; |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
88 boost::shared_ptr<MyObserver> observer(new MyObserver); |
251 | 89 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
90 // create a permanent connection between an observable and an observer |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
91 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage); |
267 | 92 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
93 testCounter = 0; |
624 | 94 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
95 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
96 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
97 // delete the observer and check that the callback is not called anymore |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
98 observer.reset(); |
267 | 99 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
100 // the connection is permanent; if we emit the same message again, the observer will be notified again |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
101 testCounter = 0; |
624 | 102 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
103 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
104 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
105 |
251 | 106 |
107 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
456
diff
changeset
|
108 #if 0 /* __cplusplus >= 201103L*/ |
251 | 109 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
110 TEST(MessageBroker, TestLambdaSimpleUseCase) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
111 { |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
112 MyObservable observable; |
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
113 MyObserver* observer(new MyObserver); |
251 | 114 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
115 // create a permanent connection between an observable and an observer |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
116 observable.RegisterObserverCallback(new LambdaCallable<MyObservable::MyCustomMessage>(*observer, [&](const MyObservable::MyCustomMessage& message) {testCounter += 2 * message.payload_;})); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
117 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
118 testCounter = 0; |
624 | 119 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
120 ASSERT_EQ(24, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
121 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
122 // delete the observer and check that the callback is not called anymore |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
123 delete observer; |
251 | 124 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
125 // the connection is permanent; if we emit the same message again, the observer will be notified again |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
126 testCounter = 0; |
624 | 127 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
128 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
129 } |
251 | 130 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
131 namespace { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
132 class MyObserverWithLambda : public IObserver { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
133 private: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
134 int multiplier_; // this is a private variable we want to access in a lambda |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
135 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
136 public: |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
137 MyObserverWithLambda(int multiplier, MyObservable& observable) |
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
138 : multiplier_(multiplier) |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
139 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
140 // register a callable to a lambda that access private members |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
141 observable.RegisterObserverCallback(new LambdaCallable<MyObservable::MyCustomMessage>(*this, [this](const MyObservable::MyCustomMessage& message) { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
142 testCounter += multiplier_ * message.payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
143 })); |
251 | 144 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
145 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
146 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
147 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
148 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
149 TEST(MessageBroker, TestLambdaCaptureThisAndAccessPrivateMembers) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
150 { |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
151 MyObservable observable; |
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1057
diff
changeset
|
152 MyObserverWithLambda* observer = new MyObserverWithLambda(3, observable); |
251 | 153 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
154 testCounter = 0; |
624 | 155 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
156 ASSERT_EQ(36, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
157 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
158 // delete the observer and check that the callback is not called anymore |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
159 delete observer; |
251 | 160 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
161 // the connection is permanent; if we emit the same message again, the observer will be notified again |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
162 testCounter = 0; |
624 | 163 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
164 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
165 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
166 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
167 #endif // C++ 11 |