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
3 // found in the LICENSE file.
7 var resultQueue = new ResultQueue();
8 var registrationReference = null;
10 // 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
12 // the response is sent.
13 function sendResultToTest(result) {
14 console.log('sendResultToTest: ' + result);
15 if (window.domAutomationController) {
16 domAutomationController.send('' + result);
20 function sendErrorToTest(error) {
21 sendResultToTest(error.name + ' - ' + error.message);
24 function registerServiceWorker() {
25 navigator.serviceWorker.register('service_worker.js', {scope: './'})
27 return navigator.serviceWorker.ready;
29 .then(function(swRegistration) {
30 sendResultToTest('ok - service worker registered');
32 .catch(sendErrorToTest);
35 function registerOneShot(tag) {
36 navigator.serviceWorker.ready
37 .then(function(swRegistration) {
38 return swRegistration.sync.register({'tag': tag});
40 .then(function(syncRegistration) {
41 sendResultToTest('ok - ' + tag + ' registered');
43 .catch(sendErrorToTest);
46 function unregisterOneShot(tag) {
47 navigator.serviceWorker.ready
48 .then(function(swRegistration) {
49 return swRegistration.sync.getRegistration(tag);
51 .then(function(syncRegistration) {
52 if (!syncRegistration) {
53 sendResultToTest('error - ' + tag + ' not found');
56 return syncRegistration.unregister();
59 sendResultToTest('ok - ' + tag + ' unregistered');
61 .catch(sendErrorToTest);
64 function unregisterOneShotTwice(tag) {
65 navigator.serviceWorker.ready
66 .then(function(swRegistration) {
67 return swRegistration.sync.getRegistration(tag);
69 .then(function(syncRegistration) {
70 if (!syncRegistration) {
71 sendResultToTest('error - ' + tag + ' not found');
74 return syncRegistration.unregister();
77 return syncRegistration.unregister();
79 .then(sendErrorToTest, function() {
80 sendResultToTest('ok - ' + tag + ' failed to unregister twice');
82 .catch(sendErrorToTest);
85 function getRegistrationOneShot(tag) {
86 navigator.serviceWorker.ready
87 .then(function(swRegistration) {
88 return swRegistration.sync.getRegistration(tag);
90 .then(function(syncRegistration) {
91 if (!syncRegistration) {
92 sendResultToTest('error - ' + tag + ' not found');
95 sendResultToTest('ok - ' + tag + ' found');
97 .catch(sendErrorToTest);
100 function getRegistrationsOneShot(tag) {
101 navigator.serviceWorker.ready
102 .then(function(swRegistration) {
103 return swRegistration.sync.getRegistrations();
105 .then(function(syncRegistrations) {
106 var tags = syncRegistrations.map(function(syncRegistration) {
107 return syncRegistration.tag;
109 sendResultToTest('ok - ' + tags.toString());
111 .catch(sendErrorToTest);
114 function completeDelayedOneShot() {
115 navigator.serviceWorker.ready
116 .then(function(swRegistration) {
117 swRegistration.active.postMessage({action: 'completeDelayedOneShot'});
118 sendResultToTest('ok - delay completing');
120 .catch(sendErrorToTest);
123 function rejectDelayedOneShot() {
124 navigator.serviceWorker.ready
125 .then(function(swRegistration) {
126 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'});
127 sendResultToTest('ok - delay rejecting');
129 .catch(sendErrorToTest);
132 function notifyWhenDoneOneShot(tag) {
133 navigator.serviceWorker.ready
134 .then(function(swRegistration) {
135 swRegistration.active.postMessage({action: 'notifyWhenDone', tag: tag});
137 .catch(sendErrorToTest);
140 function notifyWhenDoneImmediateOneShot(tag) {
141 if (registrationReference == null) {
142 sendResultToTest('error - must call storeRegistration first');
146 registrationReference.done
147 .then(function(success) {
148 sendResultToTest('ok - ' + registrationReference.tag +
149 ' result: ' + success)
151 sendResultToTest('error - ' + registrationReference.tag + ' failed');
153 .catch(sendErrorToTest)
157 function storeRegistration(tag) {
158 navigator.serviceWorker.ready
159 .then(function(swRegistration) {
160 return swRegistration.sync.getRegistration(tag);
162 .then(function(syncRegistration) {
163 registrationReference = syncRegistration;
164 sendResultToTest('ok - ' + tag + ' stored');
166 .catch(sendErrorToTest);
169 // Queue storing asynchronous results received from the Service Worker. Results
170 // are sent to the test when requested.
171 function ResultQueue() {
172 // Invariant: this.queue.length == 0 || this.pendingGets == 0
174 this.pendingGets = 0;
177 // Adds a data item to the queue. Will be sent to the test if there are
179 ResultQueue.prototype.push = function(data) {
180 if (this.pendingGets) {
182 sendResultToTest(data);
184 this.queue.unshift(data);
188 // Called by native. Sends the next data item to the test if it is available.
189 // Otherwise increments pendingGets so it will be delivered when received.
190 ResultQueue.prototype.pop = function() {
191 if (this.queue.length) {
192 sendResultToTest(this.queue.pop());
198 // Called by native. Immediately sends the next data item to the test if it is
199 // available, otherwise sends null.
200 ResultQueue.prototype.popImmediately = function() {
201 sendResultToTest(this.queue.length ? this.queue.pop() : null);
204 navigator.serviceWorker.addEventListener('message', function(event) {
205 var message = event.data;
206 if (message.type == 'sync')
207 resultQueue.push(message.data);