summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_search_searchTerms.js
blob: 0af0591355ea332380da4832f8549cf2290062a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/*
  Tests the showSearchTerms option on the about:preferences#search page.
*/

"use strict";

XPCOMUtils.defineLazyGetter(this, "QuickSuggestTestUtils", () => {
  const { QuickSuggestTestUtils: module } = ChromeUtils.importESModule(
    "resource://testing-common/QuickSuggestTestUtils.sys.mjs"
  );
  module.init(this);
  return module;
});

const GROUP_ID = "searchbarGroup";
const CHECKBOX_ID = "searchShowSearchTermCheckbox";
const PREF_SEARCHTERMS = "browser.urlbar.showSearchTerms.enabled";
const PREF_FEATUREGATE = "browser.urlbar.showSearchTerms.featureGate";

/*
  If Nimbus experiment is enabled, check option visibility.
*/
add_task(async function showSearchTermsVisibility_experiment_beforeOpen() {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_FEATUREGATE, false]],
  });
  await QuickSuggestTestUtils.withExperiment({
    valueOverrides: {
      showSearchTermsFeatureGate: true,
    },
    callback: async () => {
      await openPreferencesViaOpenPreferencesAPI("search", {
        leaveOpen: true,
      });
      let doc = gBrowser.selectedBrowser.contentDocument;
      let container = doc.getElementById(CHECKBOX_ID);
      Assert.ok(
        BrowserTestUtils.is_visible(container),
        "The option box is visible"
      );
      gBrowser.removeCurrentTab();
    },
  });
  await SpecialPowers.popPrefEnv();
});

/*
  If Nimbus experiment is not enabled initially but eventually enabled,
  check option visibility on Preferences page.
*/
add_task(async function showSearchTermsVisibility_experiment_afterOpen() {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_FEATUREGATE, false]],
  });
  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;
  let container = doc.getElementById(CHECKBOX_ID);
  Assert.ok(
    BrowserTestUtils.is_hidden(container),
    "The option box is initially hidden."
  );

  // Install experiment.
  await QuickSuggestTestUtils.withExperiment({
    valueOverrides: {
      showSearchTermsFeatureGate: true,
    },
    callback: async () => {
      Assert.ok(
        BrowserTestUtils.is_visible(container),
        "The option box is visible"
      );
    },
  });

  Assert.ok(
    BrowserTestUtils.is_hidden(container),
    "The option box is hidden again after the experiment is uninstalled."
  );

  gBrowser.removeCurrentTab();
  await SpecialPowers.popPrefEnv();
});

/*
  Check using the checkbox modifies the preference.
*/
add_task(async function showSearchTerms_checkbox() {
  // Enable the feature.
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_FEATUREGATE, true]],
  });
  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;
  doc.getElementById(GROUP_ID).scrollIntoView();

  let option = doc.getElementById(CHECKBOX_ID);

  // Evaluate checkbox pref is true.
  Assert.ok(option.checked, "Option box should be checked.");

  // Evaluate checkbox when pref is false.
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_SEARCHTERMS, false]],
  });
  Assert.ok(!option.checked, "Option box should not be checked.");
  await SpecialPowers.popPrefEnv();

  // Evaluate pref when checkbox is un-checked.
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#" + CHECKBOX_ID,
    {},
    gBrowser.selectedBrowser
  );
  Assert.equal(
    Services.prefs.getBoolPref(PREF_SEARCHTERMS),
    false,
    "Preference should be false if un-checked."
  );

  // Evaluate pref when checkbox is checked.
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#" + CHECKBOX_ID,
    {},
    gBrowser.selectedBrowser
  );
  Assert.equal(
    Services.prefs.getBoolPref(PREF_SEARCHTERMS),
    true,
    "Preference should be true if checked."
  );

  // Clean-up.
  Services.prefs.clearUserPref(PREF_SEARCHTERMS);
  gBrowser.removeCurrentTab();
  await SpecialPowers.popPrefEnv();
});

/*
  When loading the search preferences panel, the
  showSearchTerms checkbox should be disabled if
  the search bar is enabled.
*/
add_task(async function showSearchTerms_and_searchBar_preference_load() {
  // Enable the feature.
  await SpecialPowers.pushPrefEnv({
    set: [
      [PREF_FEATUREGATE, true],
      ["browser.search.widget.inNavBar", true],
    ],
  });

  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;

  let checkbox = doc.getElementById(CHECKBOX_ID);
  Assert.ok(
    checkbox.disabled,
    "showSearchTerms checkbox should be disabled when search bar is enabled."
  );

  // Clean-up.
  gBrowser.removeCurrentTab();
  await SpecialPowers.popPrefEnv();
});

/*
  If the search bar is enabled while the search
  preferences panel is open, the showSearchTerms
  checkbox should not be clickable.
*/
add_task(async function showSearchTerms_and_searchBar_preference_change() {
  // Enable the feature.
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_FEATUREGATE, true]],
  });

  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;

  let checkbox = doc.getElementById(CHECKBOX_ID);
  Assert.ok(!checkbox.disabled, "showSearchTerms checkbox should be enabled.");

  await SpecialPowers.pushPrefEnv({
    set: [["browser.search.widget.inNavBar", true]],
  });
  Assert.ok(
    checkbox.disabled,
    "showSearchTerms checkbox should be disabled when search bar is enabled."
  );

  // Clean-up.
  await SpecialPowers.popPrefEnv();
  Assert.ok(!checkbox.disabled, "showSearchTerms checkbox should be enabled.");

  gBrowser.removeCurrentTab();
  await SpecialPowers.popPrefEnv();
});