Skip to content

Truly independent web browser

Tests: Import the WPT notification tag test

sideshowbarkerc8bacc074000
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 08d7f5479a5..08562002e1d 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -12,6 +12,7 @@ Text/input/wpt-import/geolocation/
Text/input/wpt-import/permissions/
; A stored notifications grant needs a site origin.
Text/input/notifications/
+Text/input/wpt-import/notifications/
; Their harness scripts are served from the WPT root, which a file: page cannot reach.
Crash/wpt-import/permissions/
; Render-blocking tests need delayed HTTP responses and module imports.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/notifications/tag.https.txt b/Tests/LibWeb/Text/expected/wpt-import/notifications/tag.https.txt
new file mode 100644
index 00000000000..bb7ec4beba0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/notifications/tag.https.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Opening two notifications with the same tag should close the first one
+Pass Opening two notifications with the same tag should fire close event before show event
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/notifications/resources/helpers.js b/Tests/LibWeb/Text/input/wpt-import/notifications/resources/helpers.js
new file mode 100644
index 00000000000..35b84412c44
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/notifications/resources/helpers.js
@@ -0,0 +1,51 @@
+function unregisterAllServiceWorker() {
+ return navigator.serviceWorker.getRegistrations().then(registrations => {
+ return Promise.all(registrations.map(r => r.unregister()));
+ });
+}
+
+async function prepareActiveServiceWorker(script, options) {
+ await unregisterAllServiceWorker();
+ const reg = await navigator.serviceWorker.register(script, options);
+ add_completion_callback(() => reg.unregister());
+ await navigator.serviceWorker.ready;
+ return reg;
+}
+
+async function prepareActiveServiceWorkerForTest(t, script, options) {
+ const reg = await prepareActiveServiceWorker(script, options);
+ t.add_cleanup(() => reg.unregister());
+ 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 but found ${permission}`);
+ }
+}
+
+// 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 });
+ });
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/notifications/tag.https.html b/Tests/LibWeb/Text/input/wpt-import/notifications/tag.https.html
new file mode 100644
index 00000000000..6d1ad2f072c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/notifications/tag.https.html
@@ -0,0 +1,67 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Notification.tag (two tags with same or different value)</title>
+<link rel="author" title="Intel" href="http://www.intel.com/">
+<link rel="author" title="Xin Liu" href="mailto:[email protected]">
+<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>
+<script>
+function promiseEvent(target, eventName, syncListener) {
+ return new Promise(resolve => {
+ target.addEventListener(eventName, ev => {
+ syncListener?.();
+ resolve(ev);
+ }, { once: true });
+ });
+}
+
+promise_setup(async () => {
+ await trySettingPermission("granted");
+});
+
+promise_test(async t => {
+ const tom1 = new Notification("New Email to tom", {
+ tag: "Tom"
+ });
+ t.add_cleanup(() => tom1.close());
+
+ let closed = false;
+ const promiseCloseEvent = promiseEvent(tom1, "close", () => closed = true);
+ await promiseEvent(tom1, "show");
+
+ const rose = new Notification("New Email to Rose", {
+ tag: "Rose"
+ });
+ t.add_cleanup(() => rose.close());
+ await promiseEvent(rose, "show");
+ assert_false(closed, "Different tag should not close the first notification");
+
+ const tom2 = new Notification("New Email to tom", {
+ tag: "Tom"
+ });
+ t.add_cleanup(() => tom2.close());
+
+ await promiseCloseEvent; // This should not timeout
+}, "Opening two notifications with the same tag should close the first one");
+
+promise_test(async t => {
+ const events = [];
+
+ const tom1 = new Notification("New Email to tom", {
+ tag: "Tom"
+ });
+ t.add_cleanup(() => tom1.close());
+ tom1.onclose = () => events.push("tomclose");
+
+ const tom2 = new Notification("New Email to tom", {
+ tag: "Tom"
+ });
+ t.add_cleanup(() => tom2.close());
+ await promiseEvent(tom2, "show", () => events.push("tomshow"));
+
+ assert_array_equals(events, ["tomclose", "tomshow"]);
+}, "Opening two notifications with the same tag should fire close event before show event");
+</script>