| <!doctype html> |
| <meta charset="utf-8"> |
| <title>Background Fetch API: fetch() tests</title> |
| <script src="/resources/testharness.js"></script> |
| <script src="/resources/testharnessreport.js"></script> |
| <script src="/serviceworker/resources/test-helpers.js"></script> |
| |
| <h1>BackgroundFetchManager.fetch()</h1> |
| <p>This test validates the behaviour of the fetch() method.</p> |
| |
| <!-- TODO(peter): Move this to the WPT directory when it's merged and the |
| behaviour of fetch() for null and empty sequences is defined. --> |
| |
| <script> |
| 'use strict'; |
| |
| const workerUrl = 'resources/empty-worker.js'; |
| const scope = 'resources/scope/' + location.pathname; |
| const id = 'my-background-fetch'; |
| |
| promise_test(function(test) { |
| return service_worker_unregister_and_register(test, workerUrl, scope) |
| .then(registration => { |
| assert_equals(null, registration.active); |
| return registration.backgroundFetch.fetch('id', ['resources/non-existing-file.png']); |
| }) |
| .then(unreached_fulfillment(test), error => { |
| assert_equals(error.name, 'TypeError'); |
| }); |
| |
| }, 'BackgroundFetchManager.fetch() requires an activated Service Worker.'); |
| |
| promise_test(function(test) { |
| let registration = null; |
| return service_worker_unregister_and_register(test, workerUrl, scope) |
| .then(r => { |
| registration = r; |
| return wait_for_state(test, r.installing, 'activated'); |
| }) |
| .then(() => registration.backgroundFetch.fetch(id, null)) |
| .then(() => unreached_fulfillment(test), () => true /* pass */); |
| |
| }, 'BackgroundFetchManager.fetch() throws when given a null request.'); |
| |
| promise_test(function(test) { |
| let registration = null; |
| return service_worker_unregister_and_register(test, workerUrl, scope) |
| .then(r => { |
| registration = r; |
| return wait_for_state(test, r.installing, 'activated'); |
| }) |
| .then(() => registration.backgroundFetch.fetch(id, [])) |
| .then(() => unreached_fulfillment(test), () => true /* pass */); |
| |
| }, 'BackgroundFetchManager.fetch() throws when given an empty sequence.'); |
| |
| promise_test(function(test) { |
| let registration = null; |
| return service_worker_unregister_and_register(test, workerUrl, scope) |
| .then(r => { |
| registration = r; |
| return wait_for_state(test, r.installing, 'activated'); |
| }) |
| .then(() => registration.backgroundFetch.fetch(id, ['resources/non-existing-file.png', null])) |
| .then(() => unreached_fulfillment(test), () => true /* pass */); |
| |
| }, 'BackgroundFetchManager.fetch() throws when given a sequence with a null request.'); |
| |
| promise_test(function(test) { |
| const options = { |
| icons: [ |
| { |
| src: 'resources/non-existing-large-icon.png', |
| sizes: '256x256', |
| type: 'image/png' |
| }, |
| { |
| src: 'resources/non-existing-small-icon.jpg', |
| sizes: '64x64', |
| type: 'image/jpg' |
| } |
| ], |
| title: 'My Background Fetch', |
| downloadTotal: 1024 |
| }; |
| |
| let registration = null; |
| |
| return service_worker_unregister_and_register(test, workerUrl, scope) |
| .then(r => { |
| registration = r; |
| return wait_for_state(test, r.installing, 'activated'); |
| }) |
| .then(() => registration.backgroundFetch.fetch(id, ['resources/non-existing-file.png'], options)) |
| .then(backgroundFetchRegistration => { |
| assert_true(backgroundFetchRegistration instanceof BackgroundFetchRegistration); |
| assert_equals(backgroundFetchRegistration.id, id); |
| assert_equals(backgroundFetchRegistration.downloadTotal, options.downloadTotal); |
| }); |
| |
| }, 'BackgroundFetchManager.fetch() returns a BackgroundFetchRegistration object.'); |
| </script> |