Skip to content

Commit

Permalink
Observe window events only for non-persistent notifications
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D201627

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1879934
gecko-commit: 3dd0aa903d7f26c03fb8d966e0c1d4a3bd7f684e
gecko-reviewers: asuth
  • Loading branch information
saschanaz authored and moz-wptsync-bot committed Mar 3, 2024
1 parent 0a24e44 commit 57dcf26
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
17 changes: 16 additions & 1 deletion notifications/resources/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,24 @@ async function getActiveServiceWorker(script) {
return reg;
}


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

async function trySettingPermission(perm) {
try {
await test_driver.set_permission({ name: "notifications" }, perm);
} catch {
// Not all implementations support this yet, but the permission may already be set to be able to continue
}

// Using Notification.permission instead of permissions.query() as
// some implementation without set_permission support overrides
// Notification.permission.
const permission = Notification.permission === "default" ? "prompt" : Notification.permission;
if (permission !== perm) {
throw new Error(`Should have the permission ${perm} to continue`);
}
}
8 changes: 8 additions & 0 deletions notifications/resources/shownotification-window-iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script>
async function showNotification() {
const registration = await navigator.serviceWorker.ready;
await registration.showNotification('foo');
}
</script>
37 changes: 37 additions & 0 deletions notifications/shownotification-window.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/helpers.js"></script>
<iframe id="iframe" src="resources/shownotification-window-iframe.html"></iframe>
<script>
/** @type {ServiceWorkerRegistration} */
let registration;

promise_setup(async (t) => {
await trySettingPermission("granted");
registration = await getActiveServiceWorker("noop-sw.js");
await closeAllNotifications();
});

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

if (iframe.contentDocument.readyState !== "complete") {
await new Promise(resolve => iframe.onload = resolve);
}

await iframe.contentWindow.showNotification();
let notifications = await registration.getNotifications();
assert_equals(notifications.length, 1, "Should persist the notification");

iframe.contentWindow.location.reload();
// Wait for some time for potential notification close requests to be sent
await new Promise(resolve => iframe.onload = resolve);
notifications = await registration.getNotifications();
assert_equals(notifications.length, 1, "Should keep the notification");
}, 'Refreshing window does not clear persistent notifications');
</script>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@
let registration;

promise_setup(async () => {
await trySettingPermission("prompt");
registration = await getActiveServiceWorker("noop-sw.js");
await closeAllNotifications();
});

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

try {
await test_driver.set_permission({ name: "notifications" }, "prompt");
} catch {
// Not all implementations support this yet, but it may already be "prompt" to be able to continue
}

assert_equals(Notification.permission, "default", "Should have the default permission to continue");

await promise_rejects_js(t, TypeError, registration.showNotification(""), "Should throw TypeError");
const notifications = await registration.getNotifications();
assert_equals(notifications.length, 0, "Should return zero notification");
Expand Down

0 comments on commit 57dcf26

Please sign in to comment.