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

"use strict";

// Tests retained results.
// When there is a pending search (user typed a search string and blurred
// without picking a result), on focus we should the search results again.

async function checkPanelStatePersists(win, isOpen) {
  // Check for popup events, we should not see any of them because the urlbar
  // popup state should not change. This also ensures we don't cause flickering
  // open/close actions.
  function handler(event) {
    Assert.ok(false, `Received unexpected event ${event.type}`);
  }
  win.gURLBar.addEventListener("popupshowing", handler);
  win.gURLBar.addEventListener("popuphiding", handler);
  // Because the panel opening may not be immediate, we must wait a bit.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 300));
  win.gURLBar.removeEventListener("popupshowing", handler);
  win.gURLBar.removeEventListener("popuphiding", handler);
  Assert.equal(
    isOpen,
    win.gURLBar.view.isOpen,
    `check urlbar remains ${isOpen ? "open" : "closed"}`
  );
}

async function checkOpensOnFocus(win, state) {
  Assert.ok(!win.gURLBar.view.isOpen, "check urlbar panel is not open");
  win.gURLBar.blur();

  info("Check the keyboard shortcut.");
  await UrlbarTestUtils.promisePopupOpen(win, () => {
    win.document.getElementById("Browser:OpenLocation").doCommand();
  });

  await UrlbarTestUtils.promiseSearchComplete(win);
  Assert.equal(state.selectionStart, win.gURLBar.selectionStart);
  Assert.equal(state.selectionEnd, win.gURLBar.selectionEnd);

  await UrlbarTestUtils.promisePopupClose(win, () => {
    win.gURLBar.blur();
  });
  info("Focus with the mouse.");
  await UrlbarTestUtils.promisePopupOpen(win, () => {
    EventUtils.synthesizeMouseAtCenter(win.gURLBar.inputField, {}, win);
  });

  await UrlbarTestUtils.promiseSearchComplete(win);
  Assert.equal(state.selectionStart, win.gURLBar.selectionStart);
  Assert.equal(state.selectionEnd, win.gURLBar.selectionEnd);
}

async function checkDoesNotOpenOnFocus(win) {
  Assert.ok(!win.gURLBar.view.isOpen, "check urlbar panel is not open");
  win.gURLBar.blur();

  info("Check the keyboard shortcut.");
  let promiseState = checkPanelStatePersists(win, false);
  win.document.getElementById("Browser:OpenLocation").doCommand();
  await promiseState;
  win.gURLBar.blur();
  info("Focus with the mouse.");
  promiseState = checkPanelStatePersists(win, false);
  EventUtils.synthesizeMouseAtCenter(win.gURLBar.inputField, {}, win);
  await promiseState;
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.autoFill", true]],
  });
  // Add some history for the empty panel and autofill.
  await PlacesTestUtils.addVisits([
    {
      uri: "https://example.com/",
      transition: PlacesUtils.history.TRANSITIONS.TYPED,
    },
    {
      uri: "https://example.com/foo/",
      transition: PlacesUtils.history.TRANSITIONS.TYPED,
    },
  ]);
  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
  });
});

async function test_window(win) {
  for (let url of ["about:newtab", "about:home", "https://example.com/"]) {
    // withNewTab may hang on preloaded pages, thus instead of waiting for load
    // we just wait for the expected currentURI value.
    await BrowserTestUtils.withNewTab(
      { gBrowser: win.gBrowser, url, waitForLoad: false },
      async browser => {
        await TestUtils.waitForCondition(
          () => win.gBrowser.currentURI.spec == url,
          "Ensure we're on the expected page"
        );

        // In one case use a value that triggers autofill.
        let autofill = url == "https://example.com/";
        await UrlbarTestUtils.promiseAutocompleteResultPopup({
          window: win,
          value: autofill ? "ex" : "foo",
          fireInputEvent: true,
        });
        let { value, selectionStart, selectionEnd } = win.gURLBar;
        if (!autofill) {
          selectionStart = 0;
        }
        info("expected " + value + " " + selectionStart + " " + selectionEnd);
        await UrlbarTestUtils.promisePopupClose(win, () => {
          win.gURLBar.blur();
        });

        info("The panel should open when there's a search string");
        await checkOpensOnFocus(win, { value, selectionStart, selectionEnd });
        await UrlbarTestUtils.promisePopupClose(win, () => {
          win.gURLBar.blur();
        });
      }
    );
  }
}

