browser_tabdialogbox_content_prompts.js (5026B)
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_CHROME = getRootDirectory(gTestPath); 7 const TEST_DIALOG_PATH = TEST_ROOT_CHROME + "subdialog.xhtml"; 8 9 const TEST_DATA_URI = "data:text/html,<body onload='alert(1)'>"; 10 const TEST_EXTENSION_DATA = { 11 background() { 12 // eslint-disable-next-line no-undef 13 browser.test.sendMessage("url", browser.runtime.getURL("alert.html")); 14 }, 15 manifest: { 16 name: "Test Extension", 17 }, 18 files: { 19 "alert.html": `<!DOCTYPE HTML> 20 <html> 21 <head> 22 <meta charset="utf-8"> 23 <title>TabDialogBox Content Modal Test page</title> 24 <script src="./alert.js"></script> 25 </head> 26 <body> 27 <h1>TabDialogBox Content Modal</h1> 28 </body> 29 </html>`, 30 "alert.js": `window.addEventListener("load", () => alert("Hi"));`, 31 }, 32 }; 33 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 34 const TEST_ORIGIN = "http://example.com"; 35 const TEST_PAGE = 36 TEST_ROOT_CHROME.replace("chrome://mochitests/content", TEST_ORIGIN) + 37 "test_page.html"; 38 39 var commonDialogsBundle = Services.strings.createBundle( 40 "chrome://global/locale/commonDialogs.properties" 41 ); 42 43 /** 44 * Test that a manager for content prompts is added to tab dialog box. 45 */ 46 add_task(async function test_tabdialog_content_prompts() { 47 await BrowserTestUtils.withNewTab( 48 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 49 "http://example.com", 50 async function (browser) { 51 info("Open a tab prompt."); 52 let dialogBox = gBrowser.getTabDialogBox(browser); 53 dialogBox.open(TEST_DIALOG_PATH); 54 55 info("Check the content prompt dialog is only created when needed."); 56 let contentPromptDialog = document.querySelector( 57 ".content-prompt-dialog" 58 ); 59 ok(!contentPromptDialog, "Content prompt dialog should not be created."); 60 61 info("Open a content prompt"); 62 dialogBox.open(TEST_DIALOG_PATH, { 63 modalType: Ci.nsIPrompt.MODAL_TYPE_CONTENT, 64 }); 65 66 contentPromptDialog = document.querySelector(".content-prompt-dialog"); 67 ok(contentPromptDialog, "Content prompt dialog should be created."); 68 let contentPromptManager = dialogBox.getContentDialogManager(); 69 70 is( 71 contentPromptManager._dialogs.length, 72 1, 73 "Content prompt manager should have 1 dialog box." 74 ); 75 } 76 ); 77 }); 78 79 /** 80 * Test origin text for a null principal. 81 */ 82 add_task(async function test_tabdialog_null_principal_title() { 83 let dialogShown = BrowserTestUtils.waitForEvent( 84 gBrowser, 85 "DOMWillOpenModalDialog" 86 ); 87 88 await BrowserTestUtils.withNewTab(TEST_DATA_URI, async function (browser) { 89 info("Waiting for dialog to open."); 90 await dialogShown; 91 await checkOriginText(browser); 92 }); 93 }); 94 95 /** 96 * Test origin text for an extension page. 97 */ 98 add_task(async function test_tabdialog_extension_title() { 99 let extension = ExtensionTestUtils.loadExtension(TEST_EXTENSION_DATA); 100 101 await extension.startup(); 102 let url = await extension.awaitMessage("url"); 103 let dialogShown = BrowserTestUtils.waitForEvent( 104 gBrowser, 105 "DOMWillOpenModalDialog" 106 ); 107 108 await BrowserTestUtils.withNewTab(url, async function (browser) { 109 info("Waiting for dialog to open."); 110 await dialogShown; 111 await checkOriginText(browser, "Test Extension"); 112 }); 113 114 await extension.unload(); 115 }); 116 117 /** 118 * Test origin text for a regular page. 119 */ 120 add_task(async function test_tabdialog_page_title() { 121 let dialogShown = BrowserTestUtils.waitForEvent( 122 gBrowser, 123 "DOMWillOpenModalDialog" 124 ); 125 126 await BrowserTestUtils.withNewTab(TEST_PAGE, async function (browser) { 127 info("Waiting for dialog to open."); 128 await dialogShown; 129 await checkOriginText(browser, TEST_ORIGIN); 130 }); 131 }); 132 133 /** 134 * Test helper for checking the origin header of a dialog. 135 * 136 * @param {object} browser 137 * The browser the dialog was opened from. 138 * @param {string | null} origin 139 * The page origin that should be displayed in the header, if any. 140 */ 141 async function checkOriginText(browser, origin = null) { 142 info("Check the title is visible."); 143 let dialogBox = gBrowser.getTabDialogBox(browser); 144 let contentPromptManager = dialogBox.getContentDialogManager(); 145 let dialog = contentPromptManager._dialogs[0]; 146 147 info("Waiting for dialog frame to be ready."); 148 await dialog._dialogReady; 149 150 let dialogDoc = dialog._frame.contentWindow.document; 151 let titleSelector = "#titleText"; 152 let infoTitle = dialogDoc.querySelector(titleSelector); 153 ok(BrowserTestUtils.isVisible(infoTitle), "Title text is visible"); 154 155 info("Check the displayed origin text is correct."); 156 if (origin) { 157 let host = origin; 158 try { 159 host = new URL(origin).host; 160 } catch (ex) { 161 /* will fail for the extension case. */ 162 } 163 is(infoTitle.textContent, host, "Origin should be in header."); 164 } else { 165 is( 166 infoTitle.dataset.l10nId, 167 "common-dialog-title-null", 168 "Null principal string should be in header." 169 ); 170 } 171 }