| OLD | NEW |
| 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; | 8 var registrationReference = null; |
| 9 | 9 |
| 10 // Sends data back to the test. This must be in response to an earlier | 10 // Sends data back to the test. This must be in response to an earlier |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 }) | 28 }) |
| 29 .then(function(swRegistration) { | 29 .then(function(swRegistration) { |
| 30 sendResultToTest('ok - service worker registered'); | 30 sendResultToTest('ok - service worker registered'); |
| 31 }) | 31 }) |
| 32 .catch(sendErrorToTest); | 32 .catch(sendErrorToTest); |
| 33 } | 33 } |
| 34 | 34 |
| 35 function registerOneShot(tag) { | 35 function registerOneShot(tag) { |
| 36 navigator.serviceWorker.ready | 36 navigator.serviceWorker.ready |
| 37 .then(function(swRegistration) { | 37 .then(function(swRegistration) { |
| 38 return swRegistration.sync.register({'tag': tag}); | 38 return swRegistration.sync.register(tag); |
| 39 }) | 39 }) |
| 40 .then(function(syncRegistration) { | 40 .then(function() { |
| 41 sendResultToTest('ok - ' + tag + ' registered'); | 41 sendResultToTest('ok - ' + tag + ' registered'); |
| 42 }) | 42 }) |
| 43 .catch(sendErrorToTest); | 43 .catch(sendErrorToTest); |
| 44 } | 44 } |
| 45 | 45 |
| 46 function registerOneShotFromServiceWorker(tag) { | 46 function registerOneShotFromServiceWorker(tag) { |
| 47 navigator.serviceWorker.ready | 47 navigator.serviceWorker.ready |
| 48 .then(function(swRegistration) { | 48 .then(function(swRegistration) { |
| 49 swRegistration.active.postMessage({action: 'registerOneShot', tag: tag}); | 49 swRegistration.active.postMessage({action: 'registerOneShot', tag: tag}); |
| 50 sendResultToTest('ok - ' + tag + ' register sent to SW'); | 50 sendResultToTest('ok - ' + tag + ' register sent to SW'); |
| 51 }) | 51 }) |
| 52 .catch(sendErrorToTest); | 52 .catch(sendErrorToTest); |
| 53 } | 53 } |
| 54 | 54 |
| 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) { | 55 function getRegistrationOneShot(tag) { |
| 95 navigator.serviceWorker.ready | 56 navigator.serviceWorker.ready |
| 96 .then(function(swRegistration) { | 57 .then(function(swRegistration) { |
| 97 return swRegistration.sync.getRegistration(tag); | 58 return swRegistration.sync.getTags(); |
| 98 }) | 59 }) |
| 99 .then(function(syncRegistration) { | 60 .then(function(tags) { |
| 100 if (!syncRegistration) { | 61 if (tags.indexOf(tag) >= 0) { |
| 62 sendResultToTest('ok - ' + tag + ' found'); |
| 63 } else { |
| 101 sendResultToTest('error - ' + tag + ' not found'); | 64 sendResultToTest('error - ' + tag + ' not found'); |
| 102 return; | 65 return; |
| 103 } | 66 } |
| 104 sendResultToTest('ok - ' + tag + ' found'); | |
| 105 }) | 67 }) |
| 106 .catch(sendErrorToTest); | 68 .catch(sendErrorToTest); |
| 107 } | 69 } |
| 108 | 70 |
| 109 function getRegistrationOneShotFromServiceWorker(tag) { | 71 function getRegistrationOneShotFromServiceWorker(tag) { |
| 110 navigator.serviceWorker.ready | 72 navigator.serviceWorker.ready |
| 111 .then(function(swRegistration) { | 73 .then(function(swRegistration) { |
| 112 swRegistration.active.postMessage( | 74 swRegistration.active.postMessage( |
| 113 {action: 'getRegistrationOneShot', tag: tag}); | 75 {action: 'getRegistrationOneShot', tag: tag}); |
| 114 sendResultToTest('ok - getRegistration sent to SW'); | 76 sendResultToTest('ok - getRegistration sent to SW'); |
| 115 }) | 77 }) |
| 116 .catch(sendErrorToTest); | 78 .catch(sendErrorToTest); |
| 117 } | 79 } |
| 118 | 80 |
| 119 function getRegistrationsOneShot(tag) { | 81 function getRegistrationsOneShot(tag) { |
| 120 navigator.serviceWorker.ready | 82 navigator.serviceWorker.ready |
| 121 .then(function(swRegistration) { | 83 .then(function(swRegistration) { |
| 122 return swRegistration.sync.getRegistrations(); | 84 return swRegistration.sync.getTags(); |
| 123 }) | 85 }) |
| 124 .then(function(syncRegistrations) { | 86 .then(function(tags) { |
| 125 var tags = syncRegistrations.map(function(syncRegistration) { | |
| 126 return syncRegistration.tag; | |
| 127 }); | |
| 128 sendResultToTest('ok - ' + tags.toString()); | 87 sendResultToTest('ok - ' + tags.toString()); |
| 129 }) | 88 }) |
| 130 .catch(sendErrorToTest); | 89 .catch(sendErrorToTest); |
| 131 } | 90 } |
| 132 | 91 |
| 133 function getRegistrationsOneShotFromServiceWorker() { | 92 function getRegistrationsOneShotFromServiceWorker() { |
| 134 navigator.serviceWorker.ready | 93 navigator.serviceWorker.ready |
| 135 .then(function(swRegistration) { | 94 .then(function(swRegistration) { |
| 136 swRegistration.active.postMessage({action: 'getRegistrationsOneShot'}); | 95 swRegistration.active.postMessage({action: 'getRegistrationsOneShot'}); |
| 137 sendResultToTest('ok - getRegistrations sent to SW'); | 96 sendResultToTest('ok - getRegistrations sent to SW'); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 150 | 109 |
| 151 function rejectDelayedOneShot() { | 110 function rejectDelayedOneShot() { |
| 152 navigator.serviceWorker.ready | 111 navigator.serviceWorker.ready |
| 153 .then(function(swRegistration) { | 112 .then(function(swRegistration) { |
| 154 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'}); | 113 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'}); |
| 155 sendResultToTest('ok - delay rejecting'); | 114 sendResultToTest('ok - delay rejecting'); |
| 156 }) | 115 }) |
| 157 .catch(sendErrorToTest); | 116 .catch(sendErrorToTest); |
| 158 } | 117 } |
| 159 | 118 |
| 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 | 119 |
| 199 // Queue storing asynchronous results received from the Service Worker. Results | 120 // Queue storing asynchronous results received from the Service Worker. Results |
| 200 // are sent to the test when requested. | 121 // are sent to the test when requested. |
| 201 function ResultQueue() { | 122 function ResultQueue() { |
| 202 // Invariant: this.queue.length == 0 || this.pendingGets == 0 | 123 // Invariant: this.queue.length == 0 || this.pendingGets == 0 |
| 203 this.queue = []; | 124 this.queue = []; |
| 204 this.pendingGets = 0; | 125 this.pendingGets = 0; |
| 205 } | 126 } |
| 206 | 127 |
| 207 // Adds a data item to the queue. Will be sent to the test if there are | 128 // Adds a data item to the queue. Will be sent to the test if there are |
| (...skipping 21 matching lines...) Expand all Loading... |
| 229 // available, otherwise sends null. | 150 // available, otherwise sends null. |
| 230 ResultQueue.prototype.popImmediately = function() { | 151 ResultQueue.prototype.popImmediately = function() { |
| 231 sendResultToTest(this.queue.length ? this.queue.pop() : null); | 152 sendResultToTest(this.queue.length ? this.queue.pop() : null); |
| 232 }; | 153 }; |
| 233 | 154 |
| 234 navigator.serviceWorker.addEventListener('message', function(event) { | 155 navigator.serviceWorker.addEventListener('message', function(event) { |
| 235 var message = event.data; | 156 var message = event.data; |
| 236 if (message.type == 'sync' || message.type === 'register') | 157 if (message.type == 'sync' || message.type === 'register') |
| 237 resultQueue.push(message.data); | 158 resultQueue.push(message.data); |
| 238 }, false); | 159 }, false); |
| OLD | NEW |