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"
43 #include "nsAutoLock.h"
45 #include "nsComponentManagerUtils.h"
46 #include "nsServiceManagerUtils.h"
47 #include "nsThreadUtils.h"
51 typedef nsresult(*TestFuncPtr
)();
57 nsCOMPtr
<nsIThread
> newThread
;
58 nsresult rv
= NS_NewThread(getter_AddRefs(newThread
));
59 NS_ENSURE_SUCCESS(rv
,);
61 newThread
.swap(mThread
);
68 operator nsIThread
*() const {
72 nsIThread
* operator->() const {
77 nsCOMPtr
<nsIThread
> mThread
;
80 class AutoCreateAndDestroyMonitor
83 AutoCreateAndDestroyMonitor() {
84 mMonitor
= nsAutoMonitor::NewMonitor("TestTimers::AutoMon");
85 NS_ASSERTION(mMonitor
, "Out of memory!");
88 ~AutoCreateAndDestroyMonitor() {
90 nsAutoMonitor::DestroyMonitor(mMonitor
);
94 operator PRMonitor
*() {
102 class TimerCallback
: public nsITimerCallback
107 TimerCallback(nsIThread
** aThreadPtr
, PRMonitor
* aMonitor
)
108 : mThreadPtr(aThreadPtr
), mMonitor(aMonitor
) { }
110 NS_IMETHOD
Notify(nsITimer
* aTimer
) {
111 nsCOMPtr
<nsIThread
> current(do_GetCurrentThread());
113 nsAutoMonitor
mon(mMonitor
);
115 NS_ASSERTION(!*mThreadPtr
, "Timer called back more than once!");
116 *mThreadPtr
= current
;
123 nsIThread
** mThreadPtr
;
127 NS_IMPL_THREADSAFE_ISUPPORTS1(TimerCallback
, nsITimerCallback
)
132 AutoCreateAndDestroyMonitor newMon
;
133 NS_ENSURE_TRUE(newMon
, NS_ERROR_OUT_OF_MEMORY
);
135 AutoTestThread testThread
;
136 NS_ENSURE_TRUE(testThread
, NS_ERROR_OUT_OF_MEMORY
);
139 nsCOMPtr
<nsITimer
> timer
= do_CreateInstance(NS_TIMER_CONTRACTID
, &rv
);
140 NS_ENSURE_SUCCESS(rv
, rv
);
142 nsIEventTarget
* target
= static_cast<nsIEventTarget
*>(testThread
);
144 rv
= timer
->SetTarget(target
);
145 NS_ENSURE_SUCCESS(rv
, rv
);
147 nsIThread
* notifiedThread
= nsnull
;
149 nsCOMPtr
<nsITimerCallback
> callback
=
150 new TimerCallback(¬ifiedThread
, newMon
);
151 NS_ENSURE_TRUE(callback
, NS_ERROR_OUT_OF_MEMORY
);
153 rv
= timer
->InitWithCallback(callback
, PR_MillisecondsToInterval(2000),
154 nsITimer::TYPE_ONE_SHOT
);
155 NS_ENSURE_SUCCESS(rv
, rv
);
157 nsAutoMonitor
mon(newMon
);
158 while (!notifiedThread
) {
161 NS_ENSURE_TRUE(notifiedThread
== testThread
, NS_ERROR_FAILURE
);
166 int main(int argc
, char** argv
)
168 ScopedXPCOM
xpcom("TestTimers");
169 NS_ENSURE_FALSE(xpcom
.failed(), 1);
171 static TestFuncPtr testsToRun
[] = {
174 static PRUint32 testCount
= sizeof(testsToRun
) / sizeof(testsToRun
[0]);
176 for (PRUint32 i
= 0; i
< testCount
; i
++) {
177 nsresult rv
= testsToRun
[i
]();
178 NS_ENSURE_SUCCESS(rv
, 1);