browser_multiplePrompts.js (2803B)
1 "use strict"; 2 3 /** 4 * Goes through a stacked series of dialogs and ensures that 5 * the oldest one is front-most and has the right type. It 6 * then closes the oldest to newest dialog. 7 * 8 * @param {Element} tab The <tab> that has had content dialogs opened 9 * for it. 10 * @param {number} promptCount How many dialogs we expected to have been 11 * opened. 12 * 13 * @returns {Promise<void>} 14 * Resolves once the dialogs have all been closed. 15 */ 16 async function closeDialogs(tab, dialogCount) { 17 let dialogElementsCount = dialogCount; 18 let dialogs = 19 tab.linkedBrowser.tabDialogBox.getContentDialogManager().dialogs; 20 21 is( 22 dialogs.length, 23 dialogElementsCount, 24 "There should be " + dialogElementsCount + " dialog(s)." 25 ); 26 27 let i = dialogElementsCount - 1; 28 for (let dialog of dialogs) { 29 dialog.focus(true); 30 await dialog._dialogReady; 31 32 let dialogWindow = dialog.frameContentWindow; 33 let expectedType = ["alert", "prompt", "confirm"][i % 3]; 34 35 is( 36 dialogWindow.Dialog.args.text, 37 expectedType + " countdown #" + i, 38 "The #" + i + " alert should be labelled as such." 39 ); 40 i--; 41 42 dialogWindow.Dialog.ui.button0.click(); 43 44 // The click is handled async; wait for an event loop turn for that to 45 // happen. 46 await new Promise(function (resolve) { 47 Services.tm.dispatchToMainThread(resolve); 48 }); 49 } 50 51 dialogs = tab.linkedBrowser.tabDialogBox.getContentDialogManager().dialogs; 52 is(dialogs.length, 0, "Dialogs should all be dismissed."); 53 } 54 55 /* 56 * This test triggers multiple alerts on one single tab, because it"s possible 57 * for web content to do so. The behavior is described in bug 1266353. 58 * 59 * We assert the presentation of the multiple alerts, ensuring we show only 60 * the oldest one. 61 */ 62 add_task(async function () { 63 const PROMPTCOUNT = 9; 64 65 let unopenedPromptCount = PROMPTCOUNT; 66 67 let tab = await BrowserTestUtils.openNewForegroundTab( 68 gBrowser, 69 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 70 "http://example.com", 71 true 72 ); 73 info("Tab loaded"); 74 75 let promptsOpenedPromise = BrowserTestUtils.waitForEvent( 76 tab.linkedBrowser, 77 "DOMWillOpenModalDialog", 78 false, 79 () => { 80 unopenedPromptCount--; 81 return unopenedPromptCount == 0; 82 } 83 ); 84 85 await SpecialPowers.spawn(tab.linkedBrowser, [PROMPTCOUNT], maxPrompts => { 86 var i = maxPrompts; 87 let fns = ["alert", "prompt", "confirm"]; 88 function openDialog() { 89 i--; 90 if (i) { 91 SpecialPowers.Services.tm.dispatchToMainThread(openDialog); 92 } 93 content[fns[i % 3]](fns[i % 3] + " countdown #" + i); 94 } 95 SpecialPowers.Services.tm.dispatchToMainThread(openDialog); 96 }); 97 98 await promptsOpenedPromise; 99 100 await closeDialogs(tab, PROMPTCOUNT); 101 102 BrowserTestUtils.removeTab(tab); 103 });