]> git.saurik.com Git - wxWidgets.git/blame - tests/events/propagation.cpp
Document wxSL_MIN_MAX_LABELS and wxSL_VALUE_LABEL
[wxWidgets.git] / tests / events / propagation.cpp
CommitLineData
1649d288
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/events/propagation.cpp
3// Purpose: Test events propagation
4// Author: Vadim Zeitlin
5// Created: 2009-01-16
6// RCS-ID: $Id$
7// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
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
c21dcf80
VZ
21 #include "wx/app.h"
22 #include "wx/event.h"
23 #include "wx/window.h"
1649d288
VZ
24#endif // WX_PRECOMP
25
1649d288
VZ
26#include "wx/scopeguard.h"
27
28namespace
29{
30
31// this string will record the execution of all handlers
32wxString g_str;
33
34// a custom event
35wxDEFINE_EVENT(TEST_EVT, wxCommandEvent);
36
37// a custom event handler
38class TestEvtHandler : public wxEvtHandler
39{
40public:
41 TestEvtHandler(char tag)
42 : m_tag(tag)
43 {
44 Connect(TEST_EVT, wxCommandEventHandler(TestEvtHandler::OnTest));
45 }
46
47 // override ProcessEvent() to confirm that it is called for all event
48 // handlers in the chain
49 virtual bool ProcessEvent(wxEvent& event)
50 {
51 if ( event.GetEventType() == TEST_EVT )
52 g_str += 'o'; // "o" == "overridden"
53
54 return wxEvtHandler::ProcessEvent(event);
55 }
56
57private:
58 void OnTest(wxCommandEvent& event)
59 {
60 g_str += m_tag;
61
62 event.Skip();
63 }
64
65 const char m_tag;
66
67 DECLARE_NO_COPY_CLASS(TestEvtHandler)
68};
69
70// a window handling the test event
71class TestWindow : public wxWindow
72{
73public:
74 TestWindow(wxWindow *parent, char tag)
75 : wxWindow(parent, wxID_ANY),
76 m_tag(tag)
77 {
78 Connect(TEST_EVT, wxCommandEventHandler(TestWindow::OnTest));
79 }
80
81private:
82 void OnTest(wxCommandEvent& event)
83 {
84 g_str += m_tag;
85
86 event.Skip();
87 }
88
89 const char m_tag;
90
91 DECLARE_NO_COPY_CLASS(TestWindow)
92};
93
94int DoFilterEvent(wxEvent& event)
95{
96 if ( event.GetEventType() == TEST_EVT )
97 g_str += 'a';
98
99 return -1;
100}
101
102bool DoProcessEvent(wxEvent& event)
103{
104 if ( event.GetEventType() == TEST_EVT )
105 g_str += 'A';
106
107 return false;
108}
109
110} // anonymous namespace
111
112// --------------------------------------------------------------------------
113// test class
114// --------------------------------------------------------------------------
115
116class EventPropagationTestCase : public CppUnit::TestCase
117{
118public:
119 EventPropagationTestCase() {}
120
121 virtual void setUp();
122 virtual void tearDown();
123
124private:
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();
131
132 void OneHandler();
133 void TwoHandlers();
134 void WindowWithoutHandler();
135 void WindowWithHandler();
136
137 DECLARE_NO_COPY_CLASS(EventPropagationTestCase)
138};
139
140// register in the unnamed registry so that these tests are run by default
141CPPUNIT_TEST_SUITE_REGISTRATION( EventPropagationTestCase );
142
143// also include in it's own registry so that these tests can be run alone
144CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EventPropagationTestCase, "EventPropagationTestCase" );
145
146void EventPropagationTestCase::setUp()
147{
148 SetFilterEventFunc(DoFilterEvent);
149 SetProcessEventFunc(DoProcessEvent);
150
151 g_str.clear();
152}
153
154void EventPropagationTestCase::tearDown()
155{
156 SetFilterEventFunc(NULL);
157 SetProcessEventFunc(NULL);
158}
159
160void EventPropagationTestCase::OneHandler()
161{
162 wxCommandEvent event(TEST_EVT);
163 TestEvtHandler h1('1');
164 h1.ProcessEvent(event);
165 CPPUNIT_ASSERT_EQUAL( "oa1A", g_str );
166}
167
168void EventPropagationTestCase::TwoHandlers()
169{
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 );
177}
178
179void EventPropagationTestCase::WindowWithoutHandler()
180{
181 wxCommandEvent event(TEST_EVT);
182 TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
183 wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
184
185 TestWindow * const child = new TestWindow(parent, 'c');
186
004867db 187 child->GetEventHandler()->ProcessEvent(event);
1649d288
VZ
188 CPPUNIT_ASSERT_EQUAL( "acpA", g_str );
189}
190
191void EventPropagationTestCase::WindowWithHandler()
192{
193 wxCommandEvent event(TEST_EVT);
194 TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
195 wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
196
197 TestWindow * const child = new TestWindow(parent, 'c');
198
199 TestEvtHandler h1('1');
200 child->PushEventHandler(&h1);
9ebfc963 201 wxON_BLOCK_EXIT_OBJ1( *child, wxWindow::PopEventHandler, false );
1649d288
VZ
202 TestEvtHandler h2('2');
203 child->PushEventHandler(&h2);
9ebfc963 204 wxON_BLOCK_EXIT_OBJ1( *child, wxWindow::PopEventHandler, false );
1649d288
VZ
205
206 child->HandleWindowEvent(event);
207 CPPUNIT_ASSERT_EQUAL( "oa2o1cpA", g_str );
208}
209