browser_cancel_keydown_keypress_event.js (1338B)
1 const URL = 2 "https://example.com/browser/dom/tests/browser/prevent_return_key.html"; 3 4 const { PromptTestUtils } = ChromeUtils.importESModule( 5 "resource://testing-common/PromptTestUtils.sys.mjs" 6 ); 7 8 // Wait for alert dialog and dismiss it immediately. 9 function awaitAndCloseAlertDialog(browser) { 10 return PromptTestUtils.handleNextPrompt( 11 browser, 12 { modalType: Services.prompt.MODAL_TYPE_CONTENT, promptType: "alert" }, 13 { buttonNumClick: 0 } 14 ); 15 } 16 17 add_task(async function () { 18 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL); 19 let browser = tab.linkedBrowser; 20 21 // Focus and enter random text on input. 22 await SpecialPowers.spawn(browser, [], async function () { 23 let input = content.document.getElementById("input"); 24 input.focus(); 25 input.value = "abcd"; 26 }); 27 28 // Send return key (cross process) to submit the form implicitly. 29 let dialogShown = awaitAndCloseAlertDialog(browser); 30 EventUtils.synthesizeKey("KEY_Enter"); 31 await dialogShown; 32 33 // Check that the form should not have been submitted. 34 await SpecialPowers.spawn(browser, [], async function () { 35 let result = content.document.getElementById("result").innerHTML; 36 info("submit result: " + result); 37 is(result, "not submitted", "form should not have submitted"); 38 }); 39 40 gBrowser.removeCurrentTab(); 41 });