Prerender: Add WPT infra to trigger prerendering with target hint

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 <lingqi@chromium.org>
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1223465}
diff --git a/speculation-rules/prerender/resources/utils.js b/speculation-rules/prerender/resources/utils.js
index 311d37f..62b3b32 100644
--- a/speculation-rules/prerender/resources/utils.js
+++ b/speculation-rules/prerender/resources/utils.js
@@ -1,7 +1,10 @@
 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
@@ -9,7 +12,8 @@
   // 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);
 }
 
diff --git a/speculation-rules/prerender/restriction-screen-capture.https.html b/speculation-rules/prerender/restriction-screen-capture.https.html
index 3ef5298..4390522 100644
--- a/speculation-rules/prerender/restriction-screen-capture.https.html
+++ b/speculation-rules/prerender/restriction-screen-capture.https.html
@@ -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>
@@ -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);
@@ -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();