Bug 518970 - New experimental rewrite of browser_394759_privatebrowsing.js (constant random failure)
authorMarco Bonardo <mbonardo@mozilla.com>
Sat, 17 Oct 2009 14:37:42 +0200
changeset 33984 5ae7b09f730f9dc9ee364755b588e8028a7a24a0
parent 33983 6240e80d2c21252e5599fce5279036689fe2322a
child 33985 d2ac46221368caa82fc65931677b441dfcde5a8e
push idunknown
push userunknown
push dateunknown
bugs518970
milestone1.9.3a1pre
Bug 518970 - New experimental rewrite of browser_394759_privatebrowsing.js (constant random failure)
browser/components/sessionstore/test/browser/browser_394759_privatebrowsing.js
--- a/browser/components/sessionstore/test/browser/browser_394759_privatebrowsing.js
+++ b/browser/components/sessionstore/test/browser/browser_394759_privatebrowsing.js
@@ -16,16 +16,17 @@
  * The Initial Developer of the Original Code is
  * Aaron Train <aaron.train@gmail.com>.
  * Portions created by the Initial Developer are Copyright (C) 2008
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Ehsan Akhgari <ehsan.akhgari@gmail.com>
  *   Paul O’Shannessy <paul@oshannessy.com>
+ *   Marco Bonardo <mak77@bonardo.net>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -34,30 +35,30 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 function test() {
   /** Private Browsing Test for Bug 394759 **/
 
-  // test setup
   waitForExplicitFinish();
 
-  // private browsing service
+  // Setup.
   let pb = Cc["@mozilla.org/privatebrowsing;1"].
            getService(Ci.nsIPrivateBrowsingService);
+  let ss = Cc["@mozilla.org/browser/sessionstore;1"].
+           getService(Ci.nsISessionStore);
+  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
+           getService(Ci.nsIWindowWatcher);
+
+  // Remove the sessionstore.js file before setting the interval to 0
   let profilePath = Cc["@mozilla.org/file/directory_service;1"].
                     getService(Ci.nsIProperties).
                     get("ProfD", Ci.nsIFile);
-
-  // sessionstore service
-  let ss = Cc["@mozilla.org/browser/sessionstore;1"].
-           getService(Ci.nsISessionStore);
-  // Remove the sessionstore.js file before setting the interval to 0
   let sessionStoreJS = profilePath.clone();
   sessionStoreJS.append("sessionstore.js");
   if (sessionStoreJS.exists())
     sessionStoreJS.remove(false);
   ok(sessionStoreJS.exists() == false, "sessionstore.js was removed");
   // Make sure that sessionstore.js can be forced to be created by setting
   // the interval pref to 0
   gPrefService.setIntPref("browser.sessionstore.interval", 0);
@@ -74,98 +75,103 @@ function test() {
     }],
     _closedWindows: []
   });
   ss.setBrowserState(blankState);
 
   let closedWindowCount = ss.getClosedWindowCount();
   is(closedWindowCount, 0, "Correctly set window count");
 
-  let testURL_A = "about:config";
-  let testURL_B = "about:mozilla";
+  // Prevent VM timers issues, cache now and increment it manually.
+  const NOW = Date.now();
 
-  let uniqueKey_A = "bug 394759 Non-PB";
-  let uniqueValue_A = "unik" + Date.now();
-  let uniqueKey_B = "bug 394759 PB";
-  let uniqueValue_B = "uniq" + Date.now();
-
+  const TESTS = [
+    { url: "about:config",
+      key: "bug 394759 Non-PB",
+      value: "uniq" + (++NOW) },
+    { url: "about:mozilla",
+      key: "bug 394759 PB",
+      value: "uniq" + (++NOW) },
+  ];
 
