| OLD | NEW | 
|    1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |    1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | 
|    2 <html> |    2 <html> | 
|    3 <head> |    3 <head> | 
|    4 <script src="../../resources/js-test.js"></script> |    4 <script src="../../resources/js-test.js"></script> | 
|    5 </head> |    5 </head> | 
|    6 <body> |    6 <body> | 
|    7 <script> |    7 <script> | 
|    8 description("Ensure correct behavior of createImageBitmap for invalid inputs."); |    8 description("Ensure correct behavior of createImageBitmap for invalid inputs."); | 
|    9 window.jsTestIsAsync = true; |    9 window.jsTestIsAsync = true; | 
|   10  |   10  | 
|   11 var TypeError = "TypeError: Failed to execute 'createImageBitmap' on 'Window': N
     o function was found that matched the signature provided."; |   11 var reason; | 
|   12 var IndexSizeError = "IndexSizeError: Index or size was negative, or greater tha
     n the allowed value."; |  | 
|   13  |   12  | 
|   14 var image; |   13 function shouldBeRejected(promise, message) { | 
|   15 var testBitmap; // an ImageBitmap that is uncropped. We use this to test createI
     mageBitmap(testBitmap) |   14     return promise.then(function() { | 
|   16 var d;          // image.imageData |   15         testFailed('Resolved unexpectedly: ' + message); | 
|   17 var blob; |   16     }, function(e) { | 
|   18 var invalidBlob; |   17         reason = e; | 
|   19 var invalidBlobTestPassed = false; |   18         testPassed('Rejected as expected: ' + message); | 
|   20 var invalidCanvasTestPassed = false; |   19         shouldBeTrue('reason instanceof Error'); | 
|   21  |   20         debug(e); | 
|   22 // Draw to an auxillary canvas. |  | 
|   23 var aCanvas = document.createElement("canvas"); |  | 
|   24 aCanvas.setAttribute("width", "200"); |  | 
|   25 aCanvas.setAttribute("height", "200"); |  | 
|   26 var aCtx = aCanvas.getContext("2d"); |  | 
|   27  |  | 
|   28 // Create a terapixel canvas to generate an invalid canvas through allocation fa
     ilure |  | 
|   29 var invalidCanvas = document.createElement("canvas"); |  | 
|   30 invalidCanvas.setAttribute("width", "1000000"); |  | 
|   31 invalidCanvas.setAttribute("height", "1000000"); |  | 
|   32  |  | 
|   33 image = new Image(); |  | 
|   34 image.onload = imageLoaded; |  | 
|   35  |  | 
|   36 // Before image loads |  | 
|   37 shouldThrow("createImageBitmap(image)"); |  | 
|   38 image.src = aCanvas.toDataURL(); |  | 
|   39  |  | 
|   40 video = document.createElement("video"); |  | 
|   41 video.addEventListener("canplaythrough", videoLoaded, false); |  | 
|   42  |  | 
|   43 // Before video loads |  | 
|   44 shouldThrow("createImageBitmap(video)"); |  | 
|   45 video.src = "../../compositing/resources/video.ogv"; |  | 
|   46  |  | 
|   47 var imageLoaded = false; |  | 
|   48 var videoLoaded = false; |  | 
|   49 var imageBitmapLoaded = false; |  | 
|   50 var blobLoaded = false; |  | 
|   51 var invalidBlobLoaded = false; |  | 
|   52  |  | 
|   53 function imageLoaded() { |  | 
|   54     createImageBitmap(image).then(imageBitmapLoadedCallback, function() { |  | 
|   55         testFailed("Promise was rejected."); |  | 
|   56         finishJSTest(); |  | 
|   57     }); |   21     }); | 
|   58     d = aCtx.getImageData(0, 0, 200, 200); |  | 
|   59     imageLoaded = true; |  | 
|   60     loaded(); |  | 
|   61 } |   22 } | 
|   62  |   23  | 
|   63 function videoLoaded() { |   24 function checkInvalidRange(source, message) { | 
|   64     videoLoaded = true; |   25     return Promise.resolve().then(function() { | 
|   65     loaded(); |   26         return shouldBeRejected(createImageBitmap(source, 0, 0, 10, 0), message 
     + ' / invalid range'); | 
 |   27     }).then(function() { | 
 |   28         return shouldBeRejected(createImageBitmap(source, 0, 0, 0, 10), message 
     + ' / invalid range'); | 
 |   29     }); | 
|   66 } |   30 } | 
|   67  |   31  | 
|   68 function imageBitmapLoadedCallback(imageBitmap) { |   32 function createCanvas() { | 
|   69     testBitmap = imageBitmap; |   33     return new Promise(function(resolve, reject) { | 
|   70     imageBitmapLoaded = true; |   34         var canvas = document.createElement('canvas'); | 
|   71     loaded(); |   35         canvas.setAttribute('width', '200'); | 
 |   36         canvas.setAttribute('height', '200'); | 
 |   37         resolve(canvas); | 
 |   38     }); | 
|   72 } |   39 } | 
|   73  |   40  | 
|   74 var xhr = new XMLHttpRequest(); |   41 function createInvalidCanvas() { | 
|   75 xhr.open("GET", 'resources/pattern.png'); |   42     // Create a terapixel canvas to generate an invalid canvas through | 
|   76 xhr.responseType = 'blob'; |   43     // allocation failure | 
|   77 xhr.send(); |   44     return new Promise(function(resolve, reject) { | 
|   78 xhr.onload = function() { |   45         var canvas = document.createElement('canvas'); | 
|   79     blob = xhr.response; |   46         canvas.setAttribute('width', '100000000'); | 
|   80     blobLoaded = true; |   47         canvas.setAttribute('height', '100000000'); | 
|   81     loaded(); |   48         resolve(canvas); | 
 |   49     }); | 
|   82 } |   50 } | 
|   83  |   51  | 
|   84 var xhr2 = new XMLHttpRequest(); |   52 function createVideo() { | 
|   85 xhr2.open("GET", 'resources/shadow-offset.js'); |   53     return new Promise(function(resolve, reject) { | 
|   86 xhr2.responseType = 'blob'; |   54         var video = document.createElement('video'); | 
|   87 xhr2.send(); |   55         video.addEventListener('canplaythrough', resolve.bind(undefined, video),
      false); | 
|   88 xhr2.onload = function() { |   56         video.src = '../../compositing/resources/video.ogv'; | 
|   89     invalidBlob = xhr2.response; |   57     }); | 
|   90     invalidBlobLoaded = true; |  | 
|   91     loaded(); |  | 
|   92 } |   58 } | 
|   93  |   59  | 
|   94 var finishIfDoneCallsRemaining = 2; |   60 function createImage() { | 
|   95 function finishIfDone() { |   61     return createCanvas().then(function(canvas) { | 
|   96     finishIfDoneCallsRemaining--; |   62         var image = new Image(); | 
|   97     if (!finishIfDoneCallsRemaining) { |   63         var promise = new Promise(function(resolve, reject) { | 
|   98         // Because promise fulfillment is asynchonous, pass/fail messages |   64             image.onload = resolve.bind(undefined, image); | 
|   99         // must be handled here to ensure a deterministic message order. |   65         }); | 
|  100         if (invalidBlobTestPassed) { |   66         image.src = canvas.toDataURL(); | 
|  101             testPassed("createImageBitmap(invalidBlob) was rejected."); |   67         return promise; | 
|  102         } else { |   68     }); | 
|  103             testFailed("Invalid blob fulfilled."); |  | 
|  104         } |  | 
|  105         if (invalidCanvasTestPassed) { |  | 
|  106             testPassed("createImageBitmap(invalidCanvas) was rejected."); |  | 
|  107         } else { |  | 
|  108             testFailed("Invalid canvas fulfilled."); |  | 
|  109         } |  | 
|  110         finishJSTest(); |  | 
|  111     } |  | 
|  112 } |   69 } | 
|  113  |   70  | 
|  114 function loaded() { |   71 function createBlob(url) { | 
|  115     if (imageLoaded && videoLoaded && imageBitmapLoaded && blobLoaded && invalid
     BlobLoaded) { |   72     return new Promise(function(resolve, reject) { | 
|  116         shouldThrow("createImageBitmap(undefined)", "TypeError"); |   73         var xhr = new XMLHttpRequest(); | 
|  117         shouldThrow("createImageBitmap(null)", "TypeError"); |   74         xhr.open('GET', url); | 
 |   75         xhr.responseType = 'blob'; | 
 |   76         xhr.onload = function() { | 
 |   77             resolve(xhr.response); | 
 |   78         }; | 
 |   79         xhr.onerror = reject; | 
 |   80         xhr.send(); | 
 |   81     }); | 
 |   82 } | 
|  118  |   83  | 
|  119         shouldThrow("createImageBitmap(image, 0, 0, 10, 0)"); |   84 Promise.resolve().then(function() { | 
|  120         shouldThrow("createImageBitmap(image, 0, 0, 0, 10)"); |   85     return shouldBeRejected(createImageBitmap(undefined), 'undefined'); | 
 |   86 }).then(function() { | 
 |   87     return shouldBeRejected(createImageBitmap(null), 'null'); | 
 |   88 }).then(function() { | 
 |   89     return shouldBeRejected(createImageBitmap(new Image), 'empty image'); | 
 |   90 }).then(function() { | 
 |   91     return shouldBeRejected(createImageBitmap(document.createElement('video')), 
     'empty video'); | 
 |   92 }).then(function() { | 
 |   93     return createImage().then(function(image) { | 
 |   94         return checkInvalidRange(image, 'image'); | 
 |   95     }); | 
 |   96 }).then(function() { | 
 |   97     return createVideo().then(function(video) { | 
 |   98         return checkInvalidRange(video, 'video'); | 
 |   99     }); | 
 |  100 }).then(function() { | 
 |  101     return createCanvas().then(function(canvas) { | 
 |  102         return checkInvalidRange(canvas, 'canvas'); | 
 |  103     }); | 
 |  104 }).then(function() { | 
 |  105     return createCanvas().then(function(canvas) { | 
 |  106         return checkInvalidRange(canvas.getContext('2d'), 'canvas context'); | 
 |  107     }); | 
 |  108 }).then(function() { | 
 |  109     return createCanvas().then(function(canvas) { | 
 |  110         var imagedata = canvas.getContext('2d').getImageData(0, 0, canvas.width,
      canvas.height); | 
 |  111         return checkInvalidRange(imagedata, 'canvas imagedata') | 
 |  112     }); | 
 |  113 }).then(function() { | 
 |  114     return createImage().then(function(image) { | 
 |  115         return createImageBitmap(image); | 
 |  116     }).then(function(bitmap) { | 
 |  117         return checkInvalidRange(bitmap, 'image bitmap'); | 
 |  118     }); | 
 |  119 }).then(function() { | 
 |  120     return createBlob('resources/pattern.png').then(function(blob) { | 
 |  121         return checkInvalidRange(blob, 'blob'); | 
 |  122     }); | 
 |  123 }).then(function() { | 
 |  124     return createBlob('resources/shadow-offset.js').then(function(blob) { | 
 |  125         return shouldBeRejected(createImageBitmap(blob), 'invalid blob'); | 
 |  126     }); | 
 |  127 }).then(function() { | 
 |  128     return createInvalidCanvas().then(function(invalidCanvas) { | 
 |  129         return shouldBeRejected(createImageBitmap(invalidCanvas), 'invalid canva
     s'); | 
 |  130     }); | 
 |  131 }).catch(function(e) { | 
 |  132     testFailed('Unexpected rejection: ' + e); | 
 |  133 }).then(finishJSTest, finishJSTest); | 
|  121  |  134  | 
|  122         shouldThrow("createImageBitmap(video, 0, 0, 10, 0)"); |  | 
|  123         shouldThrow("createImageBitmap(video, 0, 0, 0, 10)"); |  | 
|  124  |  | 
|  125         shouldThrow("createImageBitmap(aCanvas, 0, 0, 10, 0)"); |  | 
|  126         shouldThrow("createImageBitmap(aCanvas, 0, 0, 0, 10)"); |  | 
|  127  |  | 
|  128         shouldThrow("createImageBitmap(d, 0, 0, 10, 0)"); |  | 
|  129         shouldThrow("createImageBitmap(d, 0, 0, 0, 10)"); |  | 
|  130  |  | 
|  131         shouldThrow("createImageBitmap(aCtx, 0, 0, 10, 0)"); |  | 
|  132         shouldThrow("createImageBitmap(aCtx, 0, 0, 0, 10)"); |  | 
|  133  |  | 
|  134         shouldThrow("createImageBitmap(testBitmap, 0, 0, 10, 0)"); |  | 
|  135         shouldThrow("createImageBitmap(testBitmap, 0, 0, 0, 10)"); |  | 
|  136  |  | 
|  137         shouldThrow("createImageBitmap(blob, 0, 0, 10, 0)"); |  | 
|  138         shouldThrow("createImageBitmap(blob, 0, 0, 0, 10)"); |  | 
|  139  |  | 
|  140         createImageBitmap(invalidBlob).then(function() { |  | 
|  141             finishIfDone(); |  | 
|  142         }, function() { |  | 
|  143             invalidBlobTestPassed = true; |  | 
|  144             finishIfDone(); |  | 
|  145         }); |  | 
|  146  |  | 
|  147         createImageBitmap(invalidCanvas).then(function() { |  | 
|  148             finishIfDone(); |  | 
|  149         }, function() { |  | 
|  150             invalidCanvasTestPassed = true; |  | 
|  151             finishIfDone(); |  | 
|  152         }); |  | 
|  153         } |  | 
|  154 } |  | 
|  155 </script> |  135 </script> | 
|  156 </body> |  136 </body> | 
|  157 </html> |  137 </html> | 
| OLD | NEW |