Bug 1132566 - Make test e10s compatible: browser_privatebrowsing_cookieacceptdialog.js; r=billm
authorSteven MacLeod <smacleod@mozilla.com>
Thu, 05 Mar 2015 11:06:10 -0500
changeset 233823 3dfae5c35fabcae49902a29f404e5b50c6804acb
parent 233822 034add5853cd3a9b6e7c326cd74bb65af9e5c0b2
child 233824 7713401cf0c45506116c354773604df1dcb031f7
push id28425
push usercbook@mozilla.com
push dateTue, 17 Mar 2015 10:33:06 +0000
treeherdermozilla-central@a194e8023090 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm
bugs1132566
milestone39.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 1132566 - Make test e10s compatible: browser_privatebrowsing_cookieacceptdialog.js; r=billm
browser/components/privatebrowsing/test/browser/browser.ini
browser/components/privatebrowsing/test/browser/browser_privatebrowsing_cookieacceptdialog.js
testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
--- a/browser/components/privatebrowsing/test/browser/browser.ini
+++ b/browser/components/privatebrowsing/test/browser/browser.ini
@@ -20,17 +20,17 @@ support-files =
 skip-if = true # Bug 1142678 - Loading a message sending frame script into a private about:home tab causes leaks
 [browser_privatebrowsing_aboutHomeButtonAfterWindowClose_old.js]
 skip-if = e10s
 [browser_privatebrowsing_aboutSessionRestore.js]
 [browser_privatebrowsing_cache.js]
 [browser_privatebrowsing_certexceptionsui.js]
 [browser_privatebrowsing_concurrent.js]
 [browser_privatebrowsing_cookieacceptdialog.js]
-skip-if = e10s
+skip-if = e10s # Bug 1139953 - Accept cookie dialog shown in private window when e10s enabled
 [browser_privatebrowsing_crh.js]
 skip-if = e10s
 [browser_privatebrowsing_downloadLastDir.js]
 skip-if = e10s
 [browser_privatebrowsing_downloadLastDir_c.js]
 skip-if = e10s
 [browser_privatebrowsing_downloadLastDir_toggle.js]
 skip-if = e10s
--- a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_cookieacceptdialog.js
+++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_cookieacceptdialog.js
@@ -1,54 +1,26 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // This test makes sure that private browsing mode disables the "remember"
 // option in the cookie accept dialog.
 
