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 var errorCounter = 0; |
16 document.title = result; | 16 function sendToTest(message) { |
| 17 // Duplicate messages cannot be detected by the test, don't send them. |
| 18 if (message == document.title) { |
| 19 console.log('Duplicate message: ' + message); |
| 20 message = |
| 21 'Error ' + errorCounter + ' - duplicate message: ' + message; |
| 22 errorCounter++; |
| 23 } |
| 24 document.title = message; |
17 } | 25 } |
18 | 26 |
| 27 // Installs a service worker, subscribes to push, and sets up a messaging |
| 28 // mechanism between this page and the service worker. Called by the test. |
19 function subscribePush() { | 29 function subscribePush() { |
20 GetActivatedServiceWorker( | 30 GetActivatedServiceWorker( |
21 'push_messaging_test_worker_android.js', location.pathname) | 31 'push_messaging_test_worker_android.js', location.pathname) |
22 .then(registration => | 32 .then(registration => |
23 registration.pushManager.subscribe({ userVisibleOnly: true })) | 33 registration.pushManager.subscribe({ userVisibleOnly: true })) |
24 .then(subscription => respondToTest('subscribe ok')) | 34 .then(subscription => { |
25 .catch(error => respondToTest('subscribe fail: ' + error)); | 35 setupMessageHandler(); |
| 36 sendToTest('subscribe ok'); |
| 37 }) |
| 38 .catch(error => sendToTest('subscribe fail: ' + error)); |
| 39 } |
| 40 |
| 41 // Sets whether the service worker should show a notification when it |
| 42 // receives a push message or not. Called by the test. |
| 43 function setNotifyOnPush(notify) { |
| 44 sendToServiceWorker('setNotifyOnPush', notify); |
| 45 } |
| 46 |
| 47 // Sends a message of type |type| with a data payload of |data| to the |
| 48 // service worker. |
| 49 function sendToServiceWorker(type, data) { |
| 50 navigator.serviceWorker.ready |
| 51 .then(registration => { |
| 52 registration.active.postMessage( |
| 53 JSON.stringify({'type': type, 'data': data})); |
| 54 }); |
| 55 } |
| 56 |
| 57 // The data for messages of type sendToTest will be passed to the test. |
| 58 function setupMessageHandler() { |
| 59 messagePort.addEventListener('message', event => { |
| 60 var message = JSON.parse(event.data); |
| 61 if (message.type == 'sendToTest') { |
| 62 sendToTest(message.data); |
| 63 } |
| 64 }); |
26 } | 65 } |
27 </script> | 66 </script> |
28 </body> | 67 </body> |
29 </html> | 68 </html> |
OLD | NEW |