Bug 952259 - Tests for Metro SessionStore.getBrowserState [r=msamuel a=metro-only]
authorMatt Brubeck <mbrubeck@mozilla.com>
Tue, 28 Jan 2014 15:24:52 -0800
changeset 175157 b08677d9f100d6f4478209a0b92a6ddd300dcb78
parent 175156 cf139f1f98909d42d469c5ef6761c7e79c9893ee
child 175158 dcc00e89ec8fc450e0da9973ee14d51fb45d38a7
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmsamuel, metro-only
bugs952259
milestone28.0a2
Bug 952259 - Tests for Metro SessionStore.getBrowserState [r=msamuel a=metro-only]
browser/metro/base/tests/mochitest/browser_sessionstore.js
browser/metro/base/tests/mochitest/metro.ini
browser/metro/components/SessionStore.js
new file mode 100644
--- /dev/null
+++ b/browser/metro/base/tests/mochitest/browser_sessionstore.js
@@ -0,0 +1,55 @@
+/* vim: set ts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var gSessionStore = Cc["@mozilla.org/browser/sessionstore;1"]
+                    .getService(Ci.nsISessionStore);
+
+function test() {
+  runTests();
+}
+
+function getState() {
+  return JSON.parse(gSessionStore.getBrowserState());
+}
+
+function getTabData() {
+  return getState().windows[0].tabs;
+}
+
+function isValidTabData(aData) {
+  return aData && aData.entries && aData.entries.length &&
+           typeof aData.index == "number";
+}
+
+gTests.push({
+  desc: "getBrowserState tests",
+  run: function() {
+    // Wait for Session Manager to be initialized.
+    yield waitForCondition(() => window.__SSID);
+    info(window.__SSID);
+    let tabData1 = getTabData();
+    ok(tabData1.every(isValidTabData), "Tab data starts out valid");
+
+    // Open a tab.
+    let tab = Browser.addTab("about:mozilla");
+    let tabData2 = getTabData();
+    is(tabData2.length, tabData1.length, "New tab not added yet.");
+
+    // Wait for the tab's session data to be initialized.
+    yield waitForMessage("Content:SessionHistory", tab.browser.messageManager);
+    yield waitForMs(0);
+    let tabData3 = getTabData();
+    is(tabData3.length, tabData1.length + 1, "New tab added.");
+    ok(tabData3.every(isValidTabData), "Tab data still valid");
+
+    // Close the tab.
+    Browser.closeTab(tab, { forceClose: true } );
+    let tabData4 = getTabData();
+    is(tabData4.length, tabData1.length, "Closed tab removed.");
+    ok(tabData4.every(isValidTabData), "Tab data valid again");
+  }
+});
--- a/browser/metro/base/tests/mochitest/metro.ini
+++ b/browser/metro/base/tests/mochitest/metro.ini
@@ -46,16 +46,17 @@ support-files =
 [browser_form_selects.js]
 [browser_history.js]
 [browser_inputsource.js]
 [browser_link_click.js]
 [browser_onscreen_keyboard.js]
 [browser_prefs_ui.js]
 [browser_prompt.js]
 [browser_remotetabs.js]
+[browser_sessionstore.js]
 [browser_snappedState.js]
 [browser_tabs.js]
 [browser_tabs_container.js]
 [browser_test.js]
 [browser_tiles.js]
 [browser_topsites.js]
 [browser_urlbar.js]
 [browser_urlbar_highlightURLs.js]
--- a/browser/metro/components/SessionStore.js
+++ b/browser/metro/components/SessionStore.js
@@ -333,29 +333,27 @@ SessionStore.prototype = {
 
     // Ignore non-browser windows and windows opened while shutting down
     if (aWindow.document.documentElement.getAttribute("windowtype") != "navigator:browser" || this._loadState == STATE_QUITTING)
       return;
 
     // Assign it a unique identifier and create its data object
     aWindow.__SSID = "window" + gUUIDGenerator.generateUUID().toString();
     this._windows[aWindow.__SSID] = { tabs: [], selected: 0, _closedTabs: [] };
+    this._orderedWindows.push(aWindow.__SSID);
 
     // Perform additional initialization when the first window is loading
     if (this._loadState == STATE_STOPPED) {
       this._loadState = STATE_RUNNING;
       this._lastSaveTime = Date.now();
 
       // Nothing to restore, notify observers things are complete
       if (!this.shouldRestore()) {
         this._clearCache();
         Services.obs.notifyObservers(null, "sessionstore-windows-restored", "");
-
-        // If nothing is being restored, we only have our single Metro window.
-        this._orderedWindows.push(aWindow.__SSID);
       }
     }
 
     // Add tab change listeners to all already existing tabs
     let tabs = aWindow.Browser.tabs;
     for (let i = 0; i < tabs.length; i++)
       this.onTabAdd(aWindow, tabs[i].browser, true);
 
@@ -548,25 +546,25 @@ SessionStore.prototype = {
     tabData.entries = aHistory.entries;
     tabData.index = aHistory.index;
     tabData.attributes = { image: aBrowser.mIconURL };
 
     aBrowser.__SS_data = tabData;
   },
 
   _getTabData: function(aWindow) {
-    return aWindow.Browser.tabs.map(tab => {
-      let browser = tab.browser;
-      if (browser.__SS_data) {
+    return aWindow.Browser.tabs
+      .filter(tab => tab.browser.__SS_data)
+      .map(tab => {
+        let browser = tab.browser;
         let tabData = browser.__SS_data;
         if (browser.__SS_extdata)
           tabData.extData = browser.__SS_extdata;
         return tabData;
-      }
-    });
+      });
   },
 
   _collectWindowData: function ss__collectWindowData(aWindow) {
     // Ignore windows not tracked by SessionStore
     if (!aWindow.__SSID || !this._windows[aWindow.__SSID])
       return;
 
     let winData = this._windows[aWindow.__SSID];
@@ -794,16 +792,17 @@ SessionStore.prototype = {
         let selected = data.windows[windowIndex].selected;
 
         let currentGroupId;
         try {
           currentGroupId = JSON.parse(data.windows[windowIndex].extData["tabview-groups"]).activeGroupId;
         } catch (ex) { /* currentGroupId is undefined if user has no tab groups */ }
 
         // Move all window data from sessionstore.js to this._windows.
+        this._orderedWindows = [];
         for (let i = 0; i < data.windows.length; i++) {
           let SSID;
           if (i != windowIndex) {
             SSID = "window" + gUUIDGenerator.generateUUID().toString();
             this._windows[SSID] = data.windows[i];
           } else {
             SSID = window.__SSID;
             this._windows[SSID].extData = data.windows[i].extData;