]>
git.saurik.com Git - wxWidgets.git/blob - tests/events/propagation.cpp
27c395eccf417f1de4435af161d1e6d63ae141cf
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/scopeguard.h"
29 // this string will record the execution of all handlers
33 wxDEFINE_EVENT(TEST_EVT
, wxCommandEvent
);
35 // a custom event handler
36 class TestEvtHandler
: public wxEvtHandler
39 TestEvtHandler(char tag
)
42 Connect(TEST_EVT
, wxCommandEventHandler(TestEvtHandler::OnTest
));
45 // override ProcessEvent() to confirm that it is called for all event
46 // handlers in the chain
47 virtual bool ProcessEvent(wxEvent
& event
)
49 if ( event
.GetEventType() == TEST_EVT
)
50 g_str
+= 'o'; // "o" == "overridden"
52 return wxEvtHandler::ProcessEvent(event
);
56 void OnTest(wxCommandEvent
& event
)
65 DECLARE_NO_COPY_CLASS(TestEvtHandler
)
68 // a window handling the test event
69 class TestWindow
: public wxWindow
72 TestWindow(wxWindow
*parent
, char tag
)
73 : wxWindow(parent
, wxID_ANY
),
76 Connect(TEST_EVT
, wxCommandEventHandler(TestWindow::OnTest
));
80 void OnTest(wxCommandEvent
& event
)
89 DECLARE_NO_COPY_CLASS(TestWindow
)
92 int DoFilterEvent(wxEvent
& event
)
94 if ( event
.GetEventType() == TEST_EVT
)
100 bool DoProcessEvent(wxEvent
& event
)
102 if ( event
.GetEventType() == TEST_EVT
)
108 } // anonymous namespace
110 // --------------------------------------------------------------------------
112 // --------------------------------------------------------------------------
114 class EventPropagationTestCase
: public CppUnit::TestCase
117 EventPropagationTestCase() {}
119 virtual void setUp();
120 virtual void tearDown();
123 CPPUNIT_TEST_SUITE( EventPropagationTestCase
);
124 CPPUNIT_TEST( OneHandler
);
125 CPPUNIT_TEST( TwoHandlers
);
126 CPPUNIT_TEST( WindowWithoutHandler
);
127 CPPUNIT_TEST( WindowWithHandler
);
128 CPPUNIT_TEST_SUITE_END();
132 void WindowWithoutHandler();
133 void WindowWithHandler();
135 DECLARE_NO_COPY_CLASS(EventPropagationTestCase
)
138 // register in the unnamed registry so that these tests are run by default
139 CPPUNIT_TEST_SUITE_REGISTRATION( EventPropagationTestCase
);
141 // also include in it's own registry so that these tests can be run alone
142 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EventPropagationTestCase
, "EventPropagationTestCase" );
144 void EventPropagationTestCase::setUp()
146 SetFilterEventFunc(DoFilterEvent
);
147 SetProcessEventFunc(DoProcessEvent
);
152 void EventPropagationTestCase::tearDown()
154 SetFilterEventFunc(NULL
);
155 SetProcessEventFunc(NULL
);
158 void EventPropagationTestCase::OneHandler()
160 wxCommandEvent
event(TEST_EVT
);
161 TestEvtHandler
h1('1');
162 h1
.ProcessEvent(event
);
163 CPPUNIT_ASSERT_EQUAL( "oa1A", g_str
);
166 void EventPropagationTestCase::TwoHandlers()
168 wxCommandEvent
event(TEST_EVT
);
169 TestEvtHandler
h1('1');
170 TestEvtHandler
h2('2');
171 h1
.SetNextHandler(&h2
);
172 h2
.SetPreviousHandler(&h1
);
173 h1
.ProcessEvent(event
);
174 CPPUNIT_ASSERT_EQUAL( "oa1o2A", g_str
);
177 void EventPropagationTestCase::WindowWithoutHandler()
179 wxCommandEvent
event(TEST_EVT
);
180 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
181 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
183 TestWindow
* const child
= new TestWindow(parent
, 'c');
185 child
->ProcessEvent(event
);
186 CPPUNIT_ASSERT_EQUAL( "acpA", g_str
);
189 void EventPropagationTestCase::WindowWithHandler()
191 wxCommandEvent
event(TEST_EVT
);
192 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
193 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
195 TestWindow
* const child
= new TestWindow(parent
, 'c');
197 TestEvtHandler
h1('1');
198 child
->PushEventHandler(&h1
);
199 wxON_BLOCK_EXIT_OBJ0( *child
, wxWindow::PopEventHandler
);
200 TestEvtHandler
h2('2');
201 child
->PushEventHandler(&h2
);
202 wxON_BLOCK_EXIT_OBJ0( *child
, wxWindow::PopEventHandler
);
204 child
->HandleWindowEvent(event
);
205 CPPUNIT_ASSERT_EQUAL( "oa2o1cpA", g_str
);