Skip to content

Commit

Permalink
Prerender: Add WPT infra to trigger prerendering with target hint
Browse files Browse the repository at this point in the history
This CL adds WPT test helpers to trigger prerendering with target hint
"_blank" (i.e., prerender-into-new-tab), and runs an existing tests as a
reference. Follow-up CLs will run more existing tests.

Note that this CL doesn't add test helpers to activate a prerendered
page on a new tab yet so that we cannot test deferral-until-activation
cases yet.

Bug: 1501674
Change-Id: Ia58c1c5f3800843083ada383588a938ba12cc8ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5020542
Reviewed-by: Lingqi Chi <[email protected]>
Commit-Queue: Hiroki Nakagawa <[email protected]>
Reviewed-by: Kent Tamura <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1223465}
  • Loading branch information
nhiroki authored and chromium-wpt-export-bot committed Nov 13, 2023
1 parent 7eaf605 commit 9b9cd6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions speculation-rules/prerender/resources/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const STORE_URL = '/speculation-rules/prerender/resources/key-value-store.py';

// Starts prerendering for `url`.
function startPrerendering(url) {
//
// `rule_extras` provides additional parameters for the speculation rule used
// to trigger prerendering.
function startPrerendering(url, rule_extras = {}) {
// Adds <script type="speculationrules"> and specifies a prerender candidate
// for the given URL.
// TODO(https://crbug.com/1174978): <script type="speculationrules"> may not
// start prerendering for some reason (e.g., resource limit). Implement a
// WebDriver API to force prerendering.
const script = document.createElement('script');
script.type = 'speculationrules';
script.text = `{"prerender": [{"source": "list", "urls": ["${url}"] }] }`;
script.text = JSON.stringify(
{prerender: [{source: 'list', urls: [url], ...rule_extras}]});
document.head.appendChild(script);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!DOCTYPE html>
<title>Prerendering cannot invoke the Screen Capture API</title>
<meta name="variant" content="?target_hint=_self">
<meta name="variant" content="?target_hint=_blank">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
Expand All @@ -11,6 +13,10 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('prerender-channel', uid);
Expand All @@ -26,7 +32,8 @@
// Start prerendering a page that attempts to invoke the Screen Capture API.
// This API is activated-gated so it's expected to fail:
// https://wicg.github.io/nav-speculation/prerendering.html#implicitly-restricted
startPrerendering(`resources/screen-capture.https.html?uid=${uid}`);
startPrerendering(
`resources/screen-capture.https.html?uid=${uid}`, rule_extras);
const result = await gotMessage;
assert_equals(result, 'InvalidStateError');
bc.close();
Expand Down

0 comments on commit 9b9cd6f

Please sign in to comment.