createImageBitmap-resolves-in-task.any.js (3960B)
1 // META: title=createImageBitmap resolves in a task 2 // META: script=/common/media.js 3 // META: script=./common.sub.js 4 function makeWorkerImageBitmap() { 5 return makeOffscreenCanvas().then(canvas => { 6 return createImageBitmap(canvas); 7 }); 8 } 9 var imageSourceTypes = self.GLOBAL.isWorker() ? 10 [ 11 { name: 'an OffscreenCanvas', factory: makeOffscreenCanvas }, 12 { name: 'an ImageData', factory: makeImageData }, 13 { name: 'an ImageBitmap', factory: makeWorkerImageBitmap }, 14 { name: 'a Blob', factory: makeBlob('/images/pattern.png') }, 15 ] : 16 imageSourceTypes; 17 18 var testFuncs = { 19 reject_sync: (promiseFunc, source, t) => { 20 return new Promise((resolve, reject) => { 21 let rejected = false; 22 promiseFunc(source).then(() => { 23 reject(new Error('Expected this call to reject')); 24 }, (err) => { 25 rejected = true; 26 }); 27 Promise.resolve().then(() => { 28 try { 29 assert_equals(rejected, true, 'The promise should be rejected synchronously') 30 resolve(t); 31 } catch (err) { 32 reject(err); 33 } 34 }) 35 }); 36 37 }, 38 resolve_async: (promiseFunc, source, t) => { 39 return new Promise((resolve, reject) => { 40 let taskRan = false; 41 promiseFunc(source).then(() => { 42 try { 43 assert_equals(taskRan, true, 'The promise should be resolved asynchronously') 44 resolve(t); 45 } catch (err) { 46 reject(err) 47 } 48 }, t.unreached_func('Expected this call to resolve')) 49 Promise.resolve().then(() => { 50 taskRan = true; 51 }); 52 }); 53 }, 54 reject_async: (promiseFunc, source, t) => { 55 return new Promise((resolve, reject) => { 56 let taskRan = false; 57 promiseFunc(source).then( 58 t.unreached_func('Expected this call to reject'), 59 () => { 60 try { 61 assert_equals(taskRan, true, 'The promise should be rejected asynchronously') 62 resolve(t); 63 } catch (err) { 64 reject(err) 65 } 66 }, 67 ); 68 Promise.resolve().then(() => { 69 taskRan = true; 70 }); 71 }); 72 }, 73 }; 74 75 var testCases = [{ 76 description: 'createImageBitmap with <sourceType> source and ' + 77 'invalid cropHeight', 78 promiseTestFunction: (source) => createImageBitmap(source, 0, 0, 0, 0), 79 resolution: 'reject_sync' 80 }, 81 { 82 description: 'createImageBitmap with <sourceType> source and ' + 83 'invalid resizeHeight', 84 promiseTestFunction: (source) => createImageBitmap(source, { 85 resizeWidth: 0, 86 resizeHeight: 0 87 }), 88 resolution: 'reject_sync' 89 }, 90 { 91 description: 'createImageBitmap with <sourceType> source', 92 promiseTestFunction: (source) => createImageBitmap(source), 93 resolution: 'resolve_async' 94 }, 95 ]; 96 97 imageSourceTypes.forEach(imageSourceType => { 98 testCases.forEach(testCase => { 99 let description = `${testCase.description.replace('<sourceType>', 100 imageSourceType.name)} should ${testCase.resolution.replace('_', ' ')}`; 101 102 promise_test(t => { 103 return imageSourceType.factory().then(source => { 104 const tester = testFuncs[testCase.resolution]; 105 return tester(testCase.promiseTestFunction, source, t); 106 }) 107 }, description); 108 109 }); 110 }); 111 promise_test(t => { 112 return makeBlob('data:,')().then(image => { 113 return testFuncs.reject_async((source) => createImageBitmap(source), image, t); 114 }); 115 }, 'Invalid Blob source should reject async');