Bug 1222284 - Part 2: Replace setFinishedCallback with waitForMessage for browser tests, r=btseng
authorShawn Huang <shuang@mozilla.com>
Thu, 20 Jul 2017 15:10:30 +0800
changeset 418653 c515128e7461bb95661d5b306d9ebe3053023c34
parent 418652 e7ebcd8b7d5874cf1ffb89db67a02c93db0462aa
child 418654 3c96d3de9c7c7eea1692c1bdbca882644a587660
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbtseng
bugs1222284
milestone56.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 1222284 - Part 2: Replace setFinishedCallback with waitForMessage for browser tests, r=btseng For all tests depend on testFinishedCallback() function, the following had been changed: * waitForExplicitFinish() will be replaced with add_task(). * gBrowser.selectedBrowser.addEventListener() will be replaced with waitForMessage(). * testFinishedCallback, testResult, and testException will be unified to testResult.
dom/indexedDB/test/browser_forgetThisSite.js
dom/indexedDB/test/browser_forgetThisSiteAdd.html
dom/indexedDB/test/browser_forgetThisSiteGet.html
dom/indexedDB/test/browser_permissionsPrompt.html
dom/indexedDB/test/browser_permissionsPromptAllow.js
dom/indexedDB/test/browser_permissionsPromptDeny.js
dom/indexedDB/test/browser_permissionsPromptWorker.js
dom/indexedDB/test/browser_permissionsSharedWorker.html
dom/indexedDB/test/browser_permissionsWorker.html
dom/indexedDB/test/browser_perwindow_privateBrowsing.js
--- a/dom/indexedDB/test/browser_forgetThisSite.js
+++ b/dom/indexedDB/test/browser_forgetThisSite.js
@@ -13,90 +13,51 @@ const domains = [
 const addPath = "/browser/dom/indexedDB/test/browser_forgetThisSiteAdd.html";
 const getPath = "/browser/dom/indexedDB/test/browser_forgetThisSiteGet.html";
 
 const testPageURL1 = "http://" + domains[0] + addPath;
 const testPageURL2 = "http://" + domains[1] + addPath;
 const testPageURL3 = "http://" + domains[0] + getPath;
 const testPageURL4 = "http://" + domains[1] + getPath;
 
-function test()
-{
+add_task(async function test1() {
   requestLongerTimeout(2);
-  waitForExplicitFinish();
   // Avoids the prompt
   setPermission(testPageURL1, "indexedDB");
   setPermission(testPageURL2, "indexedDB");
-  executeSoon(test1);
-}
 
-function test1()
-{
   // Set database version for domain 1
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(result, exception) {
-      ok(result == 11, "Set version on database in " + testPageURL1);
-      ok(!exception, "No exception");
-      gBrowser.removeCurrentTab();
+  gBrowser.selectedBrowser.loadURI(testPageURL1);
+  await waitForMessage(11, gBrowser);
+  gBrowser.removeCurrentTab();
+});
 
-      executeSoon(test2);
-    });
-  }, {capture: true, once: true});
-  content.location = testPageURL1;
-}
-
-function test2()
-{
+add_task(async function test2() {
   // Set database version for domain 2
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(result, exception) {
-      ok(result == 11, "Set version on database in " + testPageURL2);
-      ok(!exception, "No exception");
-      gBrowser.removeCurrentTab();
+  gBrowser.selectedBrowser.loadURI(testPageURL2);
+  await waitForMessage(11, gBrowser);
+  gBrowser.removeCurrentTab();
+});
 
-      executeSoon(test3);
-    });
-  }, {capture: true, once: true});
-  content.location = testPageURL2;
-}
-
-function test3()
-{
+add_task(async function test3() {
   // Remove database from domain 2
   ForgetAboutSite.removeDataFromDomain(domains[1]).then(() => {
     setPermission(testPageURL4, "indexedDB");
-    executeSoon(test4);
   });
-}
+});
 
