1 <!DOCTYPE HTML PUBLIC
"-//IETF//DTD HTML//EN">
4 <script src=
"../../resources/js-test.js"></script>
8 description("Ensure correct behavior of createImageBitmap for invalid inputs.");
9 window
.jsTestIsAsync
= true;
13 function shouldBeRejected(promise
, message
) {
14 return promise
.then(function() {
15 testFailed('Resolved unexpectedly: ' + message
);
18 testPassed('Rejected as expected: ' + message
);
19 shouldBeTrue('reason instanceof Error');
24 function checkInvalidRange(source
, message
) {
25 return Promise
.resolve().then(function() {
26 return shouldBeRejected(createImageBitmap(source
, 0, 0, 10, 0), message
+ ' / invalid range');
28 return shouldBeRejected(createImageBitmap(source
, 0, 0, 0, 10), message
+ ' / invalid range');
32 function createCanvas() {
33 return new Promise(function(resolve
, reject
) {
34 var canvas
= document
.createElement('canvas');
35 canvas
.setAttribute('width', '200');
36 canvas
.setAttribute('height', '200');
41 function createInvalidCanvas() {
42 // Create a terapixel canvas to generate an invalid canvas through
44 return new Promise(function(resolve
, reject
) {
45 var canvas
= document
.createElement('canvas');
46 canvas
.setAttribute('width', '100000000');
47 canvas
.setAttribute('height', '100000000');
52 function createVideo() {
53 return new Promise(function(resolve
, reject
) {
54 var video
= document
.createElement('video');
55 video
.addEventListener('canplaythrough', resolve
.bind(undefined, video
), false);
56 video
.src
= '../../compositing/resources/video.ogv';
60 function createImage() {
61 return createCanvas().then(function(canvas
) {
62 var image
= new Image();
63 var promise
= new Promise(function(resolve
, reject
) {
64 image
.onload
= resolve
.bind(undefined, image
);
66 image
.src
= canvas
.toDataURL();
71 function createBlob(url
) {
72 return new Promise(function(resolve
, reject
) {
73 var xhr
= new XMLHttpRequest();
75 xhr
.responseType
= 'blob';
76 xhr
.onload = function() {
77 resolve(xhr
.response
);
84 Promise
.resolve().then(function() {
85 return shouldBeRejected(createImageBitmap(undefined), 'undefined');
87 return shouldBeRejected(createImageBitmap(null), 'null');
89 return shouldBeRejected(createImageBitmap(new Image
), 'empty image');
91 return shouldBeRejected(createImageBitmap(document
.createElement('video')), 'empty video');
93 return createImage().then(function(image
) {
94 return checkInvalidRange(image
, 'image');
97 return createVideo().then(function(video
) {
98 return checkInvalidRange(video
, 'video');
101 return createCanvas().then(function(canvas
) {
102 return checkInvalidRange(canvas
, 'canvas');
105 return createCanvas().then(function(canvas
) {
106 return checkInvalidRange(canvas
.getContext('2d'), 'canvas context');
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')
114 return createImage().then(function(image
) {
115 return createImageBitmap(image
);
116 }).then(function(bitmap
) {
117 return checkInvalidRange(bitmap
, 'image bitmap');
120 return createBlob('resources/pattern.png').then(function(blob
) {
121 return checkInvalidRange(blob
, 'blob');
124 return createBlob('resources/shadow-offset.js').then(function(blob
) {
125 return shouldBeRejected(createImageBitmap(blob
), 'invalid blob');
128 return createInvalidCanvas().then(function(invalidCanvas
) {
129 return shouldBeRejected(createImageBitmap(invalidCanvas
), 'invalid canvas');
131 }).catch(function(e
) {
132 testFailed('Unexpected rejection: ' + e
);
133 }).then(finishJSTest
, finishJSTest
);