browser_test_focus_after_modal_state.js (2254B)
1 const TEST_URL = 2 "https://example.com/browser/dom/tests/browser/focus_after_prompt.html"; 3 4 const { PromptTestUtils } = ChromeUtils.importESModule( 5 "resource://testing-common/PromptTestUtils.sys.mjs" 6 ); 7 8 function awaitAndClosePrompt(browser) { 9 return PromptTestUtils.handleNextPrompt( 10 browser, 11 { modalType: Services.prompt.MODAL_TYPE_CONTENT, promptType: "prompt" }, 12 { buttonNumClick: 0 } 13 ); 14 } 15 16 add_setup(async function () { 17 await SpecialPowers.pushPrefEnv({ 18 set: [["test.wait300msAfterTabSwitch", true]], 19 }); 20 }); 21 22 add_task(async function () { 23 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL); 24 let browser = tab.linkedBrowser; 25 26 // Focus on editable iframe. 27 await BrowserTestUtils.synthesizeMouseAtCenter("#edit", {}, browser); 28 await SpecialPowers.spawn(browser, [], async function () { 29 is( 30 content.document.activeElement, 31 content.document.getElementById("edit"), 32 "Focus should be on iframe element" 33 ); 34 }); 35 36 let focusBlurPromise = SpecialPowers.spawn(browser, [], async function () { 37 let focusOccurred = false; 38 let blurOccurred = false; 39 40 return new Promise(resolve => { 41 let doc = content.document.getElementById("edit").contentDocument; 42 doc.addEventListener("focus", function () { 43 focusOccurred = true; 44 if (blurOccurred) { 45 resolve(true); 46 } 47 }); 48 49 doc.addEventListener("blur", function () { 50 blurOccurred = true; 51 if (focusOccurred) { 52 resolve(false); 53 } 54 }); 55 }); 56 }); 57 58 // Click on div that triggers a prompt, and then check that focus is back on 59 // the editable iframe. 60 let dialogShown = awaitAndClosePrompt(browser); 61 await SpecialPowers.spawn(browser, [], async function () { 62 let div = content.document.getElementById("clickMeDiv"); 63 div.click(); 64 }); 65 await dialogShown; 66 let blurCameFirst = await focusBlurPromise; 67 await SpecialPowers.spawn(browser, [], async function () { 68 is( 69 content.document.activeElement, 70 content.document.getElementById("edit"), 71 "Focus should be back on iframe element" 72 ); 73 }); 74 ok(blurCameFirst, "Should receive blur and then focus event"); 75 76 BrowserTestUtils.removeTab(tab); 77 });