Bug 1530961 - Fix the right-click 'search in new tab' option for search one-offs to work in all situations. r=adw
authorMark Banner <standard8@mozilla.com>
Thu, 07 Mar 2019 12:49:30 +0000
changeset 462867 3815b3ac18232c4a827e2e7d2ad990704ea797e3
parent 462866 a5c70252fc387405156d12c2dd8e8e80c1daa8d0
child 462868 1ae26ce1cf090db6b0b19ea7d7eccd42373dd5fa
push id35661
push useraiakab@mozilla.com
push dateThu, 07 Mar 2019 21:56:20 +0000
treeherdermozilla-central@ca64604d4b78 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1530961
milestone67.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 1530961 - Fix the right-click 'search in new tab' option for search one-offs to work in all situations. r=adw Differential Revision: https://phabricator.services.mozilla.com/D22353
browser/components/search/content/search-one-offs.js
browser/components/urlbar/tests/browser/browser.ini
browser/components/urlbar/tests/browser/browser_urlbarOneOffs_contextMenu.js
browser/components/urlbar/tests/legacy/browser.ini
--- a/browser/components/search/content/search-one-offs.js
+++ b/browser/components/search/content/search-one-offs.js
@@ -674,16 +674,21 @@ class SearchOneOffs {
     return this.telemetryOrigin + "-engine-one-off-item-" + this._fixUpEngineNameForID(engine.name);
   }
 
   _fixUpEngineNameForID(name) {
     return name.replace(/ /g, "-");
   }
 
   _buttonForEngine(engine) {
+    if (this.telemetryOrigin == "urlbar" && !UrlbarPrefs.get("quantumbar")) {
+      return this._popup &&
+             document.getAnonymousElementByAttribute(this._popup, "id", this._buttonIDForEngine(engine));
+    }
+
     let id = this._buttonIDForEngine(engine);
     return this._popup && document.getElementById(id);
   }
 
   /**
    * Updates the popup and textbox for the currently selected or moused-over
    * button.
    *
--- a/browser/components/urlbar/tests/browser/browser.ini
+++ b/browser/components/urlbar/tests/browser/browser.ini
@@ -114,25 +114,29 @@ skip-if = os == "linux" # Bug 1073339 - 
 [browser_UrlbarInput_overflow.js]
 [browser_UrlbarInput_search.js]
 [browser_UrlbarInput_tooltip.js]
 [browser_UrlbarInput_trimURLs.js]
 subsuite = clipboard
 [browser_UrlbarInput_unit.js]
 support-files = empty.xul
 [browser_UrlbarLoadRace.js]
+[browser_urlbarOneOffs_contextMenu.js]
+support-files =
+  searchSuggestionEngine.xml
+  searchSuggestionEngine.sjs
 [browser_urlbarOneOffs_searchSuggestions.js]
 support-files =
-  ../browser/searchSuggestionEngine.xml
-  ../browser/searchSuggestionEngine.sjs
+  searchSuggestionEngine.xml
+  searchSuggestionEngine.sjs
 [browser_urlbarOneOffs_settings.js]
 [browser_urlbarOneOffs.js]
 support-files =
-  ../browser/searchSuggestionEngine.xml
-  ../browser/searchSuggestionEngine.sjs
+  searchSuggestionEngine.xml
+  searchSuggestionEngine.sjs
 [browser_urlbarPlaceholder.js]
 support-files =
   searchSuggestionEngine.xml
   searchSuggestionEngine.sjs
 [browser_urlbarRevert.js]
 [browser_urlbarSearchFunction.js]
 [browser_urlbarSearchSingleWordNotification.js]
 [browser_urlbarSearchSuggestions.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/urlbar/tests/browser/browser_urlbarOneOffs_contextMenu.js
@@ -0,0 +1,139 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+/**
+ * Tests that the right-click menu works correctly for the one-off buttons.
+ */
+
+const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";
+
+let gMaxResults;
+
+XPCOMUtils.defineLazyGetter(this, "oneOffSearchButtons", () => {
+  return UrlbarTestUtils.getOneOffSearchButtons(window);
+});
+
+let originalEngine;
+let newEngine;
+
+add_task(async function setup() {
+  SpecialPowers.pushPrefEnv({
+    "set": [
+      ["browser.urlbar.oneOffSearches", true],
+      // Avoid hitting the network with search suggestions.
+      ["browser.urlbar.suggest.searches", false],
+      ["browser.tabs.loadInBackground", true],
+    ],
+  });
+  gMaxResults = Services.prefs.getIntPref("browser.urlbar.maxRichResults");
+
+  // Add a search suggestion engine and move it to the front so that it appears
+  // as the first one-off.
+  originalEngine = await Services.search.getDefault();
+  newEngine = await SearchTestUtils.promiseNewSearchEngine(
+    getRootDirectory(gTestPath) + TEST_ENGINE_BASENAME);
+  await Services.search.moveEngine(newEngine, 0);
+
+  registerCleanupFunction(async function() {
+    await PlacesUtils.history.clear();
+    await Services.search.setDefault(originalEngine);
+  });
+
+  await PlacesUtils.history.clear();
+
+  let visits = [];
+  for (let i = 0; i < gMaxResults; i++) {
+    visits.push({
+      uri: makeURI("http://example.com/browser_urlbarOneOffs.js/?" + i),
+      // TYPED so that the visit shows up when the urlbar's drop-down arrow is
+      // pressed.
+      transition: Ci.nsINavHistoryService.TRANSITION_TYPED,
+    });
+  }
+  await PlacesTestUtils.addVisits(visits);
+});
+
+async function searchInTab(checkFn) {
+  // Ensure we've got a different engine selected to the one we added. so that
+  // it is a different engine to select.
+  await Services.search.setDefault(originalEngine);
+
+  await BrowserTestUtils.withNewTab({gBrowser}, async testBrowser => {
+    await promiseAutocompleteResultPopup("foo");
+
+    let contextMenu = oneOffSearchButtons.querySelector(".search-one-offs-context-menu");
+    let popupShownPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
+    let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
+    EventUtils.synthesizeMouseAtCenter(oneOffs[0], {type: "contextmenu", button: 2});
+    await popupShownPromise;
+
+    let tabOpenAndLoaded = BrowserTestUtils.waitForNewTab(gBrowser, null, true);
+
+    let openInTab = oneOffSearchButtons.querySelector(".search-one-offs-context-open-in-new-tab");
+    EventUtils.synthesizeMouseAtCenter(openInTab, {});
+
+    let newTab = await tabOpenAndLoaded;
+
+    checkFn(testBrowser, newTab);
+
+    BrowserTestUtils.removeTab(newTab);
+  });
+}
+
+add_task(async function searchInNewTab_opensBackground() {
+  Services.prefs.setBoolPref("browser.tabs.loadInBackground", true);
+  await searchInTab((testBrowser, newTab) => {
+    Assert.equal(newTab.linkedBrowser.currentURI.spec,
+      "http://mochi.test:8888/?terms=foo",
+      "Should have loaded the expected URI in a new tab.");
+
+    Assert.equal(testBrowser.currentURI.spec,
+      "about:blank",
+      "Should not have touched the original tab");
+
+    Assert.equal(testBrowser, gBrowser.selectedTab.linkedBrowser,
+      "Should not have changed the selected tab");
+  });
+});
+
+add_task(async function searchInNewTab_opensForeground() {
+  Services.prefs.setBoolPref("browser.tabs.loadInBackground", false);
+
+  await searchInTab((testBrowser, newTab) => {
+    Assert.equal(newTab.linkedBrowser.currentURI.spec,
+      "http://mochi.test:8888/?terms=foo",
+      "Should have loaded the expected URI in a new tab.");
+
+    Assert.equal(testBrowser.currentURI.spec,
+      "about:blank",
+      "Should not have touched the original tab");
+
+    Assert.equal(newTab, gBrowser.selectedTab,
+      "Should have changed the selected tab");
+  });
+});
+
+add_task(async function switchDefaultEngine() {
+  await Services.search.setDefault(originalEngine);
+
+  await BrowserTestUtils.withNewTab({gBrowser}, async () => {
+    await promiseAutocompleteResultPopup("foo");
+
+    let contextMenu = oneOffSearchButtons.querySelector(".search-one-offs-context-menu");
+    let popupShownPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
+    let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
+    EventUtils.synthesizeMouseAtCenter(oneOffs[0], {type: "contextmenu", button: 2});
+    await popupShownPromise;
+
+    let engineChangedPromise =
+      SearchTestUtils.promiseSearchNotification("engine-default", "browser-search-engine-modified");
+    let setDefault = oneOffSearchButtons.querySelector(".search-one-offs-context-set-default");
+    EventUtils.synthesizeMouseAtCenter(setDefault, {});
+    await engineChangedPromise;
+
+    Assert.equal(await Services.search.getDefault(), newEngine,
+      "Should have correctly changed the engine to the new one");
+  });
+});
--- a/browser/components/urlbar/tests/legacy/browser.ini
+++ b/browser/components/urlbar/tests/legacy/browser.ini
@@ -117,16 +117,20 @@ support-files =
   ../browser/file_urlbar_edit_dos.html
 [../browser/browser_urlbarDelete.js]
 [../browser/browser_urlbarEnter.js]
 [../browser/browser_urlbarEnterAfterMouseOver.js]
 skip-if = os == "linux" # Bug 1073339 - Investigate autocomplete test unreliability on Linux/e10s
 [../browser/browser_urlbar_whereToOpen.js]
 [../browser/browser_urlbarFocusedCmdK.js]
 [../browser/browser_urlbarHashChangeProxyState.js]
+[../browser/browser_urlbarOneOffs_contextMenu.js]
+support-files =
+  ../browser/searchSuggestionEngine.xml
+  ../browser/searchSuggestionEngine.sjs
 [../browser/browser_urlbarOneOffs_searchSuggestions.js]
 support-files =
   ../browser/searchSuggestionEngine.xml
   ../browser/searchSuggestionEngine.sjs
 [../browser/browser_urlbarOneOffs_settings.js]
 [../browser/browser_urlbarOneOffs.js]
 support-files =
   ../browser/searchSuggestionEngine.xml