Bug 1564792 [wpt PR 17593] - Worker: Simplify worker-interception.https.html, a=testonly

Automatic update from web-platform-tests
Worker: Simplify worker-interception.https.html

This CL removes unnecessary iframe creation, and adds test helper functions to
simplify the tests. This is just refactoring, and should work just like before.

Bug: 731604, 906991
Change-Id: I775567c64df0a7ed4b1931caf285c06f60b0b14b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1683916
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#673963}

--

wpt-commits: 1bac9e9d1715ca642d09ceb2a089db2b82c8714c
wpt-pr: 17593
This commit is contained in:
Hiroki Nakagawa 2019-07-19 19:45:04 +00:00 коммит произвёл James Graham
Родитель 305f1508c0
Коммит e2b9a20f85
2 изменённых файлов: 87 добавлений и 144 удалений

Просмотреть файл

@ -1,59 +0,0 @@
<script src="/resources/testharness.js"></script>
<script src="test-helpers.sub.js?pipe=sub"></script>
<script>
class TestRunner {
constructor(script_url, worker_type) {
this.script_url = script_url;
this.worker_type = worker_type;
}
// Tests subresource requests on a worker.
async run(subresource_type) {
let data;
if (this.worker_type === 'worker') {
data = await this.runOnDedicatedWorker(subresource_type);
} else if (this.worker_type === 'sharedworker') {
data = await this.runOnSharedWorker(subresource_type);
} else {
data = 'Unexpected worker type! ' + worker_type;
}
assert_equals(data, 'This load was successfully intercepted.');
}
async runOnDedicatedWorker(subresource_type) {
const worker = new Worker(this.script_url);
const data = await new Promise((resolve, reject) => {
worker.onmessage = e => resolve(e.data);
worker.onerror = e => reject(e);
worker.postMessage(subresource_type);
});
worker.terminate();
return data;
}
async runOnSharedWorker(subresource_type) {
const worker = new SharedWorker(this.script_url);
return await new Promise((resolve, reject) => {
worker.port.onmessage = e => resolve(e.data);
worker.onerror = e => reject(e);
worker.port.postMessage(subresource_type);
});
}
}
window.addEventListener('message', async evt => {
const test_runner = new TestRunner(evt.data.script, evt.data.type);
const port = evt.ports[0];
try {
await test_runner.run('xhr');
await test_runner.run('fetch');
await test_runner.run('importScripts');
port.postMessage({results: 'finish'});
} catch(e) {
port.postMessage({results: 'failure:' + e});
}
});
</script>

Просмотреть файл

@ -7,19 +7,21 @@
<body>
<script>
async function setup_service_worker(t, script_url, scope) {
const r =
await service_worker_unregister_and_register(t, script_url, scope);
async function setup_service_worker(t, service_worker_url, scope) {
const r = await service_worker_unregister_and_register(
t, service_worker_url, scope);
t.add_cleanup(() => service_worker_unregister(t, scope));
await wait_for_state(t, r.installing, 'activated');
}
// ========== Worker main resource interception tests ==========
promise_test(async t => {
const worker_url = 'resources/dummy-synthesized-worker.js?dedicated';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new Worker(worker_url);
const data = await new Promise((resolve, reject) => {
w.onmessage = e => resolve(e.data);
@ -31,10 +33,10 @@ promise_test(async t => {
promise_test(async t => {
const worker_url = 'resources/dummy-synthesized-worker.js?shared';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new SharedWorker(worker_url);
const data = await new Promise((resolve, reject) => {
w.port.onmessage = e => resolve(e.data);
@ -46,10 +48,10 @@ promise_test(async t => {
promise_test(async t => {
const worker_url = 'resources/dummy-same-origin-worker.js?dedicated';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new Worker(worker_url);
const data = await new Promise((resolve, reject) => {
w.onmessage = e => resolve(e.data);
@ -61,10 +63,10 @@ promise_test(async t => {
promise_test(async t => {
const worker_url = 'resources/dummy-same-origin-worker.js?shared';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new SharedWorker(worker_url);
const data = await new Promise((resolve, reject) => {
w.port.onmessage = e => resolve(e.data);
@ -76,10 +78,10 @@ promise_test(async t => {
promise_test(async t => {
const worker_url = 'resources/dummy-cors-worker.js?dedicated';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new Worker(worker_url);
const watcher = new EventWatcher(t, w, ['message', 'error']);
await watcher.wait_for('error');
@ -88,10 +90,10 @@ promise_test(async t => {
promise_test(async t => {
const worker_url = 'resources/dummy-cors-worker.js?shared';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new SharedWorker(worker_url);
const watcher = new EventWatcher(t, w, ['message', 'error']);
await watcher.wait_for('error');
@ -100,10 +102,10 @@ promise_test(async t => {
promise_test(async t => {
const worker_url = 'resources/dummy-no-cors-worker.js?dedicated';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new Worker(worker_url);
const watcher = new EventWatcher(t, w, ['message', 'error']);
await watcher.wait_for('error');
@ -112,91 +114,91 @@ promise_test(async t => {
promise_test(async t => {
const worker_url = 'resources/dummy-no-cors-worker.js?shared';
const service_worker = 'resources/dummy-worker-interceptor.js';
const service_worker_url = 'resources/dummy-worker-interceptor.js';
const scope = worker_url;
await setup_service_worker(t, service_worker, scope);
await setup_service_worker(t, service_worker_url, scope);
const w = new SharedWorker(worker_url);
const watcher = new EventWatcher(t, w, ['message', 'error']);
await watcher.wait_for('error');
}, 'Verify a no-cors cross-origin worker script served by a service worker ' +
'fails shared worker start.');
promise_test(async t => {
const subdoc_url = 'resources/worker-interception-iframe.https.html';
const service_worker = 'resources/worker-load-interceptor.js';
const scope = 'resources/load_worker.js?dedicated';
// ========== Worker subresource interception tests ==========
await setup_service_worker(t, service_worker, scope);
const frame = await with_iframe(subdoc_url);
t.add_cleanup(() => frame.remove());
// Do not call this function multiple times without waiting for the promise
// resolution because this sets new event handlers on |worker|.
// TODO(nhiroki): To isolate multiple function calls, use MessagePort instead of
// worker's onmessage event handler.
async function request_on_worker(worker, resource_type) {
const data = await new Promise((resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = e => resolve(e.data);
frame.contentWindow.postMessage(
{ script: 'load_worker.js?dedicated', type: 'worker' },
'*', [channel.port2]);
if (worker instanceof Worker) {
worker.onmessage = e => resolve(e.data);
worker.onerror = e => reject(e);
worker.postMessage(resource_type);
} else if (worker instanceof SharedWorker) {
worker.port.onmessage = e => resolve(e.data);
worker.onerror = e => reject(e);
worker.port.postMessage(resource_type);
} else {
reject('Unexpected worker type!');
}
});
assert_equals(data.results, 'finish');
}, 'Verify subresource requests on a dedicated worker controlled by a ' +
'service worker.');
assert_equals(data, 'This load was successfully intercepted.');
}
promise_test(async t => {
const subdoc_url = 'resources/worker-interception-iframe.https.html';
const service_worker = 'resources/worker-load-interceptor.js';
const scope = 'resources/load_worker.js?shared';
// TODO(nhiroki): For optimization, register a service worker just once in an
// initial promise test.
await setup_service_worker(t, service_worker, scope);
const frame = await with_iframe(subdoc_url);
t.add_cleanup(() => frame.remove());
const data = await new Promise((resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = e => resolve(e.data);
frame.contentWindow.postMessage(
{ script: 'load_worker.js?shared', type: 'sharedworker' },
'*', [channel.port2]);
});
assert_equals(data.results, 'finish');
}, 'Verify subresource requests on a shared worker controlled by a service ' +
'worker.');
function dedicated_worker_subresource_test(worker_url, scope, description) {
promise_test(async t => {
const service_worker_url = 'resources/worker-load-interceptor.js';
await setup_service_worker(t, service_worker_url, scope);
const worker = new Worker(worker_url);
promise_test(async t => {
const subdoc_url = 'resources/worker-interception-iframe.https.html';
const service_worker = 'resources/worker-load-interceptor.js';
const scope = 'resources/nested_load_worker.js?dedicated';
await request_on_worker(worker, 'xhr');
await request_on_worker(worker, 'fetch');
await request_on_worker(worker, 'importScripts');
}, description);
}
await setup_service_worker(t, service_worker, scope);
const frame = await with_iframe(subdoc_url);
t.add_cleanup(() => frame.remove());
const data = await new Promise((resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = e => resolve(e.data);
frame.contentWindow.postMessage(
{ script: 'nested_load_worker.js?dedicated', type: 'worker' },
'*', [channel.port2]);
});
assert_equals(data.results, 'finish');
}, 'Verify subresource requests on a nested dedicated worker created from a ' +
'dedicated worker and controlled by a service worker.');
function shared_worker_subresource_test(worker_url, scope, description) {
promise_test(async t => {
const service_worker_url = 'resources/worker-load-interceptor.js';
await setup_service_worker(t, service_worker_url, scope);
const worker = new SharedWorker(worker_url);
promise_test(async t => {
const subdoc_url = 'resources/worker-interception-iframe.https.html';
const service_worker = 'resources/worker-load-interceptor.js';
const scope = 'resources/nested_load_worker.js?shared';
await request_on_worker(worker, 'xhr');
await request_on_worker(worker, 'fetch');
await request_on_worker(worker, 'importScripts');
}, description);
}
await setup_service_worker(t, service_worker, scope);
const frame = await with_iframe(subdoc_url);
t.add_cleanup(() => frame.remove());
const data = await new Promise((resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = e => resolve(e.data);
frame.contentWindow.postMessage(
{ script: 'nested_load_worker.js?shared', type: 'sharedworker' },
'*', [channel.port2]);
});
assert_equals(data.results, 'finish');
}, 'Verify subresource requests on a nested dedicated worker created from a ' +
'shared worker and controlled by a service worker.');
dedicated_worker_subresource_test(
'resources/load_worker.js?dedicated',
'resources/load_worker.js?dedicated',
'Requests on a dedicated worker controlled by a service worker.');
shared_worker_subresource_test(
'resources/load_worker.js?shared',
'resources/load_worker.js?shared',
'Requests on a shared worker controlled by a service worker.');
dedicated_worker_subresource_test(
'resources/nested_load_worker.js?dedicated',
// TODO(nhiroki): This scope is wrong. This should be
// 'resources/load_worker.js?dedicated'.
'resources/nested_load_worker.js?dedicated',
'Requests on a dedicated worker nested in a dedicated worker and ' +
'controlled by a service worker');
shared_worker_subresource_test(
'resources/nested_load_worker.js?shared',
// TODO(nhiroki): This scope is wrong. This should be
// 'resources/load_worker.js?shared'.
'resources/nested_load_worker.js?shared',
'Requests on a dedicated worker nested in a shared worker and controlled ' +
'by a service worker');
</script>
</body>