summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_searchMode_localOneOffs_actionText.js
blob: 74a2a3caba2f65877c1bf53c479e36d6a7435725 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests action text shown on heuristic and search suggestions when keyboard
 * navigating local one-off buttons.
 */

"use strict";

const DEFAULT_ENGINE_NAME = "Test";
const SUGGESTIONS_ENGINE_NAME = "searchSuggestionEngine.xml";

let engine;

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.suggest.searches", true],
      ["browser.urlbar.suggest.quickactions", false],
      ["browser.urlbar.shortcuts.quickactions", false],
    ],
  });
  engine = await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + SUGGESTIONS_ENGINE_NAME,
    setAsDefault: true,
  });
  await Services.search.moveEngine(engine, 0);

  await PlacesUtils.history.clear();

  registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
  });
});

add_task(async function localOneOff() {
  info("Type some text, select a local one-off, check heuristic action");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "query",
  });
  Assert.ok(UrlbarTestUtils.getResultCount(window) > 1, "Sanity check results");

  info("Alt UP to select the last local one-off.");
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    0,
    "the heuristic result should be selected"
  );
  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  let oneOffButtons = UrlbarTestUtils.getOneOffSearchButtons(window);
  Assert.equal(
    oneOffButtons.selectedButton.source,
    UrlbarUtils.RESULT_SOURCE.HISTORY,
    "A local one-off button should be selected"
  );
  Assert.ok(
    BrowserTestUtils.is_visible(result.element.action),
    "The heuristic action should be visible"
  );
  let [actionHistory, actionBookmarks] = await document.l10n.formatValues([
    { id: "urlbar-result-action-search-history" },
    { id: "urlbar-result-action-search-bookmarks" },
  ]);
  Assert.equal(
    result.displayed.action,
    actionHistory,
    "Check the heuristic action"
  );
  Assert.equal(
    result.image,
    "chrome://browser/skin/history.svg",
    "Check the heuristic icon"
  );

  info("Move to an engine one-off and check heuristic action");
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  Assert.ok(
    oneOffButtons.selectedButton.engine,
    "A one-off search button should be selected"
  );
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(
    BrowserTestUtils.is_visible(result.element.action),
    "The heuristic action should be visible"
  );
  Assert.ok(
    result.displayed.action.includes(oneOffButtons.selectedButton.engine.name),
    "Check the heuristic action"
  );
  Assert.equal(
    result.image,
    oneOffButtons.selectedButton.engine.iconURI.spec,
    "Check the heuristic icon"
  );

  info("Move again to a local one-off, deselect and reselect the heuristic");
  EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(
    oneOffButtons.selectedButton.source,
    UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
    "A local one-off button should be selected"
  );
  Assert.equal(
    result.displayed.action,
    actionBookmarks,
    "Check the heuristic action"
  );
  Assert.equal(
    result.image,
    "chrome://browser/skin/bookmark.svg",
    "Check the heuristic icon"
  );

  info(
    "Select the next result, then reselect the heuristic, check it's a search with the default engine"
  );
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    0,
    "the heuristic result should be selected"
  );
  EventUtils.synthesizeKey("KEY_ArrowDown", {});
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    1,
    "the heuristic result should not be selected"
  );
  EventUtils.synthesizeKey("KEY_ArrowUp", {});
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    0,
    "the heuristic result should be selected"
  );
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.SEARCH);
  Assert.equal(result.searchParams.engine, engine.name);
  Assert.ok(
    result.displayed.action.includes(engine.name),
    "Check the heuristic action"
  );
  Assert.equal(
    result.image,
    "chrome://global/skin/icons/search-glass.svg",
    "Check the heuristic icon"
  );
});

