offscreencanvas.transferrable.html (3012B)
1 <!DOCTYPE html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src="/html/canvas/resources/canvas-tests.js"></script> 5 <link rel="help" href="https://html.spec.whatwg.org/#offscreencanvas"> 6 7 <script id="myWorker" type="text/worker"> 8 9 function test1(offscreenCanvas) 10 { 11 return offscreenCanvas.width == 10 && offscreenCanvas.height == 10; 12 } 13 14 self.onmessage = function(e) { 15 switch(e.data.msg) { 16 case 'test1': 17 self.postMessage(test1(e.data.data)); 18 break; 19 } 20 }; 21 22 </script> 23 24 <script> 25 function makeWorker(script) 26 { 27 var blob = new Blob([script]); 28 return new Worker(URL.createObjectURL(blob)); 29 } 30 31 async_test(function(t) { 32 var worker = makeWorker(document.getElementById("myWorker").textContent); 33 var offscreenCanvas = new OffscreenCanvas(10, 10); 34 worker.postMessage({msg: 'test1', data: offscreenCanvas}, [offscreenCanvas]); 35 worker.addEventListener('message', t.step_func_done(function(msg) { 36 assert_true(msg.data); 37 })); 38 assert_equals(offscreenCanvas.width, 0); 39 assert_equals(offscreenCanvas.height, 0); 40 }, "Test that offscreenCanvas's size is correct after being transferred to a worker."); 41 42 43 test(function() { 44 function testException(contextType) { 45 var worker = makeWorker(document.getElementById("myWorker").textContent); 46 var offscreenCanvas = new OffscreenCanvas(10, 10); 47 var ctx = offscreenCanvas.getContext(contextType); 48 assert_throws_dom("InvalidStateError", function() { 49 worker.postMessage({offscreenCanvas}, [offscreenCanvas]); 50 }); 51 } 52 testException('2d'); 53 }, "Test that transfer an OffscreenCanvas that already have a 2d context throws exception."); 54 55 test(function() { 56 var worker = makeWorker(document.getElementById("myWorker").textContent); 57 var offscreenCanvas = new OffscreenCanvas(10, 10); 58 worker.postMessage({offscreenCanvas}, [offscreenCanvas]); 59 assert_throws_dom("DataCloneError", function() { 60 worker.postMessage({offscreenCanvas}, [offscreenCanvas]); 61 }); 62 }, "Test that transfer an OffscreenCanvas twice throws exception."); 63 64 test(function() { 65 var worker = makeWorker(document.getElementById("myWorker").textContent); 66 var offscreenCanvas = new OffscreenCanvas(10, 10); 67 worker.postMessage({offscreenCanvas}, [offscreenCanvas]); 68 assert_throws_dom("InvalidStateError", function() { 69 offscreenCanvas.getContext('2d'); 70 }); 71 }, "Test that calling getContext('2d') on a detached OffscreenCanvas throws exception."); 72 73 test(function() { 74 var worker = makeWorker(document.getElementById("myWorker").textContent); 75 var offscreenCanvas = new OffscreenCanvas(10, 10); 76 worker.postMessage({offscreenCanvas}, [offscreenCanvas]); 77 assert_throws_dom("InvalidStateError", function() { 78 offscreenCanvas.getContext('webgl'); 79 }); 80 }, "Test that calling getContext('webgl') on a detached OffscreenCanvas throws exception."); 81 82 </script>