bug 866641 - add registerCleanupFunction method to mochitest-plain. r=jmaher

This commit is contained in:
Ted Mielczarek 2013-04-29 14:35:14 -04:00
parent 27ebe04988
commit 0eb2e7a465
4 changed files with 73 additions and 1 deletions

View File

@ -22,6 +22,8 @@ _TEST_FILES = \
test_SpecialPowersExtension2.html \
file_SpecialPowersFrame1.html \
test_bug816847.html \
test_sanity_cleanup.html \
test_sanity_cleanup2.html \
$(NULL)
ifneq ($(OS_TARGET),Android)

View File

@ -225,6 +225,7 @@ SimpleTest.testPluginIsOOP = function () {
SimpleTest._tests = [];
SimpleTest._stopOnLoad = true;
SimpleTest._cleanupFunctions = [];
/**
* Something like assert.
@ -693,7 +694,11 @@ SimpleTest.executeSoon = function(aFunc) {
return SpecialPowers.executeSoon(aFunc, window);
}
setTimeout(aFunc, 0);
}
};
SimpleTest.registerCleanupFunction = function(aFunc) {
SimpleTest._cleanupFunctions.push(aFunc);
};
/**
* Finishes the tests. This is automatically called, except when
@ -706,6 +711,17 @@ SimpleTest.finish = function () {
SimpleTest._alreadyFinished = true;
// Execute all of our cleanup functions.
var func;
while ((func = SimpleTest._cleanupFunctions.pop())) {
try {
func();
}
catch (ex) {
SimpleTest.ok(false, "Cleanup function threw exception: " + ex);
}
}
if (SpecialPowers.DOMWindowUtils.isTestControllingRefreshes) {
SimpleTest.ok(false, "test left refresh driver under test control");
SpecialPowers.DOMWindowUtils.restoreNormalRefresh();

View File

@ -0,0 +1,30 @@
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
<title>SimpleTest.registerCleanupFunction test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">
// Not a great example, since we have the pushPrefEnv API to cover
// this use case, but I want to be able to test that the cleanup
// function gets run, so setting and clearing a pref seems straightforward.
function do_cleanup1() {
SpecialPowers.clearUserPref("simpletest.cleanup.1");
info("do_cleanup1 run!");
}
function do_cleanup2() {
SpecialPowers.clearUserPref("simpletest.cleanup.2");
info("do_cleanup2 run!");
}
SpecialPowers.setBoolPref("simpletest.cleanup.1", true);
SpecialPowers.setBoolPref("simpletest.cleanup.2", true);
SimpleTest.registerCleanupFunction(do_cleanup1);
SimpleTest.registerCleanupFunction(do_cleanup2);
ok(true, "dummy check");
</script>
</body>
</html>

View File

@ -0,0 +1,24 @@
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
<title>SimpleTest.registerCleanupFunction test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">
for (pref of [1, 2]) {
try {
SpecialPowers.getBoolPref("simpletest.cleanup." + pref);
ok(false, "Cleanup function should have unset pref");
}
catch(ex) {
ok(true, "Pref was not set");
}
}
</script>
</body>
</html>