summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_autocomplete_readline_navigation.js
blob: 36f990503e4d5c6576b5ca1e9ca35abb9faa8592 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests navigation between results using ctrl-n/p.
 */

function repeat(limit, func) {
  for (let i = 0; i < limit; i++) {
    func(i);
  }
}

function assertSelected(index) {
  Assert.equal(
    UrlbarTestUtils.getSelectedRowIndex(window),
    index,
    "Should have the correct item selected"
  );

  // This is true because although both the listbox and the one-offs can have
  // selections, the test doesn't check that.
  Assert.equal(
    UrlbarTestUtils.getOneOffSearchButtons(window).selectedButton,
    null,
    "A result is selected, so the one-offs should not have a selection"
  );
}

add_task(async function () {
  let maxResults = Services.prefs.getIntPref("browser.urlbar.maxRichResults");
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:mozilla"
  );
  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
    BrowserTestUtils.removeTab(tab);
  });

  let visits = [];
  repeat(maxResults, i => {
    visits.push({
      uri: makeURI("http://example.com/autocomplete/?" + i),
    });
  });
  await PlacesTestUtils.addVisits(visits);

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "example.com/autocomplete",
  });
  await UrlbarTestUtils.waitForAutocompleteResultAt(window, maxResults - 1);

  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    maxResults,
    "Should get maxResults=" + maxResults + " results"
  );
  assertSelected(0);

  info("Ctrl-n to select the next item");
  EventUtils.synthesizeKey("n", { ctrlKey: true });
  assertSelected(1);

  info("Ctrl-p to select the previous item");
  EventUtils.synthesizeKey("p", { ctrlKey: true });
  assertSelected(0);
});