blob: b77aa98c95989d1b18e2a5b3773b65fc654f8737 [file] [log] [blame]
Tim van der Lippe83f02be2020-01-23 11:11:401// Copyright 2017 The Chromium Authors. All rights reserved.
Blink Reformat4c46d092018-04-07 15:32:372// 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
10ApplicationTestRunner.registerServiceWorker = function(script, scope) {
11 return TestRunner.callFunctionInPageAsync('registerServiceWorker', [script, scope]);
12};
13
14ApplicationTestRunner.waitForActivated = function(scope) {
15 return TestRunner.callFunctionInPageAsync('waitForActivated', [scope]);
16};
17
18ApplicationTestRunner.unregisterServiceWorker = function(scope) {
19 return TestRunner.callFunctionInPageAsync('unregisterServiceWorker', [scope]);
20};
21
22ApplicationTestRunner.postToServiceWorker = function(scope, message) {
23 return TestRunner.evaluateInPageAnonymously('postToServiceWorker("' + scope + '","' + message + '")');
24};
25
26ApplicationTestRunner.waitForServiceWorker = function(callback) {
Paul Lewis4ae5f4f2020-01-23 10:19:3327 self.SDK.targetManager.observeTargets({
Blink Reformat4c46d092018-04-07 15:32:3728 targetAdded: function(target) {
Joey Arhara6abfa22019-08-08 12:23:0029 if (target.type() === SDK.Target.Type.ServiceWorker && callback) {
Blink Reformat4c46d092018-04-07 15:32:3730 setTimeout(callback.bind(null, target), 0);
31 callback = null;
32 }
33 },
34
35 targetRemoved: function(target) {}
36 });
37};
38
39ApplicationTestRunner.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 Lippe1d6e57a2019-09-30 11:55:3445 if (node.textContent === 'Received ' + (new Date(0)).toLocaleString()) {
Harley Li986e97a2018-12-14 22:25:2646 return 'Invalid scriptResponseTime (unix epoch)';
Tim van der Lippe1d6e57a2019-09-30 11:55:3447 }
Blink Reformat4c46d092018-04-07 15:32:3748 return node.textContent.replace(/Received.*/, 'Received').replace(/#\d+/, '#N');
49 })
50 .join('\n');
51};
52
53ApplicationTestRunner.deleteServiceWorkerRegistration = function(scope) {
Simon Zünda0d40622020-02-12 13:16:4254 for (const registration of TestRunner.serviceWorkerManager.registrations().values()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3455 if (registration.scopeURL === scope) {
Blink Reformat4c46d092018-04-07 15:32:3756 TestRunner.serviceWorkerManager.deleteRegistration(registration.id);
Tim van der Lippe1d6e57a2019-09-30 11:55:3457 }
Simon Zünda0d40622020-02-12 13:16:4258 }
Blink Reformat4c46d092018-04-07 15:32:3759};
60
61ApplicationTestRunner.makeFetchInServiceWorker = function(scope, url, requestInitializer, callback) {
62 TestRunner.callFunctionInPageAsync('makeFetchInServiceWorker', [scope, url, requestInitializer]).then(callback);
63};
64
65TestRunner.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`);