Bug 1252376 - refactor browser_privatetabs.js to use BrowserTestUtils instead of using less deterministic methods and make sure to garbage collect, forcibly, after each sessionstore mochitest ended. r=Gijs
authorMike de Boer <mdeboer@mozilla.com>
Fri, 25 Nov 2016 16:53:33 +0100
changeset 324521 ba2692e1cbcf1e2362b01d282eb3de0ed5e00004
parent 324520 7ce5e47c372f3f78c6c3f539cabf30ddc258afcd
child 324522 0c9297cc0e465f5fbfabf8aba64daac9edd8baca
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersGijs
bugs1252376
milestone53.0a1
Bug 1252376 - refactor browser_privatetabs.js to use BrowserTestUtils instead of using less deterministic methods and make sure to garbage collect, forcibly, after each sessionstore mochitest ended. r=Gijs MozReview-Commit-ID: BgYllazwzLp
browser/components/sessionstore/test/browser_privatetabs.js
browser/components/sessionstore/test/head.js
toolkit/modules/PrivateBrowsingUtils.jsm
--- a/browser/components/sessionstore/test/browser_privatetabs.js
+++ b/browser/components/sessionstore/test/browser_privatetabs.js
@@ -10,90 +10,85 @@ add_task(function cleanup() {
 
 add_task(function() {
   let URL_PUBLIC = "http://example.com/public/" + Math.random();
   let URL_PRIVATE = "http://example.com/private/" + Math.random();
   let tab1, tab2;
   try {
     // Setup a public tab and a private tab
     info("Setting up public tab");
-    tab1 = gBrowser.addTab(URL_PUBLIC);
-    yield promiseBrowserLoaded(tab1.linkedBrowser);
+    tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, URL_PUBLIC);
 
     info("Setting up private tab");
-    tab2 = gBrowser.addTab();
-    yield promiseBrowserLoaded(tab2.linkedBrowser);
+    tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser);
     yield setUsePrivateBrowsing(tab2.linkedBrowser, true);
     tab2.linkedBrowser.loadURI(URL_PRIVATE);
-    yield promiseBrowserLoaded(tab2.linkedBrowser);
+    yield BrowserTestUtils.browserLoaded(tab2.linkedBrowser, false, URL_PRIVATE);
 
     info("Flush to make sure chrome received all data.");
     yield TabStateFlusher.flush(tab1.linkedBrowser);
     yield TabStateFlusher.flush(tab2.linkedBrowser);
 
     info("Checking out state");
     let state = yield promiseRecoveryFileContents();
 
-    info("State: " + state);
     // Ensure that sessionstore.js only knows about the public tab
     ok(state.indexOf(URL_PUBLIC) != -1, "State contains public tab");
     ok(state.indexOf(URL_PRIVATE) == -1, "State does not contain private tab");
 
     // Ensure that we can close and undo close the public tab but not the private tab
-    gBrowser.removeTab(tab2);
+    yield BrowserTestUtils.removeTab(tab2);
     tab2 = null;
 
-    gBrowser.removeTab(tab1);
+    yield BrowserTestUtils.removeTab(tab1);
     tab1 = null;
 
     tab1 = ss.undoCloseTab(window, 0);
     ok(true, "Public tab supports undo close");
 
     is(ss.getClosedTabCount(window), 0, "Private tab does not support undo close");
 
   } finally {
     if (tab1) {
-      gBrowser.removeTab(tab1);
+      yield BrowserTestUtils.removeTab(tab1);
     }
     if (tab2) {
-      gBrowser.removeTab(tab2);
+      yield BrowserTestUtils.removeTab(tab2);
     }
   }
 });
 
 add_task(function () {
   const FRAME_SCRIPT = "data:," +
     "docShell.QueryInterface%28Components.interfaces.nsILoadContext%29.usePrivateBrowsing%3Dtrue";
 
   // Clear the list of closed windows.
   forgetClosedWindows();
 
   // Create a new window to attach our frame script to.
-  let win = yield promiseNewWindowLoaded();
+  let win = yield BrowserTestUtils.openNewBrowserWindow();
   let mm = win.getGroupMessageManager("browsers");
   mm.loadFrameScript(FRAME_SCRIPT, true);
 
   // Create a new tab in the new window that will load the frame script.
-  let tab = win.gBrowser.addTab("about:mozilla");
+  let tab = yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:mozilla");
   let browser = tab.linkedBrowser;
-  yield promiseBrowserLoaded(browser);
   yield TabStateFlusher.flush(browser);
 
   // Check that we consider the tab as private.
   let state = JSON.parse(ss.getTabState(tab));
   ok(state.isPrivate, "tab considered private");
 
   // Ensure we don't allow restoring closed private tabs in non-private windows.
-  win.gBrowser.removeTab(tab);
+  yield BrowserTestUtils.removeTab(tab);
   is(ss.getClosedTabCount(win), 0, "no tabs to restore");
 
   // Create a new tab in the new window that will load the frame script.
-  tab = win.gBrowser.addTab("about:mozilla");
+  tab = yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:mozilla");
   browser = tab.linkedBrowser;
-  yield promiseBrowserLoaded(browser);
   yield TabStateFlusher.flush(browser);
 
   // Check that we consider the tab as private.
   state = JSON.parse(ss.getTabState(tab));
   ok(state.isPrivate, "tab considered private");
 
   // Check that all private tabs are removed when the non-private
   // window is closed and we don't save windows without any tabs.
@@ -101,30 +96,29 @@ add_task(function () {
   is(ss.getClosedWindowCount(), 0, "no windows to restore");
 });
 
 add_task(function () {
   // Clear the list of closed windows.
   forgetClosedWindows();
 
   // Create a new window to attach our frame script to.
-  let win = yield promiseNewWindowLoaded({private: true});
+  let win = yield BrowserTestUtils.openNewBrowserWindow({private: true});
 
   // Create a new tab in the new window that will load the frame script.
-  let tab = win.gBrowser.addTab("about:mozilla");
+  let tab = yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:mozilla");
   let browser = tab.linkedBrowser;
-  yield promiseBrowserLoaded(browser);
   yield TabStateFlusher.flush(browser);
 
   // Check that we consider the tab as private.
   let state = JSON.parse(ss.getTabState(tab));
   ok(state.isPrivate, "tab considered private");
 
   // Ensure that closed tabs in a private windows can be restored.
-  win.gBrowser.removeTab(tab);
+  yield BrowserTestUtils.removeTab(tab);
   is(ss.getClosedTabCount(win), 1, "there is a single tab to restore");
 
   // Ensure that closed private windows can never be restored.
   yield BrowserTestUtils.closeWindow(win);
   is(ss.getClosedWindowCount(), 0, "no windows to restore");
 });
 
 function setUsePrivateBrowsing(browser, val) {
--- a/browser/components/sessionstore/test/head.js
+++ b/browser/components/sessionstore/test/head.js
@@ -18,16 +18,20 @@ var mm = Cc["@mozilla.org/globalmessagem
 for (let script of FRAME_SCRIPTS) {
   mm.loadFrameScript(script, true);
 }
 
 registerCleanupFunction(() => {
   for (let script of FRAME_SCRIPTS) {
     mm.removeDelayedFrameScript(script, true);
   }
+
+  // Force a garbage collect after the end of each test run, to make sure that it
+  // won't interfere with the timing of the next test to be run from the suite.
+  window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).garbageCollect();
 });
 
 const {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
 const {SessionStore} = Cu.import("resource:///modules/sessionstore/SessionStore.jsm", {});
 const {SessionSaver} = Cu.import("resource:///modules/sessionstore/SessionSaver.jsm", {});
 const {SessionFile} = Cu.import("resource:///modules/sessionstore/SessionFile.jsm", {});
 const {TabState} = Cu.import("resource:///modules/sessionstore/TabState.jsm", {});
 const {TabStateFlusher} = Cu.import("resource:///modules/sessionstore/TabStateFlusher.jsm", {});
--- a/toolkit/modules/PrivateBrowsingUtils.jsm
+++ b/toolkit/modules/PrivateBrowsingUtils.jsm
@@ -1,30 +1,29 @@
 /* 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.EXPORTED_SYMBOLS = ["PrivateBrowsingUtils"];
 
-Components.utils.import("resource://gre/modules/Services.jsm");
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
 
 const kAutoStartPref = "browser.privatebrowsing.autostart";
 
 // This will be set to true when the PB mode is autostarted from the command
 // line for the current session.
 var gTemporaryAutoStartMode = false;
 
-const Cc = Components.classes;
-const Ci = Components.interfaces;
-
 this.PrivateBrowsingUtils = {
   // Rather than passing content windows to this function, please use
   // isBrowserPrivate since it works with e10s.
   isWindowPrivate: function pbu_isWindowPrivate(aWindow) {
-    if (!(aWindow instanceof Components.interfaces.nsIDOMChromeWindow)) {
+    if (!(aWindow instanceof Ci.nsIDOMChromeWindow)) {
       dump("WARNING: content window passed to PrivateBrowsingUtils.isWindowPrivate. " +
            "Use isContentWindowPrivate instead (but only for frame scripts).\n"
            + new Error().stack);
     }
 
     return this.privacyContextFromWindow(aWindow).usePrivateBrowsing;
   },
 
@@ -40,19 +39,25 @@ this.PrivateBrowsingUtils = {
       // browsing status since the only alternative is to check the
       // content window, which is in another process.
       return this.isWindowPrivate(chromeWin);
     }
     return this.privacyContextFromWindow(aBrowser.contentWindow).usePrivateBrowsing;
   },
 
   privacyContextFromWindow: function pbu_privacyContextFromWindow(aWindow) {
-    return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-                  .getInterface(Ci.nsIWebNavigation)
-                  .QueryInterface(Ci.nsILoadContext);
+    let context = { usePrivateBrowsing: false };
+    try {
+      context = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+                       .getInterface(Ci.nsIWebNavigation)
+                       .QueryInterface(Ci.nsILoadContext);
+    } catch (ex) {
+      Cu.reportError(ex);
+    }
+    return context;
   },
 
   addToTrackingAllowlist(aURI) {
     let pbmtpWhitelist = Cc["@mozilla.org/pbm-tp-whitelist;1"]
                            .getService(Ci.nsIPrivateBrowsingTrackingProtectionWhitelist);
     pbmtpWhitelist.addToAllowList(aURI);
   },
 
@@ -76,17 +81,17 @@ this.PrivateBrowsingUtils = {
   enterTemporaryAutoStartMode: function pbu_enterTemporaryAutoStartMode() {
     gTemporaryAutoStartMode = true;
   },
   get isInTemporaryAutoStartMode() {
     return gTemporaryAutoStartMode;
   },
 
   whenHiddenPrivateWindowReady: function pbu_whenHiddenPrivateWindowReady(cb) {
-    Components.utils.import("resource://gre/modules/Timer.jsm");
+    Cu.import("resource://gre/modules/Timer.jsm");
 
     let win = Services.appShell.hiddenPrivateDOMWindow;
     function isNotLoaded() {
       return ["complete", "interactive"].indexOf(win.document.readyState) == -1;
     }
     if (isNotLoaded()) {
       setTimeout(function poll() {
         if (isNotLoaded()) {