From 9d440a47d15663a67983ab054db72e39dc76b186 Mon Sep 17 00:00:00 2001 From: Doug Thayer Date: Wed, 7 Jun 2017 11:51:23 -0700 Subject: [PATCH] Bug 1331736 - Use selected search suggestion with one-offs r=mak Currently in the location bar we don't match the search bar's behavior of allowing the user to use their currently selected search suggestion with a one-off search engine. This is due to logic which is intended to not search for autocompleted text, instead favoring only what the user has actually typed. This patch adds an exception to that logic for search suggestions. MozReview-Commit-ID: 13sA3r5zXvO --HG-- extra : rebase_source : ac306e5aa2387ba01ba6d9d2fbdd9bda18fd7c41 --- browser/base/content/test/urlbar/browser.ini | 4 + .../test/urlbar/browser_urlbarOneOffs.js | 4 +- ...browser_urlbarOneOffs_searchSuggestions.js | 115 ++++++++++++++++++ .../test/urlbar/searchSuggestionEngine.xml | 4 +- browser/base/content/urlbarBindings.xml | 7 ++ 5 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 browser/base/content/test/urlbar/browser_urlbarOneOffs_searchSuggestions.js diff --git a/browser/base/content/test/urlbar/browser.ini b/browser/base/content/test/urlbar/browser.ini index e2d671c74f95..4cb59cf527c4 100644 --- a/browser/base/content/test/urlbar/browser.ini +++ b/browser/base/content/test/urlbar/browser.ini @@ -70,6 +70,10 @@ skip-if = os == "linux" # Bug 1073339 - Investigate autocomplete test unreliabil [browser_urlbarHashChangeProxyState.js] [browser_urlbarKeepStateAcrossTabSwitches.js] [browser_urlbarOneOffs.js] +support-files = + searchSuggestionEngine.xml + searchSuggestionEngine.sjs +[browser_urlbarOneOffs_searchSuggestions.js] support-files = searchSuggestionEngine.xml searchSuggestionEngine.sjs diff --git a/browser/base/content/test/urlbar/browser_urlbarOneOffs.js b/browser/base/content/test/urlbar/browser_urlbarOneOffs.js index a224a54c711e..36e0cc251201 100644 --- a/browser/base/content/test/urlbar/browser_urlbarOneOffs.js +++ b/browser/base/content/test/urlbar/browser_urlbarOneOffs.js @@ -196,7 +196,7 @@ add_task(async function oneOffClick() { let oneOffs = gURLBar.popup.oneOffSearchButtons.getSelectableButtons(true); let resultsPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, - "http://mochi.test:8888/"); + "http://mochi.test:8888/?terms=foo.bar"); EventUtils.synthesizeMouseAtCenter(oneOffs[0], {}); await resultsPromise; @@ -220,7 +220,7 @@ add_task(async function oneOffReturn() { let resultsPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, - "http://mochi.test:8888/"); + "http://mochi.test:8888/?terms=foo.bar"); EventUtils.synthesizeKey("VK_RETURN", {}) await resultsPromise; diff --git a/browser/base/content/test/urlbar/browser_urlbarOneOffs_searchSuggestions.js b/browser/base/content/test/urlbar/browser_urlbarOneOffs_searchSuggestions.js new file mode 100644 index 000000000000..a4b201bf3907 --- /dev/null +++ b/browser/base/content/test/urlbar/browser_urlbarOneOffs_searchSuggestions.js @@ -0,0 +1,115 @@ +const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml"; + +add_task(async function init() { + await PlacesTestUtils.clearHistory(); + await SpecialPowers.pushPrefEnv({ + set: [["browser.urlbar.oneOffSearches", true], + ["browser.urlbar.suggest.searches", true]], + }); + let engine = await promiseNewSearchEngine(TEST_ENGINE_BASENAME); + let oldCurrentEngine = Services.search.currentEngine; + Services.search.moveEngine(engine, 0); + Services.search.currentEngine = engine; + registerCleanupFunction(async function() { + Services.search.currentEngine = oldCurrentEngine; + + await PlacesTestUtils.clearHistory(); + // Make sure the popup is closed for the next test. + gURLBar.blur(); + Assert.ok(!gURLBar.popup.popupOpen, "popup should be closed"); + }); +}); + +// Presses the Return key when a one-off is selected after selecting a search +// suggestion. +add_task(async function oneOffReturnAfterSuggestion() { + let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser); + + let typedValue = "foo"; + await promiseAutocompleteResultPopup(typedValue, window, true); + await BrowserTestUtils.waitForCondition(suggestionsPresent, + "waiting for suggestions"); + assertState(0, -1, typedValue); + + // Down to select the first search suggestion. + EventUtils.synthesizeKey("VK_DOWN", {}) + assertState(1, -1, "foofoo"); + + // Down to select the next search suggestion. + EventUtils.synthesizeKey("VK_DOWN", {}) + assertState(2, -1, "foobar"); + + // Alt+Down to select the first one-off. + EventUtils.synthesizeKey("VK_DOWN", { altKey: true }) + assertState(2, 0, "foobar"); + + let resultsPromise = + BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, + `http://mochi.test:8888/?terms=foobar`); + EventUtils.synthesizeKey("VK_RETURN", {}); + await resultsPromise; + + await PlacesTestUtils.clearHistory(); + await BrowserTestUtils.removeTab(tab); +}); + +// Clicks a one-off engine after selecting a search suggestion. +add_task(async function oneOffClickAfterSuggestion() { + let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser); + + let typedValue = "foo"; + await promiseAutocompleteResultPopup(typedValue, window, true); + await BrowserTestUtils.waitForCondition(suggestionsPresent, + "waiting for suggestions"); + assertState(0, -1, typedValue); + + // Down to select the first search suggestion. + EventUtils.synthesizeKey("VK_DOWN", {}) + assertState(1, -1, "foofoo"); + + // Down to select the next search suggestion. + EventUtils.synthesizeKey("VK_DOWN", {}) + assertState(2, -1, "foobar"); + + let oneOffs = gURLBar.popup.oneOffSearchButtons.getSelectableButtons(true); + let resultsPromise = + BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, + `http://mochi.test:8888/?terms=foobar`); + EventUtils.synthesizeMouseAtCenter(oneOffs[0], {}); + await resultsPromise; + + await PlacesTestUtils.clearHistory(); + await BrowserTestUtils.removeTab(tab); +}); + +function assertState(result, oneOff, textValue = undefined) { + Assert.equal(gURLBar.popup.selectedIndex, result, + "Expected result should be selected"); + Assert.equal(gURLBar.popup.oneOffSearchButtons.selectedButtonIndex, oneOff, + "Expected one-off should be selected"); + if (textValue !== undefined) { + Assert.equal(gURLBar.textValue, textValue, "Expected textValue"); + } +} + +async function hidePopup() { + EventUtils.synthesizeKey("VK_ESCAPE", {}); + await promisePopupHidden(gURLBar.popup); +} + +function suggestionsPresent() { + let controller = gURLBar.popup.input.controller; + let matchCount = controller.matchCount; + for (let i = 0; i < matchCount; i++) { + let url = controller.getValueAt(i); + let mozActionMatch = url.match(/^moz-action:([^,]+),(.*)$/); + if (mozActionMatch) { + let [, type, paramStr] = mozActionMatch; + let params = JSON.parse(paramStr); + if (type == "searchengine" && "searchSuggestion" in params) { + return true; + } + } + } + return false; +} diff --git a/browser/base/content/test/urlbar/searchSuggestionEngine.xml b/browser/base/content/test/urlbar/searchSuggestionEngine.xml index a5659792e14a..cefaa68fffea 100644 --- a/browser/base/content/test/urlbar/searchSuggestionEngine.xml +++ b/browser/base/content/test/urlbar/searchSuggestionEngine.xml @@ -5,5 +5,7 @@ browser_searchSuggestionEngine searchSuggestionEngine.xml - + + + diff --git a/browser/base/content/urlbarBindings.xml b/browser/base/content/urlbarBindings.xml index 59e645eea44f..f359b533eb67 100644 --- a/browser/base/content/urlbarBindings.xml +++ b/browser/base/content/urlbarBindings.xml @@ -671,6 +671,13 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.