2009-02-01 17:12:12 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/events/evthandler.cpp
|
|
|
|
// Purpose: Test the new event types and wxEvtHandler-methods
|
|
|
|
// Author: Peter Most
|
|
|
|
// Created: 2009-01-24
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2009 Peter Most
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/event.h"
|
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test events and their handlers
|
|
|
|
// ----------------------------------------------------------------------------
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-06 13:33:01 -05:00
|
|
|
const wxEventType LegacyEventType = wxNewEventType();
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-01 18:19:45 -05:00
|
|
|
class MyEvent;
|
2009-02-07 16:04:47 -05:00
|
|
|
wxDEFINE_EVENT(MyEventType, MyEvent);
|
2009-02-01 18:19:45 -05:00
|
|
|
|
2009-02-01 17:12:12 -05:00
|
|
|
class MyEvent : public wxEvent
|
|
|
|
{
|
2009-02-01 18:19:45 -05:00
|
|
|
public:
|
2009-02-06 13:33:01 -05:00
|
|
|
MyEvent() : wxEvent(0, MyEventType) { }
|
2009-02-01 18:19:45 -05:00
|
|
|
|
|
|
|
virtual wxEvent *Clone() const { return new MyEvent; }
|
2009-02-01 17:12:12 -05:00
|
|
|
};
|
|
|
|
|
2009-02-06 16:54:09 -05:00
|
|
|
typedef void (wxEvtHandler::*MyEventFunction)(MyEvent&);
|
2009-05-02 08:52:54 -04:00
|
|
|
#ifndef wxHAS_EVENT_BIND
|
2009-02-25 12:43:22 -05:00
|
|
|
#define MyEventHandler(func) wxEVENT_HANDLER_CAST(MyEventFunction, func)
|
|
|
|
#else
|
|
|
|
#define MyEventHandler(func) &func
|
|
|
|
#endif
|
2009-02-06 16:54:09 -05:00
|
|
|
#define EVT_MYEVENT(func) \
|
|
|
|
wx__DECLARE_EVT0(MyEventType, MyEventHandler(func))
|
2009-02-06 13:33:01 -05:00
|
|
|
|
2009-02-01 18:19:45 -05:00
|
|
|
class AnotherEvent : public wxEvent
|
|
|
|
{
|
|
|
|
};
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-01 18:19:45 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct Called
|
|
|
|
{
|
|
|
|
Called() { Reset(); }
|
|
|
|
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
function =
|
|
|
|
functor =
|
|
|
|
method =
|
|
|
|
smethod = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool function,
|
|
|
|
functor,
|
|
|
|
method,
|
|
|
|
smethod;
|
|
|
|
} g_called;
|
|
|
|
|
|
|
|
void GlobalOnMyEvent(MyEvent&)
|
|
|
|
{
|
|
|
|
g_called.function = true;
|
|
|
|
}
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-05-02 08:52:54 -04:00
|
|
|
void GlobalOnEvent(wxEvent&)
|
|
|
|
{
|
|
|
|
g_called.function = true;
|
|
|
|
}
|
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
void GlobalOnAnotherEvent(AnotherEvent&);
|
|
|
|
|
|
|
|
void GlobalOnIdle(wxIdleEvent&)
|
|
|
|
{
|
|
|
|
g_called.function = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyFunctor
|
2009-02-01 17:12:12 -05:00
|
|
|
{
|
2009-02-01 18:19:45 -05:00
|
|
|
void operator()(MyEvent &) { g_called.functor = true; }
|
2009-02-02 15:38:56 -05:00
|
|
|
};
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
struct IdleFunctor
|
|
|
|
{
|
|
|
|
void operator()(wxIdleEvent &) { g_called.functor = true; }
|
2009-02-01 17:12:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class MyHandler : public wxEvtHandler
|
|
|
|
{
|
2009-02-01 18:19:45 -05:00
|
|
|
public:
|
2009-02-02 15:38:56 -05:00
|
|
|
static void StaticOnMyEvent(MyEvent &) { g_called.smethod = true; }
|
|
|
|
static void StaticOnAnotherEvent(AnotherEvent &);
|
|
|
|
static void StaticOnIdle(wxIdleEvent&) { g_called.smethod = true; }
|
|
|
|
|
2009-02-01 18:19:45 -05:00
|
|
|
void OnMyEvent(MyEvent&) { g_called.method = true; }
|
2009-02-02 15:38:56 -05:00
|
|
|
void OnEvent(wxEvent&) { g_called.method = true; }
|
|
|
|
void OnAnotherEvent(AnotherEvent&);
|
|
|
|
void OnIdle(wxIdleEvent&) { g_called.method = true; }
|
|
|
|
};
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
// we can also handle events in classes not deriving from wxEvtHandler
|
|
|
|
struct MySink
|
|
|
|
{
|
|
|
|
void OnMyEvent(MyEvent&) { g_called.method = true; }
|
2009-02-06 13:33:01 -05:00
|
|
|
void OnEvent(wxEvent&) { g_called.method = true; }
|
2009-02-02 15:38:56 -05:00
|
|
|
void OnIdle(wxIdleEvent&) { g_called.method = true; }
|
|
|
|
};
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
// also test event table compilation
|
|
|
|
class MyClassWithEventTable : public wxEvtHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void OnMyEvent(MyEvent&) { g_called.method = true; }
|
|
|
|
void OnEvent(wxEvent&) { g_called.method = true; }
|
|
|
|
void OnAnotherEvent(AnotherEvent&);
|
|
|
|
void OnIdle(wxIdleEvent&) { g_called.method = true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DECLARE_EVENT_TABLE()
|
2009-02-01 17:12:12 -05:00
|
|
|
};
|
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
BEGIN_EVENT_TABLE(MyClassWithEventTable, wxEvtHandler)
|
|
|
|
EVT_IDLE(MyClassWithEventTable::OnIdle)
|
|
|
|
|
2009-02-06 13:33:01 -05:00
|
|
|
EVT_MYEVENT(MyClassWithEventTable::OnMyEvent)
|
2009-05-02 08:52:54 -04:00
|
|
|
#ifdef wxHAS_EVENT_BIND
|
2009-02-06 13:33:01 -05:00
|
|
|
EVT_MYEVENT(MyClassWithEventTable::OnEvent)
|
2009-02-06 16:54:09 -05:00
|
|
|
#endif
|
2009-02-06 13:33:01 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
// this shouldn't compile:
|
2009-02-06 13:33:01 -05:00
|
|
|
//EVT_MYEVENT(MyClassWithEventTable::OnIdle)
|
2009-02-02 15:38:56 -05:00
|
|
|
//EVT_IDLE(MyClassWithEventTable::OnAnotherEvent)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2009-02-01 18:19:45 -05:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class EvtHandlerTestCase : public CppUnit::TestCase
|
2009-02-01 17:12:12 -05:00
|
|
|
{
|
2009-02-02 15:38:56 -05:00
|
|
|
public:
|
|
|
|
EvtHandlerTestCase() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
|
|
|
|
CPPUNIT_TEST( BuiltinConnect );
|
|
|
|
CPPUNIT_TEST( LegacyConnect );
|
2009-05-02 08:52:54 -04:00
|
|
|
#ifdef wxHAS_EVENT_BIND
|
2009-02-25 12:43:22 -05:00
|
|
|
CPPUNIT_TEST( BindFunction );
|
|
|
|
CPPUNIT_TEST( BindStaticMethod );
|
|
|
|
CPPUNIT_TEST( BindFunctor );
|
|
|
|
CPPUNIT_TEST( BindMethod );
|
|
|
|
CPPUNIT_TEST( BindMethodUsingBaseEvent );
|
2009-05-02 08:52:54 -04:00
|
|
|
CPPUNIT_TEST( BindFunctionUsingBaseEvent );
|
2009-02-25 12:43:22 -05:00
|
|
|
CPPUNIT_TEST( BindNonHandler );
|
|
|
|
CPPUNIT_TEST( InvalidBind );
|
2009-05-02 08:52:54 -04:00
|
|
|
#endif // wxHAS_EVENT_BIND
|
2009-02-02 15:38:56 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void BuiltinConnect();
|
|
|
|
void LegacyConnect();
|
2009-05-02 08:52:54 -04:00
|
|
|
#ifdef wxHAS_EVENT_BIND
|
2009-02-25 12:43:22 -05:00
|
|
|
void BindFunction();
|
|
|
|
void BindStaticMethod();
|
|
|
|
void BindFunctor();
|
|
|
|
void BindMethod();
|
|
|
|
void BindMethodUsingBaseEvent();
|
2009-05-02 08:52:54 -04:00
|
|
|
void BindFunctionUsingBaseEvent();
|
2009-02-25 12:43:22 -05:00
|
|
|
void BindNonHandler();
|
|
|
|
void InvalidBind();
|
2009-05-02 08:52:54 -04:00
|
|
|
#endif // wxHAS_EVENT_BIND
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
// these member variables exceptionally don't use "m_" prefix because
|
|
|
|
// they're used so many times
|
2009-02-01 17:12:12 -05:00
|
|
|
MyHandler handler;
|
2009-02-01 18:19:45 -05:00
|
|
|
MyEvent e;
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
DECLARE_NO_COPY_CLASS(EvtHandlerTestCase)
|
|
|
|
};
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( EvtHandlerTestCase );
|
|
|
|
|
|
|
|
// also include in it's own registry so that these tests can be run alone
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase, "EvtHandlerTestCase" );
|
|
|
|
|
|
|
|
void EvtHandlerTestCase::BuiltinConnect()
|
|
|
|
{
|
|
|
|
handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
|
|
|
|
handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
|
|
|
|
|
|
|
|
handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
|
|
|
|
handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-06 13:33:01 -05:00
|
|
|
// using casts like this is even uglier than using wxIdleEventHandler but
|
|
|
|
// it should still continue to work for compatibility
|
|
|
|
handler.Connect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
|
|
|
|
handler.Disconnect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
|
|
|
|
|
2009-05-02 08:52:54 -04:00
|
|
|
#ifdef wxHAS_EVENT_BIND
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind(wxEVT_IDLE, GlobalOnIdle);
|
|
|
|
handler.Unbind(wxEVT_IDLE, GlobalOnIdle);
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
IdleFunctor f;
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind(wxEVT_IDLE, f);
|
|
|
|
handler.Unbind(wxEVT_IDLE, f);
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind(wxEVT_IDLE, &MyHandler::OnIdle, &handler);
|
|
|
|
handler.Unbind(wxEVT_IDLE, &MyHandler::OnIdle, &handler);
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
|
|
|
|
handler.Unbind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
|
2009-05-02 08:52:54 -04:00
|
|
|
#endif // wxHAS_EVENT_BIND
|
2009-02-02 15:38:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void EvtHandlerTestCase::LegacyConnect()
|
|
|
|
{
|
2009-02-06 13:33:01 -05:00
|
|
|
handler.Connect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
|
|
|
|
handler.Connect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
|
|
|
|
handler.Connect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-06 13:33:01 -05:00
|
|
|
handler.Disconnect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
|
|
|
|
handler.Disconnect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
|
|
|
|
handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-02-06 13:33:01 -05:00
|
|
|
handler.Connect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
|
|
|
|
handler.Connect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
|
|
|
|
handler.Connect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-02-06 13:33:01 -05:00
|
|
|
handler.Disconnect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
|
|
|
|
handler.Disconnect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
|
|
|
|
handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
|
2009-02-02 15:38:56 -05:00
|
|
|
}
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-05-02 08:52:54 -04:00
|
|
|
#ifdef wxHAS_EVENT_BIND
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
void EvtHandlerTestCase::BindFunction()
|
2009-02-02 15:38:56 -05:00
|
|
|
{
|
|
|
|
// function tests
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, GlobalOnMyEvent );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( g_called.function );
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Unbind( MyEventType, GlobalOnMyEvent );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( !g_called.function ); // check that it was disconnected
|
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, GlobalOnMyEvent, 0 );
|
|
|
|
handler.Unbind( MyEventType, GlobalOnMyEvent, 0 );
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, GlobalOnMyEvent, 0, 0 );
|
|
|
|
handler.Unbind( MyEventType, GlobalOnMyEvent, 0, 0 );
|
2009-02-02 15:38:56 -05:00
|
|
|
}
|
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
void EvtHandlerTestCase::BindStaticMethod()
|
2009-02-02 15:38:56 -05:00
|
|
|
{
|
|
|
|
// static method tests (this is same as functions but still test it just in
|
|
|
|
// case we hit some strange compiler bugs)
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::StaticOnMyEvent );
|
2009-02-01 18:19:45 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( g_called.smethod );
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Unbind( MyEventType, &MyHandler::StaticOnMyEvent );
|
2009-02-01 18:19:45 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
2009-02-02 15:38:56 -05:00
|
|
|
CPPUNIT_ASSERT( !g_called.smethod );
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::StaticOnMyEvent, 0 );
|
|
|
|
handler.Unbind( MyEventType, &MyHandler::StaticOnMyEvent, 0 );
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::StaticOnMyEvent, 0, 0 );
|
|
|
|
handler.Unbind( MyEventType, &MyHandler::StaticOnMyEvent, 0, 0 );
|
2009-02-02 15:38:56 -05:00
|
|
|
}
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
void EvtHandlerTestCase::BindFunctor()
|
2009-02-02 15:38:56 -05:00
|
|
|
{
|
|
|
|
// generalized functor tests
|
|
|
|
MyFunctor functor;
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, functor );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( g_called.functor );
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Unbind( MyEventType, functor );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( !g_called.functor );
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, functor, 0 );
|
|
|
|
handler.Unbind( MyEventType, functor, 0 );
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, functor, 0, 0 );
|
|
|
|
handler.Unbind( MyEventType, functor, 0, 0 );
|
2009-05-02 11:28:16 -04:00
|
|
|
|
2009-05-17 06:56:14 -04:00
|
|
|
// test that a temporary functor is working as well and also test that
|
|
|
|
// unbinding a different (though equal) instance of the same functor does
|
|
|
|
// not work
|
|
|
|
MyFunctor func;
|
2009-05-02 11:28:16 -04:00
|
|
|
handler.Bind( MyEventType, MyFunctor() );
|
2009-05-17 06:56:14 -04:00
|
|
|
CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func ));
|
2009-05-02 11:28:16 -04:00
|
|
|
|
|
|
|
handler.Bind( MyEventType, MyFunctor(), 0 );
|
2009-05-17 06:56:14 -04:00
|
|
|
CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func, 0 ));
|
2009-05-02 11:28:16 -04:00
|
|
|
|
|
|
|
handler.Bind( MyEventType, MyFunctor(), 0, 0 );
|
2009-05-17 06:56:14 -04:00
|
|
|
CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func, 0, 0 ));
|
2009-02-02 15:38:56 -05:00
|
|
|
}
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
void EvtHandlerTestCase::BindMethod()
|
2009-02-02 15:38:56 -05:00
|
|
|
{
|
|
|
|
// class method tests
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::OnMyEvent, &handler );
|
2009-02-06 13:33:01 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( g_called.method );
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Unbind( MyEventType, &MyHandler::OnMyEvent, &handler );
|
2009-02-06 13:33:01 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( !g_called.method );
|
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::OnMyEvent, &handler, 0 );
|
|
|
|
handler.Unbind( MyEventType, &MyHandler::OnMyEvent, &handler, 0 );
|
2009-02-06 13:33:01 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::OnMyEvent, &handler, 0, 0 );
|
|
|
|
handler.Unbind( MyEventType, &MyHandler::OnMyEvent, &handler, 0, 0 );
|
2009-02-06 13:33:01 -05:00
|
|
|
}
|
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
void EvtHandlerTestCase::BindMethodUsingBaseEvent()
|
2009-02-06 13:33:01 -05:00
|
|
|
{
|
|
|
|
// test connecting a method taking just wxEvent and not MyEvent: this
|
|
|
|
// should work too if we don't need any MyEvent-specific information in the
|
|
|
|
// handler
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::OnEvent, &handler );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( g_called.method );
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Unbind( MyEventType, &MyHandler::OnEvent, &handler );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( !g_called.method );
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::OnEvent, &handler, 0 );
|
|
|
|
handler.Unbind( MyEventType, &MyHandler::OnEvent, &handler, 0 );
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MyHandler::OnEvent, &handler, 0, 0 );
|
|
|
|
handler.Unbind( MyEventType, &MyHandler::OnEvent, &handler, 0, 0 );
|
2009-02-02 15:38:56 -05:00
|
|
|
}
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-02 15:38:56 -05:00
|
|
|
|
2009-05-02 08:52:54 -04:00
|
|
|
void EvtHandlerTestCase::BindFunctionUsingBaseEvent()
|
|
|
|
{
|
|
|
|
// test connecting a function taking just wxEvent and not MyEvent: this
|
|
|
|
// should work too if we don't need any MyEvent-specific information in the
|
|
|
|
// handler
|
|
|
|
handler.Bind( MyEventType, GlobalOnEvent );
|
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( g_called.function );
|
|
|
|
handler.Unbind( MyEventType, GlobalOnEvent );
|
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( !g_called.function );
|
|
|
|
|
|
|
|
handler.Bind( MyEventType, GlobalOnEvent, 0 );
|
|
|
|
handler.Unbind( MyEventType, GlobalOnEvent, 0 );
|
|
|
|
|
|
|
|
handler.Bind( MyEventType, GlobalOnEvent, 0, 0 );
|
|
|
|
handler.Unbind( MyEventType, GlobalOnEvent, 0, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
void EvtHandlerTestCase::BindNonHandler()
|
2009-02-02 15:38:56 -05:00
|
|
|
{
|
|
|
|
// class method tests for class not derived from wxEvtHandler
|
|
|
|
MySink sink;
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Bind( MyEventType, &MySink::OnMyEvent, &sink );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( g_called.method );
|
2009-02-25 12:43:22 -05:00
|
|
|
handler.Unbind( MyEventType, &MySink::OnMyEvent, &sink );
|
2009-02-02 15:38:56 -05:00
|
|
|
g_called.Reset();
|
|
|
|
handler.ProcessEvent(e);
|
|
|
|
CPPUNIT_ASSERT( !g_called.method );
|
|
|
|
}
|
2009-02-01 17:12:12 -05:00
|
|
|
|
2009-02-25 12:43:22 -05:00
|
|
|
void EvtHandlerTestCase::InvalidBind()
|
2009-02-02 15:38:56 -05:00
|
|
|
{
|
2009-02-01 18:19:45 -05:00
|
|
|
// these calls shouldn't compile but we unfortunately can't check this
|
|
|
|
// automatically, you need to uncomment them manually and test that
|
|
|
|
// compilation does indeed fail
|
2009-02-25 12:43:22 -05:00
|
|
|
|
2009-04-16 10:47:22 -04:00
|
|
|
// connecting a handler with incompatible signature shouldn't work
|
|
|
|
#ifdef TEST_INVALID_BIND_GLOBAL
|
|
|
|
handler.Bind(MyEventType, GlobalOnAnotherEvent);
|
|
|
|
#endif
|
|
|
|
#ifdef TEST_INVALID_BIND_STATIC
|
|
|
|
handler.Bind(MyEventType, &MyHandler::StaticOnAnotherEvent);
|
|
|
|
#endif
|
|
|
|
#ifdef TEST_INVALID_BIND_METHOD
|
|
|
|
handler.Bind(MyEventType, &MyHandler::OnAnotherEvent, &handler);
|
|
|
|
#endif
|
|
|
|
#ifdef TEST_INVALID_BIND_FUNCTOR
|
|
|
|
IdleFunctor f;
|
|
|
|
handler.Bind(MyEventType, f);
|
|
|
|
#endif
|
|
|
|
|
2009-04-16 17:33:37 -04:00
|
|
|
// the handler can't be omitted when calling Bind()
|
|
|
|
#ifdef TEST_INVALID_BIND_NO_HANDLER
|
|
|
|
handler.Bind(MyEventType, &MyHandler::OnMyEvent);
|
|
|
|
#endif
|
|
|
|
|
2009-04-16 10:47:22 -04:00
|
|
|
// calling a derived class method with a base class pointer must not work
|
|
|
|
#ifdef TEST_INVALID_BIND_DERIVED
|
|
|
|
struct C1 : wxEvtHandler { };
|
|
|
|
struct C2 : wxEvtHandler { void OnWhatever(wxEvent&); };
|
|
|
|
C1 c1;
|
|
|
|
c1.Bind(&C2::OnWhatever);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// using object pointer incompatible with the method must not work
|
|
|
|
#ifdef TEST_INVALID_BIND_WRONG_CLASS
|
|
|
|
MySink mySink;
|
|
|
|
MyHandler myHandler;
|
|
|
|
myHandler.Bind(MyEventType, &MyHandler::OnMyEvent, &mySink);
|
|
|
|
#endif
|
2009-02-01 17:12:12 -05:00
|
|
|
}
|
|
|
|
|
2009-05-02 08:52:54 -04:00
|
|
|
#endif // wxHAS_EVENT_BIND
|