diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /browser/base/content/test/alerts/browser_notification_do_not_disturb.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/base/content/test/alerts/browser_notification_do_not_disturb.js')
-rw-r--r-- | browser/base/content/test/alerts/browser_notification_do_not_disturb.js | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/browser/base/content/test/alerts/browser_notification_do_not_disturb.js b/browser/base/content/test/alerts/browser_notification_do_not_disturb.js new file mode 100644 index 0000000000..8fb5a8a52b --- /dev/null +++ b/browser/base/content/test/alerts/browser_notification_do_not_disturb.js @@ -0,0 +1,160 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that notifications can be silenced using nsIAlertsDoNotDisturb + * on systems where that interface and its methods are implemented for + * the nsIAlertService. + */ + +const ALERT_SERVICE = Cc["@mozilla.org/alerts-service;1"] + .getService(Ci.nsIAlertsService) + .QueryInterface(Ci.nsIAlertsDoNotDisturb); + +const PAGE = + // eslint-disable-next-line @microsoft/sdl/no-insecure-url + "http://example.org/browser/browser/base/content/test/alerts/file_dom_notifications.html"; + +// The amount of time in seconds that we will wait for a notification +// to show up before we decide that it's not coming. +const NOTIFICATION_TIMEOUT_SECS = 2000; + +add_setup(async function () { + await addNotificationPermission(PAGE); +}); + +/** + * Test that the manualDoNotDisturb attribute can prevent + * notifications from appearing. + */ +add_task(async function test_manualDoNotDisturb() { + try { + // Only run the test if the do-not-disturb + // interface has been implemented. + ALERT_SERVICE.manualDoNotDisturb; + ok(true, "Alert service implements do-not-disturb interface"); + } catch (e) { + ok( + true, + "Alert service doesn't implement do-not-disturb interface, exiting test" + ); + return; + } + + // In the event that something goes wrong during this test, make sure + // we put the attribute back to the default setting when this test file + // exits. + registerCleanupFunction(() => { + ALERT_SERVICE.manualDoNotDisturb = false; + }); + + // Make sure that do-not-disturb is not enabled before we start. + ok( + !ALERT_SERVICE.manualDoNotDisturb, + "Alert service should not be disabled when test starts" + ); + + await BrowserTestUtils.withNewTab(PAGE, async browser => { + await openNotification(browser, "showNotification2"); + + info("Notification alert showing"); + + let alertWindow = Services.wm.getMostRecentWindow("alert:alert"); + + // For now, only the XUL alert backend implements the manualDoNotDisturb + // method for nsIAlertsDoNotDisturb, so we expect there to be a XUL alert + // window. If the method gets implemented by native backends in the future, + // we'll probably want to branch here and set the manualDoNotDisturb + // attribute manually. + ok(alertWindow, "Expected a XUL alert window."); + + // We're using the XUL notification backend. This means that there's + // a menuitem for enabling manualDoNotDisturb. We exercise that + // menuitem here. + let doNotDisturbMenuItem = alertWindow.document.getElementById( + "doNotDisturbMenuItem" + ); + is(doNotDisturbMenuItem.localName, "menuitem", "menuitem found"); + + let unloadPromise = BrowserTestUtils.waitForEvent( + alertWindow, + "beforeunload" + ); + + doNotDisturbMenuItem.click(); + info("Clicked on do-not-disturb menuitem"); + await unloadPromise; + + // At this point, we should be configured to not display notifications + // to the user. + ok( + ALERT_SERVICE.manualDoNotDisturb, + "Alert service should be disabled after clicking menuitem" + ); + + // The notification should not appear, but there is no way from the + // client-side to know that it was blocked, except for waiting some time + // and realizing that the "onshow" event never fired. + await Assert.rejects( + openNotification(browser, "showNotification2", NOTIFICATION_TIMEOUT_SECS), + /timed out/, + "The notification should never display." + ); + + ALERT_SERVICE.manualDoNotDisturb = false; + }); +}); + +/** + * Test that the suppressForScreenSharing attribute can prevent + * notifications from appearing. + */ +add_task(async function test_suppressForScreenSharing() { + try { + // Only run the test if the do-not-disturb + // interface has been implemented. + ALERT_SERVICE.suppressForScreenSharing; + ok(true, "Alert service implements do-not-disturb interface"); + } catch (e) { + ok( + true, + "Alert service doesn't implement do-not-disturb interface, exiting test" + ); + return; + } + + // In the event that something goes wrong during this test, make sure + // we put the attribute back to the default setting when this test file + // exits. + registerCleanupFunction(() => { + ALERT_SERVICE.suppressForScreenSharing = false; + }); + + // Make sure that do-not-disturb is not enabled before we start. + ok( + !ALERT_SERVICE.suppressForScreenSharing, + "Alert service should not be suppressing for screen sharing when test " + + "starts" + ); + + await BrowserTestUtils.withNewTab(PAGE, async browser => { + await openNotification(browser, "showNotification2"); + + info("Notification alert showing"); + await closeNotification(browser); + ALERT_SERVICE.suppressForScreenSharing = true; + + // The notification should not appear, but there is no way from the + // client-side to know that it was blocked, except for waiting some time + // and realizing that the "onshow" event never fired. + await Assert.rejects( + openNotification(browser, "showNotification2", NOTIFICATION_TIMEOUT_SECS), + /timed out/, + "The notification should never display." + ); + }); + + ALERT_SERVICE.suppressForScreenSharing = false; +}); |