Bug 806741 - Port test_bug_461710.html to the new per-tab PB APIs. r=jdm
authorRaymond Lee <raymond@raysquare.com>
Wed, 19 Dec 2012 11:47:41 +0800
changeset 116575 87ad27a8a7de8da408901f3772a2e1b0d1bc2d77
parent 116574 3e9f8f3605bbc4b068f54460486cbe14eed41301
child 116576 3dcf9f0a42d8738c1150dbeda142d934cd677a68
push idunknown
push userunknown
push dateunknown
reviewersjdm
bugs806741
milestone20.0a1
Bug 806741 - Port test_bug_461710.html to the new per-tab PB APIs. r=jdm
toolkit/components/places/tests/Makefile.in
toolkit/components/places/tests/mochitest/bug_461710/Makefile.in
toolkit/components/places/tests/mochitest/bug_461710/iframe.html
toolkit/components/places/tests/mochitest/test_bug_461710_perwindowpb.html
--- a/toolkit/components/places/tests/Makefile.in
+++ b/toolkit/components/places/tests/Makefile.in
@@ -34,16 +34,20 @@ XPCSHELL_TESTS_COMMON = \
 MOCHITEST_FILES = \
 	mochitest/test_bug_411966.html \
 	$(NULL)
 
 ifndef MOZ_PER_WINDOW_PRIVATE_BROWSING
 MOCHITEST_FILES += \
 	mochitest/test_bug_461710.html \
 	$(NULL)
+else
+MOCHITEST_CHROME_FILES = \
+	mochitest/test_bug_461710_perwindowpb.html \
+	$(NULL)
 endif
 
 DIRS = \
 	chrome \
 	mochitest/bug_411966 \
 	mochitest/bug_461710 \
 	browser \
 	$(NULL)
--- a/toolkit/components/places/tests/mochitest/bug_461710/Makefile.in
+++ b/toolkit/components/places/tests/mochitest/bug_461710/Makefile.in
@@ -11,11 +11,12 @@ relativesrcdir = toolkit/components/plac
 
 include $(DEPTH)/config/autoconf.mk
 
 MOCHITEST_FILES	= \
 		visited_page.html \
 		link_page.html \
 		link_page-2.html \
 		link_page-3.html \
+		iframe.html \
 		$(NULL)
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/mochitest/bug_461710/iframe.html
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML>
+<html>
+  <head>
+  </head>
+  <body>
+    <iframe id="iframe"></iframe>
+  </body>
+</html>
copy from toolkit/components/places/tests/mochitest/test_bug_461710.html
copy to toolkit/components/places/tests/mochitest/test_bug_461710_perwindowpb.html
--- a/toolkit/components/places/tests/mochitest/test_bug_461710.html
+++ b/toolkit/components/places/tests/mochitest/test_bug_461710_perwindowpb.html
@@ -1,37 +1,36 @@
 <!DOCTYPE HTML>
 <html>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=461710
 -->
 <head>
   <title>Test for Bug 461710</title>
-  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=461710">Mozilla Bug 461710</a>
 <p id="display"></p>
-<iframe id="iframe"></iframe>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 /** Test for Bug 461710 **/
 
 SimpleTest.waitForExplicitFinish();
 
 const Ci = SpecialPowers.Ci;
 const Cc = SpecialPowers.Cc;
 const Cr = SpecialPowers.Cr;
 
 SpecialPowers.Cu.import("resource://gre/modules/NetUtil.jsm", window);
 var Services = SpecialPowers.Services;
 
