Mercurial > hg > orthanc-stone
annotate UnitTestsSources/TestMessageBroker.cpp @ 501:8424b10df93a bgo-commands-codegen
Dummy change
author | bgo-osimis |
---|---|
date | Mon, 25 Feb 2019 14:51:34 +0100 |
parents | a750f11892ec |
children | b70fcc134ba4 |
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/MessageBroker.h" |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
25 #include "Framework/Messages/Promise.h" |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
26 #include "Framework/Messages/IObservable.h" |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
27 #include "Framework/Messages/IObserver.h" |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
28 #include "Framework/Messages/MessageForwarder.h" |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
29 |
251 | 30 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
31 int testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
32 namespace { |
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 using namespace OrthancStone; |
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 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
37 enum CustomMessageType |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
38 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
39 CustomMessageType_First = MessageType_CustomMessage + 1, |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
40 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
41 CustomMessageType_Completed, |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
42 CustomMessageType_Increment |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
43 }; |
251 | 44 |
45 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
46 class MyObservable : public IObservable |
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 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
49 struct MyCustomMessage: public BaseMessage<CustomMessageType_Completed> |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
50 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
51 int payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
52 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
53 MyCustomMessage(int payload) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
54 : BaseMessage(), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
55 payload_(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 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
59 MyObservable(MessageBroker& broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
60 : IObservable(broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
61 {} |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
62 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
63 }; |
267 | 64 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
65 class MyObserver : public IObserver |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
66 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
67 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
68 MyObserver(MessageBroker& broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
69 : IObserver(broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
70 {} |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
71 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
72 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
73 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
74 testCounter += message.payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
75 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
76 |
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 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
79 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
80 class MyIntermediate : public IObserver, public IObservable |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
81 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
82 IObservable& observedObject_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
83 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
84 MyIntermediate(MessageBroker& broker, IObservable& observedObject) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
85 : IObserver(broker), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
86 IObservable(broker), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
87 observedObject_(observedObject) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
88 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
89 observedObject_.RegisterObserverCallback(new MessageForwarder<MyObservable::MyCustomMessage>(broker, *this)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
90 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
91 }; |
267 | 92 |
93 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
94 class MyPromiseSource : public IObservable |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
95 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
96 Promise* currentPromise_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
97 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
98 struct MyPromiseMessage: public BaseMessage<MessageType_Test1> |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
99 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
100 int increment; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
101 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
102 MyPromiseMessage(int increment) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
103 : BaseMessage(), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
104 increment(increment) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
105 {} |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
106 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
107 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
108 MyPromiseSource(MessageBroker& broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
109 : IObservable(broker), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
110 currentPromise_(NULL) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
111 {} |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
112 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
113 Promise& StartSomethingAsync() |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
114 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
115 currentPromise_ = new Promise(GetBroker()); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
116 return *currentPromise_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
117 } |
267 | 118 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
119 void CompleteSomethingAsyncWithSuccess(int payload) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
120 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
121 currentPromise_->Success(MyPromiseMessage(payload)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
122 delete currentPromise_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
123 } |
267 | 124 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
125 void CompleteSomethingAsyncWithFailure(int payload) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
126 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
127 currentPromise_->Failure(MyPromiseMessage(payload)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
128 delete currentPromise_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
129 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
130 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
131 |
251 | 132 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
133 class MyPromiseTarget : public IObserver |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
134 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
135 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
136 MyPromiseTarget(MessageBroker& broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
137 : IObserver(broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
138 {} |
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 void IncrementCounter(const MyPromiseSource::MyPromiseMessage& args) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
141 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
142 testCounter += args.increment; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
143 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
144 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
145 void DecrementCounter(const MyPromiseSource::MyPromiseMessage& args) |
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 testCounter -= args.increment; |
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 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
150 } |
251 | 151 |
152 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
153 TEST(MessageBroker, TestPermanentConnectionSimpleUseCase) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
154 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
155 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
156 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
157 MyObserver observer(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
158 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
159 // 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
|
160 observable.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
161 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
162 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
163 observable.EmitMessage(MyObservable::MyCustomMessage(12)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
164 ASSERT_EQ(12, 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 // 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
|
167 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
168 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
169 ASSERT_EQ(20, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
170 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
171 // Unregister the observer; make sure it's not called anymore |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
172 observable.Unregister(&observer); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
173 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
174 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
175 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
176 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
177 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
178 TEST(MessageBroker, TestMessageForwarderSimpleUseCase) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
179 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
180 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
181 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
182 MyIntermediate intermediate(broker, observable); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
183 MyObserver observer(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
184 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
185 // let the observer observers the intermediate that is actually forwarding the messages from the observable |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
186 intermediate.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
187 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
188 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
189 observable.EmitMessage(MyObservable::MyCustomMessage(12)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
190 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
191 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
192 // 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
|
193 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
194 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
195 ASSERT_EQ(20, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
196 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
197 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
198 TEST(MessageBroker, TestPermanentConnectionDeleteObserver) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
199 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
200 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
201 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
202 MyObserver* observer = new MyObserver(broker); |
251 | 203 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
204 // 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
|
205 observable.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(*observer, &MyObserver::HandleCompletedMessage)); |
267 | 206 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
207 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
208 observable.EmitMessage(MyObservable::MyCustomMessage(12)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
209 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
210 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
211 // 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
|
212 delete observer; |
267 | 213 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
214 // 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
|
215 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
216 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
217 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
218 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
219 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
220 TEST(MessageBroker, TestMessageForwarderDeleteIntermediate) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
221 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
222 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
223 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
224 MyIntermediate* intermediate = new MyIntermediate(broker, observable); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
225 MyObserver observer(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
226 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
227 // let the observer observers the intermediate that is actually forwarding the messages from the observable |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
228 intermediate->RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage)); |
267 | 229 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
230 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
231 observable.EmitMessage(MyObservable::MyCustomMessage(12)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
232 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
233 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
234 delete intermediate; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
235 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
236 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
237 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
238 } |
251 | 239 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
240 TEST(MessageBroker, TestCustomMessage) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
241 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
242 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
243 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
244 MyIntermediate intermediate(broker, observable); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
245 MyObserver observer(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
246 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
247 // let the observer observers the intermediate that is actually forwarding the messages from the observable |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
248 intermediate.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
249 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
250 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
251 observable.EmitMessage(MyObservable::MyCustomMessage(12)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
252 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
253 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
254 // 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
|
255 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
256 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
257 ASSERT_EQ(20, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
258 } |
251 | 259 |
260 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
261 TEST(MessageBroker, TestPromiseSuccessFailure) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
262 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
263 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
264 MyPromiseSource source(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
265 MyPromiseTarget target(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
266 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
267 // test a successful promise |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
268 source.StartSomethingAsync() |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
269 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::IncrementCounter)) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
270 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::DecrementCounter)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
271 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
272 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
273 source.CompleteSomethingAsyncWithSuccess(10); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
274 ASSERT_EQ(10, testCounter); |
251 | 275 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
276 // test a failing promise |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
277 source.StartSomethingAsync() |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
278 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::IncrementCounter)) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
279 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::DecrementCounter)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
280 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
281 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
282 source.CompleteSomethingAsyncWithFailure(15); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
283 ASSERT_EQ(-15, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
284 } |
251 | 285 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
286 TEST(MessageBroker, TestPromiseDeleteTarget) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
287 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
288 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
289 MyPromiseSource source(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
290 MyPromiseTarget* target = new MyPromiseTarget(broker); |
251 | 291 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
292 // create the promise |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
293 source.StartSomethingAsync() |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
294 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::IncrementCounter)) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
295 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::DecrementCounter)); |
251 | 296 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
297 // delete the promise target |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
298 delete target; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
299 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
300 // trigger the promise, make sure it does not throw and does not call the callback |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
301 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
302 source.CompleteSomethingAsyncWithSuccess(10); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
303 ASSERT_EQ(0, testCounter); |
251 | 304 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
305 // test a failing promise |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
306 source.StartSomethingAsync() |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
307 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::IncrementCounter)) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
308 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::DecrementCounter)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
309 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
310 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
311 source.CompleteSomethingAsyncWithFailure(15); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
312 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
313 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
314 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
315 #if __cplusplus >= 201103L |
251 | 316 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
317 #include <functional> |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
318 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
319 namespace OrthancStone { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
320 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
321 template <typename TMessage> |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
322 class LambdaCallable : public MessageHandler<TMessage> |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
323 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
324 private: |
251 | 325 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
326 IObserver& observer_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
327 std::function<void (const TMessage&)> lambda_; |
251 | 328 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
329 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
330 LambdaCallable(IObserver& observer, |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
331 std::function<void (const TMessage&)> lambdaFunction) : |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
332 observer_(observer), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
333 lambda_(lambdaFunction) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
334 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
335 } |
251 | 336 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
337 virtual void Apply(const IMessage& message) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
338 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
339 lambda_(dynamic_cast<const TMessage&>(message)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
340 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
341 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
342 virtual MessageType GetMessageType() const |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
343 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
344 return static_cast<MessageType>(TMessage::Type); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
345 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
346 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
347 virtual IObserver* GetObserver() const |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
348 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
349 return &observer_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
350 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
351 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
352 |
251 | 353 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
354 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
355 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
356 TEST(MessageBroker, TestLambdaSimpleUseCase) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
357 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
358 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
359 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
360 MyObserver* observer = new MyObserver(broker); |
251 | 361 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
362 // 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
|
363 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
|
364 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
365 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
366 observable.EmitMessage(MyObservable::MyCustomMessage(12)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
367 ASSERT_EQ(24, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
368 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
369 // 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
|
370 delete observer; |
251 | 371 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
372 // 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
|
373 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
374 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
375 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
376 } |
251 | 377 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
378 namespace { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
379 class MyObserverWithLambda : public IObserver { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
380 private: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
381 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
|
382 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
383 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
384 MyObserverWithLambda(MessageBroker& broker, int multiplier, MyObservable& observable) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
385 : IObserver(broker), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
386 multiplier_(multiplier) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
387 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
388 // 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
|
389 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
|
390 testCounter += multiplier_ * message.payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
391 })); |
251 | 392 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
393 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
394 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
395 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
396 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
397 TEST(MessageBroker, TestLambdaCaptureThisAndAccessPrivateMembers) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
398 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
399 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
400 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
401 MyObserverWithLambda* observer = new MyObserverWithLambda(broker, 3, observable); |
251 | 402 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
403 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
404 observable.EmitMessage(MyObservable::MyCustomMessage(12)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
405 ASSERT_EQ(36, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
406 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
407 // 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
|
408 delete observer; |
251 | 409 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
410 // 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
|
411 testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
412 observable.EmitMessage(MyObservable::MyCustomMessage(20)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
413 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
414 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
415 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
416 #endif // C++ 11 |