summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/alerts/browser_notification_do_not_disturb.js
blob: 8fb5a8a52b72a144b898a304e9ca8c784d917537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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;
});