2
* Copyright 2016 Google Inc. All rights reserved.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
/* eslint-env browser */
22
// Each service worker that is registered should be given a unique
23
// scope. To achieve this we register it with a scope the same as
24
// an iframe's src that is unique for each test.
25
// Service workers will then be made to claim pages on this scope -
27
var getIframe = function() {
28
return new Promise(resolve => {
29
var existingIframe = document.querySelector('.js-test-iframe');
31
return resolve(existingIframe);
34
// This will be used as a unique service worker scope
37
var newIframe = document.createElement('iframe');
38
newIframe.classList.add('js-test-iframe');
39
newIframe.src = '/test/iframe/' + testCounter;
40
newIframe.addEventListener('load', () => {
43
document.body.appendChild(newIframe);
48
unregisterAllRegistrations: function() {
49
return navigator.serviceWorker.getRegistrations()
50
.then(registrations => {
51
return Promise.all(registrations.map(registration => {
52
registration.unregister();
57
clearAllCaches: function() {
58
return window.caches.keys()
60
return Promise.all(cacheNames.map(cacheName => {
61
window.caches.delete(cacheName);
66
// Waiting for a service worker to install is handy if you only care
67
// about testing events that have occured in the install event
68
installSW: function(swUrl) {
69
return new Promise((resolve, reject) => {
75
options = {scope: iframe.contentWindow.location.pathname};
79
return navigator.serviceWorker.register(swUrl, options);
81
.then(registration => {
82
if (registration.installing === null) {
83
throw new Error(swUrl + ' already installed.');
86
// We unregister all service workers after each test - this should
87
// always trigger an install state change
88
registration.installing.onstatechange = function() {
89
if (this.state !== 'installed') {
102
// To test fetch event behaviour in a service worker you will need to wait
103
// for the service worker to activate
104
activateSW: function(swUrl) {
105
return new Promise((resolve, reject) => {
111
options = {scope: newIframe.contentWindow.location.pathname};
114
return navigator.serviceWorker.register(swUrl, options);
116
.then(registration => {
117
if (registration.installing === null) {
118
throw new Error(swUrl + ' already installed.');
121
// We unregister all service workers after each test - so this should
122
// always have an activate event if the service worker calls
123
// self.clients.claim()
124
registration.installing.onstatechange = function() {
125
if (this.state !== 'activated') {
138
// This is a helper method that checks the cache exists before
139
// getting all the cached responses.
140
// This is limited to text at the moment.
141
getAllCachedAssets: function(cacheName) {
143
return window.caches.has(cacheName)
146
throw new Error('Cache doesn\'t exist.');
149
return window.caches.open(cacheName);
151
.then(openedCache => {
156
return Promise.all(cacheKeys.map(cacheKey => {
157
return cache.match(cacheKey);
160
.then(cacheResponses => {
161
// This method extracts the response streams and pairs
164
cacheResponses.map(response => {
165
output[response.url] = response;
171
// Helper to unregister all service workers and clean all caches
172
// This should be called before each test
173
cleanState: function() {
175
this.unregisterAllRegistrations(),
176
this.clearAllCaches()
179
var iframeList = document.querySelectorAll('.js-test-iframe');
180
for (var i = 0; i < iframeList.length; i++) {
181
iframeList[i].parentElement.removeChild(iframeList[i]);