]>
git.saurik.com Git - wxWidgets.git/blob - tests/weakref/evtconnection.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/weakref/evtconnection.cpp
3 // Purpose: wxWeakRef<T> unit test
4 // Author: Arne Steinarson
6 // Copyright: (c) 2007 Arne Steinarson
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
24 #include "wx/weakref.h"
26 static int gs_value
= 0; // Increased by 1 by first object and 0x10000 by 2nd object
28 static wxObject
*gs_psrc1
;
29 static wxObject
*gs_psrc2
;
31 // We need some event types
32 const wxEventType wxEVT_TEST
= wxNewEventType(),
33 wxEVT_TEST1
= wxNewEventType(),
34 wxEVT_TEST2
= wxNewEventType();
36 class wxTestEvent
: public wxEvent
39 wxTestEvent(wxEventType type
= wxEVT_TEST
) : wxEvent(0, type
) { }
40 virtual wxEvent
*Clone() const { return new wxTestEvent(GetEventType()); }
43 class wxTestSink
: public wxEvtHandler
46 void OnTestEvent(wxEvent
& evt
)
48 if ( evt
.GetEventObject() == gs_psrc1
)
50 else if ( evt
.GetEventObject() == gs_psrc2
)
54 void OnTestEvent1(wxEvent
& )
59 void OnTestEvent2(wxEvent
&)
61 gs_value
+= 0x01000000;
67 // --------------------------------------------------------------------------
69 // --------------------------------------------------------------------------
71 class EvtConnectionTestCase
: public CppUnit::TestCase
74 EvtConnectionTestCase() {}
77 CPPUNIT_TEST_SUITE( EvtConnectionTestCase
);
78 CPPUNIT_TEST( SinkTest
);
79 CPPUNIT_TEST( SourceDestroyTest
);
80 CPPUNIT_TEST( MultiConnectionTest
);
81 CPPUNIT_TEST_SUITE_END();
84 void SourceDestroyTest();
85 void MultiConnectionTest();
87 DECLARE_NO_COPY_CLASS(EvtConnectionTestCase
)
90 // register in the unnamed registry so that these tests are run by default
91 CPPUNIT_TEST_SUITE_REGISTRATION( EvtConnectionTestCase
);
93 // also include in its own registry so that these tests can be run alone
94 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtConnectionTestCase
, "EvtConnectionTestCase" );
98 void DoConnect( wxEvtHandler
& eh1
, wxEvtHandler
& eh2
, wxTestSink
& ts
){
99 eh1
.Connect(wxEVT_TEST
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent
,
101 eh2
.Connect(wxEVT_TEST
, (wxObjectEventFunction
) &wxTestSink::OnTestEvent
,
105 void DoDisconnect( wxEvtHandler
& eh1
, wxEvtHandler
& eh2
, wxTestSink
& ts
){
106 eh1
.Disconnect(wxEVT_TEST
, (wxObjectEventFunction
) &wxTestSink::OnTestEvent
,
108 eh2
.Disconnect(wxEVT_TEST
, (wxObjectEventFunction
) &wxTestSink::OnTestEvent
,
113 void EvtConnectionTestCase::SinkTest()
115 // Let the sink be destroyed before the sources
117 // An event used below
120 // Connect two event handlers to one sink
121 wxEvtHandler eh1
, eh2
;
127 CPPUNIT_ASSERT( !ts
.GetFirst() );
128 DoConnect(eh1
, eh2
, ts
);
130 DoDisconnect(eh1
, eh2
, ts
);
132 DoConnect(eh1
, eh2
, ts
);
135 evt
.SetEventObject(&eh1
);
136 eh1
.ProcessEvent(evt
);
137 evt
.SetEventObject(&eh2
);
138 eh2
.ProcessEvent(evt
);
140 // Make sure they were processed correctly
141 CPPUNIT_ASSERT_EQUAL( 0x00010001, gs_value
);
144 // Fire events again, should be no sink connected now
146 evt
.SetEventObject(&eh1
);
147 eh1
.ProcessEvent( evt
);
148 evt
.SetEventObject(&eh2
);
149 eh2
.ProcessEvent( evt
);
151 // Make sure no processing happened
152 CPPUNIT_ASSERT_EQUAL( 0, gs_value
);
155 void EvtConnectionTestCase::SourceDestroyTest()
157 // Let the sources be destroyed before the sink
163 CPPUNIT_ASSERT( !ts
.GetFirst() );
165 // Connect two event handlers to one sink
169 DoConnect( eh1
, eh2
, ts
);
173 evt
.SetEventObject(&eh1
);
174 eh1
.ProcessEvent( evt
);
175 evt
.SetEventObject(&eh2
);
176 eh2
.ProcessEvent( evt
);
178 // Make sure they were processed correctly
179 CPPUNIT_ASSERT_EQUAL( 0x00010001, gs_value
);
183 evt
.SetEventObject(&eh1
);
184 eh1
.ProcessEvent( evt
);
186 // Make sure still connected
187 CPPUNIT_ASSERT_EQUAL( 0x00000001, gs_value
);
189 CPPUNIT_ASSERT( !ts
.GetFirst() );
192 void EvtConnectionTestCase::MultiConnectionTest()
196 wxTestEvent
evt1(wxEVT_TEST1
);
197 wxTestEvent
evt2(wxEVT_TEST2
);
201 evt
.SetEventObject(&eh1
);
209 eh1
.Connect(wxEVT_TEST
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent
,
211 eh1
.Connect(wxEVT_TEST1
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent1
,
213 eh1
.Connect(wxEVT_TEST2
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent2
,
218 eh1
.ProcessEvent(evt
);
219 eh1
.ProcessEvent(evt1
);
220 eh1
.ProcessEvent(evt2
);
221 CPPUNIT_ASSERT( gs_value
==0x01010100 );
224 // Declare weak references to the objects (using same list)
225 wxEvtHandlerRef
re(&eh1
), rs(&ts
);
229 eh1
.Disconnect(wxEVT_TEST
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent
,
231 eh1
.ProcessEvent(evt
);
232 eh1
.ProcessEvent(evt1
);
233 eh1
.ProcessEvent(evt2
);
234 CPPUNIT_ASSERT_EQUAL( 0x02010200, gs_value
);
237 // No connection should be left now
239 eh1
.ProcessEvent(evt
);
240 eh1
.ProcessEvent(evt1
);
241 eh1
.ProcessEvent(evt2
);
243 // Nothing should have been done
244 CPPUNIT_ASSERT_EQUAL( 0, gs_value
);