|
| 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