| OLD | NEW |
| 1 <!doctype html> | 1 <!doctype html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <title>Notifications: ServiceWorkerRegistration.showNotification().</title> | 4 <title>Notifications: ServiceWorkerRegistration.showNotification().</title> |
| 5 <script src="../resources/testharness.js"></script> | 5 <script src="../resources/testharness.js"></script> |
| 6 <script src="../resources/testharnessreport.js"></script> | 6 <script src="../resources/testharnessreport.js"></script> |
| 7 <script src="../serviceworker/resources/test-helpers.js"></script> | 7 <script src="../serviceworker/resources/test-helpers.js"></script> |
| 8 <script src="resources/test-helpers.js"></script> | 8 <script src="resources/test-helpers.js"></script> |
| 9 </head> | 9 </head> |
| 10 <body> | 10 <body> |
| 11 <script> | 11 <script> |
| 12 // Tests that the showNotification() function when used in a Service Worke
r | 12 // Tests that the showNotification() function when used in a Service Worke
r |
| 13 // resolves a promise, and that the notificationclick event gets fired whe
n | 13 // resolves a promise, and that the notificationclick event gets fired whe
n |
| 14 // we simulate a click on it. This test requires the test runner. | 14 // we simulate a click on it. This test requires the test runner. |
| 15 | 15 |
| 16 async_test(function(test) { | 16 async_test(function(test) { |
| 17 var scope = 'resources/scope/serviceworkerregistration-service-worker-
click', | 17 var scope = 'resources/scope/serviceworkerregistration-service-worker-
click'; |
| 18 script = 'resources/instrumentation-service-worker.js'; | 18 var script = 'instrumentation-service-worker.js'; |
| 19 var port; |
| 19 | 20 |
| 20 testRunner.setPermission('notifications', 'granted', location.origin,
location.origin); | 21 testRunner.setPermission('notifications', 'granted', location.origin,
location.origin); |
| 21 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi
on(workerInfo) { | 22 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi
on(info) { |
| 23 port = info.port; |
| 22 // (1) Tell the Service Worker to display a Web Notification. | 24 // (1) Tell the Service Worker to display a Web Notification. |
| 23 workerInfo.port.postMessage({ | 25 return sendCommand(port, { |
| 24 command: 'show', | 26 command: 'show', |
| 25 | 27 |
| 26 title: scope, | 28 title: scope, |
| 27 options: { body: 'Hello, world!' } | 29 options: { body: 'Hello, world!' } |
| 28 }); | 30 }); |
| 31 }).then(function(data) { |
| 32 // (2) Confirm that the service worker displayed the notification
successfully. |
| 33 assert_true(data.success, 'The notification must have been display
ed.'); |
| 34 return simulateNotificationClick(scope, -1 /* action_index */, por
t); |
| 35 }).then(function(data) { |
| 36 // (3) Confirm that the service worker handled the click on the co
rrect notification. |
| 37 assert_equals(data.notification.title, scope, 'The right notificat
ion must have been clicked.'); |
| 29 | 38 |
| 30 workerInfo.port.addEventListener('message', function(event) { | 39 test.done(); |
| 31 if (typeof event.data != 'object' || !event.data.command) { | |
| 32 assert_unreached('Invalid message from the Service Worker.
'); | |
| 33 return; | |
| 34 } | |
| 35 | |
| 36 // (2) Listen for confirmation from the Service Worker that th
e | |
| 37 // notification's display promise has been resolved. | |
| 38 if (event.data.command == 'show') { | |
| 39 assert_true(event.data.success, 'The notification must hav
e been displayed.'); | |
| 40 testRunner.simulateWebNotificationClick(scope, -1 /* actio
n_index */); | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 // (3) Listen for confirmation from the Service Worker that th
e | |
| 45 // notification has been clicked on. | |
| 46 if (event.data.command == 'click') { | |
| 47 assert_equals(event.data.notification.title, scope, 'The r
ight notification must have been clicked.'); | |
| 48 | |
| 49 test.done(); | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 assert_unreached('Unexpected message from the Service Worker:
' + event.data.command); | |
| 54 }); | |
| 55 }).catch(unreached_rejection(test)); | 40 }).catch(unreached_rejection(test)); |
| 56 | 41 |
| 57 }, 'Clicking on a notification displayed by a Service Worker the notificat
ionclick event.'); | 42 }, 'Clicking on a notification displayed by a Service Worker the notificat
ionclick event.'); |
| 58 </script> | 43 </script> |
| 59 </body> | 44 </body> |
| 60 </html> | 45 </html> |
| OLD | NEW |