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