Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 227 lines (184 sloc) 6.753 kB
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
1 //
2 // NotificationCenterTest.cpp
3 //
4 // $Id: //poco/1.4/Foundation/testsuite/src/NotificationCenterTest.cpp#1 $
5 //
6 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
7 // and Contributors.
8 //
628a06f @bufferoverflow LICENSE: add info about SPDX-License-Identifier usage and use it
bufferoverflow authored
9 // SPDX-License-Identifier: BSL-1.0
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
10 //
11
12
13 #include "NotificationCenterTest.h"
14 #include "CppUnit/TestCaller.h"
15 #include "CppUnit/TestSuite.h"
16 #include "Poco/NotificationCenter.h"
17 #include "Poco/Observer.h"
18 #include "Poco/NObserver.h"
19 #include "Poco/AutoPtr.h"
20
21
22 using Poco::NotificationCenter;
23 using Poco::Observer;
24 using Poco::NObserver;
25 using Poco::Notification;
26 using Poco::AutoPtr;
27
28
29 class TestNotification: public Notification
30 {
31 };
32
33
34 NotificationCenterTest::NotificationCenterTest(const std::string& name): CppUnit::TestCase(name)
35 {
36 }
37
38
39 NotificationCenterTest::~NotificationCenterTest()
40 {
41 }
42
43
44 void NotificationCenterTest::test1()
45 {
46 NotificationCenter nc;
47 nc.postNotification(new Notification);
48 }
49
50
51 void NotificationCenterTest::test2()
52 {
53 NotificationCenter nc;
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
54 Observer<NotificationCenterTest, Notification> o(*this, &NotificationCenterTest::handle1);
55 nc.addObserver(o);
56 assert (nc.hasObserver(o));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
57 assert (nc.hasObservers());
58 assert (nc.countObservers() == 1);
59 nc.postNotification(new Notification);
60 assert (_set.size() == 1);
61 assert (_set.find("handle1") != _set.end());
62 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
63 assert (!nc.hasObserver(o));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
64 assert (!nc.hasObservers());
65 assert (nc.countObservers() == 0);
66 }
67
68
69 void NotificationCenterTest::test3()
70 {
71 NotificationCenter nc;
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
72 Observer<NotificationCenterTest, Notification> o1(*this, &NotificationCenterTest::handle1);
73 Observer<NotificationCenterTest, Notification> o2(*this, &NotificationCenterTest::handle2);
74 nc.addObserver(o1);
75 assert (nc.hasObserver(o1));
76 nc.addObserver(o2);
77 assert (nc.hasObserver(o2));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
78 assert (nc.hasObservers());
79 assert (nc.countObservers() == 2);
80 nc.postNotification(new Notification);
81 assert (_set.size() == 2);
82 assert (_set.find("handle1") != _set.end());
83 assert (_set.find("handle2") != _set.end());
84 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
85 assert (!nc.hasObserver(o1));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
86 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle2));
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
87 assert (!nc.hasObserver(o2));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
88 assert (!nc.hasObservers());
89 assert (nc.countObservers() == 0);
90 }
91
92
93 void NotificationCenterTest::test4()
94 {
95 NotificationCenter nc;
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
96 Observer<NotificationCenterTest, Notification> o1(*this, &NotificationCenterTest::handle1);
97 Observer<NotificationCenterTest, Notification> o2(*this, &NotificationCenterTest::handle2);
98 nc.addObserver(o1);
99 assert (nc.hasObserver(o1));
100 nc.addObserver(o2);
101 assert (nc.hasObserver(o2));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
102 nc.postNotification(new Notification);
103 assert (_set.size() == 2);
104 assert (_set.find("handle1") != _set.end());
105 assert (_set.find("handle2") != _set.end());
106 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
107 assert (!nc.hasObserver(o1));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
108 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle2));
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
109 assert (!nc.hasObserver(o2));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
110 _set.clear();
111 nc.postNotification(new Notification);
112 assert (_set.empty());
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
113 Observer<NotificationCenterTest, Notification> o3(*this, &NotificationCenterTest::handle3);
114 nc.addObserver(o3);
115 assert (nc.hasObserver(o3));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
116 nc.postNotification(new Notification);
117 assert (_set.size() == 1);
118 assert (_set.find("handle3") != _set.end());
119 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle3));
369ca9c @aleks-f fixed SF# 3522906: Unregistering handlers from SocketReactor
aleks-f authored
120 assert (!nc.hasObserver(o3));
5a63907 @aleks-f change EOL to 'native'
aleks-f authored
121 }
122
123
124 void NotificationCenterTest::test5()
125 {
126 NotificationCenter nc;
127 nc.addObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
128 nc.addObserver(Observer<NotificationCenterTest, TestNotification>(*this, &NotificationCenterTest::handleTest));
129 nc.postNotification(new Notification);
130 assert (_set.size() == 1);
131 assert (_set.find("handle1") != _set.end());
132 _set.clear();
133 nc.postNotification(new TestNotification);
134 assert (_set.size() == 2);
135 assert (_set.find("handle1") != _set.end());
136 assert (_set.find("handleTest") != _set.end());
137 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
138 nc.removeObserver(Observer<NotificationCenterTest, TestNotification>(*this, &NotificationCenterTest::handleTest));
139 }
140
141
142 void NotificationCenterTest::testAuto()
143 {
144 NotificationCenter nc;
145 nc.addObserver(NObserver<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handleAuto));
146 nc.postNotification(new Notification);
147 assert (_set.size() == 1);
148 assert (_set.find("handleAuto") != _set.end());
149 nc.removeObserver(NObserver<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handleAuto));
150 }
151
152
153 void NotificationCenterTest::testDefaultCenter()
154 {
155 NotificationCenter& nc = NotificationCenter::defaultCenter();
156 nc.addObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
157 nc.postNotification(new Notification);
158 assert (_set.size() == 1);
159 assert (_set.find("handle1") != _set.end());
160 nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
161 }
162
163
164 void NotificationCenterTest::handle1(Poco::Notification* pNf)
165 {
166 poco_check_ptr (pNf);
167 AutoPtr<Notification> nf = pNf;
168 _set.insert("handle1");
169 }
170
171
172 void NotificationCenterTest::handle2(Poco::Notification* pNf)
173 {
174 poco_check_ptr (pNf);
175 AutoPtr<Notification> nf = pNf;
176 _set.insert("handle2");
177 }
178
179
180 void NotificationCenterTest::handle3(Poco::Notification* pNf)
181 {
182 poco_check_ptr (pNf);
183 AutoPtr<Notification> nf = pNf;
184 _set.insert("handle3");
185 }
186
187
188 void NotificationCenterTest::handleTest(TestNotification* pNf)
189 {
190 poco_check_ptr (pNf);
191 AutoPtr<TestNotification> nf = pNf;
192 _set.insert("handleTest");
193 }
194
195
196 void NotificationCenterTest::handleAuto(const AutoPtr<Notification>& pNf)
197 {
198 _set.insert("handleAuto");
199 }
200
201
202 void NotificationCenterTest::setUp()
203 {
204 _set.clear();
205 }
206
207
208 void NotificationCenterTest::tearDown()
209 {
210 }
211
212
213 CppUnit::Test* NotificationCenterTest::suite()
214 {
215 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NotificationCenterTest");
216
217 CppUnit_addTest(pSuite, NotificationCenterTest, test1);
218 CppUnit_addTest(pSuite, NotificationCenterTest, test2);
219 CppUnit_addTest(pSuite, NotificationCenterTest, test3);
220 CppUnit_addTest(pSuite, NotificationCenterTest, test4);
221 CppUnit_addTest(pSuite, NotificationCenterTest, test5);
222 CppUnit_addTest(pSuite, NotificationCenterTest, testAuto);
223 CppUnit_addTest(pSuite, NotificationCenterTest, testDefaultCenter);
224
225 return pSuite;
226 }
Something went wrong with that request. Please try again.