summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_oneOffs_searchSuggestions.js
blob: ef324b08cd30d6bb3b294a81e922f150776c721a (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests various actions relating to search suggestions and the one-off buttons.
 */

const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";
const TEST_ENGINE2_BASENAME = "searchSuggestionEngine2.xml";

const serverInfo = {
  scheme: "http",
  host: "localhost",
  port: 20709, // Must be identical to what is in searchSuggestionEngine2.xml
};

var gEngine;
var gEngine2;

add_setup(async function () {
  await PlacesUtils.history.clear();
  await UrlbarTestUtils.formHistory.clear();
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.suggest.searches", true],
      ["browser.urlbar.maxHistoricalSearchSuggestions", 2],
    ],
  });
  gEngine = await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + TEST_ENGINE_BASENAME,
  });
  gEngine2 = await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + TEST_ENGINE2_BASENAME,
  });
  let oldDefaultEngine = await Services.search.getDefault();
  await Services.search.moveEngine(gEngine2, 0);
  await Services.search.moveEngine(gEngine, 0);
  await Services.search.setDefault(
    gEngine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  registerCleanupFunction(async function () {
    await Services.search.setDefault(
      oldDefaultEngine,
      Ci.nsISearchService.CHANGE_REASON_UNKNOWN
    );

    await PlacesUtils.history.clear();
    await UrlbarTestUtils.formHistory.clear();
  });
});

async function withSuggestions(testFn) {
  // First run with remote suggestions, and then run with form history.
  await withSuggestionOnce(false, testFn);
  await withSuggestionOnce(true, testFn);
}

async function withSuggestionOnce(useFormHistory, testFn) {
  if (useFormHistory) {
    // Add foofoo twice so it's more frecent so it appears first so that the
    // order of form history results matches the order of remote suggestion
    // results.
    await UrlbarTestUtils.formHistory.add(["foofoo", "foofoo", "foobar"]);
  }
  await BrowserTestUtils.withNewTab(gBrowser, async () => {
    let value = "foo";
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value,
      fireInputEvent: true,
    });
    let index = await UrlbarTestUtils.promiseSuggestionsPresent(window);
    await assertState({
      inputValue: value,
      resultIndex: 0,
    });
    await withHttpServer(serverInfo, () => {
      return testFn(index, useFormHistory);
    });
  });
  await PlacesUtils.history.clear();
  await UrlbarTestUtils.formHistory.clear();
}

async function selectSecondSuggestion(index, isFormHistory) {
  // Down to select the first search suggestion.
  for (let i = index; i > 0; --i) {
    EventUtils.synthesizeKey("KEY_ArrowDown");
  }
  await assertState({
    inputValue: "foofoo",
    resultIndex: index,
    suggestion: {
      isFormHistory,
    },
  });

  // Down to select the next search suggestion.
  EventUtils.synthesizeKey("KEY_ArrowDown");
  await assertState({
    inputValue: "foobar",
    resultIndex: index + 1,
    suggestion: {
      isFormHistory,
    },
  });
}

// Presses the Return key when a one-off is selected after selecting a search
// suggestion.
add_task(async function test_returnAfterSuggestion() {
  await withSuggestions(async (index, usingFormHistory) => {
    await selectSecondSuggestion(index, usingFormHistory);

    // Alt+Down to select the first one-off.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    await assertState({
      inputValue: "foobar",
      resultIndex: index + 1,
      oneOffIndex: 0,
      suggestion: {
        isFormHistory: usingFormHistory,
      },
    });

    let heuristicResult = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    Assert.ok(
      !BrowserTestUtils.is_visible(heuristicResult.element.action),
      "The heuristic action should not be visible"
    );

    let resultsPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeKey("KEY_Enter");
    await resultsPromise;
    await UrlbarTestUtils.assertSearchMode(window, {
      engineName: gEngine.name,
      entry: "oneoff",
    });
    await UrlbarTestUtils.exitSearchMode(window, { backspace: true });
  });
});

// Presses the Return key when a non-default one-off is selected after selecting
// a search suggestion.
add_task(async function test_returnAfterSuggestion_nonDefault() {
  await withSuggestions(async (index, usingFormHistory) => {
    await selectSecondSuggestion(index, usingFormHistory);

    // Alt+Down twice to select the second one-off.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    await assertState({
      inputValue: "foobar",
      resultIndex: index + 1,
      oneOffIndex: 1,
      suggestion: {
        isFormHistory: usingFormHistory,
      },
    });

    let resultsPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeKey("KEY_Enter");
    await resultsPromise;
    await UrlbarTestUtils.assertSearchMode(window, {
      engineName: gEngine2.name,
      entry: "oneoff",
    });
    await UrlbarTestUtils.exitSearchMode(window, { backspace: true });
  });
});

