Skip to content

Commit e2c4c40

Browse files
Yoav Weisschromium-wpt-export-bot
Yoav Weiss
authored andcommitted
[soft navigations] Enable keyboard shortcuts as soft navigation triggers
Following the discussion on issue #3 [1], this CL adds support to soft navigations triggered by keyboard shortcuts, by adding unfocused keydown events to the events that can trigger the soft navigation heuristic. [1] WICG/soft-navigations#3 Bug: 1478772 Change-Id: Ib423a3cfc09eaf4dd9a2221b3494ab1016fa8668
1 parent c933272 commit e2c4c40

4 files changed

+54
-17
lines changed

soft-navigation-heuristics/image-lcp-followed-by-two-image-softnavs-lcp.tentative.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
const first_click_paint_promise = waitOnPaintEntriesPromise();
3333

34-
click(link);
34+
interact(link);
3535
await new Promise(resolve => {
3636
(new PerformanceObserver(resolve)).observe({
3737
type: 'soft-navigation'
@@ -54,7 +54,7 @@
5454

5555
const second_click_paint_promise = waitOnPaintEntriesPromise();
5656
const preClickLcp2 = await getLcpEntries();
57-
click(link);
57+
interact(link);
5858
await new Promise(resolve => {
5959
(new PerformanceObserver(() => resolve())).observe({
6060
type: 'soft-navigation'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Detect hashchange event.</title>
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
<script src="/resources/testdriver.js"></script>
9+
<script src="/resources/testdriver-vendor.js"></script>
10+
<script src="resources/soft-navigation-helper.js"></script>
11+
</head>
12+
<!-- Not including a body element to ensure the keydown heuristic works on
13+
malformed HTML -->
14+
<main id=main>
15+
<div>
16+
First LCP!
17+
</div>
18+
</main>
19+
<script>
20+
testSoftNavigation({
21+
addContent: () => {
22+
addTextToDivOnMain();
23+
},
24+
link: document.body,
25+
interactionType: "keydown",
26+
eventType: "keydown",
27+
test: "Keydown on body triggers SoftNavigationHeuristics"});
28+
</script>
29+
</html>
30+

soft-navigation-heuristics/navigate-child.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
await new Promise(r => t.step_timeout(r, 10));
2121
}
2222
const link = document.getElementById("link");
23-
click(link);
23+
interact(link);
2424
while (!child.location.href.includes("2")) {
2525
await new Promise(r => t.step_timeout(r, 10));
2626
}

soft-navigation-heuristics/resources/soft-navigation-helper.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var counter = 0;
2-
var clicked;
2+
var interacted;
33
var timestamps = []
44
const MAX_CLICKS = 50;
55
// Entries for one hard navigation + 50 soft navigations.
@@ -20,6 +20,7 @@ const testSoftNavigation =
2020
const testName = options.testName;
2121
const pushUrl = readValue(options.pushUrl, true);
2222
const eventType = readValue(options.eventType, "click");
23+
const interactionType = readValue(options.interactionType, 'click');
2324
const expectLCP = options.validate != 'no-lcp';
2425
promise_test(async t => {
2526
await waitInitialLCP();
@@ -29,8 +30,8 @@ const testSoftNavigation =
2930
const firstClick = (i === 0);
3031
let paint_entries_promise =
3132
waitOnPaintEntriesPromise(expectLCP && firstClick);
32-
clicked = false;
33-
click(link);
33+
interacted = false;
34+
interact(link, interactionType);
3435

3536
await new Promise(resolve => {
3637
(new PerformanceObserver(() => resolve())).observe({
@@ -61,7 +62,7 @@ const testNavigationApi = (testName, navigateEventHandler, link) => {
6162
await waitInitialLCP();
6263
const preClickLcp = await getLcpEntries();
6364
let paint_entries_promise = waitOnPaintEntriesPromise();
64-
click(link);
65+
interact(link);
6566
await new Promise(resolve => {
6667
(new PerformanceObserver(() => resolve())).observe({
6768
type: 'soft-navigation'
@@ -80,7 +81,7 @@ const testSoftNavigationNotDetected = options => {
8081
promise_test(async t => {
8182
const preClickLcp = await getLcpEntries();
8283
options.eventTarget.addEventListener(options.eventName, options.eventHandler);
83-
click(options.link);
84+
interact(options.link);
8485
await new Promise((resolve, reject) => {
8586
(new PerformanceObserver(() =>
8687
reject("Soft navigation should not be triggered"))).observe({
@@ -128,15 +129,21 @@ const runEntryValidations =
128129
}
129130
};
130131

131-
const click = link => {
132-
if (test_driver) {
133-
test_driver.click(link);
134-
timestamps[counter] = {"syncPostClick": performance.now()};
135-
}
136-
}
132+
const interact =
133+
(link, interactionType = 'click') => {
134+
if (test_driver) {
135+
if (interactionType == 'click') {
136+
test_driver.click(link);
137+
} else {
138+
test_driver.send_keys(link, 'j');
139+
}
140+
timestamps[counter] = {"syncPostInteraction": performance.now()};
141+
}
142+
}
137143

138144
const setEvent = (t, button, pushState, addContent, pushUrl, eventType) => {
139-
const eventObject = (eventType == "click") ? button : window;
145+
const eventObject =
146+
(eventType == 'click' || eventType == 'keydown') ? button : window;
140147
eventObject.addEventListener(eventType, async e => {
141148
timestamps[counter]["eventStart"] = performance.now();
142149
// Jump through a task, to ensure task tracking is working properly.
@@ -158,7 +165,7 @@ const setEvent = (t, button, pushState, addContent, pushUrl, eventType) => {
158165
await addContent(url);
159166
++counter;
160167

161-
clicked = true;
168+
interacted = true;
162169
});
163170
};
164171

@@ -178,7 +185,7 @@ const validateSoftNavigationEntry = async (clicks, extraValidations,
178185
assert_true(entry.name.includes(pushUrl ? URL : document.location.href),
179186
"The soft navigation name is properly set");
180187
const entryTimestamp = entry.startTime;
181-
assert_less_than_equal(timestamps[i]["syncPostClick"], entryTimestamp);
188+
assert_less_than_equal(timestamps[i]["syncPostInteraction"], entryTimestamp);
182189
assert_greater_than_equal(
183190
timestamps[i]['eventStart'], entryTimestamp,
184191
'Event start timestamp matches');

0 commit comments

Comments
 (0)