Bug 806724 - Port browser_ConsoleStoragePBTest.js to the new per-tab PB APIs; r=ehsan
authorMario Alvarado [:marioalv] <marioalv.mozilla@gmail.com>
Mon, 10 Dec 2012 19:19:25 -0600
changeset 124611 3ce22cb51e5613f19ca524c5eccc1d59e6e9153c
parent 124610 4dfe323a663d507e2ee9e22479ce08f6f47807f8
child 124612 45bbc96e059f5d0b879ad95a917854443625f3ba
child 124691 125899d65b9484c8efac78df3d5cd2ce7df4097f
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)
reviewersehsan
bugs806724
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 806724 - Port browser_ConsoleStoragePBTest.js to the new per-tab PB APIs; r=ehsan DONTBUILD since this is NPOTB in global PB builds
dom/tests/browser/Makefile.in
dom/tests/browser/browser_ConsoleStoragePBTest_perwindowpb.js
--- a/dom/tests/browser/Makefile.in
+++ b/dom/tests/browser/Makefile.in
@@ -40,13 +40,23 @@ MOCHITEST_BROWSER_FILES += \
   browser_webapps_permissions.js \
   test-webapp.webapp \
   test-webapp-reinstall.webapp \
   test-webapp-original.webapp \
   test-webapps-permissions.html \
   $(NULL)
 endif
 
+ifdef MOZ_PER_WINDOW_PRIVATE_BROWSING
+MOCHITEST_BROWSER_FILES += \
+  browser_ConsoleStoragePBTest_perwindowpb.js \
+  $(NULL)
+else
+MOCHITEST_BROWSER_FILES += \
+  browser_ConsoleStoragePBTest.js \
+  $(NULL)
+endif
+
 
 # TODO: Re-enable permissions tests on Mac and Windows, bug 795334
 # TODO: disabled test browser_webapps_perms_reinstall.js, re-enable when bug 794920 is fixed
 
 include $(topsrcdir)/config/rules.mk
