Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1509)

Side by Side Diff: content/test/data/background_sync/background_sync_test_helpers.js

Issue 1437883002: [Background Sync] Align exposed API with spec (Closed) Base URL: https://chromium.googlesource.com/chromium/src@remove-periodic
Patch Set: Rebase Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 var resultQueue = new ResultQueue(); 7 var resultQueue = new ResultQueue();
8 var registrationReference = null;
9 8
10 // Sends data back to the test. This must be in response to an earlier 9 // Sends data back to the test. This must be in response to an earlier
11 // request, but it's ok to respond asynchronously. The request blocks until 10 // request, but it's ok to respond asynchronously. The request blocks until
12 // the response is sent. 11 // the response is sent.
13 function sendResultToTest(result) { 12 function sendResultToTest(result) {
14 console.log('sendResultToTest: ' + result); 13 console.log('sendResultToTest: ' + result);
15 if (window.domAutomationController) { 14 if (window.domAutomationController) {
16 domAutomationController.send('' + result); 15 domAutomationController.send('' + result);
17 } 16 }
18 } 17 }
19 18
20 function sendErrorToTest(error) { 19 function sendErrorToTest(error) {
21 sendResultToTest(error.name + ' - ' + error.message); 20 sendResultToTest(error.name + ' - ' + error.message);
22 } 21 }
23 22
24 function registerServiceWorker() { 23 function registerServiceWorker() {
25 navigator.serviceWorker.register('service_worker.js', {scope: './'}) 24 navigator.serviceWorker.register('service_worker.js', {scope: './'})
26 .then(function() { 25 .then(function() {
27 return navigator.serviceWorker.ready; 26 return navigator.serviceWorker.ready;
28 }) 27 })
29 .then(function(swRegistration) { 28 .then(function(swRegistration) {
30 sendResultToTest('ok - service worker registered'); 29 sendResultToTest('ok - service worker registered');
31 }) 30 })
32 .catch(sendErrorToTest); 31 .catch(sendErrorToTest);
33 } 32 }
34 33
35 function registerOneShot(tag) { 34 function registerOneShot(tag) {
36 navigator.serviceWorker.ready 35 navigator.serviceWorker.ready
37 .then(function(swRegistration) { 36 .then(function(swRegistration) {
38 return swRegistration.sync.register({'tag': tag}); 37 return swRegistration.sync.register(tag);
39 }) 38 })
40 .then(function(syncRegistration) { 39 .then(function() {
41 sendResultToTest('ok - ' + tag + ' registered'); 40 sendResultToTest('ok - ' + tag + ' registered');
42 }) 41 })
43 .catch(sendErrorToTest); 42 .catch(sendErrorToTest);
44 } 43 }
45 44
46 function registerOneShotFromServiceWorker(tag) { 45 function registerOneShotFromServiceWorker(tag) {
47 navigator.serviceWorker.ready 46 navigator.serviceWorker.ready
48 .then(function(swRegistration) { 47 .then(function(swRegistration) {
49 swRegistration.active.postMessage({action: 'registerOneShot', tag: tag}); 48 swRegistration.active.postMessage({action: 'registerOneShot', tag: tag});
50 sendResultToTest('ok - ' + tag + ' register sent to SW'); 49 sendResultToTest('ok - ' + tag + ' register sent to SW');
51 }) 50 })
52 .catch(sendErrorToTest); 51 .catch(sendErrorToTest);
53 } 52 }
54 53
55 function unregisterOneShot(tag) {
56 navigator.serviceWorker.ready
57 .then(function(swRegistration) {
58 return swRegistration.sync.getRegistration(tag);
59 })
60 .then(function(syncRegistration) {
61 if (!syncRegistration) {
62 sendResultToTest('error - ' + tag + ' not found');
63 return;
64 }
65 return syncRegistration.unregister();
66 })
67 .then(function() {
68 sendResultToTest('ok - ' + tag + ' unregistered');
69 })
70 .catch(sendErrorToTest);
71 }
72
73 function unregisterOneShotTwice(tag) {
74 navigator.serviceWorker.ready
75 .then(function(swRegistration) {
76 return swRegistration.sync.getRegistration(tag);
77 })
78 .then(function(syncRegistration) {
79 if (!syncRegistration) {
80 sendResultToTest('error - ' + tag + ' not found');
81 return;
82 }
83 return syncRegistration.unregister();
84 })
85 .then(function() {
86 return syncRegistration.unregister();
87 })
88 .then(sendErrorToTest, function() {
89 sendResultToTest('ok - ' + tag + ' failed to unregister twice');
90 })
91 .catch(sendErrorToTest);
92 }
93
94 function getRegistrationOneShot(tag) { 54 function getRegistrationOneShot(tag) {
95 navigator.serviceWorker.ready 55 navigator.serviceWorker.ready
96 .then(function(swRegistration) { 56 .then(function(swRegistration) {
97 return swRegistration.sync.getRegistration(tag); 57 return swRegistration.sync.getTags();
98 }) 58 })
99 .then(function(syncRegistration) { 59 .then(function(tags) {
100 if (!syncRegistration) { 60 if (tags.indexOf(tag) >= 0) {
61 sendResultToTest('ok - ' + tag + ' found');
62 } else {
101 sendResultToTest('error - ' + tag + ' not found'); 63 sendResultToTest('error - ' + tag + ' not found');
102 return; 64 return;
103 } 65 }
104 sendResultToTest('ok - ' + tag + ' found');
105 }) 66 })
106 .catch(sendErrorToTest); 67 .catch(sendErrorToTest);
107 } 68 }
108 69
109 function getRegistrationOneShotFromServiceWorker(tag) { 70 function getRegistrationOneShotFromServiceWorker(tag) {
110 navigator.serviceWorker.ready 71 navigator.serviceWorker.ready
111 .then(function(swRegistration) { 72 .then(function(swRegistration) {
112 swRegistration.active.postMessage( 73 swRegistration.active.postMessage(
113 {action: 'getRegistrationOneShot', tag: tag}); 74 {action: 'getRegistrationOneShot', tag: tag});
114 sendResultToTest('ok - getRegistration sent to SW'); 75 sendResultToTest('ok - getRegistration sent to SW');
115 }) 76 })
116 .catch(sendErrorToTest); 77 .catch(sendErrorToTest);
117 } 78 }
118 79
119 function getRegistrationsOneShot(tag) { 80 function getRegistrationsOneShot(tag) {
120 navigator.serviceWorker.ready 81 navigator.serviceWorker.ready
121 .then(function(swRegistration) { 82 .then(function(swRegistration) {
122 return swRegistration.sync.getRegistrations(); 83 return swRegistration.sync.getTags();
123 }) 84 })
124 .then(function(syncRegistrations) { 85 .then(function(tags) {
125 var tags = syncRegistrations.map(function(syncRegistration) {
126 return syncRegistration.tag;
127 });
128 sendResultToTest('ok - ' + tags.toString()); 86 sendResultToTest('ok - ' + tags.toString());
129 }) 87 })
130 .catch(sendErrorToTest); 88 .catch(sendErrorToTest);
131 } 89 }
132 90
133 function getRegistrationsOneShotFromServiceWorker() { 91 function getRegistrationsOneShotFromServiceWorker() {
134 navigator.serviceWorker.ready 92 navigator.serviceWorker.ready
135 .then(function(swRegistration) { 93 .then(function(swRegistration) {
136 swRegistration.active.postMessage({action: 'getRegistrationsOneShot'}); 94 swRegistration.active.postMessage({action: 'getRegistrationsOneShot'});
137 sendResultToTest('ok - getRegistrations sent to SW'); 95 sendResultToTest('ok - getRegistrations sent to SW');
(...skipping 12 matching lines...) Expand all
150 108
151 function rejectDelayedOneShot() { 109 function rejectDelayedOneShot() {
152 navigator.serviceWorker.ready 110 navigator.serviceWorker.ready
153 .then(function(swRegistration) { 111 .then(function(swRegistration) {
154 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'}); 112 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'});
155 sendResultToTest('ok - delay rejecting'); 113 sendResultToTest('ok - delay rejecting');
156 }) 114 })
157 .catch(sendErrorToTest); 115 .catch(sendErrorToTest);
158 } 116 }
159 117
160 function notifyWhenFinishedOneShot(tag) {
161 navigator.serviceWorker.ready
162 .then(function(swRegistration) {
163 swRegistration.active.postMessage(
164 {action: 'notifyWhenFinished', tag: tag});
165 })
166 .catch(sendErrorToTest);
167 }
168
169 function notifyWhenFinishedImmediateOneShot(tag) {
170 if (registrationReference == null) {
171 sendResultToTest('error - must call storeRegistration first');
172 return;
173 }
174
175 registrationReference.finished
176 .then(function(success) {
177 sendResultToTest('ok - ' + registrationReference.tag +
178 ' result: true')
179 }, function(err) {
180 sendResultToTest('ok - ' + registrationReference.tag +
181 ' result: false')
182 })
183 .catch(sendErrorToTest)
184 }
185
186
187 function storeRegistration(tag) {
188 navigator.serviceWorker.ready
189 .then(function(swRegistration) {
190 return swRegistration.sync.getRegistration(tag);
191 })
192 .then(function(syncRegistration) {
193 registrationReference = syncRegistration;
194 sendResultToTest('ok - ' + tag + ' stored');
195 })
196 .catch(sendErrorToTest);
197 }
198 118
199 // Queue storing asynchronous results received from the Service Worker. Results 119 // Queue storing asynchronous results received from the Service Worker. Results
200 // are sent to the test when requested. 120 // are sent to the test when requested.
201 function ResultQueue() { 121 function ResultQueue() {
202 // Invariant: this.queue.length == 0 || this.pendingGets == 0 122 // Invariant: this.queue.length == 0 || this.pendingGets == 0
203 this.queue = []; 123 this.queue = [];
204 this.pendingGets = 0; 124 this.pendingGets = 0;
205 } 125 }
206 126
207 // Adds a data item to the queue. Will be sent to the test if there are 127 // Adds a data item to the queue. Will be sent to the test if there are
(...skipping 21 matching lines...) Expand all
229 // available, otherwise sends null. 149 // available, otherwise sends null.
230 ResultQueue.prototype.popImmediately = function() { 150 ResultQueue.prototype.popImmediately = function() {
231 sendResultToTest(this.queue.length ? this.queue.pop() : null); 151 sendResultToTest(this.queue.length ? this.queue.pop() : null);
232 }; 152 };
233 153
234 navigator.serviceWorker.addEventListener('message', function(event) { 154 navigator.serviceWorker.addEventListener('message', function(event) {
235 var message = event.data; 155 var message = event.data;
236 if (message.type == 'sync' || message.type === 'register') 156 if (message.type == 'sync' || message.type === 'register')
237 resultQueue.push(message.data); 157 resultQueue.push(message.data);
238 }, false); 158 }, false);
OLDNEW
« no previous file with comments | « content/browser/background_sync/background_sync_browsertest.cc ('k') | content/test/data/background_sync/service_worker.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698