Bug 1444491 - Update existing autofocus related WPTs to make them more robust r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D162035
This commit is contained in:
Sean Feng 2023-02-15 20:08:37 +00:00
parent 6b5798cb94
commit 8e0155cf74
5 changed files with 37 additions and 3 deletions

View File

@ -10,9 +10,12 @@
promise_test(async () => {
await waitForLoad(window);
const iframe = document.querySelector('iframe');
await waitUntilStableAutofocusState();
assert_equals(document.activeElement, document.querySelector('iframe'),
assert_equals(document.activeElement, iframe,
'Autofocus elements in iframes should be focused.');
const doc = iframe.contentDocument;
assert_true(!doc.querySelector(':target'));
let input = document.createElement('input');
input.autofocus = true;

View File

@ -21,7 +21,23 @@ promise_test(async () => {
doc.body.appendChild(input);
await waitUntilStableAutofocusState();
assert_not_equals(doc.activeElement, input);
}, 'Autofocus elements in iframed documents with URL fragments should be skipped.');
iframe.remove();
}, 'Autofocus elements in iframed documents with URL fragments should be skipped. (id matches)');
promise_test(async () => {
let iframe = await waitForIframeLoad("resources/frame-with-a.html");
iframe.contentWindow.location.hash = 'anchor1';
await waitForEvent(iframe.contentWindow, 'hashchange');
const doc = iframe.contentDocument;
assert_true(!!doc.querySelector(':target'));
let input = doc.createElement('input');
input.autofocus = true;
doc.body.appendChild(input);
await waitUntilStableAutofocusState();
assert_not_equals(doc.activeElement, input);
iframe.remove();
}, 'Autofocus elements in iframed documents with URL fragments should be skipped.(a element)');
promise_test(async () => {
let w = window.open('resources/frame-with-anchor.html');

View File

@ -0,0 +1,4 @@
<!DOCTYPE html>
<body>
<a name="anchor1"></a>
</body>

View File

@ -39,3 +39,13 @@ async function waitUntilStableAutofocusState(w) {
// Awaiting one animation frame is an easy way to determine autofocus state.
await waitForAnimationFrame(targetWindow);
}
async function waitForIframeLoad(src, w = window) {
const iframe = w.document.createElement("iframe");
let loadPromise = new Promise(resolve => {
iframe.addEventListener("load", () => resolve(iframe));
});
iframe.src = src;
w.document.body.appendChild(iframe);
return loadPromise;
}

View File

@ -11,7 +11,8 @@
'use strict';
promise_test(async () => {
await waitForEvent(document.body, 'focus', {capture:true});
await waitForLoad(window);
await waitForAnimationFrame();
assert_equals(document.activeElement.id, 'second');
}, 'Script-blocking style sheet should pause flushing autofocus candidates.');
</script>