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

"use strict";

add_setup(async function setup() {
  registerCleanupFunction(PlacesUtils.history.clear);
});

/**
 * Verify user typed text remains in the URL bar when tab switching, even when
 * loads fail.
 */
add_task(async function validURL() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_first_schemeless", false]],
  });
  let input = "http://i-definitely-dont-exist.example.com";
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/"
  );
  let browser = tab.linkedBrowser;
  // Note: Waiting on content document not being hidden because new tab pages can be preloaded,
  // in which case no load events fire.
  await SpecialPowers.spawn(browser, [], async () => {
    await ContentTaskUtils.waitForCondition(() => {
      return content.document && !content.document.hidden;
    });
  });
  let errorPageLoaded = BrowserTestUtils.waitForErrorPage(browser);
  gURLBar.value = input;
  gURLBar.select();
  EventUtils.sendKey("return");
  await errorPageLoaded;
  is(gURLBar.value, UrlbarTestUtils.trimURL(input), "Text is still in URL bar");
  await BrowserTestUtils.switchTab(gBrowser, tab.previousElementSibling);
  await BrowserTestUtils.switchTab(gBrowser, tab);
  is(
    gURLBar.value,
    UrlbarTestUtils.trimURL(input),
    "Text is still in URL bar after tab switch"
  );
  BrowserTestUtils.removeTab(tab);
});

/**
 * Invalid URIs fail differently (that is, immediately, in the loadURI call)
 * if keyword searches are turned off. Test that this works, too.
 */
add_task(async function invalidURL() {
  let input = "To be or not to be-that is the question";
  await SpecialPowers.pushPrefEnv({ set: [["keyword.enabled", false]] });
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:blank",
    false
  );
  let browser = tab.linkedBrowser;
  // Note: Waiting on content document not being hidden because new tab pages can be preloaded,
  // in which case no load events fire.
  await SpecialPowers.spawn(browser, [], async () => {
    await ContentTaskUtils.waitForCondition(() => {
      return content.document && !content.document.hidden;
    });
  });
  let errorPageLoaded = BrowserTestUtils.waitForErrorPage(tab.linkedBrowser);
  gURLBar.value = input;
  gURLBar.select();
  EventUtils.sendKey("return");
  await errorPageLoaded;
  is(gURLBar.value, input, "Text is still in URL bar");
  is(tab.linkedBrowser.userTypedValue, input, "Text still stored on browser");
  await BrowserTestUtils.switchTab(gBrowser, tab.previousElementSibling);
  await BrowserTestUtils.switchTab(gBrowser, tab);
  is(gURLBar.value, input, "Text is still in URL bar after tab switch");
  is(tab.linkedBrowser.userTypedValue, input, "Text still stored on browser");
  BrowserTestUtils.removeTab(tab);
});

/**
 * Test the urlbar status of text selection and focusing by tab switching.
 */