-function test() {
+add_task(function* test() {
   // initialization
   const TEST_URL = "http://mochi.test:8888/browser/browser/components/" +
                    "privatebrowsing/test/browser/" +
                    "browser_privatebrowsing_cookieacceptdialog.html";
   const BLANK_URL = "http://mochi.test:8888/";
   let cp = Cc["@mozilla.org/embedcomp/cookieprompt-service;1"].
            getService(Ci.nsICookiePromptService);
 
-  waitForExplicitFinish();
 
-  function checkRememberOption(expectedDisabled, aWindow, callback) {
-    function observer(aSubject, aTopic, aData) {
-      if (aTopic != "domwindowopened")
-        return;
-
-      Services.ww.unregisterNotification(observer);
-      let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
-      win.addEventListener("load", function onLoad(event) {
-        win.removeEventListener("load", onLoad, false);
-
-        executeSoon(function () {
-          let doc = win.document;
-          let remember = doc.getElementById("persistDomainAcceptance");
-          ok(remember, "The remember checkbox should exist");
-
-          if (expectedDisabled)
-            is(remember.getAttribute("disabled"), "true",
-               "The checkbox should be disabled");
-          else
-            ok(!remember.hasAttribute("disabled"),
-               "The checkbox should not be disabled");
-
-          waitForWindowClose(win, callback);
-        });
-      }, false);
-    }
-    Services.ww.registerNotification(observer);
-
+  function openCookieDialog(aWindow) {
     let remember = {};
     const time = (new Date("Jan 1, 2030")).getTime() / 1000;
     let cookie = {
       name: "foo",
       value: "bar",
       isDomain: true,
       host: "mozilla.org",
       path: "/baz",
@@ -62,100 +34,95 @@ function test() {
       QueryInterface: function(iid) {
         const validIIDs = [Ci.nsISupports, Ci.nsICookie, Ci.nsICookie2];
         for (var i = 0; i < validIIDs.length; ++i)
           if (iid == validIIDs[i])
             return this;
         throw Cr.NS_ERROR_NO_INTERFACE;
       }
     };
-    cp.cookieDialog(aWindow, cookie, "mozilla.org", 10, false, remember);
-  }
 
-  function checkSettingDialog(aIsPrivateWindow, aWindow, aCallback) {
-    let selectedBrowser = aWindow.gBrowser.selectedBrowser;
+    executeSoon(function () {
+      cp.cookieDialog(aWindow, cookie, "mozilla.org", 10, false, remember);
+    });
+    return BrowserTestUtils.domWindowOpened();
+  };
+
+
+  function checkRememberOption(expectedDisabled, aWindow) {
+    return Task.spawn(function* () {
+      let dialogWin = yield openCookieDialog(aWindow);
 
-    function onLoad() {
-      if (aWindow.content.location.href != TEST_URL) {
-        selectedBrowser.loadURI(TEST_URL);
-        return;
-      }
-      selectedBrowser.removeEventListener("load", onLoad, true);
-      Services.ww.unregisterNotification(observer);
+      yield new Promise(resolve => {
+        dialogWin.addEventListener("load", function onLoad(event) {
+          dialogWin.removeEventListener("load", onLoad, false);
+          resolve();
+        }, false);
+      });
 
-      ok(aIsPrivateWindow,
-         "Confirm setting dialog is not displayed for private window");
+      let doc = dialogWin.document;
+      let remember = doc.getElementById("persistDomainAcceptance");
+      ok(remember, "The remember checkbox should exist");
 
-      executeSoon(aCallback);
-    }
-    selectedBrowser.addEventListener("load", onLoad, true);
+      if (expectedDisabled)
+        is(remember.getAttribute("disabled"), "true",
+           "The checkbox should be disabled");
+      else
+        ok(!remember.hasAttribute("disabled"),
+           "The checkbox should not be disabled");
 
-    function observer(aSubject, aTopic, aData) {
-      if (aTopic != "domwindowopened")
-        return;
-      selectedBrowser.removeEventListener("load", onLoad, true);
-      Services.ww.unregisterNotification(observer);
-      ok(!aIsPrivateWindow,
-         "Confirm setting dialog is displayed for normal window");
-      let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
-      executeSoon(function () {
-        info("Wait for window close");
-        waitForWindowClose(win, aCallback);
-      });
-    }
-    Services.ww.registerNotification(observer);
+      yield BrowserTestUtils.closeWindow(dialogWin);
+    });
+  };
 
-    selectedBrowser.loadURI(TEST_URL);
-  }
+  function checkSettingDialog(aIsPrivateWindow, aWindow) {
+    return Task.spawn(function* () {
+      let dialogOpened = false;
+      let promiseDialogClosed = null;
+
+      function observer(subject, topic, data) {
+        if (topic != "domwindowopened") { return; }
+        Services.ww.unregisterNotification(observer);
+        dialogOpened = true;
 
-  let windowsToClose = [];
-  function testOnWindow(aIsPrivate, aCallback) {
-    whenNewWindowLoaded({private: aIsPrivate}, function(aWin) {
-      windowsToClose.push(aWin);
-      let selectedBrowser = aWin.gBrowser.selectedBrowser;
-      selectedBrowser.addEventListener("load", function onLoad() {
-        if (aWin.content.location.href != BLANK_URL) {
-          selectedBrowser.loadURI(BLANK_URL);
-          return;
-        }
-        selectedBrowser.removeEventListener("load", onLoad, true);
-        executeSoon(function() aCallback(aWin));
-      }, true);
-      selectedBrowser.loadURI(BLANK_URL);
+        promiseDialogClosed = BrowserTestUtils.closeWindow(
+          subject.QueryInterface(Ci.nsIDOMWindow));
+      }
+      Services.ww.registerNotification(observer);
+
+      let selectedBrowser = aWindow.gBrowser.selectedBrowser;
+      selectedBrowser.loadURI(TEST_URL);
+      yield BrowserTestUtils.browserLoaded(selectedBrowser);;
+
+      if (dialogOpened) {
+        ok(!aIsPrivateWindow,
+           "Setting dialog shown, confirm normal window");
+      } else {
+        Services.ww.unregisterNotification(observer);
+        ok(aIsPrivateWindow,
+           "Confirm setting dialog is not displayed for private window");
+      }
+
+      yield promiseDialogClosed;
     });
-  }
-
-  registerCleanupFunction(function() {
-    Services.prefs.clearUserPref("network.cookie.lifetimePolicy");
-    windowsToClose.forEach(function(aWin) {
-      aWin.close();
-    });
-  });
+  };
 
   // Ask all cookies
   Services.prefs.setIntPref("network.cookie.lifetimePolicy", 1);
 
-  testOnWindow(false, function(aWin) {
-    info("Test on public window");
-    checkRememberOption(false, aWin, function() {
-      checkSettingDialog(false, aWin, function() {
-        testOnWindow(true, function(aPrivWin) {
-          info("Test on private window");
-          checkRememberOption(true, aPrivWin, function() {
-            checkSettingDialog(true, aPrivWin, finish);
-          });
-        });
-      });
-    });
-  });
-}
+  let win = yield BrowserTestUtils.openNewBrowserWindow();
+  info("Test on public window");
+
+  yield checkRememberOption(false, win);
+  yield checkSettingDialog(false, win);
+
+  let privateWin = yield BrowserTestUtils.openNewBrowserWindow({private: true});
+  info("Test on private window");
 
-function waitForWindowClose(aWin, aCallback) {
-  function observer(aSubject, aTopic, aData) {
-    if (aTopic == "domwindowclosed") {
-      info("Window closed");
-      Services.ww.unregisterNotification(observer);
-      executeSoon(aCallback);
-    }
-  }
-  Services.ww.registerNotification(observer);
-  aWin.close();
-}
+  yield checkRememberOption(true, privateWin);
+  yield checkSettingDialog(true, privateWin);
+
+
+  // Cleanup
+  Services.prefs.clearUserPref("network.cookie.lifetimePolicy");
+  yield BrowserTestUtils.closeWindow(win);
+  yield BrowserTestUtils.closeWindow(privateWin);
+});
--- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
+++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
@@ -55,16 +55,33 @@ this.BrowserTestUtils = {
             "browser-test-utils:loadEvent", onLoad);
           resolve();
         }
       });
     });
   },
 
   /**
+   * @return {Promise}
+   *         A Promise which resolves when a "domwindowopened" notification
+   *         has been fired by the window watcher.
+   */
+  domWindowOpened() {
+    return new Promise(resolve => {
+      function observer(subject, topic, data) {
+        if (topic != "domwindowopened") { return; }
+
+        Services.ww.unregisterNotification(observer);
+        resolve(subject.QueryInterface(Ci.nsIDOMWindow));
+      }
+      Services.ww.registerNotification(observer);
+    });
+  },
+
+  /**
    * @param {Object} options
    *        {
    *          private: A boolean indicating if the window should be
    *                   private
    *        }
    * @return {Promise}
    *         Resolves with the new window once it is loaded.
    */
@@ -85,16 +102,39 @@ this.BrowserTestUtils = {
     // Wait for browser-delayed-startup-finished notification, it indicates
     // that the window has loaded completely and is ready to be used for
     // testing.
     return TestUtils.topicObserved("browser-delayed-startup-finished",
                                    subject => subject == win).then(() => win);
   },
 
   /**
+   * Closes a window.
+   *
+   * @param {Window}
+   *        A window to close.
+   *
+   * @return {Promise}
+   *         Resolves when the provided window has been closed.
+   */
+  closeWindow(win) {
+    return new Promise(resolve => {
+      function observer(subject, topic, data) {
+        if (topic == "domwindowclosed" && subject === win) {
+          Services.ww.unregisterNotification(observer);
+          resolve();
+        }
+      }
+      Services.ww.registerNotification(observer);
+      win.close();
+    });
+  },
+
+
+  /**
    * Waits a specified number of miliseconds for a specified event to be
    * fired on a specified element.
    *
    * Usage:
    *    let receivedEvent = BrowserTestUtil.waitForEvent(element, "eventName");
    *    // Do some processing here that will cause the event to be fired
    *    // ...
    *    // Now yield until the Promise is fulfilled