File: test_ext_notifications.html

package info (click to toggle)
firefox-esr 128.13.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,230,012 kB
  • sloc: cpp: 7,103,971; javascript: 6,088,450; ansic: 3,653,980; python: 1,212,330; xml: 594,604; asm: 420,652; java: 182,969; sh: 71,124; makefile: 20,747; perl: 13,449; objc: 12,399; yacc: 4,583; cs: 3,846; pascal: 2,973; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (422 lines) | stat: -rw-r--r-- 13,221 bytes parent folder | download | duplicates (6)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for notifications</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="head.js"></script>
  <script type="text/javascript" src="head_notifications.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

// A 1x1 PNG image.
// Source: https://commons.wikimedia.org/wiki/File:1x1.png (Public Domain)
let image = atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
                 "ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=");
const IMAGE_ARRAYBUFFER = Uint8Array.from(image, byte => byte.charCodeAt(0)).buffer;

add_setup(async function setup_mock_alert_service() {
  await MockAlertsService.register();
});

add_task(async function test_notification() {
  async function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    };

    let id = await browser.notifications.create(opts);

    browser.test.sendMessage("running", id);
    browser.test.notifyPass("background test passed");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  await extension.startup();
  let x = await extension.awaitMessage("running");
  is(x, "0", "got correct id from notifications.create");
  await extension.awaitFinish();
  await extension.unload();
});

add_task(async function test_notification_events() {
  async function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    };

    let createdId = "98";

    // Test an ignored listener.
    browser.notifications.onButtonClicked.addListener(function() {});

    // We cannot test onClicked listener without a mock
    // but we can attempt to add a listener.
    browser.notifications.onClicked.addListener(async function(id) {
      browser.test.assertEq(createdId, id, "onClicked has the expected ID");
      browser.test.sendMessage("notification-event", "clicked");
    });

    browser.notifications.onShown.addListener(async function listener(id) {
      browser.test.assertEq(createdId, id, "onShown has the expected ID");
      browser.test.sendMessage("notification-event", "shown");
    });

    browser.test.onMessage.addListener(async function(msg, expectedCount) {
      if (msg === "create-again") {
        let newId = await browser.notifications.create(createdId, opts);
        browser.test.assertEq(createdId, newId, "create returned the expected id.");
        browser.test.sendMessage("notification-created-twice");
      } else if (msg === "check-count") {
        let notifications = await browser.notifications.getAll();
        let ids = Object.keys(notifications);
        browser.test.assertEq(expectedCount, ids.length, `getAll() = ${ids}`);
        browser.test.sendMessage("check-count-result");
      }
    });

    // Test onClosed listener.
    browser.notifications.onClosed.addListener(function listener(id) {
      browser.test.assertEq(createdId, id, "onClosed received the expected id.");
      browser.test.sendMessage("notification-event", "closed");
    });

    await browser.notifications.create(createdId, opts);

    browser.test.sendMessage("notification-created-once");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });

  await extension.startup();

  async function waitForNotificationEvent(name) {
    info(`Waiting for notification event: ${name}`);
    is(name, await extension.awaitMessage("notification-event"),
       "Expected notification event");
  }
  async function checkNotificationCount(expectedCount) {
    extension.sendMessage("check-count", expectedCount);
    await extension.awaitMessage("check-count-result");
  }

  await extension.awaitMessage("notification-created-once");
  await waitForNotificationEvent("shown");
  await checkNotificationCount(1);

  // On most platforms, clicking the notification closes it.
  // But on macOS, the notification can repeatedly be clicked without closing.
  await MockAlertsService.clickNotificationsWithoutClose();
  await waitForNotificationEvent("clicked");
  await checkNotificationCount(1);
  await MockAlertsService.clickNotificationsWithoutClose();
  await waitForNotificationEvent("clicked");
  await checkNotificationCount(1);
  await MockAlertsService.clickNotifications();
  await waitForNotificationEvent("clicked");
  await waitForNotificationEvent("closed");
  await checkNotificationCount(0);

  extension.sendMessage("create-again");
  await extension.awaitMessage("notification-created-twice");
  await waitForNotificationEvent("shown");
  await checkNotificationCount(1);

  await MockAlertsService.closeNotifications();
  await waitForNotificationEvent("closed");
  await checkNotificationCount(0);

  await extension.unload();
});

