2009-01-16 11:21:50 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/events/propagation.cpp
|
|
|
|
// Purpose: Test events propagation
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2009-01-16
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
2009-01-17 06:38:32 -05:00
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/event.h"
|
2010-05-20 13:33:26 -04:00
|
|
|
#include "wx/scrolwin.h"
|
2009-01-17 06:38:32 -05:00
|
|
|
#include "wx/window.h"
|
2009-01-16 11:21:50 -05:00
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "wx/scopeguard.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// this string will record the execution of all handlers
|
|
|
|
wxString g_str;
|
|
|
|
|
|
|
|
// a custom event
|
|
|
|
wxDEFINE_EVENT(TEST_EVT, wxCommandEvent);
|
|
|
|
|
2010-05-20 13:33:26 -04:00
|
|
|
// a custom event handler tracing the propagation of the events of the
|
|
|
|
// specified types
|
|
|
|
template <class Event>
|
|
|
|
class TestEvtHandlerBase : public wxEvtHandler
|
2009-01-16 11:21:50 -05:00
|
|
|
{
|
|
|
|
public:
|
2010-05-20 13:33:26 -04:00
|
|
|
TestEvtHandlerBase(wxEventType evtType, char tag)
|
|
|
|
: m_evtType(evtType),
|
|
|
|
m_tag(tag)
|
2009-01-16 11:21:50 -05:00
|
|
|
{
|
2010-05-20 13:33:26 -04:00
|
|
|
Connect(evtType,
|
|
|
|
static_cast<wxEventFunction>(&TestEvtHandlerBase::OnTest));
|
2009-01-16 11:21:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// override ProcessEvent() to confirm that it is called for all event
|
|
|
|
// handlers in the chain
|
|
|
|
virtual bool ProcessEvent(wxEvent& event)
|
|
|
|
{
|
2010-05-20 13:33:26 -04:00
|
|
|
if ( event.GetEventType() == m_evtType )
|
2009-01-16 11:21:50 -05:00
|
|
|
g_str += 'o'; // "o" == "overridden"
|
|
|
|
|
|
|
|
return wxEvtHandler::ProcessEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-05-20 13:33:26 -04:00
|
|
|
void OnTest(wxEvent& event)
|
2009-01-16 11:21:50 -05:00
|
|
|
{
|
|
|
|
g_str += m_tag;
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
2010-05-20 13:33:26 -04:00
|
|
|
const wxEventType m_evtType;
|
2009-01-16 11:21:50 -05:00
|
|
|
const char m_tag;
|
|
|
|
|
2010-05-20 13:33:26 -04:00
|
|
|
wxDECLARE_NO_COPY_TEMPLATE_CLASS(TestEvtHandlerBase, Event);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestEvtHandler : TestEvtHandlerBase<wxCommandEvent>
|
|
|
|
{
|
|
|
|
TestEvtHandler(char tag)
|
|
|
|
: TestEvtHandlerBase<wxCommandEvent>(TEST_EVT, tag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestPaintEvtHandler : TestEvtHandlerBase<wxPaintEvent>
|
|
|
|
{
|
|
|
|
TestPaintEvtHandler(char tag)
|
|
|
|
: TestEvtHandlerBase<wxPaintEvent>(wxEVT_PAINT, tag)
|
|
|
|
{
|
|
|
|
}
|
2009-01-16 11:21:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
};
|
|
|
|
|
2010-05-20 13:33:26 -04:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2009-01-16 11:21:50 -05:00
|
|
|
int DoFilterEvent(wxEvent& event)
|
|
|
|
{
|
|
|
|
if ( event.GetEventType() == TEST_EVT )
|
|
|
|
g_str += 'a';
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DoProcessEvent(wxEvent& event)
|
|
|
|
{
|
|
|
|
if ( event.GetEventType() == TEST_EVT )
|
|
|
|
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 );
|
2010-05-20 13:33:26 -04:00
|
|
|
CPPUNIT_TEST( ScrollWindowWithoutHandler );
|
|
|
|
CPPUNIT_TEST( ScrollWindowWithHandler );
|
2009-01-16 11:21:50 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void OneHandler();
|
|
|
|
void TwoHandlers();
|
|
|
|
void WindowWithoutHandler();
|
|
|
|
void WindowWithHandler();
|
2010-05-20 13:33:26 -04:00
|
|
|
void ScrollWindowWithoutHandler();
|
|
|
|
void ScrollWindowWithHandler();
|
2009-01-16 11:21:50 -05:00
|
|
|
|
|
|
|
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 it's 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');
|
|
|
|
|
2009-01-25 06:58:39 -05:00
|
|
|
child->GetEventHandler()->ProcessEvent(event);
|
2009-01-16 11:21:50 -05:00
|
|
|
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);
|
2009-01-17 08:01:47 -05:00
|
|
|
wxON_BLOCK_EXIT_OBJ1( *child, wxWindow::PopEventHandler, false );
|
2009-01-16 11:21:50 -05:00
|
|
|
TestEvtHandler h2('2');
|
|
|
|
child->PushEventHandler(&h2);
|
2009-01-17 08:01:47 -05:00
|
|
|
wxON_BLOCK_EXIT_OBJ1( *child, wxWindow::PopEventHandler, false );
|
2009-01-16 11:21:50 -05:00
|
|
|
|
|
|
|
child->HandleWindowEvent(event);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "oa2o1cpA", g_str );
|
|
|
|
}
|
|
|
|
|
2010-05-20 13:33:26 -04:00
|
|
|
void EventPropagationTestCase::ScrollWindowWithoutHandler()
|
|
|
|
{
|
2010-05-21 08:07:45 -04:00
|
|
|
TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
|
|
|
|
wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
|
|
|
|
|
|
|
|
TestScrollWindow * const win = new TestScrollWindow(parent);
|
2010-05-20 13:33:26 -04:00
|
|
|
|
|
|
|
wxPaintEvent event(win->GetId());
|
|
|
|
win->ProcessWindowEvent(event);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "PD", g_str );
|
2010-05-21 08:07:45 -04:00
|
|
|
|
|
|
|
g_str.clear();
|
|
|
|
wxCommandEvent eventCmd(TEST_EVT);
|
|
|
|
win->HandleWindowEvent(eventCmd);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "apA", g_str );
|
2010-05-20 13:33:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventPropagationTestCase::ScrollWindowWithHandler()
|
|
|
|
{
|
2010-05-21 08:07:45 -04:00
|
|
|
TestWindow * const parent = new TestWindow(wxTheApp->GetTopWindow(), 'p');
|
|
|
|
wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
|
|
|
|
|
|
|
|
TestScrollWindow * const win = new TestScrollWindow(parent);
|
2010-05-20 13:33:26 -04:00
|
|
|
|
|
|
|
TestPaintEvtHandler h('h');
|
|
|
|
win->PushEventHandler(&h);
|
|
|
|
wxON_BLOCK_EXIT_OBJ1( *win, wxWindow::PopEventHandler, false );
|
|
|
|
|
|
|
|
wxPaintEvent event(win->GetId());
|
|
|
|
win->ProcessWindowEvent(event);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "ohPD", g_str );
|
2010-05-21 08:07:45 -04:00
|
|
|
|
|
|
|
g_str.clear();
|
|
|
|
wxCommandEvent eventCmd(TEST_EVT);
|
|
|
|
win->HandleWindowEvent(eventCmd);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "apA", g_str );
|
2010-05-20 13:33:26 -04:00
|
|
|
}
|
|
|
|
|