Skip to content

Commit 57bdb7f

Browse files
ltenmozmoz-wptsync-bot
authored andcommitted
Implement window.pushManager for Declarative Web Push.
Differential Revision: https://phabricator.services.mozilla.com/D328914 bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=2071974 gecko-commit: 6aaaff420e889a5e3a613db226b217914dde3a74 gecko-commit-git: a4e5ab76206becb92e6cad077cad408e24ac4960 gecko-reviewers: saschanaz, webidl, tschuster
1 parent 1d99362 commit 57bdb7f

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎notifications/resources/helpers.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ function unregisterAllServiceWorker() {
44
});
55
}
66

7-
async function prepareActiveServiceWorker(script) {
7+
async function prepareActiveServiceWorker(script, options) {
88
await unregisterAllServiceWorker();
9-
const reg = await navigator.serviceWorker.register(script);
9+
const reg = await navigator.serviceWorker.register(script, options);
1010
add_completion_callback(() => reg.unregister());
1111
await navigator.serviceWorker.ready;
1212
return reg;
1313
}
1414

15+
async function prepareActiveServiceWorkerForTest(t, script, options) {
16+
const reg = await prepareActiveServiceWorker(script, options);
17+
t.add_cleanup(() => reg.unregister());
18+
return reg;
19+
}
20+
1521
async function closeAllNotifications() {
1622
for (const n of await registration.getNotifications()) {
1723
n.close();

‎push-api/push-sw.js.headers‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Service-Worker-Allowed: /
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// META: global=window-module
2+
// META: script=/resources/testdriver.js
3+
// META: script=/resources/testdriver-vendor.js
4+
// META: script=/notifications/resources/helpers.js
5+
6+
// Tests for window.pushManager
7+
// https://w3c.github.io/push-api/#extensions-to-the-serviceworkerregistration-interface
8+
9+
import { encrypt } from "/push-api/resources/helpers.js"
10+
11+
let windowSubscription;
12+
promise_setup(async () => {
13+
await trySettingPermission("granted");
14+
windowSubscription = await pushManager.subscribe();
15+
});
16+
17+
promise_test(async t => {
18+
const registration = await prepareActiveServiceWorkerForTest(t, "push-sw.js", {scope: "/"});
19+
let swSubscription = await registration.pushManager.getSubscription();
20+
assert_object_equals(swSubscription.toJSON(), windowSubscription.toJSON(),
21+
"getSubscription() for service worker should return the window subscription.");
22+
const {promise, resolve} = Promise.withResolvers();
23+
navigator.serviceWorker.addEventListener("message", e => {
24+
resolve(e.data.data.text);
25+
}, {once: true});
26+
const result = await encrypt(
27+
new TextEncoder().encode("Hello"),
28+
windowSubscription.getKey("p256dh"),
29+
windowSubscription.getKey("auth")
30+
);
31+
await fetch(windowSubscription.endpoint, {
32+
method: "post",
33+
...result
34+
});
35+
const pushText = await promise;
36+
assert_equals(pushText, "Hello",
37+
"Service worker should get push event when push message is sent through window.pushManager.");
38+
}, "Using window.pushManager when there is a service worker with scope /");
39+
40+
promise_test(async t => {
41+
const registration = await prepareActiveServiceWorkerForTest(t, "push-sw.js");
42+
assert_equals(await registration.pushManager.getSubscription(), null,
43+
"getSubscription() should return null if only window has a push subscription.");
44+
const swSubscription = await registration.pushManager.subscribe();
45+
// https://w3c.github.io/push-api/#push-subscription
46+
// "A push endpoint MUST uniquely identify the push subscription."
47+
assert_not_equals(swSubscription.endpoint, windowSubscription.endpoint,
48+
"Service worker subscription should be different from window subscription.");
49+
}, "Using window.pushManager when there is a service worker with scope NOT equal to /");

0 commit comments

Comments
 (0)