Index: chrome/test/data/push_messaging/push_messaging_test_android.html |
diff --git a/chrome/test/data/push_messaging/push_messaging_test_android.html b/chrome/test/data/push_messaging/push_messaging_test_android.html |
index 158c27191a411966b72c2d8f8bb62d29a6eaf75c..ae79ff5ee1ba8ad3ae17af29a1b97ae74e8225fb 100644 |
--- a/chrome/test/data/push_messaging/push_messaging_test_android.html |
+++ b/chrome/test/data/push_messaging/push_messaging_test_android.html |
@@ -12,17 +12,56 @@ |
// PushMessagingTest observes changes to the tab title as an asynchronous |
// response mechanism from JavaScript to Java. |
// TODO(mvanouwerkerk): Use DomAutomationController - crbug.com/562487 |
- function respondToTest(result) { |
- document.title = result; |
+ var errorCounter = 0; |
+ function sendToTest(message) { |
+ // Duplicate messages cannot be detected by the test, don't send them. |
+ if (message == document.title) { |
+ console.log('Duplicate message: ' + message); |
+ message = |
+ 'Error ' + errorCounter + ' - duplicate message: ' + message; |
+ errorCounter++; |
+ } |
+ document.title = message; |
} |
+ // Installs a service worker, subscribes to push, and sets up a messaging |
+ // mechanism between this page and the service worker. Called by the test. |
function subscribePush() { |
GetActivatedServiceWorker( |
'push_messaging_test_worker_android.js', location.pathname) |
- .then(registration => |
- registration.pushManager.subscribe({ userVisibleOnly: true })) |
- .then(subscription => respondToTest('subscribe ok')) |
- .catch(error => respondToTest('subscribe fail: ' + error)); |
+ .then(registration => |
+ registration.pushManager.subscribe({ userVisibleOnly: true })) |
+ .then(subscription => { |
+ setupMessageHandler(); |
+ sendToTest('subscribe ok'); |
+ }) |
+ .catch(error => sendToTest('subscribe fail: ' + error)); |
+ } |
+ |
+ // Sets whether the service worker should show a notification when it |
+ // receives a push message or not. Called by the test. |
+ function setNotifyOnPush(notify) { |
+ sendToServiceWorker('setNotifyOnPush', notify); |
+ } |
+ |
+ // Sends a message of type |type| with a data payload of |data| to the |
+ // service worker. |
+ function sendToServiceWorker(type, data) { |
+ navigator.serviceWorker.ready |
+ .then(registration => { |
+ registration.active.postMessage( |
+ JSON.stringify({'type': type, 'data': data})); |
+ }); |
+ } |
+ |
+ // The data for messages of type sendToTest will be passed to the test. |
+ function setupMessageHandler() { |
+ messagePort.addEventListener('message', event => { |
+ var message = JSON.parse(event.data); |
+ if (message.type == 'sendToTest') { |
+ sendToTest(message.data); |
+ } |
+ }); |
} |
</script> |
</body> |