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

// This test makes sure that a few of aspects of autofill are correctly
// preserved:
//
// * Autofill should preserve the user's case.  If you type ExA, it should be
//   autofilled to ExAmple.com/, not example.com/.
// * When you key down and then back up to the autofill result, autofill should
//   be restored, with the text selection and the user's case both preserved.
// * When you key down/up so that no result is selected, the value that the
//   user typed to trigger autofill should be restored in the input.

"use strict";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    // The example.com engine can interfere with this test.
    set: [["browser.urlbar.suggest.engines", false]],
  });
  await cleanUp();
});

add_task(async function origin() {
  await PlacesTestUtils.addVisits([
    "http://example.com/",
    "http://mozilla.org/example",
  ]);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "ExA",
    fireInputEvent: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "ExAmple.com/");
  Assert.equal(gURLBar.selectionStart, "ExA".length);
  Assert.equal(gURLBar.selectionEnd, "ExAmple.com/".length);
  checkKeys([
    ["KEY_ArrowDown", "http://mozilla.org/example", 1],
    ["KEY_ArrowDown", "ExA", -1],
    ["KEY_ArrowUp", "http://mozilla.org/example", 1],
    ["KEY_ArrowUp", "ExAmple.com/", 0],
    ["KEY_ArrowUp", "ExA", -1],
    ["KEY_ArrowDown", "ExAmple.com/", 0],
  ]);
  await cleanUp();
});

add_task(async function originPort() {
  await PlacesTestUtils.addVisits([
    "http://example.com:8888/",
    "http://mozilla.org/example",
  ]);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "ExA",
    fireInputEvent: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "ExAmple.com:8888/");
  Assert.equal(gURLBar.selectionStart, "ExA".length);
  Assert.equal(gURLBar.selectionEnd, "ExAmple.com:8888/".length);
  checkKeys([
    ["KEY_ArrowDown", "http://mozilla.org/example", 1],
    ["KEY_ArrowDown", "ExA", -1],
    ["KEY_ArrowUp", "http://mozilla.org/example", 1],
    ["KEY_ArrowUp", "ExAmple.com:8888/", 0],
    ["KEY_ArrowUp", "ExA", -1],
    ["KEY_ArrowDown", "ExAmple.com:8888/", 0],
  ]);
  await cleanUp();
});

add_task(async function originScheme() {
  await PlacesTestUtils.addVisits([
    "http://example.com/",
    "http://mozilla.org/example",
  ]);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "http://ExA",
    fireInputEvent: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "http://ExAmple.com/");
  Assert.equal(gURLBar.selectionStart, "http://ExA".length);
  Assert.equal(gURLBar.selectionEnd, "http://ExAmple.com/".length);
  checkKeys([
    ["KEY_ArrowDown", "http://mozilla.org/example", 1],
    ["KEY_ArrowDown", "http://ExA", -1],
    ["KEY_ArrowUp", "http://mozilla.org/example", 1],
    ["KEY_ArrowUp", "http://ExAmple.com/", 0],
    ["KEY_ArrowUp", "http://ExA", -1],
    ["KEY_ArrowDown", "http://ExAmple.com/", 0],
  ]);
  await cleanUp();
});

add_task(async function originPortScheme() {
  await PlacesTestUtils.addVisits([
    "http://example.com:8888/",
    "http://mozilla.org/example",
  ]);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "http://ExA",
    fireInputEvents: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "http://ExAmple.com:8888/");
  Assert.equal(gURLBar.selectionStart, "http://ExA".length);
  Assert.equal(gURLBar.selectionEnd, "http://ExAmple.com:8888/".length);
  checkKeys([
    ["KEY_ArrowDown", "http://mozilla.org/example", 1],
    ["KEY_ArrowDown", "http://ExA", -1],
    ["KEY_ArrowUp", "http://mozilla.org/example", 1],
    ["KEY_ArrowUp", "http://ExAmple.com:8888/", 0],
    ["KEY_ArrowUp", "http://ExA", -1],
    ["KEY_ArrowDown", "http://ExAmple.com:8888/", 0],
  ]);
  await cleanUp();
});

