Prerender: Upstream tests for window.move() and window.resize()

This CL upstreams tests that make sure that window.move() and
window.resize() in prerendered pages are ignored.

Spec discussion:
https://github.com/jeremyroman/alternate-loading-modes/issues/73

Bug: 1253158
Change-Id: Ia34a18f5e364649d4b63eb10cfe07dc1aee7de22
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3223702
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Reviewed-by: Lingqi Chi <lingqi@chromium.org>
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/main@{#931827}
diff --git a/speculation-rules/prerender/resources/window-move.html b/speculation-rules/prerender/resources/window-move.html
new file mode 100644
index 0000000..bccaec5
--- /dev/null
+++ b/speculation-rules/prerender/resources/window-move.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="utils.js"></script>
+<script src="deferred-promise-utils.js"></script>
+<script>
+
+const params = new URLSearchParams(location.search);
+
+// The main test page (restriction-window-move.html) loads the
+// initiator page, then the initiator page will prerender itself with the
+// `prerendering` parameter.
+const isPrerendering = params.has('prerendering');
+
+function tryRun(func) {
+  try {
+    func();
+  } catch (e) {
+    const testChannel = new BroadcastChannel('test-channel');
+    testChannel.postMessage({status: 'FAIL: ' + e});
+  }
+}
+
+if (!isPrerendering) {
+  // Ensure that the primary page can move this window.
+  tryRun(() => {
+    const expectedPosition = {x: window.screenX + 1, y: window.screenY + 1};
+    window.moveTo(expectedPosition.x, expectedPosition.y);
+    assert_equals(window.screenX, expectedPosition.x, 'x position for primary');
+    assert_equals(window.screenY, expectedPosition.y, 'y position for primary');
+  });
+  // Start prerendering a page which tries to move this window.
+  loadInitiatorPage();
+} else {
+  const prevPosition = {x: window.screenX, y: window.screenY};
+  tryRun(
+      () => {
+        // Try to move this window, and should not succeed.
+        const moveToOrMoveBy = params.get('move');
+        switch (moveToOrMoveBy) {
+          case 'moveTo':
+            window.moveTo(window.screenX + 1, window.screenY + 1);
+            break;
+          case 'moveBy':
+            window.moveBy(1, 1);
+            break;
+          default:
+            assert_unreached(`wrong parameter: ${moveToOrMoveBy}`);
+        }
+      }
+  );
+
+  const bc = new BroadcastChannel('test-channel');
+  bc.postMessage({
+    'status': 'PASS',
+    'prevPosition': prevPosition,
+    'newPosition': {x: window.screenX, y: window.screenY}
+  });
+  bc.close();
+}
+
+</script>
diff --git a/speculation-rules/prerender/resources/window-resize.html b/speculation-rules/prerender/resources/window-resize.html
new file mode 100644
index 0000000..7edb1f7
--- /dev/null
+++ b/speculation-rules/prerender/resources/window-resize.html
@@ -0,0 +1,65 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="utils.js"></script>
+<script src="deferred-promise-utils.js"></script>
+<script>
+
+function tryRun(func) {
+  try {
+    func();
+  } catch (e) {
+    const testChannel = new BroadcastChannel('test-channel');
+    testChannel.postMessage({status: 'FAIL: ' + e});
+  }
+}
+
+const params = new URLSearchParams(location.search);
+
+// The main test page (restriction-window-resize.html) loads the
+// initiator page, then the initiator page will prerender itself with the
+// `prerendering` parameter.
+const isPrerendering = params.has('prerendering');
+
+if (!isPrerendering) {
+  // Ensure that the primary page can resize this window.
+  tryRun(() => {
+    const expectedRect = {
+      width: window.outerWidth - 1,
+      height: window.outerHeight - 1
+    };
+    window.resizeTo(expectedRect.width, expectedRect.height);
+    assert_equals(window.outerWidth, expectedRect.width, 'width for primary');
+    assert_equals(
+        window.outerHeight, expectedRect.height, 'height for primary');
+  });
+
+  // Start prerendering a page which tries to resize this window.
+  loadInitiatorPage();
+} else {
+  const prevRect = {width: window.outerWidth, height: window.outerHeight};
+  tryRun(() => {
+    // Try to resize this window, and should not succeed.
+    const resizeToOrResizeBy = params.get('resize');
+    switch (resizeToOrResizeBy) {
+      case 'resizeTo':
+        window.resizeTo(prevRect.width + 1, prevRect.height + 1);
+        break;
+      case 'resizeBy':
+        window.resizeBy(1, 1);
+        break;
+      default:
+        assert_unreached(`wrong parameter: ${resizeToOrResizeBy}`);
+    }
+  });
+
+  const bc = new BroadcastChannel('test-channel');
+  bc.postMessage({
+    'status': 'PASS',
+    'prevRect': prevRect,
+    'newRect': {width: window.outerWidth, height: window.outerHeight}
+  });
+  bc.close();
+}
+
+</script>
diff --git a/speculation-rules/prerender/restriction-window-move.html b/speculation-rules/prerender/restriction-window-move.html
new file mode 100644
index 0000000..30899cc
--- /dev/null
+++ b/speculation-rules/prerender/restriction-window-move.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<meta name="timeout" content="long">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/utils.js"></script>
+<body>
+<script>
+
+setup(() => assertSpeculationRulesIsSupported());
+
+// moveTo and moveBy operations should be ignored.
+// See https://github.com/jeremyroman/alternate-loading-modes/issues/73.
+['moveTo', 'moveBy'].forEach(moveFunc => {
+  promise_test(async t => {
+    const bc = new BroadcastChannel('test-channel');
+    t.add_cleanup(_ => bc.close());
+
+    const gotMessage = new Promise(resolve => {
+      bc.addEventListener('message', e => {
+        resolve(e.data);
+      }, {once: true});
+    });
+
+    const url = `resources/window-move.html?move=${moveFunc}`;
+
+    // We have to open a new window to run the test, since a window that was
+    // not created by window.open() cannot be moved.
+    window.open(
+        url, '_blank',
+        `width=${window.screen.availWidth / 2},height=${
+            window.screen.availHeight / 2},noopener`);
+
+    const result = await gotMessage;
+    assert_equals(result.status, 'PASS');
+    assert_equals(
+        result.prevPosition.x, result.newPosition.x,
+        'x position for prerendering');
+    assert_equals(
+        result.prevPosition.y, result.newPosition.y,
+        'y position for prerendering');
+  }, `a prerendering page cannot move its window by executing ${moveFunc}.`);
+});
+
+</script>
+</body>
diff --git a/speculation-rules/prerender/restriction-window-resize.html b/speculation-rules/prerender/restriction-window-resize.html
new file mode 100644
index 0000000..5f02ac6
--- /dev/null
+++ b/speculation-rules/prerender/restriction-window-resize.html
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<meta name="timeout" content="long">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/utils.js"></script>
+<body>
+<script>
+
+setup(() => assertSpeculationRulesIsSupported());
+
+// ResizeTo and ResizeBy operations should be ignored.
+// See https://github.com/jeremyroman/alternate-loading-modes/issues/73.
+['resizeTo', 'resizeBy'].forEach(resizeFunc => {
+  promise_test(
+      async t => {
+        const bc = new BroadcastChannel('test-channel');
+        t.add_cleanup(_ => bc.close());
+
+        const gotMessage = new Promise(resolve => {
+          bc.addEventListener('message', e => {
+            resolve(e.data);
+          }, {once: true});
+        });
+
+        const url = `resources/window-resize.html?resize=${resizeFunc}`;
+
+        // We have to open a new window to run the test, since a window that was
+        // not created by window.open() cannot be resized.
+        window.open(
+            url, '_blank',
+            `width=${window.screen.availWidth / 2},height=${
+                window.screen.availHeight / 2},noopener`);
+
+        const result = await gotMessage;
+        assert_equals(result.status, 'PASS');
+        assert_equals(
+            result.prevRect.width, result.newRect.width,
+            'width for prerendering');
+        assert_equals(
+            result.prevRect.height, result.newRect.height,
+            'height for prerendering');
+      },
+      `a prerendering page cannot resize its window by executing ${
+          resizeFunc}.`);
+});
+
+</script>
+</body>