-var gIframe = document.getElementById("iframe");
+var gIframe;
 
 /**
  * Helper function which waits until another function returns true, and
  * then notifies a callback.
  *
  * Original function stolen from docshell/test/chrome/docshell_helpers.js.
  *
  * Parameters:
@@ -56,45 +55,42 @@ function waitForTrue(fn, onWaitComplete)
           onWaitComplete.call();
         }
       }, 20);
 }
 
 const kRed = "rgb(255, 0, 0)";
 const kBlue = "rgb(0, 0, 255)";
 
-var testpath = document.location.pathname + "/../bug_461710/";
+var testpath = "/tests/toolkit/components/places/tests/mochitest/../bug_461710/";
 var prefix = "http://mochi.test:8888" + testpath;
 var subtests = [
                    "visited_page.html",   // 1
                    "link_page.html",      // 2
                    "link_page-2.html",    // 3
                    "link_page-3.html"     // 4
                ];
 
 var testNum = 0;
 function loadNextTest() {
   // run the initialization code for each test
   switch (++testNum) {
     case 1:
-      // nothing to do here
+      gIframe = normalWindowIframe;
       break;
 
     case 2:
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
       break;
 
     case 3:
-      pb.privateBrowsingEnabled = true;
-      ok(pb.privateBrowsingEnabled, "Test #" + testNum + " should be run inside of private mode");
+      gIframe = privateWindowIframe;
       break;
 
     case 4:
-      pb.privateBrowsingEnabled = false;
-      ok(!pb.privateBrowsingEnabled, "Test #" + testNum + " should be run outside of private mode");
+      gIframe = normalWindowIframe;
       break;
 
     default:
       ok(false, "Unexpected call to loadNextTest for test #" + testNum);
   }
 
   if (testNum == 1)
     observer.expectURL(prefix + subtests[0], "uri-visit-saved");
@@ -143,44 +139,54 @@ function checkTest() {
       is(getColor(doc, win, "link"), kRed, "Visited link coloring should work outside of private mode");
       break;
 
     default:
       ok(false, "Unexpected call to checkTest for test #" + testNum);
   }
 }
 
-
-var _PBSvc = null;
-function get_PBSvc() {
-  if (_PBSvc)
-    return _PBSvc;
-
-  try {
-    _PBSvc = Cc["@mozilla.org/privatebrowsing;1"].
-             getService(Ci.nsIPrivateBrowsingService);
-    return _PBSvc;
-  } catch (e) {}
-  return null;
-}
-
-
 function handleLoad() {
   checkTest();
 
   if (testNum < subtests.length) {
     loadNextTest();
   } else {
-    try {
-      Services.prefs.clearUserPref("browser.privatebrowsing.keep_current_session");
-    } catch(ex) {}
+    normalWindow.close();
+    privateWindow.close();
+
     SimpleTest.finish();
   }
 }
 
+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/places/tests/mochitest/../bug_461710/iframe.html";
+
+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);
+      win.gBrowser.selectedBrowser.focus();
+      SimpleTest.executeSoon(function() { aCallback(win); });
+    }, true);
+    SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
+  }, true);
+}
+
 const URI_VISITED_RESOLUTION_TOPIC = "visited-status-resolution";
 var observer = {
   uri: null,
   resolved: true,
   observe: function (aSubject, aTopic, aData) {
 
     if (this.uri.equals(SpecialPowers.wrap(aSubject).QueryInterface(Ci.nsIURI))) {
       this.resolved = true;
@@ -193,21 +199,34 @@ var observer = {
     this.resolved = false;
 
     this.uri = SpecialPowers.wrap(NetUtil).newURI(url);
     var topic = aOverrideTopic || URI_VISITED_RESOLUTION_TOPIC;
     Services.obs.addObserver(this, topic, false);
   }
 };
 
-var pb = get_PBSvc();
-if (!pb) { // Private Browsing might not be available
-  ok(true, "Private Browsing is not available");
-  SimpleTest.finish();
-} else {
-  Services.prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true);
-  loadNextTest();
-}
+var normalWindow;
+var privateWindow;
+
+var normalWindowIframe;
+var privateWindowIframe;
+
+testOnWindow(false, function(aWin) {
+  var selectedBrowser = aWin.gBrowser.selectedBrowser;
+
+   normalWindow = aWin;
+   normalWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
+
+  testOnWindow(true, function(aPrivateWin) {
+    selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;
+
+    privateWindow = aPrivateWin;
+    privateWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
+
+    loadNextTest();
+  });
+});
 
 </script>
 </pre>
 </body>
 </html>