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=
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
11 <script src=
"chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
13 <body xmlns=
"http://www.w3.org/1999/xhtml">
14 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=343416">Mozilla Bug
343416</a>
16 <div id=
"content" style=
"display: none">
23 <script class=
"testbody" type=
"application/javascript">
26 /** Test for Bug
343416 */
27 SimpleTest.waitForExplicitFinish();
32 QueryInterface: ChromeUtils.generateQI([
"nsIObserver"]),
33 observe: function _observe(subject, topic)
38 // var diff = Math.abs(data - newIdleSeconds *
1000);
39 // ok (diff <
5000,
"The idle time should have increased by roughly 6 seconds, " +
40 //
"as that's when we told this listener to fire.");
42 // alert(data +
" " + newIdleSeconds);
44 // Attempt to get to the nsIUserIdleService
45 var subjectOK = false;
47 var idleService = subject.QueryInterface(nsIUserIdleService);
52 ok(subjectOK,
"The subject of the notification should be the " +
53 "nsIUserIdleService.");
55 // Attempt to remove ourselves.
56 var removedObserver = false;
58 idleService.removeIdleObserver(this, newIdleSeconds);
59 removedObserver = true;
63 ok(removedObserver,
"We should be able to remove our observer here.");
64 finishedListenerOK = true;
65 if (finishedTimeoutOK)
67 clearTimeout(testBailout);
74 const nsIUserIdleService = Ci.nsIUserIdleService;
75 const nsIISCID =
"@mozilla.org/widget/useridleservice;1";
76 var idleService = null;
79 idleService = Cc[nsIISCID].getService(nsIUserIdleService);
84 ok(idleService,
"nsIUserIdleService should exist and be implemented on all tier 1 platforms.");
87 var gotIdleTime = false;
90 idleTime = idleService.idleTime;
96 ok (gotIdleTime,
"Getting the idle time should not fail " +
97 "in normal circumstances on any tier 1 platform.");
99 // Now we set up a timeout to sanity-test the idleTime after
5 seconds
100 setTimeout(testIdleTime,
5000);
101 var startTimeStamp = Date.now();
103 // Now we add the listener:
104 var newIdleSeconds = Math.floor(idleTime /
1000) +
6;
105 var addedObserver = false;
108 idleService.addIdleObserver(idleObserver, newIdleSeconds);
109 addedObserver = true;
114 ok(addedObserver,
"The nsIUserIdleService should allow us to add an observer.");
116 addedObserver = false;
119 idleService.addIdleObserver(idleObserver, newIdleSeconds);
120 addedObserver = true;
125 ok(addedObserver,
"The nsIUserIdleService should allow us to add the same observer again.");
127 var removedObserver = false;
130 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
131 removedObserver = true;
136 ok(removedObserver,
"The nsIUserIdleService should allow us to remove the observer just once.");
138 function testIdleTime()
140 /* eslint-disable-next-line no-shadow */
141 var gotIdleTime = false
144 var newIdleTime = idleService.idleTime;
149 ok(gotIdleTime,
"Getting the idle time should not fail " +
150 "in normal circumstances on any tier 1 platform.");
151 // Get the time difference, remove the approx.
5 seconds that we've waited,
152 // should be very close to
0 left.
153 var timeDiff = Math.abs((newIdleTime - idleTime) -
154 (Date.now() - startTimeStamp));
156 //
1.5 second leniency.
157 ok(timeDiff <
1500,
"The idle time should have increased by roughly the " +
158 "amount of time it took for the timeout to fire. " +
159 "You didn't touch the mouse or keyboard during the " +
161 finishedTimeoutOK = true;
164 // make sure we still exit when the listener and/or setTimeout don't fire:
165 var testBailout = setTimeout(finishThisTest,
12000);
166 var finishedTimeoutOK = false, finishedListenerOK = false;
167 function finishThisTest()
169 ok(finishedTimeoutOK,
"We set a timeout and it should have fired by now.");
170 ok(finishedListenerOK,
"We added a listener and it should have been called by now.");
171 if (!finishedListenerOK)
173 var removedListener = false;
176 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
177 removedListener = true;
182 ok(removedListener,
"We added a listener and we should be able to remove it.");