]>
git.saurik.com Git - wxWidgets.git/blob - tests/events/propagation.cpp
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 // ----------------------------------------------------------------------------
23 #include "wx/window.h"
26 #include "wx/scopeguard.h"
31 // this string will record the execution of all handlers
35 wxDEFINE_EVENT(TEST_EVT
, wxCommandEvent
);
37 // a custom event handler
38 class TestEvtHandler
: public wxEvtHandler
41 TestEvtHandler(char tag
)
44 Connect(TEST_EVT
, wxCommandEventHandler(TestEvtHandler::OnTest
));
47 // override ProcessEvent() to confirm that it is called for all event
48 // handlers in the chain
49 virtual bool ProcessEvent(wxEvent
& event
)
51 if ( event
.GetEventType() == TEST_EVT
)
52 g_str
+= 'o'; // "o" == "overridden"
54 return wxEvtHandler::ProcessEvent(event
);
58 void OnTest(wxCommandEvent
& event
)
67 DECLARE_NO_COPY_CLASS(TestEvtHandler
)
70 // a window handling the test event
71 class TestWindow
: public wxWindow
74 TestWindow(wxWindow
*parent
, char tag
)
75 : wxWindow(parent
, wxID_ANY
),
78 Connect(TEST_EVT
, wxCommandEventHandler(TestWindow::OnTest
));
82 void OnTest(wxCommandEvent
& event
)
91 DECLARE_NO_COPY_CLASS(TestWindow
)
94 int DoFilterEvent(wxEvent
& event
)
96 if ( event
.GetEventType() == TEST_EVT
)
102 bool DoProcessEvent(wxEvent
& event
)
104 if ( event
.GetEventType() == TEST_EVT
)
110 } // anonymous namespace
112 // --------------------------------------------------------------------------
114 // --------------------------------------------------------------------------
116 class EventPropagationTestCase
: public CppUnit::TestCase
119 EventPropagationTestCase() {}
121 virtual void setUp();
122 virtual void tearDown();
125 CPPUNIT_TEST_SUITE( EventPropagationTestCase
);
126 CPPUNIT_TEST( OneHandler
);
127 CPPUNIT_TEST( TwoHandlers
);
128 CPPUNIT_TEST( WindowWithoutHandler
);
129 CPPUNIT_TEST( WindowWithHandler
);
130 CPPUNIT_TEST_SUITE_END();
134 void WindowWithoutHandler();
135 void WindowWithHandler();
137 DECLARE_NO_COPY_CLASS(EventPropagationTestCase
)
140 // register in the unnamed registry so that these tests are run by default
141 CPPUNIT_TEST_SUITE_REGISTRATION( EventPropagationTestCase
);
143 // also include in it's own registry so that these tests can be run alone
144 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EventPropagationTestCase
, "EventPropagationTestCase" );
146 void EventPropagationTestCase::setUp()
148 SetFilterEventFunc(DoFilterEvent
);
149 SetProcessEventFunc(DoProcessEvent
);
154 void EventPropagationTestCase::tearDown()
156 SetFilterEventFunc(NULL
);
157 SetProcessEventFunc(NULL
);
160 void EventPropagationTestCase::OneHandler()
162 wxCommandEvent
event(TEST_EVT
);
163 TestEvtHandler
h1('1');
164 h1
.ProcessEvent(event
);
165 CPPUNIT_ASSERT_EQUAL( "oa1A", g_str
);
168 void EventPropagationTestCase::TwoHandlers()
170 wxCommandEvent
event(TEST_EVT
);
171 TestEvtHandler
h1('1');
172 TestEvtHandler
h2('2');
173 h1
.SetNextHandler(&h2
);
174 h2
.SetPreviousHandler(&h1
);
175 h1
.ProcessEvent(event
);
176 CPPUNIT_ASSERT_EQUAL( "oa1o2A", g_str
);
179 void EventPropagationTestCase::WindowWithoutHandler()
181 wxCommandEvent
event(TEST_EVT
);
182 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
183 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
185 TestWindow
* const child
= new TestWindow(parent
, 'c');
187 child
->GetEventHandler()->ProcessEvent(event
);
188 CPPUNIT_ASSERT_EQUAL( "acpA", g_str
);
191 void EventPropagationTestCase::WindowWithHandler()
193 wxCommandEvent
event(TEST_EVT
);
194 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
195 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
197 TestWindow
* const child
= new TestWindow(parent
, 'c');
199 TestEvtHandler
h1('1');
200 child
->PushEventHandler(&h1
);
201 wxON_BLOCK_EXIT_OBJ1( *child
, wxWindow::PopEventHandler
, false );
202 TestEvtHandler
h2('2');
203 child
->PushEventHandler(&h2
);
204 wxON_BLOCK_EXIT_OBJ1( *child
, wxWindow::PopEventHandler
, false );
206 child
->HandleWindowEvent(event
);
207 CPPUNIT_ASSERT_EQUAL( "oa2o1cpA", g_str
);