1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
15 * The Original Code is Racing Service Manager Test Code.
17 * The Initial Developer of the Original Code is
18 * Ben Turner <bent.mozilla@gmail.com>.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * 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"
40 #include "nsIFactory.h"
41 #include "mozilla/Module.h"
42 #include "nsXULAppAPI.h"
43 #include "nsIThread.h"
44 #include "nsIComponentRegistrar.h"
46 #include "nsAutoPtr.h"
47 #include "nsThreadUtils.h"
48 #include "nsXPCOMCIDInternal.h"
51 #include "mozilla/Monitor.h"
52 using namespace mozilla
;
55 #define TEST_ASSERTION(_test, _msg) \
56 NS_ASSERTION(_test, _msg);
58 #define TEST_ASSERTION(_test, _msg) \
61 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_test, __FILE__, __LINE__); \
66 /* f93f6bdc-88af-42d7-9d64-1b43c649a3e5 */
67 #define FACTORY_CID1 \
72 { 0x9d, 0x64, 0x1b, 0x43, 0xc6, 0x49, 0xa3, 0xe5 } \
74 NS_DEFINE_CID(kFactoryCID1
, FACTORY_CID1
);
76 /* ef38ad65-6595-49f0-8048-e819f81d15e2 */
77 #define FACTORY_CID2 \
82 { 0x80, 0x48, 0xe8, 0x19, 0xf8, 0x1d, 0x15, 0xe2 } \
84 NS_DEFINE_CID(kFactoryCID2
, FACTORY_CID2
);
86 #define FACTORY_CONTRACTID \
87 "TestRacingThreadManager/factory;1"
89 PRInt32 gComponent1Count
= 0;
90 PRInt32 gComponent2Count
= 0;
92 Monitor
* gMonitor
= nsnull
;
94 PRBool gCreateInstanceCalled
= PR_FALSE
;
95 PRBool gMainThreadWaiting
= PR_FALSE
;
97 class AutoCreateAndDestroyMonitor
100 AutoCreateAndDestroyMonitor(Monitor
** aMonitorPtr
)
101 : mMonitorPtr(aMonitorPtr
) {
103 new Monitor("TestRacingServiceManager::AutoMon");
104 TEST_ASSERTION(*aMonitorPtr
, "Out of memory!");
107 ~AutoCreateAndDestroyMonitor() {
110 *mMonitorPtr
= nsnull
;
115 Monitor
** mMonitorPtr
;
118 class Factory
: public nsIFactory
123 Factory() : mFirstComponentCreated(PR_FALSE
) { }
125 NS_IMETHOD
CreateInstance(nsISupports
* aDelegate
,
129 NS_IMETHOD
LockFactory(PRBool aLock
) {
133 PRBool mFirstComponentCreated
;
136 NS_IMPL_THREADSAFE_ISUPPORTS1(Factory
, nsIFactory
)
138 class Component1
: public nsISupports
144 // This is the real test - make sure that only one instance is ever created.
145 PRInt32 count
= PR_AtomicIncrement(&gComponent1Count
);
146 TEST_ASSERTION(count
== 1, "Too many components created!");
150 NS_IMPL_THREADSAFE_ADDREF(Component1
)
151 NS_IMPL_THREADSAFE_RELEASE(Component1
)
153 NS_INTERFACE_MAP_BEGIN(Component1
)
154 NS_INTERFACE_MAP_ENTRY(Component1
)
155 NS_INTERFACE_MAP_ENTRY(nsISupports
)
158 class Component2
: public nsISupports
164 // This is the real test - make sure that only one instance is ever created.
165 PRInt32 count
= PR_AtomicIncrement(&gComponent2Count
);
166 TEST_ASSERTION(count
== 1, "Too many components created!");
170 NS_IMPL_THREADSAFE_ADDREF(Component2
)
171 NS_IMPL_THREADSAFE_RELEASE(Component2
)
173 NS_INTERFACE_MAP_BEGIN(Component2
)
174 NS_INTERFACE_MAP_ENTRY(Component2
)
175 NS_INTERFACE_MAP_ENTRY(nsISupports
)
179 Factory::CreateInstance(nsISupports
* aDelegate
,
183 // Make sure that the second thread beat the main thread to the getService
185 TEST_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
188 MonitorAutoEnter
mon(*gMonitor
);
190 gCreateInstanceCalled
= PR_TRUE
;
193 mon
.Wait(PR_MillisecondsToInterval(3000));
196 NS_ENSURE_FALSE(aDelegate
, NS_ERROR_NO_AGGREGATION
);
197 NS_ENSURE_ARG_POINTER(aResult
);
199 nsCOMPtr
<nsISupports
> instance
;
201 if (!mFirstComponentCreated
) {
202 instance
= new Component1();
205 instance
= new Component2();
207 NS_ENSURE_TRUE(instance
, NS_ERROR_OUT_OF_MEMORY
);
209 nsresult rv
= instance
->QueryInterface(aIID
, aResult
);
210 NS_ENSURE_SUCCESS(rv
, rv
);
215 class Runnable
: public nsRunnable
220 Runnable() : mFirstRunnableDone(PR_FALSE
) { }
222 PRBool mFirstRunnableDone
;
229 MonitorAutoEnter
mon(*gMonitor
);
231 while (!gMainThreadWaiting
) {
237 nsCOMPtr
<nsISupports
> component
;
239 if (!mFirstRunnableDone
) {
240 component
= do_GetService(kFactoryCID1
, &rv
);
243 component
= do_GetService(FACTORY_CONTRACTID
, &rv
);
245 TEST_ASSERTION(NS_SUCCEEDED(rv
), "GetService failed!");
250 static Factory
* gFactory
;
252 static already_AddRefed
<nsIFactory
>
253 CreateFactory(const mozilla::Module
& module
, const mozilla::Module::CIDEntry
& entry
)
256 gFactory
= new Factory();
263 static const mozilla::Module::CIDEntry kLocalCIDs
[] = {
264 { &kFactoryCID1
, false, CreateFactory
, NULL
},
265 { &kFactoryCID2
, false, CreateFactory
, NULL
},
269 static const mozilla::Module::ContractIDEntry kLocalContracts
[] = {
270 { FACTORY_CONTRACTID
, &kFactoryCID2
},
274 static const mozilla::Module kLocalModule
= {
275 mozilla::Module::kVersion
,
280 int main(int argc
, char** argv
)
283 XRE_AddStaticComponent(&kLocalModule
);
285 ScopedXPCOM
xpcom("RacingServiceManager");
286 NS_ENSURE_FALSE(xpcom
.failed(), 1);
288 AutoCreateAndDestroyMonitor
mon(&gMonitor
);
290 nsRefPtr
<Runnable
> runnable
= new Runnable();
291 NS_ENSURE_TRUE(runnable
, 1);
293 // Run the classID test
294 nsCOMPtr
<nsIThread
> newThread
;
295 rv
= NS_NewThread(getter_AddRefs(newThread
), runnable
);
296 NS_ENSURE_SUCCESS(rv
, 1);
299 MonitorAutoEnter
mon(*gMonitor
);
301 gMainThreadWaiting
= PR_TRUE
;
304 while (!gCreateInstanceCalled
) {
309 nsCOMPtr
<nsISupports
> component(do_GetService(kFactoryCID1
, &rv
));
310 NS_ENSURE_SUCCESS(rv
, 1);
312 // Reset for the contractID test
313 gMainThreadWaiting
= gCreateInstanceCalled
= PR_FALSE
;
314 gFactory
->mFirstComponentCreated
= runnable
->mFirstRunnableDone
= PR_TRUE
;
317 rv
= newThread
->Dispatch(runnable
, NS_DISPATCH_NORMAL
);
318 NS_ENSURE_SUCCESS(rv
, 1);
321 MonitorAutoEnter
mon(*gMonitor
);
323 gMainThreadWaiting
= PR_TRUE
;
326 while (!gCreateInstanceCalled
) {
331 component
= do_GetService(FACTORY_CONTRACTID
, &rv
);
332 NS_ENSURE_SUCCESS(rv
, 1);
334 NS_RELEASE(gFactory
);