Index: chrome/test/data/notifications/platform_notification_service.html |
diff --git a/chrome/test/data/notifications/platform_notification_service.html b/chrome/test/data/notifications/platform_notification_service.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3fc27dc0c659d24c5ebcb40b343728e16cc8634e |
--- /dev/null |
+++ b/chrome/test/data/notifications/platform_notification_service.html |
@@ -0,0 +1,107 @@ |
+<!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 |
+ PlatformNotificationServiceBrowserTest. --> |
+ <script> |
+ var messagePort = null, |
+ messageStack = [], |
+ expectingMessage = false; |
+ |
+ // Requests permission to display Web Notifications. Will return the |
+ // permission level to the DOM Automation Controller. |
+ function RequestPermission() { |
+ Notification.requestPermission(function (level) { |
+ domAutomationController.send(level); |
+ }); |
+ } |
+ |
+ // Returns a promise that will be resolved with an activated Service |
+ // Worker, or rejects when the Service Worker could not be started. There |
+ // will be a message port to and from the worker in |messagePort|. |
+ // TODO(peter): Generalize this in some sort of Service Worker utility |
+ // JavaScript file so that other tests can re-use the same logic. |
+ function GetActivatedServiceWorker(script, scope) { |
+ return navigator.serviceWorker.getRegistration(scope) |
+ .then(function (registration) { |
+ // Unregister any existing Service Worker. |
+ if (registration) |
+ return registration.unregister(); |
+ }).then(function () { |
+ // Register the Service Worker again. |
+ return navigator.serviceWorker.register(script, { scope: scope }); |
+ }).then(function (registration) { |
+ if (registration.active) { |
+ return registration; |
+ } else if (registration.waiting || registration.installing) { |
+ var worker = registration.waiting || registration.installing; |
+ return new Promise(function (resolve) { |
+ worker.addEventListener('statechange', function () { |
+ if (worker.state === 'activated') |
+ resolve(registration); |
+ }); |
+ }); |
+ } else { |
+ return Promise.reject('Service Worker in invalid state.'); |
+ } |
+ }).then(function (registration) { |
+ return new Promise(function (resolve) { |
+ var channel = new MessageChannel(); |
+ channel.port1.addEventListener('message', function (event) { |
+ if (event.data == 'ready') |
+ resolve(registration); |
+ }); |
+ |
+ registration.active.postMessage(channel.port2, |
+ [ channel.port2 ]); |
+ |
+ messagePort = channel.port1; |
+ messagePort.start(); |
+ }); |
+ }); |
+ } |
+ |
+ // Renews the registered Service Worker registration for this page, then |
+ // displays a notification on the activated ServiceWorkerRegistration. |
+ function DisplayPersistentNotification(title) { |
+ GetActivatedServiceWorker('platform_notification_service.js', |
+ location.pathname) |
+ .then(function (registration) { |
+ return registration.showNotification(title, { |
+ body: 'Hello, world!', |
+ icon: 'icon.png' |
+ }); |
+ }).then(function () { |
+ messagePort.addEventListener('message', function (event) { |
+ if (expectingMessage) |
+ domAutomationController.send(event.data); |
+ else |
+ messageStack.push(event.data); |
+ }); |
+ |
+ domAutomationController.send('ok'); |
+ }).catch(function (error) { |
+ domAutomationController.send('' + error); |
+ }); |
+ } |
+ |
+ // Returns the latest received message from the worker. If no message has |
+ // been received, nothing will be done. For successfully registered |
+ // Service Workers this is OK, however, since the "message" event handler |
+ // in DisplayPersistentNotification will take care of notifying the DOM |
+ // Automation Controller instead. |
+ function GetMessageFromWorker() { |
+ if (!messageStack.length) { |
+ expectingMessage = true; |
+ return; |
+ } |
+ |
+ domAutomationController.send('' + messageStack.pop()); |
+ } |
+ </script> |
+ </body> |
+</html> |