| Index: chrome/test/data/push_messaging/push_test.js
|
| diff --git a/chrome/test/data/push_messaging/push_test.js b/chrome/test/data/push_messaging/push_test.js
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..70794dd09c230cfd6cead2a5cc9552cd311888f6
|
| --- /dev/null
|
| +++ b/chrome/test/data/push_messaging/push_test.js
|
| @@ -0,0 +1,121 @@
|
| +// Copyright 2014 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 pushData = new FutureData();
|
| +
|
| +// 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);
|
| +}
|
| +
|
| +// A container for a single piece of data. The data does not have to be
|
| +// available yet when the getter is called, as all responses to the test are
|
| +// asynchronous.
|
| +function FutureData() {
|
| + this.data = null;
|
| + this.waiting = false;
|
| +}
|
| +
|
| +// Sends the data to the test if it is available. Otherwise sets the
|
| +// |waiting| flag.
|
| +FutureData.prototype.get = function() {
|
| + if (this.data) {
|
| + sendResultToTest(this.data);
|
| + } else {
|
| + this.waiting = true;
|
| + }
|
| +};
|
| +
|
| +// Sets a new data value. If the |waiting| flag is on, it is turned off and
|
| +// the data is sent to the test.
|
| +FutureData.prototype.set = function(data) {
|
| + this.data = data;
|
| + if (this.waiting) {
|
| + sendResultToTest(data);
|
| + this.waiting = false;
|
| + }
|
| +};
|
| +
|
| +FutureData.prototype.getImmediately = function() {
|
| + sendResultToTest(this.data);
|
| +};
|
| +
|
| +// Notification permission has been coalesced with Push permission. After
|
| +// this is granted, Push API registration can succeed.
|
| +function requestNotificationPermission() {
|
| + Notification.requestPermission(function(permission) {
|
| + sendResultToTest('permission status - ' + permission);
|
| + });
|
| +}
|
| +
|
| +function registerServiceWorker() {
|
| + navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
|
| + function(swRegistration) {
|
| + sendResultToTest('ok - service worker registered');
|
| + }, sendErrorToTest);
|
| +}
|
| +
|
| +function unregisterServiceWorker() {
|
| + navigator.serviceWorker.getRegistration().then(function(swRegistration) {
|
| + swRegistration.unregister().then(function(result) {
|
| + sendResultToTest('service worker unregistration status: ' + result);
|
| + })
|
| + }).catch(sendErrorToTest);
|
| +}
|
| +
|
| +function removeManifest() {
|
| + var element = document.querySelector('link[rel="manifest"]');
|
| + if (element) {
|
| + element.parentNode.removeChild(element);
|
| + sendResultToTest('manifest removed');
|
| + } else
|
| + sendResultToTest('unable to find manifest element');
|
| +}
|
| +
|
| +function registerPush() {
|
| + navigator.serviceWorker.ready.then(function(swRegistration) {
|
| + // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
|
| + var pushManager = swRegistration.pushManager || navigator.push;
|
| + return pushManager.register().then(function(pushRegistration) {
|
| + sendResultToTest(pushRegistration.pushEndpoint + ' - ' +
|
| + pushRegistration.pushRegistrationId);
|
| + });
|
| + }).catch(sendErrorToTest);
|
| +}
|
| +
|
| +function hasPermission() {
|
| + navigator.serviceWorker.ready.then(function(swRegistration) {
|
| + // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
|
| + var pushManager = swRegistration.pushManager || navigator.push;
|
| + return pushManager.hasPermission().then(function(permission) {
|
| + sendResultToTest('permission status - ' + permission);
|
| + });
|
| + }).catch(sendErrorToTest);
|
| +}
|
| +
|
| +function isControlled() {
|
| + if (navigator.serviceWorker.controller) {
|
| + sendResultToTest('true - is controlled');
|
| + } else {
|
| + sendResultToTest('false - is not controlled');
|
| + }
|
| +}
|
| +
|
| +addEventListener('message', function(event) {
|
| + var message = JSON.parse(event.data);
|
| + if (message.type == 'push') {
|
| + pushData.set(message.data);
|
| + }
|
| +}, false);
|
|
|