-function test4()
-{
+add_task(async function test4() {
   // Get database version for domain 1
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(result, exception) {
-      ok(result == 11, "Got correct version on database in " + testPageURL3);
-      ok(!exception, "No exception");
-      gBrowser.removeCurrentTab();
+  gBrowser.selectedBrowser.loadURI(testPageURL3);
+  await waitForMessage(11, gBrowser);
+  gBrowser.removeCurrentTab();
+});
 
-      executeSoon(test5);
-    });
-  }, {capture: true, once: true});
-  content.location = testPageURL3;
-}
-
-function test5()
-{
+add_task(async function test5() {
   // Get database version for domain 2
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(result, exception) {
-      ok(result == 1, "Got correct version on database in " + testPageURL4);
-      ok(!exception, "No exception");
-      gBrowser.removeCurrentTab();
-
-      executeSoon(finish);
-    });
-  }, {capture: true, once: true});
-  content.location = testPageURL4;
-}
+  gBrowser.selectedBrowser.loadURI(testPageURL4);
+  await waitForMessage(1, gBrowser);
+  gBrowser.removeCurrentTab();
+});
--- a/dom/indexedDB/test/browser_forgetThisSiteAdd.html
+++ b/dom/indexedDB/test/browser_forgetThisSiteAdd.html
@@ -11,17 +11,17 @@
       function* testSteps()
       {
         let request = indexedDB.open("browser_forgetThisSite.js", 11);
         request.onerror = grabEventAndContinueHandler;
         request.onupgradeneeded = grabEventAndContinueHandler;
         let event = yield undefined;
 
         if (event.type == "error") {
-          testException = event.target.error.name;
+          testResult = event.target.error.name;
         }
         else {
           let db = event.target.result;
 
           testResult = db.version;
 
           event.target.transaction.oncomplete = finishTest;
           yield undefined;
--- a/dom/indexedDB/test/browser_forgetThisSiteGet.html
+++ b/dom/indexedDB/test/browser_forgetThisSiteGet.html
@@ -11,17 +11,17 @@
       function* testSteps()
       {
         let request = indexedDB.open("browser_forgetThisSite.js");
         request.onerror = grabEventAndContinueHandler;
         request.onsuccess = grabEventAndContinueHandler;
         let event = yield undefined;
 
         if (event.type == "error") {
-          testException = event.target.error.name;
+          testResult = event.target.error.name;
         }
         else {
           let db = event.target.result;
           testResult = db.version;
         }
 
         finishTest()
         yield undefined;
--- a/dom/indexedDB/test/browser_permissionsPrompt.html
+++ b/dom/indexedDB/test/browser_permissionsPrompt.html
@@ -18,17 +18,17 @@
         request.onerror = grabEventAndContinueHandler;
         request.onsuccess = grabEventAndContinueHandler;
         let event = yield undefined;
 
         if (event.type == "success") {
           testResult = event.target.result instanceof IDBDatabase;
         }
         else {
-          testException = event.target.error.name;
+          testResult = event.target.error.name;
         }
 
         event.preventDefault();
 
         finishTest()
         yield undefined;
       }
     </script>
--- a/dom/indexedDB/test/browser_permissionsPromptAllow.js
+++ b/dom/indexedDB/test/browser_permissionsPromptAllow.js
@@ -2,85 +2,61 @@
  * Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 const testPageURL = "http://mochi.test:8888/browser/" +
   "dom/indexedDB/test/browser_permissionsPrompt.html";
 const notificationID = "indexedDB-permissions-prompt";
 
-function test()
-{
-  waitForExplicitFinish();
-
+add_task(async function test1() {
   // We want a prompt.
   removePermission(testPageURL, "indexedDB");
-  executeSoon(test1);
-}
 
-function test1()
-{
   info("creating tab");
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
 
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(isIDBDatabase, exception) {
-      ok(isIDBDatabase,
-         "First database creation was successful");
-      ok(!exception, "No exception");
-      is(getPermission(testPageURL, "indexedDB"),
-         Components.interfaces.nsIPermissionManager.ALLOW_ACTION,
-         "Correct permission set");
-      gBrowser.removeCurrentTab();
-      executeSoon(test2);
-    });
-
-    registerPopupEventHandler("popupshowing", function() {
-      ok(true, "prompt showing");
-    });
-    registerPopupEventHandler("popupshown", function() {
-      ok(true, "prompt shown");
-      triggerMainCommand(this);
-    });
-    registerPopupEventHandler("popuphidden", function() {
-      ok(true, "prompt hidden");
-    });
-
-  }, {capture: true, once: true});
+  registerPopupEventHandler("popupshowing", function() {
+    ok(true, "prompt showing");
+  });
+  registerPopupEventHandler("popupshown", function() {
+    ok(true, "prompt shown");
+    triggerMainCommand(this);
+  });
+  registerPopupEventHandler("popuphidden", function() {
+    ok(true, "prompt hidden");
+  });
 
   info("loading test page: " + testPageURL);
-  content.location = testPageURL;
-}
+  gBrowser.selectedBrowser.loadURI(testPageURL);
 
-function test2()
-{
+  await waitForMessage(true, gBrowser);
+  is(getPermission(testPageURL, "indexedDB"),
+     Components.interfaces.nsIPermissionManager.ALLOW_ACTION,
+     "Correct permission set");
+  gBrowser.removeCurrentTab();
+});
+
+add_task(async function test2() {
   info("creating tab");
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
 
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(isIDBDatabase, exception) {
-      ok(isIDBDatabase,
-         "First database creation was successful");
-      ok(!exception, "No exception");
-      is(getPermission(testPageURL, "indexedDB"),
-         Components.interfaces.nsIPermissionManager.ALLOW_ACTION,
-         "Correct permission set");
-      gBrowser.removeCurrentTab();
-      unregisterAllPopupEventHandlers();
-      removePermission(testPageURL, "indexedDB");
-      executeSoon(finish);
-    });
-
-    registerPopupEventHandler("popupshowing", function() {
-      ok(false, "Shouldn't show a popup this time");
-    });
-    registerPopupEventHandler("popupshown", function() {
-      ok(false, "Shouldn't show a popup this time");
-    });
-    registerPopupEventHandler("popuphidden", function() {
-      ok(false, "Shouldn't show a popup this time");
-    });
-
-  }, {capture: true, once: true});
+  registerPopupEventHandler("popupshowing", function() {
+    ok(false, "Shouldn't show a popup this time");
+  });
+  registerPopupEventHandler("popupshown", function() {
+    ok(false, "Shouldn't show a popup this time");
+  });
+  registerPopupEventHandler("popuphidden", function() {
+    ok(false, "Shouldn't show a popup this time");
+  });
 
   info("loading test page: " + testPageURL);
