Bug 528452 - sessionstore tests that use setBrowserState should wait for sessionstore-browser-state-set (browser_526613.js), r=zeniko
☠☠ backed out by 0d98ff9aef32 ☠ ☠
authorMarco Bonardo <mbonardo@mozilla.com>
Sat, 14 Nov 2009 13:44:24 +0100
changeset 34860 01adc20ea79218983e924f446908eb5fd996f853
parent 34859 fa212b6a9d72832a0c76ddf50c2bf4096570c264
child 34861 4f62c9d9495714c0370b503ebb14726112413614
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_526613.js), r=zeniko
browser/components/sessionstore/test/browser/browser_526613.js
--- a/browser/components/sessionstore/test/browser/browser_526613.js
+++ b/browser/components/sessionstore/test/browser/browser_526613.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 526613 **/
   
   // test setup
   let ss = Cc["@mozilla.org/browser/sessionstore;1"].
            getService(Ci.nsISessionStore);
   let os = Cc["@mozilla.org/observer-service;1"].
            getService(Ci.nsIObserverService);
@@ -55,54 +70,47 @@ function test() {
         ++count;
     }
 
     return count;
   }
 
   is(browserWindowsCount(), 1, "Only one browser window should be open initially");
 
-  // backup old state
-  let oldState = ss.getBrowserState();
+  let blankState = {
+    windows: [{
+      tabs: [{ entries: [{ url: "about:blank" }] }],
+      _closedTabs: []
+    }],
+    _closedWindows: []
+  };
+
   // create a new state for testing
   let testState = {
     windows: [
       { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 },
       { tabs: [{ entries: [{ url: "about:robots"        }] }], selected: 1 },
     ],
     // make sure the first window is focused, otherwise when restoring the
     // old state, the first window is closed and the test harness gets unloaded
     selectedWindow: 1
   };
 
-  let observer = {
-    pass: 1,
-    observe: function(aSubject, aTopic, aData) {
-      is(aTopic, "sessionstore-browser-state-restored",
-         "The sessionstore-browser-state-restored notification was observed");
-
-      if (this.pass++ == 1) {  
-        is(browserWindowsCount(), 2, "Two windows should exist at this point");
+  waitForBrowserState(testState, function() {
+    is(browserWindowsCount(), 2, "Two windows should exist at this point");
 
-        // let the first window be focused (see above)
-        function pollMostRecentWindow() {
-          if (wm.getMostRecentWindow("navigator:browser") == window) {
-            ss.setBrowserState(oldState);
-          } else {
-            info("waiting for the current window to become active");
-            setTimeout(pollMostRecentWindow, 0);
-          }
-        }
-        pollMostRecentWindow();
+    // let the first window be focused (see above)
+    function pollMostRecentWindow() {
+      if (wm.getMostRecentWindow("navigator:browser") == window) {
+        waitForBrowserState(blankState, function() {
+          is(browserWindowsCount(), 1, "Only one window should exist after cleanup");
+          ok(!window.closed, "Restoring the old state should have left this window open");
+          finish();
+        });
       }
       else {
-        is(browserWindowsCount(), 1, "Only one window should exist after cleanup");
-        ok(!window.closed, "Restoring the old state should have left this window open");
-        os.removeObserver(this, "sessionstore-browser-state-restored");
-        finish();
+        info("waiting for the current window to become active");
+        setTimeout(pollMostRecentWindow, 0);
       }
     }
-  };
-  os.addObserver(observer, "sessionstore-browser-state-restored", false);
-
-  // set browser to test state
-  ss.setBrowserState(JSON.stringify(testState));
+    pollMostRecentWindow();
+  });
 }