Bug 806699 - Port browser_privatebrowsing_ui.js to the new per-window PB APIs; r=ehsan
authorMario Alvarado [:marioalv] <marioalv.mozilla@gmail.com>
Wed, 21 Nov 2012 15:26:36 -0600
changeset 113941 bfd5f652e5f068c5c04057102c18cde8f2f0026e
parent 113940 f7f8011950c99e8af0848b310f4009b959a7b378
child 113942 7b0171b51aa37803f88aacf1fd734c14cd5b0765
push id18465
push usereakhgari@mozilla.com
push dateWed, 21 Nov 2012 22:26:17 +0000
treeherdermozilla-inbound@bfd5f652e5f0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs806699
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 806699 - Port browser_privatebrowsing_ui.js to the new per-window PB APIs; r=ehsan
browser/components/privatebrowsing/test/browser/perwindow/Makefile.in
browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_ui.js
--- a/browser/components/privatebrowsing/test/browser/perwindow/Makefile.in
+++ b/browser/components/privatebrowsing/test/browser/perwindow/Makefile.in
@@ -33,16 +33,17 @@ MOCHITEST_BROWSER_FILES =  \
 		browser_privatebrowsing_opendir.js \
 		browser_privatebrowsing_openlocation.js \
 		browser_privatebrowsing_openLocationLastURL.js \
 		browser_privatebrowsing_placestitle.js \
 		browser_privatebrowsing_popupblocker.js \
 		browser_privatebrowsing_protocolhandler.js \
 		browser_privatebrowsing_protocolhandler_page.html \
 		browser_privatebrowsing_theming.js \
+		browser_privatebrowsing_ui.js \
 		browser_privatebrowsing_urlbarfocus.js \
 		browser_privatebrowsing_windowtitle.js \
 		browser_privatebrowsing_windowtitle_page.html \
 		browser_privatebrowsing_zoomrestore.js \
 		popup.html \
 		title.sjs \
 		$(NULL)
 
copy from browser/components/privatebrowsing/test/browser/global/browser_privatebrowsing_ui.js
copy to browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_ui.js
--- a/browser/components/privatebrowsing/test/browser/global/browser_privatebrowsing_ui.js
+++ b/browser/components/privatebrowsing/test/browser/perwindow/browser_privatebrowsing_ui.js
@@ -3,100 +3,80 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // This test makes sure that the gPrivateBrowsingUI object, the Private Browsing
 // menu item and its XUL <command> element work correctly.
 
 function test() {
   // initialization
   waitForExplicitFinish();
-  gPrefService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
-  let pb = Cc["@mozilla.org/privatebrowsing;1"].
-           getService(Ci.nsIPrivateBrowsingService);
-  let observerData;
-  function observer(aSubject, aTopic, aData) {
-    if (aTopic == "private-browsing")
-      observerData = aData;
-  }
-  Services.obs.addObserver(observer, "private-browsing", false);
-  let pbMenuItem = document.getElementById("privateBrowsingItem");
-  // add a new blank tab to ensure the title can be meaningfully compared later
-  gBrowser.selectedTab = gBrowser.addTab();
-  let originalTitle = document.title;
+  let windowsToClose = [];
+  let testURI = "about:blank";
+  let pbMenuItem;
+  let cmd;
+
+  function doTest(aIsPrivateMode, aWindow, aCallback) {
+    aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
+      aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
+
+      ok(aWindow.gPrivateBrowsingUI, "The gPrivateBrowsingUI object exists");
+
+      pbMenuItem = aWindow.document.getElementById("menu_newPrivateWindow");
+      ok(pbMenuItem, "The Private Browsing menu item exists");
 
-  function testNewWindow(aCallback, expected) {
-    Services.obs.addObserver(function observer1(aSubject, aTopic, aData) {
+      cmd = aWindow.document.getElementById("Tools:PrivateBrowsing");
+      isnot(cmd, null, "XUL command object for the private browsing service exists");
+
+      is(pbMenuItem.getAttribute("label"), "New Private Window",
+        "The Private Browsing menu item should read \"New Private Window\"");
+      is(PrivateBrowsingUtils.isWindowPrivate(aWindow), aIsPrivateMode,
+        "PrivateBrowsingUtils should report the correct per-window private browsing status (privateBrowsing should be " +
+        aIsPrivateMode + ")");
+
+      aCallback();
+    }, true);
+
+    aWindow.gBrowser.selectedBrowser.loadURI(testURI);
+  };
+
+  function openPrivateBrowsingModeByUI(aWindow, aCallback) {
+    Services.obs.addObserver(function observer(aSubject, aTopic, aData) {
       aSubject.addEventListener("load", function() {
         aSubject.removeEventListener("load", arguments.callee);
-        executeSoon(function() {
-          let ui = aSubject.gPrivateBrowsingUI;
-          is(ui.privateBrowsingEnabled, expected, "The privateBrowsingEnabled property on the new window is set correctly");
-          is(PrivateBrowsingUtils.isWindowPrivate(aSubject), expected, "The private bit on the new window is set correctly");
-
-          Services.obs.addObserver(function observer2(aSubject, aTopic, aData) {
-            aCallback();
-            Services.obs.removeObserver(observer2, "domwindowclosed");
-          }, "domwindowclosed", false);
-          aSubject.close();
-        });
-        Services.obs.removeObserver(observer1, "domwindowopened");
+          Services.obs.removeObserver(observer, "domwindowopened");
+          windowsToClose.push(aSubject);
+          aCallback(aSubject);
       }, false);
     }, "domwindowopened", false);
-    OpenBrowserWindow();
-  }
+
+    cmd = aWindow.document.getElementById("Tools:PrivateBrowsing");
+    var func = new Function("", cmd.getAttribute("oncommand"));
+    func.call(cmd);
+  };
 
-  // test the gPrivateBrowsingUI object
-  ok(gPrivateBrowsingUI, "The gPrivateBrowsingUI object exists");
-  is(pb.privateBrowsingEnabled, false, "The private browsing mode should not be started initially");
-  is(gPrivateBrowsingUI.privateBrowsingEnabled, false, "gPrivateBrowsingUI should expose the correct private browsing status");
-  is(PrivateBrowsingUtils.isWindowPrivate(window), false, "PrivateBrowsingUtils should expose the correct per-window private browsing status");
-  ok(pbMenuItem, "The Private Browsing menu item exists");
-  is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("startlabel"), "The Private Browsing menu item should read \"Start Private Browsing\"");
-  testNewWindow(function() {
-    gPrivateBrowsingUI.toggleMode();
-    is(pb.privateBrowsingEnabled, true, "The private browsing mode should be started");
-    is(gPrivateBrowsingUI.privateBrowsingEnabled, true, "gPrivateBrowsingUI should expose the correct private browsing status");
-    is(PrivateBrowsingUtils.isWindowPrivate(window), true, "PrivateBrowsingUtils should expose the correct per-window private browsing status");
-    // check to see if the Private Browsing mode was activated successfully
-    is(observerData, "enter", "Private Browsing mode was activated using the gPrivateBrowsingUI object");
-    is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("stoplabel"), "The Private Browsing menu item should read \"Stop Private Browsing\"");
-    testNewWindow(function() {
-      gPrivateBrowsingUI.toggleMode()
-      is(pb.privateBrowsingEnabled, false, "The private browsing mode should not be started");
-      is(gPrivateBrowsingUI.privateBrowsingEnabled, false, "gPrivateBrowsingUI should expose the correct private browsing status");
-      is(PrivateBrowsingUtils.isWindowPrivate(window), false, "PrivateBrowsingUtils should expose the correct per-window private browsing status");
-      // check to see if the Private Browsing mode was deactivated successfully
-      is(observerData, "exit", "Private Browsing mode was deactivated using the gPrivateBrowsingUI object");
-      is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("startlabel"), "The Private Browsing menu item should read \"Start Private Browsing\"");
+  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));
+    });
+  };
 
