1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/weakref/weakref.cpp
// Purpose: wxWeakRef<T> unit test
// Author: Arne Steinarson
// Created: 2008-01-10
// Copyright: (c) 2007 Arne Steinarson
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif // WX_PRECOMP
#include "wx/event.h"
#include "wx/weakref.h"
// A statically trackable derived wxObject
class wxObjectTrackable : public wxObject, public wxTrackable
{
public:
// Test member access
void TestFunc(){ }
// Make sure this does not clash with wxTrackableBase method
int GetFirst() { return 0; }
};
// --------------------------------------------------------------------------
// test class
// --------------------------------------------------------------------------
class WeakRefTestCase : public CppUnit::TestCase
{
public:
WeakRefTestCase() {}
private:
CPPUNIT_TEST_SUITE( WeakRefTestCase );
CPPUNIT_TEST( DeclareTest );
CPPUNIT_TEST( AssignTest );
CPPUNIT_TEST( AssignWeakRefTest );
CPPUNIT_TEST( MultiAssignTest );
CPPUNIT_TEST( CleanupTest );
CPPUNIT_TEST( DeleteTest );
#ifdef HAVE_DYNAMIC_CAST
CPPUNIT_TEST( DynamicRefTest );
#endif
CPPUNIT_TEST_SUITE_END();
void DeclareTest();
void AssignTest();
void AssignWeakRefTest();
void MultiAssignTest();
void CleanupTest();
void DeleteTest();
#ifdef HAVE_DYNAMIC_CAST
void DynamicRefTest();
#endif
DECLARE_NO_COPY_CLASS(WeakRefTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( WeakRefTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WeakRefTestCase, "WeakRefTestCase" );
// Test weak reference to an incomplete type, this should work if the type is
// fully defined before it is used (but currently doesn't, see #11916)
struct ForwardDeclaredClass;
wxWeakRef<ForwardDeclaredClass> g_incompleteWeakRef;
struct ForwardDeclaredClass : wxEvtHandler { };
void WeakRefTestCase::DeclareTest()
{
{
// Not initializing or initializing with NULL should work too
//
// FIXME-VC6: but it doesn't with VC6, see comment in wx/weakref.h
#ifndef __VISUALC6__
wxWeakRef<wxEvtHandler> wroDef;
wxWeakRef<wxEvtHandler> wro0(NULL);
#endif // __VISUALC6__
wxObject o; // Should not work
wxEvtHandler eh;
wxObjectTrackable ot;
// Test declare when T is wxObject
// wxWeakRef<wxObject> wro1(&o); // Gives compile time failure
wxWeakRef<wxEvtHandler> wro2(&eh);
wxWeakRef<wxObjectTrackable> wro3(&ot);
CPPUNIT_ASSERT( wro2.get() == &eh );
CPPUNIT_ASSERT( wro3.get() == &ot );
// Test accessing wxObject members
CPPUNIT_ASSERT( !wro2->GetRefData() );
CPPUNIT_ASSERT( !wro3->GetRefData() );
wxWeakRef<wxEvtHandler> wreh(&eh);
wxWeakRef<wxObjectTrackable> wrot(&ot);
CPPUNIT_ASSERT( wreh.get() == &eh );
CPPUNIT_ASSERT( wrot.get() == &ot );
}
// This test requires a working dynamic_cast<>
#ifndef wxNO_RTTI
{
ForwardDeclaredClass fdc;
g_incompleteWeakRef = &fdc;
CPPUNIT_ASSERT( g_incompleteWeakRef );
}
CPPUNIT_ASSERT( !g_incompleteWeakRef );
#endif // RTTI enabled
}
void WeakRefTestCase::AssignTest()
{
wxWeakRef<wxEvtHandler> wro1;
wxWeakRef<wxObjectTrackable> wro2;
{ // Scope for object destruction
wxEvtHandler eh;
wxObjectTrackable ot;
wro1 = &eh;
wro2 = &ot;
CPPUNIT_ASSERT( wro1.get() == &eh );
CPPUNIT_ASSERT( wro2.get() == &ot );
}
// Should be reset now
CPPUNIT_ASSERT( !wro1 );
CPPUNIT_ASSERT( !wro2 );
// Explicitly resetting should work too
//
// FIXME-VC6: as above, it doesn't work with VC6, see wx/weakref.h
#ifndef __VISUALC6__
wxEvtHandler eh;
wxObjectTrackable ot;
wro1 = &eh;
wro2 = &ot;
wro1 = NULL;
wro2 = NULL;
CPPUNIT_ASSERT( !wro1 );
CPPUNIT_ASSERT( !wro2 );
#endif // __VISUALC6__
}
void WeakRefTestCase::AssignWeakRefTest()
{
// Test declare when T is wxObject
wxWeakRef<wxEvtHandler> wro1;
wxWeakRef<wxObjectTrackable> wro2;
{ // Scope for object destruction
wxEvtHandler eh;
wxObjectTrackable ot;
wxWeakRef<wxEvtHandler> wro3;
wxWeakRef<wxObjectTrackable> wro4;
wro1 = &eh;
wro2 = &ot;
wro3 = wro1;
wro4 = wro2;
CPPUNIT_ASSERT( wro1.get() == &eh );
CPPUNIT_ASSERT( wro2.get() == &ot );
CPPUNIT_ASSERT( wro3.get() == &eh );
CPPUNIT_ASSERT( wro4.get() == &ot );
wro4.Release();
CPPUNIT_ASSERT( !wro4.get() );
}
// Should be reset now
CPPUNIT_ASSERT( !wro1 );
CPPUNIT_ASSERT( !wro2 );
}
void WeakRefTestCase::MultiAssignTest()
{
// Object is tracked by several refs
wxEvtHandler *peh = new wxEvtHandler;
// Test declare when T is wxObject
wxWeakRef<wxEvtHandler> wro1(peh);
wxWeakRef<wxEvtHandler> wro2(peh);
wxObjectTrackable *pot = new wxObjectTrackable;
wxWeakRef<wxObjectTrackable> wro3 = pot;
wxWeakRef<wxObjectTrackable> wro4 = pot;
CPPUNIT_ASSERT( wro1.get() == peh );
CPPUNIT_ASSERT( wro2.get() == peh );
CPPUNIT_ASSERT( wro3.get() == pot );
CPPUNIT_ASSERT( wro4.get() == pot );
delete peh;
delete pot;
// Should be reset now
CPPUNIT_ASSERT( !wro1 );
CPPUNIT_ASSERT( !wro2 );
CPPUNIT_ASSERT( !wro3 );
CPPUNIT_ASSERT( !wro4 );
}
void WeakRefTestCase::CleanupTest()
{
// Make sure that trackable objects have no left over tracker nodes after use.
// This time the references goes out of scope before the objects.
wxEvtHandler eh;
wxObjectTrackable ots;
wxObjectTrackable otd;
{ // Scope for object destruction
wxWeakRef<wxEvtHandler> wro1;
wxWeakRef<wxEvtHandler> wro2;
wxWeakRef<wxObjectTrackable> wro3;
wxWeakRef<wxObjectTrackable> wro4;
wro1 = &eh;
wro2 = &eh; // Has two tracker nodes now
wro3 = &ots;
wro4 = &otd;
// Access members of reffed object
wro3->TestFunc();
CPPUNIT_ASSERT( eh.GetFirst()==&wro2 );
CPPUNIT_ASSERT( ots.wxTrackable::GetFirst()==&wro3 );
CPPUNIT_ASSERT( otd.wxTrackable::GetFirst()==&wro4 );
}
// Should be reset now
CPPUNIT_ASSERT( !eh.GetFirst() );
CPPUNIT_ASSERT( !ots.wxTrackable::GetFirst() );
CPPUNIT_ASSERT( !otd.wxTrackable::GetFirst() );
}
void WeakRefTestCase::DeleteTest()
{
// Object is tracked by several refs
wxEvtHandler *peh = new wxEvtHandler;
// Declared derived type of object and test deleting it
wxEvtHandlerRef wre(peh);
wxWeakRef<wxEvtHandler> wro(peh);
CPPUNIT_ASSERT( wre.get() == peh );
CPPUNIT_ASSERT( wro.get() == peh );
delete wre.get();
CPPUNIT_ASSERT( !wre );
CPPUNIT_ASSERT( !wro );
}
#ifdef HAVE_DYNAMIC_CAST
void WeakRefTestCase::DynamicRefTest()
{
wxWeakRefDynamic<wxEvtHandler> wro1;
wxWeakRefDynamic<wxObjectTrackable> wro2;
wxWeakRefDynamic<wxObjectTrackable> wro3;
{ // Scope for object destruction
{
wxEvtHandler eh;
wro1 = &eh;
}
CPPUNIT_ASSERT( !wro1 );
wxObjectTrackable otd1;
wxObjectTrackable otd2;
wro2 = &otd1;
wro3 = &otd2;
CPPUNIT_ASSERT( wro2.get() == &otd1 );
CPPUNIT_ASSERT( wro3.get() == &otd2 );
wro3 = wro2;
CPPUNIT_ASSERT( wro2.get() == &otd1 );
CPPUNIT_ASSERT( wro3.get() == &otd1 );
}
// Should be reset now
CPPUNIT_ASSERT( !wro2 );
CPPUNIT_ASSERT( !wro3 );
}
#endif // HAVE_DYNAMIC_CAST
|