Bug 1028987 - Don't use Task.jsm to wait for cleanup function promises. r=jmaher
authorMarco Castelluccio <mar.castelluccio@studenti.unina.it>
Fri, 04 Jul 2014 14:41:24 +0200
changeset 206208 9543f68db4653d0e9c97fd8e727d42d43356eeae
parent 206207 54a623fe67682cd5e2c6112ef623b5bff2694e6b
child 206209 ae88c59d32847f545f2d0d0f15c68d19c9fbe6f1
push idunknown
push userunknown
push dateunknown
reviewersjmaher
bugs1028987
milestone33.0a1
Bug 1028987 - Don't use Task.jsm to wait for cleanup function promises. r=jmaher
testing/mochitest/tests/SimpleTest/SimpleTest.js
--- a/testing/mochitest/tests/SimpleTest/SimpleTest.js
+++ b/testing/mochitest/tests/SimpleTest/SimpleTest.js
@@ -858,36 +858,23 @@ SimpleTest.registerCleanupFunction = fun
     SimpleTest._cleanupFunctions.push(aFunc);
 };
 
 /**
  * Finishes the tests. This is automatically called, except when
  * SimpleTest.waitForExplicitFinish() has been invoked.
 **/
 SimpleTest.finish = function() {
-    var Task = SpecialPowers.Cu.import("resource://gre/modules/Task.jsm").Task;
-
     if (SimpleTest._alreadyFinished) {
         SimpleTest.ok(false, "[SimpleTest.finish()] this test already called finish!");
     }
 
     SimpleTest._alreadyFinished = true;
 
-    Task.spawn(function*() {
-        // Execute all of our cleanup functions.
-        var func;
-        while ((func = SimpleTest._cleanupFunctions.pop())) {
-          try {
-            yield func();
-          }
-          catch (ex) {
-            SimpleTest.ok(false, "Cleanup function threw exception: " + ex);
-          }
-        }
-
+    var afterCleanup = function() {
         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) {
@@ -912,17 +899,42 @@ SimpleTest.finish = function() {
         } else {
             SpecialPowers.flushAllAppsLaunchable();
             SpecialPowers.flushPermissions(function () {
               SpecialPowers.flushPrefEnv(function() {
                 SimpleTest.showReport();
               });
             });
         }
-    });
+    }
+
+    var executeCleanupFunction = function() {
+        var func = SimpleTest._cleanupFunctions.pop();
+
+        if (!func) {
+            afterCleanup();
+            return;
+        }
+
+        var ret;
+        try {
+            ret = func();
+        } catch (ex) {
+            SimpleTest.ok(false, "Cleanup function threw exception: " + ex);
+        }
+
+        if (ret && ret.constructor.name == "Promise") {
+            ret.then(executeCleanupFunction,
+                     (ex) => SimpleTest.ok(false, "Cleanup promise rejected: " + ex));
+        } else {
+            executeCleanupFunction();
+        }
+    };
+
+    executeCleanupFunction();
 };
 
 /**
  * Monitor console output from now until endMonitorConsole is called.
  *
  * Expect to receive all console messages described by the elements of
  * |msgs|, an array, in the order listed in |msgs|; each element is an
  * object which may have any number of the following properties: