Commit 7df1f3c4 authored by Hiroki Nakagawa's avatar Hiroki Nakagawa Committed by Commit Bot

ServiceWorker: Modernize clients-matchall-client-types.https.html

This CL modernizes clients-matchall-client-types.https.html using ES
features (arrow functions, async/await, etc) for code cleanup. This
doesn't change functional behavior.

Bug: n/a
Change-Id: Iaf35d11f1f90ec8e9cf9d41d585a873d7bcd8695
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2010758
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarEriko Kurimoto <elkurin@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#733872}
parent 61d887f3
...@@ -4,117 +4,89 @@ ...@@ -4,117 +4,89 @@
<script src="/resources/testharnessreport.js"></script> <script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script> <script src="resources/test-helpers.sub.js"></script>
<script> <script>
var scope = 'resources/clients-matchall-client-types'; const scope = 'resources/clients-matchall-client-types';
var iframe_url = scope + '-iframe.html'; const iframe_url = scope + '-iframe.html';
var shared_worker_url = scope + '-shared-worker.js'; const shared_worker_url = scope + '-shared-worker.js';
var dedicated_worker_url = scope + '-dedicated-worker.js'; const dedicated_worker_url = scope + '-dedicated-worker.js';
/* visibilityState, focused, url, type, frameType */ /* visibilityState, focused, url, type, frameType */
var expected_only_window = [ const expected_only_window = [
['visible', true, new URL(iframe_url, location).href, 'window', 'nested'] ['visible', true, new URL(iframe_url, location).href, 'window', 'nested']
]; ];
var expected_only_shared_worker = [ const expected_only_shared_worker = [
[undefined, undefined, new URL(shared_worker_url, location).href, 'sharedworker', 'none'] [undefined, undefined, new URL(shared_worker_url, location).href, 'sharedworker', 'none']
]; ];
var expected_only_dedicated_worker = [ const expected_only_dedicated_worker = [
[undefined, undefined, new URL(dedicated_worker_url, location).href, 'worker', 'none'] [undefined, undefined, new URL(dedicated_worker_url, location).href, 'worker', 'none']
]; ];
// These are explicitly sorted by URL in the service worker script. // These are explicitly sorted by URL in the service worker script.
var expected_all_clients = [ const expected_all_clients = [
expected_only_dedicated_worker[0], expected_only_dedicated_worker[0],
expected_only_window[0], expected_only_window[0],
expected_only_shared_worker[0], expected_only_shared_worker[0],
]; ];
function test_matchall(frame, expected, query_options) { async function test_matchall(frame, expected, query_options) {
// Make sure the frame gets focus. // Make sure the frame gets focus.
frame.focus(); frame.focus();
return new Promise(function(resolve, reject) { const data = await new Promise(resolve => {
var channel = new MessageChannel(); const channel = new MessageChannel();
channel.port1.onmessage = function(e) { resolve(e.data); }; channel.port1.onmessage = e => resolve(e.data);
frame.contentWindow.navigator.serviceWorker.controller.postMessage( frame.contentWindow.navigator.serviceWorker.controller.postMessage(
{port:channel.port2, options:query_options}, {port:channel.port2, options:query_options},
[channel.port2]); [channel.port2]);
}).then(function(data) { });
if (typeof data === 'string') {
throw new Error(data);
}
assert_equals(data.length, expected.length, 'result count'); if (typeof data === 'string') {
throw new Error(data);
}
for (var i = 0; i < data.length; ++i) { assert_equals(data.length, expected.length, 'result count');
assert_array_equals(data[i], expected[i]);
} for (let i = 0; i < data.length; ++i) {
}); assert_array_equals(data[i], expected[i]);
}
} }
promise_test(function(t) { promise_test(async t => {
var frame; const registration = await service_worker_unregister_and_register(
return service_worker_unregister_and_register( t, 'resources/clients-matchall-worker.js', scope);
t, 'resources/clients-matchall-worker.js', scope) t.add_cleanup(_ => registration.unregister());
.then(function(registration) { await wait_for_state(t, registration.installing, 'activated');
t.add_cleanup(function() { const frame = await with_iframe(iframe_url);
return service_worker_unregister(t, scope); t.add_cleanup(_ => frame.remove());
}); await test_matchall(frame, expected_only_window, {});
await test_matchall(frame, expected_only_window, {type:'window'});
}, 'Verify matchAll() with window client type');
return wait_for_state(t, registration.installing, 'activated'); promise_test(async t => {
}) const registration = await service_worker_unregister_and_register(
.then(function() { return with_iframe(iframe_url); }) t, 'resources/clients-matchall-worker.js', scope);
.then(function(f) { t.add_cleanup(_ => registration.unregister());
frame = f; await wait_for_state(t, registration.installing, 'activated');
t.add_cleanup(function() { frame.remove(); }); const frame = await with_iframe(iframe_url);
return test_matchall(frame, expected_only_window, {}); t.add_cleanup(_ => frame.remove());
})
.then(function() {
return test_matchall(frame, expected_only_window, {type:'window'});
});
}, 'Verify matchAll() with window client type');
promise_test(function(t) { // Set up worker clients.
var frame; const shared_worker = await new Promise((resolve, reject) => {
return service_worker_unregister_and_register( const w = new SharedWorker(shared_worker_url);
t, 'resources/clients-matchall-worker.js', scope) w.onerror = e => reject(e.message);
.then(function(registration) { w.port.onmessage = _ => resolve(w);
t.add_cleanup(function() { });
return service_worker_unregister(t, scope); const dedicated_worker = await new Promise((resolve, reject) => {
}); const w = new Worker(dedicated_worker_url);
w.onerror = e => reject(e.message);
w.onmessage = _ => resolve(w);
w.postMessage('Start');
});
return wait_for_state(t, registration.installing, 'activated'); await test_matchall(frame, expected_only_window, {});
}) await test_matchall(frame, expected_only_window, {type:'window'});
.then(function() { return with_iframe(iframe_url); }) await test_matchall(frame, expected_only_shared_worker,
.then(function(f) { {type:'sharedworker'});
frame = f; await test_matchall(frame, expected_only_dedicated_worker, {type:'worker'});
t.add_cleanup(function() { frame.remove(); }); await test_matchall(frame, expected_all_clients, {type:'all'});
return new Promise(function(resolve, reject) {
var w = new SharedWorker(shared_worker_url);
w.port.onmessage = resolve;
});
})
.then(function() {
return new Promise(function(resolve, reject) {
var w = new Worker(dedicated_worker_url);
w.onmessage = resolve;
w.postMessage('Start');
});
})
.then(function() {
return test_matchall(frame, expected_only_window, {});
})
.then(function() {
return test_matchall(frame, expected_only_window, {type:'window'});
})
.then(function() {
return test_matchall(frame, expected_only_shared_worker,
{type:'sharedworker'});
})
.then(function() {
return test_matchall(frame, expected_only_dedicated_worker,
{type:'worker'});
})
.then(function() {
return test_matchall(frame, expected_all_clients, {type:'all'});
});
}, 'Verify matchAll() with {window, sharedworker, worker} client types'); }, 'Verify matchAll() with {window, sharedworker, worker} client types');
</script> </script>
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