1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/evthandler.cpp
3 // Purpose: Test the new event types and wxEvtHandler-methods
7 // Copyright: (c) 2009 Peter Most
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
26 // --------------------------------------------------------------------------
28 // --------------------------------------------------------------------------
30 class EvtHandlerTestCase
: public CppUnit
::TestCase
33 EvtHandlerTestCase() {}
36 CPPUNIT_TEST_SUITE( EvtHandlerTestCase
);
37 CPPUNIT_TEST( TestConnectCompilation
);
38 CPPUNIT_TEST( TestEventFunctorCompare
);
39 CPPUNIT_TEST_SUITE_END();
41 void TestConnectCompilation();
42 void TestEventFunctorCompare();
44 DECLARE_NO_COPY_CLASS(EvtHandlerTestCase
)
47 // register in the unnamed registry so that these tests are run by default
48 CPPUNIT_TEST_SUITE_REGISTRATION( EvtHandlerTestCase
);
50 // also include in it's own registry so that these tests can be run alone
51 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase
, "EvtHandlerTestCase" );
53 const wxEventType EVT_LEGACY
= wxNewEventType();
56 wxDEFINE_EVENT( EVT_MYEVENT
, MyEvent
)
58 class MyEvent
: public wxEvent
61 MyEvent() : wxEvent(0, EVT_MYEVENT
) { }
63 virtual wxEvent
*Clone() const { return new MyEvent
; }
66 class AnotherEvent
: public wxEvent
91 void GlobalOnMyEvent(MyEvent
&)
93 g_called
.function
= true;
99 void operator()(MyEvent
&) { g_called
.functor
= true; }
101 bool operator==(const MyFunctor
&) const { return true; }
104 class MyHandler
: public wxEvtHandler
107 void OnMyEvent(MyEvent
&) { g_called
.method
= true; }
109 static void StaticOnMyEvent(MyEvent
&) { g_called
.smethod
= true; }
111 void OnEvent(wxEvent
&) { }
112 void OnAnotherEvent(AnotherEvent
&) { }
115 } // anonymous namespace
117 void EvtHandlerTestCase
::TestConnectCompilation()
119 // Test that connecting the 'legacy' events still compiles:
124 handler
.Connect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
, NULL
, &handler
);
125 handler
.Connect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
, NULL
, &handler
);
126 handler
.Connect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
, NULL
, &handler
);
128 handler
.Disconnect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
, NULL
, &handler
);
129 handler
.Disconnect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
, NULL
, &handler
);
130 handler
.Disconnect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
, NULL
, &handler
);
134 handler
.Connect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
);
135 handler
.Connect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
);
136 handler
.Connect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
);
138 handler
.Disconnect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
);
139 handler
.Disconnect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
);
140 handler
.Disconnect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler
::OnEvent
);
142 // Call (and therefore instantiate) all Connect() variants to detect template
145 #if !wxEVENTS_COMPATIBILITY_2_8
146 handler
.Connect( EVT_MYEVENT
, &MyHandler
::StaticOnMyEvent
);
148 handler
.ProcessEvent(e
);
149 CPPUNIT_ASSERT( g_called
.smethod
);
150 handler
.Disconnect( EVT_MYEVENT
, &MyHandler
::StaticOnMyEvent
);
153 handler
.ProcessEvent(e
);
154 CPPUNIT_ASSERT( !g_called
.smethod
); // check that it was disconnected
156 handler
.Connect( 0, EVT_MYEVENT
, &MyHandler
::StaticOnMyEvent
);
157 handler
.Disconnect( 0, EVT_MYEVENT
, &MyHandler
::StaticOnMyEvent
);
159 handler
.Connect( 0, 0, EVT_MYEVENT
, &MyHandler
::StaticOnMyEvent
);
160 handler
.Disconnect( 0, 0, EVT_MYEVENT
, &MyHandler
::StaticOnMyEvent
);
164 handler
.Connect( EVT_MYEVENT
, &MyHandler
::OnMyEvent
);
165 handler
.Connect( 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
);
166 handler
.Connect( 0, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
);
168 handler
.Disconnect( EVT_MYEVENT
, &MyHandler
::OnMyEvent
);
169 handler
.Disconnect( 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
);
170 handler
.Disconnect( 0, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
);
174 handler
.Connect( EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
175 handler
.Connect( 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
176 handler
.Connect( 0, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
178 handler
.Disconnect( EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
179 handler
.Disconnect( 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
180 handler
.Disconnect( 0, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
183 wxEvtHandler
::Connect( &handler
, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
184 wxEvtHandler
::Connect( &handler
, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
185 wxEvtHandler
::Connect( &handler
, 0, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
187 wxEvtHandler
::Disconnect( &handler
, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
188 wxEvtHandler
::Disconnect( &handler
, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
189 wxEvtHandler
::Disconnect( &handler
, 0, 0, EVT_MYEVENT
, &MyHandler
::OnMyEvent
, NULL
, &handler
);
195 handler
.Connect( EVT_MYEVENT
, functor
);
196 handler
.Connect( 0, EVT_MYEVENT
, functor
);
197 handler
.Connect( 0, 0, EVT_MYEVENT
, functor
);
199 handler
.Disconnect( EVT_MYEVENT
, functor
);
200 handler
.Disconnect( 0, EVT_MYEVENT
, functor
);
201 handler
.Disconnect( 0, 0, EVT_MYEVENT
, functor
);
203 // these calls shouldn't compile but we unfortunately can't check this
204 // automatically, you need to uncomment them manually and test that
205 // compilation does indeed fail
206 //handler.Connect(EVT_MYEVENT, &MyHandler::OnAnotherEvent, NULL, &handler);
207 #endif // !wxEVENTS_COMPATIBILITY_2_8
210 void EvtHandlerTestCase
::TestEventFunctorCompare()
212 //#if !wxEVENTS_COMPATIBILITY_2_8
213 // MyHandler handler1;
214 // wxEventFunctor *connectFunctor = wxNewEventFunctor( EVT_MYEVENT, &MyHandler::OnMyEvent, &handler1 );
215 // wxEventFunctor *disconnectFunctor = wxNewEventFunctor( EVT_MYEVENT, &MyHandler::OnMyEvent, &handler1 );
216 // wxEventFunctor *nullFunctor = wxNewEventFunctor( EVT_MYEVENT, &MyHandler::OnMyEvent );
218 // CPPUNIT_ASSERT( connectFunctor->Matches( *disconnectFunctor ));
219 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *connectFunctor ));
221 // CPPUNIT_ASSERT( connectFunctor->Matches( *nullFunctor ));
222 // CPPUNIT_ASSERT( nullFunctor->Matches( *connectFunctor ));
224 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *nullFunctor ));
225 // CPPUNIT_ASSERT( nullFunctor->Matches( *disconnectFunctor ));