declarative-glean.https.any.js (2890B)
1 // META: global=window-module 2 // META: script=/_mozilla/resources/GleanTest.js 3 // META: script=/resources/testdriver.js 4 // META: script=/resources/testdriver-vendor.js 5 // META: script=/notifications/resources/helpers.js 6 7 import { encrypt } from "/push-api/resources/helpers.js" 8 9 let registration; 10 let subscription; 11 12 promise_setup(async (t) => { 13 await trySettingPermission("granted"); 14 registration = await prepareActiveServiceWorker("push-sw.js"); 15 subscription = await registration.pushManager.subscribe(); 16 }); 17 18 async function pushAndReceiveMessage(t, message) { 19 await GleanTest.testResetFOG(); 20 21 const result = await encrypt( 22 new TextEncoder().encode(message), 23 subscription.getKey("p256dh"), 24 subscription.getKey("auth") 25 ); 26 27 const { promise, resolve } = Promise.withResolvers(); 28 const controller = new AbortController(); 29 navigator.serviceWorker.addEventListener("message", ev => { 30 if (ev.data.data !== message) { 31 return; 32 } 33 controller.abort(); 34 resolve(); 35 }, { signal: controller.signal }); 36 37 await fetch(subscription.endpoint, { 38 method: "post", 39 ...result 40 }); 41 42 await promise; 43 await GleanTest.flush(); 44 } 45 46 promise_test(async (t) => { 47 await pushAndReceiveMessage(t, "hello"); 48 49 const notify = await GleanTest.webPush.apiNotify.testGetValue(); 50 const dwp = await GleanTest.webPush.declarative.testGetValue(); 51 const mutable = await GleanTest.webPush.declarativeMutable.testGetValue(); 52 53 assert_equals(notify, 1, "notify should always increment for valid push messages"); 54 assert_equals(dwp, null, "declarative should not increment on non-DWP"); 55 assert_equals(mutable, null, "declarativeMutable should not increment on non-DWP"); 56 }, "Non-declarative web push"); 57 58 promise_test(async (t) => { 59 await pushAndReceiveMessage(t, `{ "web_push": 8030 }`); 60 61 const notify = await GleanTest.webPush.apiNotify.testGetValue(); 62 const dwp = await GleanTest.webPush.declarative.testGetValue(); 63 const mutable = await GleanTest.webPush.declarativeMutable.testGetValue(); 64 65 assert_equals(notify, 1, "notify should always increment for valid push messages"); 66 assert_equals(dwp, 1, "declarative should increment on DWP"); 67 assert_equals(mutable, null, "declarativeMutable should increment on DWP"); 68 }, "Declarative web push"); 69 70 promise_test(async (t) => { 71 await pushAndReceiveMessage(t, `{ "web_push": 8030, "mutable": true }`); 72 73 const notify = await GleanTest.webPush.apiNotify.testGetValue(); 74 const dwp = await GleanTest.webPush.declarative.testGetValue(); 75 const mutable = await GleanTest.webPush.declarativeMutable.testGetValue(); 76 77 assert_equals(notify, 1, "notify should always increment for valid push messages"); 78 assert_equals(dwp, 1, "declarative should increment on mutable DWP"); 79 assert_equals(mutable, 1, "declarativeMutable should increment on mutable DWP"); 80 }, "Declarative web push with mutable: true");