add semicolons after wxDEFINE_EVENT()
[wxWidgets.git] / tests / events / evthandler.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/evthandler.cpp
3 // Purpose: Test the new event types and wxEvtHandler-methods
4 // Author: Peter Most
5 // Created: 2009-01-24
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Peter Most
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/event.h"
21
22 // ----------------------------------------------------------------------------
23 // test events and their handlers
24 // ----------------------------------------------------------------------------
25
26 const wxEventType LegacyEventType = wxNewEventType();
27
28 class MyEvent;
29 wxDEFINE_EVENT(MyEventType, MyEvent);
30
31 class MyEvent : public wxEvent
32 {
33 public:
34 MyEvent() : wxEvent(0, MyEventType) { }
35
36 virtual wxEvent *Clone() const { return new MyEvent; }
37 };
38
39 typedef void (wxEvtHandler::*MyEventFunction)(MyEvent&);
40 #define MyEventHandler(func) wxEVENT_HANDLER_CAST(MyEventFunction, func)
41 #define EVT_MYEVENT(func) \
42 wx__DECLARE_EVT0(MyEventType, MyEventHandler(func))
43
44 class AnotherEvent : public wxEvent
45 {
46 };
47
48 namespace
49 {
50
51 struct Called
52 {
53 Called() { Reset(); }
54
55 void Reset()
56 {
57 function =
58 functor =
59 method =
60 smethod = false;
61 }
62
63 bool function,
64 functor,
65 method,
66 smethod;
67 } g_called;
68
69 void GlobalOnMyEvent(MyEvent&)
70 {
71 g_called.function = true;
72 }
73
74 void GlobalOnAnotherEvent(AnotherEvent&);
75
76 void GlobalOnIdle(wxIdleEvent&)
77 {
78 g_called.function = true;
79 }
80
81 struct MyFunctor
82 {
83 void operator()(MyEvent &) { g_called.functor = true; }
84 };
85
86 struct IdleFunctor
87 {
88 void operator()(wxIdleEvent &) { g_called.functor = true; }
89 };
90
91 class MyHandler : public wxEvtHandler
92 {
93 public:
94 static void StaticOnMyEvent(MyEvent &) { g_called.smethod = true; }
95 static void StaticOnAnotherEvent(AnotherEvent &);
96 static void StaticOnIdle(wxIdleEvent&) { g_called.smethod = true; }
97
98 void OnMyEvent(MyEvent&) { g_called.method = true; }
99 void OnEvent(wxEvent&) { g_called.method = true; }
100 void OnAnotherEvent(AnotherEvent&);
101 void OnIdle(wxIdleEvent&) { g_called.method = true; }
102 };
103
104 // we can also handle events in classes not deriving from wxEvtHandler
105 struct MySink
106 {
107 void OnMyEvent(MyEvent&) { g_called.method = true; }
108 void OnEvent(wxEvent&) { g_called.method = true; }
109 void OnIdle(wxIdleEvent&) { g_called.method = true; }
110 };
111
112 // also test event table compilation
113 class MyClassWithEventTable : public wxEvtHandler
114 {
115 public:
116 void OnMyEvent(MyEvent&) { g_called.method = true; }
117 void OnEvent(wxEvent&) { g_called.method = true; }
118 void OnAnotherEvent(AnotherEvent&);
119 void OnIdle(wxIdleEvent&) { g_called.method = true; }
120
121 private:
122 DECLARE_EVENT_TABLE()
123 };
124
125 BEGIN_EVENT_TABLE(MyClassWithEventTable, wxEvtHandler)
126 EVT_IDLE(MyClassWithEventTable::OnIdle)
127
128 EVT_MYEVENT(MyClassWithEventTable::OnMyEvent)
129 #if !wxEVENTS_COMPATIBILITY_2_8
130 EVT_MYEVENT(MyClassWithEventTable::OnEvent)
131 #endif
132
133 // this shouldn't compile:
134 //EVT_MYEVENT(MyClassWithEventTable::OnIdle)
135 //EVT_IDLE(MyClassWithEventTable::OnAnotherEvent)
136 END_EVENT_TABLE()
137
138 } // anonymous namespace
139
140
141 // --------------------------------------------------------------------------
142 // test class
143 // --------------------------------------------------------------------------
144
145 class EvtHandlerTestCase : public CppUnit::TestCase
146 {
147 public:
148 EvtHandlerTestCase() {}
149
150 private:
151 CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
152 CPPUNIT_TEST( BuiltinConnect );
153 CPPUNIT_TEST( LegacyConnect );
154 #if !wxEVENTS_COMPATIBILITY_2_8
155 CPPUNIT_TEST( ConnectFunction );
156 CPPUNIT_TEST( ConnectStaticMethod );
157 CPPUNIT_TEST( ConnectFunctor );
158 CPPUNIT_TEST( ConnectMethod );
159 CPPUNIT_TEST( ConnectMethodUsingBaseEvent );
160 CPPUNIT_TEST( ConnectMethodWithSink );
161 CPPUNIT_TEST( ConnectNonHandler );
162 CPPUNIT_TEST( StaticConnect );
163 CPPUNIT_TEST( InvalidConnect );
164 #endif // !wxEVENTS_COMPATIBILITY_2_8
165 CPPUNIT_TEST_SUITE_END();
166
167 void BuiltinConnect();
168 void LegacyConnect();
169 #if !wxEVENTS_COMPATIBILITY_2_8
170 void ConnectFunction();
171 void ConnectStaticMethod();
172 void ConnectFunctor();
173 void ConnectMethod();
174 void ConnectMethodUsingBaseEvent();
175 void ConnectMethodWithSink();
176 void ConnectNonHandler();
177 void StaticConnect();
178 void InvalidConnect();
179 #endif // !wxEVENTS_COMPATIBILITY_2_8
180
181
182 // these member variables exceptionally don't use "m_" prefix because
183 // they're used so many times
184 MyHandler handler;
185 MyEvent e;
186
187 DECLARE_NO_COPY_CLASS(EvtHandlerTestCase)
188 };
189
190 // register in the unnamed registry so that these tests are run by default
191 CPPUNIT_TEST_SUITE_REGISTRATION( EvtHandlerTestCase );
192
193 // also include in it's own registry so that these tests can be run alone
194 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase, "EvtHandlerTestCase" );
195
196 void EvtHandlerTestCase::BuiltinConnect()
197 {
198 handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
199 handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
200
201 handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
202 handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
203
204 // using casts like this is even uglier than using wxIdleEventHandler but
205 // it should still continue to work for compatibility
206 handler.Connect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
207 handler.Disconnect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
208
209 #if !wxEVENTS_COMPATIBILITY_2_8
210 handler.Connect(wxEVT_IDLE, GlobalOnIdle);
211 handler.Disconnect(wxEVT_IDLE, GlobalOnIdle);
212
213 IdleFunctor f;
214 handler.Connect(wxEVT_IDLE, f);
215 handler.Disconnect(wxEVT_IDLE, f);
216
217 handler.Connect(wxEVT_IDLE, &MyHandler::OnIdle);
218 handler.Disconnect(wxEVT_IDLE, &MyHandler::OnIdle);
219
220 handler.Connect(wxEVT_IDLE, &MyHandler::StaticOnIdle);
221 handler.Disconnect(wxEVT_IDLE, &MyHandler::StaticOnIdle);
222 #endif // !wxEVENTS_COMPATIBILITY_2_8
223 }
224
225 void EvtHandlerTestCase::LegacyConnect()
226 {
227 handler.Connect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
228 handler.Connect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
229 handler.Connect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
230
231 handler.Disconnect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
232 handler.Disconnect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
233 handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
234
235
236 handler.Connect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
237 handler.Connect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
238 handler.Connect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
239
240 handler.Disconnect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
241 handler.Disconnect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
242 handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
243 }
244
245 #if !wxEVENTS_COMPATIBILITY_2_8
246
247 void EvtHandlerTestCase::ConnectFunction()
248 {
249 // function tests
250 handler.Connect( MyEventType, GlobalOnMyEvent );
251 g_called.Reset();
252 handler.ProcessEvent(e);
253 CPPUNIT_ASSERT( g_called.function );
254 handler.Disconnect( MyEventType, GlobalOnMyEvent );
255 g_called.Reset();
256 handler.ProcessEvent(e);
257 CPPUNIT_ASSERT( !g_called.function ); // check that it was disconnected
258
259 handler.Connect( 0, MyEventType, GlobalOnMyEvent );
260 handler.Disconnect( 0, MyEventType, GlobalOnMyEvent );
261
262 handler.Connect( 0, 0, MyEventType, GlobalOnMyEvent );
263 handler.Disconnect( 0, 0, MyEventType, GlobalOnMyEvent );
264 }
265
266 void EvtHandlerTestCase::ConnectStaticMethod()
267 {
268 // static method tests (this is same as functions but still test it just in
269 // case we hit some strange compiler bugs)
270 handler.Connect( MyEventType, &MyHandler::StaticOnMyEvent );
271 g_called.Reset();
272 handler.ProcessEvent(e);
273 CPPUNIT_ASSERT( g_called.smethod );
274 handler.Disconnect( MyEventType, &MyHandler::StaticOnMyEvent );
275 g_called.Reset();
276 handler.ProcessEvent(e);
277 CPPUNIT_ASSERT( !g_called.smethod );
278
279 handler.Connect( 0, MyEventType, &MyHandler::StaticOnMyEvent );
280 handler.Disconnect( 0, MyEventType, &MyHandler::StaticOnMyEvent );
281
282 handler.Connect( 0, 0, MyEventType, &MyHandler::StaticOnMyEvent );
283 handler.Disconnect( 0, 0, MyEventType, &MyHandler::StaticOnMyEvent );
284 }
285
286 void EvtHandlerTestCase::ConnectFunctor()
287 {
288 // generalized functor tests
289 MyFunctor functor;
290
291 handler.Connect( MyEventType, functor );
292 g_called.Reset();
293 handler.ProcessEvent(e);
294 CPPUNIT_ASSERT( g_called.functor );
295 handler.Disconnect( MyEventType, functor );
296 g_called.Reset();
297 handler.ProcessEvent(e);
298 CPPUNIT_ASSERT( !g_called.functor );
299
300 handler.Connect( 0, MyEventType, functor );
301 handler.Disconnect( 0, MyEventType, functor );
302
303 handler.Connect( 0, 0, MyEventType, functor );
304 handler.Disconnect( 0, 0, MyEventType, functor );
305 }
306
307 void EvtHandlerTestCase::ConnectMethod()
308 {
309 // class method tests
310 handler.Connect( MyEventType, &MyHandler::OnMyEvent );
311 g_called.Reset();
312 handler.ProcessEvent(e);
313 CPPUNIT_ASSERT( g_called.method );
314 handler.Disconnect( MyEventType, &MyHandler::OnMyEvent );
315 g_called.Reset();
316 handler.ProcessEvent(e);
317 CPPUNIT_ASSERT( !g_called.method );
318
319 handler.Connect( 0, MyEventType, &MyHandler::OnMyEvent );
320 handler.Disconnect( 0, MyEventType, &MyHandler::OnMyEvent );
321
322 handler.Connect( 0, 0, MyEventType, &MyHandler::OnMyEvent );
323 handler.Disconnect( 0, 0, MyEventType, &MyHandler::OnMyEvent );
324 }
325
326 void EvtHandlerTestCase::ConnectMethodUsingBaseEvent()
327 {
328 // test connecting a method taking just wxEvent and not MyEvent: this
329 // should work too if we don't need any MyEvent-specific information in the
330 // handler
331 handler.Connect( MyEventType, &MyHandler::OnEvent );
332 g_called.Reset();
333 handler.ProcessEvent(e);
334 CPPUNIT_ASSERT( g_called.method );
335 handler.Disconnect( MyEventType, &MyHandler::OnEvent );
336 g_called.Reset();
337 handler.ProcessEvent(e);
338 CPPUNIT_ASSERT( !g_called.method );
339
340 handler.Connect( 0, MyEventType, &MyHandler::OnEvent );
341 handler.Disconnect( 0, MyEventType, &MyHandler::OnEvent );
342
343 handler.Connect( 0, 0, MyEventType, &MyHandler::OnEvent );
344 handler.Disconnect( 0, 0, MyEventType, &MyHandler::OnEvent );
345 }
346
347 void EvtHandlerTestCase::ConnectMethodWithSink()
348 {
349 handler.Connect( MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
350 handler.Connect( 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
351 handler.Connect( 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
352
353 handler.Disconnect( MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
354 handler.Disconnect( 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
355 handler.Disconnect( 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
356 }
357
358 void EvtHandlerTestCase::ConnectNonHandler()
359 {
360 // class method tests for class not derived from wxEvtHandler
361 MySink sink;
362
363 handler.Connect( MyEventType, &MySink::OnMyEvent, NULL, &sink );
364 g_called.Reset();
365 handler.ProcessEvent(e);
366 CPPUNIT_ASSERT( g_called.method );
367 handler.Disconnect( MyEventType, &MySink::OnMyEvent, NULL, &sink );
368 g_called.Reset();
369 handler.ProcessEvent(e);
370 CPPUNIT_ASSERT( !g_called.method );
371 }
372
373 void EvtHandlerTestCase::StaticConnect()
374 {
375 wxEvtHandler::Connect( &handler, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
376 wxEvtHandler::Connect( &handler, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
377 wxEvtHandler::Connect( &handler, 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
378
379 wxEvtHandler::Disconnect( &handler, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
380 wxEvtHandler::Disconnect( &handler, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
381 wxEvtHandler::Disconnect( &handler, 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
382 }
383
384 void EvtHandlerTestCase::InvalidConnect()
385 {
386 // these calls shouldn't compile but we unfortunately can't check this
387 // automatically, you need to uncomment them manually and test that
388 // compilation does indeed fail
389 //handler.Connect(MyEventType, GlobalOnAnotherEvent);
390 //IdleFunctor f; handler.Connect(MyEventType, f);
391 //handler.Connect(MyEventType, &MyHandler::StaticOnAnotherEvent);
392 //handler.Connect(MyEventType, &MyHandler::OnAnotherEvent);
393 }
394
395 #endif // !wxEVENTS_COMPATIBILITY_2_8