OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <meta charset=utf-8> |
| 3 <script src="../resources/testharness.js"></script> |
| 4 <script src="../resources/testharnessreport.js"></script> |
| 5 <script> |
| 6 |
| 7 async_test(t => { |
| 8 let c1 = new BroadcastChannel('worker'); |
| 9 let c2 = new BroadcastChannel('worker'); |
| 10 let events = []; |
| 11 |
| 12 c1.onmessage = e => events.push(e); |
| 13 c2.onmessage = e => events.push(e); |
| 14 |
| 15 let doneCount = 0; |
| 16 c2.addEventListener('message', t.step_func(e => { |
| 17 if (e.data == 'from worker') { |
| 18 c2.postMessage('from c2'); |
| 19 c1.postMessage('done'); |
| 20 } else if (e.data == 'done') { |
| 21 assert_equals(events.length, 4); |
| 22 assert_equals(events[0].data, 'from worker'); |
| 23 assert_equals(events[0].target, c1); |
| 24 assert_equals(events[1].data, 'from worker'); |
| 25 assert_equals(events[1].target, c2); |
| 26 assert_equals(events[2].data, 'from c2'); |
| 27 assert_equals(events[3].data, 'done'); |
| 28 if (++doneCount == 2) t.done(); |
| 29 } |
| 30 })); |
| 31 |
| 32 let worker = new Worker('resources/worker.js'); |
| 33 worker.onmessage = t.step_func(e => { |
| 34 assert_array_equals(e.data, ['from c2', 'done']); |
| 35 if (++doneCount == 2) t.done(); |
| 36 }); |
| 37 worker.postMessage({channel: 'worker'}); |
| 38 |
| 39 }, 'BroadcastChannel works in workers'); |
| 40 |
| 41 async_test(t => { |
| 42 let c1 = new BroadcastChannel('shared worker'); |
| 43 let c2 = new BroadcastChannel('shared worker'); |
| 44 let events = []; |
| 45 |
| 46 c1.onmessage = e => events.push(e); |
| 47 c2.onmessage = e => events.push(e); |
| 48 |
| 49 let doneCount = 0; |
| 50 c2.addEventListener('message', t.step_func(e => { |
| 51 if (e.data == 'from worker') { |
| 52 c2.postMessage('from c2'); |
| 53 c1.postMessage('done'); |
| 54 } else if (e.data == 'done') { |
| 55 assert_equals(events.length, 4); |
| 56 assert_equals(events[0].data, 'from worker'); |
| 57 assert_equals(events[0].target, c1); |
| 58 assert_equals(events[1].data, 'from worker'); |
| 59 assert_equals(events[1].target, c2); |
| 60 assert_equals(events[2].data, 'from c2'); |
| 61 assert_equals(events[3].data, 'done'); |
| 62 if (++doneCount == 2) t.done(); |
| 63 } |
| 64 })); |
| 65 |
| 66 let worker = new SharedWorker('resources/worker.js'); |
| 67 worker.port.onmessage = t.step_func(e => { |
| 68 assert_array_equals(e.data, ['from c2', 'done']); |
| 69 if (++doneCount == 2) t.done(); |
| 70 }); |
| 71 worker.port.postMessage({channel: 'shared worker'}); |
| 72 |
| 73 }, 'BroadcastChannel works in shared workers'); |
| 74 |
| 75 async_test(t => { |
| 76 let c = new BroadcastChannel('worker-close'); |
| 77 let events = []; |
| 78 |
| 79 c.onmessage = e => events.push('c1: ' + e.data); |
| 80 |
| 81 let worker = new Worker('resources/worker.js'); |
| 82 worker.onmessage = t.step_func(e => { |
| 83 assert_array_equals(events, ['c1: from worker', 'c2: echo'], |
| 84 'messages in document'); |
| 85 assert_array_equals(e.data, ['done'], 'messages in worker'); |
| 86 t.done(); |
| 87 }); |
| 88 |
| 89 c.addEventListener('message', e => { |
| 90 if (e.data == 'from worker') { |
| 91 c.close(); |
| 92 if (self.gc) self.gc(); |
| 93 window.setTimeout(() => { |
| 94 let c2 = new BroadcastChannel('worker-close'); |
| 95 c2.onmessage = e => { |
| 96 events.push('c2: ' + e.data); |
| 97 c2.postMessage('done'); |
| 98 }; |
| 99 worker.postMessage({ping: 'echo'}); |
| 100 }, 1); |
| 101 } |
| 102 }); |
| 103 |
| 104 worker.postMessage({channel: 'worker-close'}); |
| 105 |
| 106 }, 'Closing and re-opening a channel works.'); |
| 107 |
| 108 </script> |
OLD | NEW |