]>
git.saurik.com Git - wxWidgets.git/blob - tests/weakref/weakref.cpp
3550d138055c243ff392d29fff8114c8ae85eabe
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/weakref/weakref.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 // A statically trackable derived wxObject
28 class wxObjectTrackable
: public wxObject
, public wxTrackable
34 // Make sure this does not clash with wxTrackableBase method
35 int GetFirst() { return 0; }
38 // --------------------------------------------------------------------------
40 // --------------------------------------------------------------------------
42 class WeakRefTestCase
: public CppUnit::TestCase
48 CPPUNIT_TEST_SUITE( WeakRefTestCase
);
49 CPPUNIT_TEST( DeclareTest
);
50 CPPUNIT_TEST( AssignTest
);
51 CPPUNIT_TEST( AssignWeakRefTest
);
52 CPPUNIT_TEST( MultiAssignTest
);
53 CPPUNIT_TEST( CleanupTest
);
54 CPPUNIT_TEST( DeleteTest
);
55 #ifdef HAVE_DYNAMIC_CAST
56 CPPUNIT_TEST( DynamicRefTest
);
58 CPPUNIT_TEST_SUITE_END();
62 void AssignWeakRefTest();
63 void MultiAssignTest();
66 #ifdef HAVE_DYNAMIC_CAST
67 void DynamicRefTest();
70 DECLARE_NO_COPY_CLASS(WeakRefTestCase
)
73 // register in the unnamed registry so that these tests are run by default
74 CPPUNIT_TEST_SUITE_REGISTRATION( WeakRefTestCase
);
76 // also include in it's own registry so that these tests can be run alone
77 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WeakRefTestCase
, "WeakRefTestCase" );
79 void WeakRefTestCase::DeclareTest()
82 // Not initializing or initializing with NULL should work too
83 wxWeakRef
<wxEvtHandler
> wroDef
;
84 wxWeakRef
<wxEvtHandler
> wro0(NULL
);
86 wxObject o
; // Should not work
90 // Test declare when T is wxObject
91 // wxWeakRef<wxObject> wro1(&o); // Gives compile time failure
92 wxWeakRef
<wxEvtHandler
> wro2(&eh
);
93 wxWeakRef
<wxObjectTrackable
> wro3(&ot
);
95 CPPUNIT_ASSERT( wro2
.get() == &eh
);
96 CPPUNIT_ASSERT( wro3
.get() == &ot
);
98 // Test accessing wxObject members
99 CPPUNIT_ASSERT( !wro2
->GetRefData() );
100 CPPUNIT_ASSERT( !wro3
->GetRefData() );
103 wxWeakRef
<wxEvtHandler
> wreh(&eh
);
104 wxWeakRef
<wxObjectTrackable
> wrot(&ot
);
106 CPPUNIT_ASSERT( wreh
.get() == &eh
);
107 CPPUNIT_ASSERT( wrot
.get() == &ot
);
111 void WeakRefTestCase::AssignTest()
113 wxWeakRef
<wxEvtHandler
> wro1
;
114 wxWeakRef
<wxObjectTrackable
> wro2
;
116 { // Scope for object destruction
118 wxObjectTrackable ot
;
123 CPPUNIT_ASSERT( wro1
.get() == &eh
);
124 CPPUNIT_ASSERT( wro2
.get() == &ot
);
127 // Should be reset now
128 CPPUNIT_ASSERT( !wro1
);
129 CPPUNIT_ASSERT( !wro2
);
131 // Explicitly resetting should work too
133 wxObjectTrackable ot
;
141 CPPUNIT_ASSERT( !wro1
);
142 CPPUNIT_ASSERT( !wro2
);
145 void WeakRefTestCase::AssignWeakRefTest()
147 // Test declare when T is wxObject
148 wxWeakRef
<wxEvtHandler
> wro1
;
149 wxWeakRef
<wxObjectTrackable
> wro2
;
151 { // Scope for object destruction
153 wxObjectTrackable ot
;
154 wxWeakRef
<wxEvtHandler
> wro3
;
155 wxWeakRef
<wxObjectTrackable
> wro4
;
162 CPPUNIT_ASSERT( wro1
.get() == &eh
);
163 CPPUNIT_ASSERT( wro2
.get() == &ot
);
164 CPPUNIT_ASSERT( wro3
.get() == &eh
);
165 CPPUNIT_ASSERT( wro4
.get() == &ot
);
168 CPPUNIT_ASSERT( !wro4
.get() );
171 // Should be reset now
172 CPPUNIT_ASSERT( !wro1
);
173 CPPUNIT_ASSERT( !wro2
);
176 void WeakRefTestCase::MultiAssignTest()
178 // Object is tracked by several refs
179 wxEvtHandler
*peh
= new wxEvtHandler
;
181 // Test declare when T is wxObject
182 wxWeakRef
<wxEvtHandler
> wro1(peh
);
183 wxWeakRef
<wxEvtHandler
> wro2(peh
);
185 wxObjectTrackable
*pot
= new wxObjectTrackable
;
186 wxWeakRef
<wxObjectTrackable
> wro3
= pot
;
187 wxWeakRef
<wxObjectTrackable
> wro4
= pot
;
189 CPPUNIT_ASSERT( wro1
.get() == peh
);
190 CPPUNIT_ASSERT( wro2
.get() == peh
);
191 CPPUNIT_ASSERT( wro3
.get() == pot
);
192 CPPUNIT_ASSERT( wro4
.get() == pot
);
197 // Should be reset now
198 CPPUNIT_ASSERT( !wro1
);
199 CPPUNIT_ASSERT( !wro2
);
200 CPPUNIT_ASSERT( !wro3
);
201 CPPUNIT_ASSERT( !wro4
);
204 void WeakRefTestCase::CleanupTest()
206 // Make sure that trackable objects have no left over tracker nodes after use.
207 // This time the references goes out of scope before the objects.
209 wxObjectTrackable ots
;
210 wxObjectTrackable otd
;
212 { // Scope for object destruction
213 wxWeakRef
<wxEvtHandler
> wro1
;
214 wxWeakRef
<wxEvtHandler
> wro2
;
215 wxWeakRef
<wxObjectTrackable
> wro3
;
216 wxWeakRef
<wxObjectTrackable
> wro4
;
219 wro2
= &eh
; // Has two tracker nodes now
223 // Access members of reffed object
226 CPPUNIT_ASSERT( eh
.GetFirst()==&wro2
);
227 CPPUNIT_ASSERT( ots
.wxTrackable::GetFirst()==&wro3
);
228 CPPUNIT_ASSERT( otd
.wxTrackable::GetFirst()==&wro4
);
231 // Should be reset now
232 CPPUNIT_ASSERT( !eh
.GetFirst() );
233 CPPUNIT_ASSERT( !ots
.wxTrackable::GetFirst() );
234 CPPUNIT_ASSERT( !otd
.wxTrackable::GetFirst() );
237 void WeakRefTestCase::DeleteTest()
239 // Object is tracked by several refs
240 wxEvtHandler
*peh
= new wxEvtHandler
;
242 // Declared derived type of object and test deleting it
243 wxEvtHandlerRef
wre(peh
);
244 wxWeakRef
<wxEvtHandler
> wro(peh
);
246 CPPUNIT_ASSERT( wre
.get() == peh
);
247 CPPUNIT_ASSERT( wro
.get() == peh
);
251 CPPUNIT_ASSERT( !wre
);
252 CPPUNIT_ASSERT( !wro
);
255 #ifdef HAVE_DYNAMIC_CAST
257 void WeakRefTestCase::DynamicRefTest()
259 wxWeakRefDynamic
<wxEvtHandler
> wro1
;
260 wxWeakRefDynamic
<wxObjectTrackable
> wro2
;
261 wxWeakRefDynamic
<wxObjectTrackable
> wro3
;
263 { // Scope for object destruction
269 CPPUNIT_ASSERT( !wro1
);
271 wxObjectTrackable otd1
;
272 wxObjectTrackable otd2
;
276 CPPUNIT_ASSERT( wro2
.get() == &otd1
);
277 CPPUNIT_ASSERT( wro3
.get() == &otd2
);
280 CPPUNIT_ASSERT( wro2
.get() == &otd1
);
281 CPPUNIT_ASSERT( wro3
.get() == &otd1
);
284 // Should be reset now
285 CPPUNIT_ASSERT( !wro2
);
286 CPPUNIT_ASSERT( !wro3
);
289 #endif // HAVE_DYNAMIC_CAST