1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/events/propagation.cpp
// Purpose: Test events propagation
// Author: Vadim Zeitlin
// Created: 2009-01-16
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/event.h"
#include "wx/scrolwin.h"
#include "wx/window.h"
#endif // WX_PRECOMP
#include "wx/docmdi.h"
#include "wx/frame.h"
#include "wx/menu.h"
#include "wx/scopedptr.h"
#include "wx/scopeguard.h"
#include "wx/toolbar.h"
#include "wx/uiaction.h"
// FIXME: Currently under OS X testing paint event doesn't work because neither
// calling Refresh()+Update() nor even sending wxPaintEvent directly to
// the window doesn't result in calls to its event handlers, so disable
// some tests there. But this should be fixed and the tests reenabled
// because wxPaintEvent propagation in wxScrolledWindow is a perfect
// example of fragile code that could be broken under OS X.
#ifndef __WXOSX__
#define CAN_TEST_PAINT_EVENTS
#endif
namespace
{
// this string will record the execution of all handlers
wxString g_str;
// a custom event
wxDEFINE_EVENT(TEST_EVT, wxCommandEvent);
// a custom event handler tracing the propagation of the events of the
// specified types
template <class Event>
class TestEvtHandlerBase : public wxEvtHandler
{
public:
TestEvtHandlerBase(wxEventType evtType, char tag)
: m_evtType(evtType),
m_tag(tag)
{
Connect(evtType,
static_cast<wxEventFunction>(&TestEvtHandlerBase::OnTest));
}
// override ProcessEvent() to confirm that it is called for all event
// handlers in the chain
virtual bool ProcessEvent(wxEvent& event)
{
if ( event.GetEventType() == m_evtType )
g_str += 'o'; // "o" == "overridden"
return wxEvtHandler::ProcessEvent(event);
}
private:
void OnTest(wxEvent& event)
{
g_str += m_tag;
event.Skip();
}
const wxEventType m_evtType;
const char m_tag;
wxDECLARE_NO_COPY_TEMPLATE_CLASS(TestEvtHandlerBase, Event);
};
struct TestEvtHandler : TestEvtHandlerBase<wxCommandEvent>
{
TestEvtHandler(char tag)
: TestEvtHandlerBase<wxCommandEvent>(TEST_EVT, tag)
{
}
};
struct TestMenuEvtHandler : TestEvtHandlerBase<wxCommandEvent>
{
TestMenuEvtHandler(char tag)
: TestEvtHandlerBase<wxCommandEvent>(wxEVT_MENU, tag)
{
}
};
struct TestPaintEvtHandler : TestEvtHandlerBase<wxPaintEvent>
{
TestPaintEvtHandler(char tag)
: TestEvtHandlerBase<wxPaintEvent>(wxEVT_PAINT, tag)
{
}
};
// Another custom event handler, suitable for use with Connect().
struct TestEvtSink : wxEvtHandler
{
TestEvtSink(char tag)
: m_tag(tag)
{
}
void Handle(wxEvent& event)
{
g_str += m_tag;
event.Skip();
}
const char m_tag;
wxDECLARE_NO_COPY_CLASS(TestEvtSink);
};
// a window handling the test event
class TestWindow : public wxWindow
{
public:
TestWindow(wxWindow *parent, char tag)
: wxWindow(parent, wxID_ANY),
m_tag(tag)
{
Connect(TEST_EVT, wxCommandEventHandler(TestWindow::OnTest));
}
private:
void OnTest(wxCommandEvent& event)
{
g_str += m_tag;
event.Skip();
}
const char m_tag;
DECLARE_NO_COPY_CLASS(TestWindow)
};
// a scroll window handling paint event: we want to have a special test case
// for this because the event propagation is complicated even further than
// usual here by the presence of wxScrollHelperEvtHandler in the event handlers
// chain and the fact that OnDraw() virtual method must be called if EVT_PAINT
// is not handled
class TestScrollWindow : public wxScrolledWindow
{
public:
TestScrollWindow(wxWindow *parent)
: wxScrolledWindow(parent, wxID_ANY)
{
Connect(wxEVT_PAINT, wxPaintEventHandler(TestScrollWindow::OnPaint));
}
void GeneratePaintEvent()
{
#ifdef __WXGTK__
// We need to map the window, otherwise we're not going to get any
// paint events for it.
wxYield();
// Ignore events generated during the initial mapping.
g_str.clear();
#endif // __WXGTK__
Refresh();
Update();
}
virtual void OnDraw(wxDC& WXUNUSED(dc))
{
g_str += 'D'; // draw
}
private:
void OnPaint(wxPaintEvent& event)
{
g_str += 'P'; // paint
event.Skip();
}
wxDECLARE_NO_COPY_CLASS(TestScrollWindow);
};
int DoFilterEvent(wxEvent& event)
{
if ( event.GetEventType() == TEST_EVT ||
event.GetEventType() == wxEVT_MENU )
g_str += 'a';
return -1;
}
bool DoProcessEvent(wxEvent& event)
{
if ( event.GetEventType() == TEST_EVT ||
event.GetEventType() == wxEVT_MENU )
g_str += 'A';
return false;
}
} // anonymous namespace
// --------------------------------------------------------------------------
// test class
// --------------------------------------------------------------------------
class EventPropagationTestCase : public CppUnit::TestCase
{
public:
EventPropagationTestCase() {}
virtual void setUp();
virtual void tearDown();
private:
CPPUNIT_TEST_SUITE( EventPropagationTestCase );
CPPUNIT_TEST( OneHandler );
CPPUNIT_TEST( TwoHandlers );
CPPUNIT_TEST( WindowWithoutHandler );
CPPUNIT_TEST( WindowWithHandler );
CPPUNIT_TEST( ForwardEvent );
CPPUNIT_TEST( ScrollWindowWithoutHandler );
CPPUNIT_TEST( ScrollWindowWithHandler );
CPPUNIT_TEST( MenuEvent );
CPPUNIT_TEST( DocView );
WXUISIM_TEST( ContextMenuEvent );
CPPUNIT_TEST_SUITE_END();
void OneHandler();
void TwoHandlers();
void WindowWithoutHandler();
void WindowWithHandler();
void ForwardEvent();
void ScrollWindowWithoutHandler();
void ScrollWindowWithHandler();
void MenuEvent();
void DocView();
void ContextMenuEvent();
DECLARE_NO_COPY_CLASS(EventPropagationTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( EventPropagationTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EventPropagationTestCase, "EventPropagationTestCase" );
void EventPropagationTestCase::setUp()
{
SetFilterEventFunc(DoFilterEvent);
SetProcessEventFunc(DoProcessEvent);
g_str.clear();
}
void EventPropagationTestCase::tearDown()
{
SetFilterEventFunc(NULL);
SetProcessEventFunc(NULL);
}
void EventPropagationTestCase::OneHandler()
{
wxCommandEvent event(TEST_EVT);
TestEvtHandler h1('1');
h1.ProcessEvent(event);
CPPUNIT_ASSERT_EQUAL( "oa1A", g_str );
}
void EventPropagationTestCase::TwoHandlers()
{
wxCommandEvent event(TEST_EVT);
TestEvtHandler h1('1');
TestEvtHandler h2('2');
h1.SetNextHandler(&h2);
h2.SetPreviousHandler(&h1);
h1.ProcessEvent(event);
CPPUNIT_ASSERT_EQUAL( "oa1o2A", g_str );
}
void EventPropagationTestCase::WindowWithoutHandler()
{
wxCommandEvent event(TEST_EVT);
TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
TestWindow * const child = new TestWindow(parent, 'c');
child->GetEventHandler()->ProcessEvent(event);
CPPUNIT_ASSERT_EQUAL( "acpA", g_str );
}
void EventPropagationTestCase::WindowWithHandler()
{
wxCommandEvent event(TEST_EVT);
TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
TestWindow * const child = new TestWindow(parent, 'c');
TestEvtHandler h1('1');
child->PushEventHandler(&h1);
wxON_BLOCK_EXIT_OBJ1( *child, wxWindow::PopEventHandler, false );
TestEvtHandler h2('2');
child->PushEventHandler(&h2);
wxON_BLOCK_EXIT_OBJ1( *child, wxWindow::PopEventHandler, false );
child->HandleWindowEvent(event);
CPPUNIT_ASSERT_EQUAL( "oa2o1cpA", g_str );
}
void EventPropagationTestCase::ForwardEvent()
{
// The idea of this test is to check that the events explicitly forwarded
// to another event handler still get pre/post-processed as usual as this
// used to be broken by the fixes trying to avoid duplicate processing.
TestWindow * const win = new TestWindow(wxTheApp->GetTopWindow(), 'w');
wxON_BLOCK_EXIT_OBJ0( *win, wxWindow::Destroy );
TestEvtHandler h1('1');
win->PushEventHandler(&h1);
wxON_BLOCK_EXIT_OBJ1( *win, wxWindow::PopEventHandler, false );
class ForwardEvtHandler : public wxEvtHandler
{
public:
ForwardEvtHandler(wxEvtHandler& h) : m_h(&h) { }
virtual bool ProcessEvent(wxEvent& event)
{
g_str += 'f';
return m_h->ProcessEvent(event);
}
private:
wxEvtHandler *m_h;
} f(h1);
// First send the event directly to f.
wxCommandEvent event1(TEST_EVT);
f.ProcessEvent(event1);
CPPUNIT_ASSERT_EQUAL( "foa1wA", g_str );
g_str.clear();
// And then also test sending it to f indirectly.
wxCommandEvent event2(TEST_EVT);
TestEvtHandler h2('2');
h2.SetNextHandler(&f);
h2.ProcessEvent(event2);
CPPUNIT_ASSERT_EQUAL( "oa2fo1wAA", g_str );
}
void EventPropagationTestCase::ScrollWindowWithoutHandler()
{
TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
TestScrollWindow * const win = new TestScrollWindow(parent);
#ifdef CAN_TEST_PAINT_EVENTS
win->GeneratePaintEvent();
CPPUNIT_ASSERT_EQUAL( "PD", g_str );
#endif
g_str.clear();
wxCommandEvent eventCmd(TEST_EVT);
win->HandleWindowEvent(eventCmd);
CPPUNIT_ASSERT_EQUAL( "apA", g_str );
}
void EventPropagationTestCase::ScrollWindowWithHandler()
{
TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
TestScrollWindow * const win = new TestScrollWindow(parent);
#ifdef CAN_TEST_PAINT_EVENTS
TestPaintEvtHandler h('h');
win->PushEventHandler(&h);
wxON_BLOCK_EXIT_OBJ1( *win, wxWindow::PopEventHandler, false );
win->GeneratePaintEvent();
CPPUNIT_ASSERT_EQUAL( "ohPD", g_str );
#endif
g_str.clear();
wxCommandEvent eventCmd(TEST_EVT);
win->HandleWindowEvent(eventCmd);
CPPUNIT_ASSERT_EQUAL( "apA", g_str );
}
// Create a menu bar with a single menu containing wxID_APPLY menu item and
// attach it to the specified frame.
wxMenu* CreateTestMenu(wxFrame* frame)
{
wxMenu* const menu = new wxMenu;
menu->Append(wxID_APPLY);
wxMenuBar* const mb = new wxMenuBar;
mb->Append(menu, "&Menu");
frame->SetMenuBar(mb);
return menu;
}
// Helper for checking that the menu event processing resulted in the expected
// output from the handlers.
//
// Notice that this is supposed to be used with ASSERT_MENU_EVENT_RESULT()
// macro to make the file name and line number of the caller appear in the
// failure messages.
void
CheckMenuEvent(wxMenu* menu, const char* result, CppUnit::SourceLine sourceLine)
{
g_str.clear();
// Trigger the menu event: this is more reliable than using
// wxUIActionSimulator and currently works in all ports as they all call
// wxMenuBase::SendEvent() from their respective menu event handlers.
menu->SendEvent(wxID_APPLY);
CPPUNIT_NS::assertEquals( result, g_str, sourceLine, "" );
}
#define ASSERT_MENU_EVENT_RESULT(menu, result) \
CheckMenuEvent((menu), (result), CPPUNIT_SOURCELINE())
void EventPropagationTestCase::MenuEvent()
{
wxFrame* const frame = static_cast<wxFrame*>(wxTheApp->GetTopWindow());
// Create a minimal menu bar.
wxMenu* const menu = CreateTestMenu(frame);
wxMenuBar* const mb = menu->GetMenuBar();
wxScopedPtr<wxMenuBar> ensureMenuBarDestruction(mb);
wxON_BLOCK_EXIT_OBJ1( *frame, wxFrame::SetMenuBar, (wxMenuBar*)NULL );
// Check that wxApp gets the event exactly once.
ASSERT_MENU_EVENT_RESULT( menu, "aA" );
// Check that the menu event handler is called.
TestMenuEvtHandler hm('m'); // 'm' for "menu"
menu->SetNextHandler(&hm);
wxON_BLOCK_EXIT_OBJ1( *menu,
wxEvtHandler::SetNextHandler, (wxEvtHandler*)NULL );
ASSERT_MENU_EVENT_RESULT( menu, "aomA" );
// Test that the event handler associated with the menu bar gets the event.
TestMenuEvtHandler hb('b'); // 'b' for "menu Bar"
mb->PushEventHandler(&hb);
wxON_BLOCK_EXIT_OBJ1( *mb, wxWindow::PopEventHandler, false );
ASSERT_MENU_EVENT_RESULT( menu, "aomobA" );
// Also test that the window to which the menu belongs gets the event.
TestMenuEvtHandler hw('w'); // 'w' for "Window"
frame->PushEventHandler(&hw);
wxON_BLOCK_EXIT_OBJ1( *frame, wxWindow::PopEventHandler, false );
ASSERT_MENU_EVENT_RESULT( menu, "aomobowA" );
}
// Minimal viable implementations of wxDocument and wxView.
class EventTestDocument : public wxDocument
{
public:
EventTestDocument() { }
wxDECLARE_DYNAMIC_CLASS(EventTestDocument);
};
class EventTestView : public wxView
{
public:
EventTestView() { }
virtual void OnDraw(wxDC*) { }
wxDECLARE_DYNAMIC_CLASS(EventTestView);
};
wxIMPLEMENT_DYNAMIC_CLASS(EventTestDocument, wxDocument);
wxIMPLEMENT_DYNAMIC_CLASS(EventTestView, wxView);
void EventPropagationTestCase::DocView()
{
// Set up the parent frame and its menu bar.
wxDocManager docManager;
wxScopedPtr<wxDocMDIParentFrame>
parent(new wxDocMDIParentFrame(&docManager, NULL, wxID_ANY, "Parent"));
wxMenu* const menu = CreateTestMenu(parent.get());
// Set up the event handlers.
TestEvtSink sinkDM('m');
docManager.Connect(wxEVT_MENU,
wxEventHandler(TestEvtSink::Handle), NULL, &sinkDM);
TestEvtSink sinkParent('p');
parent->Connect(wxEVT_MENU,
wxEventHandler(TestEvtSink::Handle), NULL, &sinkParent);
// Check that wxDocManager and wxFrame get the event in order.
ASSERT_MENU_EVENT_RESULT( menu, "ampA" );
// Now check what happens if we have an active document.
wxDocTemplate docTemplate(&docManager, "Test", "", "", "",
"Test Document", "Test View",
wxCLASSINFO(EventTestDocument),
wxCLASSINFO(EventTestView));
wxDocument* const doc = docTemplate.CreateDocument("");
wxView* const view = doc->GetFirstView();
wxScopedPtr<wxMDIChildFrame>
child(new wxDocMDIChildFrame(doc, view, parent.get(), wxID_ANY, "Child"));
wxMenu* const menuChild = CreateTestMenu(child.get());
// Ensure that the child that we've just created is the active one.
child->Activate();
#ifdef __WXGTK__
// There are a lot of hacks related to child frame menu bar handling in
// wxGTK and, in particular, the code in src/gtk/mdi.cpp relies on getting
// idle events to really put everything in place. Moreover, as wxGTK uses
// GtkNotebook as its MDI pages container, the frame must be shown for all
// this to work as gtk_notebook_set_current_page() doesn't do anything if
// called for a hidden window (this incredible fact cost me quite some time
// to find empirically -- only to notice its confirmation in GTK+
// documentation immediately afterwards). So just do whatever it takes to
// make things work "as usual".
child->Show();
parent->Show();
wxYield();
#endif // __WXGTK__
TestEvtSink sinkDoc('d');
doc->Connect(wxEVT_MENU,
wxEventHandler(TestEvtSink::Handle), NULL, &sinkDoc);
TestEvtSink sinkView('v');
view->Connect(wxEVT_MENU,
wxEventHandler(TestEvtSink::Handle), NULL, &sinkView);
TestEvtSink sinkChild('c');
child->Connect(wxEVT_MENU,
wxEventHandler(TestEvtSink::Handle), NULL, &sinkChild);
// Check that wxDocument, wxView, wxDocManager, child frame and the parent
// get the event in order.
ASSERT_MENU_EVENT_RESULT( menuChild, "advmcpA" );
#if wxUSE_TOOLBAR
// Also check that toolbar events get forwarded to the active child.
wxToolBar* const tb = parent->CreateToolBar(wxTB_NOICONS);
tb->AddTool(wxID_APPLY, "Apply", wxNullBitmap);
tb->Realize();
// As in CheckMenuEvent(), use toolbar method actually sending the event
// instead of bothering with wxUIActionSimulator which would have been
// trickier.
g_str.clear();
tb->OnLeftClick(wxID_APPLY, true /* doesn't matter */);
CPPUNIT_ASSERT_EQUAL( "advmcpA", g_str );
#endif // wxUSE_TOOLBAR
}
#if wxUSE_UIACTIONSIMULATOR
class ContextMenuTestWindow : public wxWindow
{
public:
ContextMenuTestWindow(wxWindow *parent, char tag)
: wxWindow(parent, wxID_ANY),
m_tag(tag)
{
Connect(wxEVT_CONTEXT_MENU,
wxContextMenuEventHandler(ContextMenuTestWindow::OnMenu));
}
private:
void OnMenu(wxContextMenuEvent& event)
{
g_str += m_tag;
event.Skip();
}
const char m_tag;
wxDECLARE_NO_COPY_CLASS(ContextMenuTestWindow);
};
void EventPropagationTestCase::ContextMenuEvent()
{
ContextMenuTestWindow * const
parent = new ContextMenuTestWindow(wxTheApp->GetTopWindow(), 'p');
wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
ContextMenuTestWindow * const
child = new ContextMenuTestWindow(parent, 'c');
parent->SetSize(100, 100);
child->SetSize(0, 0, 50, 50);
child->SetFocus();
wxUIActionSimulator sim;
const wxPoint origin = parent->ClientToScreen(wxPoint(0, 0));
// Right clicking in the child should generate an event for it and the
// parent.
g_str.clear();
sim.MouseMove(origin + wxPoint(10, 10));
sim.MouseClick(wxMOUSE_BTN_RIGHT);
// At least with MSW, for WM_CONTEXTMENU to be synthesized by the system
// from the right mouse click event, we must dispatch the mouse messages.
wxYield();
CPPUNIT_ASSERT_EQUAL( "cp", g_str );
// Right clicking outside the child should generate the event just in the
// parent.
g_str.clear();
sim.MouseMove(origin + wxPoint(60, 60));
sim.MouseClick(wxMOUSE_BTN_RIGHT);
wxYield();
CPPUNIT_ASSERT_EQUAL( "p", g_str );
}
#endif // wxUSE_UIACTIONSIMULATOR
|