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 async_test(function(t) { |
8 t.step(function() { | 8 var scope = 'resources/blank.html'; |
9 var scope = 'resources/blank.html' | |
10 service_worker_unregister_and_register( | 9 service_worker_unregister_and_register( |
11 t, 'resources/postmessage-msgport-to-client-worker.js', scope).then(t.st
ep_func(onRegister)); | 10 t, 'resources/postmessage-msgport-to-client-worker.js', scope) |
12 | 11 .then(function(registration) { |
13 function onRegister(worker) { | 12 return wait_for_update(t, registration); |
14 worker.addEventListener('statechange', t.step_func(function(event) { | 13 }) |
15 if (event.target.state == 'activated') | 14 .then(function(sw) { |
16 onActive(); | 15 return wait_for_state(t, sw, 'activated'); |
17 })); | 16 }) |
18 } | 17 .then(function() { return with_iframe(scope); }) |
19 | 18 .then(function(frame) { |
20 function onActive() { | 19 var w = frame.contentWindow; |
21 with_iframe(scope, t.step_func(function(frame) { | 20 w.onmessage = t.step_func(onMessage); |
22 var w = frame.contentWindow; | 21 w.navigator.serviceWorker.controller.postMessage('ping'); |
23 w.onmessage = t.step_func(onMessage); | 22 }) |
24 w.navigator.serviceWorker.controller.postMessage('ping'); | 23 .catch(unreached_rejection(t)); |
25 })); | |
26 } | |
27 | 24 |
28 var result = []; | 25 var result = []; |
29 var expected = [ | 26 var expected = [ |
30 'Acking value: 1', | 27 'Acking value: 1', |
31 'Acking value: 2', | 28 'Acking value: 2', |
32 ]; | 29 ]; |
33 | 30 |
34 function onMessage(e) { | 31 function onMessage(e) { |
35 var message = e.data; | 32 var message = e.data; |
36 if ('port' in message) { | 33 if ('port' in message) { |
37 var port = message.port; | 34 var port = message.port; |
38 port.postMessage({value: 1}); | 35 port.postMessage({value: 1}); |
39 port.postMessage({value: 2}); | 36 port.postMessage({value: 2}); |
40 port.postMessage({done: true}); | 37 port.postMessage({done: true}); |
41 } else if ('ack' in message) { | 38 } else if ('ack' in message) { |
42 result.push(message.ack); | 39 result.push(message.ack); |
43 } else if ('done' in message) { | 40 } else if ('done' in message) { |
44 assert_array_equals(result, expected, 'Worker should post back expec
ted values via MessagePort.'); | 41 assert_array_equals( |
45 service_worker_unregister_and_done(t, scope); | 42 result, expected, |
46 } else { | 43 'Worker should post back expected values via MessagePort.'); |
47 assert_unreached('Got unexpected message from ServiceWorker'); | 44 service_worker_unregister_and_done(t, scope); |
48 } | 45 } else { |
| 46 assert_unreached('Got unexpected message from ServiceWorker'); |
| 47 } |
49 } | 48 } |
50 }); | 49 }, 'postMessage MessagePorts from ServiceWorker to Client'); |
51 </script> | 50 </script> |
OLD | NEW |