add_task(async function localOneOff_withVisit() {
  info("Type a url, select a local one-off, check heuristic action");
  await PlacesUtils.history.clear();
  for (let i = 0; i < 5; i++) {
    await PlacesTestUtils.addVisits("https://mozilla.org/");
    await PlacesTestUtils.addVisits("https://other.mozilla.org/");
  }
  const searchString = "mozilla.org";
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: searchString,
  });
  Assert.ok(UrlbarTestUtils.getResultCount(window) > 1, "Sanity check results");
  let oneOffButtons = UrlbarTestUtils.getOneOffSearchButtons(window);

  let [actionHistory, actionTabs, actionBookmarks] =
    await document.l10n.formatValues([
      { id: "urlbar-result-action-search-history" },
      { id: "urlbar-result-action-search-tabs" },
      { id: "urlbar-result-action-search-bookmarks" },
    ]);

  info("Alt UP to select the history one-off.");
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    0,
    "the heuristic result should be selected"
  );
  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(
    oneOffButtons.selectedButton.source,
    UrlbarUtils.RESULT_SOURCE.HISTORY,
    "The history one-off button should be selected"
  );
  Assert.ok(
    BrowserTestUtils.is_visible(result.element.action),
    "The heuristic action should be visible"
  );
  Assert.equal(
    result.displayed.action,
    actionHistory,
    "Check the heuristic action"
  );
  Assert.equal(
    result.image,
    "chrome://browser/skin/history.svg",
    "Check the heuristic icon"
  );
  let row = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);
  Assert.equal(
    row.querySelector(".urlbarView-title").textContent,
    searchString,
    "Check that the result title has been replaced with the search string."
  );

  info("Alt UP to select the tabs one-off.");
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(
    oneOffButtons.selectedButton.source,
    UrlbarUtils.RESULT_SOURCE.TABS,
    "The tabs one-off button should be selected"
  );
  Assert.ok(
    BrowserTestUtils.is_visible(result.element.action),
    "The heuristic action should be visible"
  );
  Assert.equal(
    result.displayed.action,
    actionTabs,
    "Check the heuristic action"
  );
  Assert.equal(
    result.image,
    "chrome://browser/skin/tab.svg",
    "Check the heuristic icon"
  );

  info("Alt UP to select the bookmarks one-off.");
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(
    oneOffButtons.selectedButton.source,
    UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
    "The bookmarks one-off button should be selected"
  );
  Assert.ok(
    BrowserTestUtils.is_visible(result.element.action),
    "The heuristic action should be visible"
  );
  Assert.equal(
    result.displayed.action,
    actionBookmarks,
    "Check the heuristic action"
  );
  Assert.equal(
    result.image,
    "chrome://browser/skin/bookmark.svg",
    "Check the heuristic icon"
  );

  info(
    "Select the next result, then reselect the heuristic, check it's a visit"
  );
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    0,
    "the heuristic result should be selected"
  );
  EventUtils.synthesizeKey("KEY_ArrowDown", {});
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    1,
    "the heuristic result should not be selected"
  );
  EventUtils.synthesizeKey("KEY_ArrowUp", {});
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    0,
    "the heuristic result should be selected"
  );
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(
    oneOffButtons.selectedButton,
    null,
    "No one-off button should be selected"
  );
  Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.URL);
  Assert.equal(
    result.displayed.url,
    result.result.payload.displayUrl,
    "Check the heuristic action"
  );
  Assert.notEqual(
    result.image,
    "chrome://browser/skin/history.svg",
    "Check the heuristic icon"
  );
  row = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);
  Assert.equal(
    row.querySelector(".urlbarView-title").textContent,
    result.result.payload.title || `https://${searchString}`,
    "Check that the result title has been restored to the fixed-up URI."
  );

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

