2 <?xml-stylesheet href=
"chrome://global/skin" type=
"text/css"?>
3 <?xml-stylesheet href=
"chrome://mochikit/content/tests/SimpleTest/test.css"
6 https://bugzilla.mozilla.org/show_bug.cgi?id=343416
8 <window title=
"Mozilla Bug 343416"
9 xmlns:
html=
"http://www.w3.org/1999/xhtml"
10 xmlns=
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
12 <title>Test for Bug
343416</title>
13 <script type=
"application/javascript"
14 src=
"chrome://mochikit/content/MochiKit/packed.js"></script>
15 <script type=
"application/javascript"
16 src=
"chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
18 <body xmlns=
"http://www.w3.org/1999/xhtml">
19 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=343416">Mozilla Bug
343416</a>
21 <div id=
"content" style=
"display: none">
28 <script class=
"testbody" type=
"application/javascript">
31 /** Test for Bug
343416 **/
32 SimpleTest.waitForExplicitFinish();
37 QueryInterface: function _qi(iid)
39 if (iid.equals(Components.interfaces.nsISupports) ||
40 iid.equals(Components.interfaces.nsIObserver))
44 throw Components.results.NS_ERROR_NO_INTERFACE;
46 observe: function _observe(subject, topic, data)
51 var diff = Math.abs(data - newIdleSeconds *
1000);
53 // ok (diff <
5000,
"The idle time should have increased by roughly 6 seconds, " +
54 //
"as that's when we told this listener to fire.");
56 // alert(data +
" " + newIdleSeconds);
58 // Attempt to get to the nsIIdleService
59 var subjectOK = false;
61 var idleService = subject.QueryInterface(nsIIdleService);
66 ok(subjectOK,
"The subject of the notification should be the " +
69 // Attempt to remove ourselves.
70 var removedObserver = false;
72 idleService.removeIdleObserver(this, newIdleSeconds);
73 removedObserver = true;
77 ok(removedObserver,
"We should be able to remove our observer here.");
78 finishedListenerOK = true;
79 if (finishedTimeoutOK)
81 clearTimeout(testBailout);
88 const nsIIdleService = Components.interfaces.nsIIdleService;
89 const nsIISCID =
"@mozilla.org/widget/idleservice;1";
90 var idleService = null;
93 idleService = Components.classes[nsIISCID].getService(nsIIdleService);
98 ok(idleService,
"nsIIdleService should exist and be implemented on all tier 1 platforms.");
101 var gotIdleTime = false;
104 idleTime = idleService.idleTime;
110 ok (gotIdleTime,
"Getting the idle time should not fail " +
111 "in normal circumstances on any tier 1 platform.");
113 // Now we set up a timeout to sanity-test the idleTime after
5 seconds
114 setTimeout(testIdleTime,
5000);
115 var startTimeStamp = Date.now();
117 // Now we add the listener:
118 var newIdleSeconds = Math.floor(idleTime /
1000) +
6;
119 var addedObserver = false;
122 idleService.addIdleObserver(idleObserver, newIdleSeconds);
123 addedObserver = true;
128 ok(addedObserver,
"The nsIIdleService should allow us to add an observer.");
130 addedObserver = false;
133 idleService.addIdleObserver(idleObserver, newIdleSeconds);
134 addedObserver = true;
139 ok(addedObserver,
"The nsIIdleService should allow us to add the same observer again.");
141 var removedObserver = false;
144 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
145 removedObserver = true;
150 ok(removedObserver,
"The nsIIdleService should allow us to remove the observer just once.");
152 function testIdleTime()
154 var gotIdleTime = false
157 var newIdleTime = idleService.idleTime;
162 ok(gotIdleTime,
"Getting the idle time should not fail " +
163 "in normal circumstances on any tier 1 platform.");
164 // Get the time difference, remove the approx.
5 seconds that we've waited,
165 // should be very close to
0 left.
166 var timeDiff = Math.abs((newIdleTime - idleTime) -
167 (Date.now() - startTimeStamp));
169 var timePassed = Date.now() - startTimeStamp;
170 var idleTimeDiff = newIdleTime - idleTime;
171 //
1 second leniency.
172 ok(timeDiff <
1000,
"The idle time should have increased by roughly the " +
173 "amount of time it took for the timeout to fire. " +
174 "You didn't touch the mouse or keyboard during the" +
176 finishedTimeoutOK = true;
179 // make sure we still exit when the listener and/or setTimeout don't fire:
180 var testBailout = setTimeout(finishThisTest,
12000);
181 var finishedTimeoutOK = false, finishedListenerOK = false;
182 function finishThisTest()
184 ok(finishedTimeoutOK,
"We set a timeout and it should have fired by now.");
185 ok(finishedListenerOK,
"We added a listener and it should have been called by now.");
186 if (!finishedListenerOK)
188 var removedListener = false;
191 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
192 removedListener = true;
197 ok(removedListener,
"We added a listener and we should be able to remove it.");