browser_permissions_postPrompt.js (2452B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const ORIGIN = "https://example.com"; 7 const PERMISSIONS_PAGE = 8 getRootDirectory(gTestPath).replace("chrome://mochitests/content", ORIGIN) + 9 "permissions.html"; 10 11 function testPostPrompt(task) { 12 let uri = Services.io.newURI(PERMISSIONS_PAGE); 13 return BrowserTestUtils.withNewTab( 14 PERMISSIONS_PAGE, 15 async function (browser) { 16 let icon = document.getElementById("web-notifications-notification-icon"); 17 ok( 18 !BrowserTestUtils.isVisible(icon), 19 "notifications icon is not visible at first" 20 ); 21 22 await SpecialPowers.spawn(browser, [], task); 23 24 await TestUtils.waitForCondition( 25 () => BrowserTestUtils.isVisible(icon), 26 "notifications icon is visible" 27 ); 28 ok( 29 !PopupNotifications.panel.hasAttribute("panelopen"), 30 "only the icon is showing, the panel is not open" 31 ); 32 33 let popupshown = BrowserTestUtils.waitForEvent( 34 PopupNotifications.panel, 35 "popupshown" 36 ); 37 icon.click(); 38 await popupshown; 39 40 ok(true, "Notification permission prompt was shown"); 41 42 let notification = PopupNotifications.panel.firstElementChild; 43 EventUtils.synthesizeMouseAtCenter(notification.button, {}); 44 45 is( 46 PermissionTestUtils.testPermission(uri, "desktop-notification"), 47 Ci.nsIPermissionManager.ALLOW_ACTION, 48 "User can override the default deny by using the prompt" 49 ); 50 51 PermissionTestUtils.remove(uri, "desktop-notification"); 52 } 53 ); 54 } 55 56 add_task(async function testNotificationPermission() { 57 Services.prefs.setBoolPref( 58 "dom.webnotifications.requireuserinteraction", 59 true 60 ); 61 Services.prefs.setBoolPref( 62 "permissions.desktop-notification.postPrompt.enabled", 63 true 64 ); 65 66 // Now test that requests without user interaction will post-prompt when the 67 // user interaction requirement is set. 68 69 await testPostPrompt(function () { 70 content.postMessage("push", "*"); 71 }); 72 73 await testPostPrompt(async function () { 74 let response = await content.Notification.requestPermission(); 75 is(response, "default", "The request was automatically denied"); 76 }); 77 78 Services.prefs.clearUserPref("dom.webnotifications.requireuserinteraction"); 79 Services.prefs.clearUserPref( 80 "permissions.desktop-notification.postPrompt.enabled" 81 ); 82 });