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