Bug 1813155 - Quick suggest provider should not be active for search strings that contain only spaces. r=mak

I broke this in D167218. We didn't have a test for it.

Differential Revision: https://phabricator.services.mozilla.com/D168139
This commit is contained in:
Drew Willcoxon
2023-01-30 16:45:32 +00:00
parent a7aaa20a65
commit b2d3b1dc40
2 changed files with 36 additions and 3 deletions

View File

@@ -223,6 +223,15 @@ class ProviderQuickSuggest extends UrlbarProvider {
if (!queryContext.searchString) {
return !!lazy.QuickSuggest.weather.suggestion;
}
// Trim only the start of the search string because a trailing space can
// affect the suggestions.
let trimmedSearchString = queryContext.searchString.trimStart();
if (!trimmedSearchString) {
return false;
}
this._trimmedSearchString = trimmedSearchString;
return (
lazy.UrlbarPrefs.get("suggest.quicksuggest.nonsponsored") ||
lazy.UrlbarPrefs.get("suggest.quicksuggest.sponsored") ||
@@ -332,9 +341,7 @@ class ProviderQuickSuggest extends UrlbarProvider {
return;
}
// Trim only the start of the search string because a trailing space can
// affect the suggestions.
let searchString = queryContext.searchString.trimStart();
let searchString = this._trimmedSearchString;
// There are two sources for quick suggest: remote settings and Merino.
let promises = [];

View File

@@ -370,6 +370,32 @@ add_task(async function caseInsensitiveAndLeadingSpaces() {
});
});
// The provider should not be active for search strings that are empty or
// contain only spaces.
add_task(async function emptySearchStringsAndSpaces() {
UrlbarPrefs.set("suggest.quicksuggest.nonsponsored", true);
UrlbarPrefs.set("suggest.quicksuggest.sponsored", true);
let searchStrings = ["", " ", " ", " "];
for (let str of searchStrings) {
let msg = JSON.stringify(str) + ` (length = ${str.length})`;
info("Testing search string: " + msg);
let context = createContext(str, {
providers: [UrlbarProviderQuickSuggest.name],
isPrivate: false,
});
await check_results({
context,
matches: [],
});
Assert.ok(
!UrlbarProviderQuickSuggest.isActive(context),
"Provider should not be active for search string: " + msg
);
}
});
// Results should be returned even when `browser.search.suggest.enabled` is
// false.
add_task(async function browser_search_suggest_enabled() {