OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html lang="en"> | 2 <html lang="en"> |
3 <head> | 3 <head> |
4 <meta charset="UTF-8"> | 4 <meta charset="UTF-8"> |
5 <title>Android Push API instrumentation test page</title> | 5 <title>Android Push API instrumentation test page</title> |
6 <link rel="manifest" href="manifest.json"> | 6 <link rel="manifest" href="manifest.json"> |
7 </head> | 7 </head> |
8 <body> | 8 <body> |
9 <!-- This page is used by the PushMessagingTest instrumentation test. --> | 9 <!-- This page is used by the PushMessagingTest instrumentation test. --> |
10 <script src="../notifications/notification_test_utils.js"></script> | 10 <script src="../notifications/notification_test_utils.js"></script> |
11 <script> | 11 <script> |
12 // PushMessagingTest observes changes to the tab title as an asynchronous | 12 // PushMessagingTest observes changes to the tab title as an asynchronous |
13 // response mechanism from JavaScript to Java. | 13 // response mechanism from JavaScript to Java. |
14 // TODO(mvanouwerkerk): Use DomAutomationController - crbug.com/562487 | 14 // TODO(mvanouwerkerk): Use DomAutomationController - crbug.com/562487 |
15 function respondToTest(result) { | 15 function sendToTest(message) { |
16 document.title = result; | 16 document.title = message; |
johnme
2015/12/08 17:12:31
Please add some error handling for the case where
Michael van Ouwerkerk
2015/12/09 15:02:52
Done.
| |
17 } | 17 } |
18 | 18 |
19 function subscribePush() { | 19 function subscribePush() { |
20 GetActivatedServiceWorker( | 20 GetActivatedServiceWorker( |
21 'push_messaging_test_worker_android.js', location.pathname) | 21 'push_messaging_test_worker_android.js', location.pathname) |
22 .then(registration => | 22 .then(registration => |
23 registration.pushManager.subscribe({ userVisibleOnly: true })) | 23 registration.pushManager.subscribe({ userVisibleOnly: true })) |
24 .then(subscription => respondToTest('subscribe ok')) | 24 .then(subscription => { |
25 .catch(error => respondToTest('subscribe fail: ' + error)); | 25 setupMessageHandler(); |
26 sendToTest('subscribe ok'); | |
27 }) | |
johnme
2015/12/08 17:12:31
Nit: this indentation feels a little odd. How abou
Michael van Ouwerkerk
2015/12/09 15:02:52
I've done something like that.
| |
28 .catch(error => sendToTest('subscribe fail: ' + error)); | |
29 } | |
30 | |
31 // Sets whether the service worker should show a notification when it | |
32 // receives a push message or not. | |
33 function setNotifyOnPush(notify) { | |
johnme
2015/12/08 17:12:31
Can you add a "Called by test" comment, as otherwi
Michael van Ouwerkerk
2015/12/09 15:02:52
Done.
| |
34 sendToServiceWorker('setNotifyOnPush', notify); | |
35 } | |
36 | |
37 // Sends a message of type |type| with a data payload of |data| to the | |
38 // service worker. | |
39 function sendToServiceWorker(type, data) { | |
40 navigator.serviceWorker.ready | |
41 .then(registration => { | |
42 registration.active.postMessage( | |
43 JSON.stringify({'type': type, 'data': data})); | |
44 }); | |
45 } | |
46 | |
47 // The data for messages of type sendToTest will be passed to the test. | |
48 function setupMessageHandler() { | |
49 messagePort.addEventListener('message', event => { | |
50 var message = JSON.parse(event.data); | |
51 if (message.type == 'sendToTest') | |
52 sendToTest(message.data); | |
53 }); | |
26 } | 54 } |
27 </script> | 55 </script> |
28 </body> | 56 </body> |
29 </html> | 57 </html> |
OLD | NEW |