add_task(async function test_notifications_events_in_event_page() {
  function background() {
    browser.notifications.onClicked.addListener(id => {
      browser.test.sendMessage("onClicked", id);
    });
    browser.notifications.onShown.addListener(id => {
      browser.test.sendMessage("onShown", id);
    });
    browser.notifications.onClosed.addListener(id => {
      browser.test.sendMessage("onClosed", id);
    });
  }
  async function tabScript() {
    let id = await browser.notifications.create({
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    });
    browser.test.sendMessage("created_notification", id);
  }
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      // Manifest V3 defaults to using event pages by default.
      manifest_version: 3,
      permissions: ["notifications"],
    },
    background,
    files: {
      "page.js": tabScript,
      "page.html": `<!DOCTYPE html><script src="page.js"><\/script>`,
    },
  });
  await extension.startup();
  await extension.terminateBackground();

  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    `moz-extension://${extension.uuid}/page.html`
  );
  let id = await extension.awaitMessage("created_notification");
  ok(id, "Created notification");
  let shownId = await extension.awaitMessage("onShown");
  is(shownId, id, "onShown should wake up event page");

  await extension.terminateBackground();

  await MockAlertsService.clickNotificationsWithoutClose();
  let clickedId = await extension.awaitMessage("onClicked");
  is(clickedId, id, "onClicked should wake up event page");

  await extension.terminateBackground();

  // On most platforms, clicking the notification closes it.
  // MockAlertsService.clickNotificationsWithoutClose() was used above to
  // simulate clicking without closing across all platforms, so here we can
  // simulate closing all (=the only one notification) without clicking:
  await MockAlertsService.closeNotifications();
  let closedId = await extension.awaitMessage("onClosed");
  is(closedId, id, "onClosed should wake up event page");

  // The above confirms the behavior from the API perspective; for completeness
  // also verify the internal state, like all other tests that check persistent
  // listeners:
  await assertPersistentListeners(
    extension,
    "notifications",
    ["onClicked", "onClosed", "onShown"],
    { primed: false }
  );
  await extension.terminateBackground();
  await assertPersistentListeners(
    extension,
    "notifications",
    ["onClicked", "onClosed", "onShown"],
    { primed: true }
  );

  await AppTestDelegate.removeTab(window, tab);
  await extension.unload();
});

add_task(async function test_notification_clear() {
  function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    };

    let createdId = "99";

    browser.notifications.onShown.addListener(async id => {
      browser.test.assertEq(createdId, id, "onShown received the expected id.");
      let wasCleared = await browser.notifications.clear(id);
      browser.test.assertTrue(wasCleared, "notifications.clear returned true.");
    });

    browser.notifications.onClosed.addListener(id => {
      browser.test.assertEq(createdId, id, "onClosed received the expected id.");
      browser.test.notifyPass("background test passed");
    });

    browser.notifications.create(createdId, opts);
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });

  await extension.startup();
  await extension.awaitFinish();
  await extension.unload();
});

add_task(async function test_notifications_empty_getAll() {
  async function background() {
    let notifications = await browser.notifications.getAll();

    browser.test.assertEq("object", typeof notifications, "getAll() returned an object");
    browser.test.assertEq(0, Object.keys(notifications).length, "the object has no properties");
    browser.test.notifyPass("getAll empty");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  await extension.startup();
  await extension.awaitFinish("getAll empty");
  await extension.unload();
});

add_task(async function test_notifications_populated_getAll() {
  async function background() {
    let opts = {
      type: "basic",
      iconUrl: "a.png",
      title: "Testing Notification",
      message: "Carry on",
    };

    await browser.notifications.create("p1", opts);
    await browser.notifications.create("p2", opts);
    let notifications = await browser.notifications.getAll();

    browser.test.assertEq("object", typeof notifications, "getAll() returned an object");
    browser.test.assertEq(2, Object.keys(notifications).length, "the object has 2 properties");

    for (let notificationId of ["p1", "p2"]) {
      for (let key of Object.keys(opts)) {
        browser.test.assertEq(
          opts[key],
          notifications[notificationId][key],
          `the notification has the expected value for option: ${key}`
        );
      }
    }

    browser.test.notifyPass("getAll populated");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
    files: {
      "a.png": IMAGE_ARRAYBUFFER,
    },
  });
  await extension.startup();
  await extension.awaitFinish("getAll populated");
  await extension.unload();
});

add_task(async function test_buttons_unsupported() {
  function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
      buttons: [{title: "Button title"}],
    };

    let exception = {};
    try {
      browser.notifications.create(opts);
    } catch (e) {
      exception = e;
    }

    browser.test.assertTrue(
      String(exception).includes('Property "buttons" is unsupported by Firefox'),
      "notifications.create with buttons option threw an expected exception"
    );
    browser.test.notifyPass("buttons-unsupported");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  await extension.startup();
  await extension.awaitFinish("buttons-unsupported");
  await extension.unload();
});

add_task(async function test_notifications_different_contexts() {
  async function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    };

    let id = await browser.notifications.create(opts);

    browser.runtime.onMessage.addListener(async (message, sender) => {
      await browser.tabs.remove(sender.tab.id);

      // We should be able to clear the notification after creating and
      // destroying the tab.html page.
      let wasCleared = await browser.notifications.clear(id);
      browser.test.assertTrue(wasCleared, "The notification was cleared.");
      browser.test.notifyPass("notifications");
    });

    browser.tabs.create({url: browser.runtime.getURL("/tab.html")});
  }

  async function tabScript() {
    // We should be able to see the notification created in the background page
    // in this page.
    let notifications = await browser.notifications.getAll();
    browser.test.assertEq(1, Object.keys(notifications).length,
                          "One notification found.");
    browser.runtime.sendMessage("continue-test");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
    files: {
      "tab.js": tabScript,
      "tab.html": `<!DOCTYPE html><html><head>
        <meta charset="utf-8">
        <script src="tab.js"><\/script>
      </head></html>`,
    },
  });

  await extension.startup();
  await extension.awaitFinish("notifications");
  await extension.unload();
});

add_task(async function teardown_mock_alert_service() {
  await MockAlertsService.unregister();
});

</script>

</body>
</html>