Commit cf00dd72 authored by Daniel Soromou's avatar Daniel Soromou Committed by Commit Bot

Service worker: Add tests for asynchronously added fetch event handlers.

Change-Id: I5aa79823c36380303e75b429774f64c710e9159b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1799691Reviewed-by: default avatarTsuyoshi Horo <horo@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Commit-Queue: Daniel Soromou <fosoromo@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#700005}
parent abc752d2
Tests that a warning is shown in the console if addEventListener is called after initial evaluation of the service worker script.
Message count: 1
Message count: 2
service-worker-lazy-…deventlistener.js:2 Event handler of 'install' event must be added on the initial evaluation of worker script.
setTimeout @ service-worker-lazy-…deventlistener.js:2
(anonymous) @ service-worker-lazy-…deventlistener.js:2
setTimeout (async)
(anonymous) @ service-worker-lazy-…deventlistener.js:1
service-worker-lazy-…deventlistener.js:2 Event handler of 'fetch' event must be added on the initial evaluation of worker script.
(anonymous) @ service-worker-lazy-…deventlistener.js:2
setTimeout (async)
(anonymous) @ service-worker-lazy-…deventlistener.js:1
......@@ -2,6 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function wait_for_message(number_of_messages) {
return new Promise(resolve => {
ConsoleTestRunner.waitForConsoleMessages(number_of_messages, () => {
resolve();
});
});
}
(async function() {
TestRunner.addResult(
`Tests that a warning is shown in the console if addEventListener is called after initial evaluation of the service worker script.\n`);
......@@ -9,14 +17,26 @@
await TestRunner.loadModule('application_test_runner');
// Note: every test that uses a storage API must manually clean-up state from previous tests.
await ApplicationTestRunner.resetState();
await TestRunner.showPanel('resources');
var scriptURL = 'http://127.0.0.1:8000/devtools/service-workers/resources/service-worker-lazy-addeventlistener.js';
var scope = 'http://127.0.0.1:8000/devtools/service-workers/resources/lazy-scope';
ConsoleTestRunner.waitForConsoleMessages(1, () => {
ConsoleTestRunner.dumpConsoleMessages();
TestRunner.completeTest();
});
let scriptURL;
let scope;
// Test a lifecycle event.
scriptURL =
'http://127.0.0.1:8000/devtools/service-workers/resources/service-worker-lazy-install-addeventlistener.js';
scope = 'http://127.0.0.1:8000/devtools/service-workers/resources/lazy-install-scope';
ApplicationTestRunner.registerServiceWorker(scriptURL, scope);
await wait_for_message(/*number_of_messages=*/1);
// Test a functional event.
scriptURL =
'http://127.0.0.1:8000/devtools/service-workers/resources/service-worker-lazy-fetch-addeventlistener.js';
scope = 'http://127.0.0.1:8000/devtools/service-workers/resources/lazy-fetch-scope';
ApplicationTestRunner.registerServiceWorker(scriptURL, scope);
await wait_for_message(/*number_of_messages=*/2);
ConsoleTestRunner.dumpConsoleMessages();
TestRunner.completeTest();
})();
<!DOCTYPE html>
<title>Service Worker: Fetch event should not be handled when listener is added after script initial evaluation.</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<body>
<script>
// This test should not be upstreamed to WPT because it asserts that the event
// added after the initial evaluation of a worker script isn't fired. But
// this precision is not mandated by the current specifications.
promise_test(async function (t) {
const scope = 'resources/simple.html?fetch-event-async-add';
const script_url = 'resources/fetch-event-async-add-test-worker.js';
t.add_cleanup(() => {
frame.remove();
return service_worker_unregister(t, scope);
});
const registration =
await service_worker_unregister_and_register(t, script_url, scope);
await wait_for_state(t, registration.installing, 'activated');
const frame = await with_iframe(scope);
assert_equals(frame.contentDocument.body.textContent,
'Here\'s a simple html file.\n');
}, 'Service worker does not handle fetch event added after initial evaluation of worker script');
</script>
</body>
var eventHandler = async function (event) {
event.respondWith(new Response('codeSupposedUnreachable'));
};
self.addEventListener('install', () => {
// TODO(crbug.com/1005060): Move this outside the install event handler when the linked bug is fixed.
setTimeout(() => {
self.addEventListener('fetch', eventHandler);
}, 0);
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment