Skip to content

Truly independent web browser

Tests/LibWeb: Import WPT tests for closing dialogs with close requests

Andreas Klingb2e3d359ae5b
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/non-modal-canceling.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/non-modal-canceling.txt
new file mode 100644
index 00000000000..403334de8de
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/non-modal-canceling.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Non-modal dialogs should not be cancelable via ESC
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/close-watcher/resources/helpers.js b/Tests/LibWeb/Text/input/wpt-import/close-watcher/resources/helpers.js
new file mode 100644
index 00000000000..ad80c28847f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/close-watcher/resources/helpers.js
@@ -0,0 +1,80 @@
+window.createRecordingCloseWatcher = (t, events, name, type, parentWatcher) => {
+ let watcher = null;
+ if (type === 'dialog') {
+ watcher = document.createElement('dialog');
+ watcher.textContent = 'hello world';
+ t.add_cleanup(() => watcher.remove());
+ if (parentWatcher?.appendChild) {
+ parentWatcher.appendChild(watcher);
+ } else {
+ document.body.appendChild(watcher);
+ }
+ watcher.showModal();
+ } else if (type === 'popover') {
+ watcher = document.createElement('div');
+ watcher.setAttribute('popover', 'auto');
+ watcher.textContent = 'hello world';
+ t.add_cleanup(() => watcher.remove());
+ if (parentWatcher?.appendChild) {
+ parentWatcher.appendChild(watcher);
+ } else {
+ document.body.appendChild(watcher);
+ }
+ watcher.showPopover();
+ } else {
+ watcher = new CloseWatcher();
+ t.add_cleanup(() => watcher.destroy());
+ }
+
+ const prefix = name === undefined ? '' : name + ' ';
+ watcher.addEventListener('cancel', e => {
+ const cancelable = e.cancelable ? '[cancelable=true]' : '[cancelable=false]';
+ events.push(prefix + 'cancel' + cancelable);
+ });
+ watcher.addEventListener('close', () => {
+ events.push(prefix + 'close');
+ });
+
+ return watcher;
+};
+
+window.createBlessedRecordingCloseWatcher = async (t, events, name, type, parentWatcher) => {
+ await maybeTopLayerBless(parentWatcher);
+ return createRecordingCloseWatcher(t, events, name, type, parentWatcher);
+};
+
+window.destroyCloseWatcher = (watcher) => {
+ if (watcher instanceof HTMLElement) {
+ watcher.remove();
+ } else {
+ watcher.destroy();
+ }
+};
+
+window.sendEscKey = () => {
+ // Esc is \uE00C, *not* \uu001B; see https://w3c.github.io/webdriver/#keyboard-actions.
+ //
+ // It's important to target document.body, and not any element that might stop receiving events
+ // if a popover or dialog is making that element inert.
+ return test_driver.send_keys(document.body, '\uE00C');
+};
+
+// For now, we always use the Esc keypress as our close request. In
+// theory, in the future, we could add a WebDriver command or similar
+// for the close request, which would allow different tests on platforms
+// with different close requests. In that case, we'd update this
+// function, but not update the sendEscKey function above.
+window.sendCloseRequest = window.sendEscKey;
+
+window.maybeTopLayerBless = (watcher) => {
+ if (watcher instanceof HTMLElement) {
+ return blessTopLayer(watcher);
+ }
+ return test_driver.bless();
+};
+
+window.waitForPotentialCloseEvent = () => {
+ // CloseWatchers fire close events synchronously, but dialog elements wait
+ // for a rAF before firing them.
+ return new Promise(requestAnimationFrame);
+};
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/interactive-elements/the-dialog-element/non-modal-canceling.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/interactive-elements/the-dialog-element/non-modal-canceling.html
new file mode 100644
index 00000000000..cfb55a652c3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/interactive-elements/the-dialog-element/non-modal-canceling.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<link rel=author href="mailto:[email protected]">
+<link rel=help href="https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script src="../../../../resources/testdriver.js"></script>
+<script src="../../../../resources/testdriver-actions.js"></script>
+<script src="../../../../resources/testdriver-vendor.js"></script>
+<script src="../../../../close-watcher/resources/helpers.js"></script>
+
+<dialog>Dialog</dialog>
+
+<script>
+promise_test(async () => {
+ const dialog = document.querySelector('dialog');
+ assert_false(dialog.open);
+ dialog.show();
+ assert_true(dialog.open);
+ await sendEscKey();
+ assert_true(dialog.open,'Escape does not close a non-modal dialog');
+ dialog.close();
+ dialog.showModal();
+ assert_true(dialog.open);
+ await sendEscKey();
+ assert_false(dialog.open,'Escape does close a modal dialog');
+},'Non-modal dialogs should not be cancelable via ESC');
+</script>