Bug 806736 - Port test_privbrowsing.html to the new per-tab PB APIs. r=ehsan a=test-only
authorRaymond Lee <raymond@raysquare.com>
Wed, 09 Jan 2013 12:21:29 +0800
changeset 127225 5020af3460e03396498c4dafc9648246faec87d2
parent 127224 c80439ee84c00d118bfb8ff1cd19a44b94b05247
child 127226 cf75b93c729aae43d6054ba7db9ff8c1f7cfcc5f
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, test-only
bugs806736
milestone20.0a2
Bug 806736 - Port test_privbrowsing.html to the new per-tab PB APIs. r=ehsan a=test-only
testing/mochitest/android.json
toolkit/components/passwordmgr/test/Makefile.in
toolkit/components/passwordmgr/test/privbrowsing_perwindowpb_iframe.html
toolkit/components/passwordmgr/test/test_privbrowsing_perwindowpb.html
--- a/testing/mochitest/android.json
+++ b/testing/mochitest/android.json
@@ -301,16 +301,17 @@
  "toolkit/components/passwordmgr/test/test_master_password.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_maxforms_1.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_maxforms_2.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_maxforms_3.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_bug_627616.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_master_password_cleanup.html": "",
  "toolkit/components/passwordmgr/test/test_notifications.html": "",
  "toolkit/components/passwordmgr/test/test_notifications_popup.html": "",
+ "toolkit/components/passwordmgr/test/test_privbrowsing_perwindowpb.html": "",
  "toolkit/components/passwordmgr/test/test_prompt.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_prompt_async.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_xhr.html": "TIMED_OUT",
  "toolkit/components/passwordmgr/test/test_xml_load.html": "TIMED_OUT",
  "toolkit/components/places/tests/test_bug_411966.html": "RANDOM",
  "toolkit/components/prompts/test/test_bug620145.html": "TIMED_OUT",
  "toolkit/components/prompts/test/test_modal_prompts.html": "TIMED_OUT",
  "toolkit/components/prompts/test/test_modal_select.html": "TIMED_OUT",
--- a/toolkit/components/passwordmgr/test/Makefile.in
+++ b/toolkit/components/passwordmgr/test/Makefile.in
@@ -87,15 +87,21 @@ MOCHITEST_FILES += \
     test_privbrowsing.html \
     $(NULL)
 
 ifneq ($(OS_TARGET),Linux)
 MOCHITEST_FILES += \
     test_prompt.html \
     $(NULL)
 endif
+
+else
+MOCHITEST_FILES += \
+    privbrowsing_perwindowpb_iframe.html \
+    test_privbrowsing_perwindowpb.html \
+    $(NULL)
 endif
 
 # This test doesn't pass because we can't ensure a cross-platform
 # event that occurs between DOMContentLoaded and Pageload
 # test_bug_221634.html
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/toolkit/components/passwordmgr/test/privbrowsing_perwindowpb_iframe.html
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+</head>
+<body>
+<iframe id="iframe"></iframe>
+</body>
+</html>
copy from toolkit/components/passwordmgr/test/test_privbrowsing.html
copy to toolkit/components/passwordmgr/test/test_privbrowsing_perwindowpb.html
--- a/toolkit/components/passwordmgr/test/test_privbrowsing.html
+++ b/toolkit/components/passwordmgr/test/test_privbrowsing_perwindowpb.html
@@ -7,17 +7,16 @@ https://bugzilla.mozilla.org/show_bug.cg
   <title>Test for Bug 248970</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="text/javascript" src="notification_common.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=248970">Mozilla Bug 248970</a>
 <p id="display"></p>
-<iframe id="iframe"></iframe>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 /** Test for Bug 248970 **/
 // based on test_notifications.html
 
 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
 const Ci = Components.interfaces;
@@ -34,67 +33,64 @@ var subtests = [
                    "subtst_privbrowsing_2.html", // 5
                    "subtst_privbrowsing_2.html", // 6
                    "subtst_privbrowsing_3.html", // 7
                    "subtst_privbrowsing_3.html", // 8
                    "subtst_privbrowsing_4.html", // 9
                    "subtst_privbrowsing_3.html" // 10
                ];
 
