Skip to content

Commit e4718b3

Browse files
mfreed7chromium-wpt-export-bot
authored andcommitted
Implement dialog.requestClose() [4/N]
This implements dialog.requestClose() and adds a test. See spec PR for details: whatwg/html#10737 Bug: 376516550 Change-Id: Iaac3d89c28844d2b54ff5b1a7b68dc356d1fd172
1 parent af91552 commit e4718b3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!doctype html>
2+
<meta charset="utf-8">
3+
<link rel=help href="https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-request-close">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="/resources/testdriver.js"></script>
7+
<script src="/resources/testdriver-actions.js"></script>
8+
<script src="/resources/testdriver-vendor.js"></script>
9+
<script src="../../popovers/resources/popover-utils.js"></script>
10+
11+
<dialog>Dialog</dialog>
12+
13+
<script>
14+
const dialog = document.querySelector('dialog');
15+
function openDialog(modal) {
16+
assert_false(dialog.open);
17+
if (modal) {
18+
dialog.showModal();
19+
} else {
20+
dialog.show();
21+
}
22+
assert_true(dialog.open);
23+
assert_equals(dialog.matches(':modal'),modal);
24+
}
25+
function getSignal(t) {
26+
const controller = new AbortController();
27+
const signal = controller.signal;
28+
t.add_cleanup(() => controller.abort());
29+
return signal;
30+
}
31+
32+
// Run this test first, since the user activation from other tests will persist.
33+
promise_test(async (t) => {
34+
t.add_cleanup(() => dialog.close());
35+
const signal = getSignal(t);
36+
dialog.addEventListener('cancel',(e) => {
37+
e.preventDefault();
38+
},{signal});
39+
openDialog(/*modal*/true);
40+
dialog.requestClose();
41+
assert_false(dialog.open,'Without user activation, requestClose can\'t be cancelled');
42+
},`requestClose requires user activation in order to be cancelable`);
43+
44+
[false,true].forEach(modal => {
45+
promise_test(async (t) => {
46+
t.add_cleanup(() => dialog.close());
47+
openDialog(modal);
48+
dialog.requestClose();
49+
assert_false(dialog.open);
50+
},`${modal ? "Modal:" : "Non-modal:"} requestClose closes the dialog`);
51+
52+
promise_test(async (t) => {
53+
t.add_cleanup(() => dialog.close());
54+
const signal = getSignal(t);
55+
let shouldPreventDefault = true;
56+
dialog.addEventListener('cancel',(e) => {
57+
if (shouldPreventDefault) {
58+
e.preventDefault();
59+
}
60+
},{signal});
61+
openDialog(modal);
62+
await clickOn(dialog); // User activation
63+
dialog.requestClose();
64+
assert_true(dialog.open,'cancel event was cancelled - dialog shouldn\'t close');
65+
shouldPreventDefault = false;
66+
await clickOn(dialog); // User activation
67+
dialog.requestClose();
68+
assert_false(dialog.open,'cancel event was not cancelled - dialog should now close');
69+
},`${modal ? "Modal:" : "Non-modal:"} requestClose can be cancelled`);
70+
});
71+
</script>

0 commit comments

Comments
 (0)