]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/events/evthandler.cpp
fix some typos (return types)
[wxWidgets.git] / tests / events / evthandler.cpp
index b6de4733738e528cb41bb9fe29dd8c32005d19d4..ea866600201362f5bce3d3008b22c65cba1839ee 100644 (file)
     #pragma hdrstop
 #endif
 
-#ifndef WX_PRECOMP
-#endif // WX_PRECOMP
-
 #include "wx/event.h"
 
+// ----------------------------------------------------------------------------
+// test events and their handlers
+// ----------------------------------------------------------------------------
 
-// --------------------------------------------------------------------------
-// test class
-// --------------------------------------------------------------------------
+const wxEventType LegacyEventType = wxNewEventType();
 
-class EvtHandlerTestCase : public CppUnit::TestCase
+class MyEvent;
+wxDEFINE_EVENT( MyEventType, MyEvent )
+
+class MyEvent : public wxEvent
 {
 public:
-    EvtHandlerTestCase() {}
+    MyEvent() : wxEvent(0, MyEventType) { }
 
-private:
-    CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
-        CPPUNIT_TEST( TestConnectCompilation );
-        CPPUNIT_TEST( TestEventFunctorCompare );
-    CPPUNIT_TEST_SUITE_END();
+    virtual wxEvent *Clone() const { return new MyEvent; }
+};
 
-    void TestConnectCompilation();
-    void TestEventFunctorCompare();
+#define EVT_MYEVENT(func) wx__DECLARE_EVT0(MyEventType, &func)
 
-    DECLARE_NO_COPY_CLASS(EvtHandlerTestCase)
+class AnotherEvent : public wxEvent
+{
 };
 
-// register in the unnamed registry so that these tests are run by default
-CPPUNIT_TEST_SUITE_REGISTRATION( EvtHandlerTestCase );
+namespace
+{
 
-// also include in it's own registry so that these tests can be run alone
-CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase, "EvtHandlerTestCase" );
+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;
+}
 
-const wxEventType EVT_LEGACY = wxNewEventType();
+void GlobalOnAnotherEvent(AnotherEvent&);
 
-class MyEvent : public wxEvent
+void GlobalOnIdle(wxIdleEvent&)
+{
+    g_called.function = true;
+}
+
+struct MyFunctor
 {
+    void operator()(MyEvent &) { g_called.functor = true; }
 };
 
-wxDEFINE_EVENT( EVT_EVENT, MyEvent )
+struct IdleFunctor
+{
+    void operator()(wxIdleEvent &) { g_called.functor = true; }
+};
+
+class MyHandler : public wxEvtHandler
+{
+public:
+    static void StaticOnMyEvent(MyEvent &) { g_called.smethod = true; }
+    static void StaticOnAnotherEvent(AnotherEvent &);
+    static void StaticOnIdle(wxIdleEvent&) { g_called.smethod = true; }
+
+    void OnMyEvent(MyEvent&) { g_called.method = true; }
+    void OnEvent(wxEvent&) { g_called.method = true; }
+    void OnAnotherEvent(AnotherEvent&);
+    void OnIdle(wxIdleEvent&) { g_called.method = true; }
+};
 
-// An arbitrary functor:
+// we can also handle events in classes not deriving from wxEvtHandler
+struct MySink
+{
+    void OnMyEvent(MyEvent&) { g_called.method = true; }
+    void OnEvent(wxEvent&) { g_called.method = true; }
+    void OnIdle(wxIdleEvent&) { g_called.method = true; }
+};
 
-class MyFunctor
+// also test event table compilation
+class MyClassWithEventTable : public wxEvtHandler
 {
-    public:
-        void operator () ( MyEvent & )
-        { }
+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; }
 
-        bool operator == ( const MyFunctor & ) const
-        { return ( true ); }
+private:
+    DECLARE_EVENT_TABLE()
 };
 
+BEGIN_EVENT_TABLE(MyClassWithEventTable, wxEvtHandler)
+    EVT_IDLE(MyClassWithEventTable::OnIdle)
 
