summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/notifications/shownotification.https.window.js
blob: 4def327181560d4da76772a717660f8beb9df839 (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
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=resources/custom-data.js

"use strict";

/** @type {ServiceWorkerRegistration} */
let registration;

function reset() {
  return navigator.serviceWorker.getRegistrations().then(registrations => {
    return Promise.all(registrations.map(r => r.unregister()));
  });
}

async function registerSw() {
  await reset();
  const reg = await navigator.serviceWorker.register("noop-sw.js");
  add_completion_callback(() => reg.unregister());
  await navigator.serviceWorker.ready;
  return reg;
}

async function cleanup() {
  for (const n of await registration.getNotifications()) {
    n.close();
  }
}

promise_setup(async () => {
  await test_driver.set_permission({ name: "notifications" }, "granted");
  registration = await registerSw();
});

promise_test(async () => {
  const notifications = await registration.getNotifications();
  assert_equals(notifications.length, 0, "Should return zero notification");
}, "fetching no notifications");

promise_test(async t => {
  t.add_cleanup(cleanup);
  await registration.showNotification("");
  const notifications = await registration.getNotifications();
  assert_equals(notifications.length, 1, "Should return one notification");
  assert_equals(notifications[0].title, "", "Should return an empty title");
}, "fetching notification with an empty title");

promise_test(async t => {
  t.add_cleanup(cleanup);
  await Promise.all([
    registration.showNotification("thunder", { tag: "fire" }),
    registration.showNotification("bird", { tag: "fox" }),
    registration.showNotification("supernova", { tag: "quantum" }),
  ]);
  const notifications = await registration.getNotifications({ tag: "quantum" });
  assert_equals(
    notifications.length,
    1,
    "Should return only the matching notification"
  );
  assert_equals(notifications[0].title, "supernova", "title should match");
  assert_equals(notifications[0].tag, "quantum", "tag should match");
}, "fetching notification by tag filter");

promise_test(async t => {
  t.add_cleanup(cleanup);
  await Promise.all([
    registration.showNotification("thunder"),
    registration.showNotification("bird"),
    registration.showNotification("supernova"),
  ]);
  const notifications = await registration.getNotifications();
  assert_equals(notifications.length, 3, "Should return three notifications");
}, "fetching multiple notifications");

// https://notifications.spec.whatwg.org/#dom-serviceworkerregistration-getnotifications
// Step 5.2: Let notifications be a list of all notifications in the list of
// notifications ... whose service worker registration is this ...
promise_test(async t => {
  t.add_cleanup(cleanup);
  const another = await navigator.serviceWorker.register("noop-sw.js", { scope: "./scope" });
  await registration.showNotification("Hello");
  const notifications = await another.getNotifications();
  assert_equals(notifications.length, 0, "Should return no notification");
}, "fetching from another registration")

// https://notifications.spec.whatwg.org/#non-persistent-notification
// A non-persistent notification is a notification without an associated
// service worker registration.
promise_test(async t => {
  t.add_cleanup(cleanup);
  const nonPersistent = new Notification("Non-persistent");
  t.add_cleanup(() => nonPersistent.close());
  await registration.showNotification("Hello");
  const notifications = await registration.getNotifications();
  assert_equals(notifications.length, 1, "Should return a notification");
  assert_equals(notifications[0].title, "Hello", "Title should match");
}, "fetching only persistent notifications")

promise_test(async t => {
  t.add_cleanup(cleanup);
  await registration.showNotification("Hello", { data: fakeCustomData });
  const notifications = await registration.getNotifications();
  assert_equals(notifications.length, 1, "Should return a notification");
  assert_custom_data(notifications[0].data);
}, "fetching a notification with custom data")