Skip to content

Commit

Permalink
Navigation API: focusReset should reset SFNSP
Browse files Browse the repository at this point in the history
SFNSP = sequential focus navigation starting point.

This was noticed while writing the spec at
WICG/navigation-api#201.

Bug: 1183545
Change-Id: If13f3c12e9c6ef2dbed628a65b823754ae04cd3d
  • Loading branch information
domenic authored and chromium-wpt-export-bot committed Mar 29, 2022
1 parent 2a0f3a9 commit bc85483
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions navigation-api/focus-reset/basic.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>

<script type="module">
import { testFocusWasReset, testFocusWasNotReset } from "./resources/helpers.mjs";
Expand Down
2 changes: 2 additions & 0 deletions navigation-api/focus-reset/multiple-transitionWhile.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>

<script type="module">
import { testFocusWasReset, testFocusWasNotReset } from "./resources/helpers.mjs";
Expand Down
32 changes: 30 additions & 2 deletions navigation-api/focus-reset/resources/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
// clean up any navigate event listeners, e.g. by using { once: true }, between
// tests.

const TAB_KEY = "\uE004";

export function testFocusWasReset(setupFunc, description) {
promise_test(async t => {
setupFunc(t);

const button = document.body.appendChild(document.createElement("button"));
t.add_cleanup(() => { button.remove(); });
const button2 = document.body.appendChild(document.createElement("button"));
button2.tabIndex = 0;
t.add_cleanup(() => {
button.remove();
button2.remove();
});

assert_equals(document.activeElement, document.body, "Start on body");
button.focus();
Expand All @@ -20,6 +27,11 @@ export function testFocusWasReset(setupFunc, description) {

await finished.catch(() => {});
assert_equals(document.activeElement, document.body, "Focus reset after the transition");

button2.onfocus = t.unreached_func("button2 must not be focused after pressing Tab");
const focusPromise = waitForFocus(t, button);
await test_driver.send_keys(document.body, TAB_KEY);
await focusPromise;
}, description);
}

Expand All @@ -28,7 +40,12 @@ export function testFocusWasNotReset(setupFunc, description) {
setupFunc(t);

const button = document.body.appendChild(document.createElement("button"));
t.add_cleanup(() => { button.remove(); });
const button2 = document.body.appendChild(document.createElement("button"));
button2.tabIndex = 0;
t.add_cleanup(() => {
button.remove();
button2.remove();
});

assert_equals(document.activeElement, document.body, "Start on body");
button.focus();
Expand All @@ -41,5 +58,16 @@ export function testFocusWasNotReset(setupFunc, description) {

await finished.catch(() => {});
assert_equals(document.activeElement, button, "Focus stays on the button after the transition");

button.onfocus = t.unreached_func("button must not be focused after pressing Tab");
const focusPromise = waitForFocus(t, button2);
await test_driver.send_keys(document.body, TAB_KEY);
await focusPromise;
}, description);
}

function waitForFocus(t, target) {
return new Promise(resolve => {
target.addEventListener("focus", () => resolve(), { once: true });
});
}

0 comments on commit bc85483

Please sign in to comment.