]>
git.saurik.com Git - wxWidgets.git/blob - tests/weakref/weakref.cpp
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 wxObject o
; // Should not work
86 // Test declare when T is wxObject
87 // wxWeakRef<wxObject> wro1(&o); // Gives compile time failure
88 wxWeakRef
<wxEvtHandler
> wro2(&eh
);
89 wxWeakRef
<wxObjectTrackable
> wro3(&ot
);
91 CPPUNIT_ASSERT( wro2
.get() == &eh
);
92 CPPUNIT_ASSERT( wro3
.get() == &ot
);
94 // Test accessing wxObject members
95 CPPUNIT_ASSERT( !wro2
->GetRefData() );
96 CPPUNIT_ASSERT( !wro3
->GetRefData() );
99 wxWeakRef
<wxEvtHandler
> wreh(&eh
);
100 wxWeakRef
<wxObjectTrackable
> wrot(&ot
);
102 CPPUNIT_ASSERT( wreh
.get() == &eh
);
103 CPPUNIT_ASSERT( wrot
.get() == &ot
);
107 void WeakRefTestCase::AssignTest()
109 wxWeakRef
<wxEvtHandler
> wro1
;
110 wxWeakRef
<wxObjectTrackable
> wro2
;
112 { // Scope for object destruction
114 wxObjectTrackable ot
;
119 CPPUNIT_ASSERT( wro1
.get() == &eh
);
120 CPPUNIT_ASSERT( wro2
.get() == &ot
);
123 // Should be reset now
124 CPPUNIT_ASSERT( !wro1
);
125 CPPUNIT_ASSERT( !wro2
);
128 void WeakRefTestCase::AssignWeakRefTest()
130 // Test declare when T is wxObject
131 wxWeakRef
<wxEvtHandler
> wro1
;
132 wxWeakRef
<wxObjectTrackable
> wro2
;
134 { // Scope for object destruction
136 wxObjectTrackable ot
;
137 wxWeakRef
<wxEvtHandler
> wro3
;
138 wxWeakRef
<wxObjectTrackable
> wro4
;
145 CPPUNIT_ASSERT( wro1
.get() == &eh
);
146 CPPUNIT_ASSERT( wro2
.get() == &ot
);
147 CPPUNIT_ASSERT( wro3
.get() == &eh
);
148 CPPUNIT_ASSERT( wro4
.get() == &ot
);
151 CPPUNIT_ASSERT( !wro4
.get() );
154 // Should be reset now
155 CPPUNIT_ASSERT( !wro1
);
156 CPPUNIT_ASSERT( !wro2
);
159 void WeakRefTestCase::MultiAssignTest()
161 // Object is tracked by several refs
162 wxEvtHandler
*peh
= new wxEvtHandler
;
164 // Test declare when T is wxObject
165 wxWeakRef
<wxEvtHandler
> wro1(peh
);
166 wxWeakRef
<wxEvtHandler
> wro2(peh
);
168 wxObjectTrackable
*pot
= new wxObjectTrackable
;
169 wxWeakRef
<wxObjectTrackable
> wro3
= pot
;
170 wxWeakRef
<wxObjectTrackable
> wro4
= pot
;
172 CPPUNIT_ASSERT( wro1
.get() == peh
);
173 CPPUNIT_ASSERT( wro2
.get() == peh
);
174 CPPUNIT_ASSERT( wro3
.get() == pot
);
175 CPPUNIT_ASSERT( wro4
.get() == pot
);
180 // Should be reset now
181 CPPUNIT_ASSERT( !wro1
);
182 CPPUNIT_ASSERT( !wro2
);
183 CPPUNIT_ASSERT( !wro3
);
184 CPPUNIT_ASSERT( !wro4
);
187 void WeakRefTestCase::CleanupTest()
189 // Make sure that trackable objects have no left over tracker nodes after use.
190 // This time the references goes out of scope before the objects.
192 wxObjectTrackable ots
;
193 wxObjectTrackable otd
;
195 { // Scope for object destruction
196 wxWeakRef
<wxEvtHandler
> wro1
;
197 wxWeakRef
<wxEvtHandler
> wro2
;
198 wxWeakRef
<wxObjectTrackable
> wro3
;
199 wxWeakRef
<wxObjectTrackable
> wro4
;
202 wro2
= &eh
; // Has two tracker nodes now
206 // Access members of reffed object
209 CPPUNIT_ASSERT( eh
.GetFirst()==&wro2
);
210 CPPUNIT_ASSERT( ots
.wxTrackable::GetFirst()==&wro3
);
211 CPPUNIT_ASSERT( otd
.wxTrackable::GetFirst()==&wro4
);
214 // Should be reset now
215 CPPUNIT_ASSERT( !eh
.GetFirst() );
216 CPPUNIT_ASSERT( !ots
.wxTrackable::GetFirst() );
217 CPPUNIT_ASSERT( !otd
.wxTrackable::GetFirst() );
220 void WeakRefTestCase::DeleteTest()
222 // Object is tracked by several refs
223 wxEvtHandler
*peh
= new wxEvtHandler
;
225 // Declared derived type of object and test deleting it
226 wxEvtHandlerRef
wre(peh
);
227 wxWeakRef
<wxEvtHandler
> wro(peh
);
229 CPPUNIT_ASSERT( wre
.get() == peh
);
230 CPPUNIT_ASSERT( wro
.get() == peh
);
234 CPPUNIT_ASSERT( !wre
);
235 CPPUNIT_ASSERT( !wro
);
238 #ifdef HAVE_DYNAMIC_CAST
240 void WeakRefTestCase::DynamicRefTest()
242 wxWeakRefDynamic
<wxEvtHandler
> wro1
;
243 wxWeakRefDynamic
<wxObjectTrackable
> wro2
;
244 wxWeakRefDynamic
<wxObjectTrackable
> wro3
;
246 { // Scope for object destruction
252 CPPUNIT_ASSERT( !wro1
);
254 wxObjectTrackable otd1
;
255 wxObjectTrackable otd2
;
259 CPPUNIT_ASSERT( wro2
.get() == &otd1
);
260 CPPUNIT_ASSERT( wro3
.get() == &otd2
);
263 CPPUNIT_ASSERT( wro2
.get() == &otd1
);
264 CPPUNIT_ASSERT( wro3
.get() == &otd1
);
267 // Should be reset now
268 CPPUNIT_ASSERT( !wro2
);
269 CPPUNIT_ASSERT( !wro3
);
272 #endif // HAVE_DYNAMIC_CAST