blobs.html (2674B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/gc.js"></script> 6 <script> 7 async_test(t => { 8 const c1 = new BroadcastChannel('blob'); 9 const c2 = new BroadcastChannel('blob'); 10 const c3 = new BroadcastChannel('blob'); 11 12 let readCount = 0; 13 c2.onmessage = t.step_func(e => { 14 // check blob 15 assert_true('blob' in e.data); 16 assert_true(e.data.blob instanceof Blob); 17 assert_equals(e.data.blob.size, 6); 18 const reader = new FileReader(); 19 reader.onerror = t.unreached_func(); 20 reader.onload = t.step_func(() => { 21 assert_equals(reader.result, 'foobar'); 22 if (++readCount == 2) 23 t.done(); 24 }); 25 reader.readAsText(e.data.blob); 26 }); 27 c3.onmessage = c2.onmessage; 28 (() => { 29 c1.postMessage({blob: new Blob(['foo', 'bar'])}); 30 })(); 31 garbageCollect(); 32 }, 'Blobs work on BroadcastChannel'); 33 34 async_test(t => { 35 const c1 = new BroadcastChannel('blobworker'); 36 const c2 = new BroadcastChannel('blobworker'); 37 const events = []; 38 39 const verifyEvents = function() { 40 assert_equals(events.length, 5); 41 assert_equals(events[0], 'from worker'); 42 assert_equals(events[1], 'from worker'); 43 assert_true(events[2].blob instanceof Blob); 44 assert_equals(events[2].blob.size, 11); 45 assert_true(events[3].blob instanceof Blob); 46 assert_equals(events[3].blob.size, 11); 47 assert_equals(events[4], 'done'); 48 const reader = new FileReader(); 49 reader.onerror = t.unreached_func(); 50 reader.onload = t.step_func(() => { 51 assert_equals(reader.result, 'hello-world'); 52 t.done(); 53 }); 54 reader.readAsText(events[3].blob); 55 }; 56 57 let receivedDone = false; 58 let receivedWorkerDone = false; 59 60 c1.onmessage = e => events.push(e.data); 61 c2.onmessage = e => events.push(e.data); 62 c2.addEventListener('message', t.step_func(e => { 63 if (e.data.blob) 64 c1.postMessage('done'); 65 if (e.data === 'done') 66 receivedDone = true; 67 if (receivedDone && receivedWorkerDone) 68 verifyEvents(); 69 })); 70 71 const worker = new Worker('resources/worker.js'); 72 worker.onmessage = t.step_func(e => { 73 receivedWorkerDone = true; 74 if (receivedDone && receivedWorkerDone) 75 verifyEvents(); 76 }); 77 worker.postMessage({channel: 'blobworker'}); 78 worker.postMessage({blob: ['hello-world']}); 79 80 }, 'Blobs work with workers on BroadcastChannel'); 81 82 </script>