4 <title>Notifications: ServiceWorkerRegistration.showNotification().
</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>
12 // Tests that the showNotification() function when used in a Service Worker
13 // resolves a promise, and that the notificationclick event gets fired when
14 // we simulate a click on it. This test requires the test runner.
16 async_test(function(test
) {
17 var scope
= 'resources/scope/serviceworkerregistration-service-worker-click',
18 script
= 'resources/instrumentation-service-worker.js';
20 testRunner
.setPermission('notifications', 'granted', location
.origin
, location
.origin
);
21 getActiveServiceWorkerWithMessagePort(test
, script
, scope
).then(function(workerInfo
) {
22 // (1) Tell the Service Worker to display a Web Notification.
23 workerInfo
.port
.postMessage({
27 options
: { body
: 'Hello, world!' }
30 workerInfo
.port
.addEventListener('message', function(event
) {
31 if (typeof event
.data
!= 'object' || !event
.data
.command
) {
32 assert_unreached('Invalid message from the Service Worker.');
36 // (2) Listen for confirmation from the Service Worker that the
37 // notification's display promise has been resolved.
38 if (event
.data
.command
== 'show') {
39 assert_true(event
.data
.success
, 'The notification must have been displayed.');
40 testRunner
.simulateWebNotificationClick(scope
);
44 // (3) Listen for confirmation from the Service Worker that the
45 // notification has been clicked on.
46 if (event
.data
.command
== 'click') {
47 assert_equals(event
.data
.notification
.title
, scope
, 'The right notification must have been clicked.');
53 assert_unreached('Unexpected message from the Service Worker: ' + event
.data
.command
);
55 }).catch(unreached_rejection(test
));
57 }, 'Clicking on a notification displayed by a Service Worker the notificationclick event.');