add_task(async function selectAndFocus() {
  // Test both protocols to ensure we're testing any trimming case.
  for (let protocol of ["http://", "https://"]) {
    const webpageTabURL = protocol + "example.com";
    const webpageTab = await BrowserTestUtils.openNewForegroundTab({
      gBrowser,
      url: webpageTabURL,
    });

    // Create a tab with userTypedValue.
    const userTypedTabText = "test";
    const userTypedTab = await BrowserTestUtils.openNewForegroundTab({
      gBrowser,
    });
    await UrlbarTestUtils.inputIntoURLBar(window, userTypedTabText);

    // Create an empty tab.
    const emptyTab = await BrowserTestUtils.openNewForegroundTab({ gBrowser });

    async function cleanup() {
      await PlacesUtils.history.clear();
      BrowserTestUtils.removeTab(webpageTab);
      BrowserTestUtils.removeTab(userTypedTab);
      BrowserTestUtils.removeTab(emptyTab);
    }

    await doSelectAndFocusTest({
      targetTab: webpageTab,
      targetSelectionStart: 0,
      targetSelectionEnd: 0,
      anotherTab: userTypedTab,
    });
    await doSelectAndFocusTest({
      targetTab: webpageTab,
      targetSelectionStart: 2,
      targetSelectionEnd: 5,
      anotherTab: userTypedTab,
    });
    await doSelectAndFocusTest({
      targetTab: webpageTab,
      targetSelectionStart: webpageTabURL.length,
      targetSelectionEnd: webpageTabURL.length,
      anotherTab: userTypedTab,
    });
    await doSelectAndFocusTest({
      targetTab: webpageTab,
      targetSelectionStart: 0,
      targetSelectionEnd: 0,
      anotherTab: emptyTab,
    });
    await doSelectAndFocusTest({
      targetTab: userTypedTab,
      targetSelectionStart: 0,
      targetSelectionEnd: 0,
      anotherTab: webpageTab,
    });
    await doSelectAndFocusTest({
      targetTab: userTypedTab,
      targetSelectionStart: 0,
      targetSelectionEnd: 0,
      anotherTab: emptyTab,
    });
    await doSelectAndFocusTest({
      targetTab: userTypedTab,
      targetSelectionStart: 1,
      targetSelectionEnd: 2,
      anotherTab: emptyTab,
    });
    await doSelectAndFocusTest({
      targetTab: userTypedTab,
      targetSelectionStart: userTypedTabText.length,
      targetSelectionEnd: userTypedTabText.length,
      anotherTab: emptyTab,
    });
    await doSelectAndFocusTest({
      targetTab: emptyTab,
      targetSelectionStart: 0,
      targetSelectionEnd: 0,
      anotherTab: webpageTab,
    });
    await doSelectAndFocusTest({
      targetTab: emptyTab,
      targetSelectionStart: 0,
      targetSelectionEnd: 0,
      anotherTab: userTypedTab,
    });

    await cleanup();
  }
});

async function doSelectAndFocusTest({
  targetTab,
  targetSelectionStart,
  targetSelectionEnd,
  anotherTab,
}) {
  const testCases = [
    {
      targetFocus: false,
      anotherFocus: false,
    },
    {
      targetFocus: true,
      anotherFocus: false,
    },
    {
      targetFocus: true,
      anotherFocus: true,
    },
  ];

  for (const { targetFocus, anotherFocus } of testCases) {
    // Setup the target tab.
    await switchTab(targetTab);
    setURLBarFocus(targetFocus);
    gURLBar.inputField.setSelectionRange(
      targetSelectionStart,
      targetSelectionEnd
    );
    const targetSelectedText = getSelectedText();
    if (gURLBar.selectionStart != gURLBar.selectionEnd) {
      Assert.ok(
        targetSelectedText,
        `Some text is selected: "${targetSelectedText}"`
      );
    }
    const targetValue = gURLBar.value;

    // Switch to another tab.
    await switchTab(anotherTab);
    setURLBarFocus(anotherFocus);

    // Switch back to the target tab.
    await switchTab(targetTab);

    // Check whether the value, selection and focusing status are reverted.
    Assert.equal(gURLBar.value, targetValue);
    Assert.equal(gURLBar.focused, targetFocus);
    if (gURLBar.focused) {
      // Check the selected text rather than the selection indices, to keep
      // untrimming into account.
      Assert.equal(targetSelectedText, getSelectedText());
    } else {
      Assert.equal(gURLBar.selectionStart, gURLBar.value.length);
      Assert.equal(gURLBar.selectionEnd, gURLBar.value.length);
    }
  }
}

function setURLBarFocus(focus) {
  if (focus) {
    // Simulate a user interaction, to eventually cause untrimming.
    EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
  } else {
    gURLBar.blur();
  }
}

function getSelectedText() {
  return gURLBar.inputField.editor.selection.toStringWithFormat(
    "text/plain",
    Ci.nsIDocumentEncoder.OutputPreformatted | Ci.nsIDocumentEncoder.OutputRaw,
    0
  );
}

async function switchTab(tab) {
  if (gBrowser.selectedTab !== tab) {
    EventUtils.synthesizeMouseAtCenter(tab, {});
    await BrowserTestUtils.waitForCondition(() => gBrowser.selectedTab === tab);
  }
}