blob: ae79ff5ee1ba8ad3ae17af29a1b97ae74e8225fb [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Android Push API instrumentation test page</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<!-- This page is used by the PushMessagingTest instrumentation test. -->
<script src="../notifications/notification_test_utils.js"></script>
<script>
// PushMessagingTest observes changes to the tab title as an asynchronous
// response mechanism from JavaScript to Java.
// TODO(mvanouwerkerk): Use DomAutomationController - crbug.com/562487
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 => {
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>
</html>