add_task(async function url() {
  await PlacesTestUtils.addVisits([
    "http://example.com/foo",
    "http://example.com/foo",
    "http://example.com/fff",
  ]);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "ExAmple.com/f",
    fireInputEvent: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "ExAmple.com/foo");
  Assert.equal(gURLBar.selectionStart, "ExAmple.com/f".length);
  Assert.equal(gURLBar.selectionEnd, "ExAmple.com/foo".length);
  checkKeys([
    ["KEY_ArrowDown", "http://example.com/fff", 1],
    ["KEY_ArrowDown", "ExAmple.com/f", -1],
    ["KEY_ArrowUp", "http://example.com/fff", 1],
    ["KEY_ArrowUp", "ExAmple.com/foo", 0],
    ["KEY_ArrowUp", "ExAmple.com/f", -1],
    ["KEY_ArrowDown", "ExAmple.com/foo", 0],
  ]);
  await cleanUp();
});

add_task(async function urlPort() {
  await PlacesTestUtils.addVisits([
    "http://example.com:8888/foo",
    "http://example.com:8888/foo",
    "http://example.com:8888/fff",
  ]);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "ExAmple.com:8888/f",
    fireInputEvent: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "ExAmple.com:8888/foo");
  Assert.equal(gURLBar.selectionStart, "ExAmple.com:8888/f".length);
  Assert.equal(gURLBar.selectionEnd, "ExAmple.com:8888/foo".length);
  checkKeys([
    ["KEY_ArrowDown", "http://example.com:8888/fff", 1],
    ["KEY_ArrowDown", "ExAmple.com:8888/f", -1],
    ["KEY_ArrowUp", "http://example.com:8888/fff", 1],
    ["KEY_ArrowUp", "ExAmple.com:8888/foo", 0],
    ["KEY_ArrowUp", "ExAmple.com:8888/f", -1],
    ["KEY_ArrowDown", "ExAmple.com:8888/foo", 0],
  ]);
  await cleanUp();
});

add_task(async function tokenAlias() {
  await SearchTestUtils.installSearchExtension({ keyword: "@example" });
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "@ExA",
    fireInputEvent: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "@ExAmple ");
  Assert.equal(gURLBar.selectionStart, "@ExA".length);
  Assert.equal(gURLBar.selectionEnd, "@ExAmple ".length);
  // Token aliases (1) hide the one-off buttons and (2) show only a single
  // result, the "Search with" result for the alias's engine, so there's no way
  // to key up/down to change the selection, so this task doesn't check key
  // presses like the others do.
  await cleanUp();
});

// This test is a little different from the others.  It backspaces over the
// autofilled substring and checks that autofill is *not* preserved.
add_task(async function backspaceNoAutofill() {
  await PlacesTestUtils.addVisits([
    "http://example.com/",
    "http://example.com/",
    "http://mozilla.org/example",
  ]);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "ExA",
    fireInputEvent: true,
  });
  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(details.autofill);
  Assert.equal(gURLBar.value, "ExAmple.com/");
  Assert.equal(gURLBar.selectionStart, "ExA".length);
  Assert.equal(gURLBar.selectionEnd, "ExAmple.com/".length);

  EventUtils.synthesizeKey("KEY_Backspace");
  await UrlbarTestUtils.promiseSearchComplete(window);
  details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.ok(!details.autofill);
  Assert.equal(gURLBar.value, "ExA");
  Assert.equal(gURLBar.selectionStart, "ExA".length);
  Assert.equal(gURLBar.selectionEnd, "ExA".length);

  let heuristicValue = "ExA";

  checkKeys([
    ["KEY_ArrowDown", "http://example.com/", 1],
    ["KEY_ArrowDown", "http://mozilla.org/example", 2],
    ["KEY_ArrowDown", "ExA", -1],
    ["KEY_ArrowUp", "http://mozilla.org/example", 2],
    ["KEY_ArrowUp", "http://example.com/", 1],
    ["KEY_ArrowUp", heuristicValue, 0],
    ["KEY_ArrowUp", "ExA", -1],
    ["KEY_ArrowDown", heuristicValue, 0],
  ]);

  await cleanUp();
});

function checkKeys(testTuples) {
  for (let [key, value, selectedIndex] of testTuples) {
    EventUtils.synthesizeKey(key);
    Assert.equal(UrlbarTestUtils.getSelectedRowIndex(window), selectedIndex);
    Assert.equal(gURLBar.untrimmedValue, value);
  }
}

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