Skip to content

Commit

Permalink
speculation-rules prefetch WPT tests
Browse files Browse the repository at this point in the history
- speculation-rules base prefetch logic should be
  able to prefetch multiple urls. To pass the test
  at least two urls should be prefetched.

Bug: 1302365
Change-Id: I1a333f6c8ab350a1a09e7f389d7bb444dc8d6dfa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3577359
Reviewed-by: Jeremy Roman <jbroman@chromium.org>
Commit-Queue: Iman Saboori <isaboori@google.com>
Cr-Commit-Position: refs/heads/main@{#990788}
  • Loading branch information
isaboori authored and pull[bot] committed Nov 29, 2023
1 parent be0325d commit 1060949
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
21 changes: 21 additions & 0 deletions speculation-rules/prefetch/multiple-url.https.html
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<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>
promise_test(async t => {
assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

let urls = getPrefetchUrlList(5);
insertSpeculationRules({ prefetch: [{ source: 'list', urls: urls }] });
await new Promise(resolve => t.step_timeout(resolve, 3000));

let prefetched_count = (await Promise.all(urls.map(isUrlPrefetched))).reduce(
(count, was_prefetched) => count + (was_prefetched ? 1 : 0), 0);

assert_greater_than_equal(prefetched_count, 2, "At least two urls should be prefetched to pass the test.");
}, "browser should be able to prefetch multiple urls");
</script>
17 changes: 17 additions & 0 deletions speculation-rules/prefetch/resources/prefetch.py
@@ -0,0 +1,17 @@
from wptserve.handlers import json_handler


@json_handler
def main(request, response):
uuid = request.GET[b"uuid"]
prefetch = request.headers.get(
"Sec-Purpose", b"").decode("utf-8").startswith("prefetch")

n = request.server.stash.take(uuid)
if n is None:
n = 0
if prefetch:
n += 1
request.server.stash.put(uuid, n)

return n
16 changes: 16 additions & 0 deletions speculation-rules/prefetch/resources/utils.sub.js
Expand Up @@ -57,6 +57,22 @@ class PrefetchAgent extends RemoteContext {
}
}

// Produces n URLs with unique UUIDs which will record when they are prefetched.
function getPrefetchUrlList(n) {
let urls = [];
for (let i=0; i<n; i++) {
let params = new URLSearchParams({uuid: token()});
urls.push(new URL(`prefetch.py?${params}`, SR_PREFETCH_UTILS_URL));
}
return urls;
}

async function isUrlPrefetched(url) {
let response = await fetch(url);
assert_true(response.ok);
return response.json();
}

// Must also include /common/utils.js and /common/dispatcher/dispatcher.js to use this.
async function spawnWindow(t, extra = {}) {
let agent = new PrefetchAgent(token(), t);
Expand Down

0 comments on commit 1060949

Please sign in to comment.