Bug 821178 - Port browser_248970_a.js and browser_248970_b.js to the per-window PB APIs. r=jdm
authorAndres Hernandez <andres@appcoast.com>
Mon, 17 Dec 2012 17:19:06 -0600
changeset 125554 ce816b018b0a383e693c541342cd4cd3153f9d99
parent 125553 746f31757355d228a158f7a94940eea3659d4e64
child 125555 1114846b3791800431b5720815142438d9dcc7cc
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs821178
milestone20.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 821178 - Port browser_248970_a.js and browser_248970_b.js to the per-window PB APIs. r=jdm
browser/components/sessionstore/test/Makefile.in
browser/components/sessionstore/test/browser_248970_b_perwindowpb.js
browser/components/sessionstore/test/head.js
--- a/browser/components/sessionstore/test/Makefile.in
+++ b/browser/components/sessionstore/test/Makefile.in
@@ -137,16 +137,17 @@ ifdef MOZ_PER_WINDOW_PRIVATE_BROWSING
 MOCHITEST_BROWSER_FILES += \
 	browser_354894_perwindowpb.js \
 	browser_394759_perwindowpb.js \
 	$(NULL)
 else
 MOCHITEST_BROWSER_FILES += \
 	browser_248970_a.js \
 	browser_248970_b.js \
++	browser_248970_b_perwindowpb.js \
 	browser_354894.js \
 	browser_394759_privatebrowsing.js \
 	$(NULL)
 endif
 
 # Disabled on Windows for frequent intermittent failures
 ifneq ($(OS_ARCH), WINNT)
 MOCHITEST_FILES += \
copy from browser/components/sessionstore/test/browser_248970_b.js
copy to browser/components/sessionstore/test/browser_248970_b_perwindowpb.js
--- a/browser/components/sessionstore/test/browser_248970_b.js
+++ b/browser/components/sessionstore/test/browser_248970_b_perwindowpb.js
@@ -1,27 +1,19 @@
 /* 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 (B) for Bug 248970 **/
+  waitForExplicitFinish();
 
