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

// This test makes sure that autofill works as expected when typing, character
// by character.

"use strict";

add_setup(async function () {
  await cleanUp();
});

add_task(async function origin() {
  await PlacesTestUtils.addVisits(["http://example.com/"]);
  // all lowercase
  await typeAndCheck([
    ["e", "example.com/"],
    ["x", "example.com/"],
    ["a", "example.com/"],
    ["m", "example.com/"],
    ["p", "example.com/"],
    ["l", "example.com/"],
    ["e", "example.com/"],
    [".", "example.com/"],
    ["c", "example.com/"],
    ["o", "example.com/"],
    ["m", "example.com/"],
    ["/", "example.com/"],
  ]);
  gURLBar.value = "";
  // mixed case
  await typeAndCheck([
    ["E", "Example.com/"],
    ["x", "Example.com/"],
    ["A", "ExAmple.com/"],
    ["m", "ExAmple.com/"],
    ["P", "ExAmPle.com/"],
    ["L", "ExAmPLe.com/"],
    ["e", "ExAmPLe.com/"],
    [".", "ExAmPLe.com/"],
    ["C", "ExAmPLe.Com/"],
    ["o", "ExAmPLe.Com/"],
    ["M", "ExAmPLe.CoM/"],
    ["/", "ExAmPLe.CoM/"],
  ]);
  await cleanUp();
});

add_task(async function url() {
  await PlacesTestUtils.addVisits(["http://example.com/foo/bar"]);
  // all lowercase
  await typeAndCheck([
    ["e", "example.com/"],
    ["x", "example.com/"],
    ["a", "example.com/"],
    ["m", "example.com/"],
    ["p", "example.com/"],
    ["l", "example.com/"],
    ["e", "example.com/"],
    [".", "example.com/"],
    ["c", "example.com/"],
    ["o", "example.com/"],
    ["m", "example.com/"],
    ["/", "example.com/"],
    ["f", "example.com/foo/"],
    ["o", "example.com/foo/"],
    ["o", "example.com/foo/"],
    ["/", "example.com/foo/"],
    ["b", "example.com/foo/bar"],
    ["a", "example.com/foo/bar"],
    ["r", "example.com/foo/bar"],
  ]);
  gURLBar.value = "";
  // mixed case
  await typeAndCheck([
    ["E", "Example.com/"],
    ["x", "Example.com/"],
    ["A", "ExAmple.com/"],
    ["m", "ExAmple.com/"],
    ["P", "ExAmPle.com/"],
    ["L", "ExAmPLe.com/"],
    ["e", "ExAmPLe.com/"],
    [".", "ExAmPLe.com/"],
    ["C", "ExAmPLe.Com/"],
    ["o", "ExAmPLe.Com/"],
    ["M", "ExAmPLe.CoM/"],
    ["/", "ExAmPLe.CoM/"],
    ["f", "ExAmPLe.CoM/foo/"],
    ["o", "ExAmPLe.CoM/foo/"],
    ["o", "ExAmPLe.CoM/foo/"],
    ["/", "ExAmPLe.CoM/foo/"],
    ["b", "ExAmPLe.CoM/foo/bar"],
    ["a", "ExAmPLe.CoM/foo/bar"],
    ["r", "ExAmPLe.CoM/foo/bar"],
  ]);
  await cleanUp();
});

add_task(async function tokenAlias() {
  // We have built-in engine aliases that may conflict with the one we choose
  // here in terms of autofill, so be careful and choose a weird alias.
  await SearchTestUtils.installSearchExtension({ keyword: "@__example" });
  // all lowercase
  await typeAndCheck([
    ["@", "@"],
    ["_", "@__example "],
    ["_", "@__example "],
    ["e", "@__example "],
    ["x", "@__example "],
    ["a", "@__example "],
    ["m", "@__example "],
    ["p", "@__example "],
    ["l", "@__example "],
    ["e", "@__example "],
  ]);
  gURLBar.value = "";
  // mixed case
  await typeAndCheck([
    ["@", "@"],
    ["_", "@__example "],
    ["_", "@__example "],
    ["E", "@__Example "],
    ["x", "@__Example "],
    ["A", "@__ExAmple "],
    ["m", "@__ExAmple "],
    ["P", "@__ExAmPle "],
    ["L", "@__ExAmPLe "],
    ["e", "@__ExAmPLe "],
  ]);
  await cleanUp();
});

async function typeAndCheck(values) {
  gURLBar.focus();
  for (let i = 0; i < values.length; i++) {
    let [char, expectedInputValue] = values[i];
    info(
      `Typing: i=${i} char=${char} ` +
        `substring="${expectedInputValue.substring(0, i + 1)}"`
    );
    EventUtils.synthesizeKey(char);
    if (i == 0 && char == "@") {
      // A single "@" doesn't trigger autofill, so skip the checks below.  (It
      // shows all the @ aliases.)
      continue;
    }
    await UrlbarTestUtils.promiseSearchComplete(window);
    let restIsSpaces = !expectedInputValue.substring(i + 1).trim();
    Assert.equal(gURLBar.value, expectedInputValue);
    Assert.equal(gURLBar.selectionStart, i + 1);
    Assert.equal(gURLBar.selectionEnd, expectedInputValue.length);
    if (restIsSpaces) {
      // Autofilled @ aliases have a trailing space.  We should check that the
      // space is autofilled when each preceding character is typed, but once
      // the final non-space char is typed, autofill actually stops and the
      // trailing space is not autofilled.  (Which is maybe not the way it
      // should work...)  Skip the check below.
      continue;
    }
    let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    Assert.ok(details.autofill);
  }
}

async function cleanUp() {
  gURLBar.value = "";
  await UrlbarTestUtils.promisePopupClose(window, () => {
    EventUtils.synthesizeKey("KEY_Escape");
  });
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesUtils.history.clear();
}