-
 var testNum = 0;
 function loadNextTest() {
   // run the initialization code for each test
   switch (++ testNum) {
     case 1:
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
+      popupNotifications = normalWindowPopupNotifications;
+      iframe = normalWindowIframe;
       break;
 
     case 2:
-      pb.privateBrowsingEnabled = true;
-      ok(pb.privateBrowsingEnabled, "Test #" + testNum + " should be run inside of private mode");
+      popupNotifications = privateWindowPopupNotifications;
+      iframe = privateWindowIframe;
       break;
 
     case 3:
-      pb.privateBrowsingEnabled = false;
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
+      popupNotifications = normalWindowPopupNotifications;
+      iframe = normalWindowIframe;
       break;
 
     case 4:
       pwmgr.addLogin(login);
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
       break;
 
     case 5:
-      pb.privateBrowsingEnabled = true;
-      ok(pb.privateBrowsingEnabled, "Test #" + testNum + " should be run inside of private mode");
+      popupNotifications = privateWindowPopupNotifications;
+      iframe = privateWindowIframe;
       break;
 
     case 6:
-      pb.privateBrowsingEnabled = false;
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
+      popupNotifications = normalWindowPopupNotifications;
+      iframe = normalWindowIframe;
       break;
 
     case 7:
       pwmgr.addLogin(login);
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
       break;
 
     case 8:
-      pb.privateBrowsingEnabled = true;
-      ok(pb.privateBrowsingEnabled, "Test #" + testNum + " should be run inside of private mode");
+      popupNotifications = privateWindowPopupNotifications;
+      iframe = privateWindowIframe;
       break;
 
     case 9:
-      ok(pb.privateBrowsingEnabled, "Test #" + testNum + " should be run inside of private mode");
       break;
 
     case 10:
-      pb.privateBrowsingEnabled = false;
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
+      popupNotifications = normalWindowPopupNotifications;
+      iframe = normalWindowIframe;
       break;
 
     default:
       ok(false, "Unexpected call to loadNextTest for test #" + testNum);
   }
 
   ok(true, "Starting test #" + testNum);
   iframe.src = prefix + subtests[testNum-1];
@@ -180,96 +176,118 @@ function checkTest() {
       break;
 
     default:
       ok(false, "Unexpected call to checkTest for test #" + testNum);
 
   }
 }
 
