Bug 528452 - sessionstore tests that use setBrowserState should wait for sessionstore-browser-state-set (browser_394759.js), r=zeniko
authorMarco Bonardo <mbonardo@mozilla.com>
Sat, 14 Nov 2009 13:44:21 +0100
changeset 34859 fa212b6a9d72832a0c76ddf50c2bf4096570c264
parent 34858 65db9569a3f925c359cf71bd19ebacea7ca80078
child 34860 01adc20ea79218983e924f446908eb5fd996f853
push idunknown
push userunknown
push dateunknown
reviewerszeniko
bugs528452
milestone1.9.3a1pre
Bug 528452 - sessionstore tests that use setBrowserState should wait for sessionstore-browser-state-set (browser_394759.js), r=zeniko
browser/components/sessionstore/test/browser/browser_394759.js
--- a/browser/components/sessionstore/test/browser/browser_394759.js
+++ b/browser/components/sessionstore/test/browser/browser_394759.js
@@ -30,16 +30,31 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * 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 waitForBrowserState(aState, aSetStateCallback) {
+  let os = Cc["@mozilla.org/observer-service;1"].
+           getService(Ci.nsIObserverService);
+  let observer = {
+    observe: function(aSubject, aTopic, aData) {
+      os.removeObserver(this, "sessionstore-browser-state-restored");
+      executeSoon(aSetStateCallback);
+    }
+  };
+  os.addObserver(observer, "sessionstore-browser-state-restored", false);
+  let ss = Cc["@mozilla.org/browser/sessionstore;1"].
+           getService(Ci.nsISessionStore);
+  ss.setBrowserState(JSON.stringify(aState));
+}
+
 function test() {
   /** Test for Bug 394759 **/
   
   // test setup
   let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
   let pb = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
   waitForExplicitFinish();
   
@@ -82,36 +97,38 @@ function test() {
             let newWin2 = ss.undoCloseWindow(0);
 
             ok(newWin2 instanceof ChromeWindow,
                "undoCloseWindow actually returned a window");
             is(ss.getClosedWindowCount(), closedWindowCount,
                "The reopened window was removed from Recently Closed Windows");
 
             newWin2.addEventListener("load", function(aEvent) {
-              this.removeEventListener("load", arguments.callee, false);
+              newWin2.removeEventListener("load", arguments.callee, false);
               newWin2.gBrowser.addEventListener("SSTabRestored", function(aEvent) {
                 newWin2.gBrowser.removeEventListener("SSTabRestored", arguments.callee, true);
 
-                is(newWin2.gBrowser.tabContainer.childNodes.length, 2,
-                   "The window correctly restored 2 tabs");
-                is(newWin2.gBrowser.currentURI.spec, testURL,
-                   "The window correctly restored the URL");
+                executeSoon(function() {
+                  is(newWin2.gBrowser.tabContainer.childNodes.length, 2,
+                     "The window correctly restored 2 tabs");
+                  is(newWin2.gBrowser.currentURI.spec, testURL,
+                     "The window correctly restored the URL");
 
-                let textbox = newWin2.content.document.getElementById("textbox");
-                is(textbox.wrappedJSObject.value, uniqueText,
-                   "The window correctly restored the form");
-                is(ss.getWindowValue(newWin2, uniqueKey), uniqueValue,
-                   "The window correctly restored the data associated with it");
+                  let textbox = newWin2.content.document.getElementById("textbox");
+                  is(textbox.wrappedJSObject.value, uniqueText,
+                     "The window correctly restored the form");
+                  is(ss.getWindowValue(newWin2, uniqueKey), uniqueValue,
+                     "The window correctly restored the data associated with it");
 
-                // clean up
-                newWin2.close();
-                if (gPrefService.prefHasUserValue("browser.sessionstore.max_windows_undo"))
-                  gPrefService.clearUserPref("browser.sessionstore.max_windows_undo");
-                executeSoon(callback);
+                  // clean up
+                  newWin2.close();
+                  if (gPrefService.prefHasUserValue("browser.sessionstore.max_windows_undo"))
+                    gPrefService.clearUserPref("browser.sessionstore.max_windows_undo");
+                  executeSoon(callback);
+                });
               }, true);
             }, false);
           });
         });
       }, true);
     }, false);
   }
   
