OLD | NEW |
---|---|
(Empty) | |
1 <!doctype html> | |
2 <html> | |
3 <head> | |
4 <title>Notifications: ServiceWorkerRegistration.getNotifications() within a Service Worker.</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 getNotifications() function when used in a Service Worke r | |
13 // return an array of the notifications which were previously displayed us ing | |
14 // the same Service Worker registration id. | |
15 async_test(function(test) { | |
16 var scope = 'resources/scope/' + location.pathname, | |
17 script = 'resources/instrumentation-service-worker.js'; | |
18 | |
19 testRunner.grantWebNotificationPermission(location.origin, true); | |
20 | |
21 var info = null; | |
22 getActiveServiceWorkerWithMessagePort(test, script, scope).then(functi on(workerInfo) { | |
23 info = workerInfo; | |
24 | |
25 // (1) Display two notifications in the Document. | |
26 return info.registration.showNotification('Hello, world!', { | |
27 body: 'First notification' | |
28 }); | |
29 }).then(function() { | |
30 return info.registration.showNotification('Hello again, world!', { | |
31 body: 'Second notification' | |
32 }); | |
33 }).then(function() { | |
34 // (2) Request the Service Worker to give us all notifications. | |
35 info.port.postMessage({ | |
36 command: 'get' | |
37 }); | |
38 | |
39 info.port.addEventListener('message', function(event) { | |
johnme
2015/04/09 10:29:50
Ditto
Peter Beverloo
2015/04/17 13:03:17
Acknowledged.
| |
40 if (typeof event.data != 'object' || !event.data.command) { | |
41 assert_unreached('Invalid message from the Service Worker. '); | |
42 return; | |
43 } | |
44 | |
45 // (3) Confirm that the Service Worker was able to read both o f them. | |
46 assert_equals(event.data.command, 'get'); | |
47 assert_true(event.data.success); | |
48 | |
49 var notifications = event.data.notifications; | |
50 | |
51 assert_equals(notifications.length, 2); | |
52 | |
53 // We don't want to make any promises about the order of the | |
54 // returned notifications in |notifications|. | |
55 var firstIndex = notifications[0].title == 'Hello, world!' ? 0 : 1; | |
56 var secondIndex = firstIndex ? 0 : 1; | |
57 | |
58 assert_equals(notifications[firstIndex].title, 'Hello, world!' ); | |
59 assert_equals(notifications[firstIndex].body, 'First notificat ion'); | |
60 | |
61 assert_equals(notifications[secondIndex].title, 'Hello again, world!'); | |
62 assert_equals(notifications[secondIndex].body, 'Second notific ation'); | |
63 | |
64 test.done(); | |
johnme
2015/04/09 10:29:50
Perhaps you could also test that registration.getN
Peter Beverloo
2015/04/17 13:03:17
We have tests covering the correctness of both pro
| |
65 }); | |
66 }).catch(unreached_rejection(test)); | |
67 | |
68 }, 'ServiceWorkerRegistration.getNotifications() returns the opened notifi cations within a Service Worker.'); | |
69 </script> | |
70 </body> | |
71 </html> | |
OLD | NEW |