]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/events/evthandler.cpp
fixes potential crash under gatekeeper
[wxWidgets.git] / tests / events / evthandler.cpp
index 309b34442fda853a8e97ee7547f2d4ff34e94ad8..d8e24a3438bb916891c93db07686c7916e979f83 100644 (file)
@@ -37,7 +37,7 @@ public:
 };
 
 typedef void (wxEvtHandler::*MyEventFunction)(MyEvent&);
 };
 
 typedef void (wxEvtHandler::*MyEventFunction)(MyEvent&);
-#if wxEVENTS_COMPATIBILITY_2_8
+#ifndef wxHAS_EVENT_BIND
     #define MyEventHandler(func) wxEVENT_HANDLER_CAST(MyEventFunction, func)
 #else
     #define MyEventHandler(func) &func
     #define MyEventHandler(func) wxEVENT_HANDLER_CAST(MyEventFunction, func)
 #else
     #define MyEventHandler(func) &func
@@ -75,6 +75,11 @@ void GlobalOnMyEvent(MyEvent&)
     g_called.function = true;
 }
 
     g_called.function = true;
 }
 
+void GlobalOnEvent(wxEvent&)
+{
+    g_called.function = true;
+}
+
 void GlobalOnAnotherEvent(AnotherEvent&);
 
 void GlobalOnIdle(wxIdleEvent&)
 void GlobalOnAnotherEvent(AnotherEvent&);
 
 void GlobalOnIdle(wxIdleEvent&)
@@ -130,7 +135,7 @@ BEGIN_EVENT_TABLE(MyClassWithEventTable, wxEvtHandler)
     EVT_IDLE(MyClassWithEventTable::OnIdle)
 
     EVT_MYEVENT(MyClassWithEventTable::OnMyEvent)
     EVT_IDLE(MyClassWithEventTable::OnIdle)
 
     EVT_MYEVENT(MyClassWithEventTable::OnMyEvent)
-#if !wxEVENTS_COMPATIBILITY_2_8
+#ifdef wxHAS_EVENT_BIND
     EVT_MYEVENT(MyClassWithEventTable::OnEvent)
 #endif
 
     EVT_MYEVENT(MyClassWithEventTable::OnEvent)
 #endif
 
@@ -155,28 +160,34 @@ private:
     CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
         CPPUNIT_TEST( BuiltinConnect );
         CPPUNIT_TEST( LegacyConnect );
     CPPUNIT_TEST_SUITE( EvtHandlerTestCase );
         CPPUNIT_TEST( BuiltinConnect );
         CPPUNIT_TEST( LegacyConnect );
-#if !wxEVENTS_COMPATIBILITY_2_8
+        CPPUNIT_TEST( DisconnectWildcard );
+        CPPUNIT_TEST( AutoDisconnect );
+#ifdef wxHAS_EVENT_BIND
         CPPUNIT_TEST( BindFunction );
         CPPUNIT_TEST( BindStaticMethod );
         CPPUNIT_TEST( BindFunctor );
         CPPUNIT_TEST( BindMethod );
         CPPUNIT_TEST( BindMethodUsingBaseEvent );
         CPPUNIT_TEST( BindFunction );
         CPPUNIT_TEST( BindStaticMethod );
         CPPUNIT_TEST( BindFunctor );
         CPPUNIT_TEST( BindMethod );
         CPPUNIT_TEST( BindMethodUsingBaseEvent );
+        CPPUNIT_TEST( BindFunctionUsingBaseEvent );
         CPPUNIT_TEST( BindNonHandler );
         CPPUNIT_TEST( InvalidBind );
         CPPUNIT_TEST( BindNonHandler );
         CPPUNIT_TEST( InvalidBind );
-#endif // !wxEVENTS_COMPATIBILITY_2_8
+#endif // wxHAS_EVENT_BIND
     CPPUNIT_TEST_SUITE_END();
 
     void BuiltinConnect();
     void LegacyConnect();
     CPPUNIT_TEST_SUITE_END();
 
     void BuiltinConnect();
     void LegacyConnect();
-#if !wxEVENTS_COMPATIBILITY_2_8
+    void DisconnectWildcard();
+    void AutoDisconnect();
+#ifdef wxHAS_EVENT_BIND
     void BindFunction();
     void BindStaticMethod();
     void BindFunctor();
     void BindMethod();
     void BindMethodUsingBaseEvent();
     void BindFunction();
     void BindStaticMethod();
     void BindFunctor();
     void BindMethod();
     void BindMethodUsingBaseEvent();
+    void BindFunctionUsingBaseEvent();
     void BindNonHandler();
     void InvalidBind();
     void BindNonHandler();
     void InvalidBind();
-#endif // !wxEVENTS_COMPATIBILITY_2_8
+#endif // wxHAS_EVENT_BIND
 
 
     // these member variables exceptionally don't use "m_" prefix because
 
 
     // these member variables exceptionally don't use "m_" prefix because
@@ -190,7 +201,7 @@ private:
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( 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
+// also include in its own registry so that these tests can be run alone
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase, "EvtHandlerTestCase" );
 
 void EvtHandlerTestCase::BuiltinConnect()
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtHandlerTestCase, "EvtHandlerTestCase" );
 
 void EvtHandlerTestCase::BuiltinConnect()
