Prerender2 WPT: Externalize background-sync web platform test
Bug: 1253158
Change-Id: Ibd667f7e21fe99ee95e7dfd3c92ef4a419a8cf4a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3893541
Commit-Queue: Julie Jeongeun Kim <jkim@igalia.com>
Reviewed-by: Lingqi Chi <lingqi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1046741}
diff --git a/speculation-rules/prerender/resources/background-sync.https.html b/speculation-rules/prerender/resources/background-sync.https.html
new file mode 100644
index 0000000..f11a951
--- /dev/null
+++ b/speculation-rules/prerender/resources/background-sync.https.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/speculation-rules/prerender/resources/utils.js"></script>
+<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
+<script>
+
+const params = new URLSearchParams(location.search);
+
+// The main test page (restriction-background-sync.tentative.https.html)
+// loads the initiator page, then the initiator page will prerender itself
+// with the `prerendering` parameter.
+const isPrerendering = params.has('prerendering');
+
+if (!isPrerendering) {
+ loadInitiatorPage();
+} else {
+ async function loadPrerenderPage() {
+ const prerenderEventCollector = new PrerenderEventCollector();
+ const scope = `resources/`;
+ const registration = await navigator.serviceWorker.getRegistration(scope);
+ const register_promise = registration.periodicSync.register(
+ 'periodic', { minInterval: 1000 });
+ prerenderEventCollector.start(register_promise, 'periodicSync.register');
+ }
+ loadPrerenderPage();
+}
+
+</script>
diff --git a/speculation-rules/prerender/restriction-background-sync.tentative.https.html b/speculation-rules/prerender/restriction-background-sync.tentative.https.html
new file mode 100644
index 0000000..7d49e14
--- /dev/null
+++ b/speculation-rules/prerender/restriction-background-sync.tentative.https.html
@@ -0,0 +1,67 @@
+<!DOCTYPE html>
+<title>Access to the Background Sync API is deferred</title>
+<meta name="timeout" content="long">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/resources/testdriver.js"></script>
+<script src="/resources/testdriver-vendor.js"></script>
+<script src="/common/utils.js"></script>
+<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
+<script src="/speculation-rules/prerender/resources/utils.js"></script>
+<body>
+<script>
+
+setup(() => assertSpeculationRulesIsSupported());
+
+promise_test(async t => {
+ const uid = token();
+ const bc = new PrerenderChannel('test-channel', uid);
+ t.add_cleanup(_ => bc.close());
+
+ const gotMessage = new Promise(resolve => {
+ bc.addEventListener('message', e => {
+ resolve(e.data);
+ }, {
+ once: true
+ });
+ });
+
+ // We grant the permission here to make a more discerning test because
+ // periodicSync.register() waits until the permission is granted, which
+ // is deferred during prerendering so the test would trivially pass without
+ // the permission.
+ await test_driver.set_permission({name: 'periodic-background-sync'}, 'granted',
+ location.origin, location.origin);
+
+ // Install the service worker first to test periodicSync.register in the
+ // prerendering page.
+ const scope = `resources/`;
+ const script = `resources/do-nothing-worker.js`;
+ const registration =
+ await service_worker_unregister_and_register(t, script, scope);
+ t.add_cleanup(() => registration.unregister());
+ await wait_for_state(t, registration.installing, 'activated');
+
+ const url = `resources/background-sync.https.html?uid=${uid}`;
+ window.open(url, '_blank', 'noopener');
+
+ const result = await gotMessage;
+ const expected = [
+ {event: 'started waiting periodicSync.register', prerendering: true},
+ {event: 'prerendering change', prerendering: false},
+ {event: 'finished waiting periodicSync.register', prerendering: false},
+ ];
+ assert_equals(result.length, expected.length);
+ for (let i = 0; i < result.length; i++) {
+ assert_equals(result[i].event, expected[i].event, `event[${i}]`);
+ assert_equals(result[i].prerendering, expected[i].prerendering,
+ `prerendering[${i}]`);
+ }
+
+ // Send a close signal to PrerenderEventCollector on the prerendered page.
+ new PrerenderChannel('close', uid).postMessage('');
+}, `The access to the Background Sync API should be deferred until the
+ prerendered page is activated`);
+
+</script>
+</body>