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 function send_ping(worker) { | 8 function send_ping(worker) { |
9 return new Promise(function(resolve) { | 9 return new Promise(function(resolve) { |
10 var channel = new MessageChannel(); | 10 var channel = new MessageChannel(); |
11 channel.port1.onmessage = function(message) { | 11 channel.port1.onmessage = function(message) { |
12 resolve(message.data); | 12 resolve(message.data); |
13 }; | 13 }; |
14 worker.postMessage({port: channel.port2}, [channel.port2]); | 14 worker.postMessage({port: channel.port2}, [channel.port2]); |
15 }); | 15 }); |
16 } | 16 } |
17 | 17 |
| 18 function delay(ms) { |
| 19 return new Promise(function(resolve) { |
| 20 window.setTimeout(resolve, ms); |
| 21 }); |
| 22 } |
| 23 |
18 promise_test(function(test) { | 24 promise_test(function(test) { |
19 var worker = 'resources/ping-worker.js'; | 25 var worker = 'resources/ping-worker.js'; |
20 var scope = 'resources/blank.html'; | 26 var scope = 'resources/blank.html'; |
21 var sw; | 27 var sw; |
22 return service_worker_unregister_and_register(test, worker, scope) | 28 return service_worker_unregister_and_register(test, worker, scope) |
23 .then(function(registration) { | 29 .then(function(registration) { |
24 return wait_for_update(test, registration); | 30 return wait_for_update(test, registration); |
25 }) | 31 }) |
26 .then(function(worker) { | 32 .then(function(worker) { |
27 sw = worker; | 33 sw = worker; |
28 return send_ping(sw); | 34 return send_ping(sw); |
29 }) | 35 }) |
30 .then(function(reply) { | 36 .then(function(reply) { |
31 assert_equals(reply, 0); | 37 assert_equals(reply, 0); |
32 return internals.terminateServiceWorker(sw); | 38 return internals.terminateServiceWorker(sw); |
33 }) | 39 }) |
34 .then(function() { | 40 .then(function() { |
35 return send_ping(sw); | 41 return send_ping(sw); |
36 }) | 42 }) |
37 .then(function(reply) { | 43 .then(function(reply) { |
38 assert_equals(reply, 0); | 44 assert_equals(reply, 0); |
39 return send_ping(sw); | 45 return send_ping(sw); |
40 }) | 46 }) |
41 .then(function(reply) { | 47 .then(function(reply) { |
42 assert_equals(reply, 1); | 48 assert_equals(reply, 1); |
43 return service_worker_unregister_and_done(test, scope); | 49 return service_worker_unregister_and_done(test, scope); |
44 }); | 50 }); |
45 }, 'postMessage to a terminated service worker.'); | 51 }, 'postMessage to a terminated service worker.'); |
| 52 |
| 53 async_test(function(t) { |
| 54 var worker_script = 'resources/postmessage-worker.js'; |
| 55 var scope = 'resources/postmessage-worker-scope'; |
| 56 var worker; |
| 57 var registration; |
| 58 var port; |
| 59 |
| 60 service_worker_unregister_and_register( |
| 61 t, worker_script, scope) |
| 62 .then(function(r) { |
| 63 registration = r; |
| 64 return wait_for_activated(t, registration); |
| 65 }) |
| 66 .then(function() { |
| 67 worker = registration.active; |
| 68 return internals.terminateServiceWorker(worker); |
| 69 }) |
| 70 .then(function() { |
| 71 var messageChannel = new MessageChannel(); |
| 72 port = messageChannel.port1; |
| 73 port.onmessage = t.step_func(on_message); |
| 74 port.postMessage({value: 0}); |
| 75 worker.postMessage({port: messageChannel.port2}, |
| 76 [messageChannel.port2]); |
| 77 port.postMessage({value: 1}); |
| 78 // Asynchronously sending more messages gives chrome a chance to be in |
| 79 // a slightly different state, so wait a minimal amount of time. |
| 80 return delay(1); |
| 81 }) |
| 82 .then(function() { |
| 83 port.postMessage({value: 2}); |
| 84 port.postMessage({done: true}); |
| 85 }) |
| 86 .catch(unreached_rejection(t)); |
| 87 |
| 88 var result = []; |
| 89 var expected = [ |
| 90 'Acking value: 0', |
| 91 'Acking value: 1', |
| 92 'Acking value: 2', |
| 93 ]; |
| 94 |
| 95 function on_message(e) { |
| 96 var message = e.data; |
| 97 if (message === 'quit') { |
| 98 assert_array_equals(result, expected, |
| 99 'Worker should post back expected values.'); |
| 100 service_worker_unregister_and_done(t, scope); |
| 101 } else { |
| 102 result.push(message); |
| 103 } |
| 104 } |
| 105 }, 'postMessage on a port that was sent to a terminated service worker'); |
| 106 |
| 107 async_test(function(t) { |
| 108 var worker_script = 'resources/postmessage-port-worker.js'; |
| 109 var scope = 'resources/postmessage-port-worker-scope'; |
| 110 var worker; |
| 111 var registration; |
| 112 var port; |
| 113 |
| 114 service_worker_unregister_and_register( |
| 115 t, worker_script, scope) |
| 116 .then(function(r) { |
| 117 registration = r; |
| 118 return wait_for_activated(t, registration); |
| 119 }) |
| 120 .then(function() { |
| 121 worker = registration.active; |
| 122 return internals.terminateServiceWorker(worker); |
| 123 }) |
| 124 .then(function() { |
| 125 var innerChannel = new MessageChannel(); |
| 126 var outerChannel = new MessageChannel(); |
| 127 port = innerChannel.port1; |
| 128 port.onmessage = t.step_func(on_message); |
| 129 port.postMessage({value: 0}); |
| 130 outerChannel.port1.postMessage({port: innerChannel.port2}, |
| 131 [innerChannel.port2]); |
| 132 worker.postMessage({port: outerChannel.port2}, [outerChannel.port2]); |
| 133 port.postMessage({value: 1}); |
| 134 // Asynchronously sending more messages gives chrome a chance to be in |
| 135 // a slightly different state, so wait a minimal amount of time. |
| 136 return delay(1); |
| 137 }) |
| 138 .then(function() { |
| 139 port.postMessage({value: 2}); |
| 140 port.postMessage({done: true}); |
| 141 }) |
| 142 .catch(unreached_rejection(t)); |
| 143 |
| 144 var result = []; |
| 145 var expected = [ |
| 146 'Acking value: 0', |
| 147 'Acking value: 1', |
| 148 'Acking value: 2', |
| 149 ]; |
| 150 |
| 151 function on_message(e) { |
| 152 var message = e.data; |
| 153 if (message === 'quit') { |
| 154 assert_array_equals(result, expected, |
| 155 'Worker should post back expected values.'); |
| 156 service_worker_unregister_and_done(t, scope); |
| 157 } else { |
| 158 result.push(message); |
| 159 } |
| 160 } |
| 161 }, 'postMessage on a port that was sent on a port that was sent to a terminate
d service worker'); |
46 </script> | 162 </script> |
OLD | NEW |