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.");
103 idleTime = idleService.idleTime;
108 ok (idleTime,
"Getting the idle time should not fail " +
109 "in normal circumstances on any tier 1 platform.");
111 // Now we set up a timeout to sanity-test the idleTime after
5 seconds
112 setTimeout(testIdleTime,
5000);
113 var startTimeStamp = Date.now();
115 // Now we add the listener:
116 var newIdleSeconds = Math.floor(idleTime /
1000) +
6;
117 var addedObserver = false;
120 idleService.addIdleObserver(idleObserver, newIdleSeconds);
121 addedObserver = true;
126 ok(addedObserver,
"The nsIIdleService should allow us to add an observer.");
128 addedObserver = false;
131 idleService.addIdleObserver(idleObserver, newIdleSeconds);
132 addedObserver = true;
137 ok(addedObserver,
"The nsIIdleService should allow us to add the same observer again.");
139 var removedObserver = false;
142 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
143 removedObserver = true;
148 ok(removedObserver,
"The nsIIdleService should allow us to remove the observer just once.");
150 function testIdleTime()
154 var newIdleTime = idleService.idleTime;
158 ok(newIdleTime,
"Getting the idle time should not fail " +
159 "in normal circumstances on any tier 1 platform.");
160 // Get the time difference, remove the approx.
5 seconds that we've waited,
161 // should be very close to
0 left.
162 var timeDiff = Math.abs((newIdleTime - idleTime) -
163 (Date.now() - startTimeStamp));
164 //
0.5 second leniency.
165 ok(timeDiff <
500,
"The idle time should have increased by roughly the " +
166 "amount of time it took for the timeout to fire.");
167 finishedTimeoutOK = true;
170 // make sure we still exit when the listener and/or setTimeout don't fire:
171 var testBailout = setTimeout(finishThisTest,
12000);
172 var finishedTimeoutOK = false, finishedListenerOK = false;
173 function finishThisTest()
175 ok(finishedTimeoutOK,
"We set a timeout and it should have fired by now.");
176 ok(finishedListenerOK,
"We added a listener and it should have been called by now.");
177 if (!finishedListenerOK)
179 var removedListener = false;
182 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
183 removedListener = true;
188 ok(removedListener,
"We added a listener and we should be able to remove it.");