Bug 1681512 - Unchecking a search engine should also disable tab-to-search for it. r=harry

Differential Revision: https://phabricator.services.mozilla.com/D99370
This commit is contained in:
Marco Bonardo 2020-12-10 17:56:38 +00:00
Родитель 042febda06
Коммит 0148f5315e
4 изменённых файлов: 90 добавлений и 1 удалений

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

@ -314,6 +314,7 @@ class ProviderTabToSearch extends UrlbarProvider {
// Add all matching engines.
let engines = await UrlbarSearchUtils.enginesForDomainPrefix(searchStr, {
matchAllDomainLevels: true,
onlyEnabled: true,
});
if (!engines.length) {
return;

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

@ -64,13 +64,26 @@ class SearchUtils {
* Match at each sub domain, for example "a.b.c.com" will be matched at
* "a.b.c.com", "b.c.com", and "c.com". Partial matches are always returned
* after perfect matches.
* @param {boolean} [options.onlyEnabled]
* Match only engines that have not been disabled on the Search Preferences
* list.
* @returns {Array<nsISearchEngine>}
* An array of all matching engines. An empty array if there are none.
*/
async enginesForDomainPrefix(prefix, { matchAllDomainLevels = false } = {}) {
async enginesForDomainPrefix(
prefix,
{ matchAllDomainLevels = false, onlyEnabled = false } = {}
) {
await this.init();
prefix = prefix.toLowerCase();
let disabledEngines = onlyEnabled
? Services.prefs
.getStringPref("browser.search.hiddenOneOffs", "")
.split(",")
.filter(e => !!e)
: [];
// Array of partially matched engines, added through matchPrefix().
let partialMatchEngines = [];
function matchPrefix(engine, engineHost) {
@ -90,6 +103,9 @@ class SearchUtils {
// Array of fully matched engines.
let engines = [];
for (let engine of await Services.search.getVisibleEngines()) {
if (disabledEngines.includes(engine.name)) {
continue;
}
let domain = engine.getResultDomain();
if (domain.startsWith(prefix) || domain.startsWith("www." + prefix)) {
engines.push(engine);

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

@ -54,6 +54,26 @@ add_task(async function hide_search_engine_nomatch() {
Assert.ok(matchedEngine2);
});
add_task(async function onlyEnabled_option_nomatch() {
let engine = await Services.search.getDefault();
let domain = engine.getResultDomain();
let token = domain.substr(0, 1);
Services.prefs.setCharPref("browser.search.hiddenOneOffs", engine.name);
let matchedEngines = await UrlbarSearchUtils.enginesForDomainPrefix(token, {
onlyEnabled: true,
});
Assert.ok(
!matchedEngines.length || matchedEngines[0].getResultDomain() != domain
);
Services.prefs.clearUserPref("browser.search.hiddenOneOffs");
matchedEngines = await UrlbarSearchUtils.enginesForDomainPrefix(token, {
onlyEnabled: true,
});
Assert.ok(
matchedEngines.length && matchedEngines[0].getResultDomain() == domain
);
});
add_task(async function add_search_engine_match() {
let promiseTopic = promiseSearchTopic("engine-added");
Assert.equal(

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

@ -410,3 +410,55 @@ add_task(async function test_publicSuffixIsHost() {
await cleanupPlaces();
await Services.search.removeEngine(suffixEngine);
});
add_task(async function test_disabledEngine() {
info("Tab-to-search results does not appear for a Pref-disabled engine.");
let engine = await Services.search.addEngineWithDetails("Disabled", {
template: "https://disabled.com/?search={searchTerms}",
});
await PlacesTestUtils.addVisits(["https://disabled.com/"]);
let context = createContext("dis", { isPrivate: false });
info("Sanity check that the engine would appear.");
await check_results({
context,
autofilled: "disabled.com/",
completed: "https://disabled.com/",
matches: [
makeVisitResult(context, {
uri: "https://disabled.com/",
title: "https://disabled.com",
heuristic: true,
providerName: "Autofill",
}),
makeSearchResult(context, {
engineName: engine.name,
engineIconUri: UrlbarUtils.ICON.SEARCH_GLASS_INVERTED,
uri: UrlbarUtils.stripPublicSuffixFromHost(engine.getResultDomain()),
keywordOffer: UrlbarUtils.KEYWORD_OFFER.SHOW,
query: "",
providerName: "TabToSearch",
}),
],
});
info("Now disable the engine.");
Services.prefs.setCharPref("browser.search.hiddenOneOffs", engine.name);
await check_results({
context,
autofilled: "disabled.com/",
completed: "https://disabled.com/",
matches: [
makeVisitResult(context, {
uri: "https://disabled.com/",
title: "https://disabled.com",
heuristic: true,
providerName: "Autofill",
}),
],
});
Services.prefs.clearUserPref("browser.search.hiddenOneOffs");
await cleanupPlaces();
await Services.search.removeEngine(engine);
});