OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: postMessage to Client</title> | 2 <title>Service Worker: postMessage via MessagePort to Client</title> |
3 <script src="/resources/testharness.js"></script> | 3 <script src="/resources/testharness.js"></script> |
4 <script src="/resources/testharnessreport.js"></script> | 4 <script src="/resources/testharnessreport.js"></script> |
5 <script src="resources/test-helpers.sub.js"></script> | 5 <script src="resources/test-helpers.sub.js"></script> |
6 <script> | 6 <script> |
7 var frame; | 7 promise_test(t => { |
8 var t = async_test('postMessage MessagePorts from ServiceWorker to Client'); | 8 var script = 'resources/postmessage-msgport-to-client-worker.js'; |
9 t.step(function() { | 9 var scope = 'resources/blank.html'; |
10 var scope = 'resources/blank.html' | 10 var port; |
11 service_worker_unregister_and_register( | 11 |
12 t, 'resources/postmessage-msgport-to-client-worker.js', scope) | 12 return service_worker_unregister_and_register(t, script, scope) |
13 .then(function(registration) { | 13 .then(registration => { |
| 14 add_completion_callback(() => registration.unregister()); |
14 return wait_for_state(t, registration.installing, 'activated'); | 15 return wait_for_state(t, registration.installing, 'activated'); |
15 }) | 16 }) |
16 .then(function() { return with_iframe(scope); }) | 17 .then(() => with_iframe(scope)) |
17 .then(function(f) { | 18 .then(frame => { |
18 frame = f; | 19 t.add_cleanup(() => frame.remove()); |
19 var w = frame.contentWindow; | 20 return new Promise(resolve => { |
20 w.navigator.serviceWorker.onmessage = t.step_func(onMessage); | 21 var w = frame.contentWindow; |
21 w.navigator.serviceWorker.controller.postMessage('ping'); | 22 w.navigator.serviceWorker.onmessage = resolve; |
| 23 w.navigator.serviceWorker.controller.postMessage('ping'); |
| 24 }); |
22 }) | 25 }) |
23 .catch(unreached_rejection(t)); | 26 .then(e => { |
24 | 27 port = e.ports[0]; |
25 var result = []; | 28 port.postMessage({value: 1}); |
26 var expected = [ | 29 port.postMessage({value: 2}); |
27 'Acking value: 1', | 30 port.postMessage({done: true}); |
28 'Acking value: 2', | 31 return new Promise(resolve => { port.onmessage = resolve; }); |
29 ]; | 32 }) |
30 | 33 .then(e => { |
31 function onMessage(e) { | 34 assert_equals(e.data.ack, 'Acking value: 1'); |
32 var message = e.data; | 35 return new Promise(resolve => { port.onmessage = resolve; }); |
33 if ('port' in message) { | 36 }) |
34 var port = message.port; | 37 .then(e => { |
35 port.postMessage({value: 1}); | 38 assert_equals(e.data.ack, 'Acking value: 2'); |
36 port.postMessage({value: 2}); | 39 return new Promise(resolve => { port.onmessage = resolve; }); |
37 port.postMessage({done: true}); | 40 }) |
38 } else if ('ack' in message) { | 41 .then(e => { assert_true(e.data.done, 'done'); }); |
39 result.push(message.ack); | 42 }, 'postMessage MessagePorts from ServiceWorker to Client'); |
40 } else if ('done' in message) { | |
41 assert_array_equals( | |
42 result, expected, | |
43 'Worker should post back expected values via MessagePort.'); | |
44 frame.remove(); | |
45 service_worker_unregister_and_done(t, scope); | |
46 } else { | |
47 assert_unreached('Got unexpected message from ServiceWorker'); | |
48 } | |
49 } | |
50 }); | |
51 </script> | 43 </script> |
OLD | NEW |