summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_valueOnTabSwitch.js
blob: 9d3e922692d261122570fd3d362987336f38b571 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * This tests for the correct URL being displayed in the URL bar after switching
 * tabs which are in different states (e.g. deleted, partially deleted).
 */

"use strict";

const TEST_URL = `${TEST_BASE_URL}dummy_page.html`;

add_task(async function () {
  // autofill may conflict with the test scope, by filling missing parts of
  // the url due to autoOpen.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.autoFill", false]],
  });

  let charsToDelete,
    deletedURLTab,
    fullURLTab,
    partialURLTab,
    testPartialURL,
    testURL;

  charsToDelete = 5;
  deletedURLTab = BrowserTestUtils.addTab(gBrowser);
  fullURLTab = BrowserTestUtils.addTab(gBrowser);
  partialURLTab = BrowserTestUtils.addTab(gBrowser);
  testURL = TEST_URL;

  let loaded1 = BrowserTestUtils.browserLoaded(
    deletedURLTab.linkedBrowser,
    false,
    testURL
  );
  let loaded2 = BrowserTestUtils.browserLoaded(
    fullURLTab.linkedBrowser,
    false,
    testURL
  );
  let loaded3 = BrowserTestUtils.browserLoaded(
    partialURLTab.linkedBrowser,
    false,
    testURL
  );
  BrowserTestUtils.loadURIString(deletedURLTab.linkedBrowser, testURL);
  BrowserTestUtils.loadURIString(fullURLTab.linkedBrowser, testURL);
  BrowserTestUtils.loadURIString(partialURLTab.linkedBrowser, testURL);
  await Promise.all([loaded1, loaded2, loaded3]);

  testURL = BrowserUIUtils.trimURL(testURL);
  testPartialURL = testURL.substr(0, testURL.length - charsToDelete);

  function cleanUp() {
    gBrowser.removeTab(fullURLTab);
    gBrowser.removeTab(partialURLTab);
    gBrowser.removeTab(deletedURLTab);
  }

  async function cycleTabs() {
    await BrowserTestUtils.switchTab(gBrowser, fullURLTab);
    is(
      gURLBar.value,
      testURL,
      "gURLBar.value should be testURL after switching back to fullURLTab"
    );

    await BrowserTestUtils.switchTab(gBrowser, partialURLTab);
    is(
      gURLBar.value,
      testPartialURL,
      "gURLBar.value should be testPartialURL after switching back to partialURLTab"
    );
    await BrowserTestUtils.switchTab(gBrowser, deletedURLTab);
    is(
      gURLBar.value,
      testURL,
      "gURLBar.value should be testURL after switching back to deletedURLTab"
    );

    await BrowserTestUtils.switchTab(gBrowser, fullURLTab);
    is(
      gURLBar.value,
      testURL,
      "gURLBar.value should be testURL after switching back to fullURLTab"
    );
  }

  function urlbarBackspace(removeAll) {
    return new Promise((resolve, reject) => {
      gBrowser.selectedBrowser.focus();
      gURLBar.addEventListener(
        "input",
        function () {
          resolve();
        },
        { once: true }
      );
      gURLBar.focus();
      if (removeAll) {
        gURLBar.select();
      } else {
        gURLBar.selectionStart = gURLBar.selectionEnd = gURLBar.value.length;
      }
      EventUtils.synthesizeKey("KEY_Backspace");
    });
  }

  async function prepareDeletedURLTab() {
    await BrowserTestUtils.switchTab(gBrowser, deletedURLTab);
    is(
      gURLBar.value,
      testURL,
      "gURLBar.value should be testURL after initial switch to deletedURLTab"
    );

    // simulate the user removing the whole url from the location bar
    await urlbarBackspace(true);
    is(gURLBar.value, "", 'gURLBar.value should be "" (just set)');
  }

  async function prepareFullURLTab() {
    await BrowserTestUtils.switchTab(gBrowser, fullURLTab);
    is(
      gURLBar.value,
      testURL,
      "gURLBar.value should be testURL after initial switch to fullURLTab"
    );
  }

  async function preparePartialURLTab() {
    await BrowserTestUtils.switchTab(gBrowser, partialURLTab);
    is(
      gURLBar.value,
      testURL,
      "gURLBar.value should be testURL after initial switch to partialURLTab"
    );

    // simulate the user removing part of the url from the location bar
    let deleted = 0;
    while (deleted < charsToDelete) {
      await urlbarBackspace(false);
      deleted++;
    }

    is(
      gURLBar.value,
      testPartialURL,
      "gURLBar.value should be testPartialURL (just set)"
    );
  }

  // prepare the three tabs required by this test

  // First tab
  await prepareFullURLTab();
  await preparePartialURLTab();
  await prepareDeletedURLTab();

  // now cycle the tabs and make sure everything looks good
  await cycleTabs();
  cleanUp();
});