copy from dom/tests/browser/browser_ConsoleStoragePBTest.js
copy to dom/tests/browser/browser_ConsoleStoragePBTest_perwindowpb.js
--- a/dom/tests/browser/browser_ConsoleStoragePBTest.js
+++ b/dom/tests/browser/browser_ConsoleStoragePBTest_perwindowpb.js
@@ -1,93 +1,88 @@
-/* Any copyright is dedicated to the Public Domain.
-   http://creativecommons.org/publicdomain/zero/1.0/ */
-
+/* 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() {
-  try {
-    var pb = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
-  } catch (ex) {
-    ok(true, "nothing to do here, PB service doesn't exist");
-    return;
-  }
-
+  // initialization
   waitForExplicitFinish();
-
-  var CSS = {};
+  let windowsToClose = [];
+  let innerID;
+  let beforeEvents;
+  let afterEvents;
+  let storageShouldOccur;
+  let consoleObserver;
+  let testURI =
+    "http://example.com/browser/dom/tests/browser/test-console-api.html";
+  let CSS = {};
   Cu.import("resource://gre/modules/ConsoleAPIStorage.jsm", CSS);
 
-  let innerID, beforeEvents, storageShouldOccur;
-
-  var ConsoleObserver = {
-    QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
+  function getInnerWindowId(aWindow) {
+    return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+                  .getInterface(Ci.nsIDOMWindowUtils)
+                  .currentInnerWindowID;
+  }
 
-    observe: function CO_observe(aSubject, aTopic, aData)
-    {
-      if (aTopic != "console-api-log-event") {
-        return;
-      }
+  function whenNewWindowLoaded(aOptions, aCallback) {
+    let win = OpenBrowserWindow(aOptions);
+    win.addEventListener("load", function onLoad() {
+      win.removeEventListener("load", onLoad, false);
+      aCallback(win);
+    }, false);
+  }
+
+  function doTest(aIsPrivateMode, aWindow, aCallback) {
+    aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
+      aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
+
+      consoleObserver = {
+        observe: function(aSubject, aTopic, aData) {
+          if (aTopic == "console-api-log-event") {
+            afterEvents = CSS.ConsoleAPIStorage.getEvents(innerID);
+            is(beforeEvents.length == afterEvents.length - 1, storageShouldOccur,
+              "storage should" + (storageShouldOccur ? "" : " not") + " occur");
 
-      let afterEvents = CSS.ConsoleAPIStorage.getEvents(innerID);
+            executeSoon(function() {
+              Services.obs.removeObserver(consoleObserver, "console-api-log-event");
+              aCallback();
+            });
+          }
+        }
+      };
 
-      is(beforeEvents.length == afterEvents.length - 1,
-         storageShouldOccur,
-         "storage should" + (storageShouldOccur ? "" : " not") + " occur");
+      aWindow.Services.obs.addObserver(
+        consoleObserver, "console-api-log-event", false);
+      aWindow.console.log("foo bar baz (private: " + aIsPrivateMode + ")");
+    }, true);
 
-      executeSoon(function() {
-        Services.obs.removeObserver(ConsoleObserver, "console-api-log-event");
-        pb.privateBrowsingEnabled = storageShouldOccur;
-      });
-    }
+    storageShouldOccur = !aIsPrivateMode;
+    innerID = getInnerWindowId(aWindow);
+    beforeEvents = CSS.ConsoleAPIStorage.getEvents(innerID);
+    aWindow.gBrowser.selectedBrowser.loadURI(testURI);
+  }
+
+  function testOnWindow(aOptions, aCallback) {
+    whenNewWindowLoaded(aOptions, function(aWin) {
+      windowsToClose.push(aWin);
+      // execute should only be called when need, like when you are opening
+      // web pages on the test. If calling executeSoon() is not necesary, then
+      // call whenNewWindowLoaded() instead of testOnWindow() on your test.
+      executeSoon(function() aCallback(aWin));
+    });
   };
 
-  function checkStorageOccurs() {
-    Services.obs.addObserver(ConsoleObserver, "console-api-log-event", false);
-
-    let win = XPCNativeWrapper.unwrap(browser.contentWindow);
-    innerID = getInnerWindowId(win);
-
-    beforeEvents = CSS.ConsoleAPIStorage.getEvents(innerID);
-    win.console.log("foo bar baz (private: " + !storageShouldOccur + ")");
-  }
-
-  function pbObserver(aSubject, aTopic, aData) {
-    if (aData == "enter") {
-      storageShouldOccur = false;
-      checkStorageOccurs();
-    } else if (aData == "exit") {
-      executeSoon(finish);
-    }
-  }
-
-  const TEST_URI = "http://example.com/browser/dom/tests/browser/test-console-api.html";
-  var tab = gBrowser.selectedTab = gBrowser.addTab(TEST_URI);
-  var browser = gBrowser.selectedBrowser;
-
-  Services.obs.addObserver(pbObserver, "private-browsing", false);
-
-  const PB_KEEP_SESSION_PREF = "browser.privatebrowsing.keep_current_session";
-  Services.prefs.setBoolPref(PB_KEEP_SESSION_PREF, true);
-
-  registerCleanupFunction(function () {
-    gBrowser.removeTab(tab);
-
-    Services.obs.removeObserver(pbObserver, "private-browsing");
-
-    if (Services.prefs.prefHasUserValue(PB_KEEP_SESSION_PREF))
-      Services.prefs.clearUserPref(PB_KEEP_SESSION_PREF);
+   // this function is called after calling finish() on the test.
+  registerCleanupFunction(function() {
+    windowsToClose.forEach(function(aWin) {
+      aWin.close();
+    });
   });
 
-  browser.addEventListener("DOMContentLoaded", function onLoad(event) {
-    if (browser.currentURI.spec != TEST_URI)
-      return;
-
-    browser.removeEventListener("DOMContentLoaded", onLoad, false);
-
-    storageShouldOccur = true;
-    checkStorageOccurs();
-  }, false);
+  // test first when not on private mode
+  testOnWindow({}, function(aWin) {
+    doTest(false, aWin, function() {
+      // then test when on private mode
+      testOnWindow({private: true}, function(aWin) {
+        doTest(true, aWin, finish);
+      });
+    });
+  });
 }
-
-function getInnerWindowId(aWindow) {
-  return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-                .getInterface(Ci.nsIDOMWindowUtils)
-                .currentInnerWindowID;
-}