Skip to content

Commit

Permalink
Prerender: upstream wpt_internal/prerender/restriction-focus.html
Browse files Browse the repository at this point in the history
The behavior is described in
https://jeremyroman.github.io/alternate-loading-modes/prerendering.html#interaction-with-system-focus

Bug: 1253158
Change-Id: I292298355d0880160bf78837ed0a8413585f3078
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3229409
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Lingqi Chi <lingqi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#932946}
  • Loading branch information
Clqsin45 authored and chromium-wpt-export-bot committed Oct 19, 2021
1 parent 8777fe5 commit 679cfd4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions speculation-rules/prerender/resources/prerender-focus.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<title>Check the focus state of prerendering pages</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<input type="text" id = "prerenderTextField">
<script>

// Check this page is being prerendered.
assert_true(document.prerendering);

const bc = new BroadcastChannel('prerender-channel');

// Try to set focus.
const element = document.getElementById('prerenderTextField');
element.focus();

// Post the focus and active states to the initiator page.
bc.postMessage({
activeElementUpdated: document.activeElement === element,
documentHasFocus: document.hasFocus()});
bc.close();

</script>
</body>
37 changes: 37 additions & 0 deletions speculation-rules/prerender/restriction-focus.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<title>Prerendering documents are not focused</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
<body>
<input type="text" id = "prerenderTextField">
<script>

setup(() => assertSpeculationRulesIsSupported());

promise_test(async t => {
const bc = new BroadcastChannel('prerender-channel');

document.getElementById('prerenderTextField').focus();
assert_true(
document.hasFocus(),
'Initial document should have focus.');

const gotMessage = new Promise(resolve => {
bc.addEventListener('message', e => {
resolve(e.data);
}, {once:true});
});

// Start prerendering a page that accesses the focus state of the page.
startPrerendering(`resources/prerender-focus.html`);

const result = await gotMessage;
assert_true(result.activeElementUpdated);
assert_false(result.documentHasFocus);
bc.close();
}, 'Prerendering document should update the active element but not have focus');

</script>
</body>

0 comments on commit 679cfd4

Please sign in to comment.