[WPT] Add incumbent/entry/relevant realm tests for Location/window.open

Based on Chromium layout tests.

Current test failures:
The settings object used for `document.referrer`
(=navigation request's client) are:

- The incumbent settings for `window.open()` on Chrome.
- The `Location`'s relevant for `location.assign()` on Safari.

Change-Id: I6b50f886985fb21b4e8a1f7138744e1cce205534
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3646085
Reviewed-by: Domenic Denicola <domenic@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1005006}
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location-assign.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location-assign.html
new file mode 100644
index 0000000..cb2984d
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location-assign.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/context-helper.js"></script>
+<script>
+window.scriptToRun = 'relevantWindow.location.assign("target.html");';
+
+async_test(t => {
+  window.addEventListener("message", t.step_func_done(function(e) {
+    // Base URL used for parsing a relative URL to `target.html`
+    // should be the base URL of the entry settings object in
+    // https://html.spec.whatwg.org/C/#dom-location-assign
+    assert_equals(
+        e.data.location,
+        new URL('target.html', entryUrl).href,
+        'Base URL should use the entry settings object');
+
+    // `document.referrer` should reflect the source browsing context,
+    // which is the incumbent in
+    // https://html.spec.whatwg.org/C/#location-object-navigate
+    assert_equals(
+        e.data.referrer, incumbentUrl,
+        'Referrer should use the incumbent settings object');
+  }));
+}, 'Fetch client and URL resolution for location.assign()');
+</script>
+<iframe id="entry" src="entry/entry.html"></iframe>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location-href.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location-href.html
new file mode 100644
index 0000000..02ff214
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location-href.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/context-helper.js"></script>
+<script>
+window.scriptToRun = 'relevantWindow.location.href = "target.html";';
+
+async_test(t => {
+  window.addEventListener("message", t.step_func_done(function(e) {
+    // Base URL used for parsing a relative URL to `target.html`
+    // should be the base URL of the entry settings object in
+    // https://html.spec.whatwg.org/C/#dom-location-href
+    assert_equals(
+        e.data.location,
+        new URL('target.html', entryUrl).href,
+        'Base URL should use the entry settings object');
+
+    // `document.referrer` should reflect the source browsing context,
+    // which is the incumbent in
+    // https://html.spec.whatwg.org/C/#location-object-navigate
+    assert_equals(
+        e.data.referrer, incumbentUrl,
+        'Referrer should use the incumbent settings object');
+  }));
+}, 'Fetch client and URL resolution for location.href setter');
+</script>
+<iframe id="entry" src="entry/entry.html"></iframe>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location.html
new file mode 100644
index 0000000..fae17dd
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-location.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/context-helper.js"></script>
+<script>
+window.scriptToRun = 'relevantWindow.location = "target.html";';
+
+async_test(t => {
+  window.addEventListener("message", t.step_func_done(function(e) {
+    // Base URL used for parsing a relative URL to `target.html`
+    // should be the base URL of the entry settings object in
+    // https://html.spec.whatwg.org/C/#dom-location-assign
+    assert_equals(
+        e.data.location,
+        new URL('target.html', entryUrl).href,
+        'Base URL should use the entry settings object');
+
+    // `document.referrer` should reflect the source browsing context,
+    // which is the incumbent in
+    // https://html.spec.whatwg.org/C/#location-object-navigate
+    assert_equals(
+        e.data.referrer, incumbentUrl,
+        'Referrer should use the incumbent settings object');
+  }));
+}, 'Fetch client and URL resolution for location setter');
+</script>
+<iframe id="entry" src="entry/entry.html"></iframe>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-window-open.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-window-open.html
new file mode 100644
index 0000000..0a391ef
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/context-for-window-open.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/context-helper.js"></script>
+<script>
+window.scriptToRun =
+    'relevantWindow.open("target.html", "target");';
+
+async_test(t => {
+  window.addEventListener("message", t.step_func_done(function(e) {
+    // Base URL used for parsing a relative URL to `target.html`
+    // should be the base URL of the entry settings object in
+    // https://html.spec.whatwg.org/C/#window-open-steps
+    assert_equals(
+        e.data.location,
+        new URL('target.html', entryUrl).href,
+        'Base URL should use the entry settings object');
+
+    // `document.referrer` should reflect the source browsing context,
+    // which is the entry in
+    // https://html.spec.whatwg.org/C/#window-open-steps
+    assert_equals(
+        e.data.referrer, entryUrl,
+       'Referrer should use the entry settings object');
+  }));
+}, 'Fetch client and URL resolution for window.open()');
+</script>
+<iframe id="entry" src="entry/entry.html"></iframe>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/entry/entry.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/entry/entry.html
new file mode 100644
index 0000000..82fecfd
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/entry/entry.html
@@ -0,0 +1,4 @@
+<body onload="top.go()">
+<iframe id="incumbent" src="../incumbent/empty.html"></iframe>
+<iframe id="relevant" src="../relevant/empty.html"></iframe>
+</body>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/entry/target.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/entry/target.html
new file mode 100644
index 0000000..5ceaeaf
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/entry/target.html
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<script src="../resources/target.js"></script>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/incumbent/empty.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/incumbent/empty.html
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/incumbent/empty.html
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/incumbent/target.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/incumbent/target.html
new file mode 100644
index 0000000..5ceaeaf
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/incumbent/target.html
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<script src="../resources/target.js"></script>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/relevant/empty.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/relevant/empty.html
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/relevant/empty.html
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/relevant/target.html b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/relevant/target.html
new file mode 100644
index 0000000..5ceaeaf
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/relevant/target.html
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<script src="../resources/target.js"></script>
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/resources/context-helper.js b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/resources/context-helper.js
new file mode 100644
index 0000000..dda338b
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/resources/context-helper.js
@@ -0,0 +1,34 @@
+// Usage: in the top-level Window, include:
+//
+// <script src="resources/context-helper.js"></script>
+// <script>
+//   window.scriptToRun = '...';
+// </script>
+// <iframe id="entry" src="entry/entry.html"></iframe>
+//
+// Then `scriptToRun` is evaluated, with:
+// - The entry Realm is that of entry/entry.html
+// - The incumbent Realm is that of incumbent/empty.html
+// - The relevant Realm of `relevantWindow`, `relevantWindow.location` etc. is
+//   that of relevant/empty.html
+
+window.scriptToRun = '';
+
+const entryUrl = new URL('entry/entry.html', location).href;
+const incumbentUrl = new URL('incumbent/empty.html', location).href;
+const relevantUrl = new URL('relevant/empty.html', location).href;
+
+function go() {
+    const entry = document.querySelector('#entry');
+    const incumbent = entry.contentDocument.querySelector('#incumbent');
+    const incumbentScript = incumbent.contentDocument.createElement('script');
+    incumbentScript.textContent = `
+    function go() {
+      const relevantWindow =
+          parent.document.querySelector('#relevant').contentWindow;
+      ${window.scriptToRun}
+    }
+    `;
+    incumbent.contentDocument.head.appendChild(incumbentScript);
+    incumbent.contentWindow.go();
+}
diff --git a/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/resources/target.js b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/resources/target.js
new file mode 100644
index 0000000..e3a507b
--- /dev/null
+++ b/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/resources/target.js
@@ -0,0 +1,11 @@
+window.onload = function() {
+    let testWindow;
+    if (opener) {
+      testWindow = opener.top;
+    } else {
+      testWindow = top;
+    }
+    testWindow.postMessage(
+        {location: location.href, referrer: document.referrer},
+        "*");
+}