Bug 599909 - to-be-restored tabs don't show up in switch-to-tab [r=dietrich, a=blocking2.0:.x]
authorPaul O’Shannessy <paul@oshannessy.com>
Sat, 29 Jan 2011 12:15:57 -0800
changeset 61590 30d746c939326dbd29fff864f869abb886d0f570
parent 61589 5502c6978c73bb33159111ef1119e23160f37b72
child 61591 8e99b3137026617564fc7da45a2b3f8914ffeed2
push idunknown
push userunknown
push dateunknown
reviewersdietrich, blocking2.0
bugs599909
milestone2.0b11pre
Bug 599909 - to-be-restored tabs don't show up in switch-to-tab [r=dietrich, a=blocking2.0:.x]
browser/components/sessionstore/src/nsSessionStore.js
browser/components/sessionstore/test/browser/Makefile.in
browser/components/sessionstore/test/browser/browser_599909.js
--- a/browser/components/sessionstore/src/nsSessionStore.js
+++ b/browser/components/sessionstore/src/nsSessionStore.js
@@ -2548,17 +2548,24 @@ SessionStoreService.prototype = {
       }
 
       browser.stop(); // in case about:blank isn't done yet
 
       // wall-paper fix for bug 439675: make sure that the URL to be loaded
       // is always visible in the address bar
       let activeIndex = (tabData.index || tabData.entries.length) - 1;
       let activePageData = tabData.entries[activeIndex] || null;
