browser_permissions_handling_user_input.js (2742B)
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 assertShown(task) { 12 return BrowserTestUtils.withNewTab( 13 PERMISSIONS_PAGE, 14 async function (browser) { 15 let popupshown = BrowserTestUtils.waitForEvent( 16 PopupNotifications.panel, 17 "popupshown" 18 ); 19 20 await SpecialPowers.spawn(browser, [], task); 21 22 await popupshown; 23 24 ok(true, "Notification permission prompt was shown"); 25 } 26 ); 27 } 28 29 function assertNotShown(task) { 30 return BrowserTestUtils.withNewTab( 31 PERMISSIONS_PAGE, 32 async function (browser) { 33 let popupshown = BrowserTestUtils.waitForEvent( 34 PopupNotifications.panel, 35 "popupshown" 36 ); 37 38 await SpecialPowers.spawn(browser, [], task); 39 40 let sawPrompt = await Promise.race([ 41 popupshown.then(() => true), 42 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 43 new Promise(c => setTimeout(() => c(false), 1000)), 44 ]); 45 46 is(sawPrompt, false, "Notification permission prompt was not shown"); 47 } 48 ); 49 } 50 51 // Tests that notification permissions are automatically denied without user interaction. 52 add_task(async function testNotificationPermission() { 53 Services.prefs.setBoolPref( 54 "dom.webnotifications.requireuserinteraction", 55 true 56 ); 57 58 // First test that when user interaction is required, requests 59 // with user interaction will show the permission prompt. 60 61 await assertShown(function () { 62 content.document.notifyUserGestureActivation(); 63 content.document.getElementById("desktop-notification").click(); 64 }); 65 66 await assertShown(function () { 67 content.document.notifyUserGestureActivation(); 68 content.document.getElementById("push").click(); 69 }); 70 71 // Now test that requests without user interaction will fail. 72 73 await assertNotShown(function () { 74 content.postMessage("push", "*"); 75 }); 76 77 await assertNotShown(async function () { 78 let response = await content.Notification.requestPermission(); 79 is(response, "default", "The request was automatically denied"); 80 }); 81 82 Services.prefs.setBoolPref( 83 "dom.webnotifications.requireuserinteraction", 84 false 85 ); 86 87 // Finally test that those requests will show a prompt again 88 // if the pref has been set to false. 89 90 await assertShown(function () { 91 content.postMessage("push", "*"); 92 }); 93 94 await assertShown(function () { 95 content.Notification.requestPermission(); 96 }); 97 98 Services.prefs.clearUserPref("dom.webnotifications.requireuserinteraction"); 99 });