Skip to content

Commit

Permalink
Bug 1577455 [wpt PR 18742] - Modernize wpt: service-worker/getregistr…
Browse files Browse the repository at this point in the history
…ations.https.html, a=testonly

Automatic update from web-platform-tests
Modernize wpt: service-worker/getregistrations.https.html

This CL modernizes the test by using ES6 styles.

Bug: 925740
Change-Id: I3d74a2629d8d6b8d44da06e4ecec60485f909ce0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1775945
Commit-Queue: Makoto Shimazu <[email protected]>
Reviewed-by: Matt Falkenhagen <[email protected]>
Cr-Commit-Position: refs/heads/master@{#691986}

--

wpt-commits: 2909c676a9c27c87493570589422a83e2f6f1964
wpt-pr: 18742
  • Loading branch information
makotoshimazu authored and moz-wptsync-bot committed Sep 3, 2019
1 parent a1a0272 commit fbfed65
Showing 1 changed file with 108 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,178 +8,122 @@
// Purge the existing registrations for the origin.
// getRegistrations() is used in order to avoid adding additional complexity
// e.g. adding an internal function.
promise_test(function() {
return navigator.serviceWorker.getRegistrations()
.then(function(registrations) {
return registrations.reduce(function(sequence, registration) {
return sequence.then(function() {
return registration.unregister();
});
}, Promise.resolve());
})
.then(function() {
return navigator.serviceWorker.getRegistrations();
})
.then(function(value) {
assert_array_equals(
value,
[],
'getRegistrations should resolve with an empty array.');
});
promise_test(async () => {
const registrations = await navigator.serviceWorker.getRegistrations();
await Promise.all(registrations.map(r => r.unregister()));
const value = await navigator.serviceWorker.getRegistrations();
assert_array_equals(
value, [],
'getRegistrations should resolve with an empty array.');
}, 'registrations are not returned following unregister');

promise_test(function(t) {
var scope = 'resources/scope/getregistrations/normal';
var script = 'resources/empty-worker.js';
var registrations = [];
return service_worker_unregister_and_register(t, script, scope)
.then(function(r) {
registrations.push(r);
return navigator.serviceWorker.getRegistrations();
})
.then(function(value) {
assert_array_equals(
value,
registrations,
'getRegistrations should resolve with array of registrations.');
return service_worker_unregister(t, scope);
});
}, 'Register then getRegistrations');
promise_test(async t => {
const scope = 'resources/scope/getregistrations/normal';
const script = 'resources/empty-worker.js';
const registrations = [
await service_worker_unregister_and_register(t, script, scope)];
t.add_cleanup(() => registrations[0].unregister());
const value = await navigator.serviceWorker.getRegistrations();
assert_array_equals(value, registrations,
'getRegistrations should resolve with an array of registrations');
}, 'Register then getRegistrations');

promise_test(function(t) {
var scope1 = 'resources/scope/getregistrations/scope1';
var scope2 = 'resources/scope/getregistrations/scope2';
var script = 'resources/empty-worker.js';
var registrations = [];
return service_worker_unregister_and_register(t, script, scope1)
.then(function(r) {
registrations.push(r);
return service_worker_unregister_and_register(t, script, scope2);
})
.then(function(r) {
registrations.push(r);
return navigator.serviceWorker.getRegistrations();
})
.then(function(value) {
assert_array_equals(
value,
registrations,
'getRegistrations should resolve with array of registrations.');
return service_worker_unregister(t, scope1);
})
.then(function() {
return service_worker_unregister(t, scope2);
});
}, 'Register multiple times then getRegistrations');
promise_test(async t => {
const scope1 = 'resources/scope/getregistrations/scope1';
const scope2 = 'resources/scope/getregistrations/scope2';
const script = 'resources/empty-worker.js';
t.add_cleanup(() => service_worker_unregister(t, scope1));
t.add_cleanup(() => service_worker_unregister(t, scope2));

promise_test(function(t) {
var scope = 'resources/scope/getregistrations/register-unregister';
var script = 'resources/empty-worker.js';
return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
return registration.unregister();
})
.then(function() {
return navigator.serviceWorker.getRegistrations();
})
.then(function(value) {
assert_array_equals(
value,
[],
'getRegistrations should resolve with an empty array.');
});
}, 'Register then Unregister then getRegistrations');
const registrations = [
await service_worker_unregister_and_register(t, script, scope1),
await service_worker_unregister_and_register(t, script, scope2)
];
const value = await navigator.serviceWorker.getRegistrations();
assert_array_equals(value, registrations);
}, 'Register multiple times then getRegistrations');

promise_test(function(t) {
var scope = 'resources/scope/getregistrations/register-unregister-controlled';
var script = 'resources/empty-worker.js';
var registrations;
return service_worker_unregister_and_register(t, script, scope)
.then(function(r) {
registration = r;
return wait_for_state(t, registration.installing, 'activated');
})
.then(function() {
return with_iframe(scope);
})
.then(function(f) {
t.add_cleanup(function() { f.remove(); });
return registration.unregister();
})
.then(function() {
return navigator.serviceWorker.getRegistrations();
})
.then(function(value) {
assert_array_equals(
value,
[],
'getRegistrations should resolve with an empty array.');
assert_equals(registration.installing, null);
assert_equals(registration.waiting, null);
assert_equals(registration.active.state, 'activated');
});
}, 'Register then Unregister with controlled frame then getRegistrations');
promise_test(async t => {
const scope = 'resources/scope/getregistrations/register-unregister';
const script = 'resources/empty-worker.js';
const registration = await service_worker_unregister_and_register(t, script, scope);
await registration.unregister();
const value = await navigator.serviceWorker.getRegistrations();
assert_array_equals(
value, [], 'getRegistrations should resolve with an empty array.');
}, 'Register then Unregister then getRegistrations');

promise_test(function(t) {
var host_info = get_host_info();
// Rewrite the url to point to remote origin.
var frame_same_origin_url = new URL("resources/frame-for-getregistrations.html", window.location);
var frame_url = host_info['HTTPS_REMOTE_ORIGIN'] + frame_same_origin_url.pathname;
var scope = 'resources/scope-for-getregistrations';
var script = 'resources/empty-worker.js';
var frame;
var registrations = [];
promise_test(async t => {
const scope = 'resources/scope/getregistrations/register-unregister-controlled';
const script = 'resources/empty-worker.js';
const registration = await service_worker_unregister_and_register(t, script, scope);
await wait_for_state(t, registration.installing, 'activated');

// Loads an iframe and waits for 'ready' message from it to resolve promise.
// Caller is responsible for removing frame.
function with_iframe_ready(url) {
return new Promise(function(resolve) {
var frame = document.createElement('iframe');
frame.src = url;
window.addEventListener('message', function onMessage(e) {
window.removeEventListener('message', onMessage);
if (e.data == 'ready') {
resolve(frame);
}
});
document.body.appendChild(frame);
});
}
// Create a frame controlled by the service worker and unregister the
// worker.
const frame = await with_iframe(scope);
t.add_cleanup(() => frame.remove());
await registration.unregister();

// We need this special frame loading function because the frame is going
// to register it's own service worker and there is the possibility that that
// register() finishes after the register() for the same domain later in the
// test. So we have to wait until the cross origin register() is done, and not
// just until the frame loads.
return with_iframe_ready(frame_url)
.then(function(f) {
t.add_cleanup(function() { f.remove(); });
frame = f;
return service_worker_unregister_and_register(t, script, scope);
})
.then(function(r) {
registrations.push(r);
return navigator.serviceWorker.getRegistrations();
})
.then(function(value) {
assert_array_equals(
value,
registrations,
'getRegistrations should only return same origin registrations.');
const value = await navigator.serviceWorker.getRegistrations();
assert_array_equals(
value, [],
'getRegistrations should resolve with an empty array.');
assert_equals(registration.installing, null);
assert_equals(registration.waiting, null);
assert_equals(registration.active.state, 'activated');
}, 'Register then Unregister with controlled frame then getRegistrations');

var channel = new MessageChannel();
var resolve;
var p = new Promise(function(r) { resolve = r; });
promise_test(async t => {
const host_info = get_host_info();
// Rewrite the url to point to remote origin.
const frame_same_origin_url = new URL("resources/frame-for-getregistrations.html", window.location);
const frame_url = host_info['HTTPS_REMOTE_ORIGIN'] + frame_same_origin_url.pathname;
const scope = 'resources/scope-for-getregistrations';
const script = 'resources/empty-worker.js';

channel.port1.onmessage = function(e) {
if (e.data == 'unregistered')
resolve();
};
frame.contentWindow.postMessage('unregister', '*', [channel.port2]);
return p;
})
.then(function() {
return service_worker_unregister(t, scope);
});
}, 'getRegistrations promise resolves only with same origin registrations.');
// Loads an iframe and waits for 'ready' message from it to resolve promise.
// Caller is responsible for removing frame.
function with_iframe_ready(url) {
return new Promise(resolve => {
const frame = document.createElement('iframe');
frame.src = url;
window.addEventListener('message', function onMessage(e) {
window.removeEventListener('message', onMessage);
if (e.data == 'ready') {
resolve(frame);
}
});
document.body.appendChild(frame);
});
}

// We need this special frame loading function because the frame is going
// to register it's own service worker and there is the possibility that that
// register() finishes after the register() for the same domain later in the
// test. So we have to wait until the cross origin register() is done, and not
// just until the frame loads.
const frame = await with_iframe_ready(frame_url);
t.add_cleanup(async () => {
// Wait until the cross-origin worker is unregistered.
let resolve;
const channel = new MessageChannel();
channel.port1.onmessage = e => {
if (e.data == 'unregistered')
resolve();
};
frame.contentWindow.postMessage('unregister', '*', [channel.port2]);
await new Promise(r => { resolve = r; });

frame.remove();
});

const registrations = [
await service_worker_unregister_and_register(t, script, scope)];
t.add_cleanup(() => registrations[0].unregister());
const value = await navigator.serviceWorker.getRegistrations();
assert_array_equals(
value, registrations,
'getRegistrations should only return same origin registrations.');
}, 'getRegistrations promise resolves only with same origin registrations.');
</script>

0 comments on commit fbfed65

Please sign in to comment.