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/scrolwin.h"
24 #include "wx/window.h"
27 #include "wx/docmdi.h"
30 #include "wx/scopedptr.h"
31 #include "wx/scopeguard.h"
32 #include "wx/toolbar.h"
33 #include "wx/uiaction.h"
35 // FIXME: Currently under OS X testing paint event doesn't work because neither
36 // calling Refresh()+Update() nor even sending wxPaintEvent directly to
37 // the window doesn't result in calls to its event handlers, so disable
38 // some tests there. But this should be fixed and the tests reenabled
39 // because wxPaintEvent propagation in wxScrolledWindow is a perfect
40 // example of fragile code that could be broken under OS X.
42 #define CAN_TEST_PAINT_EVENTS
48 // this string will record the execution of all handlers
52 wxDEFINE_EVENT(TEST_EVT
, wxCommandEvent
);
54 // a custom event handler tracing the propagation of the events of the
56 template <class Event
>
57 class TestEvtHandlerBase
: public wxEvtHandler
60 TestEvtHandlerBase(wxEventType evtType
, char tag
)
65 static_cast<wxEventFunction
>(&TestEvtHandlerBase::OnTest
));
68 // override ProcessEvent() to confirm that it is called for all event
69 // handlers in the chain
70 virtual bool ProcessEvent(wxEvent
& event
)
72 if ( event
.GetEventType() == m_evtType
)
73 g_str
+= 'o'; // "o" == "overridden"
75 return wxEvtHandler::ProcessEvent(event
);
79 void OnTest(wxEvent
& event
)
86 const wxEventType m_evtType
;
89 wxDECLARE_NO_COPY_TEMPLATE_CLASS(TestEvtHandlerBase
, Event
);
92 struct TestEvtHandler
: TestEvtHandlerBase
<wxCommandEvent
>
94 TestEvtHandler(char tag
)
95 : TestEvtHandlerBase
<wxCommandEvent
>(TEST_EVT
, tag
)
100 struct TestMenuEvtHandler
: TestEvtHandlerBase
<wxCommandEvent
>
102 TestMenuEvtHandler(char tag
)
103 : TestEvtHandlerBase
<wxCommandEvent
>(wxEVT_MENU
, tag
)
108 struct TestPaintEvtHandler
: TestEvtHandlerBase
<wxPaintEvent
>
110 TestPaintEvtHandler(char tag
)
111 : TestEvtHandlerBase
<wxPaintEvent
>(wxEVT_PAINT
, tag
)
116 // Another custom event handler, suitable for use with Connect().
117 struct TestEvtSink
: wxEvtHandler
119 TestEvtSink(char tag
)
124 void Handle(wxEvent
& event
)
133 wxDECLARE_NO_COPY_CLASS(TestEvtSink
);
136 // a window handling the test event
137 class TestWindow
: public wxWindow
140 TestWindow(wxWindow
*parent
, char tag
)
141 : wxWindow(parent
, wxID_ANY
),
144 Connect(TEST_EVT
, wxCommandEventHandler(TestWindow::OnTest
));
148 void OnTest(wxCommandEvent
& event
)
157 DECLARE_NO_COPY_CLASS(TestWindow
)
160 // a scroll window handling paint event: we want to have a special test case
161 // for this because the event propagation is complicated even further than
162 // usual here by the presence of wxScrollHelperEvtHandler in the event handlers
163 // chain and the fact that OnDraw() virtual method must be called if EVT_PAINT
165 class TestScrollWindow
: public wxScrolledWindow
168 TestScrollWindow(wxWindow
*parent
)
169 : wxScrolledWindow(parent
, wxID_ANY
)
171 Connect(wxEVT_PAINT
, wxPaintEventHandler(TestScrollWindow::OnPaint
));
174 void GeneratePaintEvent()
177 // We need to map the window, otherwise we're not going to get any
178 // paint events for it.
181 // Ignore events generated during the initial mapping.
189 virtual void OnDraw(wxDC
& WXUNUSED(dc
))
191 g_str
+= 'D'; // draw
195 void OnPaint(wxPaintEvent
& event
)
197 g_str
+= 'P'; // paint
201 wxDECLARE_NO_COPY_CLASS(TestScrollWindow
);
204 int DoFilterEvent(wxEvent
& event
)
206 if ( event
.GetEventType() == TEST_EVT
||
207 event
.GetEventType() == wxEVT_MENU
)
213 bool DoProcessEvent(wxEvent
& event
)
215 if ( event
.GetEventType() == TEST_EVT
||
216 event
.GetEventType() == wxEVT_MENU
)
222 } // anonymous namespace
224 // --------------------------------------------------------------------------
226 // --------------------------------------------------------------------------
228 class EventPropagationTestCase
: public CppUnit::TestCase
231 EventPropagationTestCase() {}
233 virtual void setUp();
234 virtual void tearDown();
237 CPPUNIT_TEST_SUITE( EventPropagationTestCase
);
238 CPPUNIT_TEST( OneHandler
);
239 CPPUNIT_TEST( TwoHandlers
);
240 CPPUNIT_TEST( WindowWithoutHandler
);
241 CPPUNIT_TEST( WindowWithHandler
);
242 CPPUNIT_TEST( ForwardEvent
);
243 CPPUNIT_TEST( ScrollWindowWithoutHandler
);
244 CPPUNIT_TEST( ScrollWindowWithHandler
);
245 CPPUNIT_TEST( MenuEvent
);
246 CPPUNIT_TEST( DocView
);
247 WXUISIM_TEST( ContextMenuEvent
);
248 CPPUNIT_TEST_SUITE_END();
252 void WindowWithoutHandler();
253 void WindowWithHandler();
255 void ScrollWindowWithoutHandler();
256 void ScrollWindowWithHandler();
259 void ContextMenuEvent();
261 DECLARE_NO_COPY_CLASS(EventPropagationTestCase
)
264 // register in the unnamed registry so that these tests are run by default
265 CPPUNIT_TEST_SUITE_REGISTRATION( EventPropagationTestCase
);
267 // also include in its own registry so that these tests can be run alone
268 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EventPropagationTestCase
, "EventPropagationTestCase" );
270 void EventPropagationTestCase::setUp()
272 SetFilterEventFunc(DoFilterEvent
);
273 SetProcessEventFunc(DoProcessEvent
);
278 void EventPropagationTestCase::tearDown()
280 SetFilterEventFunc(NULL
);
281 SetProcessEventFunc(NULL
);
284 void EventPropagationTestCase::OneHandler()
286 wxCommandEvent
event(TEST_EVT
);
287 TestEvtHandler
h1('1');
288 h1
.ProcessEvent(event
);
289 CPPUNIT_ASSERT_EQUAL( "oa1A", g_str
);
292 void EventPropagationTestCase::TwoHandlers()
294 wxCommandEvent
event(TEST_EVT
);
295 TestEvtHandler
h1('1');
296 TestEvtHandler
h2('2');
297 h1
.SetNextHandler(&h2
);
298 h2
.SetPreviousHandler(&h1
);
299 h1
.ProcessEvent(event
);
300 CPPUNIT_ASSERT_EQUAL( "oa1o2A", g_str
);
303 void EventPropagationTestCase::WindowWithoutHandler()
305 wxCommandEvent
event(TEST_EVT
);
306 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
307 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
309 TestWindow
* const child
= new TestWindow(parent
, 'c');
311 child
->GetEventHandler()->ProcessEvent(event
);
312 CPPUNIT_ASSERT_EQUAL( "acpA", g_str
);
315 void EventPropagationTestCase::WindowWithHandler()
317 wxCommandEvent
event(TEST_EVT
);
318 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
319 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
321 TestWindow
* const child
= new TestWindow(parent
, 'c');
323 TestEvtHandler
h1('1');
324 child
->PushEventHandler(&h1
);
325 wxON_BLOCK_EXIT_OBJ1( *child
, wxWindow::PopEventHandler
, false );
326 TestEvtHandler
h2('2');
327 child
->PushEventHandler(&h2
);
328 wxON_BLOCK_EXIT_OBJ1( *child
, wxWindow::PopEventHandler
, false );
330 child
->HandleWindowEvent(event
);
331 CPPUNIT_ASSERT_EQUAL( "oa2o1cpA", g_str
);
334 void EventPropagationTestCase::ForwardEvent()
336 // The idea of this test is to check that the events explicitly forwarded
337 // to another event handler still get pre/post-processed as usual as this
338 // used to be broken by the fixes trying to avoid duplicate processing.
339 TestWindow
* const win
= new TestWindow(wxTheApp
->GetTopWindow(), 'w');
340 wxON_BLOCK_EXIT_OBJ0( *win
, wxWindow::Destroy
);
342 TestEvtHandler
h1('1');
343 win
->PushEventHandler(&h1
);
344 wxON_BLOCK_EXIT_OBJ1( *win
, wxWindow::PopEventHandler
, false );
346 class ForwardEvtHandler
: public wxEvtHandler
349 ForwardEvtHandler(wxEvtHandler
& h
) : m_h(&h
) { }
351 virtual bool ProcessEvent(wxEvent
& event
)
355 return m_h
->ProcessEvent(event
);
362 // First send the event directly to f.
363 wxCommandEvent
event1(TEST_EVT
);
364 f
.ProcessEvent(event1
);
365 CPPUNIT_ASSERT_EQUAL( "foa1wA", g_str
);
368 // And then also test sending it to f indirectly.
369 wxCommandEvent
event2(TEST_EVT
);
370 TestEvtHandler
h2('2');
371 h2
.SetNextHandler(&f
);
372 h2
.ProcessEvent(event2
);
373 CPPUNIT_ASSERT_EQUAL( "oa2fo1wAA", g_str
);
376 void EventPropagationTestCase::ScrollWindowWithoutHandler()
378 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
379 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
381 TestScrollWindow
* const win
= new TestScrollWindow(parent
);
383 #ifdef CAN_TEST_PAINT_EVENTS
384 win
->GeneratePaintEvent();
385 CPPUNIT_ASSERT_EQUAL( "PD", g_str
);
389 wxCommandEvent
eventCmd(TEST_EVT
);
390 win
->HandleWindowEvent(eventCmd
);
391 CPPUNIT_ASSERT_EQUAL( "apA", g_str
);
394 void EventPropagationTestCase::ScrollWindowWithHandler()
396 TestWindow
* const parent
= new TestWindow(wxTheApp
->GetTopWindow(), 'p');
397 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
399 TestScrollWindow
* const win
= new TestScrollWindow(parent
);
401 #ifdef CAN_TEST_PAINT_EVENTS
402 TestPaintEvtHandler
h('h');
403 win
->PushEventHandler(&h
);
404 wxON_BLOCK_EXIT_OBJ1( *win
, wxWindow::PopEventHandler
, false );
406 win
->GeneratePaintEvent();
407 CPPUNIT_ASSERT_EQUAL( "ohPD", g_str
);
411 wxCommandEvent
eventCmd(TEST_EVT
);
412 win
->HandleWindowEvent(eventCmd
);
413 CPPUNIT_ASSERT_EQUAL( "apA", g_str
);
416 // Create a menu bar with a single menu containing wxID_APPLY menu item and
417 // attach it to the specified frame.
418 wxMenu
* CreateTestMenu(wxFrame
* frame
)
420 wxMenu
* const menu
= new wxMenu
;
421 menu
->Append(wxID_APPLY
);
422 wxMenuBar
* const mb
= new wxMenuBar
;
423 mb
->Append(menu
, "&Menu");
424 frame
->SetMenuBar(mb
);
429 // Helper for checking that the menu event processing resulted in the expected
430 // output from the handlers.
432 // Notice that this is supposed to be used with ASSERT_MENU_EVENT_RESULT()
433 // macro to make the file name and line number of the caller appear in the
436 CheckMenuEvent(wxMenu
* menu
, const char* result
, CppUnit::SourceLine sourceLine
)
440 // Trigger the menu event: this is more reliable than using
441 // wxUIActionSimulator and currently works in all ports as they all call
442 // wxMenuBase::SendEvent() from their respective menu event handlers.
443 menu
->SendEvent(wxID_APPLY
);
445 CPPUNIT_NS::assertEquals( result
, g_str
, sourceLine
, "" );
448 #define ASSERT_MENU_EVENT_RESULT(menu, result) \
449 CheckMenuEvent((menu), (result), CPPUNIT_SOURCELINE())
451 void EventPropagationTestCase::MenuEvent()
453 wxFrame
* const frame
= static_cast<wxFrame
*>(wxTheApp
->GetTopWindow());
455 // Create a minimal menu bar.
456 wxMenu
* const menu
= CreateTestMenu(frame
);
457 wxMenuBar
* const mb
= menu
->GetMenuBar();
458 wxScopedPtr
<wxMenuBar
> ensureMenuBarDestruction(mb
);
459 wxON_BLOCK_EXIT_OBJ1( *frame
, wxFrame::SetMenuBar
, (wxMenuBar
*)NULL
);
461 // Check that wxApp gets the event exactly once.
462 ASSERT_MENU_EVENT_RESULT( menu
, "aA" );
465 // Check that the menu event handler is called.
466 TestMenuEvtHandler
hm('m'); // 'm' for "menu"
467 menu
->SetNextHandler(&hm
);
468 wxON_BLOCK_EXIT_OBJ1( *menu
,
469 wxEvtHandler::SetNextHandler
, (wxEvtHandler
*)NULL
);
470 ASSERT_MENU_EVENT_RESULT( menu
, "aomA" );
473 // Test that the event handler associated with the menu bar gets the event.
474 TestMenuEvtHandler
hb('b'); // 'b' for "menu Bar"
475 mb
->PushEventHandler(&hb
);
476 wxON_BLOCK_EXIT_OBJ1( *mb
, wxWindow::PopEventHandler
, false );
478 ASSERT_MENU_EVENT_RESULT( menu
, "aomobA" );
481 // Also test that the window to which the menu belongs gets the event.
482 TestMenuEvtHandler
hw('w'); // 'w' for "Window"
483 frame
->PushEventHandler(&hw
);
484 wxON_BLOCK_EXIT_OBJ1( *frame
, wxWindow::PopEventHandler
, false );
486 ASSERT_MENU_EVENT_RESULT( menu
, "aomobowA" );
489 // Minimal viable implementations of wxDocument and wxView.
490 class EventTestDocument
: public wxDocument
493 EventTestDocument() { }
495 wxDECLARE_DYNAMIC_CLASS(EventTestDocument
);
498 class EventTestView
: public wxView
503 virtual void OnDraw(wxDC
*) { }
505 wxDECLARE_DYNAMIC_CLASS(EventTestView
);
508 wxIMPLEMENT_DYNAMIC_CLASS(EventTestDocument
, wxDocument
);
509 wxIMPLEMENT_DYNAMIC_CLASS(EventTestView
, wxView
);
511 void EventPropagationTestCase::DocView()
513 // Set up the parent frame and its menu bar.
514 wxDocManager docManager
;
516 wxScopedPtr
<wxDocMDIParentFrame
>
517 parent(new wxDocMDIParentFrame(&docManager
, NULL
, wxID_ANY
, "Parent"));
519 wxMenu
* const menu
= CreateTestMenu(parent
.get());
522 // Set up the event handlers.
523 TestEvtSink
sinkDM('m');
524 docManager
.Connect(wxEVT_MENU
,
525 wxEventHandler(TestEvtSink::Handle
), NULL
, &sinkDM
);
527 TestEvtSink
sinkParent('p');
528 parent
->Connect(wxEVT_MENU
,
529 wxEventHandler(TestEvtSink::Handle
), NULL
, &sinkParent
);
532 // Check that wxDocManager and wxFrame get the event in order.
533 ASSERT_MENU_EVENT_RESULT( menu
, "ampA" );
536 // Now check what happens if we have an active document.
537 wxDocTemplate
docTemplate(&docManager
, "Test", "", "", "",
538 "Test Document", "Test View",
539 wxCLASSINFO(EventTestDocument
),
540 wxCLASSINFO(EventTestView
));
541 wxDocument
* const doc
= docTemplate
.CreateDocument("");
542 wxView
* const view
= doc
->GetFirstView();
544 wxScopedPtr
<wxMDIChildFrame
>
545 child(new wxDocMDIChildFrame(doc
, view
, parent
.get(), wxID_ANY
, "Child"));
547 wxMenu
* const menuChild
= CreateTestMenu(child
.get());
549 // Ensure that the child that we've just created is the active one.
553 // There are a lot of hacks related to child frame menu bar handling in
554 // wxGTK and, in particular, the code in src/gtk/mdi.cpp relies on getting
555 // idle events to really put everything in place. Moreover, as wxGTK uses
556 // GtkNotebook as its MDI pages container, the frame must be shown for all
557 // this to work as gtk_notebook_set_current_page() doesn't do anything if
558 // called for a hidden window (this incredible fact cost me quite some time
559 // to find empirically -- only to notice its confirmation in GTK+
560 // documentation immediately afterwards). So just do whatever it takes to
561 // make things work "as usual".
567 TestEvtSink
sinkDoc('d');
568 doc
->Connect(wxEVT_MENU
,
569 wxEventHandler(TestEvtSink::Handle
), NULL
, &sinkDoc
);
571 TestEvtSink
sinkView('v');
572 view
->Connect(wxEVT_MENU
,
573 wxEventHandler(TestEvtSink::Handle
), NULL
, &sinkView
);
575 TestEvtSink
sinkChild('c');
576 child
->Connect(wxEVT_MENU
,
577 wxEventHandler(TestEvtSink::Handle
), NULL
, &sinkChild
);
579 // Check that wxDocument, wxView, wxDocManager, child frame and the parent
580 // get the event in order.
581 ASSERT_MENU_EVENT_RESULT( menuChild
, "advmcpA" );
585 // Also check that toolbar events get forwarded to the active child.
586 wxToolBar
* const tb
= parent
->CreateToolBar(wxTB_NOICONS
);
587 tb
->AddTool(wxID_APPLY
, "Apply", wxNullBitmap
);
590 // As in CheckMenuEvent(), use toolbar method actually sending the event
591 // instead of bothering with wxUIActionSimulator which would have been
594 tb
->OnLeftClick(wxID_APPLY
, true /* doesn't matter */);
596 CPPUNIT_ASSERT_EQUAL( "advmcpA", g_str
);
597 #endif // wxUSE_TOOLBAR
600 #if wxUSE_UIACTIONSIMULATOR
602 class ContextMenuTestWindow
: public wxWindow
605 ContextMenuTestWindow(wxWindow
*parent
, char tag
)
606 : wxWindow(parent
, wxID_ANY
),
609 Connect(wxEVT_CONTEXT_MENU
,
610 wxContextMenuEventHandler(ContextMenuTestWindow::OnMenu
));
614 void OnMenu(wxContextMenuEvent
& event
)
623 wxDECLARE_NO_COPY_CLASS(ContextMenuTestWindow
);
626 void EventPropagationTestCase::ContextMenuEvent()
628 ContextMenuTestWindow
* const
629 parent
= new ContextMenuTestWindow(wxTheApp
->GetTopWindow(), 'p');
630 wxON_BLOCK_EXIT_OBJ0( *parent
, wxWindow::Destroy
);
632 ContextMenuTestWindow
* const
633 child
= new ContextMenuTestWindow(parent
, 'c');
634 parent
->SetSize(100, 100);
635 child
->SetSize(0, 0, 50, 50);
638 wxUIActionSimulator sim
;
639 const wxPoint origin
= parent
->ClientToScreen(wxPoint(0, 0));
641 // Right clicking in the child should generate an event for it and the
644 sim
.MouseMove(origin
+ wxPoint(10, 10));
645 sim
.MouseClick(wxMOUSE_BTN_RIGHT
);
647 // At least with MSW, for WM_CONTEXTMENU to be synthesized by the system
648 // from the right mouse click event, we must dispatch the mouse messages.
651 CPPUNIT_ASSERT_EQUAL( "cp", g_str
);
653 // Right clicking outside the child should generate the event just in the
656 sim
.MouseMove(origin
+ wxPoint(60, 60));
657 sim
.MouseClick(wxMOUSE_BTN_RIGHT
);
659 CPPUNIT_ASSERT_EQUAL( "p", g_str
);
662 #endif // wxUSE_UIACTIONSIMULATOR