-  content.location = testPageURL;
-}
+  gBrowser.selectedBrowser.loadURI(testPageURL);
+
+  await waitForMessage(true, gBrowser);
+  is(getPermission(testPageURL, "indexedDB"),
+     Components.interfaces.nsIPermissionManager.ALLOW_ACTION,
+     "Correct permission set");
+  gBrowser.removeCurrentTab();
+  unregisterAllPopupEventHandlers();
+  removePermission(testPageURL, "indexedDB");
+});
--- a/dom/indexedDB/test/browser_permissionsPromptDeny.js
+++ b/dom/indexedDB/test/browser_permissionsPromptDeny.js
@@ -2,46 +2,16 @@
  * Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 const testPageURL = "http://mochi.test:8888/browser/" +
   "dom/indexedDB/test/browser_permissionsPrompt.html";
 const notificationID = "indexedDB-permissions-prompt";
 
-function waitForMessage(aMessage, browser) {
-  return new Promise((resolve, reject) => {
-    /* eslint-disable no-undef */
-    function contentScript() {
-      addEventListener("message", function(event) {
-        sendAsyncMessage("testLocal:exception",
-          {exception: event.data});
-      }, {once: true}, true);
-    }
-    /* eslint-enable no-undef */
-
-    let script = "data:,(" + contentScript.toString() + ")();";
-
-    let mm = browser.selectedBrowser.messageManager;
-
-    mm.addMessageListener("testLocal:exception", function listener(msg) {
-      mm.removeMessageListener("testLocal:exception", listener);
-      mm.removeDelayedFrameScript(script);
-      is(msg.data.exception, aMessage, "received " + aMessage);
-      if (msg.data.exception == aMessage) {
-        resolve();
-      } else {
-        reject();
-      }
-    });
-
-    mm.loadFrameScript(script, true);
-  });
-}
-
 add_task(async function test1() {
   removePermission(testPageURL, "indexedDB");
 
   registerPopupEventHandler("popupshowing", function() {
     ok(true, "prompt showing");
   });
   registerPopupEventHandler("popupshown", function() {
     ok(true, "prompt shown");
--- a/dom/indexedDB/test/browser_permissionsPromptWorker.js
+++ b/dom/indexedDB/test/browser_permissionsPromptWorker.js
@@ -4,84 +4,60 @@
  */
 
 const testWorkerURL = "http://mochi.test:8888/browser/" +
   "dom/indexedDB/test/browser_permissionsWorker.html";
 const testSharedWorkerURL = "http://mochi.test:8888/browser/" +
   "dom/indexedDB/test/browser_permissionsSharedWorker.html";
 const notificationID = "indexedDB-permissions-prompt";
 
-function test()
-{
-  waitForExplicitFinish();
-  executeSoon(test1);
-}
-
-function test1()
-{
+add_task(async function test1() {
   // We want a prompt.
   removePermission(testWorkerURL, "indexedDB");
+  registerPopupEventHandler("popupshowing", function() {
+    ok(true, "prompt showing");
+  });
+  registerPopupEventHandler("popupshown", function() {
+    ok(true, "prompt shown");
+    triggerMainCommand(this);
+  });
+  registerPopupEventHandler("popuphidden", function() {
+    ok(true, "prompt hidden");
+  });
+
+  info("creating tab");
+  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
+  info("loading test page: " + testWorkerURL);
+  gBrowser.selectedBrowser.loadURI(testWorkerURL);
+
+  await waitForMessage("ok", gBrowser);
+  is(getPermission(testWorkerURL, "indexedDB"),
+     Components.interfaces.nsIPermissionManager.ALLOW_ACTION,
+     "Correct permission set");
+  gBrowser.removeCurrentTab();
+});
+
+add_task(async function test2() {
+  // We want a prompt.
+  removePermission(testSharedWorkerURL, "indexedDB");
+
+  registerPopupEventHandler("popupshowing", function() {
+    ok(false, "prompt showing");
+  });
+  registerPopupEventHandler("popupshown", function() {
+    ok(false, "prompt shown");
+  });
+  registerPopupEventHandler("popuphidden", function() {
+    ok(false, "prompt hidden");
+  });
 
   info("creating tab");
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
 
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(isIDBDatabase, exception) {
-      ok(isIDBDatabase, "First database creation was successful");
-      ok(!exception, "No exception");
-      is(getPermission(testWorkerURL, "indexedDB"),
-         Components.interfaces.nsIPermissionManager.ALLOW_ACTION,
-         "Correct permission set");
-      gBrowser.removeCurrentTab();
-      executeSoon(test2);
-    });
-
-    registerPopupEventHandler("popupshowing", function() {
-      ok(true, "prompt showing");
-    });
-    registerPopupEventHandler("popupshown", function() {
-      ok(true, "prompt shown");
-      triggerMainCommand(this);
-    });
-    registerPopupEventHandler("popuphidden", function() {
-      ok(true, "prompt hidden");
-    });
-
-  }, {capture: true, once: true});
-
-  info("loading test page: " + testWorkerURL);
-  content.location = testWorkerURL;
-}
-
-function test2()
-{
-  // We want a prompt.
-  removePermission(testSharedWorkerURL, "indexedDB");
-
-  info("creating tab");
-  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
-
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    setFinishedCallback(function(isIDBDatabase, exception) {
-      ok(!isIDBDatabase, "First database creation was successful");
-      ok(exception, "No exception");
-      is(getPermission(testSharedWorkerURL, "indexedDB"),
-         Components.interfaces.nsIPermissionManager.UNKNOWN_ACTION,
-         "Correct permission set");
-      gBrowser.removeCurrentTab();
-      executeSoon(finish);
-    });
-
-    registerPopupEventHandler("popupshowing", function() {
-      ok(false, "prompt showing");
-    });
-    registerPopupEventHandler("popupshown", function() {
-      ok(false, "prompt shown");
-    });
-    registerPopupEventHandler("popuphidden", function() {
-      ok(false, "prompt hidden");
-    });
-
-  }, {capture: true, once: true});
 
   info("loading test page: " + testSharedWorkerURL);
-  content.location = testSharedWorkerURL;
-}
+  gBrowser.selectedBrowser.loadURI(testSharedWorkerURL);
+  await waitForMessage("InvalidStateError", gBrowser);
+  is(getPermission(testSharedWorkerURL, "indexedDB"),
+     Components.interfaces.nsIPermissionManager.UNKNOWN_ACTION,
+     "Correct permission set");
+  gBrowser.removeCurrentTab();
+});
--- a/dom/indexedDB/test/browser_permissionsSharedWorker.html
+++ b/dom/indexedDB/test/browser_permissionsSharedWorker.html
@@ -2,31 +2,26 @@
   Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/
 -->
 <html>
   <head>
     <title>Indexed Database Test</title>
 
     <script type="text/javascript">
