OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title> |
| 3 Tests stashed message ports when the service worker is killed. |
| 4 </title> |
| 5 <script src="../../../resources/testharness.js"></script> |
| 6 <script src="../../../resources/testharnessreport.js"></script> |
| 7 <script src="../resources/test-helpers.js"></script> |
| 8 <script> |
| 9 var worker = 'resources/stashed-ports-echo-worker.js'; |
| 10 var scope = 'resources/'; |
| 11 |
| 12 // Helper method that waits for a reply on a port, and resolves a promise with |
| 13 // the reply. |
| 14 function wait_for_reply(test, port) { |
| 15 return new Promise(function(resolve) { |
| 16 var resolved = false; |
| 17 port.onmessage = test.step_func(function(event) { |
| 18 assert_false(resolved); |
| 19 resolved = true; |
| 20 resolve(event.data); |
| 21 }); |
| 22 }); |
| 23 } |
| 24 |
| 25 test(function(test) { |
| 26 assert_exists(window, 'internals'); |
| 27 test.done(); |
| 28 }, 'internals.terminateServiceWorker is required for the following tests.'); |
| 29 |
| 30 promise_test(function(test) { |
| 31 var sw; |
| 32 var channel = new MessageChannel(); |
| 33 var portName = 'foobarname'; |
| 34 return service_worker_unregister_and_register(test, worker, scope + 'basic') |
| 35 .then(function(registration) { |
| 36 sw = registration.installing; |
| 37 sw.postMessage({name: portName, port: channel.port2}, [channel.port2])
; |
| 38 channel.port1.postMessage('first ping'); |
| 39 return wait_for_reply(test, channel.port1); |
| 40 }) |
| 41 .then(test.step_func(function(reply) { |
| 42 assert_equals(reply.data, 'first ping'); |
| 43 assert_equals(reply.name, portName); |
| 44 channel.port1.postMessage('ping pong'); |
| 45 return wait_for_reply(test, channel.port1); |
| 46 })) |
| 47 .then(test.step_func(function(reply) { |
| 48 assert_equals(reply.data, 'ping pong'); |
| 49 assert_equals(reply.name, portName); |
| 50 return internals.terminateServiceWorker(sw); |
| 51 })) |
| 52 .then(function() { |
| 53 channel.port1.postMessage('second ping'); |
| 54 return wait_for_reply(test, channel.port1); |
| 55 }) |
| 56 .then(test.step_func(function(reply) { |
| 57 assert_equals(reply.data, 'second ping'); |
| 58 assert_equals(reply.name, portName); |
| 59 })); |
| 60 }, 'Messages can be send when the service worker has been terminated'); |
| 61 |
| 62 </script> |
OLD | NEW |