OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Service Worker: postMessage</title> |
| 3 <script src="/resources/testharness.js"></script> |
| 4 <script src="/resources/testharnessreport.js"></script> |
| 5 <script src="resources/test-helpers.sub.js"></script> |
| 6 <script> |
| 7 async_test(function(t) { |
| 8 var scope = 'resources/blank.html'; |
| 9 var registration; |
| 10 var worker; |
| 11 service_worker_unregister_and_register( |
| 12 t, 'resources/postmessage-worker.js', scope) |
| 13 .then(function(r) { |
| 14 registration = r; |
| 15 worker = registration.installing; |
| 16 var messageChannel = new MessageChannel(); |
| 17 messageChannel.port1.onmessage = t.step_func(onMessage); |
| 18 worker.postMessage({port: messageChannel.port2}, |
| 19 [messageChannel.port2]); |
| 20 worker.postMessage({value: 1}); |
| 21 worker.postMessage({value: 2}); |
| 22 worker.postMessage({done: true}); |
| 23 }) |
| 24 .catch(unreached_rejection(t)); |
| 25 |
| 26 var result = []; |
| 27 var expected = [ |
| 28 'Acking value: 1', |
| 29 'Acking value: 2', |
| 30 ]; |
| 31 |
| 32 function onMessage(e) { |
| 33 var message = e.data; |
| 34 if (message === 'quit') { |
| 35 assert_array_equals(result, expected, |
| 36 'Worker should post back expected values.'); |
| 37 postMessageToRedundantWorker(); |
| 38 } else { |
| 39 result.push(message); |
| 40 } |
| 41 }; |
| 42 |
| 43 function postMessageToRedundantWorker() { |
| 44 registration.unregister(scope) |
| 45 .then(function() { |
| 46 return wait_for_state(t, worker, 'redundant'); |
| 47 }) |
| 48 .then(function() { |
| 49 assert_equals(worker.state, 'redundant'); |
| 50 assert_throws( |
| 51 {name:'InvalidStateError'}, |
| 52 function() { worker.postMessage(''); }, |
| 53 'Calling postMessage on a redundant ServiceWorker should ' + |
| 54 'throw InvalidStateError.'); |
| 55 t.done(); |
| 56 }) |
| 57 .catch(unreached_rejection(t)); |
| 58 } |
| 59 }, 'postMessage to a ServiceWorker (and back via MessagePort)'); |
| 60 </script> |
OLD | NEW |