4
// $Id: //poco/1.2/Foundation/testsuite/src/BasicEventTest.cpp#2 $
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "BasicEventTest.h"
34
#include "DummyDelegate.h"
35
#include "CppUnit/TestCaller.h"
36
#include "CppUnit/TestSuite.h"
37
#include "Poco/Delegate.h"
38
#include "Poco/Expire.h"
39
#include "Poco/Thread.h"
40
#include "Poco/Exception.h"
49
BasicEventTest::BasicEventTest(const std::string& name): CppUnit::TestCase(name)
54
BasicEventTest::~BasicEventTest()
58
void BasicEventTest::testNoDelegate()
64
Simple.notify(this, tmp);
67
Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
68
Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
69
Simple.notify(this, tmp);
72
ConstSimple += Delegate<BasicEventTest, const int>(this, &BasicEventTest::onConstSimple);
73
ConstSimple -= Delegate<BasicEventTest, const int>(this, &BasicEventTest::onConstSimple);
74
ConstSimple.notify(this, tmp);
77
//Note: passing &args will not work due to &
78
EventArgs* pArgs = &args;
79
Complex += Delegate<BasicEventTest, Poco::EventArgs*>(this, &BasicEventTest::onComplex);
80
Complex -= Delegate<BasicEventTest, Poco::EventArgs*>(this, &BasicEventTest::onComplex);
81
Complex.notify(this, pArgs);
84
Complex2 += Delegate<BasicEventTest, Poco::EventArgs>(this, &BasicEventTest::onComplex2);
85
Complex2 -= Delegate<BasicEventTest, Poco::EventArgs>(this, &BasicEventTest::onComplex2);
86
Complex2.notify(this, args);
89
const EventArgs* pCArgs = &args;
90
ConstComplex += Delegate<BasicEventTest, const Poco::EventArgs*>(this, &BasicEventTest::onConstComplex);
91
ConstComplex -= Delegate<BasicEventTest, const Poco::EventArgs*>(this, &BasicEventTest::onConstComplex);
92
ConstComplex.notify(this, pCArgs);
95
Const2Complex += Delegate<BasicEventTest, const Poco::EventArgs* const>(this, &BasicEventTest::onConst2Complex);
96
Const2Complex -= Delegate<BasicEventTest, const Poco::EventArgs* const>(this, &BasicEventTest::onConst2Complex);
97
Const2Complex.notify(this, pArgs);
101
void BasicEventTest::testSingleDelegate()
106
assert (_count == 0);
108
Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
109
Simple.notify(this, tmp);
110
assert (_count == 1);
112
ConstSimple += Delegate<BasicEventTest, const int>(this, &BasicEventTest::onConstSimple);
113
ConstSimple.notify(this, tmp);
114
assert (_count == 2);
116
EventArgs* pArgs = &args;
117
Complex += Delegate<BasicEventTest, Poco::EventArgs*>(this, &BasicEventTest::onComplex);
118
Complex.notify(this, pArgs);
119
assert (_count == 3);
121
Complex2 += Delegate<BasicEventTest, Poco::EventArgs>(this, &BasicEventTest::onComplex2);
122
Complex2.notify(this, args);
123
assert (_count == 4);
125
const EventArgs* pCArgs = &args;
126
ConstComplex += Delegate<BasicEventTest, const Poco::EventArgs*>(this, &BasicEventTest::onConstComplex);
127
ConstComplex.notify(this, pCArgs);
128
assert (_count == 5);
130
Const2Complex += Delegate<BasicEventTest, const Poco::EventArgs* const>(this, &BasicEventTest::onConst2Complex);
131
Const2Complex.notify(this, pArgs);
132
assert (_count == 6);
133
// check if 2nd notify also works
134
Const2Complex.notify(this, pArgs);
135
assert (_count == 7);
139
void BasicEventTest::testDuplicateRegister()
143
assert (_count == 0);
145
Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
146
Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
147
Simple.notify(this, tmp);
148
assert (_count == 1);
149
Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
150
Simple.notify(this, tmp);
151
assert (_count == 1);
154
void BasicEventTest::testDuplicateUnregister()
156
// duplicate unregister shouldn't give an error,
159
assert (_count == 0);
161
Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple); // should work
162
Simple.notify(this, tmp);
163
assert (_count == 0);
165
Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
166
Simple.notify(this, tmp);
167
assert (_count == 1);
169
Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
170
Simple.notify(this, tmp);
171
assert (_count == 1);
173
Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
174
Simple.notify(this, tmp);
175
assert (_count == 1);
178
void BasicEventTest::testDisabling()
182
assert (_count == 0);
184
Simple += Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
186
Simple.notify(this, tmp);
187
assert (_count == 0);
189
Simple.notify(this, tmp);
190
assert (_count == 1);
192
// unregister should also work with disabled event
194
Simple -= Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple);
196
Simple.notify(this, tmp);
197
assert (_count == 1);
200
void BasicEventTest::testExpire()
204
assert (_count == 0);
206
Simple += Expire<int>(Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple), 500);
207
Simple.notify(this, tmp);
208
assert (_count == 1);
209
Poco::Thread::sleep(700);
210
Simple.notify(this, tmp);
211
assert (_count == 1);
214
void BasicEventTest::testExpireReRegister()
218
assert (_count == 0);
220
Simple += Expire<int>(Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple), 500);
221
Simple.notify(this, tmp);
222
assert (_count == 1);
223
Poco::Thread::sleep(200);
224
Simple.notify(this, tmp);
225
assert (_count == 2);
226
// renew registration
227
Simple += Expire<int>(Delegate<BasicEventTest, int>(this, &BasicEventTest::onSimple), 600);
228
Poco::Thread::sleep(400);
229
Simple.notify(this, tmp);
230
assert (_count == 3);
231
Poco::Thread::sleep(300);
232
Simple.notify(this, tmp);
233
assert (_count == 3);
236
void BasicEventTest::testReturnParams()
239
Simple += Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple);
242
Simple.notify(this, tmp);
246
void BasicEventTest::testOverwriteDelegate()
249
Simple += Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple2);
250
// o1 can only have one entry, thus the next line will replace the entry
251
Simple += Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple);
253
int tmp = 0; // onsimple requires 0 as input
254
Simple.notify(this, tmp);
256
// now overwrite with onsimple2 with requires as input tmp = 1
257
Simple += Expire<int>(Delegate<DummyDelegate, int>(&o1, &DummyDelegate::onSimple2), 23000);
258
Simple.notify(this, tmp);
262
void BasicEventTest::testAsyncNotify()
264
Poco::BasicEvent<int>* pSimple= new Poco::BasicEvent<int>();
265
(*pSimple) += Delegate<BasicEventTest, int>(this, &BasicEventTest::onAsync);
266
assert (_count == 0);
268
Poco::ActiveResult<int>retArg = pSimple->notifyAsync(this, tmp);
269
delete pSimple; // must work even when the event got deleted!
271
assert (_count == 0);
273
assert (retArg.data() == tmp);
274
assert (_count == LARGEINC);
277
void BasicEventTest::onSimple(const void* pSender, int& i)
282
void BasicEventTest::onSimpleOther(const void* pSender, int& i)
287
void BasicEventTest::onConstSimple(const void* pSender, const int& i)
292
void BasicEventTest::onComplex(const void* pSender, Poco::EventArgs* & i)
297
void BasicEventTest::onComplex2(const void* pSender, Poco::EventArgs & i)
302
void BasicEventTest::onConstComplex(const void* pSender, const Poco::EventArgs*& i)
307
void BasicEventTest::onConst2Complex(const void* pSender, const Poco::EventArgs * const & i)
312
void BasicEventTest::onAsync(const void* pSender, int& i)
314
Poco::Thread::sleep(700);
318
int BasicEventTest::getCount() const
323
void BasicEventTest::setUp()
326
// must clear events, otherwise repeating test executions will fail
327
// because tests are only created once, only setup is called before
333
ConstComplex.clear();
334
Const2Complex.clear();
338
void BasicEventTest::tearDown()
343
CppUnit::Test* BasicEventTest::suite()
345
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("BasicEventTest");
347
CppUnit_addTest(pSuite, BasicEventTest, testNoDelegate);
348
CppUnit_addTest(pSuite, BasicEventTest, testSingleDelegate);
349
CppUnit_addTest(pSuite, BasicEventTest, testReturnParams);
350
CppUnit_addTest(pSuite, BasicEventTest, testDuplicateRegister);
351
CppUnit_addTest(pSuite, BasicEventTest, testDuplicateUnregister);
352
CppUnit_addTest(pSuite, BasicEventTest, testDisabling);
353
CppUnit_addTest(pSuite, BasicEventTest, testExpire);
354
CppUnit_addTest(pSuite, BasicEventTest, testExpireReRegister);
355
CppUnit_addTest(pSuite, BasicEventTest, testOverwriteDelegate);
356
CppUnit_addTest(pSuite, BasicEventTest, testAsyncNotify);