bug 866641 - add registerCleanupFunction method to mochitest-plain. r=jmaher
authorTed Mielczarek <ted@mielczarek.org>
Mon, 29 Apr 2013 14:35:14 -0400
changeset 130511 e13bb42d287235c2e8d3ebb3f6d84d21918aa896
parent 130510 2b526c0d0b587d5cb34f5822b4eccc5219b159cf
child 130512 18a2828b42aab405b2d6346057873e246f3a08d9
push id27396
push usertmielczarek@mozilla.com
push dateWed, 01 May 2013 18:16:30 +0000
treeherdermozilla-inbound@e13bb42d2872 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmaher
bugs866641
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 866641 - add registerCleanupFunction method to mochitest-plain. r=jmaher
testing/mochitest/tests/Makefile.in
testing/mochitest/tests/SimpleTest/SimpleTest.js
testing/mochitest/tests/test_sanity_cleanup.html
testing/mochitest/tests/test_sanity_cleanup2.html
--- a/testing/mochitest/tests/Makefile.in
+++ b/testing/mochitest/tests/Makefile.in
@@ -17,16 +17,18 @@ include $(topsrcdir)/config/rules.mk
   test_sanity.html \
   test_sanityException.html \
   test_sanityException2.html \
   test_sanityWindowSnapshot.html \
   test_SpecialPowersExtension.html \
   test_SpecialPowersExtension2.html \
   file_SpecialPowersFrame1.html \
   test_bug816847.html \
+  test_sanity_cleanup.html \
+  test_sanity_cleanup2.html \
   $(NULL)
 
 ifneq ($(OS_TARGET),Android)
 # Disabled on Android for permaorange, see bug 688052
   _TEST_FILES += \
   test_sanityEventUtils.html \
   test_sanitySimpletest.html
 endif
--- a/testing/mochitest/tests/SimpleTest/SimpleTest.js
+++ b/testing/mochitest/tests/SimpleTest/SimpleTest.js
@@ -220,16 +220,17 @@ SimpleTest.testPluginIsOOP = function ()
         testPluginIsOOP = SpecialPowers.getBoolPref("dom.ipc.plugins.enabled");
     }
 
     return testPluginIsOOP;
 };
 
 SimpleTest._tests = [];
 SimpleTest._stopOnLoad = true;
+SimpleTest._cleanupFunctions = [];
 
 /**
  * Something like assert.
 **/
 SimpleTest.ok = function (condition, name, diag) {
     var test = {'result': !!condition, 'name': name, 'diag': diag};
     SimpleTest._logResult(test, "TEST-PASS", "TEST-UNEXPECTED-FAIL");
     SimpleTest._tests.push(test);
@@ -688,29 +689,44 @@ SimpleTest.waitForClipboard = function(a
  * Executes a function shortly after the call, but lets the caller continue
  * working (or finish).
  */
 SimpleTest.executeSoon = function(aFunc) {
     if ("SpecialPowers" in window) {
         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
  * SimpleTest.waitForExplicitFinish() has been invoked.
 **/
 SimpleTest.finish = function () {
     if (SimpleTest._alreadyFinished) {
         SimpleTest.ok(false, "[SimpleTest.finish()] this test already called finish!");
     }
 
     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();
     }
     if (SimpleTest._expectingUncaughtException) {
         SimpleTest.ok(false, "expectUncaughtException was called but no uncaught exception was detected!");
     }
     if (SimpleTest._pendingWaitForFocusCount != 0) {
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/tests/test_sanity_cleanup.html
@@ -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>
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/tests/test_sanity_cleanup2.html
@@ -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>