Bug 545892 - Always pass WM_NCPAINT events to the default event procedure. r=bent...
[mozilla-central.git] / xpcom / tests / TestCOMPtr.cpp
blobca377ba27ead9b893fb2e810dd9c297f74d1611f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include <assert.h>
40 #include <stdio.h>
41 #include "nsCOMPtr.h"
42 #include "nsISupports.h"
44 #ifdef HAVE_CPP_NEW_CASTS
45 #define STATIC_CAST(T,x) static_cast<T>(x)
46 #define REINTERPRET_CAST(T,x) reinterpret_cast<T>(x)
47 #else
48 #define STATIC_CAST(T,x) ((T)(x))
49 #define REINTERPRET_CAST(T,x) ((T)(x))
50 #endif
53 #define NS_IFOO_IID \
54 { 0x6f7652e0, 0xee43, 0x11d1, \
55 { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
57 class IFoo : public nsISupports
59 public:
60 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
62 public:
63 IFoo();
64 // virtual dtor because IBar uses our Release()
65 virtual ~IFoo();
67 NS_IMETHOD_(nsrefcnt) AddRef();
68 NS_IMETHOD_(nsrefcnt) Release();
69 NS_IMETHOD QueryInterface( const nsIID&, void** );
71 static void print_totals();
73 private:
74 unsigned int refcount_;
76 static unsigned int total_constructions_;
77 static unsigned int total_destructions_;
80 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
82 class IBar;
84 // some types I'll need
85 typedef unsigned long NS_RESULT;
87 // some functions I'll need (and define below)
88 nsresult CreateIFoo( void** );
89 nsresult CreateIBar( void** result );
90 void AnIFooPtrPtrContext( IFoo** );
91 void AnISupportsPtrPtrContext( nsISupports** );
92 void AVoidPtrPtrContext( void** );
93 void set_a_IFoo( nsCOMPtr<IFoo>* result );
94 nsCOMPtr<IFoo> return_a_IFoo();
99 unsigned int IFoo::total_constructions_;
100 unsigned int IFoo::total_destructions_;
102 class test_message
104 public:
105 test_message()
107 printf("BEGIN unit tests for |nsCOMPtr|, compiled " __DATE__ "\n");
110 ~test_message()
112 IFoo::print_totals();
113 printf("END unit tests for |nsCOMPtr|.\n");
117 test_message gTestMessage;
124 void
125 IFoo::print_totals()
127 printf("total constructions/destructions --> %d/%d\n",
128 total_constructions_, total_destructions_);
131 IFoo::IFoo()
132 : refcount_(0)
134 ++total_constructions_;
135 printf(" new IFoo@%p [#%d]\n",
136 STATIC_CAST(void*, this), total_constructions_);
139 IFoo::~IFoo()
141 ++total_destructions_;
142 printf("IFoo@%p::~IFoo() [#%d]\n",
143 STATIC_CAST(void*, this), total_destructions_);
146 nsrefcnt
147 IFoo::AddRef()
149 ++refcount_;
150 printf("IFoo@%p::AddRef(), refcount --> %d\n",
151 STATIC_CAST(void*, this), refcount_);
152 return refcount_;
155 nsrefcnt
156 IFoo::Release()
158 int newcount = --refcount_;
159 if ( newcount == 0 )
160 printf(">>");
162 printf("IFoo@%p::Release(), refcount --> %d\n",
163 STATIC_CAST(void*, this), refcount_);
165 if ( newcount == 0 )
167 printf(" delete IFoo@%p\n", STATIC_CAST(void*, this));
168 printf("<<IFoo@%p::Release()\n", STATIC_CAST(void*, this));
169 delete this;
172 return newcount;
175 nsresult
176 IFoo::QueryInterface( const nsIID& aIID, void** aResult )
178 printf("IFoo@%p::QueryInterface()\n", STATIC_CAST(void*, this));
179 nsISupports* rawPtr = 0;
180 nsresult status = NS_OK;
182 if ( aIID.Equals(GetIID()) )
183 rawPtr = this;
184 else
186 nsID iid_of_ISupports = NS_ISUPPORTS_IID;
187 if ( aIID.Equals(iid_of_ISupports) )
188 rawPtr = STATIC_CAST(nsISupports*, this);
189 else
190 status = NS_ERROR_NO_INTERFACE;
193 NS_IF_ADDREF(rawPtr);
194 *aResult = rawPtr;
196 return status;
199 nsresult
200 CreateIFoo( void** result )
201 // a typical factory function (that calls AddRef)
203 printf(">>CreateIFoo() --> ");
204 IFoo* foop = new IFoo;
205 printf("IFoo@%p\n", STATIC_CAST(void*, foop));
207 foop->AddRef();
208 *result = foop;
210 printf("<<CreateIFoo()\n");
211 return 0;
214 void
215 set_a_IFoo( nsCOMPtr<IFoo>* result )
217 printf(">>set_a_IFoo()\n");
218 assert(result);
220 nsCOMPtr<IFoo> foop( do_QueryInterface(new IFoo) );
221 *result = foop;
222 printf("<<set_a_IFoo()\n");
225 nsCOMPtr<IFoo>
226 return_a_IFoo()
228 printf(">>return_a_IFoo()\n");
229 nsCOMPtr<IFoo> foop( do_QueryInterface(new IFoo) );
230 printf("<<return_a_IFoo()\n");
231 return foop;
237 #define NS_IBAR_IID \
238 { 0x6f7652e1, 0xee43, 0x11d1, \
239 { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
241 class IBar : public IFoo
243 public:
244 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBAR_IID)
246 public:
247 IBar();
248 virtual ~IBar();
250 NS_IMETHOD QueryInterface( const nsIID&, void** );
253 NS_DEFINE_STATIC_IID_ACCESSOR(IBar, NS_IBAR_IID)
255 IBar::IBar()
257 printf(" new IBar@%p\n", STATIC_CAST(void*, this));
260 IBar::~IBar()
262 printf("IBar@%p::~IBar()\n", STATIC_CAST(void*, this));
265 nsresult
266 IBar::QueryInterface( const nsID& aIID, void** aResult )
268 printf("IBar@%p::QueryInterface()\n", STATIC_CAST(void*, this));
269 nsISupports* rawPtr = 0;
270 nsresult status = NS_OK;
272 if ( aIID.Equals(GetIID()) )
273 rawPtr = this;
274 else if ( aIID.Equals(NS_GET_IID(IFoo)) )
275 rawPtr = STATIC_CAST(IFoo*, this);
276 else
278 nsID iid_of_ISupports = NS_ISUPPORTS_IID;
279 if ( aIID.Equals(iid_of_ISupports) )
280 rawPtr = STATIC_CAST(nsISupports*, this);
281 else
282 status = NS_ERROR_NO_INTERFACE;
285 NS_IF_ADDREF(rawPtr);
286 *aResult = rawPtr;
288 return status;
293 nsresult
294 CreateIBar( void** result )
295 // a typical factory function (that calls AddRef)
297 printf(">>CreateIBar() --> ");
298 IBar* barp = new IBar;
299 printf("IBar@%p\n", STATIC_CAST(void*, barp));
301 barp->AddRef();
302 *result = barp;
304 printf("<<CreateIBar()\n");
305 return 0;
308 void
309 AnIFooPtrPtrContext( IFoo** )
313 void
314 AVoidPtrPtrContext( void** )
318 void
319 AnISupportsPtrPtrContext( nsISupports** )
323 static
324 nsresult
325 TestBloat_Raw_Unsafe()
327 IBar* barP = 0;
328 nsresult result = CreateIBar(REINTERPRET_CAST(void**, &barP));
330 if ( barP )
332 IFoo* fooP = 0;
333 if ( NS_SUCCEEDED( result = barP->QueryInterface(NS_GET_IID(IFoo), REINTERPRET_CAST(void**, &fooP)) ) )
335 fooP->print_totals();
336 NS_RELEASE(fooP);
339 NS_RELEASE(barP);
342 return result;
346 static
347 nsresult
348 TestBloat_Smart()
350 nsCOMPtr<IBar> barP;
351 nsresult result = CreateIBar( getter_AddRefs(barP) );
353 nsCOMPtr<IFoo> fooP( do_QueryInterface(barP, &result) );
355 if ( fooP )
356 fooP->print_totals();
358 return result;
364 nsCOMPtr<IFoo> gFoop;
367 main()
369 printf(">>main()\n");
371 printf("sizeof(nsCOMPtr<IFoo>) --> %u\n", unsigned(sizeof(nsCOMPtr<IFoo>)));
373 TestBloat_Raw_Unsafe();
374 TestBloat_Smart();
378 printf("\n### Test 1: will a |nsCOMPtr| call |AddRef| on a pointer assigned into it?\n");
379 nsCOMPtr<IFoo> foop( do_QueryInterface(new IFoo) );
381 printf("\n### Test 2: will a |nsCOMPtr| |Release| its old pointer when a new one is assigned in?\n");
382 foop = do_QueryInterface(new IFoo);
384 // [Shouldn't compile] Is it a compile time error to try to |AddRef| by hand?
385 //foop->AddRef();
387 // [Shouldn't compile] Is it a compile time error to try to |Release| be hand?
388 //foop->Release();
390 // [Shouldn't compile] Is it a compile time error to try to |delete| an |nsCOMPtr|?
391 //delete foop;
393 printf("\n### Test 3: can you |AddRef| if you must?\n");
394 STATIC_CAST(IFoo*, foop)->AddRef();
396 printf("\n### Test 4: can you |Release| if you must?\n");
397 STATIC_CAST(IFoo*, foop)->Release();
399 printf("\n### Test 5: will a |nsCOMPtr| |Release| when it goes out of scope?\n");
403 printf("\n### Test 6: will a |nsCOMPtr| call the correct destructor?\n");
404 nsCOMPtr<IFoo> foop( do_QueryInterface(new IBar) );
408 printf("\n### Test 7: can you compare one |nsCOMPtr| with another [!=]?\n");
410 nsCOMPtr<IFoo> foo1p( do_QueryInterface(new IFoo) );
412 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
413 //AnIFooPtrPtrContext(&foo1p);
415 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
416 //AVoidPtrPtrContext(&foo1p);
418 nsCOMPtr<IFoo> foo2p( do_QueryInterface(new IFoo) );
420 if ( foo1p != foo2p )
421 printf("foo1p != foo2p\n");
422 else
423 printf("foo1p == foo2p\n");
425 printf("\n### Test 7.5: can you compare a |nsCOMPtr| with NULL, 0, nsnull [!=]?\n");
426 if ( foo1p != 0 )
427 printf("foo1p != 0\n");
428 if ( 0 != foo1p )
429 printf("0 != foo1p\n");
430 if ( foo1p == 0 )
431 printf("foo1p == 0\n");
432 if ( 0 == foo1p )
433 printf("0 == foo1p\n");
436 IFoo* raw_foo2p = foo2p.get();
438 printf("\n### Test 8: can you compare a |nsCOMPtr| with a raw interface pointer [!=]?\n");
439 if ( foo1p.get() != raw_foo2p )
440 printf("foo1p != raw_foo2p\n");
441 else
442 printf("foo1p == raw_foo2p\n");
445 printf("\n### Test 9: can you assign one |nsCOMPtr| into another?\n");
446 foo1p = foo2p;
448 printf("\n### Test 10: can you compare one |nsCOMPtr| with another [==]?\n");
449 if ( foo1p == foo2p )
450 printf("foo1p == foo2p\n");
451 else
452 printf("foo1p != foo2p\n");
454 printf("\n### Test 11: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
455 if ( raw_foo2p == foo2p.get() )
456 printf("raw_foo2p == foo2p\n");
457 else
458 printf("raw_foo2p != foo2p\n");
460 #if 1
461 printf("\n### Test 11.5: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
462 if ( nsCOMPtr<IFoo>( raw_foo2p ) == foo2p )
463 printf("raw_foo2p == foo2p\n");
464 else
465 printf("raw_foo2p != foo2p\n");
466 #endif
468 printf("\n### Test 12: bare pointer test?\n");
469 if ( foo1p )
470 printf("foo1p is not NULL\n");
471 else
472 printf("foo1p is NULL\n");
474 printf("\n### Test 13: numeric pointer test?\n");
475 if ( foo1p == 0 )
476 printf("foo1p is NULL\n");
477 else
478 printf("foo1p is not NULL\n");
480 #if 0
481 if ( foo1p == 1 )
482 printf("foo1p allowed compare with in\n");
483 #endif
485 printf("\n### Test 14: how about when two |nsCOMPtr|s referring to the same object go out of scope?\n");
489 printf("\n### Test 15,16 ...setup...\n");
490 IFoo* raw_foo1p = new IFoo;
491 raw_foo1p->AddRef();
493 IFoo* raw_foo2p = new IFoo;
494 raw_foo2p->AddRef();
496 printf("\n### Test 15: what if I don't want to |AddRef| when I construct?\n");
497 nsCOMPtr<IFoo> foo1p( dont_AddRef(raw_foo1p) );
498 //nsCOMPtr<IFoo> foo1p = dont_AddRef(raw_foo1p);
500 printf("\n### Test 16: what if I don't want to |AddRef| when I assign in?\n");
501 nsCOMPtr<IFoo> foo2p;
502 foo2p = dont_AddRef(raw_foo2p);
512 printf("\n### setup for Test 17\n");
513 nsCOMPtr<IFoo> foop;
514 printf("### Test 17: basic parameter behavior?\n");
515 CreateIFoo( nsGetterAddRefs<IFoo>(foop) );
517 printf("### End Test 17\n");
521 printf("\n### setup for Test 18\n");
522 nsCOMPtr<IFoo> foop;
523 printf("### Test 18: basic parameter behavior, using the short form?\n");
524 CreateIFoo( getter_AddRefs(foop) );
526 printf("### End Test 18\n");
530 printf("\n### setup for Test 19, 20\n");
531 nsCOMPtr<IFoo> foop;
532 printf("### Test 19: reference parameter behavior?\n");
533 set_a_IFoo(address_of(foop));
535 printf("### Test 20: return value behavior?\n");
536 foop = return_a_IFoo();
538 printf("### End Test 19, 20\n");
541 printf("\n### setup for Test 21\n");
542 nsCOMPtr<IFoo> fooP;
544 printf("### Test 21: is |QueryInterface| called on assigning in a raw pointer?\n");
545 fooP = do_QueryInterface(new IFoo);
547 printf("### End Test 21\n");
550 printf("\n### setup for Test 22\n");
551 nsCOMPtr<IFoo> fooP;
552 fooP = do_QueryInterface(new IFoo);
554 nsCOMPtr<IFoo> foo2P;
556 printf("### Test 22: is |QueryInterface| _not_ called when assigning in a smart-pointer of the same type?\n");
557 foo2P = fooP;
559 printf("### End Test 22\n");
562 printf("\n### setup for Test 23\n");
563 nsCOMPtr<IBar> barP( do_QueryInterface(new IBar) );
565 printf("### Test 23: is |QueryInterface| called when assigning in a smart-pointer of a different type?\n");
567 nsCOMPtr<IFoo> fooP( do_QueryInterface(barP) );
568 if ( fooP )
569 printf("an IBar* is an IFoo*\n");
571 printf("### End Test 23\n");
575 printf("\n### setup for Test 24\n");
576 nsCOMPtr<IFoo> fooP( do_QueryInterface(new IFoo) );
578 printf("### Test 24: does |forget| avoid an AddRef/Release when assigning to another nsCOMPtr?\n");
579 nsCOMPtr<IFoo> fooP2( fooP.forget() );
581 printf("### End Test 24\n");
584 nsCOMPtr<IFoo> fooP;
586 AnIFooPtrPtrContext( getter_AddRefs(fooP) );
587 AVoidPtrPtrContext( getter_AddRefs(fooP) );
588 AnISupportsPtrPtrContext( getter_AddRefs(fooP) );
593 nsCOMPtr<nsISupports> supportsP;
595 AVoidPtrPtrContext( getter_AddRefs(supportsP) );
596 AnISupportsPtrPtrContext( getter_AddRefs(supportsP) );
600 printf("\n### Test 25: will a static |nsCOMPtr| |Release| before program termination?\n");
601 gFoop = do_QueryInterface(new IFoo);
603 printf("<<main()\n");
604 return 0;