Bug 1906271 - Don't show the default engine icon when keyword.enabled is set to false. r=daleharvey,urlbar-reviewers,dao

Differential Revision: https://phabricator.services.mozilla.com/D218347
This commit is contained in:
Karandeep
2024-08-23 16:05:52 +00:00
parent 9c271bfe95
commit eab49e1415
2 changed files with 98 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ export class SearchModeSwitcher {
"nsISupportsWeakReference",
]);
Services.obs.addObserver(this, "browser-search-engine-modified", true);
lazy.UrlbarPrefs.addObserver(this);
this.#popup = input.document.getElementById("searchmode-switcher-popup");
@@ -181,6 +182,20 @@ export class SearchModeSwitcher {
}
}
/**
* Called when a urlbar pref changes.
*
* @param {string} pref
* The name of the pref relative to `browser.urlbar`.
*/
onPrefChanged(pref) {
switch (pref) {
case "keyword.enabled":
this.#updateSearchIcon();
break;
}
}
async #updateSearchIcon() {
try {
await lazy.UrlbarSearchUtils.init();
@@ -190,6 +205,13 @@ export class SearchModeSwitcher {
let { label, icon } = await this.#getDisplayedEngineDetails(
this.#input.searchMode
);
const keywordEnabled = lazy.UrlbarPrefs.get("keyword.enabled");
const inSearchMode = this.#input.searchMode;
if (!keywordEnabled && !inSearchMode) {
icon = lazy.UrlbarUtils.ICON.SEARCH_GLASS;
}
let iconUrl = icon ? `url(${icon})` : "";
this.#input.document.getElementById(
"searchmode-switcher-icon"
@@ -199,9 +221,11 @@ export class SearchModeSwitcher {
"urlbar-searchmode-button",
{ engine: label }
);
let labelEl = this.#input.document.getElementById(
"searchmode-switcher-title"
);
if (!this.#input.searchMode) {
labelEl.replaceChildren();
} else if (this.#input.searchMode) {

View File

@@ -325,3 +325,77 @@ add_task(async function test_search_icon_change() {
);
await BrowserTestUtils.closeWindow(newWin);
});
add_task(async function test_search_icon_change_without_keyword_enabled() {
await SpecialPowers.pushPrefEnv({
set: [["keyword.enabled", false]],
});
let newWin = await BrowserTestUtils.openNewBrowserWindow();
let searchModeSwitcherButton = newWin.document.getElementById(
"searchmode-switcher-icon"
);
let regex = /url\("([^"]+)"\)/;
let searchModeSwitcherIconUrl =
searchModeSwitcherButton.style.listStyleImage.match(regex);
const searchGlassIconUrl = UrlbarUtils.ICON.SEARCH_GLASS;
Assert.equal(
searchModeSwitcherIconUrl[1],
searchGlassIconUrl,
"The search mode switcher should have the search glass icon url since \
keyword.enabled is false and we are not in search mode."
);
let popup = UrlbarTestUtils.searchModeSwitcherPopup(newWin);
let engineName = "Bing";
info("Open the urlbar and searchmode switcher popup");
await UrlbarTestUtils.promiseAutocompleteResultPopup({
window: newWin,
value: "",
});
await UrlbarTestUtils.openSearchModeSwitcher(newWin);
info("Press on the bing menu button and enter search mode");
let popupHidden = UrlbarTestUtils.searchModeSwitcherPopupClosed(newWin);
popup.querySelector(`toolbarbutton[label=${engineName}]`).click();
await popupHidden;
const bingSearchEngineIconUrl = await Services.search
.getEngineByName(engineName)
.getIconURL();
searchModeSwitcherIconUrl =
searchModeSwitcherButton.style.listStyleImage.match(regex);
Assert.equal(
searchModeSwitcherIconUrl[1],
bingSearchEngineIconUrl,
"The search mode switcher should have the bing icon url since we are in \
search mode"
);
await UrlbarTestUtils.assertSearchMode(newWin, {
engineName: "Bing",
entry: "other",
source: 3,
});
info("Press the close button and exit search mode");
newWin.document.querySelector("#searchmode-switcher-close").click();
await UrlbarTestUtils.assertSearchMode(newWin, null);
searchModeSwitcherIconUrl = await BrowserTestUtils.waitForCondition(
() => searchModeSwitcherButton.style.listStyleImage.match(regex),
"Waiting for the search mode switcher icon to update after exiting search mode."
);
Assert.equal(
searchModeSwitcherIconUrl[1],
searchGlassIconUrl,
"The search mode switcher should have the search glass icon url since \
keyword.enabled is false"
);
await BrowserTestUtils.closeWindow(newWin);
});