Bug 1290280 - Make bug_423132.js more resilient to the initial browser being remote by default. r=mikedeboer
authorMike Conley <mconley@mozilla.com>
Sat, 13 Aug 2016 18:46:38 -0400
changeset 400821 6ad896bd72cb33577687803a5a314ff081da4531
parent 400820 e5ab45eab1be2dc9a70fc011af44716c429b89b9
child 400822 ada11d4cda081e4c48f30f0c86a718c11cc24cf4
push id26289
push userbmo:npancholi@mozilla.com
push dateMon, 15 Aug 2016 22:10:02 +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);
+});