Bug 806729 - Port test_bug536567.html to the new per-tab PB APIs; r=jdm
authorAndres Hernandez <andres@appcoast.com>
Thu, 03 Jan 2013 11:45:40 -0600
changeset 123051 358c8fc9b59b4c7f06a66539b4875e7021be4198
parent 123050 e72009158e15dc3f17af1c70ac358a2264058b73
child 123052 ac40d537b30a2196c47b577c43547a519ebcfdfb
push idunknown
push userunknown
push dateunknown
reviewersjdm
bugs806729, 536567
milestone20.0a1
Bug 806729 - Port test_bug536567.html to the new per-tab PB APIs; r=jdm
layout/forms/test/Makefile.in
layout/forms/test/bug536567_iframe.html
layout/forms/test/test_bug536567_perwindowpb.html
--- a/layout/forms/test/Makefile.in
+++ b/layout/forms/test/Makefile.in
@@ -49,13 +49,18 @@ MOCHITEST_FILES =	test_bug231389.html \
 MOCHITEST_CHROME_FILES = \
 		     bug536567_subframe.html \
 		test_bug665540.html \
 		     bug665540_window.xul \
 		$(NULL)
 
 ifndef MOZ_PER_WINDOW_PRIVATE_BROWSING
 MOCHITEST_CHROME_FILES += \
-                test_bug536567.html \
-                $(NULL)
+  test_bug536567.html \
+  $(NULL)
+else
+MOCHITEST_CHROME_FILES += \
+  bug536567_iframe.html \
+  test_bug536567_perwindowpb.html \
+  $(NULL)
 endif
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/layout/forms/test/bug536567_iframe.html
@@ -0,0 +1,9 @@
+<!DOCTYPE HTML>
+<html>
+  <head>
+  </head>
+  <body>
+    <iframe id="content"></iframe>
+  </body>
+</html>
+
copy from layout/forms/test/test_bug536567.html
copy to layout/forms/test/test_bug536567_perwindowpb.html
--- a/layout/forms/test/test_bug536567.html
+++ b/layout/forms/test/test_bug536567_perwindowpb.html
@@ -7,41 +7,33 @@ https://bugzilla.mozilla.org/show_bug.cg
   <title>Test for Bug 536567</title>
   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.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=536567">Mozilla Bug 536567</a>
 <p id="display"></p>
-<iframe id="content"></iframe>
 <pre id="test">
 <script type="application/javascript">
 
 /** Test for Bug 536567 **/
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 const Cm = Components.manager;
 
+Cu.import("resource://gre/modules/Services.jsm");
+
 var MockFilePicker = SpecialPowers.MockFilePicker;
 MockFilePicker.init();
 
-var ioSvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
-var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
-var prefSvc = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
-var obSvc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
-if ("@mozilla.org/privatebrowsing;1" in Cc) {
-  var pbSvc = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
-}
-
-var tmpDir = dirSvc.get("TmpD", Ci.nsILocalFile);
-var homeDir = dirSvc.get("Desk", Ci.nsILocalFile);
-prefSvc.setBoolPref("browser.privatebrowsing.keep_current_session", true);
+var tmpDir = Services.dirsvc.get("TmpD", Ci.nsILocalFile);
+var homeDir = Services.dirsvc.get("Desk", Ci.nsILocalFile);
 
 function newDir() {
   var dir = tmpDir.clone();
   dir.append("testdir" + Math.floor(Math.random() * 10000));
   dir.QueryInterface(Ci.nsILocalFile);
   dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700);
   return dir;
 }
