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 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.
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"
44 #include "nsComponentManagerUtils.h"
45 #include "nsServiceManagerUtils.h"
46 #include "nsThreadUtils.h"
50 #include "mozilla/Monitor.h"
51 using namespace mozilla
;
53 typedef nsresult(*TestFuncPtr
)();
59 nsCOMPtr
<nsIThread
> newThread
;
60 nsresult rv
= NS_NewThread(getter_AddRefs(newThread
));
64 newThread
.swap(mThread
);
71 operator nsIThread
*() const {
75 nsIThread
* operator->() const {
80 nsCOMPtr
<nsIThread
> mThread
;
83 class AutoCreateAndDestroyMonitor
86 AutoCreateAndDestroyMonitor() {
87 mMonitor
= new Monitor("TestTimers::AutoMon");
88 NS_ASSERTION(mMonitor
, "Out of memory!");
91 ~AutoCreateAndDestroyMonitor() {
97 operator Monitor
* () {
105 class TimerCallback
: public nsITimerCallback
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
;
126 nsIThread
** mThreadPtr
;
130 NS_IMPL_THREADSAFE_ISUPPORTS1(TimerCallback
, nsITimerCallback
)
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
);
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(¬ifiedThread
, 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
) {
164 NS_ENSURE_TRUE(notifiedThread
== testThread
, NS_ERROR_FAILURE
);
169 int main(int argc
, char** argv
)
171 ScopedXPCOM
xpcom("TestTimers");
172 NS_ENSURE_FALSE(xpcom
.failed(), 1);
174 static TestFuncPtr testsToRun
[] = {
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);