Skip to content

Commit

Permalink
Part 2: Expand instance test for service worker
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D209008

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1894039
gecko-commit: c59a28c89897d5cdf18d23a24f8720f06e71b9cd
gecko-reviewers: asuth
  • Loading branch information
saschanaz authored and moz-wptsync-bot committed May 15, 2024
1 parent 74aad7d commit d4bff26
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 71 deletions.
14 changes: 5 additions & 9 deletions notifications/getnotifications-sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
importScripts("/resources/testharness.js");
importScripts("resources/helpers.js");

async function cleanup() {
for (const n of await registration.getNotifications()) {
Expand Down Expand Up @@ -29,14 +30,9 @@ async function postAll(data) {
}
}

async function untilActivate() {
if (registration.active) {
return;
}
return new Promise(resolve => {
addEventListener("activate", resolve, { once: true });
});
}
promise_setup(async () => {
await untilActivate();
});

promise_test(async t => {
await new Promise((resolve, reject) => {
Expand All @@ -45,7 +41,7 @@ promise_test(async t => {
resolve();
}
});
untilActivate().then(() => postAll("notification-create")).catch(reject);
postAll("notification-create").catch(reject);
});
await test_notification(t, "Created from window");
}, "Get notification created from window");
Expand Down
40 changes: 40 additions & 0 deletions notifications/instance-checks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const notification_args = [
"Radio check",
{
dir: "ltr",
lang: "aa",
body: "This is a radio check.",
tag: "radio_check999",
icon: `${location.origin}/icon.png`,
data: fakeCustomData,
}
];

// promise_tests because we need to wait for promise_setup
function notification_instance_test(createFn, testTitle) {
let n;
promise_test(async t => {
n = await createFn(t);
}, `${testTitle}: Setup`);
promise_test(async () => {
assert_equals("Radio check", n.title)
}, `${testTitle}: Attribute exists with expected value: title`)
promise_test(async () => {
assert_equals("ltr", n.dir)
}, `${testTitle}: Attribute exists with expected value: dir`)
promise_test(async () => {
assert_equals("aa", n.lang)
}, `${testTitle}: Attribute exists with expected value: lang`)
promise_test(async () => {
assert_equals("This is a radio check.", n.body)
}, `${testTitle}: Attribute exists with expected value: body`)
promise_test(async () => {
assert_equals("radio_check999", n.tag)
}, `${testTitle}: Attribute exists with expected value: tag`)
promise_test(async () => {
assert_equals(`${location.origin}/icon.png`, n.icon)
}, `${testTitle}: Attribute exists with expected value: icon`)
promise_test(async () => {
assert_custom_data(n.data);
}, `${testTitle}: Attribute exists with expected value: data`)
}
34 changes: 34 additions & 0 deletions notifications/instance-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
importScripts("/resources/testharness.js");
importScripts("resources/helpers.js");
importScripts("resources/custom-data.js");
importScripts("instance-checks.js");

promise_setup(async () => {
await untilActivate();
});

notification_instance_test(async t => {
t.add_cleanup(closeAllNotifications);

await registration.showNotification(...notification_args);

let notifications = await registration.getNotifications();
assert_equals(notifications.length, 1, "The list should include one notification");

return notifications[0];
}, "getNotifications()");

// Doing this separately because this times out on Blink and GeckoView
notification_instance_test(async t => {
t.add_cleanup(closeAllNotifications);

await registration.showNotification(...notification_args);

let notifications = await registration.getNotifications();
assert_equals(notifications.length, 1, "The list should include one notification");

notifications[0].close();
const ev = await new Promise(resolve => addEventListener("notificationclose", resolve, { once: true }));

return ev.notification;
}, "notificationclose");
60 changes: 0 additions & 60 deletions notifications/instance.https.html

This file was deleted.

18 changes: 18 additions & 0 deletions notifications/instance.https.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/helpers.js
// META: script=resources/custom-data.js
// META: script=instance-checks.js

promise_setup(async () => {
await trySettingPermission("granted");
});

notification_instance_test(() => {
const n = new Notification(...notification_args);
n.close();
return n;
}, "new Notification()");

service_worker_test("instance-sw.js", "Service worker test setup");
3 changes: 1 addition & 2 deletions notifications/resources/custom-data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var fakeCustomData = (function() {
var buffer = new ArrayBuffer(2);
new DataView(buffer).setInt16(0, 42, true);
var canvas = document.createElement("canvas");
canvas.width = canvas.height = 100;
var canvas = new OffscreenCanvas(100, 100);
var context = canvas.getContext("2d");

var map = new Map();
Expand Down
10 changes: 10 additions & 0 deletions notifications/resources/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ async function trySettingPermission(perm) {
throw new Error(`Should have the permission ${perm} to continue`);
}
}

// Use this in service workers where activation is required e.g. when testing showNotification()
async function untilActivate() {
if (registration.active) {
return;
}
return new Promise(resolve => {
addEventListener("activate", resolve, { once: true });
});
}

0 comments on commit d4bff26

Please sign in to comment.