Mercurial > hg > orthanc-stone
annotate UnitTestsSources/TestMessageBroker.cpp @ 1199:922d2e61aa5d
RadiograpyScene: can now remove any layer + new key wrappers for Delete/Backspace
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 28 Nov 2019 18:28:15 +0100 |
parents | 287ec78f63b4 |
children | f72d1ab42932 2d8ab34c8c91 |
rev | line source |
---|---|
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
1 /** |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
2 * Stone of Orthanc |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
440 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
6 * |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
10 * the License, or (at your option) any later version. |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
11 * |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
15 * Affero General Public License for more details. |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
16 * |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
19 **/ |
251 | 20 |
21 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
22 #include "gtest/gtest.h" |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
23 |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
1057
diff
changeset
|
24 #include "../Framework/Messages/MessageBroker.h" |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
1057
diff
changeset
|
25 #include "../Framework/Messages/IObservable.h" |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
1057
diff
changeset
|
26 #include "../Framework/Messages/IObserver.h" |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
1057
diff
changeset
|
27 #include "../Framework/Messages/MessageForwarder.h" |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
28 |
251 | 29 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
30 int testCounter = 0; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
31 namespace { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
32 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
33 using namespace OrthancStone; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
34 |
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 class MyObservable : public IObservable |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
37 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
38 public: |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
39 struct MyCustomMessage : public IMessage |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
40 { |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
41 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__); |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
42 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
43 int payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
44 |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
45 MyCustomMessage(int payload) : |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
46 payload_(payload) |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
47 { |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
48 } |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
49 }; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
50 |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
51 MyObservable(MessageBroker& broker) : |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
52 IObservable(broker) |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
53 { |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
624
diff
changeset
|
54 } |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
55 }; |
267 | 56 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
57 class MyObserver : public IObserver |
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 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
60 MyObserver(MessageBroker& broker) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
61 : IObserver(broker) |
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 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
64 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
65 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
66 testCounter += message.payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
67 } |
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 class MyIntermediate : public IObserver, public IObservable |
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 IObservable& observedObject_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
75 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
76 MyIntermediate(MessageBroker& broker, IObservable& observedObject) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
77 : IObserver(broker), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
78 IObservable(broker), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
79 observedObject_(observedObject) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
80 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
81 observedObject_.RegisterObserverCallback(new MessageForwarder<MyObservable::MyCustomMessage>(broker, *this)); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
82 } |
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 } |
251 | 85 |
86 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
87 TEST(MessageBroker, TestPermanentConnectionSimpleUseCase) |
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 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
90 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
91 MyObserver observer(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
92 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
93 // 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
|
94 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
|
95 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
96 testCounter = 0; |
624 | 97 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
98 ASSERT_EQ(12, testCounter); |
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 // the connection is permanent; if we emit the same message again, the observer will be notified again |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
101 testCounter = 0; |
624 | 102 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
103 ASSERT_EQ(20, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
104 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
105 // 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
|
106 observable.Unregister(&observer); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
107 testCounter = 0; |
624 | 108 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
109 ASSERT_EQ(0, testCounter); |
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 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
112 TEST(MessageBroker, TestMessageForwarderSimpleUseCase) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
113 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
114 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
115 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
116 MyIntermediate intermediate(broker, observable); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
117 MyObserver observer(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
118 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
119 // 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
|
120 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
|
121 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
122 testCounter = 0; |
624 | 123 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
124 ASSERT_EQ(12, testCounter); |
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 // 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
|
127 testCounter = 0; |
624 | 128 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
129 ASSERT_EQ(20, testCounter); |
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 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
132 TEST(MessageBroker, TestPermanentConnectionDeleteObserver) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
133 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
134 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
135 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
136 MyObserver* observer = new MyObserver(broker); |
251 | 137 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
138 // 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
|
139 observable.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(*observer, &MyObserver::HandleCompletedMessage)); |
267 | 140 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
141 testCounter = 0; |
624 | 142 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
143 ASSERT_EQ(12, testCounter); |
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 // 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
|
146 delete observer; |
267 | 147 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
148 // 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
|
149 testCounter = 0; |
624 | 150 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
151 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
152 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
153 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
154 TEST(MessageBroker, TestMessageForwarderDeleteIntermediate) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
155 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
156 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
157 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
158 MyIntermediate* intermediate = new MyIntermediate(broker, observable); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
159 MyObserver observer(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
160 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
161 // 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
|
162 intermediate->RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage)); |
267 | 163 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
164 testCounter = 0; |
624 | 165 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
166 ASSERT_EQ(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
167 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
168 delete intermediate; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
169 |
624 | 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(12, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
172 } |
251 | 173 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
174 TEST(MessageBroker, TestCustomMessage) |
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 | 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 | 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 } |
251 | 193 |
194 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
456
diff
changeset
|
195 #if 0 /* __cplusplus >= 201103L*/ |
251 | 196 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
197 TEST(MessageBroker, TestLambdaSimpleUseCase) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
198 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
199 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
200 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
201 MyObserver* observer = new MyObserver(broker); |
251 | 202 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
203 // 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
|
204 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
|
205 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
206 testCounter = 0; |
624 | 207 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
208 ASSERT_EQ(24, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
209 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
210 // 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
|
211 delete observer; |
251 | 212 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
213 // 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
|
214 testCounter = 0; |
624 | 215 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
216 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
217 } |
251 | 218 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
219 namespace { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
220 class MyObserverWithLambda : public IObserver { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
221 private: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
222 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
|
223 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
224 public: |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
225 MyObserverWithLambda(MessageBroker& broker, int multiplier, MyObservable& observable) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
226 : IObserver(broker), |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
227 multiplier_(multiplier) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
228 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
229 // 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
|
230 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
|
231 testCounter += multiplier_ * message.payload_; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
232 })); |
251 | 233 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
234 } |
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 } |
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 TEST(MessageBroker, TestLambdaCaptureThisAndAccessPrivateMembers) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
239 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
240 MessageBroker broker; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
241 MyObservable observable(broker); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
242 MyObserverWithLambda* observer = new MyObserverWithLambda(broker, 3, observable); |
251 | 243 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
244 testCounter = 0; |
624 | 245 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
246 ASSERT_EQ(36, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
247 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
248 // 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
|
249 delete observer; |
251 | 250 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
251 // 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
|
252 testCounter = 0; |
624 | 253 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
254 ASSERT_EQ(0, testCounter); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
255 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
256 |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
257 #endif // C++ 11 |