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();
9 // Sends data back to the test. This must be in response to an earlier
10 // request, but it's ok to respond asynchronously. The request blocks until
11 // the response is sent.
12 function sendResultToTest(result) {
13 console.log('sendResultToTest: ' + result);
14 if (window.domAutomationController) {
15 domAutomationController.send('' + result);
19 function sendErrorToTest(error) {
20 sendResultToTest(error.name + ' - ' + error.message);
23 function registerServiceWorker() {
24 navigator.serviceWorker.register('service_worker.js', {scope: './'})
26 return navigator.serviceWorker.ready;
28 .then(function(swRegistration) {
29 sendResultToTest('ok - service worker registered');
31 .catch(sendErrorToTest);
34 function registerOneShot(tag) {
35 navigator.serviceWorker.ready
36 .then(function(swRegistration) {
37 return swRegistration.sync.register({'tag': tag});
39 .then(function(syncRegistration) {
40 sendResultToTest('ok - ' + tag + ' registered');
42 .catch(sendErrorToTest);
45 function getRegistrationOneShot(tag) {
46 navigator.serviceWorker.ready
47 .then(function(swRegistration) {
48 return swRegistration.sync.getRegistration(tag);
50 .then(function(syncRegistration) {
51 if (!syncRegistration) {
52 sendResultToTest('error - ' + tag + ' not found');
55 sendResultToTest('ok - ' + tag + ' found');
57 .catch(sendErrorToTest);
60 function getRegistrationsOneShot(tag) {
61 navigator.serviceWorker.ready
62 .then(function(swRegistration) {
63 return swRegistration.sync.getRegistrations();
65 .then(function(syncRegistrations) {
66 var tags = syncRegistrations.map(function(syncRegistration) {
67 return syncRegistration.tag;
69 sendResultToTest('ok - ' + tags.toString());
71 .catch(sendErrorToTest);
74 function completeDelayedOneShot() {
75 navigator.serviceWorker.ready
76 .then(function(swRegistration) {
77 swRegistration.active.postMessage('completeDelayedOneShot');
78 sendResultToTest('ok - delay completing');
80 .catch(sendErrorToTest);
83 function rejectDelayedOneShot() {
84 navigator.serviceWorker.ready
85 .then(function(swRegistration) {
86 swRegistration.active.postMessage('rejectDelayedOneShot');
87 sendResultToTest('ok - delay rejecting');
89 .catch(sendErrorToTest);
92 // Queue storing asynchronous results received from the Service Worker. Results
93 // are sent to the test when requested.
94 function ResultQueue() {
95 // Invariant: this.queue.length == 0 || this.pendingGets == 0
100 // Adds a data item to the queue. Will be sent to the test if there are
102 ResultQueue.prototype.push = function(data) {
103 if (this.pendingGets) {
105 sendResultToTest(data);
107 this.queue.unshift(data);
111 // Called by native. Sends the next data item to the test if it is available.
112 // Otherwise increments pendingGets so it will be delivered when received.
113 ResultQueue.prototype.pop = function() {
114 if (this.queue.length) {
115 sendResultToTest(this.queue.pop());
121 // Called by native. Immediately sends the next data item to the test if it is
122 // available, otherwise sends null.
123 ResultQueue.prototype.popImmediately = function() {
124 sendResultToTest(this.queue.length ? this.queue.pop() : null);
127 navigator.serviceWorker.addEventListener('message', function(event) {
128 var message = event.data;
129 if (message.type == 'sync')
130 resultQueue.push(message.data);