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.< /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 available in the "notificationclick" event in the | 12 // Tests that the notification object in a) the "notificationclick" event in the |
13 // Service Worker accurately reflects the attributes with which the notifi cation | 13 // Service Worker, and b) ServiceWorkerRegistration.getNotifications(), bo th |
14 // was created (for this test --) in the document. | 14 // accurately reflect the attributes with which the notification was creat ed. |
15 | |
16 // Checks that all the properties in expected also exist and are equal in actual. | |
17 function assert_object_is_superset(actual, expected, description) { | |
18 description = description || ''; | |
Peter Beverloo
2015/10/21 14:26:35
nit: remove this please, it's unused
johnme
2015/10/21 15:55:35
Done.
| |
19 Object.keys(expected).forEach(function(key) { | |
20 if (typeof expected[key] == 'object') | |
21 assert_object_equals(actual[key], expected[key], description + ' [field ' + key + ']'); | |
Peter Beverloo
2015/10/21 14:26:34
nit: perhaps store the concatenated string in a lo
johnme
2015/10/21 15:55:35
Done.
| |
22 else | |
23 assert_equals(actual[key], expected[key], description + ' [fie ld ' + key + ']'); | |
24 }); | |
25 } | |
15 | 26 |
16 async_test(function(test) { | 27 async_test(function(test) { |
17 var scope = 'resources/scope/' + location.pathname, | 28 var scope = 'resources/scope/' + location.pathname, |
18 script = 'resources/instrumentation-service-worker.js'; | 29 script = 'resources/instrumentation-service-worker.js'; |
19 | 30 |
20 var options = { | 31 var options = { |
21 title: scope, | 32 title: scope, |
22 dir: 'rtl', | 33 dir: 'rtl', |
23 lang: 'nl-NL', | 34 lang: 'nl-NL', |
24 body: 'Hello, world!', | 35 body: 'Hello, world!', |
(...skipping 18 matching lines...) Expand all Loading... | |
43 testRunner.setPermission('notifications', 'granted', location.origin, location.origin); | 54 testRunner.setPermission('notifications', 'granted', location.origin, location.origin); |
44 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi on(workerInfo) { | 55 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi on(workerInfo) { |
45 // (1) Tell the Service Worker to display a Web Notification. | 56 // (1) Tell the Service Worker to display a Web Notification. |
46 workerInfo.port.postMessage({ | 57 workerInfo.port.postMessage({ |
47 command: 'show', | 58 command: 'show', |
48 | 59 |
49 title: scope, | 60 title: scope, |
50 options: options | 61 options: options |
51 }); | 62 }); |
52 | 63 |
64 // Now limit actions to the number that we expect to be reflected on notifications. | |
65 options.actions = options.actions.slice(0, Notification.maxActions ); | |
Peter Beverloo
2015/10/21 14:26:34
Redefining maxActions might be a fun thing to test
johnme
2015/10/21 15:55:35
Done (added test to notification-properties.html).
| |
66 | |
53 workerInfo.port.addEventListener('message', function(event) { | 67 workerInfo.port.addEventListener('message', function(event) { |
54 if (typeof event.data != 'object' || !event.data.command) { | 68 if (typeof event.data != 'object' || !event.data.command) { |
55 assert_unreached('Invalid message from the Service Worker. '); | 69 assert_unreached('Invalid message from the Service Worker. '); |
56 return; | 70 return; |
57 } | 71 } |
58 | 72 |
59 // (2) Listen for confirmation from the Service Worker that th e | 73 // (2) Listen for confirmation from the Service Worker that th e |
60 // notification's display promise has been resolved. | 74 // notification's display promise has been resolved. |
61 if (event.data.command == 'show') { | 75 if (event.data.command == 'show') { |
62 assert_true(event.data.success, 'The notification must hav e been displayed.'); | 76 assert_true(event.data.success, 'The notification must hav e been displayed.'); |
63 testRunner.simulateWebNotificationClick(scope); | 77 testRunner.simulateWebNotificationClick(scope); |
64 return; | 78 return; |
65 } | 79 } |
66 | 80 |
67 // (3) Listen for confirmation from the Service Worker that th e | 81 // (3) Listen for confirmation from the Service Worker that th e |
68 // notification has been clicked on. Make sure that all proper ties | 82 // notification has been clicked on. Make sure that all proper ties |
69 // set on the Notification object are as expected. | 83 // set on the cloned Notification object are as expected. |
70 assert_equals(event.data.command, 'click', 'The notification w as expected to be clicked.'); | 84 assert_equals(event.data.command, 'click', 'The notification w as expected to be clicked.'); |
85 assert_object_is_superset(event.data.notification, options, 'T he Notification object properties must be the same in notificationclick events.' ); | |
71 | 86 |
72 options.actions = options.actions.slice(0, Notification.maxAct ions); | 87 // (4) Check that the properties are also set correctly on the |
73 Object.keys(options).forEach(function(key) { | 88 // non-cloned Notification object from getNotifications. |
74 if (typeof options[key] == 'object') | 89 workerInfo.registration.getNotifications().then(function(notif ications) { |
75 assert_object_equals(event.data.notification[key], opt ions[key], 'The ' + key + ' field must be the same.'); | 90 assert_equals(notifications.length, 1); |
76 else | 91 assert_object_is_superset(notifications[0], options, 'The Notification object properties must be the same in getNotifications.'); |
77 assert_equals(event.data.notification[key], options[ke y], 'The ' + key + ' field must be the same.'); | 92 test.done(); |
78 }); | 93 }); |
79 | |
80 test.done(); | |
81 }); | 94 }); |
82 }).catch(unreached_rejection(test)); | 95 }).catch(unreached_rejection(test)); |
83 | 96 |
84 }, 'Clicking on a notification displayed by a Service Worker the notificat ionclick event.'); | 97 }, 'Clicking on a notification displayed by a Service Worker the notificat ionclick event.'); |
85 </script> | 98 </script> |
86 </body> | 99 </body> |
87 </html> | 100 </html> |
OLD | NEW |