| OLD | NEW |
| 1 <!doctype html> | 1 <!doctype html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <title>Notifications: Property reflection in the "notificationclick" event a
nd SWR.getNotifications().</title> | 4 <title>Notifications: Property reflection in the "notificationclick" event a
nd SWR.getNotifications().</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 notification object in a) the "notificationclick" event
in the | 12 // Tests that the notification object in a) the "notificationclick" event
in the |
| 13 // Service Worker, and b) ServiceWorkerRegistration.getNotifications(), bo
th | 13 // Service Worker, and b) ServiceWorkerRegistration.getNotifications(), bo
th |
| 14 // accurately reflect the attributes with which the notification was creat
ed. | 14 // accurately reflect the attributes with which the notification was creat
ed. |
| 15 | 15 |
| 16 // Checks that all the properties in expected also exist and are equal in
actual. | 16 // Checks that all the properties in expected also exist and are equal in
actual. |
| 17 function assert_object_is_superset(actual, expected, description) { | 17 function assert_object_is_superset(actual, expected, description) { |
| 18 Object.keys(expected).forEach(function(key) { | 18 Object.keys(expected).forEach(function(key) { |
| 19 var fieldDescription = description + ' [field ' + key + ']'; | 19 var fieldDescription = description + ' [field ' + key + ']'; |
| 20 if (typeof expected[key] == 'object') | 20 if (typeof expected[key] == 'object') |
| 21 assert_object_equals(actual[key], expected[key], fieldDescript
ion); | 21 assert_object_equals(actual[key], expected[key], fieldDescript
ion); |
| 22 else | 22 else |
| 23 assert_equals(actual[key], expected[key], fieldDescription); | 23 assert_equals(actual[key], expected[key], fieldDescription); |
| 24 }); | 24 }); |
| 25 } | 25 } |
| 26 | 26 |
| 27 async_test(function(test) { | 27 async_test(function(test) { |
| 28 var scope = 'resources/scope/' + location.pathname, | 28 var scope = 'resources/scope/' + location.pathname; |
| 29 script = 'resources/instrumentation-service-worker.js'; | 29 var script = 'instrumentation-service-worker.js'; |
| 30 var port; |
| 31 var registration; |
| 30 | 32 |
| 31 var options = { | 33 var options = { |
| 32 title: scope, | 34 title: scope, |
| 33 dir: 'rtl', | 35 dir: 'rtl', |
| 34 lang: 'nl-NL', | 36 lang: 'nl-NL', |
| 35 body: 'Hello, world!', | 37 body: 'Hello, world!', |
| 36 tag: 'tag', | 38 tag: 'tag', |
| 37 // FIXME: Relative URLs for the icon attribute currently get refle
cted as | 39 // FIXME: Relative URLs for the icon attribute currently get refle
cted as |
| 38 // an absolute URL, which should probably be the given relative UR
L. | 40 // an absolute URL, which should probably be the given relative UR
L. |
| 39 icon: 'https://example/icon.png', | 41 icon: 'https://example/icon.png', |
| (...skipping 18 matching lines...) Expand all Loading... |
| 58 action: 'a' + i, | 60 action: 'a' + i, |
| 59 title: 'Action ' + i, | 61 title: 'Action ' + i, |
| 60 icon: 'https://example/action_icon_' + i + '.png', | 62 icon: 'https://example/action_icon_' + i + '.png', |
| 61 placeholder: i % 2 == 0 ? null : 'Type a reply...' | 63 placeholder: i % 2 == 0 ? null : 'Type a reply...' |
| 62 }); | 64 }); |
| 63 } | 65 } |
| 64 | 66 |
| 65 if (window.testRunner) { | 67 if (window.testRunner) { |
| 66 testRunner.setPermission('notifications', 'granted', location.orig
in, location.origin); | 68 testRunner.setPermission('notifications', 'granted', location.orig
in, location.origin); |
| 67 } | 69 } |
| 68 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi
on(workerInfo) { | 70 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi
on(info) { |
| 71 port = info.port; |
| 72 registration = info.registration; |
| 69 // (1) Tell the Service Worker to display a Web Notification. | 73 // (1) Tell the Service Worker to display a Web Notification. |
| 70 workerInfo.port.postMessage({ | 74 var showPromise = sendCommand(port, { |
| 71 command: 'show', | 75 command: 'show', |
| 72 | 76 |
| 73 title: scope, | 77 title: scope, |
| 74 options: options | 78 options: options |
| 75 }); | 79 }); |
| 76 | 80 |
| 77 // Now limit actions to the number that we expect to be reflected
on notifications. | 81 // Now limit actions to the number that we expect to be reflected
on notifications. |
| 78 options.actions = options.actions.slice(0, Notification.maxActions
); | 82 options.actions = options.actions.slice(0, Notification.maxActions
); |
| 83 return showPromise; |
| 84 }).then(function(data) { |
| 85 // (2) Confirm that the service worker displayed the notification
successfully. |
| 86 assert_true(data.success, 'The notification must have been display
ed.'); |
| 87 return simulateNotificationClick(scope, -1 /* action_index */, por
t); |
| 88 }).then(function(data) { |
| 89 // (3) Confirm that all properties set on the cloned Notification
object are as expected. |
| 90 assert_object_is_superset(data.notification, options, 'The Notific
ation object properties must be the same in notificationclick events.'); |
| 91 return registration.getNotifications(); |
| 92 }).then(function(notifications) { |
| 93 // (4) Check that the properties are also set correctly on the non
-cloned Notification |
| 94 // object from getNotifications. |
| 95 assert_equals(notifications.length, 1); |
| 96 assert_object_is_superset(notifications[0], options, 'The Notifica
tion object properties must be the same in getNotifications.'); |
| 79 | 97 |
| 80 workerInfo.port.addEventListener('message', function(event) { | 98 notifications[0].actions.foo = 'bar'; |
| 81 if (typeof event.data != 'object' || !event.data.command) { | 99 notifications[0].actions.push({ title: 'Foo' }); |
| 82 assert_unreached('Invalid message from the Service Worker.
'); | 100 if (notifications[0].actions.length) { |
| 83 return; | 101 notifications[0].actions[0].title = 'Changed'; |
| 84 } | 102 notifications[0].actions[0].foo = 'bar'; |
| 103 } |
| 104 assert_object_equals(notifications[0].actions, options.actions, 'T
he actions field should be immutable.'); |
| 85 | 105 |
| 86 // (2) Listen for confirmation from the Service Worker that th
e | 106 // TODO(johnme): This should pass before shipping Notification.act
ions; this is blocked on https://crbug.com/515920. |
| 87 // notification's display promise has been resolved. | 107 //assert_equals(notifications[0].actions, notifications[0].actions
, 'The actions field should === itself.'); |
| 88 if (event.data.command == 'show') { | |
| 89 assert_true(event.data.success, 'The notification must hav
e been displayed.'); | |
| 90 if (window.testRunner) { | |
| 91 testRunner.simulateWebNotificationClick(scope, -1 /* a
ction_index */); | |
| 92 } | |
| 93 return; | |
| 94 } | |
| 95 | 108 |
| 96 // (3) Listen for confirmation from the Service Worker that th
e | 109 test.done(); |
| 97 // notification has been clicked on. Make sure that all proper
ties | |
| 98 // set on the cloned Notification object are as expected. | |
| 99 assert_equals(event.data.command, 'click', 'The notification w
as expected to be clicked.'); | |
| 100 assert_object_is_superset(event.data.notification, options, 'T
he Notification object properties must be the same in notificationclick events.'
); | |
| 101 | |
| 102 // (4) Check that the properties are also set correctly on the | |
| 103 // non-cloned Notification object from getNotifications. | |
| 104 workerInfo.registration.getNotifications().then(function(notif
ications) { | |
| 105 assert_equals(notifications.length, 1); | |
| 106 assert_object_is_superset(notifications[0], options, 'The
Notification object properties must be the same in getNotifications.'); | |
| 107 | |
| 108 notifications[0].actions.foo = 'bar'; | |
| 109 notifications[0].actions.push({ title: 'Foo' }); | |
| 110 if (notifications[0].actions.length) { | |
| 111 notifications[0].actions[0].title = 'Changed'; | |
| 112 notifications[0].actions[0].foo = 'bar'; | |
| 113 } | |
| 114 assert_object_equals(notifications[0].actions, options.act
ions, 'The actions field should be immutable.'); | |
| 115 | |
| 116 // TODO(johnme): This should pass before shipping Notifica
tion.actions; this is blocked on https://crbug.com/515920. | |
| 117 //assert_equals(notifications[0].actions, notifications[0]
.actions, 'The actions field should === itself.'); | |
| 118 | |
| 119 test.done(); | |
| 120 }); | |
| 121 }); | |
| 122 }).catch(unreached_rejection(test)); | 110 }).catch(unreached_rejection(test)); |
| 123 | 111 |
| 124 }, 'Clicking on a notification displayed by a Service Worker the notificat
ionclick event.'); | 112 }, 'Clicking on a notification displayed by a Service Worker the notificat
ionclick event.'); |
| 125 </script> | 113 </script> |
| 126 </body> | 114 </body> |
| 127 </html> | 115 </html> |
| OLD | NEW |