-
-var _PBSvc = null;
-function get_PBSvc() {
-  if (_PBSvc)
-    return _PBSvc;
+var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
+                    .getInterface(Ci.nsIWebNavigation)
+                    .QueryInterface(Ci.nsIDocShellTreeItem)
+                    .rootTreeItem
+                    .QueryInterface(Ci.nsIInterfaceRequestor)
+                    .getInterface(Ci.nsIDOMWindow);
+var contentPage = "http://mochi.test:8888/tests/toolkit/components/passwordmgr/test/privbrowsing_perwindowpb_iframe.html";
+var testWindows = [];
 
-  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
-  try {
-    _PBSvc = Cc["@mozilla.org/privatebrowsing;1"].
-             getService(Ci.nsIPrivateBrowsingService);
-    return _PBSvc;
-  } catch (e) {}
-  return null;
+function testOnWindow(aIsPrivate, aCallback) {
+  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
+  win.addEventListener("load", function onLoad() {
+    win.removeEventListener("load", onLoad, false);
+    win.addEventListener("DOMContentLoaded", function onInnerLoad() {
+      if (win.content.location.href == "about:privatebrowsing") {
+        win.gBrowser.loadURI(contentPage);
+        return;
+      }
+      win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
+      testWindows.push(win);
+      SimpleTest.executeSoon(function() { aCallback(win); });
+    }, true);
+    SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
+  }, true);
 }
 
-
 var ignoreLoad = false;
 function handleLoad(aEvent) {
-  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
-
   // ignore every other load event ... We get one for loading the subtest (which
   // we want to ignore), and another when the subtest's form submits itself
   // (which we want to handle, to start the next test).
   ignoreLoad = !ignoreLoad;
   if (ignoreLoad) {
     ok(true, "Ignoring load of subtest #" + testNum);
     return;
   }
   ok(true, "Processing submission of subtest #" + testNum);
 
   checkTest();
 
   if (testNum < subtests.length) {
     loadNextTest();
   } else {
     ok(true, "private browsing notification tests finished.");
-    prefBranch.clearUserPref("browser.privatebrowsing.keep_current_session");
+
+    testWindows.forEach(function(aWin) {
+      aWin.close();
+    });
+
     SimpleTest.finish();
   }
 }
 
+var pwmgr = Cc["@mozilla.org/login-manager;1"].
+            getService(Ci.nsILoginManager);
+ok(pwmgr != null, "Access pwmgr");
 
-var pb = get_PBSvc();
-if (!pb) { // Private Browsing might not be available
-  ok(true, "Private browsing service is not available");
-} else {
-  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
+// We need to make sure no logins have been stored by previous tests
+// for forms in |url|, otherwise the change password notification
+// would turn into a prompt, and the test will fail.
+var url = "http://test2.example.com";
+is(pwmgr.countLogins(url, "", null), 0, "No logins should be stored for " + url);
 
-  ok(Ci != null, "Access Ci");
-  ok(Cc != null, "Access Cc");
+var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
+                                             Ci.nsILoginInfo, "init");
+var login = new nsLoginInfo(url, url, null, "notifyu1", "notifyp1", "user", "pass");
+
+var normalWindow;
+var privateWindow;
 
-  var prefBranch = Cc["@mozilla.org/preferences-service;1"].
-                   getService(Ci.nsIPrefBranch);
-  prefBranch.setBoolPref("browser.privatebrowsing.keep_current_session", true);
+var iframe;
+var normalWindowIframe;
+var privateWindowIframe;
 
-  var pwmgr = Cc["@mozilla.org/login-manager;1"].
-              getService(Ci.nsILoginManager);
-  ok(pwmgr != null, "Access pwmgr");
+var popupNotifications;
+var normalWindowPopupNotifications;
+var privateWindowPopupNotifications;
 
-  // We need to make sure no logins have been stored by previous tests
-  // for forms in |url|, otherwise the change password notification
-  // would turn into a prompt, and the test will fail.
-  var url = "http://test2.example.com";
-  is(pwmgr.countLogins(url, "", null), 0, "No logins should be stored for " + url);
+testOnWindow(false, function(aWin) {
+  var selectedBrowser = aWin.gBrowser.selectedBrowser;
+  normalWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
+  normalWindowIframe.onload = handleLoad;
+  selectedBrowser.focus();
 
-  var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
-                                               Ci.nsILoginInfo, "init");
-  var login = new nsLoginInfo(url, url, null, "notifyu1", "notifyp1", "user", "pass");
+  normalWindowPopupNotifications = getPopupNotifications(selectedBrowser.contentWindow.top);
+  ok(normalWindowPopupNotifications, "Got popupNotifications in normal window");
+  // ignore the first load for this window;
+  ignoreLoad = false;
 
-  var iframe = document.getElementById("iframe");
-  iframe.onload = handleLoad;
+  testOnWindow(true, function(aPrivateWin) {
+    selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;
+    privateWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
+    privateWindowIframe.onload = handleLoad;
+    selectedBrowser.focus();
 
-  // popupNotifications (not *popup*) is a constant, per-tab container. So, we
-  // only need to fetch it once.
-  var popupNotifications = getPopupNotifications(window.top);
-  ok(popupNotifications, "Got popupNotifications");
+    privateWindowPopupNotifications = getPopupNotifications(selectedBrowser.contentWindow.top);
+    ok(privateWindowPopupNotifications, "Got popupNotifications in private window");
+    // ignore the first load for this window;
+    ignoreLoad = false;
 
-  SimpleTest.waitForExplicitFinish();
+    SimpleTest.executeSoon(loadNextTest);
+  });
+});
 
-  loadNextTest();
-}
-
-
+SimpleTest.waitForExplicitFinish();
 </script>
 </pre>
 </body>
 </html>