summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_searchSuggestions.js
blob: 8a226a3c4c7001be21bf2215fdea78b7260c17eb (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This tests checks that search suggestions can be acted upon correctly
 * e.g. selection with modifiers, copying text.
 */

"use strict";

const SUGGEST_URLBAR_PREF = "browser.urlbar.suggest.searches";
const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";

const MAX_CHARS_PREF = "browser.urlbar.maxCharsForSearchSuggestions";

// Must run first.
add_task(async function prepare() {
  let suggestionsEnabled = Services.prefs.getBoolPref(SUGGEST_URLBAR_PREF);
  Services.prefs.setBoolPref(SUGGEST_URLBAR_PREF, true);
  await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + TEST_ENGINE_BASENAME,
    setAsDefault: true,
  });
  await UrlbarTestUtils.formHistory.clear();
  registerCleanupFunction(async function () {
    Services.prefs.setBoolPref(SUGGEST_URLBAR_PREF, suggestionsEnabled);

    // Clicking suggestions causes visits to search results pages, so clear that
    // history now.
    await PlacesUtils.history.clear();
    await UrlbarTestUtils.formHistory.clear();
  });
});

add_task(async function clickSuggestion() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  gURLBar.focus();
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "foo",
  });
  let [idx, suggestion, engineName] = await getFirstSuggestion();
  Assert.equal(
    engineName,
    "browser_searchSuggestionEngine searchSuggestionEngine.xml",
    "Expected suggestion engine"
  );

  let uri = (await Services.search.getDefault()).getSubmission(suggestion).uri;
  let loadPromise = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    uri.spec
  );
  let element = await UrlbarTestUtils.waitForAutocompleteResultAt(window, idx);
  EventUtils.synthesizeMouseAtCenter(element, {}, window);
  await loadPromise;

  let formHistory = (
    await UrlbarTestUtils.formHistory.search({ source: engineName })
  ).map(entry => entry.value);
  Assert.deepEqual(
    formHistory,
    ["foofoo"],
    "Should find form history after adding it"
  );

  BrowserTestUtils.removeTab(tab);
  await UrlbarTestUtils.formHistory.clear();
});

async function testPressEnterOnSuggestion(
  expectedUrl = null,
  keyModifiers = {}
) {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  gURLBar.focus();
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "foo",
  });
  let [idx, suggestion, engineName] = await getFirstSuggestion();
  Assert.equal(
    engineName,
    "browser_searchSuggestionEngine searchSuggestionEngine.xml",
    "Expected suggestion engine"
  );

  let hasExpectedUrl = !!expectedUrl;
  if (!expectedUrl) {
    expectedUrl = (await Services.search.getDefault()).getSubmission(suggestion)
      .uri.spec;
  }

  let promiseLoad = BrowserTestUtils.waitForDocLoadAndStopIt(
    expectedUrl,
    gBrowser.selectedBrowser
  );

  let promiseFormHistory;
  if (!hasExpectedUrl) {
    promiseFormHistory = UrlbarTestUtils.formHistory.promiseChanged("add");
  }

  for (let i = 0; i < idx; ++i) {
    EventUtils.synthesizeKey("KEY_ArrowDown");
  }
  EventUtils.synthesizeKey("KEY_Enter", keyModifiers);

  await promiseLoad;

  if (!hasExpectedUrl) {
    await promiseFormHistory;
    let formHistory = (
      await UrlbarTestUtils.formHistory.search({ source: engineName })
    ).map(entry => entry.value);
    Assert.deepEqual(
      formHistory,
      ["foofoo"],
      "Should find form history after adding it"
    );
  }

  BrowserTestUtils.removeTab(tab);
  await UrlbarTestUtils.formHistory.clear();
}

add_task(async function plainEnterOnSuggestion() {
  await testPressEnterOnSuggestion();
});

add_task(async function ctrlEnterOnSuggestion() {
  await testPressEnterOnSuggestion("https://www.foofoo.com/", {
    ctrlKey: true,
  });
});

add_task(async function copySuggestionText() {
  gURLBar.focus();
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "foo",
  });
  let [idx, suggestion] = await getFirstSuggestion();
  for (let i = 0; i < idx; ++i) {
    EventUtils.synthesizeKey("KEY_ArrowDown");
  }
  gURLBar.select();
  await SimpleTest.promiseClipboardChange(suggestion, () => {
    goDoCommand("cmd_copy");
  });
});

