OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Tests that postMessage works during and after terminating a service worke r</title> | 2 <title>Tests that postMessage works during and after terminating a service worke r</title> |
3 <script src="../../resources/testharness.js"></script> | 3 <script src="../../resources/testharness.js"></script> |
4 <script src="../../resources/testharnessreport.js"></script> | 4 <script src="../../resources/testharnessreport.js"></script> |
5 <script src="../../resources/testharness-helpers.js"></script> | 5 <script src="../../resources/testharness-helpers.js"></script> |
6 <script src="../resources/test-helpers.js"></script> | 6 <script src="../resources/test-helpers.js"></script> |
7 <script> | 7 <script> |
8 | 8 |
9 function sendPing(worker) { | 9 function sendPing(worker) { |
10 return new Promise(function(resolve) { | 10 return new Promise(function(resolve) { |
11 var channel = new MessageChannel(); | 11 var channel = new MessageChannel(); |
12 channel.port1.onmessage = function(message) { | 12 channel.port1.onmessage = function(message) { |
13 resolve(message.data); | 13 resolve(message.data); |
14 }; | 14 }; |
15 worker.postMessage({port: channel.port2}, [channel.port2]); | 15 worker.postMessage({port: channel.port2}, [channel.port2]); |
16 }); | 16 }); |
17 } | 17 } |
18 | 18 |
19 function delay(ms) { | |
20 return new Promise(function(resolve) { | |
21 window.setTimeout(resolve, ms); | |
22 }); | |
23 } | |
24 | |
19 promise_test(function(test) { | 25 promise_test(function(test) { |
20 var worker = 'resources/ping-worker.js'; | 26 var worker = 'resources/ping-worker.js'; |
21 var scope = 'resources/blank.html'; | 27 var scope = 'resources/blank.html'; |
22 var sw; | 28 var sw; |
23 return service_worker_unregister_and_register(test, worker, scope) | 29 return service_worker_unregister_and_register(test, worker, scope) |
24 .then(function(registration) { | 30 .then(function(registration) { |
25 return wait_for_update(test, registration); | 31 return wait_for_update(test, registration); |
26 }) | 32 }) |
27 .then(function(worker) { | 33 .then(function(worker) { |
28 sw = worker; | 34 sw = worker; |
29 return sendPing(sw); | 35 return sendPing(sw); |
30 }) | 36 }) |
31 .then(function(reply) { | 37 .then(function(reply) { |
32 assert_equals(reply, 1); | 38 assert_equals(reply, 1); |
33 return internals.terminateServiceWorker(sw); | 39 return internals.terminateServiceWorker(sw); |
34 }) | 40 }) |
35 .then(function() { | 41 .then(function() { |
36 return sendPing(sw); | 42 return sendPing(sw); |
37 }) | 43 }) |
38 .then(function(reply) { | 44 .then(function(reply) { |
39 assert_equals(reply, 1); | 45 assert_equals(reply, 1); |
40 return sendPing(sw); | 46 return sendPing(sw); |
41 }) | 47 }) |
42 .then(function(reply) { | 48 .then(function(reply) { |
43 assert_equals(reply, 2); | 49 assert_equals(reply, 2); |
44 return service_worker_unregister_and_done(test, scope); | 50 return service_worker_unregister_and_done(test, scope); |
45 }); | 51 }); |
46 }, 'Terminate terminates the worker.'); | 52 }, 'Terminate terminates the worker.'); |
47 | 53 |
54 async_test(function(t) { | |
55 var worker_script = 'resources/postmessage-worker.js'; | |
56 var scope = 'resources/postmessage-worker-scope'; | |
57 var worker; | |
58 var registration; | |
59 var port; | |
60 | |
61 service_worker_unregister_and_register( | |
62 t, worker_script, scope) | |
63 .then(function(r) { | |
64 registration = r; | |
65 return wait_for_activated(t, registration); | |
66 }) | |
67 .then(function() { | |
68 return get_newest_worker(registration); | |
falken
2014/11/20 04:09:28
registration.active
Marijn Kruisselbrink
2014/11/20 21:42:38
Done.
| |
69 }) | |
70 .then(function(sw) { | |
71 worker = sw; | |
72 internals.terminateServiceWorker(sw); | |
73 // FIXME: delay is temporary it work around http://crbug.com/433439 | |
74 return delay(500); | |
75 }) | |
76 .then(function() { | |
77 var messageChannel = new MessageChannel(); | |
78 port = messageChannel.port1; | |
79 port.onmessage = t.step_func(onMessage); | |
80 port.postMessage({value: 0}); | |
81 worker.postMessage({port: messageChannel.port2}, [messageChannel.port2 ]); | |
82 port.postMessage({value: 1}); | |
83 return delay(1); | |
falken
2014/11/20 04:09:29
can you comment what this delay is for?
Marijn Kruisselbrink
2014/11/20 21:42:38
Done.
| |
84 }) | |
85 .then(function() { | |
86 port.postMessage({value: 2}); | |
87 port.postMessage({done: true}); | |
88 }) | |
89 .catch(unreached_rejection(t)); | |
90 | |
91 var result = []; | |
92 var expected = [ | |
93 'Acking value: 0', | |
94 'Acking value: 1', | |
95 'Acking value: 2', | |
96 ]; | |
97 | |
98 function onMessage(e) { | |
99 var message = e.data; | |
100 console.log(message); | |
falken
2014/11/20 04:09:28
debugging code?
Marijn Kruisselbrink
2014/11/20 21:42:38
Done.
| |
101 if (message === 'quit') { | |
102 assert_array_equals(result, expected, | |
103 'Worker should post back expected values.'); | |
104 service_worker_unregister_and_done(t, scope); | |
105 } else { | |
106 result.push(message); | |
107 } | |
108 }; | |
109 }, 'postMessage a port to a not running service worker'); | |
falken
2014/11/20 04:09:28
hard to parse this... "on a port"?
Marijn Kruisselbrink
2014/11/20 21:42:37
Done.
| |
110 | |
111 async_test(function(t) { | |
112 var worker_script = 'resources/postmessage-port-worker.js'; | |
113 var scope = 'resources/postmessage-port-worker-scope'; | |
114 var worker; | |
115 var registration; | |
116 var port; | |
117 | |
118 service_worker_unregister_and_register( | |
119 t, worker_script, scope) | |
120 .then(function(r) { | |
121 registration = r; | |
122 return wait_for_activated(t, registration); | |
123 }) | |
124 .then(function() { | |
125 return get_newest_worker(registration); | |
126 }) | |
127 .then(function(sw) { | |
128 worker = sw; | |
129 internals.terminateServiceWorker(sw); | |
130 // FIXME: delay is temporary it work around http://crbug.com/433439 | |
131 return delay(500); | |
132 }) | |
133 .then(function() { | |
134 var innerChannel = new MessageChannel(); | |
135 var outerChannel = new MessageChannel(); | |
136 port = innerChannel.port1; | |
137 port.onmessage = t.step_func(onMessage); | |
138 port.postMessage({value: 0}); | |
139 outerChannel.port1.postMessage({port: innerChannel.port2}, [innerChann el.port2]); | |
140 worker.postMessage({port: outerChannel.port2}, [outerChannel.port2]); | |
141 port.postMessage({value: 1}); | |
142 return delay(1); | |
143 }) | |
144 .then(function() { | |
145 port.postMessage({value: 2}); | |
146 port.postMessage({done: true}); | |
147 }) | |
148 .catch(unreached_rejection(t)); | |
149 | |
150 var result = []; | |
151 var expected = [ | |
152 'Acking value: 0', | |
153 'Acking value: 1', | |
154 'Acking value: 2', | |
155 ]; | |
156 | |
157 function onMessage(e) { | |
158 var message = e.data; | |
159 console.log(message); | |
falken
2014/11/20 04:09:29
debugging code?
Marijn Kruisselbrink
2014/11/20 21:42:38
Done.
| |
160 if (message === 'quit') { | |
161 assert_array_equals(result, expected, | |
162 'Worker should post back expected values.'); | |
163 service_worker_unregister_and_done(t, scope); | |
164 } else { | |
165 result.push(message); | |
166 } | |
167 }; | |
168 }, 'postMessage a port to a port to a not running service worker'); | |
falken
2014/11/20 04:09:28
I can't parse this
Marijn Kruisselbrink
2014/11/20 21:42:37
I tried to rephrase it. If you have suggestions on
| |
169 | |
48 </script> | 170 </script> |
OLD | NEW |