-      browser.userTypedValue = activePageData ? activePageData.url || null : null;
+      let uri = activePageData ? activePageData.url || null : null;
+      browser.userTypedValue = uri;
+
+      // Also make sure currentURI is set so that switch-to-tab works before
+      // the tab is restored. We'll reset this to about:blank when we try to
+      // restore the tab to ensure that docshell doeesn't get confused.
+      if (uri)
+        browser.docShell.setCurrentURI(this._getURIFromString(uri));
 
       // If the page has a title, set it.
       if (activePageData) {
         if (activePageData.title) {
           tab.label = activePageData.title;
           tab.crop = "end";
         } else if (activePageData.url != "about:blank") {
           tab.label = activePageData.url;
@@ -2719,16 +2726,19 @@ SessionStoreService.prototype = {
 
     // Remove the history listener, since we no longer need it once we start restoring
     this._removeSHistoryListener(aTab);
 
     let activeIndex = (tabData.index || tabData.entries.length) - 1;
     if (activeIndex >= tabData.entries.length)
       activeIndex = tabData.entries.length - 1;
 
+    // Reset currentURI.
+    browser.webNavigation.setCurrentURI(this._getURIFromString("about:blank"));
+
     // Attach data that will be restored on "load" event, after tab is restored.
     if (activeIndex > -1) {
       // restore those aspects of the currently active documents which are not
       // preserved in the plain history entries (mainly scroll state and text data)
       browser.__SS_restore_data = tabData.entries[activeIndex] || {};
       browser.__SS_restore_pageStyle = tabData.pageStyle || "";
       browser.__SS_restore_tab = aTab;
 
--- a/browser/components/sessionstore/test/browser/Makefile.in
+++ b/browser/components/sessionstore/test/browser/Makefile.in
@@ -126,16 +126,17 @@ include $(topsrcdir)/config/rules.mk
 	browser_590268.js \
 	browser_597315.js \
 	browser_597315_index.html \
 	browser_597315_a.html \
 	browser_597315_b.html \
 	browser_597315_c.html \
 	browser_597315_c1.html \
 	browser_597315_c2.html \
+	browser_599909.js \
 	browser_600545.js \
 	browser_601955.js \
 	browser_607016.js \
 	browser_615394-SSWindowState_events.js \
 	browser_618151.js \
 	browser_623779.js \
 	browser_625257.js \
 	$(NULL)
new file mode 100644
--- /dev/null
+++ b/browser/components/sessionstore/test/browser/browser_599909.js
@@ -0,0 +1,153 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is sessionstore test code.
+ *
+ * The Initial Developer of the Original Code is
+ * Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Paul O’Shannessy <paul@oshannessy.com>
+ *
+ * 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
+ * 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 ***** */
+
+const TAB_STATE_NEEDS_RESTORE = 1;
+const TAB_STATE_RESTORING = 2;
+
+Cu.import("resource://gre/modules/Services.jsm");
+let ss = Cc["@mozilla.org/browser/sessionstore;1"].
+         getService(Ci.nsISessionStore);
+
+let stateBackup = ss.getBrowserState();
+
+function cleanup() {
+  // Reset the pref
+  try {
+    Services.prefs.clearUserPref("browser.sessionstore.max_concurrent_tabs");
+  } catch (e) {}
+  ss.setBrowserState(stateBackup);
+  executeSoon(finish);
+}
+
+function test() {
+  /** Bug 599909 - to-be-reloaded tabs don't show up in switch-to-tab **/
+  waitForExplicitFinish();
+
+  // Set the pref to 0 so we know exactly how many tabs should be restoring at
+  // any given time. This guarantees that a finishing load won't start another.
+  Services.prefs.setIntPref("browser.sessionstore.max_concurrent_tabs", 0);
+
+  let state = { windows: [{ tabs: [
+    { entries: [{ url: "http://example.org/#1" }] },
+    { entries: [{ url: "http://example.org/#2" }] },
+    { entries: [{ url: "http://example.org/#3" }] },
+    { entries: [{ url: "http://example.org/#4" }] }
+  ], selected: 1 }] };
+
+  let tabsForEnsure = {};
+  state.windows[0].tabs.forEach(function(tab) {
+    tabsForEnsure[tab.entries[0].url] = 1;
+  });
+
+  let tabsRestoring = 0;
+  let tabsRestored = 0;
+
+  function handleEvent(aEvent) {
+    if (aEvent.type == "SSTabRestoring")
+      tabsRestoring++;
+    else
+      tabsRestored++;
+
+    if (tabsRestoring < state.windows[0].tabs.length ||
+        tabsRestored < 1)
+      return;
+
+    gBrowser.tabContainer.removeEventListener("SSTabRestoring", handleEvent, true);
+    gBrowser.tabContainer.removeEventListener("SSTabRestored", handleEvent, true);
+    executeSoon(function() {
+      checkAutocompleteResults(tabsForEnsure, cleanup);
+    });
+  }
+
+  // currentURI is set before SSTabRestoring is fired, so we can sucessfully check
+  // after that has fired for all tabs. Since 1 tab will be restored though, we
+  // also need to wait for 1 SSTabRestored since currentURI will be set, unset, then set.
+  gBrowser.tabContainer.addEventListener("SSTabRestoring", handleEvent, true);
+  gBrowser.tabContainer.addEventListener("SSTabRestored", handleEvent, true);
+  ss.setBrowserState(JSON.stringify(state));
+}
+
+// The following was taken from browser/base/content/test/browser_tabMatchesInAwesomebar.js
+// so that we could do the same sort of checking.
+var gController = Cc["@mozilla.org/autocomplete/controller;1"].
+                  getService(Ci.nsIAutoCompleteController);
+
+function checkAutocompleteResults(aExpected, aCallback) {
+  gController.input = {
+    timeout: 10,
+    textValue: "",
+    searches: ["history"],
+    searchParam: "enable-actions",
+    popupOpen: false,
+    minResultsForPopup: 0,
+    invalidate: function() {},
+    disableAutoComplete: false,
+    completeDefaultIndex: false,
+    get popup() { return this; },
+    onSearchBegin: function() {},
+    onSearchComplete:  function ()
+    {
+      info("Found " + gController.matchCount + " matches.");
+      // Check to see the expected uris and titles match up (in any order)
+      for (let i = 0; i < gController.matchCount; i++) {
+        let uri = gController.getValueAt(i).replace(/^moz-action:[^,]+,/i, "");
+
+        info("Search for '" + uri + "' in open tabs.");
+        ok(uri in aExpected, "Registered open page found in autocomplete.");
+        // Remove the found entry from expected results.
+        delete aExpected[uri];
+      }
+
+      // Make sure there is no reported open page that is not open.
+      for (let entry in aExpected) {
+        ok(false, "'" + entry + "' not found in autocomplete.");
+      }
+
+      executeSoon(aCallback);
+    },
+    setSelectedIndex: function() {},
+    get searchCount() { return this.searches.length; },
+    getSearchAt: function(aIndex) this.searches[aIndex],
+    QueryInterface: XPCOMUtils.generateQI([
+      Ci.nsIAutoCompleteInput,
+      Ci.nsIAutoCompletePopup,
+    ])
+  };
+
+  info("Searching open pages.");
+  gController.startSearch(Services.prefs.getCharPref("browser.urlbar.restrict.openpage"));
+}