| 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: Action reflection in the "notificationclick" event.</t
itle> |
| 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 action property of the "notificationclick" event in the |
| 13 // resolves a promise, and that the notificationclick event gets fired whe
n | 13 // Service Worker accurately reflects which action was activated, if any. |
| 14 // we simulate a click on it. This test requires the test runner. | |
| 15 | 14 |
| 16 async_test(function(test) { | 15 async_test(function(test) { |
| 17 var scope = 'resources/scope/serviceworkerregistration-service-worker-
click', | 16 var scope = 'resources/scope/' + location.pathname, |
| 18 script = 'resources/instrumentation-service-worker.js'; | 17 script = 'resources/instrumentation-service-worker.js'; |
| 19 | 18 |
| 19 var options = { |
| 20 actions: [] |
| 21 }; |
| 22 |
| 23 var expectedActions = []; |
| 24 for (var i = 0; i < Notification.maxActions; ++i) { |
| 25 var action = { action: "action" + i, title: "Action " + i }; |
| 26 options.actions.push(action); |
| 27 expectedActions.push(action.action); |
| 28 } |
| 29 // Expect empty string when main body of notification is activated. |
| 30 expectedActions.push(""); |
| 31 |
| 20 testRunner.setPermission('notifications', 'granted', location.origin,
location.origin); | 32 testRunner.setPermission('notifications', 'granted', location.origin,
location.origin); |
| 21 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi
on(workerInfo) { | 33 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi
on(workerInfo) { |
| 22 // (1) Tell the Service Worker to display a Web Notification. | 34 // (1) Tell the Service Worker to display a Web Notification. |
| 23 workerInfo.port.postMessage({ | 35 workerInfo.port.postMessage({ |
| 24 command: 'show', | 36 command: 'show', |
| 25 | 37 |
| 26 title: scope, | 38 title: scope, |
| 27 options: { body: 'Hello, world!' } | 39 options: options |
| 28 }); | 40 }); |
| 29 | 41 |
| 30 workerInfo.port.addEventListener('message', function(event) { | 42 workerInfo.port.addEventListener('message', function(event) { |
| 31 if (typeof event.data != 'object' || !event.data.command) { | 43 if (typeof event.data != 'object' || !event.data.command) { |
| 32 assert_unreached('Invalid message from the Service Worker.
'); | 44 assert_unreached('Invalid message from the Service Worker.
'); |
| 33 return; | 45 return; |
| 34 } | 46 } |
| 35 | 47 |
| 36 // (2) Listen for confirmation from the Service Worker that th
e | 48 // (2) Listen for confirmation from the Service Worker that th
e |
| 37 // notification's display promise has been resolved. | 49 // notification's display promise has been resolved. |
| 38 if (event.data.command == 'show') { | 50 if (event.data.command == 'show') { |
| 39 assert_true(event.data.success, 'The notification must hav
e been displayed.'); | 51 assert_true(event.data.success, 'The notification must hav
e been displayed.'); |
| 52 for (var i = 0; i < options.actions.length; ++i) |
| 53 testRunner.simulateWebNotificationClick(scope, i); |
| 40 testRunner.simulateWebNotificationClick(scope); | 54 testRunner.simulateWebNotificationClick(scope); |
| 41 return; | 55 return; |
| 42 } | 56 } |
| 43 | 57 |
| 44 // (3) Listen for confirmation from the Service Worker that th
e | 58 // (3) Listen for confirmation from the Service Worker that th
e |
| 45 // notification has been clicked on. | 59 // notification has been clicked on. Make sure that the action |
| 46 if (event.data.command == 'click') { | 60 // property set on the NotificationEvent object is as expected
. |
| 47 assert_equals(event.data.notification.title, scope, 'The r
ight notification must have been clicked.'); | 61 assert_equals(event.data.command, 'click', 'The notification w
as expected to be clicked.'); |
| 48 | 62 |
| 63 assert_equals(event.data.action, expectedActions.shift()); |
| 64 |
| 65 if (expectedActions.length === 0) |
| 49 test.done(); | 66 test.done(); |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 assert_unreached('Unexpected message from the Service Worker:
' + event.data.command); | |
| 54 }); | 67 }); |
| 55 }).catch(unreached_rejection(test)); | 68 }).catch(unreached_rejection(test)); |
| 56 | 69 |
| 57 }, 'Clicking on a notification displayed by a Service Worker the notificat
ionclick event.'); | 70 }, 'NotificationEvent action property should be reflect which action was c
licked.'); |
| 58 </script> | 71 </script> |
| 59 </body> | 72 </body> |
| 60 </html> | 73 </html> |
| OLD | NEW |