summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_panelUINotifications_multiWindow.js
blob: fd757638571e8b93a9111105969a95ed42e1e60c (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict";

const { AppMenuNotifications } = ChromeUtils.importESModule(
  "resource://gre/modules/AppMenuNotifications.sys.mjs"
);

/**
 * Tests that when we try to show a notification in a background window, it
 * does not display until the window comes back into the foreground. However,
 * it should display a badge.
 */
add_task(async function testDoesNotShowDoorhangerForBackgroundWindow() {
  let options = {
    gBrowser: window.gBrowser,
    url: "about:blank",
  };

  await BrowserTestUtils.withNewTab(options, async function (browser) {
    let win = await BrowserTestUtils.openNewBrowserWindow();
    await SimpleTest.promiseFocus(win);
    let mainActionCalled = false;
    let mainAction = {
      callback: () => {
        mainActionCalled = true;
      },
    };
    AppMenuNotifications.showNotification("update-manual", mainAction);
    is(
      PanelUI.notificationPanel.state,
      "closed",
      "The background window's doorhanger is closed."
    );
    is(
      PanelUI.menuButton.hasAttribute("badge-status"),
      true,
      "The background window has a badge."
    );

    let popupShown = BrowserTestUtils.waitForEvent(
      PanelUI.notificationPanel,
      "popupshown"
    );

    await BrowserTestUtils.closeWindow(win);
    await SimpleTest.promiseFocus(window);
    await popupShown;

    let notifications = [...PanelUI.notificationPanel.children].filter(
      n => !n.hidden
    );
    is(
      notifications.length,
      1,
      "PanelUI doorhanger is only displaying one notification."
    );
    let doorhanger = notifications[0];
    is(
      doorhanger.id,
      "appMenu-update-manual-notification",
      "PanelUI is displaying the update-manual notification."
    );

    let button = doorhanger.button;

    Assert.equal(
      PanelUI.notificationPanel.state,
      "open",
      "Expect panel state to be open when clicking panel buttons"
    );
    button.click();

    ok(mainActionCalled, "Main action callback was called");
    is(
      PanelUI.notificationPanel.state,
      "closed",
      "update-manual doorhanger is closed."
    );
    is(
      PanelUI.menuButton.hasAttribute("badge-status"),
      false,
      "Should not have a badge status"
    );
  });
});

/**
 * Tests that when we try to show a notification in a background window and in
 * a foreground window, if the foreground window's main action is called, the
 * background window's doorhanger will be removed.
 */
add_task(
  async function testBackgroundWindowNotificationsAreRemovedByForeground() {
    let options = {
      gBrowser: window.gBrowser,
      url: "about:blank",
    };

    await BrowserTestUtils.withNewTab(options, async function (browser) {
      let win = await BrowserTestUtils.openNewBrowserWindow();
      await SimpleTest.promiseFocus(win);
      AppMenuNotifications.showNotification("update-manual", { callback() {} });
      let notifications = [...win.PanelUI.notificationPanel.children].filter(
        n => !n.hidden
      );
      is(
        notifications.length,
        1,
        "PanelUI doorhanger is only displaying one notification."
      );
      let doorhanger = notifications[0];
      doorhanger.button.click();

      await BrowserTestUtils.closeWindow(win);
      await SimpleTest.promiseFocus(window);

      is(
        PanelUI.notificationPanel.state,
        "closed",
        "update-manual doorhanger is closed."
      );
      is(
        PanelUI.menuButton.hasAttribute("badge-status"),
        false,
        "Should not have a badge status"
      );
    });
  }
);

/**
 * Tests that when we try to show a notification in a background window and in
 * a foreground window, if the foreground window's doorhanger is dismissed,
 * the background window's doorhanger will also be dismissed once the window
 * regains focus.
 */
add_task(
  async function testBackgroundWindowNotificationsAreDismissedByForeground() {
    let options = {
      gBrowser: window.gBrowser,
      url: "about:blank",
    };

    await BrowserTestUtils.withNewTab(options, async function (browser) {
      let win = await BrowserTestUtils.openNewBrowserWindow();
      await SimpleTest.promiseFocus(win);
      AppMenuNotifications.showNotification("update-manual", { callback() {} });
      let notifications = [...win.PanelUI.notificationPanel.children].filter(
        n => !n.hidden
      );
      is(
        notifications.length,
        1,
        "PanelUI doorhanger is only displaying one notification."
      );
      let doorhanger = notifications[0];
      let button = doorhanger.secondaryButton;
      button.click();

      await BrowserTestUtils.closeWindow(win);
      await SimpleTest.promiseFocus(window);

      is(
        PanelUI.notificationPanel.state,
        "closed",
        "The background window's doorhanger is closed."
      );
      is(
        PanelUI.menuButton.hasAttribute("badge-status"),
        true,
        "The dismissed notification should still have a badge status"
      );

      AppMenuNotifications.removeNotification(/.*/);
    });
  }
);

/**
 * Tests that when we open a new window while a notification is showing, the
 * notification also shows on the new window.
 */
add_task(async function testOpenWindowAfterShowingNotification() {
  AppMenuNotifications.showNotification("update-manual", { callback() {} });

  let win = await BrowserTestUtils.openNewBrowserWindow();
  await SimpleTest.promiseFocus(win);
  let notifications = [...win.PanelUI.notificationPanel.children].filter(
    n => !n.hidden
  );
  is(
    notifications.length,
    1,
    "PanelUI doorhanger is only displaying one notification."
  );
  let doorhanger = notifications[0];
  let button = doorhanger.secondaryButton;
  button.click();

  await BrowserTestUtils.closeWindow(win);
  await SimpleTest.promiseFocus(window);

  is(
    PanelUI.notificationPanel.state,
    "closed",
    "The background window's doorhanger is closed."
  );
  is(
    PanelUI.menuButton.hasAttribute("badge-status"),
    true,
    "The dismissed notification should still have a badge status"
  );

  AppMenuNotifications.removeNotification(/.*/);
});