browser_notification_remove_permission.js (2299B)
1 "use strict"; 2 3 const { PermissionTestUtils } = ChromeUtils.importESModule( 4 "resource://testing-common/PermissionTestUtils.sys.mjs" 5 ); 6 7 var tab; 8 var notificationURL = 9 "https://example.org/browser/browser/base/content/test/alerts/file_dom_notifications.html"; 10 var alertWindowClosed = false; 11 var permRemoved = false; 12 13 function test() { 14 waitForExplicitFinish(); 15 16 registerCleanupFunction(function () { 17 gBrowser.removeTab(tab); 18 window.restore(); 19 }); 20 21 addNotificationPermission(notificationURL).then(function openTab() { 22 tab = BrowserTestUtils.addTab(gBrowser, notificationURL); 23 gBrowser.selectedTab = tab; 24 BrowserTestUtils.browserLoaded(tab.linkedBrowser).then(() => onLoad()); 25 }); 26 } 27 28 function onLoad() { 29 openNotification(tab.linkedBrowser, "showNotification2").then(onAlertShowing); 30 } 31 32 function onAlertShowing() { 33 info("Notification alert showing"); 34 35 let alertWindow = Services.wm.getMostRecentWindow("alert:alert"); 36 if (!alertWindow) { 37 ok(true, "Notifications don't use XUL windows on all platforms."); 38 closeNotification(tab.linkedBrowser).then(finish); 39 return; 40 } 41 ok( 42 PermissionTestUtils.testExactPermission( 43 notificationURL, 44 "desktop-notification" 45 ), 46 "Permission should exist prior to removal" 47 ); 48 let disableForOriginMenuItem = alertWindow.document.getElementById( 49 "disableForOriginMenuItem" 50 ); 51 is(disableForOriginMenuItem.localName, "menuitem", "menuitem found"); 52 Services.obs.addObserver(permObserver, "perm-changed"); 53 alertWindow.addEventListener("beforeunload", onAlertClosing); 54 disableForOriginMenuItem.click(); 55 info("Clicked on disable-for-origin menuitem"); 56 } 57 58 function permObserver(subject, topic, data) { 59 if (topic != "perm-changed") { 60 return; 61 } 62 63 let permission = subject.QueryInterface(Ci.nsIPermission); 64 is( 65 permission.type, 66 "desktop-notification", 67 "desktop-notification permission changed" 68 ); 69 is(data, "deleted", "desktop-notification permission deleted"); 70 71 Services.obs.removeObserver(permObserver, "perm-changed"); 72 permRemoved = true; 73 if (alertWindowClosed) { 74 finish(); 75 } 76 } 77 78 function onAlertClosing(event) { 79 event.target.removeEventListener("beforeunload", onAlertClosing); 80 81 alertWindowClosed = true; 82 if (permRemoved) { 83 finish(); 84 } 85 }