annotate UnitTestsSources/TestMessageBroker.cpp @ 1870:3889ae96d2e9

added copyright UCLouvain
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Jan 2022 11:04:09 +0100
parents 9ac2a65d4172
children 7053b8a0aaec
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
1739
9ac2a65d4172 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
1870
3889ae96d2e9 added copyright UCLouvain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1739
diff changeset
6 * Copyright (C) 2021-2021 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
7 *
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
8 * 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
9 * 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
10 * 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
11 * 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
12 *
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
13 * 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
14 * 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
15 * 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
16 * Affero General Public License for more details.
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
17 *
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
18 * 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
19 * 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
20 **/
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
21
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
22
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1279
diff changeset
23 #include <gtest/gtest.h>
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
24
1587
a1405ab3a91c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
25 #include "../OrthancStone/Sources/Messages/IObservable.h"
a1405ab3a91c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
26 #include "../OrthancStone/Sources/Messages/ObserverBase.h"
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
27
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
28
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
29 int testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
30 namespace {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
31
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
32 using namespace OrthancStone;
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
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
35 class MyObservable : public IObservable
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 public:
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
38 struct MyCustomMessage : public IMessage
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
39 {
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
40 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
41
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
42 int payload_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
43
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
44 MyCustomMessage(int payload) :
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
45 payload_(payload)
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
46 {
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
47 }
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
48 };
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
49 };
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
50
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
51 class MyObserver : public ObserverBase<MyObserver>
428
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 public:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
54 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
55 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
56 testCounter += message.payload_;
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 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
60
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
61
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
62 TEST(MessageBroker, TestPermanentConnectionSimpleUseCase)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
63 {
1066
b537002f83a9 removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1057
diff changeset
64 MyObservable observable;
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
65 boost::shared_ptr<MyObserver> observer(new MyObserver);
428
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 // create a permanent connection between an observable and an observer
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
68 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage);
428
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 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
71 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
72 ASSERT_EQ(12, testCounter);
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 // 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
75 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
76 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
77 ASSERT_EQ(20, testCounter);
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 // Unregister the observer; make sure it's not called anymore
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
80 observer.reset();
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
81 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
82 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
83 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
84 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
85
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
86 TEST(MessageBroker, TestPermanentConnectionDeleteObserver)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
87 {
1066
b537002f83a9 removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1057
diff changeset
88 MyObservable observable;
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
89 boost::shared_ptr<MyObserver> observer(new MyObserver);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
90
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
91 // create a permanent connection between an observable and an observer
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
92 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage);
267
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 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
95 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
96 ASSERT_EQ(12, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
97
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
98 // delete the observer and check that the callback is not called anymore
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
99 observer.reset();
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
100
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
101 // 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
102 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
103 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
104 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
105 }