Bug 596580: Fix mozJSSubScriptLoader's version finding. (r=brendan)
[mozilla-central.git] / xpcom / tests / TestTimers.cpp
blob10a4b7b91d19b10014789844c7a547f788a7a740
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
13 * License.
15 * The Original Code is Timer 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.
22 * Contributor(s):
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 "nsIThread.h"
41 #include "nsITimer.h"
43 #include "nsCOMPtr.h"
44 #include "nsComponentManagerUtils.h"
45 #include "nsServiceManagerUtils.h"
46 #include "nsThreadUtils.h"
47 #include "prinrval.h"
48 #include "prmon.h"
50 #include "mozilla/Monitor.h"
51 using namespace mozilla;
53 typedef nsresult(*TestFuncPtr)();
55 class AutoTestThread
57 public:
58 AutoTestThread() {
59 nsCOMPtr<nsIThread> newThread;
60 nsresult rv = NS_NewThread(getter_AddRefs(newThread));
61 if (NS_FAILED(rv))
62 return;
64 newThread.swap(mThread);
67 ~AutoTestThread() {
68 mThread->Shutdown();
71 operator nsIThread*() const {
72 return mThread;
75 nsIThread* operator->() const {
76 return mThread;
79 private:
80 nsCOMPtr<nsIThread> mThread;
83 class AutoCreateAndDestroyMonitor
85 public:
86 AutoCreateAndDestroyMonitor() {
87 mMonitor = new Monitor("TestTimers::AutoMon");
88 NS_ASSERTION(mMonitor, "Out of memory!");
91 ~AutoCreateAndDestroyMonitor() {
92 if (mMonitor) {
93 delete mMonitor;
97 operator Monitor* () {
98 return mMonitor;
101 private:
102 Monitor* mMonitor;
105 class TimerCallback : public nsITimerCallback
107 public:
108 NS_DECL_ISUPPORTS
110 TimerCallback(nsIThread** aThreadPtr, Monitor* aMonitor)
111 : mThreadPtr(aThreadPtr), mMonitor(aMonitor) { }
113 NS_IMETHOD Notify(nsITimer* aTimer) {
114 nsCOMPtr<nsIThread> current(do_GetCurrentThread());
116 MonitorAutoEnter mon(*mMonitor);
118 NS_ASSERTION(!*mThreadPtr, "Timer called back more than once!");
119 *mThreadPtr = current;
121 mon.Notify();
123 return NS_OK;
125 private:
126 nsIThread** mThreadPtr;
127 Monitor* mMonitor;
130 NS_IMPL_THREADSAFE_ISUPPORTS1(TimerCallback, nsITimerCallback)
132 nsresult
133 TestTargetedTimers()
135 AutoCreateAndDestroyMonitor newMon;
136 NS_ENSURE_TRUE(newMon, NS_ERROR_OUT_OF_MEMORY);
138 AutoTestThread testThread;
139 NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
141 nsresult rv;
142 nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
143 NS_ENSURE_SUCCESS(rv, rv);
145 nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
147 rv = timer->SetTarget(target);
148 NS_ENSURE_SUCCESS(rv, rv);
150 nsIThread* notifiedThread = nsnull;
152 nsCOMPtr<nsITimerCallback> callback =
153 new TimerCallback(&notifiedThread, newMon);
154 NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
156 rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(2000),
157 nsITimer::TYPE_ONE_SHOT);
158 NS_ENSURE_SUCCESS(rv, rv);
160 MonitorAutoEnter mon(*newMon);
161 while (!notifiedThread) {
162 mon.Wait();
164 NS_ENSURE_TRUE(notifiedThread == testThread, NS_ERROR_FAILURE);
166 return NS_OK;
169 int main(int argc, char** argv)
171 ScopedXPCOM xpcom("TestTimers");
172 NS_ENSURE_FALSE(xpcom.failed(), 1);
174 static TestFuncPtr testsToRun[] = {
175 TestTargetedTimers
177 static PRUint32 testCount = sizeof(testsToRun) / sizeof(testsToRun[0]);
179 for (PRUint32 i = 0; i < testCount; i++) {
180 nsresult rv = testsToRun[i]();
181 NS_ENSURE_SUCCESS(rv, 1);
184 return 0;