From 1df09889a2a67fbe8a4443c21700722e87f3d422 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Sat, 26 Feb 2022 22:25:30 +0000 Subject: [PATCH] Bug 1754222 [wpt PR 32750] - App history: basic focusReset support, a=testonly Automatic update from web-platform-tests App history: basic focusReset support Based on the draft at https://github.com/WICG/app-history/pull/201. This includes handling multiple conflicting focusReset options, with a console warning if necessary. Limitations, to be addressed in future commits: * This does not take into account autofocus="" elements. * This will reset focus even if the user has moved focus in the meantime. Bug: 1183545 Change-Id: I1eae4fc58af653fa7e463a1346a9bebc9536a59f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3441109 Reviewed-by: Nate Chapin Commit-Queue: Domenic Denicola Cr-Commit-Position: refs/heads/main@{#970838} -- wpt-commits: e6bbf057e52677c2f90fc22925673e3ca72bcd69 wpt-pr: 32750 --- .../tests/app-history/focus-reset/basic.html | 64 ++++++++++++++++++ .../focus-reset/multiple-transitionWhile.html | 67 +++++++++++++++++++ .../focus-reset/resources/helpers.mjs | 45 +++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 testing/web-platform/tests/app-history/focus-reset/basic.html create mode 100644 testing/web-platform/tests/app-history/focus-reset/multiple-transitionWhile.html create mode 100644 testing/web-platform/tests/app-history/focus-reset/resources/helpers.mjs diff --git a/testing/web-platform/tests/app-history/focus-reset/basic.html b/testing/web-platform/tests/app-history/focus-reset/basic.html new file mode 100644 index 0000000000000..fb57cf864482a --- /dev/null +++ b/testing/web-platform/tests/app-history/focus-reset/basic.html @@ -0,0 +1,64 @@ + + + + + diff --git a/testing/web-platform/tests/app-history/focus-reset/multiple-transitionWhile.html b/testing/web-platform/tests/app-history/focus-reset/multiple-transitionWhile.html new file mode 100644 index 0000000000000..d38df573e5953 --- /dev/null +++ b/testing/web-platform/tests/app-history/focus-reset/multiple-transitionWhile.html @@ -0,0 +1,67 @@ + + + + + diff --git a/testing/web-platform/tests/app-history/focus-reset/resources/helpers.mjs b/testing/web-platform/tests/app-history/focus-reset/resources/helpers.mjs new file mode 100644 index 0000000000000..e3012a0b12e61 --- /dev/null +++ b/testing/web-platform/tests/app-history/focus-reset/resources/helpers.mjs @@ -0,0 +1,45 @@ +// Usage note: if you use these more than once in a given file, be sure to +// clean up any navigate event listeners, e.g. by using { once: true }, between +// tests. + +export function testFocusWasReset(setupFunc, description) { + promise_test(async t => { + setupFunc(t); + + const button = document.body.appendChild(document.createElement("button")); + t.add_cleanup(() => { button.remove(); }); + + assert_equals(document.activeElement, document.body, "Start on body"); + button.focus(); + assert_equals(document.activeElement, button, "focus() worked"); + + const { committed, finished } = appHistory.navigate("#" + location.hash.substring(1) + "1"); + + await committed; + assert_equals(document.activeElement, button, "Focus stays on the button during the transition"); + + await finished.catch(() => {}); + assert_equals(document.activeElement, document.body, "Focus reset after the transition"); + }, description); +} + +export function testFocusWasNotReset(setupFunc, description) { + promise_test(async t => { + setupFunc(t); + + const button = document.body.appendChild(document.createElement("button")); + t.add_cleanup(() => { button.remove(); }); + + assert_equals(document.activeElement, document.body, "Start on body"); + button.focus(); + assert_equals(document.activeElement, button, "focus() worked"); + + const { committed, finished } = appHistory.navigate("#" + location.hash.substring(1) + "1"); + + await committed; + assert_equals(document.activeElement, button, "Focus stays on the button during the transition"); + + await finished.catch(() => {}); + assert_equals(document.activeElement, button, "Focus stays on the button after the transition"); + }, description); +}