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(); | 7 var pushData = new FutureData(); |
| 8 var pushRegistration = null; |
8 | 9 |
9 // Sends data back to the test. This must be in response to an earlier | 10 // 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 // request, but it's ok to respond asynchronously. The request blocks until |
11 // the response is sent. | 12 // the response is sent. |
12 function sendResultToTest(result) { | 13 function sendResultToTest(result) { |
13 console.log('sendResultToTest: ' + result); | 14 console.log('sendResultToTest: ' + result); |
14 if (window.domAutomationController) { | 15 if (window.domAutomationController) { |
15 domAutomationController.send('' + result); | 16 domAutomationController.send('' + result); |
16 } | 17 } |
17 } | 18 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 element.parentNode.removeChild(element); | 82 element.parentNode.removeChild(element); |
82 sendResultToTest('manifest removed'); | 83 sendResultToTest('manifest removed'); |
83 } else | 84 } else |
84 sendResultToTest('unable to find manifest element'); | 85 sendResultToTest('unable to find manifest element'); |
85 } | 86 } |
86 | 87 |
87 function registerPush() { | 88 function registerPush() { |
88 navigator.serviceWorker.ready.then(function(swRegistration) { | 89 navigator.serviceWorker.ready.then(function(swRegistration) { |
89 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. | 90 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. |
90 var pushManager = swRegistration.pushManager || navigator.push; | 91 var pushManager = swRegistration.pushManager || navigator.push; |
91 pushManager.register().then(function(pushRegistration) { | 92 pushManager.register().then(function(registration) { |
| 93 pushRegistration = registration; |
92 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + | 94 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + |
93 pushRegistration.pushRegistrationId); | 95 pushRegistration.pushRegistrationId); |
94 }, sendErrorToTest); | 96 }, sendErrorToTest); |
95 }, sendErrorToTest); | 97 }, sendErrorToTest); |
96 } | 98 } |
97 | 99 |
98 function hasPermission() { | 100 function hasPermission() { |
99 navigator.serviceWorker.ready.then(function(swRegistration) { | 101 navigator.serviceWorker.ready.then(function(swRegistration) { |
100 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. | 102 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. |
101 var pushManager = swRegistration.pushManager || navigator.push; | 103 var pushManager = swRegistration.pushManager || navigator.push; |
102 pushManager.hasPermission().then(function(permission) { | 104 pushManager.hasPermission().then(function(permission) { |
103 sendResultToTest('permission status - ' + permission); | 105 sendResultToTest('permission status - ' + permission); |
104 }, sendErrorToTest); | 106 }, sendErrorToTest); |
105 }, sendErrorToTest); | 107 }, sendErrorToTest); |
106 } | 108 } |
107 | 109 |
108 function isControlled() { | 110 function isControlled() { |
109 if (navigator.serviceWorker.controller) { | 111 if (navigator.serviceWorker.controller) { |
110 sendResultToTest('true - is controlled'); | 112 sendResultToTest('true - is controlled'); |
111 } else { | 113 } else { |
112 sendResultToTest('false - is not controlled'); | 114 sendResultToTest('false - is not controlled'); |
113 } | 115 } |
114 } | 116 } |
115 | 117 |
| 118 function unregister() { |
| 119 if (!pushRegistration) { |
| 120 sendErrorToTest('no registration'); |
| 121 return; |
| 122 } |
| 123 |
| 124 pushRegistration.unregister().then(function(result) { |
| 125 sendResultToTest('unregister result: ' + result); |
| 126 }); |
| 127 } |
| 128 |
116 addEventListener('message', function(event) { | 129 addEventListener('message', function(event) { |
117 var message = JSON.parse(event.data); | 130 var message = JSON.parse(event.data); |
118 if (message.type == 'push') { | 131 if (message.type == 'push') { |
119 pushData.set(message.data); | 132 pushData.set(message.data); |
120 } | 133 } |
121 }, false); | 134 }, false); |
122 </script> | 135 </script> |
123 </head> | 136 </head> |
124 <body> | 137 <body> |
125 <h1>Push API Test</h1> | 138 <h1>Push API Test</h1> |
126 </body> | 139 </body> |
127 </html> | 140 </html> |
OLD | NEW |