dialog-closedby.html (5435B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <meta name="timeout" content="long"> 4 <link rel=help href="https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-light-dismiss"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/resources/testdriver.js"></script> 8 <script src="/resources/testdriver-actions.js"></script> 9 <script src="/resources/testdriver-vendor.js"></script> 10 <script src="../../popovers/resources/popover-utils.js"></script> 11 12 <button id="outside">Outside</button> 13 14 <!-- test cases: --> 15 16 <!-- normal cases: --> 17 <dialog closedby="any" data-behavior="any"></dialog> 18 <dialog closedby="closerequest" data-behavior="closerequest"></dialog> 19 <dialog closedby="none" data-behavior="none"></dialog> 20 21 <!-- case sensitivity: --> 22 <dialog closedby="AnY" data-behavior="any"></dialog> 23 <dialog closedby="ClOsErEqUeSt" data-behavior="closerequest"></dialog> 24 <dialog closedby="NoNe" data-behavior="none"></dialog> 25 26 <!-- invalid value, no value, missing attribute: --> 27 <dialog closedby="invalid" data-behavior="auto"></dialog> 28 <dialog closedby data-behavior="auto"></dialog> 29 <dialog data-behavior="auto"></dialog> 30 31 <script> 32 function openDialog(dialog,openMethod) { 33 assert_false(dialog.open,'Should be closed to start'); 34 assert_equals(dialog.matches('[open]'),dialog.open,'[open] should match .open'); 35 switch (openMethod) { 36 case 'modeless' : 37 dialog.show(); 38 break; 39 case 'modal' : 40 dialog.showModal(); 41 break; 42 case 'open' : 43 dialog.open = true; 44 break; 45 default: 46 assert_unreached('Invalid open method'); 47 } 48 assert_true(dialog.open,'Should be open now'); 49 assert_equals(dialog.matches('[open]'),dialog.open,'[open] should match .open'); 50 } 51 52 function getDefaultExpectations(behavior, openMethod) { 53 switch (behavior) { 54 case 'any': 55 return { 56 respondsToEsc: true, 57 respondsToLightDismiss: true, 58 expectedReflectionWhileOpen: behavior, 59 expectedReflectionWhileClosed: behavior, 60 }; 61 case 'closerequest': 62 return { 63 respondsToEsc: true, 64 respondsToLightDismiss: false, 65 expectedReflectionWhileOpen: behavior, 66 expectedReflectionWhileClosed: behavior, 67 }; 68 case 'none': 69 return { 70 respondsToEsc: false, 71 respondsToLightDismiss: false, 72 expectedReflectionWhileOpen: behavior, 73 expectedReflectionWhileClosed: behavior, 74 }; 75 case 'auto': 76 if (openMethod === 'modal') { 77 return { 78 respondsToEsc: true, 79 respondsToLightDismiss: false, 80 expectedReflectionWhileOpen: 'closerequest', 81 expectedReflectionWhileClosed: 'none', 82 }; 83 } else { 84 return { 85 respondsToEsc: false, 86 respondsToLightDismiss: false, 87 expectedReflectionWhileOpen: 'none', 88 expectedReflectionWhileClosed: 'none', 89 }; 90 } 91 default: 92 assert_unreached('Invalid expectation'); 93 } 94 } 95 96 function runTest(dialog, openMethod) { 97 promise_test(async (t) => { 98 assert_false(dialog.open,'setup'); 99 assert_false(dialog.matches('[open]')); 100 t.add_cleanup(() => dialog.close()); 101 let expectations = getDefaultExpectations(dialog.dataset.behavior, openMethod); 102 103 // Open the dialog 104 openDialog(dialog,openMethod); 105 assert_equals(dialog.matches(':modal'),openMethod === 'modal',':modal incorrect'); 106 const closedByReflectionWhileOpen = dialog.closedBy; 107 108 // Try hitting ESC 109 const ESC = '\uE00C'; 110 const close_fired = new Promise(resolve => { 111 dialog.addEventListener('close', resolve, { once: true }) 112 }); 113 await test_driver.send_keys(document.documentElement,ESC); 114 if (expectations.respondsToEsc) { 115 await close_fired; 116 } else { 117 await waitForRender(); 118 } 119 const respondsToEsc = !dialog.open; 120 assert_equals(!dialog.matches('[open]'),respondsToEsc,'[open] should match dialog.open'); 121 dialog.close(); 122 123 // Try clicking outside 124 openDialog(dialog,openMethod); 125 assert_equals(dialog.matches(':modal'),openMethod === 'modal',':modal incorrect'); 126 await clickOn(outside); 127 const respondsToLightDismiss = !dialog.open; 128 assert_equals(!dialog.matches('[open]'),respondsToLightDismiss,'[open] should match dialog.open'); 129 dialog.close(); 130 131 // See if expectations match 132 assert_equals(respondsToEsc,expectations.respondsToEsc,`Dialog ${expectations.respondsToEsc ? "should" : "should NOT"} respond to ESC`); 133 assert_equals(respondsToLightDismiss,expectations.respondsToLightDismiss,`Dialog ${expectations.respondsToLightDismiss ? "should" : "should NOT"} respond to light dismiss`); 134 assert_equals(closedByReflectionWhileOpen,expectations.expectedReflectionWhileOpen,'Reflection should be limited to known values (open)'); 135 assert_equals(dialog.closedBy,expectations.expectedReflectionWhileClosed,'Reflection should be limited to known values (closed)'); 136 }, `closedby=${dialog.getAttribute('closedby')}, ${openMethod}`); 137 } 138 139 // Run tests 140 document.querySelectorAll('dialog').forEach((dialog) => { 141 for(openMethod of ['modeless','modal','open']) { 142 runTest(dialog, openMethod); 143 } 144 }); 145 </script>