summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_searchsuggestions.js
blob: 6aca310eb2f9af7fa73a8918fd39225baaaa1f02 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const SUGGEST_PREF_NAME = "browser.search.suggest.enabled";
const URLBAR_SUGGEST_PREF_NAME = "browser.urlbar.suggest.searches";
const PRIVATE_PREF_NAME = "browser.search.suggest.enabled.private";
const SEARCHBAR_PREF_NAME = "browser.search.widget.inNavBar";

let initialUrlbarSuggestValue;
let initialSuggestionsInPrivateValue;

add_setup(async function () {
  const originalSuggest = Services.prefs.getBoolPref(SUGGEST_PREF_NAME);
  initialUrlbarSuggestValue = Services.prefs.getBoolPref(
    URLBAR_SUGGEST_PREF_NAME
  );
  initialSuggestionsInPrivateValue =
    Services.prefs.getBoolPref(PRIVATE_PREF_NAME);

  registerCleanupFunction(() => {
    Services.prefs.setBoolPref(SUGGEST_PREF_NAME, originalSuggest);
    Services.prefs.setBoolPref(
      PRIVATE_PREF_NAME,
      initialSuggestionsInPrivateValue
    );
  });
});

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);
  Services.prefs.setBoolPref(SEARCHBAR_PREF_NAME, true);

  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });

  let doc = gBrowser.selectedBrowser.contentDocument;
  let urlbarBox = doc.getElementById("urlBarSuggestion");
  let privateBox = doc.getElementById("showSearchSuggestionsPrivateWindows");
  ok(!urlbarBox.disabled, "Should have enabled the urlbar checkbox");
  ok(
    !privateBox.disabled,
    "Should have enabled the private mode suggestions checkbox"
  );
  is(
    urlbarBox.checked,
    initialUrlbarSuggestValue,
    "Should have the correct value for the urlbar checkbox"
  );
  is(
    privateBox.checked,
    initialSuggestionsInPrivateValue,
    "Should have the correct value for the private mode suggestions checkbox"
  );

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

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

  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, false);
  ok(!urlbarBox.checked, "Should have unchecked the urlbar box");
  ok(urlbarBox.disabled, "Should have disabled the urlbar box");
  Services.prefs.setBoolPref(SEARCHBAR_PREF_NAME, false);
  ok(urlbarBox.hidden, "Should have hidden the urlbar box");
  ok(!privateBox.checked, "Should have unchecked the private suggestions box");
  ok(privateBox.disabled, "Should have disabled the private suggestions box");

  gBrowser.removeCurrentTab();
});

// Open with suggestions disabled
add_task(async function test_suggestions_start_disabled() {
  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, false);

  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });

  let doc = gBrowser.selectedBrowser.contentDocument;
  let urlbarBox = doc.getElementById("urlBarSuggestion");
  ok(urlbarBox.disabled, "Should have the urlbar box disabled");
  let privateBox = doc.getElementById("showSearchSuggestionsPrivateWindows");
  ok(privateBox.disabled, "Should have the private suggestions box disabled");

  Services.prefs.setBoolPref(SUGGEST_PREF_NAME, true);

  ok(!urlbarBox.disabled, "Should have enabled the urlbar box");
  ok(!privateBox.disabled, "Should have enabled the private suggestions box");

  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);
});