Skip to content

Commit

Permalink
service workers: Simplified tests with async/await - Part 3
Browse files Browse the repository at this point in the history
- Fix a couple of tests with function t.add_cleanup() as it supports promise, see detail in #8748.
- The usage of unregister() method in some tests is not rigorous.  As [spec]\(https://w3c.github.io/ServiceWorker/#dom-serviceworkerregistration-unregister):
>>   [NewObject] Promise<boolean> unregister();
  unregister() method must run these steps:
>> 1. Let promise be a promise.
...
>> 4. Return promise.
  • Loading branch information
wanghongjuan committed Apr 29, 2019
1 parent d3cf77a commit c93aa52
Show file tree
Hide file tree
Showing 11 changed files with 734 additions and 828 deletions.
233 changes: 88 additions & 145 deletions service-workers/service-worker/activation.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,154 +10,97 @@
// waiting worker. The active worker controls |iframe| and has an inflight
// message event that can be finished by calling
// |registration.active.postMessage('go')|.
function setup_activation_test(t, scope, worker_url) {
var registration;
var iframe;
return navigator.serviceWorker.getRegistration(scope)
.then(r => {
if (r)
return r.unregister();
})
.then(() => {
// Create an in-scope iframe. Do this prior to registration to avoid
// racing between an update triggered by navigation and the update()
// call below.
return with_iframe(scope);
})
.then(f => {
iframe = f;
// Create an active worker.
return navigator.serviceWorker.register(worker_url, { scope: scope });
})
.then(r => {
registration = r;
add_result_callback(() => registration.unregister());
return wait_for_state(t, r.installing, 'activated');
})
.then(() => {
// Check that the frame was claimed.
assert_not_equals(
iframe.contentWindow.navigator.serviceWorker.controller, null);
// Create an in-flight request.
registration.active.postMessage('wait');
// Now there is both a controllee and an in-flight request.
// Initiate an update.
return registration.update();
})
.then(() => {
// Wait for a waiting worker.
return wait_for_state(t, registration.installing, 'installed');
})
.then(() => {
return wait_for_activation_on_dummy_scope(t, self);
})
.then(() => {
assert_not_equals(registration.waiting, null);
assert_not_equals(registration.active, null);
return Promise.resolve({registration: registration, iframe: iframe});
});
async function setup_activation_test(t, scope, worker_url) {
let registration = await navigator.serviceWorker.getRegistration(scope);
const iframe = await with_iframe(scope);
registration = await navigator.serviceWorker.register(worker_url, {
scope: scope
});
t.add_cleanup(async () => {
if (iframe) iframe.remove();
if (registration) await registration.unregister();
});

await wait_for_state(t, registration.installing, 'activated');
// Check that the frame was claimed.
assert_not_equals(
iframe.contentWindow.navigator.serviceWorker.controller, null);
// Create an in-flight request.
registration.active.postMessage('wait');
// Now there is both a controllee and an in-flight request.
// Initiate an update.
await registration.update();
// Wait for a waiting worker.
await wait_for_state(t, registration.installing, 'installed');
await wait_for_activation_on_dummy_scope(t, self);
assert_not_equals(registration.waiting, null);
assert_not_equals(registration.active, null);
return Promise.resolve({registration: registration, iframe: iframe});
}
promise_test(t => {
var scope = 'resources/no-controllee';
var worker_url = 'resources/mint-new-worker.py';
var registration;
var iframe;
var new_worker;
return setup_activation_test(t, scope, worker_url)
.then(result => {
registration = result.registration;
iframe = result.iframe;
// Finish the in-flight request.
registration.active.postMessage('go');
return wait_for_activation_on_dummy_scope(t, self);
})
.then(() => {
// The new worker is still waiting. Remove the frame and it should
// activate.
new_worker = registration.waiting;
assert_equals(new_worker.state, 'installed');
var reached_active = wait_for_state(t, new_worker, 'activating');
iframe.remove();
return reached_active;
})
.then(() => {
assert_equals(new_worker, registration.active);
});
}, 'loss of controllees triggers activation');
promise_test(t => {
var scope = 'resources/no-request';
var worker_url = 'resources/mint-new-worker.py';
var registration;
var iframe;
var new_worker;
return setup_activation_test(t, scope, worker_url)
.then(result => {
registration = result.registration;
iframe = result.iframe;
// Remove the iframe.
iframe.remove();
return new Promise(resolve => setTimeout(resolve, 0));
})
.then(() => {
// Finish the request.
new_worker = registration.waiting;
var reached_active = wait_for_state(t, new_worker, 'activating');
registration.active.postMessage('go');
return reached_active;
})
.then(() => {
assert_equals(registration.active, new_worker);
});
}, 'finishing a request triggers activation');
promise_test(t => {
var scope = 'resources/skip-waiting';
var worker_url = 'resources/mint-new-worker.py?skip-waiting';
var registration;
var new_worker;
return setup_activation_test(t, scope, worker_url)
.then(result => {
registration = result.registration;
// Finish the request. The iframe does not need to be removed because
// skipWaiting() was called.
new_worker = registration.waiting;
var reached_active = wait_for_state(t, new_worker, 'activating');
registration.active.postMessage('go');
return reached_active;
})
.then(() => {
assert_equals(registration.active, new_worker);
});
}, 'skipWaiting bypasses no controllee requirement');
promise_test(async t => {
const SCOPE = 'resources/no-controllee';
const WORKER_URL = 'resources/mint-new-worker.py';
const result = await setup_activation_test(t, SCOPE, WORKER_URL);
const registration = result.registration;
const iframe = result.iframe;
// Finish the in-flight request.
registration.active.postMessage('go');
await wait_for_activation_on_dummy_scope(t, self);
// The new worker is still waiting. Remove the frame and it should activate.
const new_worker = registration.waiting;
iframe.remove();
assert_equals(new_worker.state, 'installed');
await wait_for_state(t, new_worker, 'activating');
assert_equals(new_worker, registration.active);
}, 'loss of controllees triggers activation');

promise_test(async t => {
const SCOPE = 'resources/no-request';
const WORKER_URL = 'resources/mint-new-worker.py';
const result = await setup_activation_test(t, SCOPE, WORKER_URL);
const registration = result.registration;
const iframe = result.iframe;
// Remove the iframe.
iframe.remove();
await new Promise(resolve => setTimeout(resolve, 0));
const new_worker = registration.waiting;
registration.active.postMessage('go');
await wait_for_state(t, new_worker, 'activating');
assert_equals(registration.active, new_worker);
}, 'finishing a request triggers activation');

promise_test(async t => {
const SCOPE = 'resources/skip-waiting';
const WORKER_URL = 'resources/mint-new-worker.py?skip-waiting';
const result = await setup_activation_test(t, SCOPE, WORKER_URL);
const registration = result.registration;
// Finish the request. The iframe does not need to be removed because
// skipWaiting() was called.
const new_worker = registration.waiting;
registration.active.postMessage('go');
await wait_for_state(t, new_worker, 'activating');
assert_equals(registration.active, new_worker);
}, 'skipWaiting bypasses no controllee requirement');

// This test is not really about activation, but otherwise is very
// similar to the other tests here.
promise_test(t => {
var scope = 'resources/unregister';
var worker_url = 'resources/mint-new-worker.py';
var registration;
var iframe;
var new_worker;
return setup_activation_test(t, scope, worker_url)
.then(result => {
registration = result.registration;
iframe = result.iframe;
// Remove the iframe.
iframe.remove();
return registration.unregister();
})
.then(() => {
// The unregister operation should wait for the active worker to
// finish processing its events before clearing the registration.
new_worker = registration.waiting;
var reached_redundant = wait_for_state(t, new_worker, 'redundant');
registration.active.postMessage('go');
return reached_redundant;
})
.then(() => {
assert_equals(registration.installing, null);
assert_equals(registration.waiting, null);
assert_equals(registration.active, null);
});
}, 'finishing a request triggers unregister');
promise_test(async t => {
const SCOPE = 'resources/unregister';
const WORKER_URL = 'resources/mint-new-worker.py';
const result = await setup_activation_test(t, SCOPE, WORKER_URL);
const registration = result.registration;
const iframe = result.iframe;
// Remove the iframe.
iframe.remove();
await registration.unregister();
// The unregister operation should wait for the active worker to
// finish processing its events before clearing the registration.
const new_worker = registration.waiting;
registration.active.postMessage('go');
await wait_for_state(t, new_worker, 'redundant');
assert_equals(registration.installing, null);
assert_equals(registration.waiting, null);
assert_equals(registration.active, null);
}, 'finishing a request triggers unregister');
</script>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,47 @@
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>

// Note that Chrome cannot pass these tests because of https://crbug.com/731599.

function service_worker_interception_test(url, description) {
promise_test(async t => {
// Register a service worker whose scope includes |url|.
const kServiceWorkerScriptURL =
'resources/service-worker-interception-service-worker.js';
'resources/service-worker-interception-service-worker.js';
const registration = await service_worker_unregister_and_register(
t, kServiceWorkerScriptURL, url);
add_result_callback(() => registration.unregister());
t,
kServiceWorkerScriptURL,
url
);

t.add_cleanup(async () => {
if (registration) await registration.unregister();
});

await wait_for_state(t, registration.installing, 'activated');

// Start a dedicated worker for |url|. The top-level script request and any
// module imports should be intercepted by the service worker.
const worker = new Worker(url, { type: 'module' });
const msg_event = await new Promise(resolve => worker.onmessage = resolve);
const msg_event = await new Promise(
resolve => (worker.onmessage = resolve)
);
assert_equals(msg_event.data, 'LOADED_FROM_SERVICE_WORKER');
}, description);
}

service_worker_interception_test(
'resources/service-worker-interception-network-worker.js',
'Top-level module loading should be intercepted by a service worker.');
'resources/service-worker-interception-network-worker.js',
'Top-level module loading should be intercepted by a service worker.'
);

service_worker_interception_test(
'resources/service-worker-interception-static-import-worker.js',
'Static import should be intercepted by a service worker.');
'resources/service-worker-interception-static-import-worker.js',
'Static import should be intercepted by a service worker.'
);

service_worker_interception_test(
'resources/service-worker-interception-dynamic-import-worker.js',
'Dynamic import should be intercepted by a service worker.');

'resources/service-worker-interception-dynamic-import-worker.js',
'Dynamic import should be intercepted by a service worker.'
);
</script>
Loading

0 comments on commit c93aa52

Please sign in to comment.