add a unit test for new events (see #10000)
[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 : public wxEvent
56 {
57 };
58
59 wxDEFINE_EVENT( EVT_EVENT, MyEvent )
60
61 // An arbitrary functor:
62
63 class MyFunctor
64 {
65 public:
66 void operator () ( MyEvent & )
67 { }
68
69 bool operator == ( const MyFunctor & ) const
70 { return ( true ); }
71 };
72
73
74 class MyHandler : public wxEvtHandler
75 {
76 public:
77 void handleMethod( MyEvent & )
78 { }
79
80 static void handleFunction( MyEvent & )
81 { }
82
83 void handleEvent( wxEvent & )
84 { }
85
86 };
87
88 void EvtHandlerTestCase::TestConnectCompilation()
89 {
90 // Test that connecting the 'legacy' events still compiles:
91
92 MyHandler handler;
93
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 );
97
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 );
101
102
103
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 );
107
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 );
111
112 // Call (and therefore instantiate) all Connect() variants to detect template
113 // errors:
114
115 #if !wxEVENTS_COMPATIBILITY_2_8
116
117 handler.Connect( EVT_EVENT, &MyHandler::handleFunction );
118 handler.Connect( 0, EVT_EVENT, &MyHandler::handleFunction );
119 handler.Connect( 0, 0, EVT_EVENT, &MyHandler::handleFunction );
120
121 handler.Disconnect( EVT_EVENT, &MyHandler::handleFunction );
122 handler.Disconnect( 0, EVT_EVENT, &MyHandler::handleFunction );
123 handler.Disconnect( 0, 0, EVT_EVENT, &MyHandler::handleFunction );
124
125
126
127 handler.Connect( EVT_EVENT, &MyHandler::handleMethod );
128 handler.Connect( 0, EVT_EVENT, &MyHandler::handleMethod );
129 handler.Connect( 0, 0, EVT_EVENT, &MyHandler::handleMethod );
130
131 handler.Disconnect( EVT_EVENT, &MyHandler::handleMethod );
132 handler.Disconnect( 0, EVT_EVENT, &MyHandler::handleMethod );
133 handler.Disconnect( 0, 0, EVT_EVENT, &MyHandler::handleMethod );
134
135
136
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 );
140
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 );
144
145
146
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 );
150
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 );
154
155
156
157 MyFunctor functor;
158
159 handler.Connect( EVT_EVENT, functor );
160 handler.Connect( 0, EVT_EVENT, functor );
161 handler.Connect( 0, 0, EVT_EVENT, functor );
162
163 handler.Disconnect( EVT_EVENT, functor );
164 handler.Disconnect( 0, EVT_EVENT, functor );
165 handler.Disconnect( 0, 0, EVT_EVENT, functor );
166 #endif
167 }
168
169 void EvtHandlerTestCase::TestEventFunctorCompare()
170 {
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 );
176 //
177 // CPPUNIT_ASSERT( connectFunctor->Matches( *disconnectFunctor ));
178 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *connectFunctor ));
179 //
180 // CPPUNIT_ASSERT( connectFunctor->Matches( *nullFunctor ));
181 // CPPUNIT_ASSERT( nullFunctor->Matches( *connectFunctor ));
182 //
183 // CPPUNIT_ASSERT( disconnectFunctor->Matches( *nullFunctor ));
184 // CPPUNIT_ASSERT( nullFunctor->Matches( *disconnectFunctor ));
185 //#endif
186 }
187
188