Roll src/third_party/WebKit a452221:9ff6d11 (svn 202117:202119)
[chromium-blink-merge.git] / content / test / data / background_sync / background_sync_test_helpers.js
blobe48bfb85ab2cdd2b6a64babffc906d39bd34a2a5
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.
5 'use strict';
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: './'})
25 .then(function() {
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 unregisterOneShot(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');
53 return;
55 return syncRegistration.unregister();
57 .then(function() {
58 sendResultToTest('ok - ' + tag + ' unregistered');
60 .catch(sendErrorToTest);
63 function unregisterOneShotTwice(tag) {
64 navigator.serviceWorker.ready
65 .then(function(swRegistration) {
66 return swRegistration.sync.getRegistration(tag);
68 .then(function(syncRegistration) {
69 if (!syncRegistration) {
70 sendResultToTest('error - ' + tag + ' not found');
71 return;
73 return syncRegistration.unregister();
75 .then(function() {
76 return syncRegistration.unregister();
78 .then(sendErrorToTest, function() {
79 sendResultToTest('ok - ' + tag + ' failed to unregister twice');
81 .catch(sendErrorToTest);
84 function getRegistrationOneShot(tag) {
85 navigator.serviceWorker.ready
86 .then(function(swRegistration) {
87 return swRegistration.sync.getRegistration(tag);
89 .then(function(syncRegistration) {
90 if (!syncRegistration) {
91 sendResultToTest('error - ' + tag + ' not found');
92 return;
94 sendResultToTest('ok - ' + tag + ' found');
96 .catch(sendErrorToTest);
99 function getRegistrationsOneShot(tag) {
100 navigator.serviceWorker.ready
101 .then(function(swRegistration) {
102 return swRegistration.sync.getRegistrations();
104 .then(function(syncRegistrations) {
105 var tags = syncRegistrations.map(function(syncRegistration) {
106 return syncRegistration.tag;
108 sendResultToTest('ok - ' + tags.toString());
110 .catch(sendErrorToTest);
113 function completeDelayedOneShot() {
114 navigator.serviceWorker.ready
115 .then(function(swRegistration) {
116 swRegistration.active.postMessage('completeDelayedOneShot');
117 sendResultToTest('ok - delay completing');
119 .catch(sendErrorToTest);
122 function rejectDelayedOneShot() {
123 navigator.serviceWorker.ready
124 .then(function(swRegistration) {
125 swRegistration.active.postMessage('rejectDelayedOneShot');
126 sendResultToTest('ok - delay rejecting');
128 .catch(sendErrorToTest);
131 // Queue storing asynchronous results received from the Service Worker. Results
132 // are sent to the test when requested.
133 function ResultQueue() {
134 // Invariant: this.queue.length == 0 || this.pendingGets == 0
135 this.queue = [];
136 this.pendingGets = 0;
139 // Adds a data item to the queue. Will be sent to the test if there are
140 // pendingGets.
141 ResultQueue.prototype.push = function(data) {
142 if (this.pendingGets) {
143 this.pendingGets--;
144 sendResultToTest(data);
145 } else {
146 this.queue.unshift(data);
150 // Called by native. Sends the next data item to the test if it is available.
151 // Otherwise increments pendingGets so it will be delivered when received.
152 ResultQueue.prototype.pop = function() {
153 if (this.queue.length) {
154 sendResultToTest(this.queue.pop());
155 } else {
156 this.pendingGets++;
160 // Called by native. Immediately sends the next data item to the test if it is
161 // available, otherwise sends null.
162 ResultQueue.prototype.popImmediately = function() {
163 sendResultToTest(this.queue.length ? this.queue.pop() : null);
166 navigator.serviceWorker.addEventListener('message', function(event) {
167 var message = event.data;
168 if (message.type == 'sync')
169 resultQueue.push(message.data);
170 }, false);