Commit 22fced22 authored by David Bokan's avatar David Bokan Committed by Commit Bot

Add WPT for ForceLoadAtTop DocumentPolicy

DocumentPolicy has been enabled in M86 so we want to ship the
ForceLoadAtTop policy soon. This CL adds a web platform test to ensure
its correct operation.

Bug: 1047900
Change-Id: I127331aece02d5b23b77941f5cc2958c81a63315
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2342344Reviewed-by: default avatarNick Burris <nburris@chromium.org>
Commit-Queue: David Bokan <bokan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795943}
parent 6d0df2ee
<!doctype html>
<title>Navigating to a text fragment anchor</title>
<script src="stash.js"></script>
<script src="force-load-at-top.js"></script>
<style>
p#target {
margin: 2000px 0px 2000px 0px;
}
</style>
<!-- This page is loaded with the force-load-at-top Document-Policy header -->
<body>
<p>Top of page</p>
<p id="target">target</p>
<p id="history">history</p>
</body>
<!doctype html>
<title>ForceLoadAtTop blocks scroll on load</title>
<meta charset=utf-8>
<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/common/utils.js"></script>
<script src="stash.js"></script>
<!--See comment in scroll-to-text-fragment.html for why this test has the
structure it has -->
<script>
let test_cases = [
{
fragment: '#:~:text=target',
type: 'text fragment'
},
{
fragment: '#target:~:text=target',
type: 'text fragment with element fallback'
},
{
fragment: '#target',
type: 'element fragment'
},
{
fragment: '#history',
type: 'history scroll restoration'
},
];
let document_policy_value = [
'force-load-at-top',
'no-force-load-at-top'
];
for (const value of document_policy_value) {
// If no-force-load-at-top is specified we expect to allow scrolling,
// otherwise scroll on load should be blocked.
const scroll_expected = value == 'no-force-load-at-top';
const block_verb = scroll_expected ? "must not" : "must";
for (const test_case of test_cases) {
promise_test(t => new Promise((resolve, reject) => {
let key = token();
test_driver.bless('Open a URL with a text fragment directive', () => {
window.open(`${value}-target.html?key=${key}${test_case.fragment}`, '_blank', 'noopener');
});
fetchResults(key, resolve, reject);
}).then(data => {
assert_equals(data.scrolled, scroll_expected);
}), `${value} ${block_verb} block scroll on load from ${test_case.type}.`);
}
}
</script>
function checkScroll() {
// Ensure two animation frames on load to test the fallback to element
// anchor, which gets queued for the next frame if the text fragment is not
// found.
requestAnimationFrame(() => {
requestAnimationFrame(() => {
let results = {
scrolled: (window.pageYOffset != 0),
};
let key = (new URL(document.location)).searchParams.get("key");
stashResultsThenClose(key, results);
});
});
}
window.addEventListener('load', () => {
if (location.hash == "#history") {
// This is the "history" test - on the first load we'll navigate to a page
// that calls history.back(). When we load a second time (from the back
// navigation), record the scroll state at that point to check how history
// scroll restoration is handled.
if (history.state == null) {
history.pushState("test", "test", "");
requestAnimationFrame(() => {
location.href = "navigate-back.html";
});
return;
}
}
checkScroll();
});
<!doctype html>
<script>
onload = () => {
requestAnimationFrame(() => {
history.back();
});
};
</script>
<!doctype html>
<title>Navigating to a text fragment anchor</title>
<script src="stash.js"></script>
<script src="force-load-at-top.js"></script>
<style>
p#target {
margin: 2000px 0px 2000px 0px;
}
</style>
<!-- This page is loaded with the no-force-load-at-top Document-Policy header -->
<body>
<p>Top of page</p>
<p id="target">target</p>
<p id="history">history</p>
</body>
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