|
| 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