widget/tests/test_bug343416.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial