summaryrefslogtreecommitdiffstats
path: root/toolkit/components/autocomplete/tests/unit/test_autofillSelectedPopupIndex.js
blob: 9b38b95c9cf6821ac8750c74ac2237250c54a31b (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
"use strict";

add_task(async function sameCaseAsMatch() {
  await runTest("moz");
});

add_task(async function differentCaseFromMatch() {
  await runTest("MOZ");
});

async function runTest(searchStr) {
  let matches = ["mozilla.org", "example.com"];
  let result = new AutoCompleteResultBase(matches);
  result.defaultIndex = 0;

  let search = new AutoCompleteSearchBase("search", result);
  registerAutoCompleteSearch(search);

  let input = new AutoCompleteInputBase([search.name]);
  input.completeSelectedIndex = true;
  input.completeDefaultIndex = true;

  // Start off with the search string in the input.  The selection must be
  // collapsed and the caret must be at the end to trigger autofill below.
  input.textValue = searchStr;
  input.selectTextRange(searchStr.length, searchStr.length);
  Assert.equal(
    input.selectionStart,
    searchStr.length,
    "Selection should start at the end of the input"
  );
  Assert.equal(
    input.selectionEnd,
    searchStr.length,
    "Selection should end at the end of the input"
  );

  let controller = Cc["@mozilla.org/autocomplete/controller;1"].createInstance(
    Ci.nsIAutoCompleteController
  );
  controller.input = input;
  input.controller = controller;

  // Start a search.
  await new Promise(resolve => {
    controller.startSearch(searchStr);
    input.onSearchComplete = () => {
      // The first match should have autofilled, but the case of the search
      // string should be preserved.
      let expectedValue = searchStr + matches[0].substr(searchStr.length);
      Assert.equal(input.textValue, expectedValue, "Should have autofilled");
      Assert.equal(
        input.selectionStart,
        searchStr.length,
        "Selection should start after search string"
      );
      Assert.equal(
        input.selectionEnd,
        expectedValue.length,
        "Selection should end at the end of the input"
      );
      resolve();
    };
  });

  // Key down to select the second match in the popup.
  // Hardcode KeyboardEvent.DOM_VK_DOWN, because we can't easily
  // include KeyboardEvent here.
  controller.handleKeyNavigation(0x28 /* KeyboardEvent.DOM_VK_DOWN */);
  let expectedValue = matches[1];
  Assert.equal(
    input.textValue,
    expectedValue,
    "Should have filled second match"
  );
  Assert.equal(
    input.selectionStart,
    expectedValue.length,
    "Selection should start at the end of the input"
  );
  Assert.equal(
    input.selectionEnd,
    expectedValue.length,
    "Selection should end at the end of the input"
  );

  // Key up to select the first match again.  The input should be restored
  // exactly as it was when the first match was autofilled above: the search
  // string's case should be preserved, and the selection should be preserved.
  // Hardcode KeyboardEvent.DOM_VK_UP, because we can't easily
  // include KeyboardEvent here.
  controller.handleKeyNavigation(0x26 /* KeyboardEvent.DOM_VK_UP */);
  expectedValue = searchStr + matches[0].substr(searchStr.length);
  Assert.equal(
    input.textValue,
    expectedValue,
    "Should have filled first match again"
  );
  Assert.equal(
    input.selectionStart,
    searchStr.length,
    "Selection should start after search string again"
  );
  Assert.equal(
    input.selectionEnd,
    expectedValue.length,
    "Selection should end at the end of the input again"
  );
}