]>
git.saurik.com Git - wxWidgets.git/blob - tests/events/evthandler.cpp
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();
55 class MyEvent
: public wxEvent
59 wxDEFINE_EVENT( EVT_EVENT
, MyEvent
)
61 // An arbitrary functor:
66 void operator () ( MyEvent
& )
69 bool operator == ( const MyFunctor
& ) const
74 class MyHandler
: public wxEvtHandler
77 void handleMethod( MyEvent
& )
80 static void handleFunction( MyEvent
& )
83 void handleEvent( wxEvent
& )
88 void EvtHandlerTestCase::TestConnectCompilation()
90 // Test that connecting the 'legacy' events still compiles:
94 handler
.Connect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
, NULL
, &handler
);
95 handler
.Connect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
, NULL
, &handler
);
96 handler
.Connect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
, NULL
, &handler
);
98 handler
.Disconnect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
, NULL
, &handler
);
99 handler
.Disconnect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
, NULL
, &handler
);
100 handler
.Disconnect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
, NULL
, &handler
);
104 handler
.Connect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
);
105 handler
.Connect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
);
106 handler
.Connect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
);
108 handler
.Disconnect( EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
);
109 handler
.Disconnect( 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
);
110 handler
.Disconnect( 0, 0, EVT_LEGACY
, (wxObjectEventFunction
)&MyHandler::handleEvent
);
112 // Call (and therefore instantiate) all Connect() variants to detect template
115 #if !wxEVENTS_COMPATIBILITY_2_8
117 handler
.Connect( EVT_EVENT
, &MyHandler::handleFunction
);
118 handler
.Connect( 0, EVT_EVENT
, &MyHandler::handleFunction
);
119 handler
.Connect( 0, 0, EVT_EVENT
, &MyHandler::handleFunction
);
121 handler
.Disconnect( EVT_EVENT
, &MyHandler::handleFunction
);
122 handler
.Disconnect( 0, EVT_EVENT
, &MyHandler::handleFunction
);
123 handler
.Disconnect( 0, 0, EVT_EVENT
, &MyHandler::handleFunction
);
127 handler
.Connect( EVT_EVENT
, &MyHandler::handleMethod
);
128 handler
.Connect( 0, EVT_EVENT
, &MyHandler::handleMethod
);
129 handler
.Connect( 0, 0, EVT_EVENT
, &MyHandler::handleMethod
);
131 handler
.Disconnect( EVT_EVENT
, &MyHandler::handleMethod
);
132 handler
.Disconnect( 0, EVT_EVENT
, &MyHandler::handleMethod
);
133 handler
.Disconnect( 0, 0, EVT_EVENT
, &MyHandler::handleMethod
);
137 handler
.Connect( EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
138 handler
.Connect( 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
139 handler
.Connect( 0, 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
141 handler
.Disconnect( EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
142 handler
.Disconnect( 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
143 handler
.Disconnect( 0, 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
147 wxEvtHandler::Connect( &handler
, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
148 wxEvtHandler::Connect( &handler
, 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
149 wxEvtHandler::Connect( &handler
, 0, 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
151 wxEvtHandler::Disconnect( &handler
, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
152 wxEvtHandler::Disconnect( &handler
, 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
153 wxEvtHandler::Disconnect( &handler
, 0, 0, EVT_EVENT
, &MyHandler::handleMethod
, NULL
, &handler
);
159 handler
.Connect( EVT_EVENT
, functor
);
160 handler
.Connect( 0, EVT_EVENT
, functor
);
161 handler
.Connect( 0, 0, EVT_EVENT
, functor
);
163 handler
.Disconnect( EVT_EVENT
, functor
);
164 handler
.Disconnect( 0, EVT_EVENT
, functor
);
165 handler
.Disconnect( 0, 0, EVT_EVENT
, functor
);
169 void EvtHandlerTestCase::TestEventFunctorCompare()
171 //#if !wxEVENTS_COMPATIBILITY_2_8
172 // MyHandler handler1;
173 // wxEventFunctor *connectFunctor = wxNewEventFunctor( EVT_EVENT, &MyHandler::handleMethod, &handler1 );
174 // wxEventFunctor *disconnectFunctor = wxNewEventFunctor( EVT_EVENT, &MyHandler::handleMethod, &handler1 );
175 // wxEventFunctor *nullFunctor = wxNewEventFunctor( EVT_EVENT, &MyHandler::handleMethod );
177 // CPPUNIT_ASSERT( connectFunctor->Matches( *disconnectFunctor ));
178 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *connectFunctor ));
180 // CPPUNIT_ASSERT( connectFunctor->Matches( *nullFunctor ));
181 // CPPUNIT_ASSERT( nullFunctor->Matches( *connectFunctor ));
183 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *nullFunctor ));
184 // CPPUNIT_ASSERT( nullFunctor->Matches( *disconnectFunctor ));