add_task(async function typeMaxChars() {
  gURLBar.focus();

  let maxChars = 10;
  await SpecialPowers.pushPrefEnv({
    set: [[MAX_CHARS_PREF, maxChars]],
  });

  // Make a string as long as maxChars and type it.
  let value = "";
  for (let i = 0; i < maxChars; i++) {
    value += String.fromCharCode("a".charCodeAt(0) + i);
  }
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value,
  });

  // Suggestions should be fetched since we allow them when typing, and the
  // value so far isn't longer than maxChars anyway.
  await assertSuggestions([value + "foo", value + "bar"]);

  // Now type some additional chars.  Suggestions should still be fetched since
  // we allow them when typing.
  for (let i = 0; i < 3; i++) {
    let char = String.fromCharCode("z".charCodeAt(0) - i);
    value += char;
    EventUtils.synthesizeKey(char);
    await assertSuggestions([value + "foo", value + "bar"]);
  }

  await SpecialPowers.popPrefEnv();
});

add_task(async function pasteMaxChars() {
  gURLBar.focus();

  let maxChars = 10;
  await SpecialPowers.pushPrefEnv({
    set: [[MAX_CHARS_PREF, maxChars]],
  });

  // Make a string as long as maxChars and paste it.
  let value = "";
  for (let i = 0; i < maxChars; i++) {
    value += String.fromCharCode("a".charCodeAt(0) + i);
  }
  await selectAndPaste(value);

  // Suggestions should be fetched since the pasted string is not longer than
  // maxChars.
  await assertSuggestions([value + "foo", value + "bar"]);

  // Now type some additional chars.  Suggestions should still be fetched since
  // we allow them when typing.
  for (let i = 0; i < 3; i++) {
    let char = String.fromCharCode("z".charCodeAt(0) - i);
    value += char;
    EventUtils.synthesizeKey(char);
    await assertSuggestions([value + "foo", value + "bar"]);
  }

  await SpecialPowers.popPrefEnv();
});

add_task(async function pasteMoreThanMaxChars() {
  gURLBar.focus();

  let maxChars = 10;
  await SpecialPowers.pushPrefEnv({
    set: [[MAX_CHARS_PREF, maxChars]],
  });

  // Make a string longer than maxChars and paste it.
  let value = "";
  for (let i = 0; i < 2 * maxChars; i++) {
    value += String.fromCharCode("a".charCodeAt(0) + i);
  }
  await selectAndPaste(value);

  // Suggestions should not be fetched since the value was pasted and it was
  // longer than maxChars.
  await assertSuggestions([]);

  // Now type some additional chars.  Suggestions should now be fetched since we
  // allow them when typing.
  for (let i = 0; i < 3; i++) {
    let char = String.fromCharCode("z".charCodeAt(0) - i);
    value += char;
    EventUtils.synthesizeKey(char);
    await assertSuggestions([value + "foo", value + "bar"]);
  }

  // Paste again.  The string is longer than maxChars, so suggestions should not
  // be fetched.
  await selectAndPaste(value);
  await assertSuggestions([]);

  await SpecialPowers.popPrefEnv();
});

add_task(async function heuristicAddsFormHistory() {
  await UrlbarTestUtils.formHistory.clear();
  let formHistory = (await UrlbarTestUtils.formHistory.search()).map(
    entry => entry.value
  );
  Assert.deepEqual(formHistory, [], "Form history should be empty initially");

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);

  gURLBar.focus();
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "foo",
  });

  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(result.heuristic);
  Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.SEARCH);
  Assert.equal(result.searchParams.query, "foo");

  let uri = (await Services.search.getDefault()).getSubmission("foo").uri;
  let loadPromise = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    uri.spec
  );
  let formHistoryPromise = UrlbarTestUtils.formHistory.promiseChanged("add");
  let element = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);
  EventUtils.synthesizeMouseAtCenter(element, {}, window);
  await loadPromise;

  await formHistoryPromise;
  formHistory = (
    await UrlbarTestUtils.formHistory.search({
      source: result.searchParams.engine,
    })
  ).map(entry => entry.value);
  Assert.deepEqual(
    formHistory,
    ["foo"],
    "Should find form history after adding it"
  );

  BrowserTestUtils.removeTab(tab);
  await UrlbarTestUtils.formHistory.clear();
});

async function getFirstSuggestion() {
  let results = await getSuggestionResults();
  if (!results.length) {
    return [-1, null, null];
  }
  let result = results[0];
  return [
    result.index,
    result.searchParams.suggestion,
    result.searchParams.engine,
  ];
}

async function getSuggestionResults() {
  await UrlbarTestUtils.promiseSearchComplete(window);

  let results = [];
  let matchCount = UrlbarTestUtils.getResultCount(window);
  for (let i = 0; i < matchCount; i++) {
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    if (
      result.type == UrlbarUtils.RESULT_TYPE.SEARCH &&
      result.searchParams.suggestion
    ) {
      result.index = i;
      results.push(result);
    }
  }
  return results;
}

async function assertSuggestions(expectedSuggestions) {
  let results = await getSuggestionResults();
  let actualSuggestions = results.map(r => r.searchParams.suggestion);
  Assert.deepEqual(
    actualSuggestions,
    expectedSuggestions,
    "Expected suggestions"
  );
}