annotate UnitTestsSources/TestMessageBroker.cpp @ 879:12b591d5d63c am-dev

some Qt integration (wip)
author Alain Mazy <alain@mazy.be>
date Fri, 05 Jul 2019 14:52:43 +0200
parents f0008c55e5f7
children 5df50e0f0390
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a750f11892ec merge default->am-vsol-upgrade
amazy
parents: 428 439
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
20
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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 class MyObservable : public IObservable
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 public:
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
40 struct MyCustomMessage : public IMessage
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
41 {
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
42 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
43
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
44 int payload_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
45
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
46 MyCustomMessage(int payload) :
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
47 payload_(payload)
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
48 {
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
49 }
428
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
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
52 MyObservable(MessageBroker& broker) :
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
53 IObservable(broker)
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
54 {
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
55 }
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
56 };
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
57
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
58 class MyObserver : public IObserver
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
59 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
60 public:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
61 MyObserver(MessageBroker& broker)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
62 : IObserver(broker)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
63 {}
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
64
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
65 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message)
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 testCounter += message.payload_;
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
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
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
73 class MyIntermediate : public IObserver, public IObservable
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
74 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
75 IObservable& observedObject_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
76 public:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
77 MyIntermediate(MessageBroker& broker, IObservable& observedObject)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
78 : IObserver(broker),
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
79 IObservable(broker),
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
80 observedObject_(observedObject)
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 observedObject_.RegisterObserverCallback(new MessageForwarder<MyObservable::MyCustomMessage>(broker, *this));
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 };
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
85
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
86
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
87 class MyPromiseSource : public IObservable
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 Promise* currentPromise_;
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
90
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
91 public:
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
92 struct MyPromiseMessage: public IMessage
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
93 {
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
94 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
95
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
96 int increment;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
97
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
98 MyPromiseMessage(int increment) :
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
99 increment(increment)
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
100 {
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
101 }
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
102 };
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
103
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
104 MyPromiseSource(MessageBroker& broker)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
105 : IObservable(broker),
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
106 currentPromise_(NULL)
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
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
109 Promise& StartSomethingAsync()
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
110 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
111 currentPromise_ = new Promise(GetBroker());
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
112 return *currentPromise_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
113 }
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
114
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
115 void CompleteSomethingAsyncWithSuccess(int payload)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
116 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
117 currentPromise_->Success(MyPromiseMessage(payload));
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
118 delete currentPromise_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
119 }
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
120
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
121 void CompleteSomethingAsyncWithFailure(int payload)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
122 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
123 currentPromise_->Failure(MyPromiseMessage(payload));
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
124 delete currentPromise_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
125 }
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
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
128
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
129 class MyPromiseTarget : public IObserver
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 public:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
132 MyPromiseTarget(MessageBroker& broker)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
133 : IObserver(broker)
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
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
136 void IncrementCounter(const MyPromiseSource::MyPromiseMessage& args)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
137 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
138 testCounter += args.increment;
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
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
141 void DecrementCounter(const MyPromiseSource::MyPromiseMessage& args)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
142 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
143 testCounter -= args.increment;
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 };
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
146 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
147
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
148
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
149 TEST(MessageBroker, TestPermanentConnectionSimpleUseCase)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
150 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
151 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
152 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
153 MyObserver observer(broker);
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 // 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
156 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
157
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
158 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
159 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
160 ASSERT_EQ(12, testCounter);
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 // 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
163 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
164 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
165 ASSERT_EQ(20, testCounter);
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 // 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
168 observable.Unregister(&observer);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
169 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
170 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
171 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
172 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
173
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
174 TEST(MessageBroker, TestMessageForwarderSimpleUseCase)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
175 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
176 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
177 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
178 MyIntermediate intermediate(broker, observable);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
179 MyObserver observer(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
180
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
181 // 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
182 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
183
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
184 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
185 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
186 ASSERT_EQ(12, testCounter);
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 // 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
189 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
190 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
191 ASSERT_EQ(20, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
192 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
193
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
194 TEST(MessageBroker, TestPermanentConnectionDeleteObserver)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
195 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
196 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
197 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
198 MyObserver* observer = new MyObserver(broker);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
199
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
200 // 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
201 observable.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(*observer, &MyObserver::HandleCompletedMessage));
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
202
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
203 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
204 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
205 ASSERT_EQ(12, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
206
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
207 // 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
208 delete observer;
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
209
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
210 // 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
211 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
212 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
213 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
214 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
215
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
216 TEST(MessageBroker, TestMessageForwarderDeleteIntermediate)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
217 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
218 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
219 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
220 MyIntermediate* intermediate = new MyIntermediate(broker, observable);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
221 MyObserver observer(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
222
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
223 // 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
224 intermediate->RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage));
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
225
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
226 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
227 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
228 ASSERT_EQ(12, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
229
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
230 delete intermediate;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
231
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
232 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
233 ASSERT_EQ(12, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
234 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
235
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
236 TEST(MessageBroker, TestCustomMessage)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
237 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
238 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
239 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
240 MyIntermediate intermediate(broker, observable);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
241 MyObserver observer(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
242
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
243 // 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
244 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
245
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
246 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
247 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
248 ASSERT_EQ(12, testCounter);
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 // 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
251 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
252 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
253 ASSERT_EQ(20, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
254 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
255
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
256
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
257 TEST(MessageBroker, TestPromiseSuccessFailure)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
258 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
259 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
260 MyPromiseSource source(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
261 MyPromiseTarget target(broker);
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 // test a successful promise
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
264 source.StartSomethingAsync()
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
265 .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
266 .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
267
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
268 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
269 source.CompleteSomethingAsyncWithSuccess(10);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
270 ASSERT_EQ(10, testCounter);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
271
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
272 // test a failing promise
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
273 source.StartSomethingAsync()
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
274 .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
275 .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
276
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
277 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
278 source.CompleteSomethingAsyncWithFailure(15);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
279 ASSERT_EQ(-15, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
280 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
281
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
282 TEST(MessageBroker, TestPromiseDeleteTarget)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
283 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
284 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
285 MyPromiseSource source(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
286 MyPromiseTarget* target = new MyPromiseTarget(broker);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
287
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
288 // create the promise
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
289 source.StartSomethingAsync()
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
290 .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
291 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::DecrementCounter));
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
292
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
293 // delete the promise target
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
294 delete target;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
295
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
296 // 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
297 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
298 source.CompleteSomethingAsyncWithSuccess(10);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
299 ASSERT_EQ(0, testCounter);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
300
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
301 // test a failing promise
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
302 source.StartSomethingAsync()
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
303 .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
304 .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
305
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
306 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
307 source.CompleteSomethingAsyncWithFailure(15);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
308 ASSERT_EQ(0, testCounter);
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
560
aaeec7be8fb7 add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
311 #if 0 /* __cplusplus >= 201103L*/
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
312
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
313 TEST(MessageBroker, TestLambdaSimpleUseCase)
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 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
316 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
317 MyObserver* observer = new MyObserver(broker);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
318
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
319 // 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
320 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
321
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
322 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
323 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
324 ASSERT_EQ(24, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
325
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
326 // 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
327 delete observer;
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
328
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
329 // 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
330 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
331 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
332 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
333 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
334
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
335 namespace {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
336 class MyObserverWithLambda : public IObserver {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
337 private:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
338 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
339
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
340 public:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
341 MyObserverWithLambda(MessageBroker& broker, int multiplier, MyObservable& observable)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
342 : IObserver(broker),
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
343 multiplier_(multiplier)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
344 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
345 // 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
346 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
347 testCounter += multiplier_ * message.payload_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
348 }));
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
349
428
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 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
353
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
354 TEST(MessageBroker, TestLambdaCaptureThisAndAccessPrivateMembers)
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 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
357 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
358 MyObserverWithLambda* observer = new MyObserverWithLambda(broker, 3, observable);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
359
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
360 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
361 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
362 ASSERT_EQ(36, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
363
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
364 // 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
365 delete observer;
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
366
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
367 // 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
368 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
369 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
370 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
371 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
372
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
373 #endif // C++ 11