-    // testFinishedCallback is expected to be set in the scope by head.js.
-    /* global testFinishedCallback */
-    let testIsIDBDatabase;
-    let testException;
-
     function runTest() {
       let w = new SharedWorker("browser_permissionsSharedWorker.js");
       w.port.onmessage = function(e) {
+        let message;
         if (e.data.status == "success") {
-          testIsIDBDatabase = e.data.isIDBDatabase;
+          message = "ok";
         } else {
-          testException = e.data.error;
+          message = e.data.error;
         }
-
-        setTimeout(testFinishedCallback, 0, testIsIDBDatabase, testException);
+        postMessage(message, "*");
       }
 
       const name = window.location.pathname + "_sharedWorker";
       w.port.postMessage(name);
     }
     </script>
 
   </head>
--- a/dom/indexedDB/test/browser_permissionsWorker.html
+++ b/dom/indexedDB/test/browser_permissionsWorker.html
@@ -2,31 +2,26 @@
   Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/
 -->
 <html>
   <head>
     <title>Indexed Database Test</title>
 
     <script type="text/javascript">
-    // testFinishedCallback is expected to be set in the scope by head.js.
-    /* global testFinishedCallback */
-    let testIsIDBDatabase;
-    let testException;
-
     function runTest() {
       let w = new Worker("browser_permissionsWorker.js");
       w.onmessage = function(e) {
+        let message;
         if (e.data.status == "success") {
-          testIsIDBDatabase = e.data.isIDBDatabase;
+          message = "ok";
         } else {
-          testException = e.data.error;
+          message = e.data.error;
         }
-
-        setTimeout(testFinishedCallback, 0, testIsIDBDatabase, testException);
+        postMessage(message, "*");
       }
 
       const name = window.location.pathname;
       w.postMessage(name);
     }
     </script>
 
   </head>
