Commit ec96e9da authored by Yannic Bonenberger's avatar Yannic Bonenberger Committed by Commit Bot

Add LayoutTest for update() from workers without controllers

Bug: 805496
Change-Id: Idb1cc63960409eeb8117d74267f5df33f401eb08
Reviewed-on: https://chromium-review.googlesource.com/932101
Commit-Queue: Yannic Bonenberger <contact@yannic-bonenberger.com>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577538}
parent c51effeb
......@@ -210,3 +210,15 @@ function login(test, local, remote) {
'password2' + suffix, 'cookie2');
});
}
function wait_for_port_message(port, handler) {
return new Promise((resolve, reject) => {
port.onmessage = (e) => {
try {
resolve(handler(e));
} catch (e) {
reject(e);
}
};
});
}
const rejectAfter = (timeout) => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(), timeout);
});
}
const startUpdate = () => {
let p = [];
for (let i = 0; i < 10; i++) {
p.push(self.registration.update());
}
return Promise.all(p);
};
const update = () => {
// update() rejects in one of these cases:
// 1. at least one update() rejects, or
// 2. at least one update() does not resolve after 15 seconds.
return Promise.race([startUpdate(), rejectAfter(150000)]);
};
self.addEventListener('message', (e) => {
const port = e.data;
port.onmessage = (e) => {
update().then(() => {
port.postMessage('success');
}).catch((e) => {
port.postMessage('failure');
});
};
});
This is a testharness.js-based test.
FAIL Verify multiple updates from service workers without controllees do not resolve immediately assert_equals: update should not have succeeded expected "failure" but got "success"
Harness: the test ran to completion.
<!DOCTYPE html>
<!-- This is not a WPT test because it tests non-specified Chrome-specific behavior. It tests that self-updating service workers eventually fail to update. -->
<title>Service Worker: update no controllee</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<body>
<script>
promise_test(async (t) => {
// Set up ServiceWorker and wait until it's |activated|.
const url = 'resources/update-no-controllee-worker.js';
const scope = 'resources/blank.html';
const registration = await service_worker_unregister_and_register(t, url, scope);
await wait_for_state(t, registration.installing, 'activated');
const frame = await with_iframe(scope);
const w = frame.contentWindow;
const sw = w.navigator.serviceWorker;
assert_true(sw.controller instanceof w.ServiceWorker,
'controller should be a ServiceWorker object');
const channel = new MessageChannel();
sw.controller.postMessage(channel.port1, [channel.port1]);
// Remove frame so our worker has no controller.
frame.remove();
assert_equals(
sw.controller, null, 'disconnected frame should not be controlled');
// Send a message to the worker to start updating and wait for its response.
// We expect at least one of the updates, and therefore the complete proccess,
// to fail immediately because the delay is too long.
const result = wait_for_port_message(channel.port2, (e) => {
assert_equals(e.data, 'failure', 'update should not have succeeded');
});
channel.port2.postMessage('');
await result;
// Cleanup.
await registration.unregister();
}, 'Verify multiple updates from service workers without controllees ' +
'do not resolve immediately');
</script>
</body>
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