Skip to content

Truly independent web browser

Tests: Cover notification tag replacement and the per-origin cap

sideshowbarker05f33c71e70a
diff --git a/Tests/LibWeb/Text/expected/notifications/cap-replaces-oldest.txt b/Tests/LibWeb/Text/expected/notifications/cap-replaces-oldest.txt
new file mode 100644
index 00000000000..1bd25b28525
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/cap-replaces-oldest.txt
@@ -0,0 +1,5 @@
+shown: 8
+show events: 100
+close events: 92
+first closed: n0
+oldest still shown: n92
diff --git a/Tests/LibWeb/Text/expected/notifications/renotify-flag-reaches-backend.txt b/Tests/LibWeb/Text/expected/notifications/renotify-flag-reaches-backend.txt
new file mode 100644
index 00000000000..a417f68a0da
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/renotify-flag-reaches-backend.txt
@@ -0,0 +1,2 @@
+renotify on replacement: true
+renotify without the option: false
diff --git a/Tests/LibWeb/Text/expected/notifications/tag-replace-then-close-old.txt b/Tests/LibWeb/Text/expected/notifications/tag-replace-then-close-old.txt
new file mode 100644
index 00000000000..155577a3785
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/tag-replace-then-close-old.txt
@@ -0,0 +1 @@
+close events on a: 1
diff --git a/Tests/LibWeb/Text/expected/notifications/tag-replaces-and-closes-old.txt b/Tests/LibWeb/Text/expected/notifications/tag-replaces-and-closes-old.txt
new file mode 100644
index 00000000000..81a8e16bec5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/notifications/tag-replaces-and-closes-old.txt
@@ -0,0 +1,3 @@
+a closed, shown: 1
+shown title: B
+replaces a: true
diff --git a/Tests/LibWeb/Text/input/notifications/cap-replaces-oldest.html b/Tests/LibWeb/Text/input/notifications/cap-replaces-oldest.html
new file mode 100644
index 00000000000..8bf956e619b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/cap-replaces-oldest.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // An origin keeps at most eight notifications at a time; each one past that closes the oldest.
+ asyncTest(async done => {
+ await internals.setPermission("notifications", "granted");
+ const total = 100;
+ let shows = 0;
+ let closes = 0;
+ let firstClosed = null;
+ const notifications = [];
+ const allShown = new Promise(resolve => {
+ for (let i = 0; i < total; ++i) {
+ const notification = new Notification(`n${i}`, { tag: `t${i}` });
+ notification.onshow = () => { if (++shows === total) resolve(); };
+ notification.onclose = () => { firstClosed ??= i; ++closes; };
+ notifications.push(notification);
+ }
+ });
+ await allShown;
+ await new Promise(resolve => setTimeout(resolve, 100));
+ const state = JSON.parse(internals.notificationsStateForTesting());
+ println(`shown: ${state.shown.length}`);
+ println(`show events: ${shows}`);
+ println(`close events: ${closes}`);
+ println(`first closed: n${firstClosed}`);
+ println(`oldest still shown: ${state.shown[0].title}`);
+ for (const notification of notifications)
+ notification.close();
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/notifications/renotify-flag-reaches-backend.html b/Tests/LibWeb/Text/input/notifications/renotify-flag-reaches-backend.html
new file mode 100644
index 00000000000..0741a0fae0e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/renotify-flag-reaches-backend.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ await internals.setPermission("notifications", "granted");
+ const shown = n => new Promise(resolve => (n.onshow = resolve));
+ const quiet = new Notification("first", { tag: "x" });
+ await shown(quiet);
+ const loud = new Notification("second", { tag: "x", renotify: true });
+ await shown(loud);
+ let state = JSON.parse(internals.notificationsStateForTesting());
+ println(`renotify on replacement: ${state.shown[0].renotify}`);
+ const quietAgain = new Notification("third", { tag: "x" });
+ await shown(quietAgain);
+ state = JSON.parse(internals.notificationsStateForTesting());
+ println(`renotify without the option: ${state.shown[0].renotify}`);
+ quietAgain.close();
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/notifications/tag-replace-then-close-old.html b/Tests/LibWeb/Text/input/notifications/tag-replace-then-close-old.html
new file mode 100644
index 00000000000..d325251b3ea
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/tag-replace-then-close-old.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // close() on a notification that a same-tag one is replacing in the same task fires close exactly once.
+ asyncTest(async done => {
+ await internals.setPermission("notifications", "granted");
+ const a = new Notification("A", { tag: "x" });
+ await new Promise(resolve => (a.onshow = resolve));
+ let closes = 0;
+ a.onclose = () => closes++;
+ const b = new Notification("B", { tag: "x" });
+ a.close();
+ await new Promise(resolve => (b.onshow = resolve));
+ await new Promise(resolve => setTimeout(resolve, 100));
+ println(`close events on a: ${closes}`);
+ b.close();
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/notifications/tag-replaces-and-closes-old.html b/Tests/LibWeb/Text/input/notifications/tag-replaces-and-closes-old.html
new file mode 100644
index 00000000000..a88a4fc8706
--- /dev/null
+++ b/Tests/LibWeb/Text/input/notifications/tag-replaces-and-closes-old.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A notification with the same origin and tag takes the earlier one's place: the earlier one gets close.
+ asyncTest(async done => {
+ await internals.setPermission("notifications", "granted");
+ const shown = n => new Promise(resolve => (n.onshow = resolve));
+ const a = new Notification("A", { tag: "x" });
+ await shown(a);
+ const aId = JSON.parse(internals.notificationsStateForTesting()).shown[0].id;
+ const aClosed = new Promise(resolve => (a.onclose = resolve));
+ const b = new Notification("B", { tag: "x" });
+ await shown(b);
+ await aClosed;
+ const state = JSON.parse(internals.notificationsStateForTesting());
+ println(`a closed, shown: ${state.shown.length}`);
+ println(`shown title: ${state.shown[0].title}`);
+ println(`replaces a: ${state.shown[0].replaces === aId}`);
+ b.close();
+ done();
+ });
+</script>