Bug 1899500 - Implement explicit resource management in Baseline compiler. r=arai
[gecko.git] / browser / components / preferences / tests / browser_search_searchTerms.js
blob2f82ddd9ea7cc42ef6818e4c8fece7d62d5a9e2e
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 /*
5   Tests the showSearchTerms option on the about:preferences#search page.
6 */
8 "use strict";
10 ChromeUtils.defineLazyGetter(this, "QuickSuggestTestUtils", () => {
11   const { QuickSuggestTestUtils: module } = ChromeUtils.importESModule(
12     "resource://testing-common/QuickSuggestTestUtils.sys.mjs"
13   );
14   module.init(this);
15   return module;
16 });
18 const { CustomizableUITestUtils } = ChromeUtils.importESModule(
19   "resource://testing-common/CustomizableUITestUtils.sys.mjs"
21 let gCUITestUtils = new CustomizableUITestUtils(window);
23 const CHECKBOX_ID = "searchShowSearchTermCheckbox";
24 const PREF_SEARCHTERMS = "browser.urlbar.showSearchTerms.enabled";
25 const PREF_FEATUREGATE = "browser.urlbar.showSearchTerms.featureGate";
28   If Nimbus experiment is enabled, check option visibility.
30 add_task(async function showSearchTermsVisibility_experiment_beforeOpen() {
31   await SpecialPowers.pushPrefEnv({
32     set: [[PREF_FEATUREGATE, false]],
33   });
34   await QuickSuggestTestUtils.withExperiment({
35     valueOverrides: {
36       showSearchTermsFeatureGate: true,
37     },
38     callback: async () => {
39       await openPreferencesViaOpenPreferencesAPI("search", {
40         leaveOpen: true,
41       });
42       let doc = gBrowser.selectedBrowser.contentDocument;
43       let container = doc.getElementById(CHECKBOX_ID);
44       Assert.ok(
45         BrowserTestUtils.isVisible(container),
46         "The option box is visible"
47       );
48       gBrowser.removeCurrentTab();
49     },
50   });
51   await SpecialPowers.popPrefEnv();
52 });
55   If Nimbus experiment is not enabled initially but eventually enabled,
56   check option visibility on Preferences page.
58 add_task(async function showSearchTermsVisibility_experiment_afterOpen() {
59   await SpecialPowers.pushPrefEnv({
60     set: [[PREF_FEATUREGATE, false]],
61   });
62   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
63   let doc = gBrowser.selectedBrowser.contentDocument;
64   let container = doc.getElementById(CHECKBOX_ID);
65   Assert.ok(
66     BrowserTestUtils.isHidden(container),
67     "The option box is initially hidden."
68   );
70   // Install experiment.
71   await QuickSuggestTestUtils.withExperiment({
72     valueOverrides: {
73       showSearchTermsFeatureGate: true,
74     },
75     callback: async () => {
76       Assert.ok(
77         BrowserTestUtils.isVisible(container),
78         "The option box is visible"
79       );
80     },
81   });
83   Assert.ok(
84     BrowserTestUtils.isHidden(container),
85     "The option box is hidden again after the experiment is uninstalled."
86   );
88   gBrowser.removeCurrentTab();
89   await SpecialPowers.popPrefEnv();
90 });
93   Check using the checkbox modifies the preference.
95 add_task(async function showSearchTerms_checkbox() {
96   // Enable the feature.
97   await SpecialPowers.pushPrefEnv({
98     set: [[PREF_FEATUREGATE, true]],
99   });
100   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
101   let doc = gBrowser.selectedBrowser.contentDocument;
103   let option = doc.getElementById(CHECKBOX_ID);
105   // Evaluate checkbox pref is true.
106   Assert.ok(option.checked, "Option box should be checked.");
108   // Evaluate checkbox when pref is false.
109   await SpecialPowers.pushPrefEnv({
110     set: [[PREF_SEARCHTERMS, false]],
111   });
112   Assert.ok(!option.checked, "Option box should not be checked.");
113   await SpecialPowers.popPrefEnv();
115   // Evaluate pref when checkbox is un-checked.
116   await BrowserTestUtils.synthesizeMouseAtCenter(
117     "#" + CHECKBOX_ID,
118     {},
119     gBrowser.selectedBrowser
120   );
121   Assert.equal(
122     Services.prefs.getBoolPref(PREF_SEARCHTERMS),
123     false,
124     "Preference should be false if un-checked."
125   );
127   // Evaluate pref when checkbox is checked.
128   await BrowserTestUtils.synthesizeMouseAtCenter(
129     "#" + CHECKBOX_ID,
130     {},
131     gBrowser.selectedBrowser
132   );
133   Assert.equal(
134     Services.prefs.getBoolPref(PREF_SEARCHTERMS),
135     true,
136     "Preference should be true if checked."
137   );
139   // Clean-up.
140   Services.prefs.clearUserPref(PREF_SEARCHTERMS);
141   gBrowser.removeCurrentTab();
142   await SpecialPowers.popPrefEnv();
146   When loading the search preferences panel, the
147   showSearchTerms checkbox should be hidden if
148   the search bar is enabled.
150 add_task(async function showSearchTerms_and_searchBar_preference_load() {
151   // Enable the feature.
152   await SpecialPowers.pushPrefEnv({
153     set: [[PREF_FEATUREGATE, true]],
154   });
155   await gCUITestUtils.addSearchBar();
157   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
158   let doc = gBrowser.selectedBrowser.contentDocument;
160   let checkbox = doc.getElementById(CHECKBOX_ID);
161   Assert.ok(
162     checkbox.hidden,
163     "showSearchTerms checkbox should be hidden when search bar is enabled."
164   );
166   // Clean-up.
167   gBrowser.removeCurrentTab();
168   await SpecialPowers.popPrefEnv();
169   gCUITestUtils.removeSearchBar();
173   If the search bar is enabled while the search
174   preferences panel is open, the showSearchTerms
175   checkbox should not be clickable.
177 add_task(async function showSearchTerms_and_searchBar_preference_change() {
178   // Enable the feature.
179   await SpecialPowers.pushPrefEnv({
180     set: [[PREF_FEATUREGATE, true]],
181   });
183   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
184   let doc = gBrowser.selectedBrowser.contentDocument;
186   let checkbox = doc.getElementById(CHECKBOX_ID);
187   Assert.ok(!checkbox.hidden, "showSearchTerms checkbox should be shown.");
189   await gCUITestUtils.addSearchBar();
190   Assert.ok(
191     checkbox.hidden,
192     "showSearchTerms checkbox should be hidden when search bar is enabled."
193   );
195   // Clean-up.
196   gCUITestUtils.removeSearchBar();
197   Assert.ok(!checkbox.hidden, "showSearchTerms checkbox should be shown.");
199   gBrowser.removeCurrentTab();
200   await SpecialPowers.popPrefEnv();