browser_openPromptInBackgroundTab.js (5367B)
1 "use strict"; 2 3 const { PermissionTestUtils } = ChromeUtils.importESModule( 4 "resource://testing-common/PermissionTestUtils.sys.mjs" 5 ); 6 7 const ROOT = getRootDirectory(gTestPath).replace( 8 "chrome://mochitests/content/", 9 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 10 "http://example.com/" 11 ); 12 let pageWithAlert = ROOT + "openPromptOffTimeout.html"; 13 14 registerCleanupFunction(function () { 15 Services.perms.removeAll(); 16 }); 17 18 /* 19 * This test opens a tab that alerts when it is hidden. We then switch away 20 * from the tab, and check that by default the tab is not automatically 21 * re-selected. We also check that a checkbox appears in the alert that allows 22 * the user to enable this automatically re-selecting. We then check that 23 * checking the checkbox does actually enable that behaviour. 24 */ 25 add_task(async function test_modal_ui() { 26 // Make sure we clear the focus tab permission set in the previous test 27 PermissionTestUtils.remove(pageWithAlert, "focus-tab-by-prompt"); 28 29 let firstTab = gBrowser.selectedTab; 30 // load page that opens prompt when page is hidden 31 let openedTab = await BrowserTestUtils.openNewForegroundTab( 32 gBrowser, 33 pageWithAlert, 34 true 35 ); 36 let openedTabGotAttentionPromise = BrowserTestUtils.waitForAttribute( 37 "attention", 38 openedTab 39 ); 40 // switch away from that tab again - this triggers the alert. 41 await BrowserTestUtils.switchTab(gBrowser, firstTab); 42 // ... but that's async on e10s... 43 await openedTabGotAttentionPromise; 44 // check for attention attribute 45 is( 46 openedTab.hasAttribute("attention"), 47 true, 48 "Tab with alert should have 'attention' attribute." 49 ); 50 ok(!openedTab.selected, "Tab with alert should not be selected"); 51 52 // switch tab back, and check the checkbox is displayed: 53 await BrowserTestUtils.switchTab(gBrowser, openedTab); 54 // check the prompt is there 55 let promptElements = openedTab.linkedBrowser 56 .closest(".browserSidebarContainer") 57 .querySelectorAll(".content-prompt-dialog"); 58 59 let dialogBox = gBrowser.getTabDialogBox(openedTab.linkedBrowser); 60 let contentPromptManager = dialogBox.getContentDialogManager(); 61 is(promptElements.length, 1, "There should be 1 prompt"); 62 is( 63 contentPromptManager._dialogs.length, 64 1, 65 "Content prompt manager should have 1 dialog box." 66 ); 67 68 // make sure the checkbox appears and that the permission for allowing tab switching 69 // is set when the checkbox is tickted and the dialog is accepted 70 let dialog = contentPromptManager._dialogs[0]; 71 72 await dialog._dialogReady; 73 74 let dialogDoc = dialog._frame.contentWindow.document; 75 let checkbox = dialogDoc.querySelector("checkbox[label*='example.com']"); 76 let button = dialogDoc.querySelector("#commonDialog").getButton("accept"); 77 78 ok(checkbox, "The checkbox should be there"); 79 ok(!checkbox.checked, "Checkbox shouldn't be checked"); 80 81 // tick box and accept dialog 82 checkbox.checked = true; 83 button.click(); 84 // Wait for that click to actually be handled completely. 85 await new Promise(function (resolve) { 86 Services.tm.dispatchToMainThread(resolve); 87 }); 88 89 ok(!contentPromptManager._dialogs.length, "Dialog should be closed"); 90 91 // check permission is set 92 is( 93 Services.perms.ALLOW_ACTION, 94 PermissionTestUtils.testPermission(pageWithAlert, "focus-tab-by-prompt"), 95 "Tab switching should now be allowed" 96 ); 97 98 // Check if the control center shows the correct permission. 99 let shown = BrowserTestUtils.waitForEvent( 100 window, 101 "popupshown", 102 true, 103 event => event.target == gPermissionPanel._permissionPopup 104 ); 105 gPermissionPanel._identityPermissionBox.click(); 106 await shown; 107 let labelText = SitePermissions.getPermissionLabel("focus-tab-by-prompt"); 108 let permissionsList = document.getElementById( 109 "permission-popup-permission-list" 110 ); 111 let label = permissionsList.querySelector( 112 ".permission-popup-permission-label" 113 ); 114 is(label.textContent, labelText); 115 gPermissionPanel.hidePopup(); 116 117 // Check if the identity icon signals granted permission. 118 ok( 119 gPermissionPanel._identityPermissionBox.hasAttribute("hasPermissions"), 120 "identity-permission-box signals granted permissions" 121 ); 122 123 let openedTabSelectedPromise = BrowserTestUtils.waitForAttribute( 124 "selected", 125 openedTab, 126 "true" 127 ); 128 129 // switch to other tab again 130 await BrowserTestUtils.switchTab(gBrowser, firstTab); 131 132 // This is sync in non-e10s, but in e10s we need to wait for this, so yield anyway. 133 // Note that the switchTab promise doesn't actually guarantee anything about *which* 134 // tab ends up as selected when its event fires, so using that here wouldn't work. 135 await openedTabSelectedPromise; 136 137 Assert.strictEqual(contentPromptManager._dialogs.length, 1, "Dialog opened."); 138 dialog = contentPromptManager._dialogs[0]; 139 await dialog._dialogReady; 140 141 // should be switched back 142 ok(openedTab.selected, "Ta-dah, the other tab should now be selected again!"); 143 144 // In e10s, with the conformant promise scheduling, we have to wait for next tick 145 // to ensure that the prompt is open before removing the opened tab, because the 146 // promise callback of 'openedTabSelectedPromise' could be done at the middle of 147 // RemotePrompt.openTabPrompt() while 'DOMModalDialogClosed' event is fired. 148 // await TestUtils.waitForTick(); 149 150 await BrowserTestUtils.removeTab(openedTab); 151 });