-      testNewWindow(function() {
-        // These are tests for the private bit setter.  Note that the setter should
-        // not be used anywhere else for now!
-        setPrivateWindow(window, true);
-        is(PrivateBrowsingUtils.isWindowPrivate(window), true, "PrivateBrowsingUtils should accept the correct per-window private browsing status");
-        setPrivateWindow(window, false);
-        is(PrivateBrowsingUtils.isWindowPrivate(window), false, "PrivateBrowsingUtils should accept the correct per-window private browsing status");
+   // this function is called after calling finish() on the test.
+  registerCleanupFunction(function() {
+    windowsToClose.forEach(function(aWin) {
+      aWin.close();
+    });
+  });
 
-        // now, test using the <command> object
-        let cmd = document.getElementById("Tools:PrivateBrowsing");
-        isnot(cmd, null, "XUL command object for the private browsing service exists");
-        var func = new Function("", cmd.getAttribute("oncommand"));
-        func.call(cmd);
-        // check to see if the Private Browsing mode was activated successfully
-        is(observerData, "enter", "Private Browsing mode was activated using the command object");
-        // check to see that the window title has been changed correctly
-        isnot(document.title, originalTitle, "Private browsing mode has correctly changed the title");
-        func.call(cmd);
-        // check to see if the Private Browsing mode was deactivated successfully
-        is(observerData, "exit", "Private Browsing mode was deactivated using the command object");
-        // check to see that the window title has been restored correctly
-        is(document.title, originalTitle, "Private browsing mode has correctly restored the title");
-
-        // cleanup
-        gBrowser.removeCurrentTab();
-        Services.obs.removeObserver(observer, "private-browsing");
-        gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
-
-        finish();
-      }, false);
-    }, true);
-  }, false);
+  // test first when not on private mode
+  testOnWindow({}, function(aWin) {
+    doTest(false, aWin, function() {
+      // then test when on private mode, opening a new private window from the
+      // user interface.
+      openPrivateBrowsingModeByUI(aWin, function(aPrivateWin) {
+        doTest(true, aPrivateWin, finish);
+      });
+    });
+  });
 }