add_task(async function test_normalWindow() {
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await test_window(win);
  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_privateWindow() {
  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await test_window(privateWin);
  await BrowserTestUtils.closeWindow(privateWin);
});

add_task(async function test_tabSwitch() {
  info("Check that switching tabs reopens the view.");
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: win,
    value: "ex",
    fireInputEvent: true,
  });
  let { value, selectionStart, selectionEnd } = win.gURLBar;
  Assert.equal(value, "example.com/", "Check autofill value");
  Assert.ok(
    selectionStart > 0 && selectionEnd > selectionStart,
    "Check autofill selection"
  );

  Assert.ok(win.gURLBar.focused, "The urlbar should be focused");
  let tab1 = win.gBrowser.selectedTab;

  async function check_autofill() {
    // The urlbar code waits for both TabSelect and the focus change, thus
    // we can't just wait for search completion here, we have to poll for a
    // value.
    await TestUtils.waitForCondition(
      () => win.gURLBar.value == "example.com/",
      "wait for autofill value"
    );
    // Ensure stable results.
    await UrlbarTestUtils.promiseSearchComplete(win);
    Assert.equal(selectionStart, win.gURLBar.selectionStart);
    Assert.equal(selectionEnd, win.gURLBar.selectionEnd);
  }

  info("Open a new tab with the same search");
  let tab2 = await BrowserTestUtils.openNewForegroundTab(win.gBrowser);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: win,
    value: "ex",
    fireInputEvent: true,
  });

  info("Switch across tabs");
  for (let tab of win.gBrowser.tabs) {
    await UrlbarTestUtils.promisePopupOpen(win, async () => {
      await BrowserTestUtils.switchTab(win.gBrowser, tab);
    });
    await check_autofill();
  }

  info("Close tab and check the view is open.");
  await UrlbarTestUtils.promisePopupOpen(win, () => {
    BrowserTestUtils.removeTab(tab2);
  });
  await check_autofill();

  info("Open a new tab with a different search");
  tab2 = await BrowserTestUtils.openNewForegroundTab(win.gBrowser);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: win,
    value: "xam",
    fireInputEvent: true,
  });

  info("Switch to the first tab and check the panel remains open");
  let promiseState = checkPanelStatePersists(win, true);
  await BrowserTestUtils.switchTab(win.gBrowser, tab1);
  await promiseState;
  await UrlbarTestUtils.promiseSearchComplete(win);
  await check_autofill();

  info("Switch to the second tab and check the panel remains open");
  promiseState = checkPanelStatePersists(win, true);
  await BrowserTestUtils.switchTab(win.gBrowser, tab2);
  await promiseState;
  await UrlbarTestUtils.promiseSearchComplete(win);
  Assert.equal(win.gURLBar.value, "xam", "check value");
  Assert.equal(win.gURLBar.selectionStart, 3);
  Assert.equal(win.gURLBar.selectionEnd, 3);

  info("autofill in tab2, switch to tab1, then back to tab2 with the mouse");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: win,
    value: "e",
    fireInputEvent: true,
  });
  // Adjust selection start, we are using a different search string.
  await BrowserTestUtils.switchTab(win.gBrowser, tab1);
  await UrlbarTestUtils.promiseSearchComplete(win);
  await check_autofill();
  tab2.click();
  selectionStart = 1;
  await check_autofill();

  info("Check we don't rerun a search if the shortcut is used on an open view");
  EventUtils.synthesizeKey("KEY_Backspace", {}, win);
  await UrlbarTestUtils.promiseSearchComplete(win);
  Assert.ok(win.gURLBar.view.isOpen, "The view should be open");
  Assert.equal(win.gURLBar.value, "e", "The value should be the typed one");
  win.document.getElementById("Browser:OpenLocation").doCommand();
  // A search should not run here, so there's nothing to wait for.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 300));
  Assert.ok(win.gURLBar.view.isOpen, "The view should be open");
  Assert.equal(win.gURLBar.value, "e", "The value should not change");

  info(
    "Tab switch from an empty search tab with unfocused urlbar to a tab with a search string and a focused urlbar"
  );
  win.gURLBar.value = "";
  win.gURLBar.blur();
  await UrlbarTestUtils.promisePopupOpen(win, async () => {
    await BrowserTestUtils.switchTab(win.gBrowser, tab1);
  });

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_tabSwitch_pageproxystate() {
  info("Switching tabs on valid pageproxystate doesn't reopen.");

  info("Adding some visits for the empty panel");
  await PlacesTestUtils.addVisits([
    "https://example.com/",
    "https://example.org/",
  ]);
  registerCleanupFunction(PlacesUtils.history.clear);

  let win = await BrowserTestUtils.openNewBrowserWindow();
  BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, "about:robots");
  let tab1 = win.gBrowser.selectedTab;

  info("Open a new tab and the empty search");
  let tab2 = await BrowserTestUtils.openNewForegroundTab(win.gBrowser);
  await UrlbarTestUtils.promisePopupOpen(win, async () => {
    win.gURLBar.focus();
    // On Linux and Mac down moves caret to the end of the text unless it's
    // there already.
    win.gURLBar.selectionStart = win.gURLBar.selectionEnd =
      win.gURLBar.value.length;
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, win);
  });
  await UrlbarTestUtils.promiseSearchComplete(win);
  let result = await UrlbarTestUtils.getDetailsOfResultAt(win, 0);
  Assert.notEqual(result.url, "about:robots");

  info("Switch to the first tab and start searching with DOWN");
  await UrlbarTestUtils.promisePopupClose(win, async () => {
    await BrowserTestUtils.switchTab(win.gBrowser, tab1);
  });
  await checkPanelStatePersists(win, false);
  await UrlbarTestUtils.promisePopupOpen(win, async () => {
    win.gURLBar.focus();
    // On Linux and Mac down moves caret to the end of the text unless it's
    // there already.
    win.gURLBar.selectionStart = win.gURLBar.selectionEnd =
      win.gURLBar.value.length;
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, win);
  });
  await UrlbarTestUtils.promiseSearchComplete(win);

  info("Switcihng to the second tab should not reopen the search");
  await UrlbarTestUtils.promisePopupClose(win, async () => {
    await BrowserTestUtils.switchTab(win.gBrowser, tab2);
  });
  await checkPanelStatePersists(win, false);

  info("Switching to the first tab should not reopen the search");
  let promiseState = await checkPanelStatePersists(win, false);
  await BrowserTestUtils.switchTab(win.gBrowser, tab1);
  await promiseState;
  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_tabSwitch_emptySearch() {
  info("Switching between empty-search tabs should not reopen the view.");
  let win = await BrowserTestUtils.openNewBrowserWindow();

  info("Open the empty search");
  let tab1 = win.gBrowser.selectedTab;
  await UrlbarTestUtils.promisePopupOpen(win, async () => {
    win.gURLBar.focus();
    // On Linux and Mac down moves caret to the end of the text unless it's
    // there already.
    win.gURLBar.selectionStart = win.gURLBar.selectionEnd =
      win.gURLBar.value.length;
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, win);
  });
  await UrlbarTestUtils.promiseSearchComplete(win);

  info("Open a new tab and the empty search");
  let tab2 = await BrowserTestUtils.openNewForegroundTab(win.gBrowser);
  await UrlbarTestUtils.promisePopupOpen(win, async () => {
    win.gURLBar.focus();
    // On Linux and Mac down moves caret to the end of the text unless it's
    // there already.
    win.gURLBar.selectionStart = win.gURLBar.selectionEnd =
      win.gURLBar.value.length;
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, win);
  });
  await UrlbarTestUtils.promiseSearchComplete(win);

  info("Switching to the first tab should not reopen the view");
  await UrlbarTestUtils.promisePopupClose(win, async () => {
    await BrowserTestUtils.switchTab(win.gBrowser, tab1);
  });
  await checkPanelStatePersists(win, false);

  info("Switching to the second tab should not reopen the view");
  let promiseState = await checkPanelStatePersists(win, false);
  await BrowserTestUtils.switchTab(win.gBrowser, tab2);
  await promiseState;

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_pageproxystate_valid() {
  info("Focusing on valid pageproxystate should not reopen the view.");
  let win = await BrowserTestUtils.openNewBrowserWindow();

  info("Search for a full url and confirm it with Enter");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: win,
    value: "about:robots",
    fireInputEvent: true,
  });
  let loadedPromise = BrowserTestUtils.browserLoaded(
    win.gBrowser.selectedBrowser
  );
  EventUtils.synthesizeKey("KEY_Enter", {}, win);
  await loadedPromise;

  Assert.ok(!win.gURLBar.focused, "The urlbar should not be focused");
  info("Focus the urlbar");
  win.document.getElementById("Browser:OpenLocation").doCommand();

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_allowAutofill() {
  info("Check we respect allowAutofill from the last search");
  let win = await BrowserTestUtils.openNewBrowserWindow();

  await UrlbarTestUtils.promisePopupOpen(win, async () => {
    await selectAndPaste("e", win);
  });
  Assert.equal(win.gURLBar.value, "e", "Should not autofill");
  let context = await win.gURLBar.lastQueryContextPromise;
  Assert.equal(context.allowAutofill, false, "Check initial allowAutofill");
  await UrlbarTestUtils.promisePopupClose(win);

  await UrlbarTestUtils.promisePopupOpen(win, async () => {
    win.document.getElementById("Browser:OpenLocation").doCommand();
  });
  await UrlbarTestUtils.promiseSearchComplete(win);
  Assert.equal(win.gURLBar.value, "e", "Should not autofill");
  context = await win.gURLBar.lastQueryContextPromise;
  Assert.equal(context.allowAutofill, false, "Check reopened allowAutofill");

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_clicks_after_autofill() {
  info(
    "Check that clickin on an autofilled input field doesn't requery, causing loss of the caret position"
  );
  let win = await BrowserTestUtils.openNewBrowserWindow();
  info("autofill in tab2, switch to tab1, then back to tab2 with the mouse");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: win,
    value: "e",
    fireInputEvent: true,
  });
  Assert.equal(win.gURLBar.value, "example.com/", "Should have autofilled");

  // Check single click.
  let input = win.gURLBar.inputField;
  EventUtils.synthesizeMouse(input, 30, 10, {}, win);
  // Wait a bit, in case of a mistake this would run a query, otherwise not.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 300));
  Assert.ok(win.gURLBar.selectionStart < win.gURLBar.value.length);
  Assert.equal(win.gURLBar.selectionStart, win.gURLBar.selectionEnd);

  // Check double click.
  EventUtils.synthesizeMouse(input, 30, 10, { clickCount: 2 }, win);
  // Wait a bit, in case of a mistake this would run a query, otherwise not.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 300));
  Assert.ok(win.gURLBar.selectionStart < win.gURLBar.value.length);
  Assert.ok(win.gURLBar.selectionEnd > win.gURLBar.selectionStart);

  await BrowserTestUtils.closeWindow(win);
});