Bug 545892 - Always pass WM_NCPAINT events to the default event procedure. r=bent...
[mozilla-central.git] / xpcom / tests / TestCOMArray.cpp
blob40ca258a758e4b5090331236b97140a426c42db4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:ts=4:et:sw=4:
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is the Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
39 #include "nsCOMArray.h"
41 // {9e70a320-be02-11d1-8031-006008159b5a}
42 #define NS_IFOO_IID \
43 {0x9e70a320, 0xbe02, 0x11d1, \
44 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
46 class IFoo : public nsISupports {
47 public:
49 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
51 NS_IMETHOD_(nsrefcnt) RefCnt() = 0;
52 NS_IMETHOD_(PRInt32) ID() = 0;
55 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
57 class Foo : public IFoo {
58 public:
60 Foo(PRInt32 aID);
61 ~Foo();
63 // nsISupports implementation
64 NS_DECL_ISUPPORTS
66 // IFoo implementation
67 NS_IMETHOD_(nsrefcnt) RefCnt() { return mRefCnt; }
68 NS_IMETHOD_(PRInt32) ID() { return mID; }
70 static PRInt32 gCount;
72 PRInt32 mID;
75 PRInt32 Foo::gCount = 0;
77 Foo::Foo(PRInt32 aID)
79 mID = aID;
80 ++gCount;
83 Foo::~Foo()
85 --gCount;
88 NS_IMPL_ISUPPORTS1(Foo, IFoo)
91 typedef nsCOMArray<IFoo> Array;
94 // {0e70a320-be02-11d1-8031-006008159b5a}
95 #define NS_IBAR_IID \
96 {0x0e70a320, 0xbe02, 0x11d1, \
97 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
99 class IBar : public nsISupports {
100 public:
102 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBAR_IID)
105 NS_DEFINE_STATIC_IID_ACCESSOR(IBar, NS_IBAR_IID)
107 class Bar : public IBar {
108 public:
110 Bar(nsCOMArray<IBar>& aArray, PRInt32 aIndex);
111 ~Bar();
113 // nsISupports implementation
114 NS_DECL_ISUPPORTS
116 static PRInt32 sReleaseCalled;
118 private:
119 nsCOMArray<IBar>& mArray;
120 PRInt32 mIndex;
123 PRInt32 Bar::sReleaseCalled = 0;
125 typedef nsCOMArray<IBar> Array2;
127 Bar::Bar(Array2& aArray, PRInt32 aIndex)
128 : mArray(aArray)
129 , mIndex(aIndex)
133 Bar::~Bar()
135 if (mArray.RemoveObjectAt(mIndex)) {
136 fail("We should never manage to remove the object here");
140 NS_IMPL_ADDREF(Bar)
141 NS_IMPL_QUERY_INTERFACE1(Bar, IBar)
143 NS_IMETHODIMP_(nsrefcnt)
144 Bar::Release(void)
146 ++Bar::sReleaseCalled;
147 NS_PRECONDITION(0 != mRefCnt, "dup release");
148 NS_ASSERT_OWNINGTHREAD(_class);
149 --mRefCnt;
150 NS_LOG_RELEASE(this, mRefCnt, "Bar");
151 if (mRefCnt == 0) {
152 mRefCnt = 1; /* stabilize */
153 delete this;
154 return 0;
156 return mRefCnt;
160 int main(int argc, char **argv)
162 ScopedXPCOM xpcom("nsCOMArrayTests");
163 if (xpcom.failed()) {
164 return 1;
167 int rv = 0;
169 Array arr;
171 for (PRInt32 i = 0; i < 20; ++i) {
172 nsCOMPtr<IFoo> foo = new Foo(i);
173 arr.AppendObject(foo);
176 if (arr.Count() != 20 || Foo::gCount != 20) {
177 fail("nsCOMArray::AppendObject failed");
178 rv = 1;
181 arr.SetCount(10);
183 if (arr.Count() != 10 || Foo::gCount != 10) {
184 fail("nsCOMArray::SetCount shortening of array failed");
185 rv = 1;
188 arr.SetCount(30);
190 if (arr.Count() != 30 || Foo::gCount != 10) {
191 fail("nsCOMArray::SetCount lengthening of array failed");
192 rv = 1;
195 for (PRInt32 i = 0; i < 10; ++i) {
196 if (arr[i] == nsnull) {
197 fail("nsCOMArray elements should be non-null");
198 rv = 1;
199 break;
203 for (PRInt32 i = 10; i < 30; ++i) {
204 if (arr[i] != nsnull) {
205 fail("nsCOMArray elements should be null");
206 rv = 1;
207 break;
211 PRInt32 base;
213 Array2 arr2;
215 IBar *ninthObject;
216 for (PRInt32 i = 0; i < 20; ++i) {
217 nsCOMPtr<IBar> bar = new Bar(arr2, i);
218 if (i == 8) {
219 ninthObject = bar;
221 arr2.AppendObject(bar);
224 base = Bar::sReleaseCalled;
226 arr2.SetCount(10);
227 if (Bar::sReleaseCalled != base + 10) {
228 fail("Release called multiple times for SetCount");
231 arr2.RemoveObjectAt(9);
232 if (Bar::sReleaseCalled != base + 11) {
233 fail("Release called multiple times for RemoveObjectAt");
236 arr2.RemoveObject(ninthObject);
237 if (Bar::sReleaseCalled != base + 12) {
238 fail("Release called multiple times for RemoveObject");
241 arr2.Clear();
242 if (Bar::sReleaseCalled != base + 20) {
243 fail("Release called multiple times for Clear");
247 Bar::sReleaseCalled = 0;
250 Array2 arr2;
252 for (PRInt32 i = 0; i < 20; ++i) {
253 nsCOMPtr<IBar> bar = new Bar(arr2, i);
254 arr2.AppendObject(bar);
257 base = Bar::sReleaseCalled;
259 // Let arr2 be destroyed
261 if (Bar::sReleaseCalled != base + 20) {
262 fail("Release called multiple times for nsCOMArray::~nsCOMArray");
265 return rv;