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>Platform Notification Service BrowserTest service page</title> | 5 <title>Platform Notification Service BrowserTest service page</title> |
6 </head> | 6 </head> |
7 <body> | 7 <body> |
8 <!-- This page is intended to be used by the | 8 <!-- This page is intended to be used by the cross-platform |
9 PlatformNotificationServiceBrowserTest. --> | 9 PlatformNotificationServiceBrowserTest. --> |
| 10 <script src="notification_test_utils.js"></script> |
10 <script> | 11 <script> |
11 var messagePort = null, | 12 var messagePort = null, |
12 messageStack = [], | 13 messageStack = [], |
13 expectingMessage = false; | 14 expectingMessage = false; |
14 | 15 |
15 // Requests permission to display Web Notifications. Will return the | 16 // Requests permission to display Web Notifications. Will return the |
16 // permission level to the DOM Automation Controller. | 17 // permission level to the DOM Automation Controller. |
17 function RequestPermission() { | 18 function RequestPermission() { |
18 Notification.requestPermission(function (level) { | 19 Notification.requestPermission(function (level) { |
19 domAutomationController.send(level); | 20 domAutomationController.send(level); |
20 }); | 21 }); |
21 } | 22 } |
22 | 23 |
23 // Returns a promise that will be resolved with an activated Service | |
24 // Worker, or rejects when the Service Worker could not be started. There | |
25 // will be a message port to and from the worker in |messagePort|. | |
26 // TODO(peter): Generalize this in some sort of Service Worker utility | |
27 // JavaScript file so that other tests can re-use the same logic. | |
28 function GetActivatedServiceWorker(script, scope) { | |
29 return navigator.serviceWorker.getRegistration(scope) | |
30 .then(function (registration) { | |
31 // Unregister any existing Service Worker. | |
32 if (registration) | |
33 return registration.unregister(); | |
34 }).then(function () { | |
35 // Register the Service Worker again. | |
36 return navigator.serviceWorker.register(script, { scope: scope }); | |
37 }).then(function (registration) { | |
38 if (registration.active) { | |
39 return registration; | |
40 } else if (registration.waiting || registration.installing) { | |
41 var worker = registration.waiting || registration.installing; | |
42 return new Promise(function (resolve) { | |
43 worker.addEventListener('statechange', function () { | |
44 if (worker.state === 'activated') | |
45 resolve(registration); | |
46 }); | |
47 }); | |
48 } else { | |
49 return Promise.reject('Service Worker in invalid state.'); | |
50 } | |
51 }).then(function (registration) { | |
52 return new Promise(function (resolve) { | |
53 var channel = new MessageChannel(); | |
54 channel.port1.addEventListener('message', function (event) { | |
55 if (event.data == 'ready') | |
56 resolve(registration); | |
57 }); | |
58 | |
59 registration.active.postMessage(channel.port2, | |
60 [ channel.port2 ]); | |
61 | |
62 messagePort = channel.port1; | |
63 messagePort.start(); | |
64 }); | |
65 }); | |
66 } | |
67 | |
68 // Renews the registered Service Worker registration for this page, then | 24 // Renews the registered Service Worker registration for this page, then |
69 // displays a notification on the activated ServiceWorkerRegistration. | 25 // displays a notification on the activated ServiceWorkerRegistration. |
70 function DisplayPersistentNotification(title) { | 26 function DisplayPersistentNotification(title) { |
71 GetActivatedServiceWorker('platform_notification_service.js', | 27 GetActivatedServiceWorker('platform_notification_service.js', |
72 location.pathname) | 28 location.pathname) |
73 .then(function (registration) { | 29 .then(function (registration) { |
74 return registration.showNotification(title, { | 30 return registration.showNotification(title, { |
75 body: 'Hello, world!', | 31 body: 'Hello, world!', |
76 icon: 'icon.png' | 32 icon: 'icon.png' |
77 }); | 33 }); |
(...skipping 20 matching lines...) Expand all Loading... |
98 if (!messageStack.length) { | 54 if (!messageStack.length) { |
99 expectingMessage = true; | 55 expectingMessage = true; |
100 return; | 56 return; |
101 } | 57 } |
102 | 58 |
103 domAutomationController.send('' + messageStack.pop()); | 59 domAutomationController.send('' + messageStack.pop()); |
104 } | 60 } |
105 </script> | 61 </script> |
106 </body> | 62 </body> |
107 </html> | 63 </html> |
OLD | NEW |