OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <title>Push API Test</title> | 4 <title>Push API Test</title> |
5 <link rel="manifest" href="manifest.json"> | 5 <link rel="manifest" href="manifest.json"> |
6 <script> | 6 <script> |
| 7 var pushData = new FutureData(); |
| 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. |
7 function sendResultToTest(result) { | 12 function sendResultToTest(result) { |
8 console.log(result); | 13 console.log('sendResultToTest: ' + result); |
9 if (window.domAutomationController) { | 14 if (window.domAutomationController) { |
10 domAutomationController.send('' + result); | 15 domAutomationController.send('' + result); |
11 } | 16 } |
12 } | 17 } |
13 | 18 |
14 function sendErrorToTest(error) { | 19 function sendErrorToTest(error) { |
15 sendResultToTest(error.name + ' - ' + error.message); | 20 sendResultToTest(error.name + ' - ' + error.message); |
16 } | 21 } |
17 | 22 |
| 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 |
| 25 // asynchronous. |
| 26 function FutureData() { |
| 27 this.data = null; |
| 28 this.waiting = false; |
| 29 } |
| 30 |
| 31 // Sends the data to the test if it is available. Otherwise sets the |
| 32 // |waiting| flag. |
| 33 FutureData.prototype.get = function() { |
| 34 if (this.data) { |
| 35 sendResultToTest(this.data); |
| 36 } else { |
| 37 this.waiting = true; |
| 38 } |
| 39 }; |
| 40 |
| 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) { |
| 44 this.data = data; |
| 45 if (this.waiting) { |
| 46 sendResultToTest(data); |
| 47 this.waiting = false; |
| 48 } |
| 49 }; |
| 50 |
18 function registerServiceWorker() { | 51 function registerServiceWorker() { |
19 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
function(swRegistration) { | 52 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( |
20 console.log(swRegistration); | 53 function(swRegistration) { |
21 sendResultToTest('ok'); | 54 sendResultToTest('ok - service worker registered'); |
22 }, sendErrorToTest); | 55 }, sendErrorToTest); |
23 } | 56 } |
24 | 57 |
25 function registerPush() { | 58 function registerPush() { |
26 navigator.serviceWorker.ready.then(function() { | 59 navigator.serviceWorker.ready.then(function() { |
27 navigator.push.register().then(function(pushRegistration) { | 60 navigator.push.register().then(function(pushRegistration) { |
28 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistrat
ion.pushRegistrationId); | 61 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + |
| 62 pushRegistration.pushRegistrationId); |
29 }, sendErrorToTest); | 63 }, sendErrorToTest); |
30 }, sendErrorToTest); | 64 }, sendErrorToTest); |
31 } | 65 } |
| 66 |
| 67 function isControlled() { |
| 68 if (navigator.serviceWorker.controller) { |
| 69 sendResultToTest('true - is controlled'); |
| 70 } else { |
| 71 sendResultToTest('false - is not controlled'); |
| 72 } |
| 73 } |
| 74 |
| 75 addEventListener('message', function(event) { |
| 76 var message = JSON.parse(event.data); |
| 77 if (message.type == 'push') { |
| 78 pushData.set(message.data); |
| 79 } |
| 80 }, false); |
32 </script> | 81 </script> |
33 </head> | 82 </head> |
34 <body>Push API Test</body> | 83 <body> |
| 84 <h1>Push API Test</h1> |
| 85 </body> |
35 </html> | 86 </html> |
OLD | NEW |