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-port-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 function delay(ms) { | |
scheib
2015/05/06 22:36:18
unused
Marijn Kruisselbrink
2015/05/13 02:46:02
Done.
| |
26 return new Promise(function(resolve) { | |
27 window.setTimeout(resolve, ms); | |
28 }); | |
29 } | |
30 | |
31 test(function(test) { | |
32 assert_exists(window, 'internals'); | |
33 test.done(); | |
34 }, 'internals.terminateServiceWorker is required for the following tests.'); | |
35 | |
36 promise_test(function(test) { | |
37 var sw; | |
38 var channel = new MessageChannel(); | |
39 var portName = 'foobarname'; | |
40 return service_worker_unregister_and_register(test, worker, scope + 'basic') | |
41 .then(function(registration) { | |
42 sw = registration.installing; | |
43 sw.postMessage({name: portName, port: channel.port2}, [channel.port2]) ; | |
44 channel.port1.postMessage('first ping'); | |
45 return wait_for_reply(test, channel.port1); | |
46 }) | |
47 .then(test.step_func(function(reply) { | |
48 assert_equals(reply.data, 'first ping'); | |
49 assert_equals(reply.name, portName); | |
50 channel.port1.postMessage('ping pong'); | |
51 return wait_for_reply(test, channel.port1); | |
52 })) | |
53 .then(test.step_func(function(reply) { | |
54 assert_equals(reply.data, 'ping pong'); | |
55 assert_equals(reply.name, portName); | |
56 return internals.terminateServiceWorker(sw); | |
57 })) | |
58 .then(function() { | |
59 channel.port1.postMessage('second ping'); | |
60 return wait_for_reply(test, channel.port1); | |
61 }) | |
62 .then(test.step_func(function(reply) { | |
63 assert_equals(reply.data, 'second ping'); | |
64 assert_equals(reply.name, portName); | |
65 })); | |
66 }, 'Messages can be send when the service worker has been terminated'); | |
67 | |
68 </script> | |
OLD | NEW |