|
12 | 12 | <button id="outside">Outside</button>
|
13 | 13 |
|
14 | 14 | <!-- test cases: -->
|
| 15 | + |
| 16 | +<!-- normal cases: --> |
15 | 17 | <dialog closedby="any" data-behavior="any"></dialog>
|
16 | 18 | <dialog closedby="closerequest" data-behavior="closerequest"></dialog>
|
17 | 19 | <dialog closedby="none" data-behavior="none"></dialog>
|
18 | 20 |
|
| 21 | +<!-- case sensitivity: --> |
19 | 22 | <dialog closedby="AnY" data-behavior="any"></dialog>
|
20 | 23 | <dialog closedby="ClOsErEqUeSt" data-behavior="closerequest"></dialog>
|
21 | 24 | <dialog closedby="NoNe" data-behavior="none"></dialog>
|
22 | 25 |
|
| 26 | +<!-- invalid value, no value, missing attribute: --> |
23 | 27 | <dialog closedby="invalid" data-behavior="auto"></dialog>
|
24 | 28 | <dialog closedby data-behavior="auto"></dialog>
|
25 | 29 | <dialog data-behavior="auto"></dialog>
|
26 | 30 |
|
27 | 31 | <script>
|
28 |
| - function openDialog(dialog,modal) { |
29 |
| - assert_false(dialog.open); |
30 |
| - assert_false(dialog.matches(':open')); |
31 |
| - if (modal) { |
32 |
| - dialog.showModal(); |
33 |
| - } else { |
34 |
| - dialog.show(); |
| 32 | + async 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'); |
35 | 47 | }
|
36 |
| - assert_true(dialog.open); |
37 |
| - assert_true(dialog.matches(':open')); |
38 |
| - assert_equals(dialog.matches(':modal'),modal); |
| 48 | + await waitForRender(); |
| 49 | + assert_true(dialog.open,'Should be open now'); |
| 50 | + assert_equals(dialog.matches(':open'),dialog.open,':open should match .open'); |
39 | 51 | }
|
40 |
| - function runTest(dialog) { |
41 |
| - for(modal of [false,true]) { |
42 |
| - promise_test(async (t) => { |
43 |
| - assert_false(dialog.open); |
44 |
| - assert_false(dialog.matches(':open')); |
45 |
| - t.add_cleanup(() => dialog.close()); |
46 |
| - // Try hitting ESC |
47 |
| - openDialog(dialog,modal); |
48 |
| - const closedByReflectionWhileOpen = dialog.closedBy; |
49 |
| - const ESC = '\uE00C'; |
50 |
| - await new test_driver.send_keys(document.documentElement,ESC); |
51 |
| - const respondsToEsc = !dialog.open; |
52 |
| - const respondsToEsc2 = !dialog.matches(':open'); |
53 |
| - dialog.close(); |
54 |
| - // Try clicking outside |
55 |
| - openDialog(dialog,modal); |
56 |
| - await clickOn(outside); |
57 |
| - const respondsToLightDismiss = !dialog.open; |
58 |
| - const respondsToLightDismiss2 = !dialog.matches(':open'); |
59 |
| - dialog.close(); |
60 |
| - // See if expectations match |
61 |
| - let expectedReflectionWhileOpen = dialog.dataset.behavior; |
62 |
| - let expectedReflectionWhileClosed = dialog.dataset.behavior; |
63 |
| - switch (dialog.dataset.behavior) { |
64 |
| - case 'any': |
65 |
| - assert_true(respondsToEsc,'Dialog should respond to ESC'); |
66 |
| - assert_true(respondsToEsc2,'Dialog should respond to ESC (:open)'); |
67 |
| - assert_true(respondsToLightDismiss,'Dialog should respond to light dismiss'); |
68 |
| - assert_true(respondsToLightDismiss2,'Dialog should respond to light dismiss (:open)'); |
69 |
| - break; |
70 |
| - case 'closerequest': |
71 |
| - assert_true(respondsToEsc,'Dialog should respond to ESC'); |
72 |
| - assert_true(respondsToEsc2,'Dialog should respond to ESC (:open)'); |
73 |
| - assert_false(respondsToLightDismiss,'Dialog should NOT respond to light dismiss'); |
74 |
| - assert_false(respondsToLightDismiss2,'Dialog should NOT respond to light dismiss (:open)'); |
75 |
| - break; |
76 |
| - case 'none': |
77 |
| - assert_false(respondsToEsc,'Dialog should NOT respond to ESC'); |
78 |
| - assert_false(respondsToEsc2,'Dialog should NOT respond to ESC (:open)'); |
79 |
| - assert_false(respondsToLightDismiss,'Dialog should NOT respond to light dismiss'); |
80 |
| - assert_false(respondsToLightDismiss2,'Dialog should NOT respond to light dismiss (:open)'); |
81 |
| - break; |
82 |
| - case 'auto': |
83 |
| - if (modal) { |
84 |
| - assert_true(respondsToEsc,'Modal dialog in auto state should respond to ESC'); |
85 |
| - assert_false(respondsToLightDismiss,'Modal dialog in auto state should NOT respond to light dismiss'); |
86 |
| - expectedReflectionWhileOpen = 'closerequest'; |
87 |
| - } else { |
88 |
| - assert_false(respondsToEsc,'Non-modal dialog in auto state should NOT respond to ESC'); |
89 |
| - assert_false(respondsToLightDismiss,'Non-modal dialog in auto state should NOT respond to light dismiss'); |
90 |
| - expectedReflectionWhileOpen = 'none'; |
91 |
| - } |
92 |
| - expectedReflectionWhileClosed = 'none'; |
93 |
| - break; |
94 |
| - default: |
95 |
| - assert_unreached('Invalid expectation'); |
| 52 | + |
| 53 | + function getDefaultExpectations(behavior, openMethod) { |
| 54 | + switch (behavior) { |
| 55 | + case 'any': |
| 56 | + return { |
| 57 | + respondsToEsc: true, |
| 58 | + respondsToLightDismiss: true, |
| 59 | + expectedReflectionWhileOpen: behavior, |
| 60 | + expectedReflectionWhileClosed: behavior, |
| 61 | + }; |
| 62 | + case 'closerequest': |
| 63 | + return { |
| 64 | + respondsToEsc: true, |
| 65 | + respondsToLightDismiss: false, |
| 66 | + expectedReflectionWhileOpen: behavior, |
| 67 | + expectedReflectionWhileClosed: behavior, |
| 68 | + }; |
| 69 | + case 'none': |
| 70 | + return { |
| 71 | + respondsToEsc: false, |
| 72 | + respondsToLightDismiss: false, |
| 73 | + expectedReflectionWhileOpen: behavior, |
| 74 | + expectedReflectionWhileClosed: behavior, |
| 75 | + }; |
| 76 | + case 'auto': |
| 77 | + if (openMethod === 'modal') { |
| 78 | + return { |
| 79 | + respondsToEsc: true, |
| 80 | + respondsToLightDismiss: false, |
| 81 | + expectedReflectionWhileOpen: 'closerequest', |
| 82 | + expectedReflectionWhileClosed: 'none', |
| 83 | + }; |
| 84 | + } else { |
| 85 | + return { |
| 86 | + respondsToEsc: false, |
| 87 | + respondsToLightDismiss: false, |
| 88 | + expectedReflectionWhileOpen: 'none', |
| 89 | + expectedReflectionWhileClosed: 'none', |
| 90 | + }; |
96 | 91 | }
|
97 |
| - // Check reflection |
98 |
| - assert_equals(closedByReflectionWhileOpen,expectedReflectionWhileOpen,'Reflection should be limited to known values (open)'); |
99 |
| - assert_equals(dialog.closedBy,expectedReflectionWhileClosed,'Reflection should be limited to known values (closed)'); |
100 |
| - }, `closedby=${dialog.getAttribute('closedby')}, ${modal ? 'Modal' : 'Non-modal'}`); |
| 92 | + default: |
| 93 | + assert_unreached('Invalid expectation'); |
101 | 94 | }
|
102 | 95 | }
|
103 | 96 |
|
104 |
| - // Add close button, in case of manual testing |
105 |
| - const testDialogs = document.querySelectorAll('dialog'); |
106 |
| - testDialogs.forEach(dialog => { |
107 |
| - const button = dialog.appendChild(document.createElement('button')); |
108 |
| - button.innerText = 'Close'; |
109 |
| - button.addEventListener('click',() => dialog.close()); |
110 |
| - }); |
| 97 | + function runTest(dialog, openMethod) { |
| 98 | + promise_test(async (t) => { |
| 99 | + assert_false(dialog.open,'setup'); |
| 100 | + assert_false(dialog.matches(':open')); |
| 101 | + t.add_cleanup(() => dialog.close()); |
| 102 | + |
| 103 | + // Open the dialog |
| 104 | + await 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 | + await test_driver.send_keys(document.documentElement,ESC); |
| 111 | + await waitForRender(); |
| 112 | + const respondsToEsc = !dialog.open; |
| 113 | + assert_equals(!dialog.matches(':open'),respondsToEsc,':open should match dialog.open'); |
| 114 | + dialog.close(); |
| 115 | + |
| 116 | + // Try clicking outside |
| 117 | + await openDialog(dialog,openMethod); |
| 118 | + assert_equals(dialog.matches(':modal'),openMethod === 'modal',':modal incorrect'); |
| 119 | + await clickOn(outside); |
| 120 | + const respondsToLightDismiss = !dialog.open; |
| 121 | + assert_equals(!dialog.matches(':open'),respondsToLightDismiss,':open should match dialog.open'); |
| 122 | + dialog.close(); |
| 123 | + |
| 124 | + // See if expectations match |
| 125 | + let expectations = getDefaultExpectations(dialog.dataset.behavior, openMethod); |
| 126 | + assert_equals(respondsToEsc,expectations.respondsToEsc,`Dialog ${expectations.respondsToEsc ? "should" : "should NOT"} respond to ESC`); |
| 127 | + assert_equals(respondsToLightDismiss,expectations.respondsToLightDismiss,`Dialog ${expectations.respondsToLightDismiss ? "should" : "should NOT"} respond to light dismiss`); |
| 128 | + assert_equals(closedByReflectionWhileOpen,expectations.expectedReflectionWhileOpen,'Reflection should be limited to known values (open)'); |
| 129 | + assert_equals(dialog.closedBy,expectations.expectedReflectionWhileClosed,'Reflection should be limited to known values (closed)'); |
| 130 | + }, `closedby=${dialog.getAttribute('closedby')}, ${openMethod}`); |
| 131 | + } |
111 | 132 |
|
112 | 133 | // Run tests
|
113 |
| - testDialogs.forEach(runTest); |
| 134 | + document.querySelectorAll('dialog').forEach((dialog) => { |
| 135 | + for(openMethod of ['modeless','modal','open']) { |
| 136 | + runTest(dialog, openMethod); |
| 137 | + } |
| 138 | + }); |
114 | 139 | </script>
|
0 commit comments