2 <title>Tests that postMessage works during and after terminating a service worker
</title>
3 <script src=
"../../resources/testharness.js"></script>
4 <script src=
"../../resources/testharnessreport.js"></script>
5 <script src=
"../../resources/testharness-helpers.js"></script>
6 <script src=
"../resources/test-helpers.js"></script>
8 function send_ping(worker
) {
9 return new Promise(function(resolve
) {
10 var channel
= new MessageChannel();
11 channel
.port1
.onmessage = function(message
) {
12 resolve(message
.data
);
14 worker
.postMessage({port
: channel
.port2
}, [channel
.port2
]);
19 return new Promise(function(resolve
) {
20 window
.setTimeout(resolve
, ms
);
24 promise_test(function(test
) {
25 var worker
= 'resources/ping-worker.js';
26 var scope
= 'resources/blank.html';
28 return service_worker_unregister_and_register(test
, worker
, scope
)
29 .then(function(registration
) {
30 sw
= registration
.installing
;
33 .then(function(reply
) {
34 assert_equals(reply
, 0);
35 return internals
.terminateServiceWorker(sw
);
40 .then(function(reply
) {
41 assert_equals(reply
, 0);
44 .then(function(reply
) {
45 assert_equals(reply
, 1);
46 return service_worker_unregister_and_done(test
, scope
);
48 }, 'postMessage to a terminated service worker.');
50 async_test(function(t
) {
51 var worker_script
= 'resources/postmessage-worker.js';
52 var scope
= 'resources/postmessage-worker-scope';
57 service_worker_unregister_and_register(
58 t
, worker_script
, scope
)
61 return wait_for_state(t
, r
.installing
, 'activated');
64 worker
= registration
.active
;
65 return internals
.terminateServiceWorker(worker
);
68 var messageChannel
= new MessageChannel();
69 port
= messageChannel
.port1
;
70 port
.onmessage
= t
.step_func(on_message
);
71 port
.postMessage({value
: 0});
72 worker
.postMessage({port
: messageChannel
.port2
},
73 [messageChannel
.port2
]);
74 port
.postMessage({value
: 1});
75 // Asynchronously sending more messages gives chrome a chance to be in
76 // a slightly different state, so wait a minimal amount of time.
80 port
.postMessage({value
: 2});
81 port
.postMessage({done
: true});
83 .catch(unreached_rejection(t
));
92 function on_message(e
) {
94 if (message
=== 'quit') {
95 assert_array_equals(result
, expected
,
96 'Worker should post back expected values.');
97 service_worker_unregister_and_done(t
, scope
);
102 }, 'postMessage on a port that was sent to a terminated service worker');
104 async_test(function(t
) {
105 var worker_script
= 'resources/postmessage-port-worker.js';
106 var scope
= 'resources/postmessage-port-worker-scope';
111 service_worker_unregister_and_register(
112 t
, worker_script
, scope
)
115 return wait_for_state(t
, r
.installing
, 'activated');
118 worker
= registration
.active
;
119 return internals
.terminateServiceWorker(worker
);
122 var innerChannel
= new MessageChannel();
123 var outerChannel
= new MessageChannel();
124 port
= innerChannel
.port1
;
125 port
.onmessage
= t
.step_func(on_message
);
126 port
.postMessage({value
: 0});
127 outerChannel
.port1
.postMessage({port
: innerChannel
.port2
},
128 [innerChannel
.port2
]);
129 worker
.postMessage({port
: outerChannel
.port2
}, [outerChannel
.port2
]);
130 port
.postMessage({value
: 1});
131 // Asynchronously sending more messages gives chrome a chance to be in
132 // a slightly different state, so wait a minimal amount of time.
136 port
.postMessage({value
: 2});
137 port
.postMessage({done
: true});
139 .catch(unreached_rejection(t
));
148 function on_message(e
) {
149 var message
= e
.data
;
150 if (message
=== 'quit') {
151 assert_array_equals(result
, expected
,
152 'Worker should post back expected values.');
153 service_worker_unregister_and_done(t
, scope
);
155 result
.push(message
);
158 }, 'postMessage on a port that was sent on a port that was sent to a terminated service worker');