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