// Clicks a one-off engine after selecting a search suggestion.
add_task(async function test_clickAfterSuggestion() {
  await withSuggestions(async (index, usingFormHistory) => {
    await selectSecondSuggestion(index, usingFormHistory);

    let oneOffs =
      UrlbarTestUtils.getOneOffSearchButtons(window).getSelectableButtons(true);
    let resultsPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeMouseAtCenter(oneOffs[1], {});
    await resultsPromise;
    await UrlbarTestUtils.assertSearchMode(window, {
      engineName: gEngine2.name,
      entry: "oneoff",
    });
    await UrlbarTestUtils.exitSearchMode(window, { backspace: true });
  });
});

// Clicks a non-default one-off engine after selecting a search suggestion.
add_task(async function test_clickAfterSuggestion_nonDefault() {
  await withSuggestions(async (index, usingFormHistory) => {
    await selectSecondSuggestion(index, usingFormHistory);

    let oneOffs =
      UrlbarTestUtils.getOneOffSearchButtons(window).getSelectableButtons(true);
    let resultsPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeMouseAtCenter(oneOffs[1], {});
    await resultsPromise;
    await UrlbarTestUtils.assertSearchMode(window, {
      engineName: gEngine2.name,
      entry: "oneoff",
    });
    await UrlbarTestUtils.exitSearchMode(window, { backspace: true });
  });
});

// Selects a non-default one-off engine and then clicks a search suggestion.
add_task(async function test_selectOneOffThenSuggestion() {
  await withSuggestions(async (index, usingFormHistory) => {
    // Select a non-default one-off engine.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    await assertState({
      inputValue: "foo",
      resultIndex: 0,
      oneOffIndex: 1,
    });

    let heuristicResult = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    Assert.ok(
      BrowserTestUtils.is_visible(heuristicResult.element.action),
      "The heuristic action should be visible because the result is selected"
    );

    // Now click the second suggestion.
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, index + 1);
    // Note search history results don't change their engine when the selected
    // one-off button changes!
    let resultsPromise = BrowserTestUtils.browserLoaded(
      gBrowser.selectedBrowser,
      false,
      usingFormHistory
        ? `http://mochi.test:8888/?terms=foobar`
        : `http://localhost:20709/?terms=foobar`
    );
    EventUtils.synthesizeMouseAtCenter(result.element.row, {});
    await resultsPromise;
  });
});

add_task(async function overridden_engine_not_reused() {
  info(
    "An overridden search suggestion item should not be reused by a search with another engine"
  );
  await BrowserTestUtils.withNewTab(gBrowser, async () => {
    let typedValue = "foo";
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: typedValue,
      fireInputEvent: true,
    });
    let index = await UrlbarTestUtils.promiseSuggestionsPresent(window);
    // Down to select the first search suggestion.
    for (let i = index; i > 0; --i) {
      EventUtils.synthesizeKey("KEY_ArrowDown");
    }
    await assertState({
      inputValue: "foofoo",
      resultIndex: index,
      suggestion: {
        isFormHistory: false,
      },
    });

    // ALT+Down to select the second search engine.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    await assertState({
      inputValue: "foofoo",
      resultIndex: index,
      oneOffIndex: 1,
      suggestion: {
        isFormHistory: false,
      },
    });

    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
    let label = result.displayed.action;
    // Run again the query, check the label has been replaced.
    await UrlbarTestUtils.promisePopupClose(window);
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: typedValue,
      fireInputEvent: true,
    });
    index = await UrlbarTestUtils.promiseSuggestionsPresent(window);
    await assertState({
      inputValue: "foo",
      resultIndex: 0,
    });
    result = await UrlbarTestUtils.getDetailsOfResultAt(window, index);
    Assert.notEqual(
      result.displayed.action,
      label,
      "The label should have been updated"
    );
  });
});

async function assertState({
  resultIndex,
  inputValue,
  oneOffIndex = -1,
  suggestion = null,
}) {
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    resultIndex,
    "Expected result should be selected"
  );
  Assert.equal(
    UrlbarTestUtils.getOneOffSearchButtons(window).selectedButtonIndex,
    oneOffIndex,
    "Expected one-off should be selected"
  );
  if (inputValue !== undefined) {
    Assert.equal(gURLBar.value, inputValue, "Expected input value");
  }

  if (suggestion) {
    let result = await UrlbarTestUtils.getDetailsOfResultAt(
      window,
      resultIndex
    );
    Assert.equal(
      result.type,
      UrlbarUtils.RESULT_TYPE.SEARCH,
      "Result type should be SEARCH"
    );
    if (suggestion.isFormHistory) {
      Assert.equal(
        result.source,
        UrlbarUtils.RESULT_SOURCE.HISTORY,
        "Result source should be HISTORY"
      );
    } else {
      Assert.equal(
        result.source,
        UrlbarUtils.RESULT_SOURCE.SEARCH,
        "Result source should be SEARCH"
      );
    }
    Assert.equal(
      typeof result.searchParams.suggestion,
      "string",
      "Result should have a suggestion"
    );
    Assert.equal(
      result.searchParams.suggestion,
      suggestion.value || inputValue,
      "Result should have the expected suggestion"
    );
  }
}