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 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});
24 .catch(unreached_rejection(t
));
32 function onMessage(e
) {
34 if (message
=== 'quit') {
35 assert_array_equals(result
, expected
,
36 'Worker should post back expected values.');
37 postMessageToRedundantWorker();
43 function postMessageToRedundantWorker() {
44 registration
.unregister(scope
)
46 return wait_for_state(t
, worker
, 'redundant');
49 assert_equals(worker
.state
, 'redundant');
51 {name
:'InvalidStateError'},
52 function() { worker
.postMessage(''); },
53 'Calling postMessage on a redundant ServiceWorker should ' +
54 'throw InvalidStateError.');
57 .catch(unreached_rejection(t
));
59 }, 'postMessage to a ServiceWorker (and back via MessagePort)');