diff options
Diffstat (limited to 'browser/components/urlbar/tests/browser/browser_keepStateAcrossTabSwitches.js')
-rw-r--r-- | browser/components/urlbar/tests/browser/browser_keepStateAcrossTabSwitches.js | 195 |
1 files changed, 108 insertions, 87 deletions
diff --git a/browser/components/urlbar/tests/browser/browser_keepStateAcrossTabSwitches.js b/browser/components/urlbar/tests/browser/browser_keepStateAcrossTabSwitches.js index 6ad6ce43e6..abe12846ab 100644 --- a/browser/components/urlbar/tests/browser/browser_keepStateAcrossTabSwitches.js +++ b/browser/components/urlbar/tests/browser/browser_keepStateAcrossTabSwitches.js @@ -3,6 +3,10 @@ "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. @@ -78,94 +82,94 @@ add_task(async function invalidURL() { * Test the urlbar status of text selection and focusing by tab switching. */ add_task(async function selectAndFocus() { - // Create a tab with normal web page. Use a test-url that uses a protocol that - // is not trimmed. - const webpageTabURL = - UrlbarTestUtils.getTrimmedProtocolWithSlashes() == "https://" - ? "http://example.com" - : "https://example.com"; - const webpageTab = await BrowserTestUtils.openNewForegroundTab({ - gBrowser, - url: webpageTabURL, - }); + // 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 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 }); + // Create an empty tab. + const emptyTab = await BrowserTestUtils.openNewForegroundTab({ gBrowser }); - registerCleanupFunction(async () => { - await PlacesUtils.history.clear(); - BrowserTestUtils.removeTab(webpageTab); - BrowserTestUtils.removeTab(userTypedTab); - BrowserTestUtils.removeTab(emptyTab); - }); + 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 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({ @@ -197,6 +201,13 @@ async function doSelectAndFocusTest({ 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. @@ -210,8 +221,9 @@ async function doSelectAndFocusTest({ Assert.equal(gURLBar.value, targetValue); Assert.equal(gURLBar.focused, targetFocus); if (gURLBar.focused) { - Assert.equal(gURLBar.selectionStart, targetSelectionStart); - Assert.equal(gURLBar.selectionEnd, targetSelectionEnd); + // 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); @@ -221,12 +233,21 @@ async function doSelectAndFocusTest({ function setURLBarFocus(focus) { if (focus) { - gURLBar.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, {}); |