Commit e8315e0d authored by Charlie Hu's avatar Charlie Hu Committed by Commit Bot

Fix flaky test focus-without-user-activation-tentative.sub.html

Previously the test gives 400 ms timeout to wait for |onfocus| event
to happen, while sometimes 400 ms is simply not enough, which result
in flakiness.

The solution is to remove this timeout restriction if we are expecting
an onfocus event to happen.

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

Skipping focus-without-user-activation-enabled-tentative.sub.html
because the desired behaviour is not yet implemented.

Bug: 1067482
Change-Id: I3d1c8f77309054bbad0e666543372e28040ddcd2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2202677Reviewed-by: default avatarIan Clelland <iclelland@chromium.org>
Commit-Queue: Charlie Hu <chenleihu@google.com>
Cr-Commit-Position: refs/heads/master@{#772309}
parent 572e1398
...@@ -5364,6 +5364,9 @@ crbug.com/1029941 [ Linux ] virtual/exotic-color-space/images/yuv-decode-eligibl ...@@ -5364,6 +5364,9 @@ crbug.com/1029941 [ Linux ] virtual/exotic-color-space/images/yuv-decode-eligibl
# Failing document policy tests # Failing document policy tests
crbug.com/993790 external/wpt/document-policy/required-policy/separate-document-policies.html [ Failure ] crbug.com/993790 external/wpt/document-policy/required-policy/separate-document-policies.html [ Failure ]
# Skipping because of unimplemented behaviour
crbug.com/965495 external/wpt/feature-policy/experimental-features/focus-without-user-activation-enabled-tentative.sub.html [ Skip ]
# Temporary suppression to allow devtools-frontend changes # Temporary suppression to allow devtools-frontend changes
crbug.com/1029489 http/tests/devtools/elements/elements-linkify-attributes.js [ Pass Failure Timeout ] crbug.com/1029489 http/tests/devtools/elements/elements-linkify-attributes.js [ Pass Failure Timeout ]
crbug.com/1029521 virtual/threaded/external/wpt/web-animations/timing-model/animations/updating-the-finished-state.html [ Pass Failure Timeout ] crbug.com/1029521 virtual/threaded/external/wpt/web-animations/timing-model/animations/updating-the-finished-state.html [ Pass Failure Timeout ]
......
...@@ -1729,7 +1729,8 @@ crbug.com/1050754 external/wpt/eventsource/shared-worker/eventsource-onopen.htm ...@@ -1729,7 +1729,8 @@ crbug.com/1050754 external/wpt/eventsource/shared-worker/eventsource-onopen.htm
crbug.com/1050754 external/wpt/eventsource/shared-worker/eventsource-prototype.htm [ Failure ] crbug.com/1050754 external/wpt/eventsource/shared-worker/eventsource-prototype.htm [ Failure ]
crbug.com/1050754 external/wpt/eventsource/shared-worker/eventsource-url.htm [ Failure ] crbug.com/1050754 external/wpt/eventsource/shared-worker/eventsource-url.htm [ Failure ]
crbug.com/1050754 external/wpt/feature-policy/experimental-features/document-write.tentative.html [ Failure ] crbug.com/1050754 external/wpt/feature-policy/experimental-features/document-write.tentative.html [ Failure ]
crbug.com/1050754 external/wpt/feature-policy/experimental-features/focus-without-user-activation-tentative.sub.html [ Failure ] crbug.com/1050754 external/wpt/feature-policy/experimental-features/focus-without-user-activation-disabled-tentative.html [ Failure ]
crbug.com/1050754 external/wpt/feature-policy/experimental-features/focus-without-user-activation-enabled-tentative.sub.html [ Skip ]
crbug.com/1050754 external/wpt/feature-policy/experimental-features/layout-animations-disabled-tentative.html [ Failure ] crbug.com/1050754 external/wpt/feature-policy/experimental-features/layout-animations-disabled-tentative.html [ Failure ]
crbug.com/1050754 external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-js-tentative.html [ Timeout ] crbug.com/1050754 external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-js-tentative.html [ Timeout ]
crbug.com/1050754 external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-keyframes-tentative.html [ Timeout ] crbug.com/1050754 external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-keyframes-tentative.html [ Timeout ]
......
...@@ -1782,7 +1782,8 @@ external/wpt/eventsource/shared-worker/eventsource-onopen.htm [ Failure ] ...@@ -1782,7 +1782,8 @@ external/wpt/eventsource/shared-worker/eventsource-onopen.htm [ Failure ]
external/wpt/eventsource/shared-worker/eventsource-prototype.htm [ Failure ] external/wpt/eventsource/shared-worker/eventsource-prototype.htm [ Failure ]
external/wpt/eventsource/shared-worker/eventsource-url.htm [ Failure ] external/wpt/eventsource/shared-worker/eventsource-url.htm [ Failure ]
external/wpt/feature-policy/experimental-features/document-write.tentative.html [ Failure ] external/wpt/feature-policy/experimental-features/document-write.tentative.html [ Failure ]
external/wpt/feature-policy/experimental-features/focus-without-user-activation-tentative.sub.html [ Failure ] external/wpt/feature-policy/experimental-features/focus-without-user-activation-disabled-tentative.html [ Failure ]
external/wpt/feature-policy/experimental-features/focus-without-user-activation-enabled-tentative.sub.html [ Skip ]
external/wpt/feature-policy/experimental-features/layout-animations-disabled-tentative.html [ Failure ] external/wpt/feature-policy/experimental-features/layout-animations-disabled-tentative.html [ Failure ]
external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-js-tentative.html [ Timeout ] external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-js-tentative.html [ Timeout ]
external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-keyframes-tentative.html [ Timeout ] external/wpt/feature-policy/experimental-features/layout-animations-disabled-violation-report-keyframes-tentative.html [ Timeout ]
......
<!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 = "/feature-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>
...@@ -5,49 +5,31 @@ ...@@ -5,49 +5,31 @@
<title> 'focus-without-user-activation' Policy : Correctly block automatic focus when policy disabled <title> 'focus-without-user-activation' Policy : Correctly block automatic focus when policy disabled
</title> </title>
<body> <body>
<input onblur="did_blur();" autofocus/>
<script> <script>
"use strict" "use strict"
// Note: According to html spec: https://html.spec.whatwg.org/#attr-fe-autofocus,
const url = "http://{{hosts[alt][www1]}}:{{ports[http][0]}}/feature-policy/experimental-features/resources/focus_steal.html"; // topDocument's autofocus processed flag initially is false and is set to true
// after flushing autofocus candidates, i.e. flush of autofocus candidates
let did_blur_ = false; // only happens once per page load.
function did_blur() { // In order to test the behaviour with both focus-without-user-activation on and off:
did_blur_ = true; // two test files are necessary:
} // - focus-without-user-activation-disabled-tentative.html
// - focus-without-user-activation-enabled-tentative.sub.html
function short_delay(test_instance) {
// Long enough to allow focus to propagate correctly. // Cross origin subframe should not be able to use autofocus to steal focus
const SHORT_DELAY = 400; // from main frame by default. However, with focus-without-user-activation
return new Promise( (r) => test_instance.step_timeout(r, SHORT_DELAY)); // enabled for subframe, subframe should be able to autofocus.
} const url = "http://{{hosts[alt][www1]}}:{{ports[http][0]}}/feature-policy/experimental-features/resources/focus-without-user-activation-iframe-tentative.html";
function reset_focus() { function subframe_focused(subframe, event_name, timeout) {
did_blur_ = false; return new Promise(resolve => {
document.querySelector("input").focus(); window.onmessage = m => resolve(m.data.focused);
} subframe.contentWindow.postMessage({
event: event_name,
promise_test( async (instance) => { timeout: timeout
const frame = createIframe(document.body, { }, "*");
sandbox: "allow-scripts allow-same-origin",
allow: "focus-without-user-activation 'none'",
src: url
}); });
await wait_for_load(frame); }
await short_delay(instance);
assert_false(did_blur_, "'autofocus' should not work.");
frame.contentWindow.postMessage("focus-input", "*");
await short_delay(instance);
assert_false(did_blur_, "'element.focus' should not work.");
frame.contentWindow.postMessage("focus-window", "*");
await short_delay(instance);
assert_false(did_blur_, "'window.focus' should not work.");
}, "When the policy is disabled, 'autofocus' and scripted focus do not focus " +
"the document.");
promise_test( async (instance) => { promise_test( async (instance) => {
const frame = createIframe(document.body, { const frame = createIframe(document.body, {
...@@ -55,19 +37,14 @@ ...@@ -55,19 +37,14 @@
allow: "focus-without-user-activation *", allow: "focus-without-user-activation *",
src: url src: url
}); });
await wait_for_load(frame);
await short_delay(instance);
reset_focus(); await wait_for_load(frame);
frame.contentWindow.postMessage("focus-input", "*"); assert_true(await subframe_focused(frame, "autofocus"), "'autofocus' should work.");
await short_delay(instance); window.focus(); // Reset focus state in subframe.
assert_true(did_blur_, "'element.focus' should work."); assert_true(await subframe_focused(frame, "focus-input"), "'element.focus' should work.");
did_blur_ = false; window.focus(); // Reset focus state in subframe.
assert_true(await subframe_focused(frame, "focus-window"), "'window.focus' should work.");
reset_focus(); window.focus(); // Reset focus state in subframe.
frame.contentWindow.postMessage("focus-window", "*");
await short_delay(instance);
assert_true(did_blur_, "'window.focus' should work.");
}, "When the policy is enabled, 'autofocus' and scripted focus do focus " + }, "When the policy is enabled, 'autofocus' and scripted focus do focus " +
"the document."); "the document.");
</script> </script>
......
<!doctype html>
<input autofocus onfocus="autofocus_onfocus()"/>
<script>
let autofocused = false;
function autofocus_onfocus() {
autofocused = true;
}
/**
* @param target object: Target to call |focus()| with.
* @param timeout integer | undefined: Timeout to wait for the focus event.
* If unspecified, a timeout will not be set.
* @param focus_target boolean | undefined: Wether to focus the target after
* listening for |onfocus| event.
*/
function wait_focus_event(target, timeout, focus_target) {
return new Promise((resolve) => {
if (timeout)
setTimeout(() => resolve(false), timeout);
target.onfocus = () => resolve(true);
if (focus_target)
target.focus();
});
}
function post_result(destination, result) {
destination.postMessage({focused: result}, "*");
}
window.addEventListener("message", (e) => {
if (e.data.event === "autofocus") {
if (autofocused)
post_result(e.source, true);
wait_focus_event(document.querySelector("input"), e.data.timeout)
.then(result => post_result(e.source, result));
} else if (e.data.event === "focus-window") {
wait_focus_event(window, e.data.timeout, true /* focus_target */)
.then(result => post_result(e.source, result));
} else if (e.data.event === "focus-input") {
const input_element = document.querySelector("input");
wait_focus_event(input_element, e.data.timeout, true /* focus_target */)
.then(result => post_result(e.source, result));
}
});
</script>
<!doctype html>
<input autofocus/>
<script>
window.addEventListener("message", (e) => {
if (e.data === "focus-window") {
window.focus();
}
else if (e.data === "focus-input") {
document.querySelector("input").focus();
}
});
</script>
...@@ -296,6 +296,7 @@ SET TIMEOUT: css/css-fonts/font-display/font-display-feature-policy-01.tentative ...@@ -296,6 +296,7 @@ SET TIMEOUT: css/css-fonts/font-display/font-display-feature-policy-01.tentative
SET TIMEOUT: css/css-fonts/font-display/font-display-feature-policy-02.tentative.html SET TIMEOUT: css/css-fonts/font-display/font-display-feature-policy-02.tentative.html
SET TIMEOUT: css/css-fonts/font-display/font-display-preload.html SET TIMEOUT: css/css-fonts/font-display/font-display-preload.html
SET TIMEOUT: document-policy/font-display/font-display-document-policy-01.tentative.html SET TIMEOUT: document-policy/font-display/font-display-document-policy-01.tentative.html
SET TIMEOUT: feature-policy/experimental-features/resources/focus-without-user-activation-iframe-tentative.html
SET TIMEOUT: html/browsers/windows/auxiliary-browsing-contexts/resources/close-opener.html SET TIMEOUT: html/browsers/windows/auxiliary-browsing-contexts/resources/close-opener.html
SET TIMEOUT: html/cross-origin-embedder-policy/resources/navigate-none.sub.html SET TIMEOUT: html/cross-origin-embedder-policy/resources/navigate-none.sub.html
SET TIMEOUT: html/cross-origin-embedder-policy/resources/navigate-require-corp.sub.html SET TIMEOUT: html/cross-origin-embedder-policy/resources/navigate-require-corp.sub.html
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment