OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <title>Service Worker: postMessage to Client</title> | |
3 <script src="../resources/testharness.js"></script> | |
4 <script src="../resources/testharnessreport.js"></script> | |
5 <script src="resources/test-helpers.js"></script> | |
6 <script> | |
7 | |
8 promise_test(t => { | |
9 var script = 'resources/postmessage-to-client-worker.js'; | |
10 var scope = 'resources/blank.html'; | |
11 var w; | |
12 | |
13 return service_worker_unregister_and_register(t, script, scope) | |
14 .then(registration => { | |
15 add_completion_callback(() => registration.unregister()); | |
16 return wait_for_state(t, registration.installing, 'activated'); | |
17 }) | |
18 .then(() => with_iframe(scope)) | |
19 .then(frame => { | |
20 return new Promise(resolve => { | |
21 w = frame.contentWindow; | |
22 w.navigator.serviceWorker.onmessage = resolve; | |
23 w.navigator.serviceWorker.controller.postMessage('ping'); | |
24 }); | |
25 }) | |
26 .then(e => { | |
27 var message = e.data; | |
28 assert_equals(e.constructor, w.MessageEvent, | |
29 'message events should use MessageEvent interface.'); | |
30 assert_equals(e.type, 'message', 'type should be "message".'); | |
31 assert_equals( | |
32 e.origin, location.origin, | |
33 'origin of message should be origin of Service Worker.'); | |
34 assert_equals(e.lastEventId, '', | |
35 'lastEventId should be an empty string.'); | |
36 assert_equals(e.source.constructor, w.ServiceWorker, | |
37 'source should use ServiceWorker interface.'); | |
38 assert_equals( | |
39 e.source, w.navigator.serviceWorker.controller, | |
40 'source should be the service worker that sent the message.'); | |
41 assert_equals(e.ports.length, 0, 'ports should be an empty array.'); | |
42 assert_equals(message, 'Sending message via clients'); | |
43 return new Promise(resolve => { | |
44 w.navigator.serviceWorker.onmessage = resolve; | |
45 }); | |
46 }) | |
47 .then(e => { assert_equals(e.data, 'quit'); }); | |
48 }, 'postMessage from ServiceWorker to Client.'); | |
49 | |
50 </script> | |
OLD | NEW |