-class MyHandler : public wxEvtHandler
-{
-    public:
-        void handleMethod( MyEvent & )
-        { }
+    EVT_MYEVENT(MyClassWithEventTable::OnMyEvent)
+    EVT_MYEVENT(MyClassWithEventTable::OnEvent)
 
-        static void handleFunction( MyEvent & )
-        { }
+    // this shouldn't compile:
+    //EVT_MYEVENT(MyClassWithEventTable::OnIdle)
+    //EVT_IDLE(MyClassWithEventTable::OnAnotherEvent)
+END_EVENT_TABLE()
 
-        void handleEvent( wxEvent & )
-        { }
+} // anonymous namespace
 
-};
 
-void EvtHandlerTestCase::TestConnectCompilation()
+// --------------------------------------------------------------------------
+// test class
+// --------------------------------------------------------------------------
+
+class EvtHandlerTestCase : public CppUnit::TestCase
 {
-    // Test that connecting the 'legacy' events still compiles:
+public:
+    EvtHandlerTestCase() {}
 
-    MyHandler handler;
+private:
+    CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
+        CPPUNIT_TEST( BuiltinConnect );
+        CPPUNIT_TEST( LegacyConnect );
+#if !wxEVENTS_COMPATIBILITY_2_8
+        CPPUNIT_TEST( ConnectFunction );
+        CPPUNIT_TEST( ConnectStaticMethod );
+        CPPUNIT_TEST( ConnectFunctor );
+        CPPUNIT_TEST( ConnectMethod );
+        CPPUNIT_TEST( ConnectMethodUsingBaseEvent );
+        CPPUNIT_TEST( ConnectMethodWithSink );
+        CPPUNIT_TEST( ConnectNonHandler );
+        CPPUNIT_TEST( StaticConnect );
+        CPPUNIT_TEST( InvalidConnect );
+#endif // !wxEVENTS_COMPATIBILITY_2_8
+    CPPUNIT_TEST_SUITE_END();
 
-    handler.Connect( EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent, NULL, &handler );
-    handler.Connect( 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent, NULL, &handler );
-    handler.Connect( 0, 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent, NULL, &handler );
+    void BuiltinConnect();
+    void LegacyConnect();
+#if !wxEVENTS_COMPATIBILITY_2_8
+    void ConnectFunction();
+    void ConnectStaticMethod();
+    void ConnectFunctor();
+    void ConnectMethod();
+    void ConnectMethodUsingBaseEvent();
+    void ConnectMethodWithSink();
+    void ConnectNonHandler();
+    void StaticConnect();
+    void InvalidConnect();
+#endif // !wxEVENTS_COMPATIBILITY_2_8
+
+
+    // these member variables exceptionally don't use "m_" prefix because
+    // they're used so many times
+    MyHandler handler;
+    MyEvent e;
 
-    handler.Disconnect( EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent, NULL, &handler );
-    handler.Disconnect( 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent, NULL, &handler );
-    handler.Disconnect( 0, 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent, NULL, &handler );
+    DECLARE_NO_COPY_CLASS(EvtHandlerTestCase)
+};
 
+// 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" );
 
-    handler.Connect( EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent );
-    handler.Connect( 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent );
-    handler.Connect( 0, 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent );
+void EvtHandlerTestCase::BuiltinConnect()
+{
+    handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
+    handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle));
 
-    handler.Disconnect( EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent );
-    handler.Disconnect( 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent );
-    handler.Disconnect( 0, 0, EVT_LEGACY, (wxObjectEventFunction)&MyHandler::handleEvent );
+    handler.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
+    handler.Disconnect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &handler);
 
-    // Call (and therefore instantiate) all Connect() variants to detect template
-    // errors:
+    // 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);
 
 #if !wxEVENTS_COMPATIBILITY_2_8
+    handler.Connect(wxEVT_IDLE, GlobalOnIdle);
+    handler.Disconnect(wxEVT_IDLE, GlobalOnIdle);
 
-    handler.Connect( EVT_EVENT, &MyHandler::handleFunction );
-    handler.Connect( 0, EVT_EVENT, &MyHandler::handleFunction );
-    handler.Connect( 0, 0, EVT_EVENT, &MyHandler::handleFunction );
+    IdleFunctor f;
+    handler.Connect(wxEVT_IDLE, f);
+    handler.Disconnect(wxEVT_IDLE, f);
 
-    handler.Disconnect( EVT_EVENT, &MyHandler::handleFunction );
-    handler.Disconnect( 0, EVT_EVENT, &MyHandler::handleFunction );
-    handler.Disconnect( 0, 0, EVT_EVENT, &MyHandler::handleFunction );
+    handler.Connect(wxEVT_IDLE, &MyHandler::OnIdle);
+    handler.Disconnect(wxEVT_IDLE, &MyHandler::OnIdle);
 
