annotate UnitTestsSources/TestMessageBroker.cpp @ 582:e36e69a380a5

Scene2D
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Apr 2019 16:43:08 +0200
parents 1201b12eb9f8
children 8adc8cfb50c7
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 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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
44
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
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
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
92
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
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
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
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
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
151
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
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
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
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
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
259
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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
560
aaeec7be8fb7 add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
315 #if 0 /* __cplusplus >= 201103L*/
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
316
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
317 TEST(MessageBroker, TestLambdaSimpleUseCase)
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 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
320 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
321 MyObserver* observer = new MyObserver(broker);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
322
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
323 // 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
324 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
325
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
326 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
327 observable.EmitMessage(MyObservable::MyCustomMessage(12));
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
328 ASSERT_EQ(24, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
329
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
330 // 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
331 delete observer;
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
332
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
333 // 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
334 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
335 observable.EmitMessage(MyObservable::MyCustomMessage(20));
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
336 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
337 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
338
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
339 namespace {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
340 class MyObserverWithLambda : public IObserver {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
341 private:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
342 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
343
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
344 public:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
345 MyObserverWithLambda(MessageBroker& broker, int multiplier, MyObservable& observable)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
346 : IObserver(broker),
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
347 multiplier_(multiplier)
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 // 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
350 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
351 testCounter += multiplier_ * message.payload_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
352 }));
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
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 }
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 TEST(MessageBroker, TestLambdaCaptureThisAndAccessPrivateMembers)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
359 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
360 MessageBroker broker;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
361 MyObservable observable(broker);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
362 MyObserverWithLambda* observer = new MyObserverWithLambda(broker, 3, observable);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
363
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
364 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
365 observable.EmitMessage(MyObservable::MyCustomMessage(12));
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
366 ASSERT_EQ(36, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
367
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
368 // 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
369 delete observer;
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
370
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
371 // 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
372 testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
373 observable.EmitMessage(MyObservable::MyCustomMessage(20));
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
374 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
375 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
376
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
377 #endif // C++ 11