Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 type="application/javascript"
12 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
14 <body xmlns="http://www.w3.org/1999/xhtml">
15 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=343416">Mozilla Bug 343416</a>
16 <p id="display"></p>
17 <div id="content" style="display: none">
19 </div>
20 <pre id="test">
21 </pre>
22 </body>
24 <script class="testbody" type="application/javascript">
25 <![CDATA[
27 /** Test for Bug 343416 **/
28 SimpleTest.waitForExplicitFinish();
30 // Observer:
31 var idleObserver =
32 {
33 QueryInterface: function _qi(iid)
34 {
35 if (iid.equals(Components.interfaces.nsISupports) ||
36 iid.equals(Components.interfaces.nsIObserver))
37 {
38 return this;
39 }
40 throw Components.results.NS_ERROR_NO_INTERFACE;
41 },
42 observe: function _observe(subject, topic, data)
43 {
44 if (topic != "idle")
45 return;
47 var diff = Math.abs(data - newIdleSeconds * 1000);
49 // ok (diff < 5000, "The idle time should have increased by roughly 6 seconds, " +
50 // "as that's when we told this listener to fire.");
51 // if (diff >= 5000)
52 // alert(data + " " + newIdleSeconds);
54 // Attempt to get to the nsIIdleService
55 var subjectOK = false;
56 try {
57 var idleService = subject.QueryInterface(nsIIdleService);
58 subjectOK = true;
59 }
60 catch (ex)
61 {}
62 ok(subjectOK, "The subject of the notification should be the " +
63 "nsIIdleService.");
65 // Attempt to remove ourselves.
66 var removedObserver = false;
67 try {
68 idleService.removeIdleObserver(this, newIdleSeconds);
69 removedObserver = true;
70 }
71 catch (ex)
72 {}
73 ok(removedObserver, "We should be able to remove our observer here.");
74 finishedListenerOK = true;
75 if (finishedTimeoutOK)
76 {
77 clearTimeout(testBailout);
78 finishThisTest();
79 }
80 }
81 };
84 const nsIIdleService = Components.interfaces.nsIIdleService;
85 const nsIISCID = "@mozilla.org/widget/idleservice;1";
86 var idleService = null;
87 try
88 {
89 idleService = Components.classes[nsIISCID].getService(nsIIdleService);
90 }
91 catch (ex)
92 {}
94 ok(idleService, "nsIIdleService should exist and be implemented on all tier 1 platforms.");
96 var idleTime = null;
97 var gotIdleTime = false;
98 try
99 {
100 idleTime = idleService.idleTime;
101 gotIdleTime = true;
102 }
103 catch (ex)
104 {}
106 ok (gotIdleTime, "Getting the idle time should not fail " +
107 "in normal circumstances on any tier 1 platform.");
109 // Now we set up a timeout to sanity-test the idleTime after 5 seconds
110 setTimeout(testIdleTime, 5000);
111 var startTimeStamp = Date.now();
113 // Now we add the listener:
114 var newIdleSeconds = Math.floor(idleTime / 1000) + 6;
115 var addedObserver = false;
116 try
117 {
118 idleService.addIdleObserver(idleObserver, newIdleSeconds);
119 addedObserver = true;
120 }
121 catch (ex)
122 {}
124 ok(addedObserver, "The nsIIdleService should allow us to add an observer.");
126 addedObserver = false;
127 try
128 {
129 idleService.addIdleObserver(idleObserver, newIdleSeconds);
130 addedObserver = true;
131 }
132 catch (ex)
133 {}
135 ok(addedObserver, "The nsIIdleService should allow us to add the same observer again.");
137 var removedObserver = false;
138 try
139 {
140 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
141 removedObserver = true;
142 }
143 catch (ex)
144 {}
146 ok(removedObserver, "The nsIIdleService should allow us to remove the observer just once.");
148 function testIdleTime()
149 {
150 var gotIdleTime = false
151 try
152 {
153 var newIdleTime = idleService.idleTime;
154 gotIdleTime = true
155 }
156 catch (ex)
157 {}
158 ok(gotIdleTime, "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));
165 var timePassed = Date.now() - startTimeStamp;
166 var idleTimeDiff = newIdleTime - idleTime;
167 // 1.5 second leniency.
168 ok(timeDiff < 1500, "The idle time should have increased by roughly the " +
169 "amount of time it took for the timeout to fire. " +
170 "You didn't touch the mouse or keyboard during the " +
171 "test did you?");
172 finishedTimeoutOK = true;
173 }
175 // make sure we still exit when the listener and/or setTimeout don't fire:
176 var testBailout = setTimeout(finishThisTest, 12000);
177 var finishedTimeoutOK = false, finishedListenerOK = false;
178 function finishThisTest()
179 {
180 ok(finishedTimeoutOK, "We set a timeout and it should have fired by now.");
181 ok(finishedListenerOK, "We added a listener and it should have been called by now.");
182 if (!finishedListenerOK)
183 {
184 var removedListener = false;
185 try
186 {
187 idleService.removeIdleObserver(idleObserver, newIdleSeconds);
188 removedListener = true;
189 }
190 catch (ex)
191 {}
193 ok(removedListener, "We added a listener and we should be able to remove it.");
194 }
195 // Done:
196 SimpleTest.finish();
197 }
199 ]]>
200 </script>
202 </window>