-  function test(aLambda) {
-    try {
-      return aLambda() || true;
-    } catch(ex) { }
-    return false;
-  }
-
-  var file = Components.classes["@mozilla.org/file/directory_service;1"]
-             .getService(Components.interfaces.nsIProperties)
-             .get("TmpD", Components.interfaces.nsIFile);
-  var filePath = file.path;
-
+  let windowsToClose = [];
+  let file = Services.dirsvc.get("TmpD", Ci.nsIFile);
+  let filePath = file.path;
   let fieldList = {
     "//input[@name='input']":     Date.now().toString(),
     "//input[@name='spaced 1']":  Math.random().toString(),
     "//input[3]":                 "three",
     "//input[@type='checkbox']":  true,
     "//input[@name='uncheck']":   false,
     "//input[@type='radio'][1]":  false,
     "//input[@type='radio'][2]":  true,
@@ -29,16 +21,29 @@ function test() {
     "//select":                   2,
     "//select[@multiple]":        [1, 3],
     "//textarea[1]":              "",
     "//textarea[2]":              "Some text... " + Math.random(),
     "//textarea[3]":              "Some more text\n" + new Date(),
     "//input[@type='file']":      filePath
   };
 
+  registerCleanupFunction(function() {
+    windowsToClose.forEach(function(win) {
+      win.close();
+    });
+  });
+
+  function test(aLambda) {
+    try {
+      return aLambda() || true;
+    } catch(ex) { }
+    return false;
+  }
+
   function getElementByXPath(aTab, aQuery) {
     let doc = aTab.linkedBrowser.contentDocument;
     let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
     return doc.evaluate(aQuery, doc, null, xptype, null).singleNodeValue;
   }
 
   function setFormValue(aTab, aQuery, aValue) {
     let node = getElementByXPath(aTab, aQuery);
@@ -63,113 +68,100 @@ function test() {
     if (node instanceof Ci.nsIDOMHTMLTextAreaElement)
       return aValue == node.value;
     if (!node.multiple)
       return aValue == node.selectedIndex;
     return Array.every(node.options, function(aOpt, aIx)
             (aValue.indexOf(aIx) > -1) == aOpt.selected);
   }
 
-  // test setup
-  waitForExplicitFinish();
-
-  // private browsing service
-  let pb = Cc["@mozilla.org/privatebrowsing;1"].
-           getService(Ci.nsIPrivateBrowsingService);
-  gPrefService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
-
   //////////////////////////////////////////////////////////////////
-  // Test (B) : Session data restoration between modes            //
+  // Test (B) : Session data restoration between windows          //
   //////////////////////////////////////////////////////////////////
 
   let rootDir = getRootDirectory(gTestPath);
   const testURL = rootDir + "browser_248970_b_sample.html";
   const testURL2 = "http://mochi.test:8888/browser/" +
-  "browser/components/sessionstore/test/browser_248970_b_sample.html";
+    "browser/components/sessionstore/test/browser_248970_b_sample.html";
 
-  // get closed tab count
-  let count = ss.getClosedTabCount(window);
-  let max_tabs_undo = gPrefService.getIntPref("browser.sessionstore.max_tabs_undo");
-  ok(0 <= count && count <= max_tabs_undo,
-    "getClosedTabCount should return zero or at most max_tabs_undo");
+  whenNewWindowLoaded(false, function(aWin) {
+    windowsToClose.push(aWin);
 
-  // setup a state for tab (A) so we can check later that is restored
-  let key = "key";
-  let value = "Value " + Math.random();
-  let state = { entries: [{ url: testURL }], extData: { key: value } };
+    // get closed tab count
+    let count = ss.getClosedTabCount(aWin);
+    let max_tabs_undo =
+      Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo");
+    ok(0 <= count && count <= max_tabs_undo,
+      "getClosedTabCount should return zero or at most max_tabs_undo");
 
-  // public session, add new tab: (A)
-  let tab_A = gBrowser.addTab(testURL);
-  ss.setTabState(tab_A, JSON.stringify(state));
-  tab_A.linkedBrowser.addEventListener("load", function(aEvent) {
-    this.removeEventListener("load", arguments.callee, true);
-
-    // make sure that the next closed tab will increase getClosedTabCount
-    gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", max_tabs_undo + 1)
+    // setup a state for tab (A) so we can check later that is restored
+    let key = "key";
+    let value = "Value " + Math.random();
+    let state = { entries: [{ url: testURL }], extData: { key: value } };
 
-    // populate tab_A with form data
-    for (let i in fieldList)
-      setFormValue(tab_A, i, fieldList[i]);
-
-    // public session, close tab: (A)
-    gBrowser.removeTab(tab_A);
-
-    // verify that closedTabCount increased
-    ok(ss.getClosedTabCount(window) > count, "getClosedTabCount has increased after closing a tab");
+    // public session, add new tab: (A)
+    let tab_A = aWin.gBrowser.addTab(testURL);
+    ss.setTabState(tab_A, JSON.stringify(state));
+    whenBrowserLoaded(tab_A.linkedBrowser, function() {
+      // make sure that the next closed tab will increase getClosedTabCount
+      Services.prefs.setIntPref(
+        "browser.sessionstore.max_tabs_undo", max_tabs_undo + 1)
 
-    // verify tab: (A), in undo list
-    let tab_A_restored = test(function() ss.undoCloseTab(window, 0));
-    ok(tab_A_restored, "a tab is in undo list");
-    tab_A_restored.linkedBrowser.addEventListener("load", function(aEvent) {
-      this.removeEventListener("load", arguments.callee, true);
+      // populate tab_A with form data
+      for (let i in fieldList)
+        setFormValue(tab_A, i, fieldList[i]);
 
-      is(testURL, this.currentURI.spec, "it's the same tab that we expect");
-      gBrowser.removeTab(tab_A_restored);
+      // public session, close tab: (A)
+      aWin.gBrowser.removeTab(tab_A);
 
-      // enter private browsing mode
-      pb.privateBrowsingEnabled = true;
-      ok(pb.privateBrowsingEnabled, "private browsing enabled");
+      // verify that closedTabCount increased
+      ok(ss.getClosedTabCount(aWin) > count,
+         "getClosedTabCount has increased after closing a tab");
 
-      // setup a state for tab (B) so we can check that its duplicated properly
-      let key1 = "key1";
-      let value1 = "Value " + Math.random();
-      let state1 = { entries: [{ url: testURL2 }], extData: { key1: value1 } };
+      // verify tab: (A), in undo list
+      let tab_A_restored = test(function() ss.undoCloseTab(aWin, 0));
+      ok(tab_A_restored, "a tab is in undo list");
+      whenBrowserLoaded(tab_A_restored.linkedBrowser, function() {
+        is(testURL, tab_A_restored.linkedBrowser.currentURI.spec,
+           "it's the same tab that we expect");
+        aWin.gBrowser.removeTab(tab_A_restored);
 
-      // private browsing session, new tab: (B)
-      let tab_B = gBrowser.addTab(testURL2);
-      ss.setTabState(tab_B, JSON.stringify(state1));
-      tab_B.linkedBrowser.addEventListener("load", function(aEvent) {
-        this.removeEventListener("load", arguments.callee, true);
+        whenNewWindowLoaded(true, function(aWin) {
+          windowsToClose.push(aWin);
 
-        // populate tab: (B) with different form data
-        for (let item in fieldList)
-          setFormValue(tab_B, item, fieldList[item]);
-
-        // duplicate tab: (B)
-        let tab_C = gBrowser.duplicateTab(tab_B);
-        tab_C.linkedBrowser.addEventListener("load", function(aEvent) {
-          this.removeEventListener("load", arguments.callee, true);
+          // setup a state for tab (B) so we can check that its duplicated
+          // properly
+          let key1 = "key1";
+          let value1 = "Value " + Math.random();
+          let state1 = {
+            entries: [{ url: testURL2 }], extData: { key1: value1 }
+          };
 
-          // verify the correctness of the duplicated tab
-          is(ss.getTabValue(tab_C, key1), value1,
-            "tab successfully duplicated - correct state");
+          let tab_B = aWin.gBrowser.addTab(testURL2);
+          ss.setTabState(tab_B, JSON.stringify(state1));
+          whenBrowserLoaded(tab_B.linkedBrowser, function() {
+            // populate tab: (B) with different form data
+            for (let item in fieldList)
+              setFormValue(tab_B, item, fieldList[item]);
 
-          for (let item in fieldList)
-            ok(compareFormValue(tab_C, item, fieldList[item]),
-              "The value for \"" + item + "\" was correctly duplicated");
-
-          // private browsing session, close tab: (C) and (B)
-          gBrowser.removeTab(tab_C);
-          gBrowser.removeTab(tab_B);
+            // duplicate tab: (B)
+            let tab_C = aWin.gBrowser.duplicateTab(tab_B);
+            whenBrowserLoaded(tab_C.linkedBrowser, function() {
+              // verify the correctness of the duplicated tab
+              is(ss.getTabValue(tab_C, key1), value1,
+                "tab successfully duplicated - correct state");
 
-          // exit private browsing mode
-          pb.privateBrowsingEnabled = false;
-          ok(!pb.privateBrowsingEnabled, "private browsing disabled");
+              for (let item in fieldList)
+                ok(compareFormValue(tab_C, item, fieldList[item]),
+                  "The value for \"" + item + "\" was correctly duplicated");
+
+              // private browsing session, close tab: (C) and (B)
+              aWin.gBrowser.removeTab(tab_C);
+              aWin.gBrowser.removeTab(tab_B);
 
-          // cleanup
-          if (gPrefService.prefHasUserValue("browser.privatebrowsing.keep_current_session"))
-            gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
-          finish();
-        }, true);
-      }, true);
-    }, true);
-  }, true);
+              finish();
+            });
+          });
+        });
+      });
+    });
+  });
 }
--- a/browser/components/sessionstore/test/head.js
+++ b/browser/components/sessionstore/test/head.js
@@ -272,8 +272,16 @@ registerCleanupFunction(function () {
 // because apparently it's buggy. See bug 599253.
 function closeAllButPrimaryWindow() {
   for (let win in BrowserWindowIterator()) {
     if (win != window) {
       win.close();
     }
   }
 }
+
+function whenNewWindowLoaded(aIsPrivate, aCallback) {
+  let win = OpenBrowserWindow({private: aIsPrivate});
+  win.addEventListener("load", function onLoad() {
+    win.removeEventListener("load", onLoad, false);
+    aCallback(win);
+  }, false);
+}