Prerender: upstream wpt_internal/prerender/restriction-focus.html

The behavior is described in
https://jeremyroman.github.io/alternate-loading-modes/prerendering.html#interaction-with-system-focus

Bug: 1253158
Change-Id: I292298355d0880160bf78837ed0a8413585f3078
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3229409
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Lingqi Chi <lingqi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#932946}
diff --git a/speculation-rules/prerender/resources/prerender-focus.html b/speculation-rules/prerender/resources/prerender-focus.html
new file mode 100644
index 0000000..f6a7953
--- /dev/null
+++ b/speculation-rules/prerender/resources/prerender-focus.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<title>Check the focus state of prerendering pages</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<body>
+<input type="text" id = "prerenderTextField">
+<script>
+
+// Check this page is being prerendered.
+assert_true(document.prerendering);
+
+const bc = new BroadcastChannel('prerender-channel');
+
+// Try to set focus.
+const element = document.getElementById('prerenderTextField');
+element.focus();
+
+// Post the focus and active states to the initiator page.
+bc.postMessage({
+  activeElementUpdated: document.activeElement === element,
+  documentHasFocus: document.hasFocus()});
+bc.close();
+
+</script>
+</body>
diff --git a/speculation-rules/prerender/restriction-focus.html b/speculation-rules/prerender/restriction-focus.html
new file mode 100644
index 0000000..b6576eb
--- /dev/null
+++ b/speculation-rules/prerender/restriction-focus.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<title>Prerendering documents are not focused</title>
+<meta name="timeout" content="long">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/utils.js"></script>
+<body>
+<input type="text" id = "prerenderTextField">
+<script>
+
+setup(() => assertSpeculationRulesIsSupported());
+
+promise_test(async t => {
+  const bc = new BroadcastChannel('prerender-channel');
+
+  document.getElementById('prerenderTextField').focus();
+  assert_true(
+      document.hasFocus(),
+      'Initial document should have focus.');
+
+  const gotMessage = new Promise(resolve => {
+    bc.addEventListener('message', e => {
+      resolve(e.data);
+    }, {once:true});
+  });
+
+  // Start prerendering a page that accesses the focus state of the page.
+  startPrerendering(`resources/prerender-focus.html`);
+
+  const result = await gotMessage;
+  assert_true(result.activeElementUpdated);
+  assert_false(result.documentHasFocus);
+  bc.close();
+}, 'Prerendering document should update the active element but not have focus');
+
+</script>
+</body>