Skip to content

Commit 1427e38

Browse files
Prerender WPT: add navigateExpectingNoPrerenderingActivation() helper (#52966)
This more clearly communicates the intent of these tests, and is a good counterpart to navigateExpectingPrerenderingActivation(). Also fix one more missing await in these tests. Bug: 40273170 Change-Id: I924a92534a6d139ef515787ad875ce80b21b712c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6620151 Reviewed-by: Hiroki Nakagawa <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1469158} Co-authored-by: Domenic Denicola <[email protected]>
1 parent d82b80f commit 1427e38

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

speculation-rules/prerender/cancel-prerendering-after-clear-site-data-cache-same-origin.tentative.https.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
// Because Clear-Site-Data response header is sent, the existing prerender
4949
// is expected to be canceled.
5050
// And the navigation is expected to create another page instead of activation.
51-
referrerRC.navigateTo(prerenderedRC.url);
52-
assert_equals(await getActivationStart(prerenderedRC), 0);
51+
await referrerRC.navigateExpectingNoPrerenderingActivation(prerenderedRC);
5352
}, "clear-site-data prerenderCache headers should prevent it from being activated");
5453

5554
// Test that Clear-Site-Data header value "cache" clears prerender cache
@@ -83,8 +82,7 @@
8382
// Because Clear-Site-Data response header is sent, the existing prerender
8483
// is expected to be canceled.
8584
// And the navigation is expected to create another page instead of activation.
86-
await referrerRC.navigateTo(prerenderedRC.url);
87-
assert_equals(await getActivationStart(prerenderedRC), 0);
85+
await referrerRC.navigateExpectingNoPrerenderingActivation(prerenderedRC);
8886
}, "clear-site-data cache headers should prevent it from being activated");
8987
</script>
9088
</body>

speculation-rules/prerender/credentialed-prerender-not-opt-in.https.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
// Because the prerender doesn't use opt-in header, it is expected to be canceled.
2323
// And the navigation is expected to create another page instead of activation.
24-
await referrerRC.navigateTo(prerenderedRC.url);
25-
assert_equals(await getActivationStart(prerenderedRC), 0);
24+
await referrerRC.navigateExpectingNoPrerenderingActivation(prerenderedRC);
2625
});
2726
</script>

speculation-rules/prerender/resources/utils.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,40 @@ if (globalThis.PreloadingRemoteContextHelper) {
425425
);
426426
}
427427

428+
/**
429+
* Navigates to the URL identified by `destinationRC`, but expects that the
430+
* navigation does not cause a prerendering activation. (E.g., because the
431+
* prerender was canceled by something in the test code.) If the navigation
432+
* results in a prerendering activation, the returned promise will be
433+
* rejected with a testharness.js AssertionError.
434+
* @param {RemoteContextWrapper} destinationRC - The `RemoteContextWrapper`
435+
* pointing to the destination URL. Usually this is obtained by
436+
* prerendering (e.g., via `addPrerender()`), even though we are testing
437+
* that the prerendering does not activate.
438+
* @param {(string) => Promise<undefined>} [navigateFn] - An optional
439+
* function to customize the navigation. It will be passed the URL of the
440+
* prerendered content, and will run as a script in this (see
441+
* `RemoteContextWrapper.prototype.executeScript`). If not given,
442+
* navigation will be done via the `location.href` setter (see
443+
* `RemoteContextWrapper.prototype.navigateTo`).
444+
* @returns {Promise<undefined>}
445+
*/
446+
async navigateExpectingNoPrerenderingActivation(destinationRC, navigateFn) {
447+
if (navigateFn === undefined) {
448+
await this.navigateTo(destinationRC.url);
449+
} else {
450+
await this.navigate(navigateFn, [destinationRC.url]);
451+
}
452+
453+
assert_equals(
454+
await destinationRC.executeScript(() => {
455+
return performance.getEntriesByType("navigation")[0].activationStart;
456+
}),
457+
0,
458+
"The prerendered page must not be activated."
459+
);
460+
}
461+
428462
/**
429463
* Starts prerendering a page with this `PreloadingRemoteContextWrapper` as the
430464
* referrer, using `<script type="speculationrules">`.
@@ -445,13 +479,6 @@ if (globalThis.PreloadingRemoteContextHelper) {
445479
};
446480
}
447481

448-
async function getActivationStart(prerenderedRC) {
449-
return await prerenderedRC.executeScript(() => {
450-
const entry = performance.getEntriesByType("navigation")[0];
451-
return entry.activationStart;
452-
});
453-
}
454-
455482
// Used by the opened window, to tell the main test runner to terminate a
456483
// failed test.
457484
function failTest(reason, uid) {

0 commit comments

Comments
 (0)