Skip to content

Commit

Permalink
[WPT] Use sourceDocument for prefetch record lookup
Browse files Browse the repository at this point in the history
WPT updates for WICG/nav-speculation#267:

- Updated the expected behavior for an existing test:
  `prerender/prefetch.https.html`.
- Added other prefetch test cases.

Except for "<a>" subtest, `sourceDocument` is different from
`navigable's active document`.

Chromium's status:

- "<a>" subtest - already passing
- Others -- failing. Some of them will pass after [1],
  and remaining failures after [1] are tracked by
  https://crbug.com/1432886.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/4372403

Bug: 1440607, 1432886, 1422815, WICG/nav-speculation#267
Change-Id: I4098347c42f45188811700fcc8d7925bcc3c4162
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4422475
Reviewed-by: Domenic Denicola <domenic@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1141005}
  • Loading branch information
hiroshige-g authored and chromium-wpt-export-bot committed May 8, 2023
1 parent 7ac59b9 commit c209438
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 2 deletions.
79 changes: 79 additions & 0 deletions speculation-rules/prefetch/initiators-a-element.sub.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>
<meta name="variant" content="?cross-site">
<meta name="variant" content="?same-site">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/utils.sub.js"></script>
<script>
// In https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate,
// `sourceDocument` (instead of `navigable`'s active document) should be
// used as the referring document for prefetch.
promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

const win = await spawnWindow(t, { protocol: 'https' });

const hostname =
location.search === '?cross-site' ? '{{hosts[alt][www]}}' : undefined;
const nextUrl = win.getExecutorURL({ protocol: 'https', hostname, page: 2 });

await win.forceSinglePrefetch(nextUrl);

// sourceDocument == `win`'s Document == active document of window being
// navigated.
await win.execute_script((url) => {
window.executor.suspend(() => {
const a = document.createElement('a');
a.setAttribute('href', url);
document.body.appendChild(a);
a.click();
});
}, [nextUrl]);

assert_equals(
await win.execute_script(() => location.href),
nextUrl.toString(),
"expected navigation to reach destination URL");

assert_prefetched(await win.getRequestHeaders());
}, `<a>`);

promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

const win = await spawnWindow(t, { protocol: 'https' });

const hostname =
location.search === '?cross-site' ? '{{hosts[alt][www]}}' : undefined;
const nextUrl = win.getExecutorURL({ protocol: 'https', hostname, page: 2 });

await win.forceSinglePrefetch(nextUrl);

// sourceDocument == `win`'s Document != active document of window being
// navigated, since the window being navigated is a new window.
await win.execute_script((url) => {
window.executor.suspend(() => {
const a = document.createElement('a');
a.setAttribute('href', url);
a.setAttribute('target', '_blank');
document.body.appendChild(a);
a.click();
});
}, [nextUrl]);

// Below, the scripts given to `win.execute_script()` are executed on the
// `nextUrl` page in the new window, because `window.executor.suspend()`
// above made `win`'s original page stop processing `execute_script()`,
// while the new page of `nextUrl` in the new window starts processing
// `execute_script()` for the same ID.
assert_equals(
await win.execute_script(() => location.href),
nextUrl.toString(),
"expected navigation to reach destination URL");

assert_prefetched(await win.getRequestHeaders());
}, `<a target="blank">`);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<meta name="variant" content="?cross-site">
<meta name="variant" content="?same-site">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/utils.sub.js"></script>
<script>
// In https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate,
// `sourceDocument` (instead of `navigable`'s active document) should be
// used as the referring document for prefetch.
promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

const win = await spawnWindow(t, { protocol: 'https' });

const hostname =
location.search === '?cross-site' ? '{{hosts[alt][www]}}' : undefined;
const nextUrl = win.getExecutorURL({ protocol: 'https', hostname, page: 2 });

await win.forceSinglePrefetch(nextUrl);

// In https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate,
// `sourceDocument` is the incumbent Document and thus `win`'s Document.
// `navigable`'s active document is `iframe`'s Document.
await win.execute_script((url) => {
window.executor.suspend(() => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.location.href = url;
});
}, [nextUrl]);

