Skip to content

Commit fdcb8ff

Browse files
Nick Burrisnatechapin
authored andcommitted
Create Web Platform Tests for Scroll to text
Add a web platform test that performs navigations to a test page with various targetText parameters and checks if the target page successfully scrolled as expected. Using a BroadcastChannel is the only way to communicate whether the target page scrolled, since scroll to text is specifically restricted from iframes or pages with an opener, so there's no other way for the test page to track the status of the child target page. Bug: 994299 Change-Id: I69243e739c3a7469ac48647508e379f204ccfbf6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1756707 Commit-Queue: Nick Burris <[email protected]> Reviewed-by: Robert Ma <[email protected]> Cr-Commit-Position: refs/heads/master@{#689453}
1 parent 2a61022 commit fdcb8ff

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<title>Navigating to a text fragment anchor</title>
3+
<script>
4+
function checkScroll() {
5+
let bc = new BroadcastChannel('scroll-to-text-fragment');
6+
bc.postMessage({ didScrollToTarget: window.scrollY > 0 });
7+
bc.close();
8+
window.close();
9+
}
10+
</script>
11+
<style>
12+
body {
13+
height: 3200px;
14+
}
15+
p {
16+
position: absolute;
17+
top: 3000px;
18+
}
19+
</style>
20+
<body onload="checkScroll()">
21+
<p id="text">This is a test page</p>
22+
</body>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!doctype html>
2+
<title>Navigating to a text fragment anchor</title>
3+
<meta name="timeout" content="long">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="/resources/testdriver.js"></script>
7+
<script src="/resources/testdriver-vendor.js"></script>
8+
<script>
9+
let test_cases = [
10+
{ fragment: '#', expect_scroll: false },
11+
{ fragment: '##targetText=test', expect_scroll: true },
12+
{ fragment: '##targetText=this,page', expect_scroll: true },
13+
{ fragment: '##targetText=this-,is,test', expect_scroll: true },
14+
{ fragment: '##targetText=this-,is,test,-page', expect_scroll: true },
15+
{ fragment: '##targetText=this-,is,page,-none', expect_scroll: false },
16+
{ fragment: '##targetText=this,test,-page', expect_scroll: true },
17+
{ fragment: '##targetText=this%20is%20a%20test%20page', expect_scroll: true },
18+
{ fragment: '##targetText=this&targetText=test,page', expect_scroll: true },
19+
{ fragment: '#pagestate##targetText=test', expect_scroll: true },
20+
{ fragment: '#pagestate##targetText=nomatch', expect_scroll: false },
21+
];
22+
23+
for (const test_case of test_cases) {
24+
promise_test(t => new Promise(resolve => {
25+
let channel = new BroadcastChannel('scroll-to-text-fragment');
26+
channel.addEventListener("message", e => {
27+
resolve(e.data.didScrollToTarget);
28+
}, {once: true});
29+
30+
test_driver.bless('Open a URL with a text fragment anchor', () => {
31+
window.open('scroll-to-text-fragment-target.html' + test_case.fragment, '_blank', 'noopener');
32+
});
33+
}).then(scroll => {
34+
assert_equals(scroll, test_case.expect_scroll,
35+
'Expected ' + test_case.fragment + (test_case.expect_scroll ? ' to scroll.' : ' to not scroll.'));
36+
}), 'Test navigation with text fragment anchor ' + test_case.fragment);
37+
}
38+
</script>

0 commit comments

Comments
 (0)