OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: postMessage</title> | 2 <title>Service Worker: postMessage</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/test-helpers.js"></script> | 5 <script src="resources/test-helpers.js"></script> |
6 <script> | 6 <script> |
7 async_test(function(t) { | 7 |
| 8 promise_test(t => { |
| 9 var script = 'resources/postmessage-worker.js'; |
8 var scope = 'resources/blank.html'; | 10 var scope = 'resources/blank.html'; |
9 var registration; | 11 var registration; |
10 var worker; | 12 var worker; |
11 service_worker_unregister_and_register( | 13 var port; |
12 t, 'resources/postmessage-worker.js', scope) | 14 |
13 .then(function(r) { | 15 return service_worker_unregister_and_register(t, script, scope) |
| 16 .then(r => { |
| 17 add_completion_callback(() => r.unregister()); |
14 registration = r; | 18 registration = r; |
15 worker = registration.installing; | 19 worker = registration.installing; |
| 20 |
16 var messageChannel = new MessageChannel(); | 21 var messageChannel = new MessageChannel(); |
17 messageChannel.port1.onmessage = t.step_func(onMessage); | 22 port = messageChannel.port1; |
18 worker.postMessage({port: messageChannel.port2}, | 23 return new Promise(resolve => { |
19 [messageChannel.port2]); | 24 port.onmessage = resolve; |
20 worker.postMessage({value: 1}); | 25 worker.postMessage({port: messageChannel.port2}, |
21 worker.postMessage({value: 2}); | 26 [messageChannel.port2]); |
22 worker.postMessage({done: true}); | 27 worker.postMessage({value: 1}); |
| 28 worker.postMessage({value: 2}); |
| 29 worker.postMessage({done: true}); |
| 30 }); |
23 }) | 31 }) |
24 .catch(unreached_rejection(t)); | 32 .then(e => { |
| 33 assert_equals(e.data, 'Acking value: 1'); |
| 34 return new Promise(resolve => { port.onmessage = resolve; }); |
| 35 }) |
| 36 .then(e => { |
| 37 assert_equals(e.data, 'Acking value: 2'); |
| 38 return new Promise(resolve => { port.onmessage = resolve; }); |
| 39 }) |
| 40 .then(e => { |
| 41 assert_equals(e.data, 'quit'); |
| 42 return registration.unregister(scope); |
| 43 }) |
| 44 .then(() => { return wait_for_state(t, worker, 'redundant'); }) |
| 45 .then(() => { |
| 46 assert_equals(worker.state, 'redundant'); |
| 47 assert_throws( |
| 48 {name:'InvalidStateError'}, |
| 49 function() { worker.postMessage(''); }, |
| 50 'Calling postMessage on a redundant ServiceWorker should ' + |
| 51 'throw InvalidStateError.'); |
| 52 }); |
| 53 }, 'postMessage to a ServiceWorker (and back via MessagePort)'); |
25 | 54 |
26 var result = []; | |
27 var expected = [ | |
28 'Acking value: 1', | |
29 'Acking value: 2', | |
30 ]; | |
31 | |
32 function onMessage(e) { | |
33 var message = e.data; | |
34 if (message === 'quit') { | |
35 assert_array_equals(result, expected, | |
36 'Worker should post back expected values.'); | |
37 postMessageToRedundantWorker(); | |
38 } else { | |
39 result.push(message); | |
40 } | |
41 }; | |
42 | |
43 function postMessageToRedundantWorker() { | |
44 registration.unregister(scope) | |
45 .then(function() { | |
46 return wait_for_state(t, worker, 'redundant'); | |
47 }) | |
48 .then(function() { | |
49 assert_equals(worker.state, 'redundant'); | |
50 assert_throws( | |
51 {name:'InvalidStateError'}, | |
52 function() { worker.postMessage(''); }, | |
53 'Calling postMessage on a redundant ServiceWorker should ' + | |
54 'throw InvalidStateError.'); | |
55 t.done(); | |
56 }) | |
57 .catch(unreached_rejection(t)); | |
58 } | |
59 }, 'postMessage to a ServiceWorker (and back via MessagePort)'); | |
60 </script> | 55 </script> |
OLD | NEW |