-  // Open a window
-  let newWin = openDialog(location, "_blank", "chrome,all,dialog=no", testURL_A);
-  newWin.addEventListener("load", function(aEvent) {
-    newWin.removeEventListener("load", arguments.callee, false);
-    newWin.gBrowser.addEventListener("load", function(aEvent) {
-      newWin.gBrowser.removeEventListener("load", arguments.callee, true);
-      info("Window has been loaded");
-      executeSoon(function() {
-        newWin.gBrowser.addTab();
-        executeSoon(function() {
-          // mark the window with some unique data to be restored later on
-          ss.setWindowValue(newWin, uniqueKey_A, uniqueValue_A);
+  function openWindowAndTest(aTestIndex, aRunNextTestInPBMode) {
+    info("Opening new window");
+    let windowObserver = {
+      observe: function(aSubject, aTopic, aData) {
+        if (aTopic === "domwindowopened") {
+          ww.unregisterNotification(this);
+          info("New window has been opened");
+          let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
+          win.addEventListener("load", function onLoad(event) {
+            win.removeEventListener("load", onLoad, false);
+            info("New window has been loaded");
+            win.gBrowser.addEventListener("load", function(aEvent) {
+              win.gBrowser.removeEventListener("load", arguments.callee, true);
+              info("New window browser has been loaded");
+              executeSoon(function() {
+                // Add a tab.
+                win.gBrowser.addTab();
 
-          newWin.close();
-
-          // ensure that we incremented # of close windows
-          is(ss.getClosedWindowCount(), closedWindowCount + 1,
-             "The closed window was added to the list");
+                executeSoon(function() {
+                  // Mark the window with some unique data to be restored later on.
+                  ss.setWindowValue(win, TESTS[aTestIndex].key, TESTS[aTestIndex].value);
 
-          // ensure we added window to undo list
-          let data = JSON.parse(ss.getClosedWindowData())[0];
-          ok(data.toSource().indexOf(uniqueValue_A) > -1,
-             "The closed window data was stored correctly");
+                  win.close();
 
-          // enter private browsing mode
-          pb.privateBrowsingEnabled = true;
-          ok(pb.privateBrowsingEnabled, "private browsing enabled");
+                  // Ensure that we incremented # of close windows.
+                  is(ss.getClosedWindowCount(), closedWindowCount + 1,
+                     "The closed window was added to the list");
 
-          // ensure that we have 0 undo windows when entering PB
-          is(ss.getClosedWindowCount(), 0,
-             "Recently Closed Windows are removed when entering Private Browsing");
-          is(ss.getClosedWindowData(), "[]",
-             "Recently Closed Windows data is cleared when entering Private Browsing");
+                  // Ensure we added window to undo list.
+                  let data = JSON.parse(ss.getClosedWindowData())[0];
+                  ok(data.toSource().indexOf(TESTS[aTestIndex].value) > -1,
+                     "The closed window data was stored correctly");
+
+                  if (aRunNextTestInPBMode) {
+                    // Enter private browsing mode.
+                    pb.privateBrowsingEnabled = true;
+                    ok(pb.privateBrowsingEnabled, "private browsing enabled");
 
-          // open another window in PB
-          let pbWin = openDialog(location, "_blank", "chrome,all,dialog=no", testURL_B);
-          pbWin.addEventListener("load", function(aEvent) {
-            pbWin.removeEventListener("load", arguments.callee, false);
-            pbWin.gBrowser.addEventListener("load", function(aEvent) {
-              pbWin.gBrowser.removeEventListener("load", arguments.callee, true);
-
-              executeSoon(function() {
-                // Add another tab, though it's not strictly needed
-                pbWin.gBrowser.addTab();
-                executeSoon(function() {
-                  // mark the window with some unique data to be restored later on
-                  ss.setWindowValue(pbWin, uniqueKey_B, uniqueValue_B);
-
-                  pbWin.close();
+                    // Ensure that we have 0 undo windows when entering PB.
+                    is(ss.getClosedWindowCount(), 0,
+                       "Recently Closed Windows are removed when entering Private Browsing");
+                    is(ss.getClosedWindowData(), "[]",
+                       "Recently Closed Windows data is cleared when entering Private Browsing");
+                  }
+                  else {
+                    // Exit private browsing mode.
+                    pb.privateBrowsingEnabled = false;
+                    ok(!pb.privateBrowsingEnabled, "private browsing disabled");
 
-                  // ensure we added window to undo list
-                  let data = JSON.parse(ss.getClosedWindowData())[0];
-                  ok(data.toSource().indexOf(uniqueValue_B) > -1,
-                     "The closed window data was stored correctly in PB mode");
-
-                  // exit private browsing mode
-                  pb.privateBrowsingEnabled = false;
-                  ok(!pb.privateBrowsingEnabled, "private browsing disabled");
+                    // Ensure that we still have the closed windows from before.
+                    is(ss.getClosedWindowCount(), closedWindowCount + 1,
+                       "The correct number of recently closed windows were restored " +
+                       "when exiting PB mode");
 
-                  // ensure that we still have the closed windows from before
-                  is(ss.getClosedWindowCount(), closedWindowCount + 1,
-                     "The correct number of recently closed windows were restored " +
-                     "when exiting PB mode");
+                    let data = JSON.parse(ss.getClosedWindowData())[0];
+                    ok(data.toSource().indexOf(TESTS[aTestIndex].value) > -1,
+                       "The data associated with the recently closed window was " +
+                       "restored when exiting PB mode");
+                  }
 
-                  let data = JSON.parse(ss.getClosedWindowData())[0];
-                  ok(data.toSource().indexOf(uniqueValue_A) > -1,
-                     "The data associated with the recently closed window was " +
-                     "restored when exiting PB mode");
-
-                  // cleanup
-                  gPrefService.clearUserPref("browser.sessionstore.interval");
-                  finish();
+                  if (aTestIndex == TESTS.length - 1) {
+                    // Cleanup and finish.
+                    gPrefService.clearUserPref("browser.sessionstore.interval");
+                    finish();
+                  }
+                  else {
+                    // Run next test.
+                    openWindowAndTest(aTestIndex + 1, !aRunNextTestInPBMode);
+                  }
                 });
               });
             }, true);
           }, false);
-        });
-      });
-    }, true);
-  }, false);
+        }
+      }
+    };
+    ww.registerNotification(windowObserver);
+    // Open a window.
+    openDialog(location, "_blank", "chrome,all,dialog=no", TESTS[aTestIndex].url);
+  }
+
+  openWindowAndTest(0, true);
 }