|
1 | 1 | <!doctype html> |
2 | 2 | <script src="/resources/testharness.js"></script> |
3 | 3 | <script src="/resources/testharnessreport.js"></script> |
| 4 | +<meta name="variant" content="?delayed-checkpoint"> |
| 5 | +<meta name="variant" content="?same-checkpoint"> |
4 | 6 |
|
5 | 7 | <button autofocus id="initialAutofocusTarget">Initial autofocus target</button> |
6 | 8 |
|
7 | 9 | <script type="module"> |
| 10 | + |
| 11 | +const delayed = location.search === "?delayed-checkpoint"; |
| 12 | + |
| 13 | +function createExpected(t, decoy, autofocusTarget) { |
| 14 | + const handler = delayed ? { handler: () => new Promise(r => t.step_timeout(r, 0)) } : {}; |
| 15 | + const expectedDecoy = delayed ? decoy : autofocusTarget; |
| 16 | + const decoyMessage = delayed ? "Focus moves to the autofocused button after the transition" : "Focus stays on the non-autofocused button after the transition"; |
| 17 | + const expectedBody = delayed ? document.body : expectedDecoy; |
| 18 | + const bodyMessage = delayed ? "Focus gets reset after the transition" : "Focus moves to the autofocused button after the transition"; |
| 19 | + |
| 20 | + return { handler, expectedDecoy, decoyMessage, expectedBody, bodyMessage }; |
| 21 | +} |
| 22 | + |
8 | 23 | promise_setup(async () => { |
9 | 24 | // Get the overall autofocus processed flag to flip to true, so that |
10 | 25 | // we only test the navigation API-specific stuff. |
|
22 | 37 | decoy.focus(); |
23 | 38 | assert_equals(document.activeElement, decoy, "focus() worked"); |
24 | 39 |
|
| 40 | + const { handler, expectedDecoy, decoyMessage } = createExpected(t, decoy, autofocusTarget); |
25 | 41 | navigation.addEventListener("navigate", e => { |
26 | | - e.intercept(); |
| 42 | + e.intercept(handler); |
27 | 43 | }, { once: true }); |
28 | 44 |
|
29 | 45 | const { committed, finished } = navigation.navigate("#1"); |
30 | 46 |
|
31 | 47 | await committed; |
32 | | - assert_equals(document.activeElement, decoy, "Focus stays on the non-autofocused button during the transition"); |
| 48 | + assert_equals(document.activeElement, expectedDecoy, decoyMessage); |
33 | 49 |
|
34 | 50 | await finished; |
35 | 51 | assert_equals(document.activeElement, autofocusTarget, "Focus moves to the autofocused button after the transition"); |
|
43 | 59 | decoy.focus(); |
44 | 60 | assert_equals(document.activeElement, decoy, "focus() worked"); |
45 | 61 |
|
| 62 | + const { handler, expectedDecoy, decoyMessage } = createExpected(t, decoy, autofocusTarget); |
46 | 63 | navigation.addEventListener("navigate", e => { |
47 | | - e.intercept(); |
| 64 | + e.intercept(handler); |
48 | 65 | }, { once: true }); |
49 | 66 |
|
50 | 67 | const { committed, finished } = navigation.navigate("#1"); |
51 | 68 |
|
52 | 69 | await committed; |
53 | | - assert_equals(document.activeElement, decoy, "Focus stays on the initially-focused button during the transition"); |
| 70 | + assert_equals(document.activeElement, expectedDecoy, decoyMessage); |
54 | 71 |
|
55 | 72 | await finished; |
56 | 73 | assert_equals(document.activeElement, autofocusTarget, "Focus moves to the first autofocused button after the transition"); |
|
64 | 81 | decoy.focus(); |
65 | 82 | assert_equals(document.activeElement, decoy, "focus() worked"); |
66 | 83 |
|
| 84 | + const { handler, expectedDecoy, decoyMessage, expectedBody, bodyMessage } = createExpected(t, decoy, autofocusTarget); |
67 | 85 | navigation.addEventListener("navigate", e => { |
68 | | - e.intercept(); |
| 86 | + e.intercept(handler); |
69 | 87 | }, { once: true }); |
70 | 88 |
|
71 | 89 | const { committed, finished } = navigation.navigate("#1"); |
72 | 90 |
|
73 | 91 | await committed; |
74 | | - assert_equals(document.activeElement, decoy, "Focus stays on the non-autofocused button during the transition"); |
| 92 | + assert_equals(document.activeElement, expectedDecoy, decoyMessage); |
75 | 93 |
|
76 | 94 | autofocusTarget.disabled = true; |
77 | 95 |
|
78 | 96 | await finished; |
79 | | - assert_equals(document.activeElement, document.body, "Focus gets reset after the transition"); |
| 97 | + assert_equals(document.activeElement, expectedBody, bodyMessage); |
80 | 98 | }, "An element with autofocus, present before navigation but disabled before finished, does not get focused"); |
81 | 99 |
|
82 | 100 | promise_test(async t => { |
|
87 | 105 | decoy.focus(); |
88 | 106 | assert_equals(document.activeElement, decoy, "focus() worked"); |
89 | 107 |
|
| 108 | + const { handler, expectedDecoy, decoyMessage, expectedBody, bodyMessage } = createExpected(t, decoy, autofocusTarget); |
90 | 109 | navigation.addEventListener("navigate", e => { |
91 | | - e.intercept(); |
| 110 | + e.intercept(handler); |
92 | 111 | }, { once: true }); |
93 | 112 |
|
94 | 113 | const { committed, finished } = navigation.navigate("#1"); |
95 | 114 |
|
96 | 115 | await committed; |
97 | | - assert_equals(document.activeElement, decoy, "Focus stays on the non-autofocused button during the transition"); |
| 116 | + assert_equals(document.activeElement, expectedDecoy, decoyMessage); |
98 | 117 |
|
99 | 118 | autofocusTarget.autofocus = false; |
100 | 119 |
|
101 | 120 | await finished; |
102 | | - assert_equals(document.activeElement, document.body, "Focus gets reset after the transition"); |
| 121 | + assert_equals(document.activeElement, expectedBody, bodyMessage); |
103 | 122 | }, "An element with autofocus, present before navigation but with its autofocus attribute removed before finished, does not get focused"); |
104 | 123 |
|
105 | 124 | promise_test(async t => { |
|
110 | 129 | decoy.focus(); |
111 | 130 | assert_equals(document.activeElement, decoy, "focus() worked"); |
112 | 131 |
|
| 132 | + const { handler } = createExpected(t, decoy, autofocusTarget); |
113 | 133 | navigation.addEventListener("navigate", e => { |
114 | | - e.intercept(); |
| 134 | + e.intercept(handler); |
115 | 135 | }, { once: true }); |
116 | 136 |
|
117 | 137 | const { committed, finished } = navigation.navigate("#1"); |
|
122 | 142 | decoy.disabled = true; |
123 | 143 |
|
124 | 144 | await finished; |
125 | | - assert_equals(document.activeElement, autofocusTarget, "Focus moves to the second autofocused button after the transition"); |
| 145 | + |
| 146 | + if (delayed) { |
| 147 | + assert_equals(document.activeElement, autofocusTarget, "Focus moves to the second autofocused button after the transition"); |
| 148 | + } else { |
| 149 | + assert_equals(document.activeElement, decoy, "Focus stays on the initially-focused button during the transition"); |
| 150 | + } |
126 | 151 | }, "Two elements with autofocus, present before navigation, but the first gets disabled; the second gets focused"); |
127 | 152 |
|
128 | 153 | promise_test(async t => { |
| 154 | + if (delayed) { |
| 155 | + assert_true(delayed, "This is only relevant for non-finished ") |
| 156 | + return; |
| 157 | + } |
129 | 158 | const decoy = createAndAppend(t); |
130 | 159 |
|
131 | 160 | assert_equals(document.activeElement, document.body, "Start on body"); |
132 | 161 | decoy.focus(); |
133 | 162 | assert_equals(document.activeElement, decoy, "focus() worked"); |
134 | 163 |
|
| 164 | + const { handler } = createExpected(t, decoy, document.body); |
135 | 165 | navigation.addEventListener("navigate", e => { |
136 | | - e.intercept(); |
| 166 | + e.intercept(() => { |
| 167 | + t.step_timeout(handler, 0) |
| 168 | + }); |
137 | 169 | }, { once: true }); |
138 | 170 |
|
139 | 171 | const { committed, finished } = navigation.navigate("#1"); |
140 | 172 |
|
141 | 173 | await committed; |
142 | | - assert_equals(document.activeElement, decoy, "Focus stays on the non-autofocused button during the transition"); |
| 174 | + assert_equals(document.activeElement, document.body, "Focus gets reset after the transition"); |
143 | 175 |
|
144 | 176 | const autofocusTarget = createAndAppend(t, { autofocus: true }); |
145 | 177 |
|
146 | 178 | await finished; |
147 | | - assert_equals(document.activeElement, autofocusTarget, "Focus moves to the autofocused button after the transition"); |
| 179 | + assert_equals(document.activeElement, document.body, "Focus gets reset after the transition"); |
| 180 | + |
| 181 | + await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r))); |
| 182 | + assert_equals(document.activeElement, document.body, "Focus stays reset two animation frames after the transition"); |
| 183 | + |
148 | 184 | }, "An element with autofocus, introduced between committed and finished, gets focused"); |
149 | 185 |
|
150 | 186 | promise_test(async t => { |
|
154 | 190 | decoy.focus(); |
155 | 191 | assert_equals(document.activeElement, decoy, "focus() worked"); |
156 | 192 |
|
| 193 | + const { handler } = createExpected(t, decoy, document.body); |
157 | 194 | navigation.addEventListener("navigate", e => { |
158 | | - e.intercept(); |
| 195 | + e.intercept(handler); |
159 | 196 | }, { once: true }); |
160 | 197 |
|
161 | 198 | const { committed, finished } = navigation.navigate("#1"); |
162 | 199 |
|
163 | 200 | await committed; |
164 | | - assert_equals(document.activeElement, decoy, "Focus stays on the non-autofocused button during the transition"); |
| 201 | + assert_equals(document.activeElement, delayed ? decoy : document.body, delayed ? "Focus stays on the non-autofocused button during the transition" : "Focus gets reset after the transition"); |
165 | 202 |
|
166 | 203 | await finished; |
167 | 204 | assert_equals(document.activeElement, document.body, "Focus gets reset after the transition"); |
|
0 commit comments