Bug 462294 - Add "View Video" to context menu for <video> elements. r=gavin, ui...
[wine-gecko.git] / xpcom / tests / TestTimers.cpp
blob55a050e44af460062d3729589dd29e427cc37f4b
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 "nsAutoLock.h"
44 #include "nsCOMPtr.h"
45 #include "nsComponentManagerUtils.h"
46 #include "nsServiceManagerUtils.h"
47 #include "nsThreadUtils.h"
48 #include "prinrval.h"
49 #include "prmon.h"
51 typedef nsresult(*TestFuncPtr)();
53 class AutoTestThread
55 public:
56 AutoTestThread() {
57 nsCOMPtr<nsIThread> newThread;
58 nsresult rv = NS_NewThread(getter_AddRefs(newThread));
59 NS_ENSURE_SUCCESS(rv,);
61 newThread.swap(mThread);
64 ~AutoTestThread() {
65 mThread->Shutdown();
68 operator nsIThread*() const {
69 return mThread;
72 nsIThread* operator->() const {
73 return mThread;
76 private:
77 nsCOMPtr<nsIThread> mThread;
80 class AutoCreateAndDestroyMonitor
82 public:
83 AutoCreateAndDestroyMonitor() {
84 mMonitor = nsAutoMonitor::NewMonitor("TestTimers::AutoMon");
85 NS_ASSERTION(mMonitor, "Out of memory!");
88 ~AutoCreateAndDestroyMonitor() {
89 if (mMonitor) {
90 nsAutoMonitor::DestroyMonitor(mMonitor);
94 operator PRMonitor*() {
95 return mMonitor;
98 private:
99 PRMonitor* mMonitor;
102 class TimerCallback : public nsITimerCallback
104 public:
105 NS_DECL_ISUPPORTS
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;
118 mon.Notify();
120 return NS_OK;
122 private:
123 nsIThread** mThreadPtr;
124 PRMonitor* mMonitor;
127 NS_IMPL_THREADSAFE_ISUPPORTS1(TimerCallback, nsITimerCallback)
129 nsresult
130 TestTargetedTimers()
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);
138 nsresult rv;
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(&notifiedThread, 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) {
159 mon.Wait();
161 NS_ENSURE_TRUE(notifiedThread == testThread, NS_ERROR_FAILURE);
163 return NS_OK;
166 int main(int argc, char** argv)
168 ScopedXPCOM xpcom("TestTimers");
169 NS_ENSURE_FALSE(xpcom.failed(), 1);
171 static TestFuncPtr testsToRun[] = {
172 TestTargetedTimers
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);
181 return 0;