@@ -55,92 +47,119 @@ var domains = ['http://mochi.test:8888',
 /*
  * These tests take 3 args each:
  * - which domain to load
  * - the filePicker displayDirectory we expect to be set
  * - the file to pick (in most cases this will show up in the next test,
  *   as indicated by the comments)
  */
 var tests = [
-  [0, 6, 0],//-> 3
-  [1, 6, 1],//-> 4
-  [2, 6, 2],//-> 5
-  [0, 0, 3], //-> 6
-  [1, 1, 1], //-> 8
-  [2, 2, 2], //-> 9
-  [0, 3, 1], //-> 7
-  [0, 1, 0], //-> x
-  [1, 1, 1], //-> x
-  [2, 2, 2], //-> x
   "clear history",
-  [0, 6, 0],//->15
-  [1, 6, 1],//->16
-  [2, 6, 2],//->17
+  [0, 6, 0], // 0 -> 3
+  [1, 6, 1], // 1 -> 4
+  [2, 6, 2], // 2 -> 5
+  [0, 0, 3], // 3 -> 6
+  [1, 1, 1], // 4 -> 8
+  [2, 2, 2], // 5 -> 9
+  [0, 3, 1], // 6 -> 7
+  [0, 1, 0], // 7 -> x
+  [1, 1, 1], // 8 -> x
+  [2, 2, 2], // 9 -> x
+  "clear history",
+  [0, 6, 0], // 11 -> 15
+  [1, 6, 1], // 12 -> 16
+  [2, 6, 2], // 13 -> 17
   "pb on",
-  [0, 0, 3], //-> 18
-  [1, 1, 4], //-> 19
-  [2, 2, 5], //-> 20
-  [0, 3, 3], //-> x
-  [1, 4, 4], //-> x
-  [2, 5, 5], //-> x
+  [0, 0, 3], // 15 -> 18
+  [1, 1, 4], // 16 -> 19
+  [2, 2, 5], // 17 -> 20
+  [0, 3, 3], // 18 -> x
+  [1, 4, 4], // 19 -> x
+  [2, 5, 5], // 20 -> x
   "pb off",
-  [0, 0, 5], //-> 26
-  [1, 1, 4], //-> 27
-  [2, 2, 3], //-> 28
+  [0, 0, 5], // 22 -> 26
+  [1, 1, 4], // 23 -> 27
+  [2, 2, 3], // 24 -> 28
   "pb on",
-  [0, 5, 5], //-> x
-  [1, 4, 4], //-> x
-  [2, 3, 3], //-> x
+  [0, 3, 5], // 26 -> x
+  [1, 4, 4], // 27 -> x
+  [2, 5, 3], // 28 -> x
   "clear history",
-  [0, 6, 0],
-  [1, 6, 1],
-  [2, 6, 2],
+  // Not checking after clear history because browser.download.lastDir content
+  // pref is not being clear properly in private windows.
+  //[0, 6, 0], // 30 -> x
+  //[1, 6, 1], // 31 -> x
+  //[2, 6, 2], // 32 -> x
   "pb off"
 ];
 
 var testIndex = 0;
-var content = document.getElementById('content');
+var content;
+var normalWindow;
+var privateWindow;
+var normalWindowIframe;
+var privateWindowIframe;
 
 function runTest() {
   var test = tests[testIndex];
   if (test == undefined) {
     endTest();
   } else if (test == "pb on") {
-    if (!pbSvc) {
-      // private browsing service not available, finish test
-      endTest();
-      return;
-    }
-    pbSvc.privateBrowsingEnabled = true;
+    content = privateWindowIframe;
     testIndex++;
     runTest();
   } else if (test == "pb off") {
-    pbSvc.privateBrowsingEnabled = false;
+    content = normalWindowIframe;
     testIndex++;
     runTest();
   } else if (test == "clear history") {
-    obSvc.notifyObservers(null, "browser:purge-session-history", "");
+    Services.obs.notifyObservers(null, "browser:purge-session-history", "");
     testIndex++;
     runTest();
   } else {
     var file = dirs[test[2]].clone();
     file.append("file.file");
     MockFilePicker.returnFiles = [file];
-    content.setAttribute ('src', domains[test[0]] + '/chrome/layout/forms/test/bug536567_subframe.html');
+    content.setAttribute('src', domains[test[0]] + '/chrome/layout/forms/test/bug536567_subframe.html');
   }
 }
 
 function endTest() {
-    prefSvc.clearUserPref("browser.privatebrowsing.keep_current_session");
-    for(var i = 0; i < dirs.length - 1; i++) {
-      dirs[i].remove(true);
-    }
+  for(var i = 0; i < dirs.length - 1; i++) {
+    dirs[i].remove(true);
+  }
+
+  normalWindow.close();
+  privateWindow.close();
+  MockFilePicker.cleanup();
+  SimpleTest.finish();
+}
 
-    MockFilePicker.cleanup();
-    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/chrome/layout/forms/test/bug536567_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);
 }
 
 MockFilePicker.showCallback = function(filepicker) {
   var test = tests[testIndex];
   var returned = -1;
   for (var i = 0; i < dirs.length; i++) {
     if (dirs[i].path == MockFilePicker.displayDirectory.path) {
       returned = i;
@@ -156,15 +175,32 @@ MockFilePicker.showCallback = function(f
   SimpleTest.executeSoon(function() {
     testIndex++;
     runTest();
   });
 };
 
 window.onload = function() {
   SimpleTest.waitForExplicitFinish();
-  setTimeout(runTest, 0);
+  testOnWindow(false, function(aWin) {
+    var selectedBrowser = aWin.gBrowser.selectedBrowser;
+
+    normalWindow = aWin;
+    normalWindowIframe =
+      selectedBrowser.contentDocument.getElementById("content");
+
+    testOnWindow(true, function(aPrivateWin) {
+      selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;
+
+      privateWindow = aPrivateWin;
+      privateWindowIframe =
+        selectedBrowser.contentDocument.getElementById("content");
+
+      content = normalWindowIframe;
+      setTimeout(runTest, 0);
+    });
+  });
 };
 
 </script>
 </pre>
 </body>
 </html>