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

// Checks that we trim invalid urls when they are selected, so that if the user
// modifies the selected url, or just closes the results pane, we do a visit
// rather than searching for the trimmed string.

const url = BrowserUIUtils.trimURLProtocol + "invalid.somehost/mytest";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.trimURLs", true]],
  });
  await PlacesTestUtils.addVisits(url);
  registerCleanupFunction(PlacesUtils.history.clear);
});

add_task(async function test_escape() {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "invalid",
  });
  // Look for our result.
  let resultCount = UrlbarTestUtils.getResultCount(window);
  Assert.greater(resultCount, 1, "There should be at least two results");
  for (let i = 0; i < resultCount; i++) {
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    info(`Result at ${i} has url ${result.url}`);
    if (result.url.startsWith(url)) {
      break;
    }
    EventUtils.synthesizeKey("KEY_ArrowDown");
  }
  Assert.equal(
    gURLBar.value,
    url,
    "The string displayed in the textbox should be the untrimmed url"
  );
  // Close the results pane by ESC.
  await UrlbarTestUtils.promisePopupClose(window, () => {
    EventUtils.synthesizeKey("KEY_Escape");
  });
  // Confirm the result and check the loaded page.
  let promise = waitforLoadURL();
  EventUtils.synthesizeKey("KEY_Enter");
  let loadedUrl = await promise;
  Assert.equal(loadedUrl, url, "Should try to load a url");
});

add_task(async function test_edit_url() {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "invalid",
  });
  // Look for our result.
  let resultCount = UrlbarTestUtils.getResultCount(window);
  Assert.greater(resultCount, 1, "There should be at least two results");
  for (let i = 1; i < resultCount; i++) {
    EventUtils.synthesizeKey("KEY_ArrowDown");
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    info(`Result at ${i} has url ${result.url}`);
    if (result.url.startsWith(url)) {
      break;
    }
  }
  Assert.equal(
    gURLBar.value,
    url,
    "The string displayed in the textbox should be the untrimmed url"
  );
  // Modify the url.
  EventUtils.synthesizeKey("2");
  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.URL, "Should visit a url");
  Assert.equal(result.url, url + "2", "Should visit the modified url");

  // Confirm the result and check the loaded page.
  let promise = waitforLoadURL();
  EventUtils.synthesizeKey("KEY_Enter");
  let loadedUrl = await promise;
  Assert.equal(loadedUrl, url + "2", "Should try to load the modified url");
});

async function waitforLoadURL() {
  let sandbox = sinon.createSandbox();
  let loadedUrl = await new Promise(resolve =>
    sandbox.stub(gURLBar, "_loadURL").callsFake(resolve)
  );
  sandbox.restore();
  return loadedUrl;
}