Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/common.js"></script>
<title> 'focus-without-user-activation' Policy : Correctly block automatic focus when policy disabled
</title>
<body>
<script>
"use strict"
// Note: According to html spec: https://html.spec.whatwg.org/#attr-fe-autofocus,
// topDocument's autofocus processed flag initially is false and is set to true
// after flushing autofocus candidates, i.e. flush of autofocus candidates
// only happens once per page load.
// In order to test the behaviour with both focus-without-user-activation on and off:
// two test files are necessary:
// - focus-without-user-activation-disabled-tentative.html
// - focus-without-user-activation-enabled-tentative.sub.html
// Use same origin url here because when iframe document has cross origin
// url, autofocus will be blocked by default with following console error:
// "Blocked autofocusing on a form control in a cross-origin subframe."
const url = "/permissions-policy/experimental-features/resources/focus-without-user-activation-iframe-tentative.html";
function subframe_focused(subframe, event_name, timeout) {
return new Promise(resolve => {
window.onmessage = m => resolve(m.data.focused);
subframe.contentWindow.postMessage({
event: event_name,
timeout: timeout
}, "*");
});
}
promise_test( async (instance) => {
const frame = createIframe(document.body, {
sandbox: "allow-scripts allow-same-origin",
allow: "focus-without-user-activation 'none'",
src: url
});
await wait_for_load(frame);
assert_false(await subframe_focused(frame, "autofocus", 400), "'autofocus' should not work.");
window.focus(); // Reset focus state in subframe.
assert_false(await subframe_focused(frame, "focus-input", 400), "'element.focus' should not work.");
window.focus(); // Reset focus state in subframe.
assert_false(await subframe_focused(frame, "focus-window", 400), "'window.focus' should not work.");
window.focus(); // Reset focus state in subframe.
}, "When the policy is disabled, 'autofocus' and scripted focus do not focus " +
"the document.");
</script>
</body>