canvas-ImageBitmap-close.html (3586B)
1 <!DOCTYPE html> 2 <body> 3 <p>Tests that the close method of ImageBitmap does dispose the image data.</p> 4 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 9 promise_test(function(t) { 10 var worker = new Worker('worker-onmessage-noop.js'); 11 12 var imgHeight = 10; 13 var imgWidth = 10; 14 var imageData = new ImageData(10, 10); 15 var bitmap; 16 var ctx; 17 return createImageBitmap(imageData).then(imageBitmap => { 18 bitmap = imageBitmap; 19 assert_equals(bitmap.width, imgWidth, "bitmap.width = 10"); 20 assert_equals(bitmap.height, imgWidth, "bitmap.height = 10"); 21 22 // Apply structured clone to the bitmap, nothing should be changed 23 worker.postMessage({data: bitmap}); 24 assert_equals(bitmap.width, imgWidth, "bitmap.width = 10"); 25 assert_equals(bitmap.height, imgWidth, "bitmap.height = 10"); 26 27 // After calling close, the image data associated with the bitmap should no longer exist 28 bitmap.close(); 29 assert_equals(bitmap.width, 0, "bitmap.width = 0"); 30 assert_equals(bitmap.height, 0, "bitmap.height = 0"); 31 32 var canvas = document.createElement("canvas"); 33 canvas.width = imgWidth; 34 canvas.height = imgHeight; 35 ctx = canvas.getContext("2d"); 36 assert_throws_dom("InvalidStateError", function() { ctx.drawImage(bitmap, 0, 0); }); 37 38 // Try to apply structured clone to an already closed bitmap 39 try { 40 worker.postMessage({data: bitmap}); 41 throw new Error("Apply clone to an closed bitmap should be rejected"); 42 } 43 catch(ex) { 44 // Apply structured clone to an already closed bitmap is rejected as expected. 45 } 46 47 // Try to apply transferring to an already closed bitmap 48 try { 49 worker.postMessage({data: bitmap}, [bitmap]); 50 throw new Error("Apply transferring to an closed bitmap should be rejected"); 51 } catch(ex) { 52 // Apply structured clone to an already closed bitmap is rejected as expected. 53 } 54 55 // Calling createImageBitmap from a closed bitmap should be rejected 56 return createImageBitmap(bitmap).then(function() { 57 throw new Error("createImageBitmap from a closed bitmap should be rejected"); 58 }, ex => { 59 // Calling createImageBitmap from a closed ImageBitmap is rejected as expected. 60 }); 61 }).then(() => { 62 // Call close to a already closed bitmap should be noop. 63 bitmap.close(); 64 assert_equals(bitmap.width, 0, "bitmap.height = 0"); 65 assert_equals(bitmap.height, 0, "bitmap.height = 0"); 66 67 return createImageBitmap(imageData).then(imageBitmap => { 68 bitmap = imageBitmap; 69 assert_equals(bitmap.width, imgWidth, "bitmap.width = 10"); 70 assert_equals(bitmap.height, imgWidth, "bitmap.height = 10"); 71 72 // Transfer the bitmap to a worker 73 worker.postMessage({data: bitmap}, [bitmap]); 74 75 // After transferring, the bitmap is neutered. 76 assert_equals(bitmap.width, 0, "bitmap.height = 0"); 77 assert_equals(bitmap.height, 0, "bitmap.height = 0"); 78 79 // Calling close to a neutered bitmap should be noop. 80 bitmap.close(); 81 assert_equals(bitmap.width, 0, "bitmap.height = 0"); 82 assert_equals(bitmap.height, 0, "bitmap.height = 0"); 83 84 }); 85 }).catch(function(ex) { 86 throw new Error("No exception should be thrown."); 87 }) 88 }); 89 </script>