Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / notifications / serviceworker-notificationclick-event-data-reflection.html
blob5b67070e889e68fde3b96d16b26d63f957349493
1 <!doctype html>
2 <html>
3 <head>
4 <title>Notifications: data property reflection in the "notificationclick" event.</title>
5 <script src="../resources/testharness.js"></script>
6 <script src="../resources/testharnessreport.js"></script>
7 <script src="../serviceworker/resources/test-helpers.js"></script>
8 <script src="resources/test-helpers.js"></script>
9 </head>
10 <body>
11 <script>
12 // Tests that the notification available in the "notificationclick" event in the
13 // Service Worker accurately reflects the data attributes of several type
14 // with which the notification was created (for this test --) in the document.
16 async_test(function(test) {
17 var scope = 'resources/scope/' + location.pathname,
18 script = 'resources/instrumentation-service-worker.js';
20 // Set notification's data of several type to a structured clone of options's data.
21 var notificationDataList = new Array(
22 true, // Check Boolean type
23 1024, // Check Number type
24 Number.NaN, // Check Number.NaN type
25 'any data', // Check String type
26 null, // Check null
27 new Array('Saab', 'Volvo', 'BMW'), // Check Array type
28 { first: 'first', second: 'second' } // Check object
31 testRunner.setPermission('notifications', 'granted', location.origin, location.origin);
32 getActiveServiceWorkerWithMessagePort(test, script, scope).then(function(workerInfo) {
33 // (1) Tell the Service Worker to display a Web Notification.
34 var assertNotificationDataReflects = function(pos) {
35 workerInfo.port.postMessage({
36 command: 'show',
38 title: scope,
39 options: {
40 title: scope,
41 tag: pos,
42 data: notificationDataList[pos]
44 });
47 workerInfo.port.addEventListener('message', function(event) {
48 if (typeof event.data != 'object' || !event.data.command) {
49 assert_unreached('Invalid message from the Service Worker.');
50 return;
53 // (2) Listen for confirmation from the Service Worker that the
54 // notification's display promise has been resolved.
55 if (event.data.command == 'show') {
56 assert_true(event.data.success, 'The notification must have been displayed.');
57 testRunner.simulateWebNotificationClick(scope);
58 return;
61 // (3) Listen for confirmation from the Service Worker that the
62 // notification has been clicked on. Make sure that all properties
63 // set on the Notification object are as expected.
64 assert_equals(event.data.command, 'click', 'The notification was expected to be clicked.');
66 var pos = event.data.notification.tag;
68 if (typeof notificationDataList[pos] === 'object' && notificationDataList[pos] !== null)
69 assert_object_equals(event.data.notification.data, notificationDataList[pos], 'The data field must be the same.');
70 else
71 assert_equals(event.data.notification.data, notificationDataList[pos], 'The data field must be the same.');
73 if (++pos < notificationDataList.length)
74 assertNotificationDataReflects(pos);
75 else
76 test.done();
77 });
79 assertNotificationDataReflects(0);
80 }).catch(unreached_rejection(test));
82 }, 'Clicking on a notification displayed by a Service Worker the notificationclick event.');
83 </script>
84 </body>
85 </html>