Bug 1290280 - Make bug_423132.js more resilient to the initial browser being remote by default. r?mikedeboer draft
authorMike Conley <mconley@mozilla.com>
Sat, 13 Aug 2016 18:46:38 -0400
changeset 400675 ee48bb9da41fc07fbdabcedc3f128c42e95f3a54
parent 400483 0d227c6f30866a3cc811be0c100284ef724d44ad
child 528288 4e48aa5ab5ca6c8829e3b228e206a5a918cc42b3
push id26240
push usermconley@mozilla.com
push dateMon, 15 Aug 2016 12:49:42 +0000
reviewersmikedeboer
bugs1290280, 1261842
milestone51.0a1
Bug 1290280 - Make bug_423132.js more resilient to the initial browser being remote by default. r?mikedeboer The problem with this test was that it was actually relying on the old broken behaviour where the initial browser of the new window it opens would be flipped from remote back to non-remote before loading its contents and flipping remote again. Because it now starts remote (and stays there instead of doing all of the extra work), the test was more likely to fall into the trap that I described in https://groups.google.com/forum/#!searchin/mozilla.dev.platform/1261842%7Csort:relevance/mozilla.dev.platform/gthFqog3J-M/Ypx-SNhEQgAJ where the promiseBrowserLoaded was firing for the wrong page load, which meant that the cookie hadn't had a chance to be set yet. I've converted the test to use the properly instrumented BrowserTestUtils functions which wait for the window to be properly ready, and it appears to pass on try with multiple retriggers. MozReview-Commit-ID: BtQRx7og52A
browser/components/sessionstore/test/browser_423132.js
--- a/browser/components/sessionstore/test/browser_423132.js
+++ b/browser/components/sessionstore/test/browser_423132.js
@@ -1,81 +1,59 @@
-/* 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/. */
-
-function test() {
-  // test that cookies are stored and restored correctly by sessionstore,
-  // bug 423132.
+"use strict";
 
-  waitForExplicitFinish();
-
-  let cs = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
-  cs.removeAll();
-
-  // make sure that sessionstore.js can be forced to be created by setting
-  // the interval pref to 0
-  gPrefService.setIntPref("browser.sessionstore.interval", 0);
-
+/**
+ * Tests that cookies are stored and restored correctly
+ * by sessionstore (bug 423132).
+ */
+add_task(function*() {
   const testURL = "http://mochi.test:8888/browser/" +
     "browser/components/sessionstore/test/browser_423132_sample.html";
 
-  // open a new window
-  let newWin = openDialog(location, "_blank", "chrome,all,dialog=no", "about:blank");
-
-  // make sure sessionstore saves the cookie data, then close the window
-  newWin.addEventListener("load", function (aEvent) {
-    newWin.removeEventListener("load", arguments.callee, false);
-
-    // Wait for sessionstore to be ready to restore this window
-    executeSoon(function() {
-      newWin.gBrowser.loadURI(testURL, null, null);
+  Services.cookies.removeAll();
+  // make sure that sessionstore.js can be forced to be created by setting
+  // the interval pref to 0
+  yield SpecialPowers.pushPrefEnv({
+    set: [["browser.sessionstore.interval", 0]]
+  });
 
-      promiseBrowserLoaded(newWin.gBrowser.selectedBrowser).then(() => {
-        let ready = () => {
-          // get the sessionstore state for the window
-          let state = ss.getWindowState(newWin);
+  let win = yield BrowserTestUtils.openNewBrowserWindow();
+  let browser = win.gBrowser.selectedBrowser;
+  browser.loadURI(testURL);
+  yield BrowserTestUtils.browserLoaded(browser);
 
-          // verify our cookie got set during pageload
-          let e = cs.enumerator;
-          let cookie;
-          let i = 0;
-          while (e.hasMoreElements()) {
-            cookie = e.getNext().QueryInterface(Ci.nsICookie);
-            i++;
-          }
-          is(i, 1, "expected one cookie");
+  yield TabStateFlusher.flush(browser);
 
-          // remove the cookie
-          cs.removeAll();
+  // get the sessionstore state for the window
+  let state = ss.getWindowState(win);
 
-          // restore the window state
-          ss.setWindowState(newWin, state, true);
+  // verify our cookie got set during pageload
+  let enumerator = Services.cookies.enumerator;
+  let cookie;
+  let i = 0;
+  while (enumerator.hasMoreElements()) {
+    cookie = enumerator.getNext().QueryInterface(Ci.nsICookie);
+    i++;
+  }
+  Assert.equal(i, 1, "expected one cookie");
 
-          // at this point, the cookie should be restored...
-          e = cs.enumerator;
-          let cookie2;
-          while (e.hasMoreElements()) {
-            cookie2 = e.getNext().QueryInterface(Ci.nsICookie);
-            if (cookie.name == cookie2.name)
-              break;
-          }
-          is(cookie.name, cookie2.name, "cookie name successfully restored");
-          is(cookie.value, cookie2.value, "cookie value successfully restored");
-          is(cookie.path, cookie2.path, "cookie path successfully restored");
+  // remove the cookie
+  Services.cookies.removeAll();
+
+  // restore the window state
+  ss.setWindowState(win, state, true);
 
-          // clean up
-          if (gPrefService.prefHasUserValue("browser.sessionstore.interval"))
-            gPrefService.clearUserPref("browser.sessionstore.interval");
-          cs.removeAll();
-          BrowserTestUtils.closeWindow(newWin).then(finish);
-        };
+  // at this point, the cookie should be restored...
+  enumerator = Services.cookies.enumerator;
+  let cookie2;
+  while (enumerator.hasMoreElements()) {
+    cookie2 = enumerator.getNext().QueryInterface(Ci.nsICookie);
+    if (cookie.name == cookie2.name)
+      break;
+  }
+  is(cookie.name, cookie2.name, "cookie name successfully restored");
+  is(cookie.value, cookie2.value, "cookie value successfully restored");
+  is(cookie.path, cookie2.path, "cookie path successfully restored");
 
-        function flushAndReady() {
-          TabStateFlusher.flush(newWin.gBrowser.selectedBrowser).then(ready);
-        }
-
-        flushAndReady();
-      }, true, testURL);
-    });
-  }, false);
-}
-
+  // clean up
+  Services.cookies.removeAll();
+  yield BrowserTestUtils.closeWindow(win);
+});