summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_searchRestoreDefaults.js
blob: 80588d0cceae0288ebf897af998d48c43a791926 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const { SearchTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/SearchTestUtils.sys.mjs"
);
const { SearchUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/SearchUtils.sys.mjs"
);
add_task(async function test_restore_functionality() {
  // Ensure no engines are hidden to begin with.
  for (let engine of await Services.search.getAppProvidedEngines()) {
    if (engine.hidden) {
      engine.hidden = false;
    }
  }

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

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

  Assert.ok(
    restoreDefaultsButton.disabled,
    "Should have disabled the restore default search engines button on open"
  );

  let updatedPromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );

  let tree = doc.querySelector("#engineList");
  // Check for default search engines to be displayed in the engineList
  let defaultEngines = await Services.search.getAppProvidedEngines();
  for (let i = 0; i < defaultEngines.length; i++) {
    let cellName = tree.view.getCellText(
      i,
      tree.columns.getNamedColumn("engineName")
    );
    if (cellName == "DuckDuckGo") {
      tree.view.selection.select(i);
      break;
    }
  }
  doc.getElementById("removeEngineButton").click();
  await updatedPromise;

  let engine = await Services.search.getEngineByName("DuckDuckGo");

  Assert.ok(engine.hidden, "Should have hidden the engine");
  Assert.ok(
    !restoreDefaultsButton.disabled,
    "Should have enabled the restore default search engines button"
  );

  updatedPromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  restoreDefaultsButton.click();
  await updatedPromise;
  // Let the stack unwind so that the restore defaults button can update its
  // state.
  await new Promise(resolve => Services.tm.dispatchToMainThread(resolve));

  Assert.ok(!engine.hidden, "Should have re-enabled the disabled engine");
  Assert.ok(
    restoreDefaultsButton.disabled,
    "Should have disabled the restore default search engines button after use"
  );

  gBrowser.removeCurrentTab();
});

add_task(async function test_restoreEnabledOnOpenWithEngineHidden() {
  let engine = await Services.search.getEngineByName("DuckDuckGo");
  engine.hidden = true;

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

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

  Assert.ok(
    !restoreDefaultsButton.disabled,
    "Should have enabled the restore default search engines button on open"
  );

  let updatedPromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  restoreDefaultsButton.click();
  await updatedPromise;
  // Let the stack unwind so that the restore defaults button can update its
  // state.
  await new Promise(resolve => Services.tm.dispatchToMainThread(resolve));

  Assert.ok(!engine.hidden, "Should have re-enabled the disabled engine");
  Assert.ok(
    restoreDefaultsButton.disabled,
    "Should have disabled the restore default search engines button after use"
  );

  gBrowser.removeCurrentTab();
});

// This removes the last two engines and then the remaining engines from top to
// bottom, and then it restores the default engines.  See bug 1681818.
add_task(async function test_removeOutOfOrder() {
  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });

  let doc = gBrowser.selectedBrowser.contentDocument;
  let restoreDefaultsButton = doc.getElementById("restoreDefaultSearchEngines");
  Assert.ok(
    restoreDefaultsButton.disabled,
    "The restore-defaults button is disabled initially"
  );

  let tree = doc.querySelector("#engineList");
  let removeEngineButton = doc.getElementById("removeEngineButton");
  removeEngineButton.scrollIntoView();

  let defaultEngines = await Services.search.getAppProvidedEngines();

  // Remove the last two engines.  After each removal, the selection should move
  // to the first local shortcut.
  for (let i = 0; i < 2; i++) {
    tree.view.selection.select(defaultEngines.length - i - 1);
    let updatedPromise = SearchTestUtils.promiseSearchNotification(
      SearchUtils.MODIFIED_TYPE.CHANGED,
      SearchUtils.TOPIC_ENGINE_MODIFIED
    );
    removeEngineButton.click();
    await updatedPromise;
    Assert.ok(
      removeEngineButton.disabled,
      "The remove-engine button is disabled because a local shortcut is selected"
    );
    Assert.ok(
      !restoreDefaultsButton.disabled,
      "The restore-defaults button is enabled after removing an engine"
    );
  }

  // Remove the remaining engines from top to bottom except for the default engine
  // which can't be removed.
  for (let i = 0; i < defaultEngines.length - 3; i++) {
    tree.view.selection.select(0);

    if (defaultEngines[0].name == Services.search.defaultEngine.name) {
      tree.view.selection.select(1);
    }

    let updatedPromise = SearchTestUtils.promiseSearchNotification(
      SearchUtils.MODIFIED_TYPE.CHANGED,
      SearchUtils.TOPIC_ENGINE_MODIFIED
    );
    removeEngineButton.click();
    await updatedPromise;
    Assert.ok(
      !restoreDefaultsButton.disabled,
      "The restore-defaults button is enabled after removing an engine"
    );
  }
  Assert.ok(
    removeEngineButton.disabled,
    "The remove-engine button is disabled because only one engine remains"
  );

  // Click the restore-defaults button.
  let updatedPromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  restoreDefaultsButton.click();
  await updatedPromise;

  // Wait for the restore-defaults button to update its state.
  await TestUtils.waitForCondition(
    () => restoreDefaultsButton.disabled,
    "Waiting for the restore-defaults button to become disabled"
  );

  Assert.ok(
    restoreDefaultsButton.disabled,
    "The restore-defaults button is disabled after restoring defaults"
  );
  Assert.equal(
    tree.view.rowCount,
    defaultEngines.length + UrlbarUtils.LOCAL_SEARCH_MODES.length,
    "All engines are restored"
  );

  gBrowser.removeCurrentTab();
});

add_task(async function test_removeAndRestoreMultiple() {
  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });

  let doc = gBrowser.selectedBrowser.contentDocument;
  let restoreDefaultsButton = doc.getElementById("restoreDefaultSearchEngines");
  let tree = doc.querySelector("#engineList");
  let removeEngineButton = doc.getElementById("removeEngineButton");
  removeEngineButton.scrollIntoView();

  let defaultEngines = await Services.search.getAppProvidedEngines();

  // Remove the second and fourth engines.
  for (let i = 0; i < 2; i++) {
    tree.view.selection.select(i * 2 + 1);
    let updatedPromise = SearchTestUtils.promiseSearchNotification(
      SearchUtils.MODIFIED_TYPE.CHANGED,
      SearchUtils.TOPIC_ENGINE_MODIFIED
    );
    removeEngineButton.click();
    await updatedPromise;
  }

  // Click the restore-defaults button.
  let updatedPromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  restoreDefaultsButton.click();
  await updatedPromise;

  // Remove the third engine.
  tree.view.selection.select(3);
  updatedPromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  removeEngineButton.click();
  await updatedPromise;

  // Now restore again.
  updatedPromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  restoreDefaultsButton.click();
  await updatedPromise;

  // Wait for the restore-defaults button to update its state.
  await TestUtils.waitForCondition(
    () => restoreDefaultsButton.disabled,
    "Waiting for the restore-defaults button to become disabled"
  );

  Assert.equal(
    tree.view.rowCount,
    defaultEngines.length + UrlbarUtils.LOCAL_SEARCH_MODES.length,
    "Should have the correct amount of engines"
  );

  gBrowser.removeCurrentTab();
});