Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / widget / tests / test_bug343416.xhtml
blobbf4dbbb9b4cc369f5a135d184d4edbd4e9094ff4
1 <?xml version="1.0"?>
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
4 type="text/css"?>
5 <!--
6 https://bugzilla.mozilla.org/show_bug.cgi?id=343416
7 -->
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>
15 <p id="display"></p>
16 <div id="content" style="display: none">
18 </div>
19 <pre id="test">
20 </pre>
21 </body>
23 <script class="testbody" type="application/javascript">
24 <![CDATA[
26 /** Test for Bug 343416 */
27 SimpleTest.waitForExplicitFinish();
29 // Observer:
30 var idleObserver =
32 QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
33 observe: function _observe(subject, topic, data)
35 if (topic != "idle")
36 return;
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.");
41 // if (diff >= 5000)
42 // alert(data + " " + newIdleSeconds);
44 // Attempt to get to the nsIUserIdleService
45 var subjectOK = false;
46 try {
47 var idleService = subject.QueryInterface(nsIUserIdleService);
48 subjectOK = true;
50 catch (ex)
52 ok(subjectOK, "The subject of the notification should be the " +
53 "nsIUserIdleService.");
55 // Attempt to remove ourselves.
56 var removedObserver = false;
57 try {
58 idleService.removeIdleObserver(this, newIdleSeconds);
59 removedObserver = true;
61 catch (ex)
63 ok(removedObserver, "We should be able to remove our observer here.");
64 finishedListenerOK = true;
65 if (finishedTimeoutOK)
67 clearTimeout(testBailout);
68 finishThisTest();
74 const nsIUserIdleService = Ci.nsIUserIdleService;
75 const nsIISCID = "@mozilla.org/widget/useridleservice;1";
76 var idleService = null;
77 try
79 idleService = Cc[nsIISCID].getService(nsIUserIdleService);
81 catch (ex)
84 ok(idleService, "nsIUserIdleService should exist and be implemented on all tier 1 platforms.");
86 var idleTime = null;
87 var gotIdleTime = false;
88 try
90 idleTime = idleService.idleTime;
91 gotIdleTime = true;
93 catch (ex)
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;
111 catch (ex)
114 ok(addedObserver, "The nsIUserIdleService should allow us to add an observer.");
116 addedObserver = false;
119 idleService.addIdleObserver(idleObserver, newIdleSeconds);
120 addedObserver = true;
122 catch (ex)
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;
133 catch (ex)
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;
145 gotIdleTime = true
147 catch (ex)
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 " +
160 "test did you?");
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;
179 catch (ex)
182 ok(removedListener, "We added a listener and we should be able to remove it.");
184 // Done:
185 SimpleTest.finish();
189 </script>
191 </window>