Bug 390595 - Need unittest for nsIIdleService r=roc, sr=neil, Test Only.

This commit is contained in:
gijskruitbosch@gmail.com 2007-11-15 12:50:51 -08:00
Родитель 84ae245d1c
Коммит b9565cc9ec
3 изменённых файлов: 252 добавлений и 1 удалений

Просмотреть файл

@ -42,7 +42,11 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
DIRS = public src
DIRS = public src
ifdef MOZ_MOCHITEST
DIRS += tests
endif
include $(topsrcdir)/config/rules.mk

51
widget/tests/Makefile.in Normal file
Просмотреть файл

@ -0,0 +1,51 @@
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2007
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = widget/test
include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_TEST_FILES = test_bug343416.xul \
$(NULL)
libs:: $(_TEST_FILES)
$(INSTALL) $^ $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)

Просмотреть файл

@ -0,0 +1,196 @@
<?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/MochiKit/packed.js"></script>
<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);
ok (diff < 5000, "The idle time should have increased by roughly 6 seconds," +
" as that's where we told this listener to fire.");
if (diff >= 5000)
alert(data + " " + newIdleSeconds);
// 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;
try
{
idleTime = idleService.idleTime;
}
catch (ex)
{}
ok (idleTime, "Getting the idle Time should not fail " +
"in normal circumstances on any tier 1 platform.");
// 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()
{
try
{
var newIdleTime = idleService.idleTime;
}
catch (ex)
{}
ok(newIdleTime, "Getting the idle time should not fail in normal" +
"circumstances on any tier 1 platform.");
// 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));
// 0.5 second leniency.
ok(timeDiff < 500, "The idle time should have increased by roughly the " +
"amount of time it took for the timeout to fire.");
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>