1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/tests/test_bug343416.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,202 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> 1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" 1.7 + type="text/css"?> 1.8 +<!-- 1.9 +https://bugzilla.mozilla.org/show_bug.cgi?id=343416 1.10 +--> 1.11 +<window title="Mozilla Bug 343416" 1.12 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.13 + 1.14 + <script type="application/javascript" 1.15 + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" /> 1.16 + 1.17 +<body xmlns="http://www.w3.org/1999/xhtml"> 1.18 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=343416">Mozilla Bug 343416</a> 1.19 +<p id="display"></p> 1.20 +<div id="content" style="display: none"> 1.21 + 1.22 +</div> 1.23 +<pre id="test"> 1.24 +</pre> 1.25 +</body> 1.26 + 1.27 +<script class="testbody" type="application/javascript"> 1.28 +<![CDATA[ 1.29 + 1.30 +/** Test for Bug 343416 **/ 1.31 +SimpleTest.waitForExplicitFinish(); 1.32 + 1.33 +// Observer: 1.34 +var idleObserver = 1.35 +{ 1.36 + QueryInterface: function _qi(iid) 1.37 + { 1.38 + if (iid.equals(Components.interfaces.nsISupports) || 1.39 + iid.equals(Components.interfaces.nsIObserver)) 1.40 + { 1.41 + return this; 1.42 + } 1.43 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.44 + }, 1.45 + observe: function _observe(subject, topic, data) 1.46 + { 1.47 + if (topic != "idle") 1.48 + return; 1.49 + 1.50 + var diff = Math.abs(data - newIdleSeconds * 1000); 1.51 + 1.52 +// ok (diff < 5000, "The idle time should have increased by roughly 6 seconds, " + 1.53 +// "as that's when we told this listener to fire."); 1.54 +// if (diff >= 5000) 1.55 +// alert(data + " " + newIdleSeconds); 1.56 + 1.57 + // Attempt to get to the nsIIdleService 1.58 + var subjectOK = false; 1.59 + try { 1.60 + var idleService = subject.QueryInterface(nsIIdleService); 1.61 + subjectOK = true; 1.62 + } 1.63 + catch (ex) 1.64 + {} 1.65 + ok(subjectOK, "The subject of the notification should be the " + 1.66 + "nsIIdleService."); 1.67 + 1.68 + // Attempt to remove ourselves. 1.69 + var removedObserver = false; 1.70 + try { 1.71 + idleService.removeIdleObserver(this, newIdleSeconds); 1.72 + removedObserver = true; 1.73 + } 1.74 + catch (ex) 1.75 + {} 1.76 + ok(removedObserver, "We should be able to remove our observer here."); 1.77 + finishedListenerOK = true; 1.78 + if (finishedTimeoutOK) 1.79 + { 1.80 + clearTimeout(testBailout); 1.81 + finishThisTest(); 1.82 + } 1.83 + } 1.84 +}; 1.85 + 1.86 + 1.87 +const nsIIdleService = Components.interfaces.nsIIdleService; 1.88 +const nsIISCID = "@mozilla.org/widget/idleservice;1"; 1.89 +var idleService = null; 1.90 +try 1.91 +{ 1.92 + idleService = Components.classes[nsIISCID].getService(nsIIdleService); 1.93 +} 1.94 +catch (ex) 1.95 +{} 1.96 + 1.97 +ok(idleService, "nsIIdleService should exist and be implemented on all tier 1 platforms."); 1.98 + 1.99 +var idleTime = null; 1.100 +var gotIdleTime = false; 1.101 +try 1.102 +{ 1.103 + idleTime = idleService.idleTime; 1.104 + gotIdleTime = true; 1.105 +} 1.106 +catch (ex) 1.107 +{} 1.108 + 1.109 +ok (gotIdleTime, "Getting the idle time should not fail " + 1.110 + "in normal circumstances on any tier 1 platform."); 1.111 + 1.112 +// Now we set up a timeout to sanity-test the idleTime after 5 seconds 1.113 +setTimeout(testIdleTime, 5000); 1.114 +var startTimeStamp = Date.now(); 1.115 + 1.116 +// Now we add the listener: 1.117 +var newIdleSeconds = Math.floor(idleTime / 1000) + 6; 1.118 +var addedObserver = false; 1.119 +try 1.120 +{ 1.121 + idleService.addIdleObserver(idleObserver, newIdleSeconds); 1.122 + addedObserver = true; 1.123 +} 1.124 +catch (ex) 1.125 +{} 1.126 + 1.127 +ok(addedObserver, "The nsIIdleService should allow us to add an observer."); 1.128 + 1.129 +addedObserver = false; 1.130 +try 1.131 +{ 1.132 + idleService.addIdleObserver(idleObserver, newIdleSeconds); 1.133 + addedObserver = true; 1.134 +} 1.135 +catch (ex) 1.136 +{} 1.137 + 1.138 +ok(addedObserver, "The nsIIdleService should allow us to add the same observer again."); 1.139 + 1.140 +var removedObserver = false; 1.141 +try 1.142 +{ 1.143 + idleService.removeIdleObserver(idleObserver, newIdleSeconds); 1.144 + removedObserver = true; 1.145 +} 1.146 +catch (ex) 1.147 +{} 1.148 + 1.149 +ok(removedObserver, "The nsIIdleService should allow us to remove the observer just once."); 1.150 + 1.151 +function testIdleTime() 1.152 +{ 1.153 + var gotIdleTime = false 1.154 + try 1.155 + { 1.156 + var newIdleTime = idleService.idleTime; 1.157 + gotIdleTime = true 1.158 + } 1.159 + catch (ex) 1.160 + {} 1.161 + ok(gotIdleTime, "Getting the idle time should not fail " + 1.162 + "in normal circumstances on any tier 1 platform."); 1.163 + // Get the time difference, remove the approx. 5 seconds that we've waited, 1.164 + // should be very close to 0 left. 1.165 + var timeDiff = Math.abs((newIdleTime - idleTime) - 1.166 + (Date.now() - startTimeStamp)); 1.167 + 1.168 + var timePassed = Date.now() - startTimeStamp; 1.169 + var idleTimeDiff = newIdleTime - idleTime; 1.170 + // 1.5 second leniency. 1.171 + ok(timeDiff < 1500, "The idle time should have increased by roughly the " + 1.172 + "amount of time it took for the timeout to fire. " + 1.173 + "You didn't touch the mouse or keyboard during the " + 1.174 + "test did you?"); 1.175 + finishedTimeoutOK = true; 1.176 +} 1.177 + 1.178 +// make sure we still exit when the listener and/or setTimeout don't fire: 1.179 +var testBailout = setTimeout(finishThisTest, 12000); 1.180 +var finishedTimeoutOK = false, finishedListenerOK = false; 1.181 +function finishThisTest() 1.182 +{ 1.183 + ok(finishedTimeoutOK, "We set a timeout and it should have fired by now."); 1.184 + ok(finishedListenerOK, "We added a listener and it should have been called by now."); 1.185 + if (!finishedListenerOK) 1.186 + { 1.187 + var removedListener = false; 1.188 + try 1.189 + { 1.190 + idleService.removeIdleObserver(idleObserver, newIdleSeconds); 1.191 + removedListener = true; 1.192 + } 1.193 + catch (ex) 1.194 + {} 1.195 + 1.196 + ok(removedListener, "We added a listener and we should be able to remove it."); 1.197 + } 1.198 + // Done: 1.199 + SimpleTest.finish(); 1.200 +} 1.201 + 1.202 +]]> 1.203 +</script> 1.204 + 1.205 +</window>