From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- .../background-fetch/resources/feature-name.txt | 1 + .../tests/background-fetch/resources/upload.py | 3 + .../tests/background-fetch/resources/utils.js | 71 ++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 testing/web-platform/tests/background-fetch/resources/feature-name.txt create mode 100644 testing/web-platform/tests/background-fetch/resources/upload.py create mode 100644 testing/web-platform/tests/background-fetch/resources/utils.js (limited to 'testing/web-platform/tests/background-fetch/resources') diff --git a/testing/web-platform/tests/background-fetch/resources/feature-name.txt b/testing/web-platform/tests/background-fetch/resources/feature-name.txt new file mode 100644 index 0000000000..4d54f5054c --- /dev/null +++ b/testing/web-platform/tests/background-fetch/resources/feature-name.txt @@ -0,0 +1 @@ +Background Fetch \ No newline at end of file diff --git a/testing/web-platform/tests/background-fetch/resources/upload.py b/testing/web-platform/tests/background-fetch/resources/upload.py new file mode 100644 index 0000000000..4c421c7d37 --- /dev/null +++ b/testing/web-platform/tests/background-fetch/resources/upload.py @@ -0,0 +1,3 @@ +# Simply returns the request body to check if the upload succeeded. +def main(request, response): + return 200, [(b"Content-Type", request.headers[b'content-type'])], request.body diff --git a/testing/web-platform/tests/background-fetch/resources/utils.js b/testing/web-platform/tests/background-fetch/resources/utils.js new file mode 100644 index 0000000000..10895c3ccf --- /dev/null +++ b/testing/web-platform/tests/background-fetch/resources/utils.js @@ -0,0 +1,71 @@ +'use strict'; + +let nextBackgroundFetchId = 0; + +function loadScript(path) { + let script = document.createElement('script'); + let promise = new Promise(resolve => script.onload = resolve); + script.src = path; + script.async = false; + document.head.appendChild(script); + return promise; +} + +// Waits for a single message received from a registered Service Worker. +async function getMessageFromServiceWorker() { + return new Promise(resolve => { + function listener(event) { + navigator.serviceWorker.removeEventListener('message', listener); + resolve(event.data); + } + + navigator.serviceWorker.addEventListener('message', listener); + }); +} + +// Registers the |name| instrumentation Service Worker located at "service_workers/" +// with a scope unique to the test page that's running, and waits for it to be +// activated. The Service Worker will be unregistered automatically. +// +// Depends on /service-workers/service-worker/resources/test-helpers.sub.js +async function registerAndActivateServiceWorker(test, name) { + const script = `service_workers/${name}`; + const scope = 'service_workers/scope' + location.pathname; + + let serviceWorkerRegistration = + await service_worker_unregister_and_register(test, script, scope); + + add_completion_callback(() => serviceWorkerRegistration.unregister()); + + await wait_for_state(test, serviceWorkerRegistration.installing, 'activated'); + return serviceWorkerRegistration; +} + +// Creates a Promise test for |func| given the |description|. The |func| will be +// executed with the `backgroundFetch` object of an activated Service Worker +// Registration. +// |workerName| is the name of the service worker file in the service_workers +// directory to register. +function backgroundFetchTest(func, description, workerName = 'sw.js') { + promise_test(async t => { + if (typeof test_driver === 'undefined') { + await loadScript('/resources/testdriver.js'); + await loadScript('/resources/testdriver-vendor.js'); + } + + await test_driver.set_permission({name: 'background-fetch'}, 'granted'); + + const serviceWorkerRegistration = + await registerAndActivateServiceWorker(t, workerName); + serviceWorkerRegistration.active.postMessage(null); + + assert_equals(await getMessageFromServiceWorker(), 'ready'); + + return func(t, serviceWorkerRegistration.backgroundFetch); + }, description); +} + +// Returns a Background Fetch ID that's unique for the current page. +function uniqueId() { + return 'id' + nextBackgroundFetchId++; +} -- cgit v1.2.3