Bug 1003096 - Make browser_tabview_bug595601.js wait until the test session is restored r=smacleod
authorTim Taubert <ttaubert@mozilla.com>
Wed, 30 Apr 2014 12:30:56 +0200
changeset 181495 e057952237dc616077a3ac2afafbcbca7490b199
parent 181494 f55a3989aee0584638d7c593bfa57092e0b04a40
child 181496 27c4e67174a21ea2f2e70cf84e3e55f195c9f327
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewerssmacleod
bugs1003096, 595601
milestone32.0a1
Bug 1003096 - Make browser_tabview_bug595601.js wait until the test session is restored r=smacleod
browser/components/tabview/test/browser_tabview_bug595601.js
--- a/browser/components/tabview/test/browser_tabview_bug595601.js
+++ b/browser/components/tabview/test/browser_tabview_bug595601.js
@@ -42,52 +42,32 @@ function test() {
   });
 
   TabView._initFrame(function () {
     executeSoon(testRestoreWithHiddenTabs);
   });
 }
 
 function testRestoreWithHiddenTabs() {
-  let checked = false;
-  let ssReady = false;
-  let tabsRestored = false;
+  TabsProgressListener.setCallback(function (needsRestore, isRestoring) {
+    if (needsRestore <= 4) {
+      TabsProgressListener.unsetCallback();
+      is(needsRestore, 4, "4/8 tabs restored");
+    }
+  });
 
-  let check = function () {
-    if (checked || !ssReady || !tabsRestored)
-      return;
-
-    checked = true;
-
+  waitForBrowserState(state, 4, function () {
     is(gBrowser.tabs.length, 8, "there are now eight tabs");
     is(gBrowser.visibleTabs.length, 4, "four visible tabs");
 
     let cw = TabView.getContentWindow();
     is(cw.GroupItems.groupItems.length, 2, "there are now two groupItems");
 
     testSwitchToInactiveGroup();
-  }
-
-  whenSessionStoreReady(function () {
-    ssReady = true;
-    check();
   });
-
-  TabsProgressListener.setCallback(function (needsRestore, isRestoring) {
-    if (4 < needsRestore)
-      return;
-
-    TabsProgressListener.unsetCallback();
-    is(needsRestore, 4, "4/8 tabs restored");
-
-    tabsRestored = true;
-    check();
-  });
-
-  ss.setBrowserState(JSON.stringify(state));
 }
 
 function testSwitchToInactiveGroup() {
   let firstProgress = true;
 
   TabsProgressListener.setCallback(function (needsRestore, isRestoring) {
     if (firstProgress) {
       firstProgress = false;
@@ -103,21 +83,26 @@ function testSwitchToInactiveGroup() {
 
     is(gBrowser.visibleTabs.length, 4, "four visible tabs");
     waitForFocus(finish);
   });
 
   gBrowser.selectedTab = gBrowser.tabs[4];
 }
 
-function whenSessionStoreReady(callback) {
-  window.addEventListener("SSWindowStateReady", function onReady() {
-    window.removeEventListener("SSWindowStateReady", onReady, false);
-    executeSoon(callback);
-  }, false);
+function waitForBrowserState(state, numTabs, callback) {
+  let tabContainer = gBrowser.tabContainer;
+  tabContainer.addEventListener("SSTabRestored", function onRestored() {
+    if (--numTabs <= 0) {
+      tabContainer.removeEventListener("SSTabRestored", onRestored, true);
+      executeSoon(callback);
+    }
+  }, true);
+
+  ss.setBrowserState(JSON.stringify(state));
 }
 
 function countTabs() {
   let needsRestore = 0, isRestoring = 0;
   let windowsEnum = Services.wm.getEnumerator("navigator:browser");
 
   while (windowsEnum.hasMoreElements()) {
     let window = windowsEnum.getNext();