add_task(async function localOneOff_suggestion() {
  info("Type some text, select the first suggestion, then a local one-off");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "query",
  });
  let count = UrlbarTestUtils.getResultCount(window);
  Assert.ok(count > 1, "Sanity check results");
  let result = null;
  let suggestionIndex = -1;
  for (let i = 1; i < count; ++i) {
    EventUtils.synthesizeKey("KEY_ArrowDown");
    let index = await UrlbarTestUtils.getSelectedRowIndex(window);
    result = await UrlbarTestUtils.getDetailsOfResultAt(window, index);
    if (
      result.type == UrlbarUtils.RESULT_TYPE.SEARCH &&
      result.searchParams.suggestion
    ) {
      suggestionIndex = i;
      break;
    }
  }
  Assert.ok(
    result.searchParams.suggestion,
    "Should have selected a search suggestion"
  );
  Assert.ok(
    result.displayed.action.includes(result.searchParams.engine),
    "Check the search suggestion action"
  );

  info("Alt UP to select the last local one-off.");
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  Assert.equal(
    await UrlbarTestUtils.getSelectedRowIndex(window),
    suggestionIndex,
    "the suggestion should still be selected"
  );

  let [actionHistory] = await document.l10n.formatValues([
    { id: "urlbar-result-action-search-history" },
  ]);
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, suggestionIndex);
  Assert.equal(
    result.displayed.action,
    actionHistory,
    "Check the search suggestion action changed to local one-off"
  );
  // Like in the normal engine one-offs case, we don't replace the favicon.
  Assert.equal(
    result.image,
    "chrome://global/skin/icons/search-glass.svg",
    "Check the suggestion icon"
  );

  info("DOWN to select the next suggestion");
  EventUtils.synthesizeKey("KEY_ArrowDown");
  result = await UrlbarTestUtils.getDetailsOfResultAt(
    window,
    suggestionIndex + 1
  );
  Assert.ok(
    result.searchParams.suggestion,
    "Should have selected a search suggestion"
  );
  Assert.ok(
    result.displayed.action.includes(result.searchParams.engine),
    "Check the search suggestion action"
  );
  Assert.equal(
    result.image,
    "chrome://global/skin/icons/search-glass.svg",
    "Check the suggestion icon"
  );

  info("UP back to the previous suggestion");
  EventUtils.synthesizeKey("KEY_ArrowUp");
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, suggestionIndex);
  Assert.ok(
    result.displayed.action.includes(result.searchParams.engine),
    "Check the search suggestion action"
  );
  Assert.equal(
    result.image,
    "chrome://global/skin/icons/search-glass.svg",
    "Check the suggestion icon"
  );
});

add_task(async function localOneOff_shortcut() {
  info("Select a search shortcut, then a local one-off");

  await PlacesUtils.history.clear();
  // Enough vists to get this site into Top Sites.
  for (let i = 0; i < 5; i++) {
    await PlacesTestUtils.addVisits("http://example.com/");
  }

  await updateTopSites(
    sites => sites && sites[0] && sites[0].searchTopSite,
    true
  );

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "",
  });
  let count = UrlbarTestUtils.getResultCount(window);
  Assert.ok(count > 1, "Sanity check results");
  let result = null;
  let shortcutIndex = -1;
  for (let i = 0; i < count; ++i) {
    EventUtils.synthesizeKey("KEY_ArrowDown");
    let index = await UrlbarTestUtils.getSelectedRowIndex(window);
    result = await UrlbarTestUtils.getDetailsOfResultAt(window, index);
    if (
      result.type == UrlbarUtils.RESULT_TYPE.SEARCH &&
      result.searchParams.keyword
    ) {
      shortcutIndex = i;
      break;
    }
  }
  Assert.ok(result.searchParams.keyword, "Should have selected a shortcut");
  let shortcutEngine = result.searchParams.engine;

  info("Alt UP to select the last local one-off.");
  EventUtils.synthesizeKey("KEY_ArrowUp", { altKey: true });
  Assert.equal(
    await UrlbarTestUtils.getSelectedRowIndex(window),
    shortcutIndex,
    "the shortcut should still be selected"
  );
  result = await UrlbarTestUtils.getDetailsOfResultAt(window, shortcutIndex);
  Assert.equal(
    result.displayed.action,
    "",
    "Check the shortcut action is empty"
  );
  Assert.equal(
    result.searchParams.engine,
    shortcutEngine,
    "Check the shortcut engine"
  );
  Assert.ok(
    result.displayed.title.includes(shortcutEngine),
    "Check the shortcut title"
  );
  Assert.notEqual(
    result.image,
    "chrome://global/skin/icons/search-glass.svg",
    "Check the icon was not replaced"
  );

  await UrlbarTestUtils.exitSearchMode(window);
  await PlacesUtils.history.clear();
});