Bug 1648385 - Allow search suggestions in the Urlbar when they are disabled by a pref but a restriction token or token alias is used. r=adw

Differential Revision: https://phabricator.services.mozilla.com/D81166
This commit is contained in:
Harry Twyford 2020-06-26 13:55:00 +00:00
Родитель be1983c22c
Коммит c2ab0a3fe6
3 изменённых файлов: 153 добавлений и 6 удалений

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

@ -104,6 +104,25 @@ 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.
* @param {UrlbarQueryContext} queryContext The query context object.
* @returns {boolean} True if the user typed a token alias or search
* restriction token.
*/
_isTokenOrRestrictionPresent(queryContext) {
return (
queryContext.searchString.startsWith("@") ||
(queryContext.restrictSource &&
queryContext.restrictSource == UrlbarUtils.RESULT_SOURCE.SEARCH) ||
queryContext.tokens.some(
t => t.type == UrlbarTokenizer.TYPE.RESTRICT_SEARCH
)
);
}
/**
* Returns whether suggestions in general are allowed for a given query
* context. If this returns false, then we shouldn't fetch either form
@ -118,7 +137,10 @@ class ProviderSearchSuggestions extends UrlbarProvider {
_allowSuggestions(queryContext) {
if (
!queryContext.allowSearchSuggestions ||
!UrlbarPrefs.get("suggest.searches") ||
// If the user typed a restriction token or token alias, we ignore the
// pref to disable suggestions in the Urlbar.
(!UrlbarPrefs.get("suggest.searches") &&
!this._isTokenOrRestrictionPresent(queryContext)) ||
!UrlbarPrefs.get("browser.search.suggest.enabled") ||
(queryContext.isPrivate &&
!UrlbarPrefs.get("browser.search.suggest.enabled.private"))
@ -141,10 +163,10 @@ class ProviderSearchSuggestions extends UrlbarProvider {
}
// Skip all remaining checks and allow remote suggestions at this point if
// the user used a search engine token alias. We want "@engine query" to
// return suggestions from the engine. We'll return early from startQuery
// the user used a token alias or restriction token. We want "@engine query"
// to return suggestions from the engine. We'll return early from startQuery
// if the query doesn't match an alias.
if (queryContext.searchString.startsWith("@")) {
if (this._isTokenOrRestrictionPresent(queryContext)) {
return true;
}

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

@ -173,6 +173,78 @@ add_task(async function disabled_privateWindow() {
await cleanUpSuggestions();
});
add_task(async function disabled_urlbarSuggestions_withRestrictionToken() {
Services.prefs.setBoolPref(SUGGEST_PREF, false);
Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, true);
let context = createContext(
`${UrlbarTokenizer.RESTRICT.SEARCH} ${SEARCH_STRING}`,
{ isPrivate: false }
);
await check_results({
context,
matches: [
makeSearchResult(context, {
query: SEARCH_STRING,
engineName: ENGINE_NAME,
heuristic: true,
}),
...makeExpectedSuggestionResults(context, {
query: SEARCH_STRING,
}),
],
});
await cleanUpSuggestions();
});
add_task(
async function disabled_urlbarSuggestions_withRestrictionToken_private() {
Services.prefs.setBoolPref(SUGGEST_PREF, false);
Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, true);
Services.prefs.setBoolPref(PRIVATE_ENABLED_PREF, false);
let context = createContext(
`${UrlbarTokenizer.RESTRICT.SEARCH} ${SEARCH_STRING}`,
{ isPrivate: true }
);
await check_results({
context,
matches: [
makeSearchResult(context, {
query: SEARCH_STRING,
engineName: ENGINE_NAME,
heuristic: true,
}),
],
});
await cleanUpSuggestions();
}
);
add_task(
async function disabled_urlbarSuggestions_withRestrictionToken_private_enabled() {
Services.prefs.setBoolPref(SUGGEST_PREF, false);
Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, true);
Services.prefs.setBoolPref(PRIVATE_ENABLED_PREF, true);
let context = createContext(
`${UrlbarTokenizer.RESTRICT.SEARCH} ${SEARCH_STRING}`,
{ isPrivate: true }
);
await check_results({
context,
matches: [
makeSearchResult(context, {
query: SEARCH_STRING,
engineName: ENGINE_NAME,
heuristic: true,
}),
...makeExpectedSuggestionResults(context, {
query: SEARCH_STRING,
}),
],
});
await cleanUpSuggestions();
}
);
add_task(async function enabled_by_pref_privateWindow() {
Services.prefs.setBoolPref(SUGGEST_PREF, true);
Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, true);

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

@ -7,10 +7,16 @@
*/
const SUGGESTIONS_ENGINE_NAME = "engine-suggestions.xml";
const SUGGEST_PREF = "browser.urlbar.suggest.searches";
const SUGGEST_ENABLED_PREF = "browser.search.suggest.enabled";
let engine;
add_task(async function setup() {
engine = await addTestSuggestionsEngine();
});
add_task(async function engineWithSuggestions() {
let engine = await addTestSuggestionsEngine();
// History matches should not appear with @ aliases, so this visit/match
// should not appear when searching with the @ alias below.
let historyTitle = "fire";
@ -126,3 +132,50 @@ add_task(async function engineWithSuggestions() {
await PlacesUtils.bookmarks.eraseEverything();
await PlacesUtils.history.clear();
});
add_task(async function disabled_urlbarSuggestions() {
Services.prefs.setBoolPref(SUGGEST_PREF, false);
Services.prefs.setBoolPref(SUGGEST_ENABLED_PREF, true);
let alias = "@moz";
engine.alias = alias;
Assert.equal(engine.alias, alias);
for (let private of [false, true]) {
let context = createContext(`${alias} term`, { isPrivate: private });
let expectedMatches = [
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
query: "term",
heuristic: true,
}),
];
if (!private) {
expectedMatches.push(
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
query: "term",
suggestion: "term foo",
})
);
expectedMatches.push(
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
query: "term",
suggestion: "term bar",
})
);
}
await check_results({
context,
matches: expectedMatches,
});
}
engine.alias = "";
Services.prefs.clearUserPref(SUGGEST_PREF);
Services.prefs.clearUserPref(SUGGEST_ENABLED_PREF);
});