<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Platform Notification Service BrowserTest service page</title>
</head>
<body>
<!-- This page is intended to be used by the cross-platform
PlatformNotificationServiceBrowserTest. -->
<script src="notification_test_utils.js"></script>
<script src="../result_queue.js"></script>
<script>
var messagePort = null,
messageQueue = new ResultQueue();
// Requests permission to display Web Notifications. Will return the
// permission level to the DOM Automation Controller.
function RequestPermission() {
return Notification.requestPermission();
}
// Instantiates a new non-persistent notification with the given |title|
// and |options| and forwards the notification shown or error event.
function DisplayNonPersistentNotification(title, options) {
const notification = new Notification(title, options || {});
return new Promise(resolve => {
notification.addEventListener('show', e =>
resolve('ok'));
notification.addEventListener('error', e =>
resolve('could not show notification'));
});
}
// Instantiates a new non-persistent notification with the given |title|
// and |options| and then closes it.
// Forwards 'ok' upon receiving the 'close' event.
function DisplayAndCloseNonPersistentNotification(title, options) {
const notification = new Notification(title, options || {});
notification.addEventListener('show', e => {
notification.close();
});
return new Promise(resolve => {
notification.addEventListener('close', e => {
resolve('ok');
});
});
}
// Renews the registered Service Worker registration for this page, then
// displays a notification on the activated ServiceWorkerRegistration.
function DisplayPersistentNotification(title, options) {
options = options || { body: 'Hello, world!',
icon: 'icon.png' };
return GetActivatedServiceWorker('platform_notification_service.js',
location.pathname)
.then(function (registration) {
return registration.showNotification(title, options);
}).then(function () {
messagePort.addEventListener('message', function (event) {
messageQueue.push(event.data);
});
return 'ok';
}).catch(function (error) {
return '' + error;
});
}
// Displays a persistent notification having every field in its options
// bag filled out with non-default values.
function DisplayPersistentAllOptionsNotification() {
return DisplayPersistentNotification('Title', {
dir: 'rtl',
lang: 'nl-NL',
body: 'Contents',
tag: 'replace-id',
image: 'icon.png',
icon: 'icon.png',
badge: 'icon.png',
timestamp: 621046800000,
renotify: true,
silent: true,
requireInteraction: true,
data: [
{ property: 'value' }
],
actions: [
{ action: 'actionId', title: 'actionTitle', icon: 'icon.png' }
]
});
}
// Displays a persistent notification with vibrate field.
function DisplayPersistentNotificationVibrate() {
return DisplayPersistentNotification('Title', {
body: 'Contents',
vibrate: [100, 200, 300]
});
}
// Displays a persistent notification with a data: URL as its image.
function DisplayPersistentNotificationDataUrlImage() {
return fetch('icon.png').then(function(response) {
return response.blob();
}).then(function(blob) {
var reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise(resolve => {
reader.onloadend = resolve;
}).then(function() {
return DisplayPersistentNotification('Data URL Title', {
body: 'Contents',
icon: reader.result
});
});
});
}
// Displays a persistent notification with a blob URL as its image.
function DisplayPersistentNotificationBlobImage() {
return fetch('icon.png').then(function(response) {
return response.blob();
}).then(function(blob) {
return DisplayPersistentNotification('Blob Title', {
body: 'Contents',
icon: URL.createObjectURL(blob)
});
});
}
// Displays a persistent notification with action buttons.
function DisplayPersistentNotificationWithActionButtons() {
return DisplayPersistentNotification('action_button_click', {
body: 'Contents',
actions: [
{ action: 'actionId1', title: 'actionTitle1', icon: 'icon.png' },
{ action: 'actionId2', title: 'actionTitle2', icon: 'icon.png' }
]
});
}
// Displays a persistent notification with a reply button.
function DisplayPersistentNotificationWithReplyButton() {
return DisplayPersistentNotification('action_button_click', {
body: 'Contents',
actions: [
{ action: 'actionId1', title: 'actionTitle1', icon: 'icon.png',
type: 'text' }
]
});
}
// Displays a persistent notification with scenario set to incoming-call.
function DisplayIncomingCallNotification() {
return DisplayPersistentNotification('Title', {
body: 'Contents',
image: 'icon.png',
scenario: 'incoming-call',
});
}
// Displays a persistent notification with scenario set to incoming-call
// with an action button.
function DisplayIncomingCallNotificationWithActionButton() {
return DisplayPersistentNotification('Title', {
body: 'Contents',
image: 'icon.png',
scenario: 'incoming-call',
actions: [
{ action: 'actionId1', title: 'actionTitle1', icon: 'icon.png' }
]
});
}
// Gets a comma separated list of currently displayed notification titles
function GetDisplayedNotifications() {
return GetActivatedServiceWorker('platform_notification_service.js',
location.pathname).then(function (sw) {
return sw.getNotifications();
}).then(function (notifications) {
var notificationResult = '';
for (var i = 0; i < notifications.length; ++i) {
notificationResult = notificationResult.concat(notifications[i].title);
if (i < (notifications.length -1))
notificationResult = notificationResult.concat(',');
}
messageQueue.push(notificationResult);
return 'ok';
}).catch(function (error) {
return '' + error;
});
}
// Returns the next received message from the worker. Messages are
// returned in the order they are received.
function GetMessageFromWorker() {
return messageQueue.pop();
}
</script>
</body>
</html>