Bug 729241 - Switch-to-tab selections update adaptive records. r=harry,mak
authormcheang <mcheang@mozilla.com>
Mon, 13 Dec 2021 20:01:46 +0000
changeset 601907 359a3b16c34b15a6c467ba286cd133f362781130
parent 601906 0284a394524078a97e28aa90055f5c2cd51f91b3
child 601908 d1c894f81d2a11efc998f4294fe137cb371c1d2b
push id154653
push usermcheang@mozilla.com
push dateMon, 13 Dec 2021 20:04:09 +0000
treeherderautoland@359a3b16c34b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersharry, mak
bugs729241
milestone97.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 729241 - Switch-to-tab selections update adaptive records. r=harry,mak Differential Revision: https://phabricator.services.mozilla.com/D132743
browser/components/urlbar/UrlbarInput.jsm
browser/components/urlbar/tests/browser/browser.ini
browser/components/urlbar/tests/browser/browser_switchTab_inputHistory.js
--- a/browser/components/urlbar/UrlbarInput.jsm
+++ b/browser/components/urlbar/UrlbarInput.jsm
@@ -828,31 +828,42 @@ class UrlbarInput {
         this.handleRevert();
         let prevTab = this.window.gBrowser.selectedTab;
         let loadOpts = {
           adoptIntoActiveWindow: UrlbarPrefs.get(
             "switchTabs.adoptIntoActiveWindow"
           ),
         };
 
+        // We cache the search string because switching tab may clear it.
+        let searchString = this._lastSearchString;
         this.controller.engagementEvent.record(event, {
-          searchString: this._lastSearchString,
+          searchString,
           selIndex,
           selType: "tabswitch",
           provider: result.providerName,
         });
 
         let switched = this.window.switchToTabHavingURI(
           Services.io.newURI(url),
           false,
           loadOpts
         );
         if (switched && prevTab.isEmpty) {
           this.window.gBrowser.removeTab(prevTab);
         }
+
+        if (switched && !this.isPrivate && !result.heuristic) {
+          // We don't await for this, because a rejection should not interrupt
+          // the load. Just reportError it.
+          UrlbarUtils.addToInputHistory(url, searchString).catch(
+            Cu.reportError
+          );
+        }
+
         return;
       }
       case UrlbarUtils.RESULT_TYPE.SEARCH: {
         if (result.payload.providesSearchMode) {
           let searchModeParams = this._searchModeForResult(result);
           if (searchModeParams) {
             this.searchMode = searchModeParams;
             this.search("");
@@ -996,17 +1007,18 @@ class UrlbarInput {
       }
     }
 
     if (!url) {
       throw new Error(`Invalid url for result ${JSON.stringify(result)}`);
     }
 
     if (!this.isPrivate && !result.heuristic) {
-      // This should not interrupt the load anyway.
+      // We don't await for this, because a rejection should not interrupt
+      // the load. Just reportError it.
       UrlbarUtils.addToInputHistory(url, this._lastSearchString).catch(
         Cu.reportError
       );
     }
 
     this.controller.engagementEvent.record(event, {
       searchString: this._lastSearchString,
       selIndex,
--- a/browser/components/urlbar/tests/browser/browser.ini
+++ b/browser/components/urlbar/tests/browser/browser.ini
@@ -269,16 +269,17 @@ support-files =
 [browser_stopSearchOnSelection.js]
 support-files =
   searchSuggestionEngineSlow.xml
   searchSuggestionEngine.sjs
 [browser_suggestedIndex.js]
 [browser_suppressFocusBorder.js]
 [browser_switchTab_closesUrlbarPopup.js]
 [browser_switchTab_decodeuri.js]
+[browser_switchTab_inputHistory.js]
 [browser_switchTab_override.js]
 [browser_switchToTab_chiclet.js]
 [browser_switchToTab_closes_newtab.js]
 [browser_switchToTab_fullUrl_repeatedKeydown.js]
 [browser_switchToTabHavingURI_aOpenParams.js]
 [browser_tabKeyBehavior.js]
 [browser_tabMatchesInAwesomebar_perwindowpb.js]
 [browser_tabMatchesInAwesomebar.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/urlbar/tests/browser/browser_switchTab_inputHistory.js
@@ -0,0 +1,91 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * This tests ensures that the urlbar adaptive behavior updates
+ * when using switch to tab in the address bar dropdown.
+ */
+
+"use strict";
+
+add_task(async function setup() {
+  registerCleanupFunction(async () => {
+    await PlacesUtils.history.clear();
+  });
+});
+
+add_task(async function test_adaptive_with_search_term_and_switch_tab() {
+  await PlacesUtils.history.clear();
+  let urls = [
+    "https://example.com/",
+    "https://example.com/#cat",
+    "https://example.com/#cake",
+    "https://example.com/#car",
+  ];
+
+  info(`Load tabs in same order as urls`);
+  let tabs = [];
+  for (let url of urls) {
+    let tabPromise = BrowserTestUtils.waitForNewTab(gBrowser, url, false, true);
+    gBrowser.loadTabs([url], {
+      inBackground: true,
+      triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
+    });
+
+    let tab = await tabPromise;
+    tabs.push(tab);
+  }
+
+  info(`Switch to tab 0`);
+  await BrowserTestUtils.switchTab(gBrowser, tabs[0]);
+
+  info("Wait for autocomplete");
+  await UrlbarTestUtils.promiseAutocompleteResultPopup({
+    window,
+    value: "ca",
+  });
+
+  let result1 = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
+  Assert.notEqual(result1.url, urls[1], `${urls[1]} url should not be first`);
+
+  info(`Scroll down to select the ${urls[1]} entry using keyboard`);
+  let result2 = await UrlbarTestUtils.getDetailsOfResultAt(
+    window,
+    UrlbarTestUtils.getSelectedRowIndex(window)
+  );
+
+  while (result2.url != urls[1]) {
+    EventUtils.synthesizeKey("KEY_ArrowDown");
+    result2 = await UrlbarTestUtils.getDetailsOfResultAt(
+      window,
+      UrlbarTestUtils.getSelectedRowIndex(window)
+    );
+  }
+
+  Assert.equal(
+    result2.type,
+    UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
+    "Selected entry should be tab switch"
+  );
+  Assert.equal(result2.url, urls[1]);
+
+  info("Visiting tab 1");
+  EventUtils.synthesizeKey("KEY_Enter");
+  Assert.equal(gBrowser.selectedTab, tabs[1], "Should have switched to tab 1");
+
+  info("Switch back to tab 0");
+  await BrowserTestUtils.switchTab(gBrowser, tabs[0]);
+
+  info("Wait for autocomplete");
+  await UrlbarTestUtils.promiseAutocompleteResultPopup({
+    window,
+    value: "ca",
+  });
+
+  let result3 = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
+  Assert.equal(result3.url, urls[1], `${urls[1]} url should be first`);
+
+  for (let tab of tabs) {
+    BrowserTestUtils.removeTab(tab);
+  }
+});