]> git.saurik.com Git - wxWidgets.git/blame - tests/events/evthandler.cpp
Fix event handling order in doc/view framework.
[wxWidgets.git] / tests / events / evthandler.cpp
CommitLineData
b2238cc3
VZ
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
b2238cc3
VZ
20#include "wx/event.h"
21
f3ff831f
VZ
22// ----------------------------------------------------------------------------
23// test events and their handlers
24// ----------------------------------------------------------------------------
b2238cc3 25
a01ada05 26const wxEventType LegacyEventType = wxNewEventType();
b2238cc3 27
9b3ff3c0 28class MyEvent;
16be58c1 29wxDEFINE_EVENT(MyEventType, MyEvent);
9b3ff3c0 30
b2238cc3
VZ
31class MyEvent : public wxEvent
32{
9b3ff3c0 33public:
a01ada05 34 MyEvent() : wxEvent(0, MyEventType) { }
9b3ff3c0
VZ
35
36 virtual wxEvent *Clone() const { return new MyEvent; }
b2238cc3
VZ
37};
38
bb87b19b 39typedef void (wxEvtHandler::*MyEventFunction)(MyEvent&);
890d70eb 40#ifndef wxHAS_EVENT_BIND
5293e4b7
VZ
41 #define MyEventHandler(func) wxEVENT_HANDLER_CAST(MyEventFunction, func)
42#else
43 #define MyEventHandler(func) &func
44#endif
bb87b19b
VZ
45#define EVT_MYEVENT(func) \
46 wx__DECLARE_EVT0(MyEventType, MyEventHandler(func))
a01ada05 47
9b3ff3c0
VZ
48class AnotherEvent : public wxEvent
49{
50};
b2238cc3 51
9b3ff3c0
VZ
52namespace
53{
54
55struct Called
56{
57 Called() { Reset(); }
58
59 void Reset()
60 {
61 function =
62 functor =
63 method =
64 smethod = false;
65 }
66
67 bool function,
68 functor,
69 method,
70 smethod;
71} g_called;
72
73void GlobalOnMyEvent(MyEvent&)
74{
75 g_called.function = true;
76}
b2238cc3 77
890d70eb
VZ
78void GlobalOnEvent(wxEvent&)
79{
80 g_called.function = true;
81}
82
f3ff831f
VZ
83void GlobalOnAnotherEvent(AnotherEvent&);
84
85void GlobalOnIdle(wxIdleEvent&)
86{
87 g_called.function = true;
88}
89
90struct MyFunctor
b2238cc3 91{
9b3ff3c0 92 void operator()(MyEvent &) { g_called.functor = true; }
f3ff831f 93};
b2238cc3 94
f3ff831f
VZ
95struct IdleFunctor
96{
97 void operator()(wxIdleEvent &) { g_called.functor = true; }
b2238cc3
VZ
98};
99
b2238cc3
VZ
100class MyHandler : public wxEvtHandler
101{
9b3ff3c0 102public:
f3ff831f
VZ
103 static void StaticOnMyEvent(MyEvent &) { g_called.smethod = true; }
104 static void StaticOnAnotherEvent(AnotherEvent &);
105 static void StaticOnIdle(wxIdleEvent&) { g_called.smethod = true; }
106
9b3ff3c0 107 void OnMyEvent(MyEvent&) { g_called.method = true; }
f3ff831f
VZ
108 void OnEvent(wxEvent&) { g_called.method = true; }
109 void OnAnotherEvent(AnotherEvent&);
110 void OnIdle(wxIdleEvent&) { g_called.method = true; }
111};
b2238cc3 112
f3ff831f
VZ
113// we can also handle events in classes not deriving from wxEvtHandler
114struct MySink
115{
116 void OnMyEvent(MyEvent&) { g_called.method = true; }
a01ada05 117 void OnEvent(wxEvent&) { g_called.method = true; }
f3ff831f
VZ
118 void OnIdle(wxIdleEvent&) { g_called.method = true; }
119};
b2238cc3 120
f3ff831f
VZ
121// also test event table compilation
122class MyClassWithEventTable : public wxEvtHandler
123{
124public:
125 void OnMyEvent(MyEvent&) { g_called.method = true; }
126 void OnEvent(wxEvent&) { g_called.method = true; }
127 void OnAnotherEvent(AnotherEvent&);
128 void OnIdle(wxIdleEvent&) { g_called.method = true; }
129
130private:
131 DECLARE_EVENT_TABLE()
b2238cc3
VZ
132};
133
f3ff831f
VZ
134BEGIN_EVENT_TABLE(MyClassWithEventTable, wxEvtHandler)
135 EVT_IDLE(MyClassWithEventTable::OnIdle)
136
a01ada05 137 EVT_MYEVENT(MyClassWithEventTable::OnMyEvent)
890d70eb 138#ifdef wxHAS_EVENT_BIND
a01ada05 139 EVT_MYEVENT(MyClassWithEventTable::OnEvent)
bb87b19b 140#endif
a01ada05 141
f3ff831f 142 // this shouldn't compile:
a01ada05 143 //EVT_MYEVENT(MyClassWithEventTable::OnIdle)
f3ff831f
VZ
144 //EVT_IDLE(MyClassWithEventTable::OnAnotherEvent)
145END_EVENT_TABLE()
146
9b3ff3c0
VZ
147} // anonymous namespace
148
f3ff831f
VZ
149
150// --------------------------------------------------------------------------
151// test class
152// --------------------------------------------------------------------------
153
154class EvtHandlerTestCase : public CppUnit::TestCase
b2238cc3 155{
f3ff831f
VZ
156public:
157 EvtHandlerTestCase() {}
158
159private:
160 CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
161 CPPUNIT_TEST( BuiltinConnect );
162 CPPUNIT_TEST( LegacyConnect );
75b2220e
VZ
163 CPPUNIT_TEST( DisconnectWildcard );
164 CPPUNIT_TEST( AutoDisconnect );
890d70eb 165#ifdef wxHAS_EVENT_BIND
5293e4b7
VZ
166 CPPUNIT_TEST( BindFunction );
167 CPPUNIT_TEST( BindStaticMethod );
168 CPPUNIT_TEST( BindFunctor );
169 CPPUNIT_TEST( BindMethod );
170 CPPUNIT_TEST( BindMethodUsingBaseEvent );
890d70eb 171 CPPUNIT_TEST( BindFunctionUsingBaseEvent );
5293e4b7
VZ
172 CPPUNIT_TEST( BindNonHandler );
173 CPPUNIT_TEST( InvalidBind );
890d70eb 174#endif // wxHAS_EVENT_BIND
f3ff831f
VZ
175 CPPUNIT_TEST_SUITE_END();
176
177 void BuiltinConnect();
178 void LegacyConnect();
75b2220e
VZ
179 void DisconnectWildcard();
180 void AutoDisconnect();
890d70eb 181#ifdef wxHAS_EVENT_BIND
5293e4b7
VZ
182 void BindFunction();
183 void BindStaticMethod();
184 void BindFunctor();
185 void BindMethod();
186 void BindMethodUsingBaseEvent();
890d70eb 187 void BindFunctionUsingBaseEvent();
5293e4b7
VZ
188 void BindNonHandler();
189 void InvalidBind();
890d70eb 190#endif // wxHAS_EVENT_BIND
f3ff831f 191
b2238cc3 192
f3ff831f
VZ
193 // these member variables exceptionally don't use "m_" prefix because
194 // they're used so many times
b2238cc3 195 MyHandler handler;
9b3ff3c0 196 MyEvent e;
b2238cc3 197
f3ff831f
VZ
198 DECLARE_NO_COPY_CLASS(EvtHandlerTestCase)
199};
b2238cc3 200
f3ff831f
VZ
201// register in the unnamed registry so that these tests are run by default
202CPPUNIT_TEST_SUITE_REGISTRATION( EvtHandlerTestCase );
203
e3778b4d 204// also include in its own registry so that these tests can be run alone
f3ff831f
VZ
205CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase, "EvtHandlerTestCase" );
206
207void EvtHandlerTestCase::BuiltinConnect()
208{
209 handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
210 handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
211
212 handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
213 handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
b2238cc3 214
a01ada05
VZ
215 // using casts like this is even uglier than using wxIdleEventHandler but
216 // it should still continue to work for compatibility
217 handler.Connect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
218 handler.Disconnect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
219
890d70eb 220#ifdef wxHAS_EVENT_BIND
5293e4b7
VZ
221 handler.Bind(wxEVT_IDLE, GlobalOnIdle);
222 handler.Unbind(wxEVT_IDLE, GlobalOnIdle);
b2238cc3 223
f3ff831f 224 IdleFunctor f;
5293e4b7
VZ
225 handler.Bind(wxEVT_IDLE, f);
226 handler.Unbind(wxEVT_IDLE, f);
b2238cc3 227
5293e4b7
VZ
228 handler.Bind(wxEVT_IDLE, &MyHandler::OnIdle, &handler);
229 handler.Unbind(wxEVT_IDLE, &MyHandler::OnIdle, &handler);
f3ff831f 230
5293e4b7
VZ
231 handler.Bind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
232 handler.Unbind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
890d70eb 233#endif // wxHAS_EVENT_BIND
f3ff831f
VZ
234}
235
236void EvtHandlerTestCase::LegacyConnect()
237{
a01ada05
VZ
238 handler.Connect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
239 handler.Connect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
240 handler.Connect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
b2238cc3 241
a01ada05
VZ
242 handler.Disconnect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
243 handler.Disconnect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
244 handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
b2238cc3 245
f3ff831f 246
a01ada05
VZ
247 handler.Connect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
248 handler.Connect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
249 handler.Connect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
f3ff831f 250
a01ada05
VZ
251 handler.Disconnect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
252 handler.Disconnect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
253 handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
f3ff831f 254}
b2238cc3 255
75b2220e
VZ
256void EvtHandlerTestCase::DisconnectWildcard()
257{
258 // should be able to disconnect a different handler using "wildcard search"
259 MyHandler sink;
260 wxEvtHandler source;
261 source.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &sink);
262 CPPUNIT_ASSERT(source.Disconnect(wxID_ANY, wxEVT_IDLE));
263 // destruction of source and sink here should properly clean up the
264 // wxEventConnectionRef without crashing
265}
266
267void EvtHandlerTestCase::AutoDisconnect()
268{
269 wxEvtHandler source;
270 {
271 MyHandler sink;
272 source.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &sink);
273 // mismatched event type, so nothing should be disconnected
274 CPPUNIT_ASSERT(!source.Disconnect(wxEVT_THREAD, wxIdleEventHandler(MyHandler::OnIdle), NULL, &sink));
275 }
276 // destruction of sink should have automatically disconnected it, so
277 // there should be nothing to disconnect anymore
278 CPPUNIT_ASSERT(!source.Disconnect(wxID_ANY, wxEVT_IDLE));
279}
280
890d70eb 281#ifdef wxHAS_EVENT_BIND
f3ff831f 282
5293e4b7 283void EvtHandlerTestCase::BindFunction()
f3ff831f
VZ
284{
285 // function tests
5293e4b7 286 handler.Bind( MyEventType, GlobalOnMyEvent );
f3ff831f
VZ
287 g_called.Reset();
288 handler.ProcessEvent(e);
289 CPPUNIT_ASSERT( g_called.function );
5293e4b7 290 handler.Unbind( MyEventType, GlobalOnMyEvent );
f3ff831f
VZ
291 g_called.Reset();
292 handler.ProcessEvent(e);
293 CPPUNIT_ASSERT( !g_called.function ); // check that it was disconnected
294
5293e4b7
VZ
295 handler.Bind( MyEventType, GlobalOnMyEvent, 0 );
296 handler.Unbind( MyEventType, GlobalOnMyEvent, 0 );
f3ff831f 297
5293e4b7
VZ
298 handler.Bind( MyEventType, GlobalOnMyEvent, 0, 0 );
299 handler.Unbind( MyEventType, GlobalOnMyEvent, 0, 0 );
f3ff831f
VZ
300}
301
5293e4b7 302void EvtHandlerTestCase::BindStaticMethod()
f3ff831f
VZ
303{
304 // static method tests (this is same as functions but still test it just in
305 // case we hit some strange compiler bugs)
5293e4b7 306 handler.Bind( MyEventType, &MyHandler::StaticOnMyEvent );
9b3ff3c0
VZ
307 g_called.Reset();
308 handler.ProcessEvent(e);
309 CPPUNIT_ASSERT( g_called.smethod );
5293e4b7 310 handler.Unbind( MyEventType, &MyHandler::StaticOnMyEvent );
9b3ff3c0
VZ
311 g_called.Reset();
312 handler.ProcessEvent(e);
f3ff831f 313 CPPUNIT_ASSERT( !g_called.smethod );
b2238cc3 314
5293e4b7
VZ
315 handler.Bind( MyEventType, &MyHandler::StaticOnMyEvent, 0 );
316 handler.Unbind( MyEventType, &MyHandler::StaticOnMyEvent, 0 );
b2238cc3 317
5293e4b7
VZ
318 handler.Bind( MyEventType, &MyHandler::StaticOnMyEvent, 0, 0 );
319 handler.Unbind( MyEventType, &MyHandler::StaticOnMyEvent, 0, 0 );
f3ff831f 320}
b2238cc3 321
5293e4b7 322void EvtHandlerTestCase::BindFunctor()
f3ff831f
VZ
323{
324 // generalized functor tests
325 MyFunctor functor;
b2238cc3 326
5293e4b7 327 handler.Bind( MyEventType, functor );
f3ff831f
VZ
328 g_called.Reset();
329 handler.ProcessEvent(e);
330 CPPUNIT_ASSERT( g_called.functor );
5293e4b7 331 handler.Unbind( MyEventType, functor );
f3ff831f
VZ
332 g_called.Reset();
333 handler.ProcessEvent(e);
334 CPPUNIT_ASSERT( !g_called.functor );
b2238cc3 335
5293e4b7
VZ
336 handler.Bind( MyEventType, functor, 0 );
337 handler.Unbind( MyEventType, functor, 0 );
f3ff831f 338
5293e4b7
VZ
339 handler.Bind( MyEventType, functor, 0, 0 );
340 handler.Unbind( MyEventType, functor, 0, 0 );
ca6911c3 341
09be3364
VZ
342 // test that a temporary functor is working as well and also test that
343 // unbinding a different (though equal) instance of the same functor does
344 // not work
345 MyFunctor func;
ca6911c3 346 handler.Bind( MyEventType, MyFunctor() );
09be3364 347 CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func ));
ca6911c3
VZ
348
349 handler.Bind( MyEventType, MyFunctor(), 0 );
09be3364 350 CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func, 0 ));
ca6911c3
VZ
351
352 handler.Bind( MyEventType, MyFunctor(), 0, 0 );
09be3364 353 CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func, 0, 0 ));
f3ff831f 354}
b2238cc3 355
5293e4b7 356void EvtHandlerTestCase::BindMethod()
f3ff831f
VZ
357{
358 // class method tests
5293e4b7 359 handler.Bind( MyEventType, &MyHandler::OnMyEvent, &handler );
a01ada05
VZ
360 g_called.Reset();
361 handler.ProcessEvent(e);
362 CPPUNIT_ASSERT( g_called.method );
5293e4b7 363 handler.Unbind( MyEventType, &MyHandler::OnMyEvent, &handler );
a01ada05
VZ
364 g_called.Reset();
365 handler.ProcessEvent(e);
366 CPPUNIT_ASSERT( !g_called.method );
367
5293e4b7
VZ
368 handler.Bind( MyEventType, &MyHandler::OnMyEvent, &handler, 0 );
369 handler.Unbind( MyEventType, &MyHandler::OnMyEvent, &handler, 0 );
a01ada05 370
5293e4b7
VZ
371 handler.Bind( MyEventType, &MyHandler::OnMyEvent, &handler, 0, 0 );
372 handler.Unbind( MyEventType, &MyHandler::OnMyEvent, &handler, 0, 0 );
a01ada05
VZ
373}
374
5293e4b7 375void EvtHandlerTestCase::BindMethodUsingBaseEvent()
a01ada05
VZ
376{
377 // test connecting a method taking just wxEvent and not MyEvent: this
378 // should work too if we don't need any MyEvent-specific information in the
379 // handler
5293e4b7 380 handler.Bind( MyEventType, &MyHandler::OnEvent, &handler );
f3ff831f
VZ
381 g_called.Reset();
382 handler.ProcessEvent(e);
383 CPPUNIT_ASSERT( g_called.method );
5293e4b7 384 handler.Unbind( MyEventType, &MyHandler::OnEvent, &handler );
f3ff831f
VZ
385 g_called.Reset();
386 handler.ProcessEvent(e);
387 CPPUNIT_ASSERT( !g_called.method );
b2238cc3 388
5293e4b7
VZ
389 handler.Bind( MyEventType, &MyHandler::OnEvent, &handler, 0 );
390 handler.Unbind( MyEventType, &MyHandler::OnEvent, &handler, 0 );
b2238cc3 391
5293e4b7
VZ
392 handler.Bind( MyEventType, &MyHandler::OnEvent, &handler, 0, 0 );
393 handler.Unbind( MyEventType, &MyHandler::OnEvent, &handler, 0, 0 );
f3ff831f 394}
b2238cc3 395
f3ff831f 396
890d70eb
VZ
397void EvtHandlerTestCase::BindFunctionUsingBaseEvent()
398{
399 // test connecting a function taking just wxEvent and not MyEvent: this
400 // should work too if we don't need any MyEvent-specific information in the
401 // handler
402 handler.Bind( MyEventType, GlobalOnEvent );
403 g_called.Reset();
404 handler.ProcessEvent(e);
405 CPPUNIT_ASSERT( g_called.function );
406 handler.Unbind( MyEventType, GlobalOnEvent );
407 g_called.Reset();
408 handler.ProcessEvent(e);
409 CPPUNIT_ASSERT( !g_called.function );
410
411 handler.Bind( MyEventType, GlobalOnEvent, 0 );
412 handler.Unbind( MyEventType, GlobalOnEvent, 0 );
413
414 handler.Bind( MyEventType, GlobalOnEvent, 0, 0 );
415 handler.Unbind( MyEventType, GlobalOnEvent, 0, 0 );
416}
417
418
419
5293e4b7 420void EvtHandlerTestCase::BindNonHandler()
f3ff831f
VZ
421{
422 // class method tests for class not derived from wxEvtHandler
423 MySink sink;
b2238cc3 424
5293e4b7 425 handler.Bind( MyEventType, &MySink::OnMyEvent, &sink );
f3ff831f
VZ
426 g_called.Reset();
427 handler.ProcessEvent(e);
428 CPPUNIT_ASSERT( g_called.method );
5293e4b7 429 handler.Unbind( MyEventType, &MySink::OnMyEvent, &sink );
f3ff831f
VZ
430 g_called.Reset();
431 handler.ProcessEvent(e);
432 CPPUNIT_ASSERT( !g_called.method );
433}
b2238cc3 434
5293e4b7 435void EvtHandlerTestCase::InvalidBind()
f3ff831f 436{
9b3ff3c0
VZ
437 // these calls shouldn't compile but we unfortunately can't check this
438 // automatically, you need to uncomment them manually and test that
439 // compilation does indeed fail
5293e4b7 440
449cb073
VZ
441 // connecting a handler with incompatible signature shouldn't work
442#ifdef TEST_INVALID_BIND_GLOBAL
443 handler.Bind(MyEventType, GlobalOnAnotherEvent);
444#endif
445#ifdef TEST_INVALID_BIND_STATIC
446 handler.Bind(MyEventType, &MyHandler::StaticOnAnotherEvent);
447#endif
448#ifdef TEST_INVALID_BIND_METHOD
449 handler.Bind(MyEventType, &MyHandler::OnAnotherEvent, &handler);
450#endif
451#ifdef TEST_INVALID_BIND_FUNCTOR
452 IdleFunctor f;
453 handler.Bind(MyEventType, f);
454#endif
455
48c56e04
VZ
456 // the handler can't be omitted when calling Bind()
457#ifdef TEST_INVALID_BIND_NO_HANDLER
458 handler.Bind(MyEventType, &MyHandler::OnMyEvent);
459#endif
460
449cb073
VZ
461 // calling a derived class method with a base class pointer must not work
462#ifdef TEST_INVALID_BIND_DERIVED
463 struct C1 : wxEvtHandler { };
464 struct C2 : wxEvtHandler { void OnWhatever(wxEvent&); };
465 C1 c1;
466 c1.Bind(&C2::OnWhatever);
467#endif
468
469 // using object pointer incompatible with the method must not work
470#ifdef TEST_INVALID_BIND_WRONG_CLASS
471 MySink mySink;
472 MyHandler myHandler;
473 myHandler.Bind(MyEventType, &MyHandler::OnMyEvent, &mySink);
474#endif
b2238cc3
VZ
475}
476
890d70eb 477#endif // wxHAS_EVENT_BIND