Bug 1658646 - Show local results for search modes other than general purpose search engines. r=adw

Differential Revision: https://phabricator.services.mozilla.com/D86998
This commit is contained in:
Harry Twyford 2020-08-14 03:57:15 +00:00
Родитель 62af4b9d9e
Коммит 13a0e9233c
14 изменённых файлов: 130 добавлений и 58 удалений

Просмотреть файл

@ -1096,7 +1096,9 @@ class UrlbarInput {
if (this.searchMode) {
options.searchMode = this.searchMode;
options.sources = [this.searchMode.source];
if (this.searchMode.source) {
options.sources = [this.searchMode.source];
}
}
// TODO (Bug 1522902): This promise is necessary for tests, because some
@ -1193,10 +1195,15 @@ class UrlbarInput {
this._searchModeLabel.removeAttribute("data-l10n-id");
if (engineName) {
this.searchMode = {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName,
};
this.searchMode = { engineName };
if (source) {
this.searchMode.source = source;
} else if (UrlbarUtils.WEB_ENGINE_NAMES.has(engineName)) {
// History results for general-purpose search engines are often not
// useful, so we hide them in search mode. See bug 1658646 for
// discussion.
this.searchMode.source = UrlbarUtils.RESULT_SOURCE.SEARCH;
}
this._searchModeIndicatorTitle.textContent = engineName;
this._searchModeLabel.textContent = engineName;
this.document.l10n.setAttributes(
@ -1253,7 +1260,12 @@ class UrlbarInput {
*/
searchModeShortcut() {
if (this.view.oneOffsRefresh) {
this.setSearchMode({ engineName: Services.search.defaultEngine.name });
// We restrict to search results when entering search mode from this
// shortcut to honor historical behaviour.
this.setSearchMode({
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: Services.search.defaultEngine.name,
});
this.search("");
} else {
this.search(UrlbarTokenizer.RESTRICT.SEARCH);

Просмотреть файл

@ -102,9 +102,9 @@ class ProviderSearchSuggestions extends UrlbarProvider {
}
/**
* Returns whether the user typed a token alias or a restriction token. We use
* this value to override the pref to disable search suggestions in the
* Urlbar.
* Returns whether the user typed a token alias or restriction token, or is in
* search mode. We use this value to override the pref to disable search
* suggestions in the Urlbar.
* @param {UrlbarQueryContext} queryContext The query context object.
* @returns {boolean} True if the user typed a token alias or search
* restriction token.
@ -116,7 +116,9 @@ class ProviderSearchSuggestions extends UrlbarProvider {
queryContext.restrictSource == UrlbarUtils.RESULT_SOURCE.SEARCH) ||
queryContext.tokens.some(
t => t.type == UrlbarTokenizer.TYPE.RESTRICT_SEARCH
)
) ||
(queryContext.searchMode &&
queryContext.sources.includes(UrlbarUtils.RESULT_SOURCE.SEARCH))
);
}

Просмотреть файл

@ -64,7 +64,11 @@ class ProviderTopSites extends UrlbarProvider {
* @returns {boolean} Whether this provider should be invoked for the search.
*/
isActive(queryContext) {
return !queryContext.restrictSource && !queryContext.searchString;
return (
!queryContext.restrictSource &&
!queryContext.searchString &&
!queryContext.searchMode
);
}
/**

Просмотреть файл

@ -491,10 +491,13 @@ var UrlbarTestUtils = {
);
let buttons = oneOffs.getSelectableButtons(true);
searchMode = searchMode || {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: buttons[0].engine.name,
};
if (!searchMode) {
searchMode = { engineName: buttons[0].engine.name };
if (UrlbarUtils.WEB_ENGINE_NAMES.has(searchMode.engineName)) {
searchMode.source = UrlbarUtils.RESULT_SOURCE.SEARCH;
}
}
let oneOff = buttons.find(o =>
searchMode.engineName
? o.engine.name == searchMode.engineName

Просмотреть файл

@ -484,7 +484,6 @@ add_task(async function oneOffClick() {
"Urlbar view is still open."
);
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: oneOffs[0].engine.name,
});
window.gURLBar.setSearchMode({});
@ -535,7 +534,6 @@ add_task(async function oneOffReturn() {
"Urlbar view is still open."
);
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: oneOffs[0].engine.name,
});
window.gURLBar.setSearchMode({});

Просмотреть файл

@ -187,7 +187,6 @@ add_task(async function test_returnAfterSuggestion() {
EventUtils.synthesizeKey("KEY_Enter");
await resultsPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: gEngine.name,
});
await UrlbarTestUtils.exitSearchMode(window, { backspace: true });
@ -259,7 +258,6 @@ add_task(async function test_returnAfterSuggestion_nonDefault() {
EventUtils.synthesizeKey("KEY_Enter");
await resultsPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: gEngine2.name,
});
await UrlbarTestUtils.exitSearchMode(window, { backspace: true });
@ -311,7 +309,6 @@ add_task(async function test_clickAfterSuggestion() {
EventUtils.synthesizeMouseAtCenter(oneOffs[1], {});
await resultsPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: gEngine2.name,
});
await UrlbarTestUtils.exitSearchMode(window, { backspace: true });
@ -363,7 +360,6 @@ add_task(async function test_clickAfterSuggestion_nonDefault() {
EventUtils.synthesizeMouseAtCenter(oneOffs[1], {});
await resultsPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: gEngine2.name,
});
await UrlbarTestUtils.exitSearchMode(window, { backspace: true });

Просмотреть файл

@ -230,10 +230,7 @@ add_task(async function test_search_mode_engine_web() {
add_task(async function test_search_mode_engine_other() {
await doSearchModeTest(
{
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: extraEngine.name,
},
{ engineName: extraEngine.name },
{ id: "urlbar-placeholder-search-mode-other", args: null }
);
});

Просмотреть файл

@ -197,7 +197,6 @@ add_task(async function escape() {
window
).getSelectableButtons(true);
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: oneOffs[0].engine.name,
});
@ -373,7 +372,6 @@ add_task(async function tab_switch() {
window
).getSelectableButtons(true);
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: oneOffs[0].engine.name,
});
@ -411,7 +409,6 @@ add_task(async function tab_switch() {
await BrowserTestUtils.switchTab(gBrowser, tabs[0]);
await searchPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: oneOffs[0].engine.name,
});
@ -435,7 +432,6 @@ add_task(async function tab_switch() {
await BrowserTestUtils.switchTab(gBrowser, tabs[0]);
await searchPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: oneOffs[0].engine.name,
});
@ -451,7 +447,6 @@ add_task(async function tab_switch() {
await BrowserTestUtils.switchTab(gBrowser, tabs[0]);
await searchPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: oneOffs[0].engine.name,
});

Просмотреть файл

@ -82,7 +82,6 @@ add_task(async function replaced_on_space() {
await searchPromise;
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: aliasEngine.name,
});
Assert.ok(!gURLBar.value, "The Urlbar value should be cleared.");

Просмотреть файл

@ -261,7 +261,6 @@ add_task(async function spaceToEnterSearchMode() {
"Panel has no results, therefore should have noresults attribute"
);
UrlbarTestUtils.assertSearchMode(win, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: engine.name,
});
this.Assert.equal(

Просмотреть файл

@ -142,6 +142,52 @@ add_task(async function nonEmptySearch_nonMatching() {
});
});
add_task(async function nonEmptySearch_withHistory() {
// URLs with the same host as the search engine.
await PlacesTestUtils.addVisits([
"http://mochi.test/ciao",
"http://mochi.test/ciao1",
]);
await BrowserTestUtils.withNewTab("about:robots", async function(browser) {
await UrlbarTestUtils.promiseAutocompleteResultPopup({
window,
value: "ciao",
});
await UrlbarTestUtils.enterSearchMode(window);
Assert.equal(gURLBar.value, "ciao", "Urlbar value should be set.");
await checkResults([
{
isSearchHistory: false,
suggestion: undefined,
},
{
isSearchHistory: false,
suggestion: "ciaofoo",
},
{
isSearchHistory: false,
suggestion: "ciaobar",
},
{
isSearchHistory: false,
suggestion: undefined,
historyUrl: "http://mochi.test/ciao1",
},
{
isSearchHistory: false,
suggestion: undefined,
historyUrl: "http://mochi.test/ciao",
},
]);
await UrlbarTestUtils.exitSearchMode(window, { clickClose: true });
});
await PlacesUtils.history.clear();
});
async function checkResults(resultsDetails) {
Assert.equal(
UrlbarTestUtils.getResultCount(window),
@ -150,20 +196,29 @@ async function checkResults(resultsDetails) {
);
for (let i = 0; i < resultsDetails.length; ++i) {
let result = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
Assert.equal(
result.searchParams.engine,
suggestionsEngine.name,
"It should be a search result for our suggestion engine."
);
Assert.equal(
result.searchParams.isSearchHistory,
resultsDetails[i].isSearchHistory,
"Check if it should be a local suggestion result."
);
Assert.equal(
result.searchParams.suggestion,
resultsDetails[i].suggestion,
"Check the suggestion value"
);
if (result.searchParams) {
Assert.equal(
result.searchParams?.engine,
suggestionsEngine.name,
"It should be a search result for our suggestion engine."
);
Assert.equal(
result.searchParams?.isSearchHistory,
resultsDetails[i].isSearchHistory,
"Check if it should be a local suggestion result."
);
Assert.equal(
result.searchParams?.suggestion,
resultsDetails[i].suggestion,
"Check the suggestion value"
);
}
if (resultsDetails[i].historyUrl) {
Assert.equal(
result.url,
resultsDetails[i].historyUrl,
"The history result should have the correct URL."
);
}
}
}

Просмотреть файл

@ -300,7 +300,6 @@ add_task(async function clickAndFillAlias() {
EventUtils.synthesizeMouseAtCenter(testEngineItem, {});
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: testEngineItem.result.payload.engine,
});
@ -393,7 +392,6 @@ add_task(async function enterAndFillAlias() {
EventUtils.synthesizeKey("KEY_Enter");
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: details.searchParams.engine,
});
@ -461,7 +459,6 @@ add_task(async function enterAutofillsAlias() {
EventUtils.synthesizeKey("KEY_Enter");
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: testEngineItem.result.payload.engine,
});

Просмотреть файл

@ -214,7 +214,6 @@ add_task(async function selectSearchTopSite() {
EventUtils.synthesizeMouseAtCenter(amazonSearch, {});
UrlbarTestUtils.assertSearchMode(window, {
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
engineName: amazonSearch.result.payload.engine,
});
gURLBar.setSearchMode({});

Просмотреть файл

@ -492,6 +492,7 @@ function Search(
this._maxResults = queryContext.maxResults;
this._userContextId = queryContext.userContextId;
this._currentPage = queryContext.currentPage;
this._engineName = queryContext.searchMode?.engineName;
} else {
let params = new Set(searchParam.split(" "));
this._enableActions = params.has("enable-actions");
@ -549,10 +550,6 @@ function Search(
let behavior = sourceToBehaviorMap.get(queryContext.restrictSource);
this.setBehavior(behavior);
if (behavior == "search" && queryContext.engineName) {
this._engineName = queryContext.engineName;
}
// When we are in restrict mode, all the tokens are valid for searching, so
// there is no _heuristicToken.
this._heuristicToken = null;
@ -790,6 +787,16 @@ Search.prototype = {
return;
}
// this._engineName is set if the user is in search mode. We fetch only
// local results with the same host as the search mode engine.
if (this._engineName && !this._keywordSubstitute) {
let engine = Services.search.getEngineByName(this._engineName);
this._keywordSubstitute = {
host: engine.getResultDomain(),
keyword: null,
};
}
// Add the first heuristic result, if any. Set _addingHeuristicResult
// to true so that when the result is added, "heuristic" can be included in
// its style.
@ -1085,7 +1092,10 @@ Search.prototype = {
this._addMatch(match);
if (!this._keywordSubstitute) {
this._keywordSubstitute = entry.url.host;
this._keywordSubstitute = {
host: entry.url.host,
keyword,
};
}
return true;
},
@ -1104,7 +1114,10 @@ Search.prototype = {
};
this._addSearchEngineMatch(this._searchEngineAliasMatch);
if (!this._keywordSubstitute) {
this._keywordSubstitute = engine.getResultDomain();
this._keywordSubstitute = {
host: engine.getResultDomain(),
keyword: alias,
};
}
return true;
},
@ -1695,7 +1708,10 @@ Search.prototype = {
get _keywordSubstitutedSearchString() {
let tokens = this._searchTokens.map(t => t.value);
if (this._keywordSubstitute) {
tokens = [this._keywordSubstitute, ...tokens.slice(1)];
tokens = [
this._keywordSubstitute.host,
...tokens.slice(this._keywordSubstitute.keyword ? 1 : 0),
];
}
return tokens.join(" ");
},