]>
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
7 // Copyright: (c) 2007 Arne Steinarson
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/weakref.h"
27 static int gs_value
= 0; // Increased by 1 by first object and 0x10000 by 2nd object
29 static wxObject
*gs_psrc1
;
30 static wxObject
*gs_psrc2
;
32 // We need some event types
33 const wxEventType wxEVT_TEST
= wxNewEventType(),
34 wxEVT_TEST1
= wxNewEventType(),
35 wxEVT_TEST2
= wxNewEventType();
37 class wxTestEvent
: public wxEvent
40 wxTestEvent(wxEventType type
= wxEVT_TEST
) : wxEvent(0, type
) { }
41 virtual wxEvent
*Clone() const { return new wxTestEvent(GetEventType()); }
44 class wxTestSink
: public wxEvtHandler
47 void OnTestEvent(wxEvent
& evt
)
49 if ( evt
.GetEventObject() == gs_psrc1
)
51 else if ( evt
.GetEventObject() == gs_psrc2
)
55 void OnTestEvent1(wxEvent
& )
60 void OnTestEvent2(wxEvent
&)
62 gs_value
+= 0x01000000;
68 // --------------------------------------------------------------------------
70 // --------------------------------------------------------------------------
72 class EvtConnectionTestCase
: public CppUnit::TestCase
75 EvtConnectionTestCase() {}
78 CPPUNIT_TEST_SUITE( EvtConnectionTestCase
);
79 CPPUNIT_TEST( SinkTest
);
80 CPPUNIT_TEST( SourceDestroyTest
);
81 CPPUNIT_TEST( MultiConnectionTest
);
82 CPPUNIT_TEST_SUITE_END();
85 void SourceDestroyTest();
86 void MultiConnectionTest();
88 DECLARE_NO_COPY_CLASS(EvtConnectionTestCase
)
91 // register in the unnamed registry so that these tests are run by default
92 CPPUNIT_TEST_SUITE_REGISTRATION( EvtConnectionTestCase
);
94 // also include in it's own registry so that these tests can be run alone
95 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtConnectionTestCase
, "EvtConnectionTestCase" );
99 void DoConnect( wxEvtHandler
& eh1
, wxEvtHandler
& eh2
, wxTestSink
& ts
){
100 eh1
.Connect(wxEVT_TEST
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent
,
102 eh2
.Connect(wxEVT_TEST
, (wxObjectEventFunction
) &wxTestSink::OnTestEvent
,
106 void DoDisconnect( wxEvtHandler
& eh1
, wxEvtHandler
& eh2
, wxTestSink
& ts
){
107 eh1
.Disconnect(wxEVT_TEST
, (wxObjectEventFunction
) &wxTestSink::OnTestEvent
,
109 eh2
.Disconnect(wxEVT_TEST
, (wxObjectEventFunction
) &wxTestSink::OnTestEvent
,
114 void EvtConnectionTestCase::SinkTest()
116 // Let the sink be destroyed before the sources
118 // An event used below
121 // Connect two event handlers to one sink
122 wxEvtHandler eh1
, eh2
;
128 CPPUNIT_ASSERT( !ts
.GetFirst() );
129 DoConnect(eh1
, eh2
, ts
);
131 DoDisconnect(eh1
, eh2
, ts
);
133 DoConnect(eh1
, eh2
, ts
);
136 evt
.SetEventObject(&eh1
);
137 eh1
.ProcessEvent(evt
);
138 evt
.SetEventObject(&eh2
);
139 eh2
.ProcessEvent(evt
);
141 // Make sure they were processed correctly
142 CPPUNIT_ASSERT_EQUAL( 0x00010001, gs_value
);
145 // Fire events again, should be no sink connected now
147 evt
.SetEventObject(&eh1
);
148 eh1
.ProcessEvent( evt
);
149 evt
.SetEventObject(&eh2
);
150 eh2
.ProcessEvent( evt
);
152 // Make sure no processing happened
153 CPPUNIT_ASSERT_EQUAL( 0, gs_value
);
156 void EvtConnectionTestCase::SourceDestroyTest()
158 // Let the sources be destroyed before the sink
164 CPPUNIT_ASSERT( !ts
.GetFirst() );
166 // Connect two event handlers to one sink
170 DoConnect( eh1
, eh2
, ts
);
174 evt
.SetEventObject(&eh1
);
175 eh1
.ProcessEvent( evt
);
176 evt
.SetEventObject(&eh2
);
177 eh2
.ProcessEvent( evt
);
179 // Make sure they were processed correctly
180 CPPUNIT_ASSERT_EQUAL( 0x00010001, gs_value
);
184 evt
.SetEventObject(&eh1
);
185 eh1
.ProcessEvent( evt
);
187 // Make sure still connected
188 CPPUNIT_ASSERT_EQUAL( 0x00000001, gs_value
);
190 CPPUNIT_ASSERT( !ts
.GetFirst() );
193 void EvtConnectionTestCase::MultiConnectionTest()
197 wxTestEvent
evt1(wxEVT_TEST1
);
198 wxTestEvent
evt2(wxEVT_TEST2
);
202 evt
.SetEventObject(&eh1
);
210 eh1
.Connect(wxEVT_TEST
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent
,
212 eh1
.Connect(wxEVT_TEST1
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent1
,
214 eh1
.Connect(wxEVT_TEST2
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent2
,
219 eh1
.ProcessEvent(evt
);
220 eh1
.ProcessEvent(evt1
);
221 eh1
.ProcessEvent(evt2
);
222 CPPUNIT_ASSERT( gs_value
==0x01010100 );
225 // Declare weak references to the objects (using same list)
226 wxEvtHandlerRef
re(&eh1
), rs(&ts
);
230 eh1
.Disconnect(wxEVT_TEST
, (wxObjectEventFunction
)&wxTestSink::OnTestEvent
,
232 eh1
.ProcessEvent(evt
);
233 eh1
.ProcessEvent(evt1
);
234 eh1
.ProcessEvent(evt2
);
235 CPPUNIT_ASSERT_EQUAL( 0x02010200, gs_value
);
238 // No connection should be left now
240 eh1
.ProcessEvent(evt
);
241 eh1
.ProcessEvent(evt1
);
242 eh1
.ProcessEvent(evt2
);
244 // Nothing should have been done
245 CPPUNIT_ASSERT_EQUAL( 0, gs_value
);