Chromium Code Reviews| Index: chrome/test/data/budget_service/budget_test.js |
| diff --git a/chrome/test/data/budget_service/budget_test.js b/chrome/test/data/budget_service/budget_test.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..21a189dae9171e42968be8e265d7e3efb9a2e90c |
| --- /dev/null |
| +++ b/chrome/test/data/budget_service/budget_test.js |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +'use strict'; |
| + |
| +var resultQueue = new ResultQueue(); |
| + |
| +// Sends data back to the test. This must be in response to an earlier |
| +// request, but it's ok to respond asynchronously. The request blocks until |
| +// the response is sent. |
| +function sendResultToTest(result) { |
| + console.log('sendResultToTest: ' + result); |
| + if (window.domAutomationController) { |
| + domAutomationController.send('' + result); |
| + } |
| +} |
| + |
| +function sendErrorToTest(error) { |
| + sendResultToTest(error.name + ' - ' + error.message); |
| +} |
| + |
| +// Queue storing asynchronous results received from the Service Worker. Results |
| +// are sent to the test when requested. |
| +function ResultQueue() { |
|
Peter Beverloo
2016/09/27 15:57:47
Could we share this code with the push messaging t
harkness
2016/09/28 12:29:21
Done.
|
| + // Invariant: this.queue.length == 0 || this.pendingGets == 0 |
| + this.queue = []; |
| + this.pendingGets = 0; |
| +} |
| + |
| +// Adds a data item to the queue. Will be sent to the test if there are |
| +// pendingGets. |
| +ResultQueue.prototype.push = function(data) { |
| + if (this.pendingGets > 0) { |
| + this.pendingGets--; |
| + sendResultToTest(data); |
| + } else { |
| + this.queue.unshift(data); |
| + } |
| +}; |
| + |
| +// Called by native. Sends the next data item to the test if it is available. |
| +// Otherwise increments pendingGets so it will be delivered when received. |
| +ResultQueue.prototype.pop = function() { |
| + if (this.queue.length) { |
| + sendResultToTest(this.queue.pop()); |
| + } else { |
| + this.pendingGets++; |
| + } |
| +}; |
| + |
| +// Called by native. Immediately sends the next data item to the test if it is |
| +// available, otherwise sends null. |
| +ResultQueue.prototype.popImmediately = function() { |
| + sendResultToTest(this.queue.length ? this.queue.pop() : null); |
| +}; |
| + |
| +function registerServiceWorker() { |
| + // The base dir used to resolve service_worker.js. |
| + navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( |
| + function(swRegistration) { |
| + sendResultToTest('ok - service worker registered'); |
| + }, sendErrorToTest); |
| +} |
| + |
| +// Query for the budget and return the current total. |
| +function documentGetBudget() { |
| + navigator.budget.getBudget().then(function(budget) { |
| + sendResultToTest("ok - budget returned value of " + budget[0].budgetAt); |
| + }, function() { |
| + sendResultToTest("failed - unable to get budget values"); |
| + }); |
| +} |
| + |
| +// Request a reservation for a silent push. |
| +function documentReserveBudget() { |
| + navigator.budget.reserve('silent-push').then(function(reserved) { |
| + if (reserved) |
| + sendResultToTest("ok - reserved budget"); |
| + else |
| + sendResultToTest("failed - not able to reserve budget"); |
| + }, function() { |
| + sendResultToTest("failed - error while trying to reserve budget"); |
| + }); |
| +} |
| + |
| +function workerGetBudget() { |
| + navigator.serviceWorker.controller.postMessage({command: 'workerGet'}); |
| +} |
| + |
| +function workerReserveBudget() { |
| + navigator.serviceWorker.controller.postMessage({command: 'workerReserve'}); |
| +} |
| + |
| +function isControlled() { |
| + if (navigator.serviceWorker.controller) { |
| + sendResultToTest('true - is controlled'); |
| + } else { |
| + sendResultToTest('false - is not controlled'); |
| + } |
| +} |
| + |
| +navigator.serviceWorker.addEventListener('message', function(event) { |
| + var message = JSON.parse(event.data); |
| + if (message.type == 'push') |
| + resultQueue.push(message.data); |
| + else |
| + sendResultToTest(message.data); |
| +}, false); |