LazyLoad: Add test for deferred iframes using attributes at parse time

This CL adds tests for base URL and referrer-policy state at parse time
are retained when the deferred frame is loaded-in.

Bug: 984983
Change-Id: Iaea240cf54b6b4e6977c129fcea971051d5900d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1708293
Commit-Queue: rajendrant <rajendrant@chromium.org>
Reviewed-by: Dominic Farolino <dom@chromium.org>
Cr-Commit-Position: refs/heads/master@{#679460}
diff --git a/loading/lazyload/common.js b/loading/lazyload/common.js
new file mode 100644
index 0000000..f5013fc
--- /dev/null
+++ b/loading/lazyload/common.js
@@ -0,0 +1,13 @@
+// Helper to access the element, its associated loading promise, and also to
+// resolve the promise.
+class ElementLoadPromise {
+  constructor(element_id) {
+    this.element_id = element_id;
+    this.promise = new Promise(resolve => {
+      this.resolve = resolve
+    });
+  }
+  element() {
+    return document.getElementById(this.element_id);
+  }
+}
diff --git a/loading/lazyload/original-base-url-applied-tentative.html b/loading/lazyload/original-base-url-applied-tentative.html
new file mode 100644
index 0000000..06f9c25
--- /dev/null
+++ b/loading/lazyload/original-base-url-applied-tentative.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<head>
+  <title>Deferred iframes and images with loading='lazy' use the original base URL specified at the parse time</title>
+  <link rel="author" title="Raj T" href="mailto:rajendrant@chromium.org">
+  <script src="/resources/testharness.js"></script>
+  <script src="/resources/testharnessreport.js"></script>
+  <script src="common.js"></script>
+</head>
+
+<!--
+Marked as tentative until https://github.com/whatwg/html/pull/3752 is landed.
+-->
+
+<script>
+  const below_viewport_iframe = new ElementLoadPromise("below_viewport_iframe");
+
+  // Change the base URL and scroll down to load the deferred elements.
+  window.addEventListener("load", () => {
+    window.history.pushState(1, document.title, '/invalid-url-where-no-subresources-exist/')
+    below_viewport_iframe.element().scrollIntoView();
+  });
+
+  async_test(function(t) {
+    below_viewport_iframe.promise.then(
+      t.step_func_done(function() {
+        assert_true(below_viewport_iframe.element().contentDocument.body.innerHTML.includes("<p>Subframe</p>"));
+      }));
+  }, "Test that when deferred iframe is loaded, it uses the base URL computed at parse time.");
+</script>
+
+<body>
+  <div style="height:10000px;"></div>
+  <script>
+    // Change the base URL so that the iframe makes use of that in its relative
+    // URL to absolute URL computation at parse time.
+    window.history.pushState(1, document.title, 'resources/')
+  </script>
+  <iframe id="below_viewport_iframe" src="subframe.html" loading="lazy" width="200px" height="100px" onload="below_viewport_iframe.resolve();">
+  </iframe>
+</body>
diff --git a/loading/lazyload/original-referrer-policy-applied-tentative.sub.html b/loading/lazyload/original-referrer-policy-applied-tentative.sub.html
new file mode 100644
index 0000000..2ffbc82
--- /dev/null
+++ b/loading/lazyload/original-referrer-policy-applied-tentative.sub.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<head>
+  <title>Deferred iframes and images with loading='lazy' use the original referrer-policy specified at the parse time</title>
+  <link rel="author" title="Raj T" href="mailto:rajendrant@chromium.org">
+  <link rel="help" href="https://github.com/scott-little/lazyload">
+  <script src="/resources/testharness.js"></script>
+  <script src="/resources/testharnessreport.js"></script>
+  <script src="common.js"></script>
+</head>
+
+<!--
+Marked as tentative until https://github.com/whatwg/html/pull/3752 is landed.
+-->
+
+<script>
+  const below_viewport_iframe = new ElementLoadPromise("below_viewport_iframe");
+
+  // Change the referrer-policy and scroll down to load the deferred elements.
+  window.addEventListener("load", () => {
+    below_viewport_iframe.element().referrerPolicy = "no-referrer";
+    document.getElementById("below_viewport_iframe").scrollIntoView();
+  });
+
+  async_test(function(t) {
+    below_viewport_iframe.promise.then(
+      t.step_func_done(function() {
+        // The referer header should be the full URL (as specified in the iframe
+        // at parse time), and not the origin (as specified in meta referrer
+        // tag) or null (as overridden by iframe referrerpolicy=no-referrer).
+        assert_true(below_viewport_iframe.element().contentDocument.body.innerHTML
+            .includes("Referer: http://{{host}}:{{ports[http][0]}}/loading/lazyload/"));
+      }));
+  }, "Test that when deferred iframe is loaded, it uses the referrer-policy specified at parse time.");
+</script>
+
+<body>
+  <meta name="referrer" content="origin">
+  <div style="height:10000px;"></div>
+  <iframe id="below_viewport_iframe" src="/xhr/resources/echo-headers.py" loading="lazy" width="200px" height="100px" referrerpolicy="unsafe-url" onload="below_viewport_iframe.resolve();">
+  </iframe>
+</body>