1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/propagation.cpp
3 // Purpose: Test events propagation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/window.h"
25 #include "wx/scopeguard.h"
30 // this string will record the execution of all handlers
34 wxDEFINE_EVENT(TEST_EVT
, wxCommandEvent
);
36 // a custom event handler
37 class TestEvtHandler
: public wxEvtHandler
40 TestEvtHandler(char tag
)
43 Connect(TEST_EVT
, wxCommandEventHandler(TestEvtHandler
::OnTest
));
46 // override ProcessEvent() to confirm that it is called for all event
47 // handlers in the chain
48 virtual bool ProcessEvent(wxEvent
& event
)
50 if ( event
.GetEventType() == TEST_EVT
)
51 g_str
+= 'o'; // "o" == "overridden"
53 return wxEvtHandler
::ProcessEvent(event
);
57 void OnTest(wxCommandEvent
& event
)
66 DECLARE_NO_COPY_CLASS(TestEvtHandler
)
69 // a window handling the test event
70 class TestWindow
: public wxWindow
73 TestWindow(wxWindow
*parent
, char tag
)
74 : wxWindow(parent
, wxID_ANY
),
77 Connect(TEST_EVT
, wxCommandEventHandler(TestWindow
::OnTest
));
81 void OnTest(wxCommandEvent
& event
)
90 DECLARE_NO_COPY_CLASS(TestWindow
)
93 int DoFilterEvent(wxEvent
& event
)
95 if ( event
.GetEventType() == TEST_EVT
)
101 bool DoProcessEvent(wxEvent
& event
)
103 if ( event
.GetEventType() == TEST_EVT
)
109 } // anonymous namespace
111 // --------------------------------------------------------------------------
113 // --------------------------------------------------------------------------
115 class EventPropagationTestCase
: public CppUnit
::TestCase
118 EventPropagationTestCase() {}
120 virtual void setUp();
121 virtual void tearDown();
124 CPPUNIT_TEST_SUITE( EventPropagationTestCase
);
125 CPPUNIT_TEST( OneHandler
);
126 CPPUNIT_TEST( TwoHandlers
);
127 CPPUNIT_TEST( WindowWithoutHandler
);
128 CPPUNIT_TEST( WindowWithHandler
);
129 CPPUNIT_TEST_SUITE_END();
133 void WindowWithoutHandler();
134 void WindowWithHandler();
136 DECLARE_NO_COPY_CLASS(EventPropagationTestCase
)
139 // register in the unnamed registry so that these tests are run by default
140 CPPUNIT_TEST_SUITE_REGISTRATION( EventPropagationTestCase
);
142 // also include in it's own registry so that these tests can be run alone
143 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EventPropagationTestCase
, "EventPropagationTestCase" );
145 void EventPropagationTestCase
::setUp()
147 SetFilterEventFunc(DoFilterEvent
);
148 SetProcessEventFunc(DoProcessEvent
);
153 void EventPropagationTestCase
::tearDown()
155 SetFilterEventFunc(NULL
);
156 SetProcessEventFunc(NULL
);
159 void EventPropagationTestCase
::OneHandler()
161 wxCommandEvent
event(TEST_EVT
);
162 TestEvtHandler
h1('1');
163 h1
.ProcessEvent(event
);
164 CPPUNIT_ASSERT_EQUAL( "oa1A", g_str
);
167 void EventPropagationTestCase
::TwoHandlers()
169 wxCommandEvent
event(TEST_EVT
);
170 TestEvtHandler
h1('1');
171 TestEvtHandler
h2('2');
172 h1
.SetNextHandler(&h2
);
173 h2
.SetPreviousHandler(&h1
);
174 h1
.ProcessEvent(event
);
175 CPPUNIT_ASSERT_EQUAL( "oa1o2A", g_str
);
178 void EventPropagationTestCase
::WindowWithoutHandler()
180 wxCommandEvent
event(TEST_EVT
);
181 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
182 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow
::Destroy
);
184 TestWindow
* const child
= new TestWindow(parent
, 'c');
186 child
->ProcessEvent(event
);
187 CPPUNIT_ASSERT_EQUAL( "acpA", g_str
);
190 void EventPropagationTestCase
::WindowWithHandler()
192 wxCommandEvent
event(TEST_EVT
);
193 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
194 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow
::Destroy
);
196 TestWindow
* const child
= new TestWindow(parent
, 'c');
198 TestEvtHandler
h1('1');
199 child
->PushEventHandler(&h1
);
200 wxON_BLOCK_EXIT_OBJ0( *child
, wxWindow
::PopEventHandler
);
201 TestEvtHandler
h2('2');
202 child
->PushEventHandler(&h2
);
203 wxON_BLOCK_EXIT_OBJ0( *child
, wxWindow
::PopEventHandler
);
205 child
->HandleWindowEvent(event
);
206 CPPUNIT_ASSERT_EQUAL( "oa2o1cpA", g_str
);