browser_popup_blocker.js (6750B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const baseURL = getRootDirectory(gTestPath).replace( 6 "chrome://mochitests/content", 7 "https://example.com" 8 ); 9 10 function clearAllPermissionsByPrefix(aPrefix) { 11 for (let perm of Services.perms.all) { 12 if (perm.type.startsWith(aPrefix)) { 13 Services.perms.removePermission(perm); 14 } 15 } 16 } 17 18 add_setup(async function () { 19 // Enable the popup blocker. 20 await SpecialPowers.pushPrefEnv({ 21 set: [["dom.disable_open_during_load", true]], 22 }); 23 }); 24 25 // Tests that we show a special message when popup blocking exceeds 26 // a certain maximum of popups per page. 27 add_task(async function test_maximum_reported_blocks() { 28 Services.prefs.setIntPref("privacy.popups.maxReported", 5); 29 30 // Open the test page. 31 let tab = await BrowserTestUtils.openNewForegroundTab( 32 gBrowser, 33 baseURL + "popup_blocker_10_popups.html" 34 ); 35 36 // Wait for the popup-blocked notification. 37 let notification = await TestUtils.waitForCondition(() => { 38 let tempNotif = gBrowser 39 .getNotificationBox() 40 .getNotificationWithValue("popup-blocked"); 41 42 // The textContent in the notification object is contains 43 // blank spaces as placeholder if there is no value in it 44 if (tempNotif?.messageText.textContent.trim().length) { 45 return tempNotif; 46 } 47 return false; 48 }); 49 50 // Slightly hacky way to ensure we show the correct message in this case. 51 ok( 52 notification.messageText.textContent.includes("more than"), 53 "Notification label has 'more than'" 54 ); 55 ok( 56 notification.messageText.textContent.includes("5"), 57 "Notification label shows the maximum number of popups" 58 ); 59 60 gBrowser.removeTab(tab); 61 62 Services.prefs.clearUserPref("privacy.popups.maxReported"); 63 }); 64 65 add_task(async function test_opening_blocked_popups() { 66 // Open the test page. 67 let tab = await BrowserTestUtils.openNewForegroundTab( 68 gBrowser, 69 baseURL + "popup_blocker.html" 70 ); 71 72 await testPopupBlockingToolbar(tab); 73 }); 74 75 add_task(async function test_opening_blocked_popups_privateWindow() { 76 let win = await BrowserTestUtils.openNewBrowserWindow({ 77 private: true, 78 }); 79 // Open the test page. 80 let tab = await BrowserTestUtils.openNewForegroundTab( 81 win.gBrowser, 82 baseURL + "popup_blocker.html" 83 ); 84 await testPopupBlockingToolbar(tab); 85 await BrowserTestUtils.closeWindow(win); 86 }); 87 88 // This is a test for Bug 1988311. 89 // Make sure that everything also functions correctly on special pages, 90 // such as "about:privatebrowsing". 91 add_task(async function test_opening_blocked_popups_about_privatebrowsing() { 92 const tab = await BrowserTestUtils.openNewForegroundTab( 93 gBrowser, 94 "about:privatebrowsing" 95 ); 96 97 const browser = tab.linkedBrowser; 98 const uri = Services.io.newURI( 99 "javascript:" + 100 `window.open("${baseURL}" + "popup_blocker_a.html");` + 101 `window.open("${baseURL}" + "popup_blocker_b.html");` 102 ); 103 const triggeringPrincipal = 104 Services.scriptSecurityManager.getSystemPrincipal(); 105 106 browser.loadURI(uri, { triggeringPrincipal }); 107 await testPopupBlockingToolbar(tab); 108 }); 109 110 // Bug 2006600. 111 // When a notification has been dismissed by a user, it should not appear 112 // again when switching to a different tab and back. 113 add_task(async function test_dismissed_notification_switch_tabs() { 114 // Open the test page. 115 const tab = await BrowserTestUtils.openNewForegroundTab( 116 gBrowser, 117 baseURL + "popup_blocker.html" 118 ); 119 120 // Wait for the notification. 121 let notification; 122 await TestUtils.waitForCondition( 123 () => 124 (notification = gBrowser 125 .getNotificationBox() 126 .getNotificationWithValue("popup-blocked")) 127 ); 128 129 // Click dismiss button. 130 const mozButton = notification.shadowRoot.querySelector("moz-button.close"); 131 mozButton.click(); 132 133 // Open a new (foreground) tab and switch back. 134 const differentTab = await BrowserTestUtils.openNewForegroundTab( 135 gBrowser, 136 "about:blank" 137 ); 138 await BrowserTestUtils.switchTab(gBrowser, tab); 139 140 // Make sure no notification appears. 141 try { 142 await TestUtils.waitForCondition( 143 () => 144 (notification = gBrowser 145 .getNotificationBox() 146 .getNotificationWithValue("popup-blocked")), 147 null, 148 50, 149 10 150 ); 151 } catch (e) { 152 notification = null; 153 } 154 ok(!notification, "Notification should not reappear"); 155 156 gBrowser.removeTab(tab); 157 gBrowser.removeTab(differentTab); 158 }); 159 160 async function testPopupBlockingToolbar(tab) { 161 let win = tab.ownerGlobal; 162 // Wait for the popup-blocked notification. 163 let notification; 164 await TestUtils.waitForCondition( 165 () => 166 (notification = win.gBrowser 167 .getNotificationBox() 168 .getNotificationWithValue("popup-blocked")) 169 ); 170 171 // Show the menu. 172 let popupShown = BrowserTestUtils.waitForEvent(win, "popupshown"); 173 let popupFilled = waitForBlockedPopups(2, { 174 doc: win.document, 175 }); 176 EventUtils.synthesizeMouseAtCenter( 177 notification.buttonContainer.querySelector("button"), 178 {}, 179 win 180 ); 181 let popup_event = await popupShown; 182 let menu = popup_event.target; 183 is(menu.id, "blockedPopupOptions", "Blocked popup menu shown"); 184 185 await popupFilled; 186 187 // Pressing "allow" should open all blocked popups. 188 let popupTabs = []; 189 function onTabOpen(event) { 190 popupTabs.push(event.target); 191 } 192 win.gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen); 193 194 // Press the button. 195 let allow = win.document.getElementById("blockedPopupAllowSite"); 196 allow.doCommand(); 197 await TestUtils.waitForCondition( 198 () => 199 popupTabs.length == 2 && 200 popupTabs.every( 201 aTab => aTab.linkedBrowser.currentURI.spec != "about:blank" 202 ) 203 ); 204 205 win.gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen); 206 207 ok( 208 popupTabs[0].linkedBrowser.currentURI.spec.endsWith("popup_blocker_a.html"), 209 "Popup a" 210 ); 211 ok( 212 popupTabs[1].linkedBrowser.currentURI.spec.endsWith("popup_blocker_b.html"), 213 "Popup b" 214 ); 215 216 let popupPerms = Services.perms.getAllByTypeSince("popup", 0); 217 is(popupPerms.length, 1, "One popup permission added"); 218 let popupPerm = popupPerms[0]; 219 let expectedExpireType = PrivateBrowsingUtils.isWindowPrivate(win) 220 ? Services.perms.EXPIRE_SESSION 221 : Services.perms.EXPIRE_NEVER; 222 is( 223 popupPerm.expireType, 224 expectedExpireType, 225 "Check expireType is appropriate for the window" 226 ); 227 228 // Clean up. 229 win.gBrowser.removeTab(tab); 230 for (let popup of popupTabs) { 231 win.gBrowser.removeTab(popup); 232 } 233 clearAllPermissionsByPrefix("popup"); 234 // Ensure the menu closes. 235 menu.hidePopup(); 236 }