Skip to content

Commit

Permalink
Bug 1868756 - For users with the search bar disabled, "Show search su…
Browse files Browse the repository at this point in the history
…ggestions" should be unchecked when either browser.search.suggest.enabled or browser.urlbar.suggest.searches are false. r=dao, a=dmeehan

Differential Revision: https://phabricator.services.mozilla.com/D195889
  • Loading branch information
klubana-m committed Dec 27, 2023
1 parent ddde8ff commit 2784097
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 41 deletions.
3 changes: 1 addition & 2 deletions browser/components/preferences/search.inc.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
data-l10n-id="search-suggestions-desc" />

<checkbox id="suggestionsInSearchFieldsCheckbox"
data-l10n-id="search-show-suggestions-option"
preference="browser.search.suggest.enabled"/>
data-l10n-id="search-show-suggestions-option" />
<vbox class="indent">
<checkbox id="urlBarSuggestion" data-l10n-id="search-show-suggestions-url-bar-option" />
<checkbox id="showSearchSuggestionsFirstCheckbox"
Expand Down
18 changes: 17 additions & 1 deletion browser/components/preferences/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var gSearchPane = {
let privateSuggestsPref = Preferences.get(
"browser.search.suggest.enabled.private"
);

let updateSuggestionCheckboxes =
this._updateSuggestionCheckboxes.bind(this);
suggestsPref.on("change", updateSuggestionCheckboxes);
Expand All @@ -83,8 +84,16 @@ var gSearchPane = {
let suggestionsInSearchFieldsCheckbox = document.getElementById(
"suggestionsInSearchFieldsCheckbox"
);
// We only want to call _updateSuggestionCheckboxes once after updating
// all prefs.
suggestionsInSearchFieldsCheckbox.addEventListener("command", () => {
urlbarSuggestsPref.value = suggestionsInSearchFieldsCheckbox.checked;
this._skipUpdateSuggestionCheckboxesFromPrefChanges = true;
if (!searchBarPref.value) {
urlbarSuggestsPref.value = suggestionsInSearchFieldsCheckbox.checked;
}
suggestsPref.value = suggestionsInSearchFieldsCheckbox.checked;
this._skipUpdateSuggestionCheckboxesFromPrefChanges = false;
this._updateSuggestionCheckboxes();
});
let privateWindowCheckbox = document.getElementById(
"showSearchSuggestionsPrivateWindows"
Expand Down Expand Up @@ -185,6 +194,9 @@ var gSearchPane = {
},

_updateSuggestionCheckboxes() {
if (this._skipUpdateSuggestionCheckboxesFromPrefChanges) {
return;
}
let suggestsPref = Preferences.get("browser.search.suggest.enabled");
let permanentPB = Services.prefs.getBoolPref(
"browser.privatebrowsing.autostart"
Expand All @@ -202,6 +214,10 @@ var gSearchPane = {
let urlbarSuggestsPref = Preferences.get("browser.urlbar.suggest.searches");
let searchBarPref = Preferences.get("browser.search.widget.inNavBar");

suggestionsInSearchFieldsCheckbox.checked =
suggestsPref.value &&
(searchBarPref.value ? true : urlbarSuggestsPref.value);

urlbarSuggests.disabled = !suggestsPref.value || permanentPB;
urlbarSuggests.hidden = !searchBarPref.value;

Expand Down
175 changes: 137 additions & 38 deletions browser/components/preferences/tests/browser_searchsuggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,60 @@ add_setup(async function () {
});
});

async function toggleElement(
id,
prefName,
element,
initialCheckboxValue,
initialPrefValue,
descCheckbox,
descPref,
shouldUpdatePref = false
) {
await BrowserTestUtils.synthesizeMouseAtCenter(
`#${id}`,
{},
gBrowser.selectedBrowser
);
is(
element.checked,
!initialCheckboxValue,
`Should have flipped the ${descCheckbox} checkbox`
);
let expectedPrefValue = shouldUpdatePref
? !initialPrefValue
: initialPrefValue;
let prefValue = Services.prefs.getBoolPref(prefName);

is(
prefValue,
expectedPrefValue,
`Should ${
shouldUpdatePref ? "" : "not"
} have updated the ${descPref} preference value`
);

await BrowserTestUtils.synthesizeMouseAtCenter(
`#${id}`,
{},
gBrowser.selectedBrowser
);
is(
element.checked,
initialCheckboxValue,
`Should have flipped the ${descCheckbox} checkbox back to the original value`
);
prefValue = Services.prefs.getBoolPref(prefName);
expectedPrefValue = shouldUpdatePref ? !expectedPrefValue : initialPrefValue;
is(
prefValue,
expectedPrefValue,
`Should ${
shouldUpdatePref ? "" : "not"
} have updated the ${descPref} preference value`
);
}

// Open with suggestions enabled
add_task(async function test_suggestions_start_enabled() {
Services.prefs.setBoolPref(SUGGEST_PREF_NAME, true);
Expand All @@ -49,55 +103,26 @@ add_task(async function test_suggestions_start_enabled() {
"Should have the correct value for the private mode suggestions checkbox"
);

async function toggleElement(id, prefName, element, initialValue, desc) {
await BrowserTestUtils.synthesizeMouseAtCenter(
`#${id}`,
{},
gBrowser.selectedBrowser
);
is(
element.checked,
!initialValue,
`Should have flipped the ${desc} checkbox`
);
let prefValue = Services.prefs.getBoolPref(prefName);
is(
prefValue,
!initialValue,
`Should have updated the ${desc} preference value`
);

await BrowserTestUtils.synthesizeMouseAtCenter(
`#${id}`,
{},
gBrowser.selectedBrowser
);
is(
element.checked,
initialValue,
`Should have flipped the ${desc} checkbox back to the original value`
);
prefValue = Services.prefs.getBoolPref(prefName);
is(
prefValue,
initialValue,
`Should have updated the ${desc} preference back to the original value`
);
}

await toggleElement(
"urlBarSuggestion",
URLBAR_SUGGEST_PREF_NAME,
urlbarBox,
initialUrlbarSuggestValue,
"urlbar"
initialUrlbarSuggestValue,
"urlbar",
"urlbar",
true
);

await toggleElement(
"showSearchSuggestionsPrivateWindows",
PRIVATE_PREF_NAME,
privateBox,
initialSuggestionsInPrivateValue,
"private suggestion"
initialSuggestionsInPrivateValue,
"private suggestion",
"private suggestion",
true
);

Services.prefs.setBoolPref(SUGGEST_PREF_NAME, false);
Expand Down Expand Up @@ -130,3 +155,77 @@ add_task(async function test_suggestions_start_disabled() {

gBrowser.removeCurrentTab();
});

add_task(async function test_sync_search_suggestions_prefs() {
info("Adding the search bar to the toolbar");
Services.prefs.setBoolPref(SEARCHBAR_PREF_NAME, true);
Services.prefs.setBoolPref(SUGGEST_PREF_NAME, true);
Services.prefs.setBoolPref(URLBAR_SUGGEST_PREF_NAME, false);
await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });

let doc = gBrowser.selectedBrowser.contentDocument;
let suggestionsInSearchFieldsCheckbox = doc.getElementById(
"suggestionsInSearchFieldsCheckbox"
);

is(
suggestionsInSearchFieldsCheckbox.checked,
true,
"Should have the correct value for the search suggestions checkbox"
);

is(
Services.prefs.getBoolPref(SUGGEST_PREF_NAME),
true,
`${SUGGEST_PREF_NAME} should be enabled`
);
is(
Services.prefs.getBoolPref(URLBAR_SUGGEST_PREF_NAME),
false,
`${URLBAR_SUGGEST_PREF_NAME} should be disabled`
);

await toggleElement(
"suggestionsInSearchFieldsCheckbox",
URLBAR_SUGGEST_PREF_NAME,
suggestionsInSearchFieldsCheckbox,
suggestionsInSearchFieldsCheckbox.checked,
false,
"search suggestion",
"urlbar suggest"
);

info("Removing the search bar from the toolbar");
Services.prefs.setBoolPref(SEARCHBAR_PREF_NAME, false);

const suggestsPref = [true, false];
const urlbarSuggestsPref = [true, false];

for (let suggestState of suggestsPref) {
for (let urlbarSuggestsState of urlbarSuggestsPref) {
Services.prefs.setBoolPref(SUGGEST_PREF_NAME, suggestState);
Services.prefs.setBoolPref(URLBAR_SUGGEST_PREF_NAME, urlbarSuggestsState);

if (suggestState && urlbarSuggestsState) {
ok(
suggestionsInSearchFieldsCheckbox.checked,
"Should have checked the suggestions checkbox"
);
} else {
ok(
!suggestionsInSearchFieldsCheckbox.checked,
"Should have unchecked the suggestions checkbox"
);
}
ok(
!suggestionsInSearchFieldsCheckbox.disabled,
"Should have the suggestions checkbox enabled"
);
}
}

gBrowser.removeCurrentTab();
Services.prefs.clearUserPref(SEARCHBAR_PREF_NAME);
Services.prefs.clearUserPref(URLBAR_SUGGEST_PREF_NAME);
Services.prefs.clearUserPref(SUGGEST_PREF_NAME);
});

0 comments on commit 2784097

Please sign in to comment.