test that the connected event handler is really called; some naming changes
[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 #ifndef WX_PRECOMP
21 #endif // WX_PRECOMP
22
23 #include "wx/event.h"
24
25
26 // --------------------------------------------------------------------------
27 // test class
28 // --------------------------------------------------------------------------
29
30 class EvtHandlerTestCase : public CppUnit::TestCase
31 {
32 public:
33 EvtHandlerTestCase() {}
34
35 private:
36 CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
37 CPPUNIT_TEST( TestConnectCompilation );
38 CPPUNIT_TEST( TestEventFunctorCompare );
39 CPPUNIT_TEST_SUITE_END();
40
41 void TestConnectCompilation();
42 void TestEventFunctorCompare();
43
44 DECLARE_NO_COPY_CLASS(EvtHandlerTestCase)
45 };
46
47 // register in the unnamed registry so that these tests are run by default
48 CPPUNIT_TEST_SUITE_REGISTRATION( EvtHandlerTestCase );
49
50 // also include in it's own registry so that these tests can be run alone
51 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase, "EvtHandlerTestCase" );
52
53 const wxEventType EVT_LEGACY = wxNewEventType();
54
55 class MyEvent;
56 wxDEFINE_EVENT( EVT_MYEVENT, MyEvent )
57
58 class MyEvent : public wxEvent
59 {
60 public:
61 MyEvent() : wxEvent(0, EVT_MYEVENT) { }
62
63 virtual wxEvent *Clone() const { return new MyEvent; }
64 };
65
66 class AnotherEvent : public wxEvent
67 {
68 };
69
70 namespace
71 {
72
73 struct Called
74 {
75 Called() { Reset(); }
76
77 void Reset()
78 {
79 function =
80 functor =
81 method =
82 smethod = false;
83 }
84
85 bool function,
86 functor,
87 method,
88 smethod;
89 } g_called;
90
91 void GlobalOnMyEvent(MyEvent&)
92 {
93 g_called.function = true;
94 }
95
96 class MyFunctor
97 {
98 public:
99 void operator()(MyEvent &) { g_called.functor = true; }
100
101 bool operator==(const MyFunctor &) const { return true; }
102 };
103
104 class MyHandler : public wxEvtHandler
105 {
106 public:
107 void OnMyEvent(MyEvent&) { g_called.method = true; }
108
109 static void StaticOnMyEvent(MyEvent &) { g_called.smethod = true; }
110
111 void OnEvent(wxEvent &) { }
112 void OnAnotherEvent(AnotherEvent&) { }
113 };
114
115 } // anonymous namespace
116
117 void EvtHandlerTestCase::TestConnectCompilation()
118 {
119 // Test that connecting the 'legacy' events still compiles:
120
121 MyHandler handler;
122 MyEvent e;
123
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 );
127
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 );
131
132
133
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 );
137
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 );
141
142 // Call (and therefore instantiate) all Connect() variants to detect template
143 // errors:
144
145 #if !wxEVENTS_COMPATIBILITY_2_8
146 handler.Connect( EVT_MYEVENT, &MyHandler::StaticOnMyEvent );
147 g_called.Reset();
148 handler.ProcessEvent(e);
149 CPPUNIT_ASSERT( g_called.smethod );
150 handler.Disconnect( EVT_MYEVENT, &MyHandler::StaticOnMyEvent );
151
152 g_called.Reset();
153 handler.ProcessEvent(e);
154 CPPUNIT_ASSERT( !g_called.smethod ); // check that it was disconnected
155
156 handler.Connect( 0, EVT_MYEVENT, &MyHandler::StaticOnMyEvent );
157 handler.Disconnect( 0, EVT_MYEVENT, &MyHandler::StaticOnMyEvent );
158
159 handler.Connect( 0, 0, EVT_MYEVENT, &MyHandler::StaticOnMyEvent );
160 handler.Disconnect( 0, 0, EVT_MYEVENT, &MyHandler::StaticOnMyEvent );
161
162
163
164 handler.Connect( EVT_MYEVENT, &MyHandler::OnMyEvent );
165 handler.Connect( 0, EVT_MYEVENT, &MyHandler::OnMyEvent );
166 handler.Connect( 0, 0, EVT_MYEVENT, &MyHandler::OnMyEvent );
167
168 handler.Disconnect( EVT_MYEVENT, &MyHandler::OnMyEvent );
169 handler.Disconnect( 0, EVT_MYEVENT, &MyHandler::OnMyEvent );
170 handler.Disconnect( 0, 0, EVT_MYEVENT, &MyHandler::OnMyEvent );
171
172
173
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 );
177
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 );
181
182
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 );
186
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 );
190
191
192
193 MyFunctor functor;
194
195 handler.Connect( EVT_MYEVENT, functor );
196 handler.Connect( 0, EVT_MYEVENT, functor );
197 handler.Connect( 0, 0, EVT_MYEVENT, functor );
198
199 handler.Disconnect( EVT_MYEVENT, functor );
200 handler.Disconnect( 0, EVT_MYEVENT, functor );
201 handler.Disconnect( 0, 0, EVT_MYEVENT, functor );
202
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
208 }
209
210 void EvtHandlerTestCase::TestEventFunctorCompare()
211 {
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 );
217 //
218 // CPPUNIT_ASSERT( connectFunctor->Matches( *disconnectFunctor ));
219 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *connectFunctor ));
220 //
221 // CPPUNIT_ASSERT( connectFunctor->Matches( *nullFunctor ));
222 // CPPUNIT_ASSERT( nullFunctor->Matches( *connectFunctor ));
223 //
224 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *nullFunctor ));
225 // CPPUNIT_ASSERT( nullFunctor->Matches( *disconnectFunctor ));
226 //#endif
227 }
228
229