+    handler.Connect(wxEVT_IDLE, &MyHandler::StaticOnIdle);
+    handler.Disconnect(wxEVT_IDLE, &MyHandler::StaticOnIdle);
+#endif // !wxEVENTS_COMPATIBILITY_2_8
+}
+
+void EvtHandlerTestCase::LegacyConnect()
+{
+    handler.Connect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
+    handler.Connect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
+    handler.Connect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
 
+    handler.Disconnect( LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
+    handler.Disconnect( 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
+    handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent );
 
-    handler.Connect( EVT_EVENT, &MyHandler::handleMethod );
-    handler.Connect( 0, EVT_EVENT, &MyHandler::handleMethod );
-    handler.Connect( 0, 0, EVT_EVENT, &MyHandler::handleMethod );
 
-    handler.Disconnect( EVT_EVENT, &MyHandler::handleMethod );
-    handler.Disconnect( 0, EVT_EVENT, &MyHandler::handleMethod );
-    handler.Disconnect( 0, 0, EVT_EVENT, &MyHandler::handleMethod );
+    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 );
 
+    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 );
+}
 
+#if !wxEVENTS_COMPATIBILITY_2_8
 
-    handler.Connect( EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    handler.Connect( 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    handler.Connect( 0, 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
+void EvtHandlerTestCase::ConnectFunction()
+{
+    // function tests
+    handler.Connect( MyEventType, GlobalOnMyEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( g_called.function );
+    handler.Disconnect( MyEventType, GlobalOnMyEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( !g_called.function ); // check that it was disconnected
+
+    handler.Connect( 0, MyEventType, GlobalOnMyEvent );
+    handler.Disconnect( 0, MyEventType, GlobalOnMyEvent );
+
+    handler.Connect( 0, 0, MyEventType, GlobalOnMyEvent );
+    handler.Disconnect( 0, 0, MyEventType, GlobalOnMyEvent );
+}
 
-    handler.Disconnect( EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    handler.Disconnect( 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    handler.Disconnect( 0, 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
+void EvtHandlerTestCase::ConnectStaticMethod()
+{
+    // static method tests (this is same as functions but still test it just in
+    // case we hit some strange compiler bugs)
+    handler.Connect( MyEventType, &MyHandler::StaticOnMyEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( g_called.smethod );
+    handler.Disconnect( MyEventType, &MyHandler::StaticOnMyEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( !g_called.smethod );
+
+    handler.Connect( 0, MyEventType, &MyHandler::StaticOnMyEvent );
+    handler.Disconnect( 0, MyEventType, &MyHandler::StaticOnMyEvent );
+
+    handler.Connect( 0, 0, MyEventType, &MyHandler::StaticOnMyEvent );
+    handler.Disconnect( 0, 0, MyEventType, &MyHandler::StaticOnMyEvent );
+}
 
+void EvtHandlerTestCase::ConnectFunctor()
+{
+    // generalized functor tests
+    MyFunctor functor;
 
+    handler.Connect( MyEventType, functor );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( g_called.functor );
+    handler.Disconnect( MyEventType, functor );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( !g_called.functor );
 
-    wxEvtHandler::Connect( &handler, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    wxEvtHandler::Connect( &handler, 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    wxEvtHandler::Connect( &handler, 0, 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
+    handler.Connect( 0, MyEventType, functor );
+    handler.Disconnect( 0, MyEventType, functor );
 
-    wxEvtHandler::Disconnect( &handler, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    wxEvtHandler::Disconnect( &handler, 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
-    wxEvtHandler::Disconnect( &handler, 0, 0, EVT_EVENT, &MyHandler::handleMethod, NULL, &handler );
+    handler.Connect( 0, 0, MyEventType, functor );
+    handler.Disconnect( 0, 0, MyEventType, functor );
+}
 
+void EvtHandlerTestCase::ConnectMethod()
+{
+    // class method tests
+    handler.Connect( MyEventType, &MyHandler::OnMyEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( g_called.method );
+    handler.Disconnect( MyEventType, &MyHandler::OnMyEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( !g_called.method );
+
+    handler.Connect( 0, MyEventType, &MyHandler::OnMyEvent );
+    handler.Disconnect( 0, MyEventType, &MyHandler::OnMyEvent );
+
+    handler.Connect( 0, 0, MyEventType, &MyHandler::OnMyEvent );
+    handler.Disconnect( 0, 0, MyEventType, &MyHandler::OnMyEvent );
+}
 
+void EvtHandlerTestCase::ConnectMethodUsingBaseEvent()
+{
+    // 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
+    handler.Connect( MyEventType, &MyHandler::OnEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( g_called.method );
+    handler.Disconnect( MyEventType, &MyHandler::OnEvent );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( !g_called.method );
+
+    handler.Connect( 0, MyEventType, &MyHandler::OnEvent );
+    handler.Disconnect( 0, MyEventType, &MyHandler::OnEvent );
+
+    handler.Connect( 0, 0, MyEventType, &MyHandler::OnEvent );
+    handler.Disconnect( 0, 0, MyEventType, &MyHandler::OnEvent );
+}
 
-    MyFunctor functor;
+void EvtHandlerTestCase::ConnectMethodWithSink()
+{
+    handler.Connect( MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    handler.Connect( 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    handler.Connect( 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
 
-    handler.Connect( EVT_EVENT, functor );
-    handler.Connect( 0, EVT_EVENT, functor );
-    handler.Connect( 0, 0, EVT_EVENT, functor );
+    handler.Disconnect( MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    handler.Disconnect( 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    handler.Disconnect( 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+}
 
-    handler.Disconnect( EVT_EVENT, functor );
-    handler.Disconnect( 0, EVT_EVENT, functor );
-    handler.Disconnect( 0, 0, EVT_EVENT, functor );
-#endif
+void EvtHandlerTestCase::ConnectNonHandler()
+{
+    // class method tests for class not derived from wxEvtHandler
+    MySink sink;
+
+    handler.Connect( MyEventType, &MySink::OnMyEvent, NULL, &sink );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( g_called.method );
+    handler.Disconnect( MyEventType, &MySink::OnMyEvent, NULL, &sink );
+    g_called.Reset();
+    handler.ProcessEvent(e);
+    CPPUNIT_ASSERT( !g_called.method );
 }
 
-void EvtHandlerTestCase::TestEventFunctorCompare()
-{
-//#if !wxEVENTS_COMPATIBILITY_2_8
-//    MyHandler handler1;
-//    wxEventFunctor *connectFunctor = wxNewEventFunctor( EVT_EVENT, &MyHandler::handleMethod, &handler1 );
-//    wxEventFunctor *disconnectFunctor = wxNewEventFunctor( EVT_EVENT, &MyHandler::handleMethod, &handler1 );
-//    wxEventFunctor *nullFunctor = wxNewEventFunctor( EVT_EVENT, &MyHandler::handleMethod );
-//   
-//    CPPUNIT_ASSERT( connectFunctor->Matches( *disconnectFunctor ));
-//    CPPUNIT_ASSERT( disconnectFunctor->Matches( *connectFunctor ));
-//
-//    CPPUNIT_ASSERT( connectFunctor->Matches( *nullFunctor ));
-//    CPPUNIT_ASSERT( nullFunctor->Matches( *connectFunctor ));
-//
-//    CPPUNIT_ASSERT( disconnectFunctor->Matches( *nullFunctor ));
-//    CPPUNIT_ASSERT( nullFunctor->Matches( *disconnectFunctor ));
-//#endif
+void EvtHandlerTestCase::StaticConnect()
+{
+    wxEvtHandler::Connect( &handler, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    wxEvtHandler::Connect( &handler, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    wxEvtHandler::Connect( &handler, 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+
+    wxEvtHandler::Disconnect( &handler, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    wxEvtHandler::Disconnect( &handler, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
+    wxEvtHandler::Disconnect( &handler, 0, 0, MyEventType, &MyHandler::OnMyEvent, NULL, &handler );
 }
 
+void EvtHandlerTestCase::InvalidConnect()
+{
+    // 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
+    //handler.Connect(MyEventType, GlobalOnAnotherEvent);
+    //IdleFunctor f; handler.Connect(MyEventType, f);
+    //handler.Connect(MyEventType, &MyHandler::StaticOnAnotherEvent);
+    //handler.Connect(MyEventType, &MyHandler::OnAnotherEvent);
+}
 
+#endif // !wxEVENTS_COMPATIBILITY_2_8