@@ -206,7 +217,7 @@ void EvtHandlerTestCase::BuiltinConnect()
     handler.Connect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
     handler.Disconnect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
 
     handler.Connect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
     handler.Disconnect(wxEVT_IDLE, (wxObjectEventFunction)(wxEventFunction)&MyHandler::OnIdle);
 
-#if !wxEVENTS_COMPATIBILITY_2_8
+#ifdef wxHAS_EVENT_BIND
     handler.Bind(wxEVT_IDLE, GlobalOnIdle);
     handler.Unbind(wxEVT_IDLE, GlobalOnIdle);
 
     handler.Bind(wxEVT_IDLE, GlobalOnIdle);
     handler.Unbind(wxEVT_IDLE, GlobalOnIdle);
 
@@ -219,7 +230,7 @@ void EvtHandlerTestCase::BuiltinConnect()
 
     handler.Bind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
     handler.Unbind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
 
     handler.Bind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
     handler.Unbind(wxEVT_IDLE, &MyHandler::StaticOnIdle);
-#endif // !wxEVENTS_COMPATIBILITY_2_8
+#endif // wxHAS_EVENT_BIND
 }
 
 void EvtHandlerTestCase::LegacyConnect()
 }
 
 void EvtHandlerTestCase::LegacyConnect()
@@ -242,7 +253,32 @@ void EvtHandlerTestCase::LegacyConnect()
     handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
 }
 
     handler.Disconnect( 0, 0, LegacyEventType, (wxObjectEventFunction)&MyHandler::OnEvent, NULL, &handler );
 }
 
-#if !wxEVENTS_COMPATIBILITY_2_8
+void EvtHandlerTestCase::DisconnectWildcard()
+{
+    // should be able to disconnect a different handler using "wildcard search"
+    MyHandler sink;
+    wxEvtHandler source;
+    source.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &sink);
+    CPPUNIT_ASSERT(source.Disconnect(wxID_ANY, wxEVT_IDLE));
+    // destruction of source and sink here should properly clean up the
+    // wxEventConnectionRef without crashing
+}
+
+void EvtHandlerTestCase::AutoDisconnect()
+{
+    wxEvtHandler source;
+    {
+        MyHandler sink;
+        source.Connect(wxEVT_IDLE, wxIdleEventHandler(MyHandler::OnIdle), NULL, &sink);
+        // mismatched event type, so nothing should be disconnected
+        CPPUNIT_ASSERT(!source.Disconnect(wxEVT_THREAD, wxIdleEventHandler(MyHandler::OnIdle), NULL, &sink));
+    }
+    // destruction of sink should have automatically disconnected it, so
+    // there should be nothing to disconnect anymore
+    CPPUNIT_ASSERT(!source.Disconnect(wxID_ANY, wxEVT_IDLE));
+}
+
+#ifdef wxHAS_EVENT_BIND
 
 void EvtHandlerTestCase::BindFunction()
 {
 
 void EvtHandlerTestCase::BindFunction()
 {
@@ -302,6 +338,19 @@ void EvtHandlerTestCase::BindFunctor()
 
     handler.Bind( MyEventType, functor, 0, 0 );
     handler.Unbind( MyEventType, functor, 0, 0 );
 
     handler.Bind( MyEventType, functor, 0, 0 );
     handler.Unbind( MyEventType, functor, 0, 0 );
+
+    // 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;
+    handler.Bind( MyEventType, MyFunctor() );
+    CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func ));
+
+    handler.Bind( MyEventType, MyFunctor(), 0 );
+    CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func, 0 ));
+
+    handler.Bind( MyEventType, MyFunctor(), 0, 0 );
+    CPPUNIT_ASSERT( !handler.Unbind( MyEventType, func, 0, 0 ));
 }
 
 void EvtHandlerTestCase::BindMethod()
 }
 
 void EvtHandlerTestCase::BindMethod()
@@ -345,6 +394,29 @@ void EvtHandlerTestCase::BindMethodUsingBaseEvent()
 }
 
 
 }
 
 
+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 );
+}
+
+
+
 void EvtHandlerTestCase::BindNonHandler()
 {
     // class method tests for class not derived from wxEvtHandler
 void EvtHandlerTestCase::BindNonHandler()
 {
     // class method tests for class not derived from wxEvtHandler
@@ -381,6 +453,11 @@ void EvtHandlerTestCase::InvalidBind()
     handler.Bind(MyEventType, f);
 #endif
 
     handler.Bind(MyEventType, f);
 #endif
 
+    // the handler can't be omitted when calling Bind()
+#ifdef TEST_INVALID_BIND_NO_HANDLER
+    handler.Bind(MyEventType, &MyHandler::OnMyEvent);
+#endif
+
     // calling a derived class method with a base class pointer must not work
 #ifdef TEST_INVALID_BIND_DERIVED
     struct C1 : wxEvtHandler { };
     // calling a derived class method with a base class pointer must not work
 #ifdef TEST_INVALID_BIND_DERIVED
     struct C1 : wxEvtHandler { };
@@ -397,4 +474,4 @@ void EvtHandlerTestCase::InvalidBind()
 #endif
 }
 
 #endif
 }
 
-#endif // !wxEVENTS_COMPATIBILITY_2_8
+#endif // wxHAS_EVENT_BIND