Update web-platform-tests to revision b382ac7192087da0a7439902e20be76ab7587ee8

This commit is contained in:
WPT Sync Bot 2018-08-10 21:31:07 -04:00
parent 3e96a322ae
commit defee2aae0
45 changed files with 645 additions and 189 deletions

View file

@ -14,14 +14,14 @@ async function getMessageFromServiceWorker() {
});
}
// Registers the instrumentation Service Worker located at "resources/sw.js"
// Registers the |name| instrumentation Service Worker located at "service_workers/"
// with a scope unique to the test page that's running, and waits for it to be
// activated. The Service Worker will be unregistered automatically.
//
// Depends on /service-workers/service-worker/resources/test-helpers.sub.js
async function registerAndActivateServiceWorker(test) {
const script = 'resources/sw.js';
const scope = 'resources/scope' + location.pathname;
async function registerAndActivateServiceWorker(test, name) {
const script = `service_workers/${name}`;
const scope = 'service_workers/scope' + location.pathname;
let serviceWorkerRegistration =
await service_worker_unregister_and_register(test, script, scope);
@ -35,10 +35,13 @@ async function registerAndActivateServiceWorker(test) {
// Creates a Promise test for |func| given the |description|. The |func| will be
// executed with the `backgroundFetch` object of an activated Service Worker
// Registration.
function backgroundFetchTest(func, description) {
// |workerName| is the name of the service worker file in the service_workers
// directory to register.
function backgroundFetchTest(func, description, workerName = 'sw.js') {
promise_test(async t => {
const serviceWorkerRegistration = await registerAndActivateServiceWorker(t);
serviceWorkerRegistration.active.postMessage(null /* unused */);
const serviceWorkerRegistration =
await registerAndActivateServiceWorker(t, workerName);
serviceWorkerRegistration.active.postMessage(null);
assert_equals(await getMessageFromServiceWorker(), 'ready');

View file

@ -0,0 +1,12 @@
// The source to post setup and completion results to.
let source = null;
function sendMessageToDocument(msg) {
source.postMessage(msg);
}
// Notify the document that the SW is registered and ready.
self.addEventListener('message', event => {
source = event.source;
sendMessageToDocument('ready');
});

View file

@ -0,0 +1,22 @@
importScripts('sw-helpers.js');
async function updateUI(event) {
let updateParams = [];
switch (event.id) {
case 'update-once':
updateParams = [{title: 'Title1'}];
break;
case 'update-twice':
updateParams = [{title: 'Title1'}, {title: 'Title2'}];
break;
}
return Promise.all(updateParams.map(param => event.updateUI(param)))
.then(() => 'update success')
.catch(e => e.message);
}
self.addEventListener('backgroundfetched', event => {
event.waitUntil(updateUI(event)
.then(update => sendMessageToDocument({ type: event.type, update })))
});

View file

@ -1,4 +1,4 @@
let source = null;
importScripts('sw-helpers.js');
async function getFetchResult(settledFetch) {
if (!settledFetch.response)
@ -11,14 +11,9 @@ async function getFetchResult(settledFetch) {
};
}
self.addEventListener('message', event => {
source = event.source;
source.postMessage('ready');
});
self.addEventListener('backgroundfetched', event => {
event.waitUntil(
event.fetches.values()
.then(fetches => Promise.all(fetches.map(fetch => getFetchResult(fetch))))
.then(results => source.postMessage({ type: event.type, results })));
.then(results => sendMessageToDocument({ type: event.type, results })));
});

View file

@ -0,0 +1,32 @@
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/utils.js
'use strict';
// Covers functionality provided by BackgroundFetchUpdateEvent.updateUI().
//
// https://wicg.github.io/background-fetch/#backgroundfetchupdateuievent
const swName = 'sw-update-ui.js';
backgroundFetchTest(async (test, backgroundFetch) => {
const registrationId = 'update-once';
const registration =
await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt');
assert_equals(registration.id, registrationId);
const message = await getMessageFromServiceWorker();
assert_equals(message.update, 'update success');
}, 'Background Fetch updateUI resolves', swName);
backgroundFetchTest(async (test, backgroundFetch) => {
const registrationId = 'update-twice';
const registration =
await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt');
assert_equals(registration.id, registrationId);
const message = await getMessageFromServiceWorker();
assert_equals(message.update, 'updateUI may only be called once.');
}, 'Background Fetch updateUI called twice fails', swName);