2007-11-15 23:50:51 +03:00
|
|
|
<?xml version="1.0"?>
|
|
|
|
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
|
|
|
|
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
|
|
|
|
type="text/css"?>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=343416
|
|
|
|
-->
|
|
|
|
<window title="Mozilla Bug 343416"
|
|
|
|
xmlns:html="http://www.w3.org/1999/xhtml"
|
|
|
|
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
|
|
|
|
|
|
|
|
<title>Test for Bug 343416</title>
|
|
|
|
<script type="application/javascript"
|
|
|
|
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
|
|
|
|
|
|
|
|
<body xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=343416">Mozilla Bug 343416</a>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
<script class="testbody" type="application/javascript">
|
|
|
|
<![CDATA[
|
|
|
|
|
|
|
|
/** Test for Bug 343416 **/
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
|
|
|
// Observer:
|
|
|
|
var idleObserver =
|
|
|
|
{
|
|
|
|
QueryInterface: function _qi(iid)
|
|
|
|
{
|
|
|
|
if (iid.equals(Components.interfaces.nsISupports) ||
|
|
|
|
iid.equals(Components.interfaces.nsIObserver))
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
observe: function _observe(subject, topic, data)
|
|
|
|
{
|
|
|
|
if (topic != "idle")
|
|
|
|
return;
|
|
|
|
|
|
|
|
var diff = Math.abs(data - newIdleSeconds * 1000);
|
2007-12-05 01:24:35 +03:00
|
|
|
|
2008-10-13 04:03:27 +04:00
|
|
|
// ok (diff < 5000, "The idle time should have increased by roughly 6 seconds, " +
|
|
|
|
// "as that's when we told this listener to fire.");
|
2007-12-05 01:24:35 +03:00
|
|
|
// if (diff >= 5000)
|
|
|
|
// alert(data + " " + newIdleSeconds);
|
2007-11-15 23:50:51 +03:00
|
|
|
|
|
|
|
// Attempt to get to the nsIIdleService
|
|
|
|
var subjectOK = false;
|
|
|
|
try {
|
|
|
|
var idleService = subject.QueryInterface(nsIIdleService);
|
|
|
|
subjectOK = true;
|
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
ok(subjectOK, "The subject of the notification should be the " +
|
|
|
|
"nsIIdleService.");
|
|
|
|
|
|
|
|
// Attempt to remove ourselves.
|
|
|
|
var removedObserver = false;
|
|
|
|
try {
|
|
|
|
idleService.removeIdleObserver(this, newIdleSeconds);
|
|
|
|
removedObserver = true;
|
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
ok(removedObserver, "We should be able to remove our observer here.");
|
|
|
|
finishedListenerOK = true;
|
|
|
|
if (finishedTimeoutOK)
|
|
|
|
{
|
|
|
|
clearTimeout(testBailout);
|
|
|
|
finishThisTest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const nsIIdleService = Components.interfaces.nsIIdleService;
|
|
|
|
const nsIISCID = "@mozilla.org/widget/idleservice;1";
|
|
|
|
var idleService = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
idleService = Components.classes[nsIISCID].getService(nsIIdleService);
|
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ok(idleService, "nsIIdleService should exist and be implemented on all tier 1 platforms.");
|
|
|
|
|
|
|
|
var idleTime = null;
|
2010-04-16 21:37:16 +04:00
|
|
|
var gotIdleTime = false;
|
2007-11-15 23:50:51 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
idleTime = idleService.idleTime;
|
2010-04-16 21:37:16 +04:00
|
|
|
gotIdleTime = true;
|
2007-11-15 23:50:51 +03:00
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
ok (gotIdleTime, "Getting the idle time should not fail " +
|
|
|
|
"in normal circumstances on any tier 1 platform.");
|
2007-11-15 23:50:51 +03:00
|
|
|
|
|
|
|
// Now we set up a timeout to sanity-test the idleTime after 5 seconds
|
|
|
|
setTimeout(testIdleTime, 5000);
|
|
|
|
var startTimeStamp = Date.now();
|
|
|
|
|
|
|
|
// Now we add the listener:
|
|
|
|
var newIdleSeconds = Math.floor(idleTime / 1000) + 6;
|
|
|
|
var addedObserver = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
idleService.addIdleObserver(idleObserver, newIdleSeconds);
|
|
|
|
addedObserver = true;
|
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ok(addedObserver, "The nsIIdleService should allow us to add an observer.");
|
|
|
|
|
|
|
|
addedObserver = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
idleService.addIdleObserver(idleObserver, newIdleSeconds);
|
|
|
|
addedObserver = true;
|
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ok(addedObserver, "The nsIIdleService should allow us to add the same observer again.");
|
|
|
|
|
|
|
|
var removedObserver = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
idleService.removeIdleObserver(idleObserver, newIdleSeconds);
|
|
|
|
removedObserver = true;
|
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ok(removedObserver, "The nsIIdleService should allow us to remove the observer just once.");
|
|
|
|
|
|
|
|
function testIdleTime()
|
|
|
|
{
|
2010-04-16 21:37:16 +04:00
|
|
|
var gotIdleTime = false
|
2007-11-15 23:50:51 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var newIdleTime = idleService.idleTime;
|
2010-04-16 21:37:16 +04:00
|
|
|
gotIdleTime = true
|
2007-11-15 23:50:51 +03:00
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
2010-04-16 21:37:16 +04:00
|
|
|
ok(gotIdleTime, "Getting the idle time should not fail " +
|
2008-10-13 04:03:27 +04:00
|
|
|
"in normal circumstances on any tier 1 platform.");
|
2007-11-15 23:50:51 +03:00
|
|
|
// Get the time difference, remove the approx. 5 seconds that we've waited,
|
|
|
|
// should be very close to 0 left.
|
|
|
|
var timeDiff = Math.abs((newIdleTime - idleTime) -
|
|
|
|
(Date.now() - startTimeStamp));
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
var timePassed = Date.now() - startTimeStamp;
|
|
|
|
var idleTimeDiff = newIdleTime - idleTime;
|
|
|
|
// 1 second leniency.
|
|
|
|
ok(timeDiff < 1000, "The idle time should have increased by roughly the " +
|
|
|
|
"amount of time it took for the timeout to fire. " +
|
2011-09-14 07:04:13 +04:00
|
|
|
"You didn't touch the mouse or keyboard during the " +
|
2010-04-16 21:37:16 +04:00
|
|
|
"test did you?");
|
2007-11-15 23:50:51 +03:00
|
|
|
finishedTimeoutOK = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we still exit when the listener and/or setTimeout don't fire:
|
|
|
|
var testBailout = setTimeout(finishThisTest, 12000);
|
|
|
|
var finishedTimeoutOK = false, finishedListenerOK = false;
|
|
|
|
function finishThisTest()
|
|
|
|
{
|
|
|
|
ok(finishedTimeoutOK, "We set a timeout and it should have fired by now.");
|
|
|
|
ok(finishedListenerOK, "We added a listener and it should have been called by now.");
|
|
|
|
if (!finishedListenerOK)
|
|
|
|
{
|
|
|
|
var removedListener = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
idleService.removeIdleObserver(idleObserver, newIdleSeconds);
|
|
|
|
removedListener = true;
|
|
|
|
}
|
|
|
|
catch (ex)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ok(removedListener, "We added a listener and we should be able to remove it.");
|
|
|
|
}
|
|
|
|
// Done:
|
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
]]>
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</window>
|