browser_notification_do_not_disturb.js (5203B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests that notifications can be silenced using nsIAlertsDoNotDisturb 8 * on systems where that interface and its methods are implemented for 9 * the nsIAlertService. 10 */ 11 12 const ALERT_SERVICE = Cc["@mozilla.org/alerts-service;1"] 13 .getService(Ci.nsIAlertsService) 14 .QueryInterface(Ci.nsIAlertsDoNotDisturb); 15 16 const PAGE = 17 "https://example.org/browser/browser/base/content/test/alerts/file_dom_notifications.html"; 18 19 // The amount of time in seconds that we will wait for a notification 20 // to show up before we decide that it's not coming. 21 const NOTIFICATION_TIMEOUT_SECS = 2000; 22 23 add_setup(async function () { 24 await addNotificationPermission(PAGE); 25 }); 26 27 /** 28 * Test that the manualDoNotDisturb attribute can prevent 29 * notifications from appearing. 30 */ 31 add_task(async function test_manualDoNotDisturb() { 32 try { 33 // Only run the test if the do-not-disturb 34 // interface has been implemented. 35 ALERT_SERVICE.manualDoNotDisturb; 36 ok(true, "Alert service implements do-not-disturb interface"); 37 } catch (e) { 38 ok( 39 true, 40 "Alert service doesn't implement do-not-disturb interface, exiting test" 41 ); 42 return; 43 } 44 45 // In the event that something goes wrong during this test, make sure 46 // we put the attribute back to the default setting when this test file 47 // exits. 48 registerCleanupFunction(() => { 49 ALERT_SERVICE.manualDoNotDisturb = false; 50 }); 51 52 // Make sure that do-not-disturb is not enabled before we start. 53 ok( 54 !ALERT_SERVICE.manualDoNotDisturb, 55 "Alert service should not be disabled when test starts" 56 ); 57 58 await BrowserTestUtils.withNewTab(PAGE, async browser => { 59 await openNotification(browser, "showNotification2"); 60 61 info("Notification alert showing"); 62 63 let alertWindow = Services.wm.getMostRecentWindow("alert:alert"); 64 65 // For now, only the XUL alert backend implements the manualDoNotDisturb 66 // method for nsIAlertsDoNotDisturb, so we expect there to be a XUL alert 67 // window. If the method gets implemented by native backends in the future, 68 // we'll probably want to branch here and set the manualDoNotDisturb 69 // attribute manually. 70 ok(alertWindow, "Expected a XUL alert window."); 71 72 // We're using the XUL notification backend. This means that there's 73 // a menuitem for enabling manualDoNotDisturb. We exercise that 74 // menuitem here. 75 let doNotDisturbMenuItem = alertWindow.document.getElementById( 76 "doNotDisturbMenuItem" 77 ); 78 is(doNotDisturbMenuItem.localName, "menuitem", "menuitem found"); 79 80 let unloadPromise = BrowserTestUtils.waitForEvent( 81 alertWindow, 82 "beforeunload" 83 ); 84 85 doNotDisturbMenuItem.click(); 86 info("Clicked on do-not-disturb menuitem"); 87 await unloadPromise; 88 89 // At this point, we should be configured to not display notifications 90 // to the user. 91 ok( 92 ALERT_SERVICE.manualDoNotDisturb, 93 "Alert service should be disabled after clicking menuitem" 94 ); 95 96 // The notification should not appear, but there is no way from the 97 // client-side to know that it was blocked, except for waiting some time 98 // and realizing that the "onshow" event never fired. 99 await Assert.rejects( 100 openNotification(browser, "showNotification2", NOTIFICATION_TIMEOUT_SECS), 101 /timed out/, 102 "The notification should never display." 103 ); 104 105 ALERT_SERVICE.manualDoNotDisturb = false; 106 }); 107 }); 108 109 /** 110 * Test that the suppressForScreenSharing attribute can prevent 111 * notifications from appearing. 112 */ 113 add_task(async function test_suppressForScreenSharing() { 114 try { 115 // Only run the test if the do-not-disturb 116 // interface has been implemented. 117 ALERT_SERVICE.suppressForScreenSharing; 118 ok(true, "Alert service implements do-not-disturb interface"); 119 } catch (e) { 120 ok( 121 true, 122 "Alert service doesn't implement do-not-disturb interface, exiting test" 123 ); 124 return; 125 } 126 127 // In the event that something goes wrong during this test, make sure 128 // we put the attribute back to the default setting when this test file 129 // exits. 130 registerCleanupFunction(() => { 131 ALERT_SERVICE.suppressForScreenSharing = false; 132 }); 133 134 // Make sure that do-not-disturb is not enabled before we start. 135 ok( 136 !ALERT_SERVICE.suppressForScreenSharing, 137 "Alert service should not be suppressing for screen sharing when test " + 138 "starts" 139 ); 140 141 await BrowserTestUtils.withNewTab(PAGE, async browser => { 142 await openNotification(browser, "showNotification2"); 143 144 info("Notification alert showing"); 145 await closeNotification(browser); 146 ALERT_SERVICE.suppressForScreenSharing = true; 147 148 // The notification should not appear, but there is no way from the 149 // client-side to know that it was blocked, except for waiting some time 150 // and realizing that the "onshow" event never fired. 151 await Assert.rejects( 152 openNotification(browser, "showNotification2", NOTIFICATION_TIMEOUT_SECS), 153 /timed out/, 154 "The notification should never display." 155 ); 156 }); 157 158 ALERT_SERVICE.suppressForScreenSharing = false; 159 });