--- a/dom/indexedDB/test/browser_perwindow_privateBrowsing.js
+++ b/dom/indexedDB/test/browser_perwindow_privateBrowsing.js
@@ -2,67 +2,34 @@
  * Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 const testPageURL = "http://mochi.test:8888/browser/" +
   "dom/indexedDB/test/browser_permissionsPrompt.html";
 const notificationID = "indexedDB-permissions-prompt";
 
-function test()
-{
-  waitForExplicitFinish();
+add_task(async function test1() {
   // Avoids the actual prompt
   setPermission(testPageURL, "indexedDB");
-  executeSoon(test1);
-}
 
-function test1()
-{
+  info("creating tab");
   gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
-  gBrowser.selectedBrowser.addEventListener("load", function() {
-    if (content.location != testPageURL) {
-      content.location = testPageURL;
-      return;
-    }
-    gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
+
+  info("loading test page: " + testPageURL);
+  gBrowser.selectedBrowser.loadURI(testPageURL);
 
-    setFinishedCallback(function(isIDBDatabase, exception) {
-      ok(isIDBDatabase,
-         "First database creation was successful");
-      ok(!exception, "No exception");
-      gBrowser.removeCurrentTab();
-
-      executeSoon(test2);
-    });
-  }, true);
-  content.location = testPageURL;
-}
+  await waitForMessage(true, gBrowser);
+  gBrowser.removeCurrentTab();
+});
 
-function test2()
-{
-  var win = OpenBrowserWindow({private: true});
-  win.addEventListener("load", function() {
-    executeSoon(() => test3(win));
-  }, {once: true});
-  registerCleanupFunction(() => win.close());
-}
+add_task(async function test2() {
+  info("creating private window");
+  let win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
 
-function test3(win)
-{
+  info("creating tab");
   win.gBrowser.selectedTab = win.gBrowser.addTab();
-  win.gBrowser.selectedBrowser.addEventListener("load", function() {
-    if (win.content.location != testPageURL) {
-      win.content.location = testPageURL;
-      return;
-    }
-    win.gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
-
-    setFinishedCallback(function(isIDBDatabase, exception) {
-      ok(!isIDBDatabase, "No database");
-      is(exception, "InvalidStateError", "Correct exception");
-      win.gBrowser.removeCurrentTab();
-
-      executeSoon(finish);
-    }, win);
-  }, true);
-  win.content.location = testPageURL;
-}
+  win.gBrowser.selectedBrowser.loadURI(testPageURL);
+  await waitForMessage("InvalidStateError", win.gBrowser);
+  win.gBrowser.removeCurrentTab();
+  await BrowserTestUtils.closeWindow(win);
+  win = null;
+});