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.js"></script>
7 async_test(function(t) {
8 var scope = 'resources/blank.html';
11 service_worker_unregister_and_register(
12 t, 'resources/postmessage-worker.js', scope)
15 return get_newest_worker(registration);
19 var messageChannel = new MessageChannel();
20 messageChannel.port1.onmessage = t.step_func(onMessage);
21 sw.postMessage({port: messageChannel.port2}, [messageChannel.port2]);
22 sw.postMessage({value: 1});
23 sw.postMessage({value: 2});
24 sw.postMessage({done: true});
26 .catch(unreached_rejection(t));
34 function onMessage(e) {
36 if (message === 'quit') {
37 assert_array_equals(result, expected,
38 'Worker should post back expected values.');
39 postMessageToRedundantWorker();
45 function postMessageToRedundantWorker() {
46 registration.unregister(scope)
48 return wait_for_state(t, worker, 'redundant');
51 assert_equals(worker.state, 'redundant');
53 {name:'InvalidStateError'},
54 function() { worker.postMessage(''); },
55 'Calling postMessage on a redundant ServiceWorker should ' +
56 'throw InvalidStateError.');
59 .catch(unreached_rejection(t));
61 }, 'postMessage to a ServiceWorker (and back via MessagePort)');