Tim van der Lippe | 83f02be | 2020-01-23 11:11:40 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
| 6 | * @fileoverview using private properties isn't a Closure violation in tests. |
| 7 | * @suppress {accessControls} |
| 8 | */ |
| 9 | |
| 10 | ApplicationTestRunner.registerServiceWorker = function(script, scope) { |
| 11 | return TestRunner.callFunctionInPageAsync('registerServiceWorker', [script, scope]); |
| 12 | }; |
| 13 | |
| 14 | ApplicationTestRunner.waitForActivated = function(scope) { |
| 15 | return TestRunner.callFunctionInPageAsync('waitForActivated', [scope]); |
| 16 | }; |
| 17 | |
| 18 | ApplicationTestRunner.unregisterServiceWorker = function(scope) { |
| 19 | return TestRunner.callFunctionInPageAsync('unregisterServiceWorker', [scope]); |
| 20 | }; |
| 21 | |
| 22 | ApplicationTestRunner.postToServiceWorker = function(scope, message) { |
| 23 | return TestRunner.evaluateInPageAnonymously('postToServiceWorker("' + scope + '","' + message + '")'); |
| 24 | }; |
| 25 | |
| 26 | ApplicationTestRunner.waitForServiceWorker = function(callback) { |
Paul Lewis | 4ae5f4f | 2020-01-23 10:19:33 | [diff] [blame] | 27 | self.SDK.targetManager.observeTargets({ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 28 | targetAdded: function(target) { |
Joey Arhar | a6abfa2 | 2019-08-08 12:23:00 | [diff] [blame] | 29 | if (target.type() === SDK.Target.Type.ServiceWorker && callback) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | setTimeout(callback.bind(null, target), 0); |
| 31 | callback = null; |
| 32 | } |
| 33 | }, |
| 34 | |
| 35 | targetRemoved: function(target) {} |
| 36 | }); |
| 37 | }; |
| 38 | |
| 39 | ApplicationTestRunner.dumpServiceWorkersView = function() { |
| 40 | const swView = UI.panels.resources.visibleView; |
| 41 | |
| 42 | return swView._currentWorkersView._sectionList.childTextNodes() |
| 43 | .concat(swView._otherWorkersView._sectionList.childTextNodes()) |
| 44 | .map(function(node) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 45 | if (node.textContent === 'Received ' + (new Date(0)).toLocaleString()) { |
Harley Li | 986e97a | 2018-12-14 22:25:26 | [diff] [blame] | 46 | return 'Invalid scriptResponseTime (unix epoch)'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 47 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 48 | return node.textContent.replace(/Received.*/, 'Received').replace(/#\d+/, '#N'); |
| 49 | }) |
| 50 | .join('\n'); |
| 51 | }; |
| 52 | |
| 53 | ApplicationTestRunner.deleteServiceWorkerRegistration = function(scope) { |
Simon Zünd | a0d4062 | 2020-02-12 13:16:42 | [diff] [blame] | 54 | for (const registration of TestRunner.serviceWorkerManager.registrations().values()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 55 | if (registration.scopeURL === scope) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 56 | TestRunner.serviceWorkerManager.deleteRegistration(registration.id); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 57 | } |
Simon Zünd | a0d4062 | 2020-02-12 13:16:42 | [diff] [blame] | 58 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | ApplicationTestRunner.makeFetchInServiceWorker = function(scope, url, requestInitializer, callback) { |
| 62 | TestRunner.callFunctionInPageAsync('makeFetchInServiceWorker', [scope, url, requestInitializer]).then(callback); |
| 63 | }; |
| 64 | |
| 65 | TestRunner.deprecatedInitAsync(` |
| 66 | let registrations = {}; |
| 67 | |
| 68 | function registerServiceWorker(script, scope) { |
| 69 | return navigator.serviceWorker.register(script, { |
| 70 | scope: scope |
| 71 | }) |
| 72 | .then(reg => registrations[scope] = reg) |
| 73 | .catch(err => { |
| 74 | return Promise.reject(new Error('Service Worker registration error: ' + |
| 75 | err.toString())); |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | function waitForActivated(scope) { |
| 80 | let reg = registrations[scope]; |
| 81 | if (!reg) |
| 82 | return Promise.reject(new Error('The registration')); |
| 83 | let worker = reg.installing || reg.waiting || reg.active; |
| 84 | if (worker.state === 'activated') |
| 85 | return Promise.resolve(); |
| 86 | if (worker.state === 'redundant') |
| 87 | return Promise.reject(new Error('The worker is redundant')); |
| 88 | return new Promise(resolve => { |
| 89 | worker.addEventListener('statechange', () => { |
| 90 | if (worker.state === 'activated') |
| 91 | resolve(); |
| 92 | }); |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | function postToServiceWorker(scope, message) { |
| 97 | registrations[scope].active.postMessage(message); |
| 98 | } |
| 99 | |
| 100 | function unregisterServiceWorker(scope) { |
| 101 | let registration = registrations[scope]; |
| 102 | |
| 103 | if (!registration) |
| 104 | return Promise.reject('ServiceWorker for ' + scope + ' is not registered'); |
| 105 | |
| 106 | return registration.unregister().then(() => delete registrations[scope]); |
| 107 | } |
| 108 | |
| 109 | function makeFetchInServiceWorker(scope, url, requestInitializer) { |
| 110 | let script = 'resources/network-fetch-worker.js'; |
| 111 | |
| 112 | return navigator.serviceWorker.register(script, { |
| 113 | scope: scope |
| 114 | }).then(registration => { |
| 115 | let worker = registration.installing; |
| 116 | |
| 117 | return new Promise(resolve => { |
| 118 | navigator.serviceWorker.onmessage = e => { |
| 119 | resolve(e.data); |
| 120 | }; |
| 121 | |
| 122 | worker.postMessage({ |
| 123 | url: url, |
| 124 | init: requestInitializer |
| 125 | }); |
| 126 | }); |
| 127 | }); |
| 128 | } |
| 129 | `); |