@@ -139,32 +156,32 @@ function test() {
       }
       // hack to force window to be considered a popup (toolbar=no didn't work)
       let winData = windowsToOpen.shift();
       let settings = "chrome,dialog=no," +
                      (winData.isPopup ? "all=no" : "all");
       let url = "http://window" + windowsToOpen.length + ".example.com";
       let window = openDialog(location, "_blank", settings, url);
       window.addEventListener("load", function(aEvent) {
-        this.removeEventListener("load", arguments.callee, true);
+        this.removeEventListener("load", arguments.callee, false);
         window.gBrowser.addEventListener("load", function(aEvent) {
           this.removeEventListener("load", arguments.callee, true);
           // the window _should_ have state with a tab of url, but it doesn't
           // always happend before window.close(). addTab ensure we don't treat
           // this window as a stateless window
           window.gBrowser.addTab();
 
           executeSoon(function() {
             window.close();
             executeSoon(function() {
               openWindowRec(windowsToOpen, expectedResults, recCallback);
             });
           });
         }, true);
-      }, true);
+      }, false);
     }
 
     let windowsToOpen = [{isPopup: false},
                          {isPopup: false},
                          {isPopup: true},
                          {isPopup: true},
                          {isPopup: true}];
     let expectedResults = {mac: {popup: 3, normal: 0},
@@ -184,18 +201,16 @@ function test() {
   function test_purge(callback) {
     // utility functions
     function countClosedTabsByTitle(aClosedTabList, aTitle)
       aClosedTabList.filter(function(aData) aData.title == aTitle).length;
 
     function countOpenTabsByTitle(aOpenTabList, aTitle)
       aOpenTabList.filter(function(aData) aData.entries.some(function(aEntry) aEntry.title == aTitle) ).length
 
-    // backup old state
-    let oldState = ss.getBrowserState();
     // create a new state for testing
     const REMEMBER = Date.now(), FORGET = Math.random();
     let testState = {
       windows: [ { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 } ],
       _closedWindows : [
         // _closedWindows[0]
         {
           tabs: [
@@ -244,53 +259,59 @@ function test() {
               title: REMEMBER
             }
           ]
         }
       ]
     };
     
     // set browser to test state
-    ss.setBrowserState(JSON.stringify(testState));
+    waitForBrowserState(testState, function() {
+      // purge domain & check that we purged correctly for closed windows
+      pb.removeDataFromDomain("mozilla.org");
 
-    // purge domain & check that we purged correctly for closed windows
-    pb.removeDataFromDomain("mozilla.org");
-
-    let closedWindowData = JSON.parse(ss.getClosedWindowData());
+      let closedWindowData = JSON.parse(ss.getClosedWindowData());
 
-    // First set of tests for _closedWindows[0] - tests basics
-    let window = closedWindowData[0];
-    is(window.tabs.length, 1, "1 tab was removed");
-    is(countOpenTabsByTitle(window.tabs, FORGET), 0,
-       "The correct tab was removed");
-    is(countOpenTabsByTitle(window.tabs, REMEMBER), 1,
-       "The correct tab was remembered");
-    is(window.selected, 1, "Selected tab has changed");
-    is(window.title, REMEMBER, "The window title was correctly updated");
+      // First set of tests for _closedWindows[0] - tests basics
+      let window = closedWindowData[0];
+      is(window.tabs.length, 1, "1 tab was removed");
+      is(countOpenTabsByTitle(window.tabs, FORGET), 0,
+         "The correct tab was removed");
+      is(countOpenTabsByTitle(window.tabs, REMEMBER), 1,
+         "The correct tab was remembered");
+      is(window.selected, 1, "Selected tab has changed");
+      is(window.title, REMEMBER, "The window title was correctly updated");
 
-    // Test more complicated case 
-    window = closedWindowData[1];
-    is(window.tabs.length, 3, "2 tabs were removed");
-    is(countOpenTabsByTitle(window.tabs, FORGET), 0,
-       "The correct tabs were removed");
-    is(countOpenTabsByTitle(window.tabs, REMEMBER), 3,
-       "The correct tabs were remembered");
-    is(window.selected, 3, "Selected tab has changed");
-    is(window.title, REMEMBER, "The window title was correctly updated");
+      // Test more complicated case 
+      window = closedWindowData[1];
+      is(window.tabs.length, 3, "2 tabs were removed");
+      is(countOpenTabsByTitle(window.tabs, FORGET), 0,
+         "The correct tabs were removed");
+      is(countOpenTabsByTitle(window.tabs, REMEMBER), 3,
+         "The correct tabs were remembered");
+      is(window.selected, 3, "Selected tab has changed");
+      is(window.title, REMEMBER, "The window title was correctly updated");
 
-    // Tests handling of _closedTabs
-    window = closedWindowData[2];
-    is(countClosedTabsByTitle(window._closedTabs, REMEMBER), 1,
-       "The correct number of tabs were removed, and the correct ones");
-    is(countClosedTabsByTitle(window._closedTabs, FORGET), 0,
-       "All tabs to be forgotten were indeed removed");
+      // Tests handling of _closedTabs
+      window = closedWindowData[2];
+      is(countClosedTabsByTitle(window._closedTabs, REMEMBER), 1,
+         "The correct number of tabs were removed, and the correct ones");
+      is(countClosedTabsByTitle(window._closedTabs, FORGET), 0,
+         "All tabs to be forgotten were indeed removed");
 
-    // restore pre-test state
-    ss.setBrowserState(oldState);
-    executeSoon(callback);
+      // Restore blank state.
+      let blankState = {
+        windows: [{
+          tabs: [{ entries: [{ url: "about:blank" }] }],
+          _closedTabs: []
+        }],
+        _closedWindows: []
+      };
+      waitForBrowserState(blankState, callback);
+    });
   }
   
   test_basic(function() {
     test_behavior(function() {
       test_purge(function() {
         finish();
       });
     });