browser_beforeunload_urlbar.js (1840B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_ROOT = getRootDirectory(gTestPath).replace( 7 "chrome://mochitests/content", 8 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 9 "http://example.com" 10 ); 11 12 add_task(async function test_beforeunload_stay_clears_urlbar() { 13 await SpecialPowers.pushPrefEnv({ 14 set: [["dom.require_user_interaction_for_beforeunload", false]], 15 }); 16 17 const TEST_URL = TEST_ROOT + "file_beforeunload_stop.html"; 18 await BrowserTestUtils.withNewTab(TEST_URL, async function (browser) { 19 gURLBar.focus(); 20 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 21 const inputValue = "http://example.org/?q=typed"; 22 gURLBar.value = inputValue.slice(0, -1); 23 EventUtils.sendString(inputValue.slice(-1)); 24 25 let promptOpenedPromise = BrowserTestUtils.promiseAlertDialogOpen("cancel"); 26 EventUtils.synthesizeKey("VK_RETURN"); 27 await promptOpenedPromise; 28 await TestUtils.waitForTick(); 29 30 // Can't just compare directly with TEST_URL because the URL may be trimmed. 31 // Just need it to not be the example.org thing we typed in. 32 ok( 33 gURLBar.value.endsWith("_stop.html"), 34 "Url bar should be reset to point to the stop html file" 35 ); 36 ok( 37 gURLBar.value.includes("example.com"), 38 "Url bar should be reset to example.com" 39 ); 40 // Check the lock/identity icons are back: 41 is( 42 gURLBar.getAttribute("pageproxystate"), 43 "valid", 44 "Should be in valid pageproxy state." 45 ); 46 47 // Now we need to get rid of the handler to avoid the prompt coming up when trying to close the 48 // tab when we exit `withNewTab`. :-) 49 await SpecialPowers.spawn(browser, [], function () { 50 content.window.onbeforeunload = null; 51 }); 52 }); 53 });