2 <title>Service Worker: postMessage to Client
</title>
3 <script src=
"../resources/testharness.js"></script>
4 <script src=
"../resources/testharnessreport.js"></script>
5 <script src=
"resources/test-helpers.js"></script>
7 var t
= async_test('postMessage MessagePorts from ServiceWorker to Client');
9 var scope
= 'resources/blank.html'
10 service_worker_unregister_and_register(
11 t
, 'resources/postmessage-msgport-to-client-worker.js', scope
)
12 .then(function(registration
) {
13 return wait_for_state(t
, registration
.installing
, 'activated');
15 .then(function() { return with_iframe(scope
); })
16 .then(function(frame
) {
17 var w
= frame
.contentWindow
;
18 w
.navigator
.serviceWorker
.onmessage
= t
.step_func(onMessage
);
19 w
.navigator
.serviceWorker
.controller
.postMessage('ping');
21 .catch(unreached_rejection(t
));
29 function onMessage(e
) {
31 if ('port' in message
) {
32 var port
= message
.port
;
33 port
.postMessage({value
: 1});
34 port
.postMessage({value
: 2});
35 port
.postMessage({done
: true});
36 } else if ('ack' in message
) {
37 result
.push(message
.ack
);
38 } else if ('done' in message
) {
41 'Worker should post back expected values via MessagePort.');
42 service_worker_unregister_and_done(t
, scope
);
44 assert_unreached('Got unexpected message from ServiceWorker');