Bug 1368074 - Autocomplete popup is not re-opened when repeating the same search in a new tab. r=mconley draft
authorMarco Bonardo <mbonardo@mozilla.com>
Tue, 30 May 2017 17:28:34 +0200
changeset 587147 a9601dee7f20e4fdeb536f28853f344cd84cbcd1
parent 586973 7b8937970f9ca85db88cb2496f2112175fd847c8
child 631197 96b3c656c7ce4b27417f91c57c3a4d88581d8fb2
push id61628
push usermak77@bonardo.net
push dateWed, 31 May 2017 15:55:05 +0000
reviewersmconley
bugs1368074
milestone55.0a1
Bug 1368074 - Autocomplete popup is not re-opened when repeating the same search in a new tab. r=mconley MozReview-Commit-ID: GSZoWFgw0X8
browser/base/content/test/urlbar/browser.ini
browser/base/content/test/urlbar/browser_new_tab_urlbar_reset.js
browser/base/content/urlbarBindings.xml
toolkit/components/autocomplete/nsAutoCompleteController.cpp
--- a/browser/base/content/test/urlbar/browser.ini
+++ b/browser/base/content/test/urlbar/browser.ini
@@ -33,16 +33,17 @@ subsuite = clipboard
 support-files =
   redirect_bug623155.sjs
 [browser_bug783614.js]
 [browser_canonizeURL.js]
 [browser_dragdropURL.js]
 [browser_locationBarCommand.js]
 [browser_locationBarExternalLoad.js]
 [browser_moz_action_link.js]
+[browser_new_tab_urlbar_reset.js]
 [browser_pasteAndGo.js]
 subsuite = clipboard
 [browser_removeUnsafeProtocolsFromURLBarPaste.js]
 subsuite = clipboard
 [browser_search_favicon.js]
 [browser_tabMatchesInAwesomebar.js]
 support-files =
   moz.png
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/urlbar/browser_new_tab_urlbar_reset.js
@@ -0,0 +1,16 @@
+"use strict";
+
+/**
+ * Verify that urlbar state is reset when openig a new tab, so searching for the
+ * same text will reopen the results popup.
+ */
+add_task(async function() {
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank", false);
+  await promiseAutocompleteResultPopup("m");
+  ok(gURLBar.popupOpen, "The popup is open");
+  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank", false);
+  await promiseAutocompleteResultPopup("m");
+  ok(gURLBar.popupOpen, "The popup is open");
+  await BrowserTestUtils.removeTab(tab);
+  await BrowserTestUtils.removeTab(tab2);
+});
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -117,16 +117,21 @@ file, You can obtain one at http://mozil
           pasteAndGo.setAttribute("label", label);
           pasteAndGo.setAttribute("anonid", "paste-and-go");
           pasteAndGo.setAttribute("oncommand",
               "gURLBar.select(); goDoCommand('cmd_paste'); gURLBar.handleCommand();");
           cxmenu.insertBefore(pasteAndGo, insertLocation.nextSibling);
         }
 
         this._enableOrDisableOneOffSearches();
+
+        // The autocomplete controller uses heuristic on some internal caches
+        // to handle cases like backspace, autofill or repeated searches.
+        // Ensure to clear those internal caches when switching tabs.
+        gBrowser.tabContainer.addEventListener("TabSelect", this);
       ]]></constructor>
 
       <destructor><![CDATA[
         this._prefs.removeObserver("", this);
         this._prefs = null;
         this.inputField.controllers.removeController(this._copyCutController);
         this.inputField.removeEventListener("paste", this);
         this.inputField.removeEventListener("mousedown", this);
@@ -1142,16 +1147,20 @@ file, You can obtain one at http://mozil
                 break;
               }
               this.setAttribute("textoverflow", "true");
               break;
             case "underflow":
               this.removeAttribute("textoverflow");
               this._hideURLTooltip();
               break;
+            case "TabSelect":
+              this.detachController();
+              this.attachController();
+              break;
           }
         ]]></body>
       </method>
 
       <!--
         onBeforeTextValueSet is called by the base-binding's .textValue getter.
         It should return the value that the getter should use.
       -->
--- a/toolkit/components/autocomplete/nsAutoCompleteController.cpp
+++ b/toolkit/components/autocomplete/nsAutoCompleteController.cpp
@@ -659,17 +659,17 @@ nsAutoCompleteController::HandleDelete(b
 {
   *_retval = false;
   if (!mInput)
     return NS_OK;
 
   nsCOMPtr<nsIAutoCompleteInput> input(mInput);
   bool isOpen = false;
   input->GetPopupOpen(&isOpen);
-  if (!isOpen || mRowCount <= 0) {
+  if (!isOpen || mRowCount == 0) {
     // Nothing left to delete, proceed as normal
     bool unused = false;
     HandleText(&unused);
     return NS_OK;
   }
 
   nsCOMPtr<nsIAutoCompletePopup> popup;
   input->GetPopup(getter_AddRefs(popup));
@@ -702,17 +702,18 @@ nsAutoCompleteController::HandleDelete(b
   // Unselect the current item.
   popup->SetSelectedIndex(-1);
 
   // Tell the tree that the row count changed.
   if (mTree)
     mTree->RowCountChanged(mRowCount, -1);
 
   // Adjust index, if needed.
-  if (index >= (int32_t)mRowCount)
+  MOZ_ASSERT(index >= 0); // We verified this above, after RowIndexToSearch.
+  if (static_cast<uint32_t>(index) >= mRowCount)
     index = mRowCount - 1;
 
   if (mRowCount > 0) {
     // There are still rows in the popup, select the current index again.
     popup->SetSelectedIndex(index);
 
     // Complete to the new current value.
     bool shouldComplete = false;
@@ -1974,17 +1975,17 @@ nsAutoCompleteController::GetResultValue
 }
 
 nsresult
 nsAutoCompleteController::GetResultValueLabelAt(int32_t aIndex,
                                                 bool aGetFinalValue,
                                                 bool aGetValue,
                                                 nsAString & _retval)
 {
-  NS_ENSURE_TRUE(aIndex >= 0 && (uint32_t) aIndex < mRowCount, NS_ERROR_ILLEGAL_VALUE);
+  NS_ENSURE_TRUE(aIndex >= 0 && static_cast<uint32_t>(aIndex) < mRowCount, NS_ERROR_ILLEGAL_VALUE);
 
   int32_t rowIndex;
   nsIAutoCompleteResult *result;
   nsresult rv = GetResultAt(aIndex, &result, &rowIndex);
   NS_ENSURE_SUCCESS(rv, rv);
 
   uint16_t searchResult;
   result->GetSearchResult(&searchResult);