4 <title>Push API Test
</title>
5 <link rel=
"manifest" href=
"manifest.json">
7 var pushData
= new FutureData();
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 // A container for a single piece of data. The data does not have to be
24 // available yet when the getter is called, as all responses to the test are
26 function FutureData() {
31 // Sends the data to the test if it is available. Otherwise sets the
33 FutureData
.prototype.get = function() {
35 sendResultToTest(this.data
);
41 // Sets a new data value. If the |waiting| flag is on, it is turned off and
42 // the data is sent to the test.
43 FutureData
.prototype.set = function(data
) {
46 sendResultToTest(data
);
51 function requestNotificationPermission() {
52 Notification
.requestPermission(function(permission
) {
53 sendResultToTest('permission status - ' + permission
);
57 function registerServiceWorker() {
58 navigator
.serviceWorker
.register('service_worker.js', {scope
: './'}).then(
59 function(swRegistration
) {
60 sendResultToTest('ok - service worker registered');
64 function removeManifest() {
65 var element
= document
.querySelector('link[rel="manifest"]');
67 element
.parentNode
.removeChild(element
);
68 sendResultToTest('manifest removed');
70 sendResultToTest('unable to find manifest element');
73 function registerPush() {
74 navigator
.serviceWorker
.ready
.then(function() {
75 navigator
.push
.register().then(function(pushRegistration
) {
76 sendResultToTest(pushRegistration
.pushEndpoint
+ ' - ' +
77 pushRegistration
.pushRegistrationId
);
82 function isControlled() {
83 if (navigator
.serviceWorker
.controller
) {
84 sendResultToTest('true - is controlled');
86 sendResultToTest('false - is not controlled');
90 addEventListener('message', function(event
) {
91 var message
= JSON
.parse(event
.data
);
92 if (message
.type
== 'push') {
93 pushData
.set(message
.data
);
99 <h1>Push API Test
</h1>