Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Side by Side Diff: chrome/test/data/budget_service/budget_test.js

Issue 2370103003: Added browser tests to test the BudgetAPI. Also added BudgetAPI to ExperimentalWebPlatformFeatures. (Closed)
Patch Set: Fixed copyright Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 'use strict';
6
7 var resultQueue = new ResultQueue();
8
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);
16 }
17 }
18
19 function sendErrorToTest(error) {
20 sendResultToTest(error.name + ' - ' + error.message);
21 }
22
23 // Queue storing asynchronous results received from the Service Worker. Results
24 // are sent to the test when requested.
25 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.
26 // Invariant: this.queue.length == 0 || this.pendingGets == 0
27 this.queue = [];
28 this.pendingGets = 0;
29 }
30
31 // Adds a data item to the queue. Will be sent to the test if there are
32 // pendingGets.
33 ResultQueue.prototype.push = function(data) {
34 if (this.pendingGets > 0) {
35 this.pendingGets--;
36 sendResultToTest(data);
37 } else {
38 this.queue.unshift(data);
39 }
40 };
41
42 // Called by native. Sends the next data item to the test if it is available.
43 // Otherwise increments pendingGets so it will be delivered when received.
44 ResultQueue.prototype.pop = function() {
45 if (this.queue.length) {
46 sendResultToTest(this.queue.pop());
47 } else {
48 this.pendingGets++;
49 }
50 };
51
52 // Called by native. Immediately sends the next data item to the test if it is
53 // available, otherwise sends null.
54 ResultQueue.prototype.popImmediately = function() {
55 sendResultToTest(this.queue.length ? this.queue.pop() : null);
56 };
57
58 function registerServiceWorker() {
59 // The base dir used to resolve service_worker.js.
60 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
61 function(swRegistration) {
62 sendResultToTest('ok - service worker registered');
63 }, sendErrorToTest);
64 }
65
66 // Query for the budget and return the current total.
67 function documentGetBudget() {
68 navigator.budget.getBudget().then(function(budget) {
69 sendResultToTest("ok - budget returned value of " + budget[0].budgetAt);
70 }, function() {
71 sendResultToTest("failed - unable to get budget values");
72 });
73 }
74
75 // Request a reservation for a silent push.
76 function documentReserveBudget() {
77 navigator.budget.reserve('silent-push').then(function(reserved) {
78 if (reserved)
79 sendResultToTest("ok - reserved budget");
80 else
81 sendResultToTest("failed - not able to reserve budget");
82 }, function() {
83 sendResultToTest("failed - error while trying to reserve budget");
84 });
85 }
86
87 function workerGetBudget() {
88 navigator.serviceWorker.controller.postMessage({command: 'workerGet'});
89 }
90
91 function workerReserveBudget() {
92 navigator.serviceWorker.controller.postMessage({command: 'workerReserve'});
93 }
94
95 function isControlled() {
96 if (navigator.serviceWorker.controller) {
97 sendResultToTest('true - is controlled');
98 } else {
99 sendResultToTest('false - is not controlled');
100 }
101 }
102
103 navigator.serviceWorker.addEventListener('message', function(event) {
104 var message = JSON.parse(event.data);
105 if (message.type == 'push')
106 resultQueue.push(message.data);
107 else
108 sendResultToTest(message.data);
109 }, false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698