// Below, the scripts given to `win.execute_script()` are executed on the
// `nextUrl` page in the iframe, because `window.executor.suspend()` above
// made `win`'s original page stop processing `execute_script()`,
// while the new page of `nextUrl` in the iframe starts processing
// `execute_script()` for the same ID.
assert_equals(
await win.execute_script(() => location.href),
nextUrl.toString(),
"expected navigation to reach destination URL");

assert_prefetched(await win.getRequestHeaders());
}, `location.href across iframe`);
</script>
68 changes: 68 additions & 0 deletions speculation-rules/prefetch/initiators-window-open.sub.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<meta name="variant" content="?cross-site">
<meta name="variant" content="?same-site">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/utils.sub.js"></script>
<script>
// In https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate,
// `sourceDocument` (instead of `navigable`'s active document) should be
// used as the referring document for prefetch.
promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

const win = await spawnWindow(t, { protocol: 'https' });

const hostname =
location.search === '?cross-site' ? '{{hosts[alt][www]}}' : undefined;
const nextUrl = win.getExecutorURL({ protocol: 'https', hostname, page: 2 });

await win.forceSinglePrefetch(nextUrl);

await win.execute_script((url) => {
window.executor.suspend(() => {
window.open(url, "_blank");
});
}, [nextUrl]);

// Below, the scripts given to `win.execute_script()` are executed on the
// `nextUrl` page in the new window, because `window.executor.suspend()`
// above made `win`'s original page stop processing `execute_script()`,
// while the new page of `nextUrl` in the new window starts processing
// `execute_script()` for the same ID. Same for below.
assert_equals(
await win.execute_script(() => location.href),
nextUrl.toString(),
"expected navigation to reach destination URL");

assert_prefetched(await win.getRequestHeaders());
}, `window.open()`);

promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

const win = await spawnWindow(t, { protocol: 'https' });

const hostname =
location.search === '?cross-site' ? '{{hosts[alt][www]}}' : undefined;
const nextUrl = win.getExecutorURL({ protocol: 'https', hostname, page: 2 });

await win.forceSinglePrefetch(nextUrl);

await win.execute_script((url) => {
window.executor.suspend(() => {
window.open(url, "_blank", "noopener");
});
}, [nextUrl]);

assert_equals(
await win.execute_script(() => location.href),
nextUrl.toString(),
"expected navigation to reach destination URL");

assert_prefetched(await win.getRequestHeaders());
}, `window.open(noopener)`);
</script>
30 changes: 30 additions & 0 deletions speculation-rules/prefetch/prefetch-traverse-reload.sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@
assert_prefetched(await agent.getRequestHeaders(), "traversal should use prefetch");
}, "prefetches can be used for traversal navigations");

promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

let agent = await spawnWindow(t, { protocol: 'https', pipe: 'header(Cache-Control, no-store)' });
let previousUrl = await agent.execute_script(() => location.href);
await agent.execute_script(async () => {
window.preventBfcache = new WebSocket('wss://{{ports[wss][0]}}/echo');
});

let nextUrl = agent.getExecutorURL({ protocol: 'https', page: 2 });
await agent.navigate(nextUrl);

await agent.forceSinglePrefetch(previousUrl);
// In https://html.spec.whatwg.org/multipage/nav-history-apis.html#delta-traverse,
// `sourceDocument` is `History`'s relevant global object's associated
// Document. In this case, it's `iframe.contentDocument`, and thus the
// prefetch from `win`'s Document (iframe's parent Document) isn't used.
await agent.execute_script(() => {
window.executor.suspend(() => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.history.go(-1);
});
});

assert_equals(previousUrl, await agent.execute_script(() => location.href));
assert_not_prefetched(await agent.getRequestHeaders(),
"prefetch from different Document should not be used");
}, "History's Document is used for traversal navigations");

promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

Expand Down
4 changes: 2 additions & 2 deletions speculation-rules/prerender/prefetch.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
await create_prerendered_page(t, {}, {prefetch: true}, {});

assert_equals(await tryToActivate(), 'activated');
assert_equals(await getNetworkRequestCount(), '2', 'Network request count');
}, "Prerender navigation requests don't use prefetched results (for now)");
assert_equals(await getNetworkRequestCount(), '1', 'Network request count');
}, "Prerender navigation requests should use prefetched results");
</script>
</body>

0 comments on commit c209438

Please sign in to comment.