Skip to content

Truly independent web browser

Tests: Check a one-time grant ends when its page closes itself

sideshowbarker0c43fcb11027
diff --git a/Tests/LibWeb/Text/expected/PermissionsAPI/permission-allow-once-ends-when-the-page-closes-itself.txt b/Tests/LibWeb/Text/expected/PermissionsAPI/permission-allow-once-ends-when-the-page-closes-itself.txt
new file mode 100644
index 00000000000..8e8c11ce1ee
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PermissionsAPI/permission-allow-once-ends-when-the-page-closes-itself.txt
@@ -0,0 +1,2 @@
+allowed this time: granted
+after the popup closed itself: prompt
diff --git a/Tests/LibWeb/Text/input/PermissionsAPI/permission-allow-once-ends-when-the-page-closes-itself.html b/Tests/LibWeb/Text/input/PermissionsAPI/permission-allow-once-ends-when-the-page-closes-itself.html
new file mode 100644
index 00000000000..8dcb6c38511
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PermissionsAPI/permission-allow-once-ends-when-the-page-closes-itself.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A one-time grant ends when the last page showing the site goes away, a page that closes itself included: the
+ // popup that got it closes, and the next popup on the site is asked again.
+ asyncTest(async done => {
+ const httpServer = httpTestServer();
+ const runId = crypto.randomUUID();
+ const htmlHeaders = { "Content-Type": "text/html" };
+ const site = uniqueLocalhostHostname("allow-once-closed-site");
+
+ const echo = async (name, body) => {
+ const url = new URL(await httpServer.createEcho("GET", `/${name}-${runId}`, {
+ status: 200,
+ headers: htmlHeaders,
+ body: `<!DOCTYPE html><script>${body}<\/script>`,
+ }));
+ url.hostname = site;
+ return url.href;
+ };
+ const report = `const report = message => opener.postMessage(message, "*");
+ const state = async () => (await navigator.permissions.query({ name: "microphone" })).state;`;
+
+ const firstPageUrl = await echo("first", `${report}
+ internals.setNextPermissionPromptOutcome("microphone", "allow-once");
+ internals.requestPermission("microphone").then(value => {
+ report(\`allowed this time: \${value}\`);
+ window.close();
+ });`);
+ const returnUrl = await echo("return", `${report}
+ state().then(value => report(\`after the popup closed itself: \${value}\`));`);
+
+ const popups = [];
+ addEventListener("message", event => {
+ println(event.data);
+ if (event.data.startsWith("allowed this time")) {
+ const closing = popups[0];
+ const poll = setInterval(() => {
+ if (!closing.closed)
+ return;
+ clearInterval(poll);
+ popups.push(window.open(returnUrl));
+ }, 10);
+ return;
+ }
+ popups.forEach(popup => popup.close());
+ done();
+ });
+ popups.push(window.open(firstPageUrl));
+ });
+</script>