Best JavaScript code snippet using wpt
image-decoder.any.js
Source:image-decoder.any.js
2// META: script=/webcodecs/image-decoder-utils.js3function testFourColorsDecode(filename, mimeType, options = {}) {4 var decoder = null;5 return ImageDecoder.isTypeSupported(mimeType).then(support => {6 assert_implements_optional(7 support, 'Optional codec ' + mimeType + ' not supported.');8 return fetch(filename).then(response => {9 return testFourColorsDecodeBuffer(response.body, mimeType, options);10 });11 });12}13// Note: Requiring all data to do YUV decoding is a Chromium limitation, other14// implementations may support YUV decode with partial ReadableStream data.15function testFourColorsYuvDecode(filename, mimeType, options = {}) {16 var decoder = null;17 return ImageDecoder.isTypeSupported(mimeType).then(support => {18 assert_implements_optional(19 support, 'Optional codec ' + mimeType + ' not supported.');20 return fetch(filename).then(21 response => {return response.arrayBuffer().then(buffer => {22 return testFourColorsDecodeBuffer(buffer, mimeType, options);23 })});24 });25}26function testFourColorsDecodeBuffer(buffer, mimeType, options = {}) {27 var decoder = new ImageDecoder(28 {data: buffer, type: mimeType, preferAnimation: options.preferAnimation});29 return decoder.decode().then(result => {30 assert_equals(result.image.displayWidth, 320);31 assert_equals(result.image.displayHeight, 240);32 if (options.preferAnimation !== undefined) {33 assert_greater_than(decoder.tracks.length, 1);34 assert_equals(35 options.preferAnimation, decoder.tracks.selectedTrack.animated);36 }37 if (options.yuvFormat !== undefined)38 assert_equals(result.image.format, options.yuvFormat);39 if (options.tolerance === undefined)40 options.tolerance = 0;41 let canvas = new OffscreenCanvas(42 result.image.displayWidth, result.image.displayHeight);43 let ctx = canvas.getContext('2d');44 ctx.drawImage(result.image, 0, 0);45 let top_left = ctx.getImageData(0, 0, 1, 1);46 let top_right = ctx.getImageData(result.image.displayWidth - 1, 0, 1, 1);47 let bottom_left = ctx.getImageData(0, result.image.displayHeight - 1, 1, 1);48 let left_corner = ctx.getImageData(49 result.image.displayWidth - 1, result.image.displayHeight - 1, 1, 1);50 assert_array_approx_equals(51 top_left.data, [0xFF, 0xFF, 0x00, 0xFF], options.tolerance,52 'top left corner is yellow');53 assert_array_approx_equals(54 top_right.data, [0xFF, 0x00, 0x00, 0xFF], options.tolerance,55 'top right corner is red');56 assert_array_approx_equals(57 bottom_left.data, [0x00, 0x00, 0xFF, 0xFF], options.tolerance,58 'bottom left corner is blue');59 assert_array_approx_equals(60 left_corner.data, [0x00, 0xFF, 0x00, 0xFF], options.tolerance,61 'bottom right corner is green');62 });63}64promise_test(t => {65 return testFourColorsDecode('four-colors.jpg', 'image/jpeg');66}, 'Test JPEG image decoding.');67promise_test(t => {68 return testFourColorDecodeWithExifOrientation(1);69}, 'Test JPEG w/ EXIF orientation top-left.');70promise_test(t => {71 return testFourColorDecodeWithExifOrientation(2);72}, 'Test JPEG w/ EXIF orientation top-right.');73promise_test(t => {74 return testFourColorDecodeWithExifOrientation(3);75}, 'Test JPEG w/ EXIF orientation bottom-right.');76promise_test(t => {77 return testFourColorDecodeWithExifOrientation(4);78}, 'Test JPEG w/ EXIF orientation bottom-left.');79promise_test(t => {80 return testFourColorDecodeWithExifOrientation(5);81}, 'Test JPEG w/ EXIF orientation left-top.');82promise_test(t => {83 return testFourColorDecodeWithExifOrientation(6);84}, 'Test JPEG w/ EXIF orientation right-top.');85promise_test(t => {86 return testFourColorDecodeWithExifOrientation(7);87}, 'Test JPEG w/ EXIF orientation right-bottom.');88promise_test(t => {89 return testFourColorDecodeWithExifOrientation(8);90}, 'Test JPEG w/ EXIF orientation left-bottom.');91promise_test(t => {92 return testFourColorsDecode('four-colors.png', 'image/png');93}, 'Test PNG image decoding.');94promise_test(t => {95 return testFourColorsDecode('four-colors.avif', 'image/avif');96}, 'Test AVIF image decoding.');97promise_test(t => {98 return testFourColorsDecode(99 'four-colors-full-range-bt2020-pq-444-10bpc.avif', 'image/avif');100}, 'Test high bit depth HDR AVIF image decoding.');101promise_test(t => {102 return testFourColorsDecode(103 'four-colors-flip.avif', 'image/avif', {preferAnimation: false});104}, 'Test multi-track AVIF image decoding w/ preferAnimation=false.');105promise_test(t => {106 return testFourColorsDecode(107 'four-colors-flip.avif', 'image/avif', {preferAnimation: true});108}, 'Test multi-track AVIF image decoding w/ preferAnimation=true.');109promise_test(t => {110 return testFourColorsDecode('four-colors.webp', 'image/webp');111}, 'Test WEBP image decoding.');112promise_test(t => {113 return testFourColorsDecode('four-colors.gif', 'image/gif');114}, 'Test GIF image decoding.');115promise_test(t => {116 return testFourColorsYuvDecode(117 'four-colors-limited-range-420-8bpc.jpg', 'image/jpeg',118 {yuvFormat: 'I420', tolerance: 1});119}, 'Test JPEG image YUV 4:2:0 decoding.');120promise_test(t => {121 return testFourColorsYuvDecode(122 'four-colors-limited-range-420-8bpc.avif', 'image/avif',123 {yuvFormat: 'I420', tolerance: 1});124}, 'Test AVIF image YUV 4:2:0 decoding.');125promise_test(t => {126 return testFourColorsYuvDecode(127 'four-colors-limited-range-422-8bpc.avif', 'image/avif',128 {yuvFormat: 'I422', tolerance: 1});129}, 'Test AVIF image YUV 4:2:2 decoding.');130promise_test(t => {131 return testFourColorsYuvDecode(132 'four-colors-limited-range-444-8bpc.avif', 'image/avif',133 {yuvFormat: 'I444', tolerance: 1});134}, 'Test AVIF image YUV 4:4:4 decoding.');135promise_test(t => {136 return testFourColorsYuvDecode(137 'four-colors-limited-range-420-8bpc.webp', 'image/webp',138 {yuvFormat: 'I420', tolerance: 1});139}, 'Test WEBP image YUV 4:2:0 decoding.');140promise_test(t => {141 return fetch('four-colors.png').then(response => {142 let decoder = new ImageDecoder({data: response.body, type: 'junk/type'});143 return promise_rejects_dom(t, 'NotSupportedError', decoder.decode());144 });145}, 'Test invalid mime type rejects decode() requests');146promise_test(t => {147 return fetch('four-colors.png').then(response => {148 let decoder = new ImageDecoder({data: response.body, type: 'junk/type'});149 return promise_rejects_dom(t, 'NotSupportedError', decoder.tracks.ready);150 });151}, 'Test invalid mime type rejects decodeMetadata() requests');152promise_test(t => {153 return ImageDecoder.isTypeSupported('image/png').then(support => {154 assert_implements_optional(155 support, 'Optional codec image/png not supported.');156 return fetch('four-colors.png')157 .then(response => {158 return response.arrayBuffer();159 })160 .then(buffer => {161 let decoder = new ImageDecoder({data: buffer, type: 'image/png'});162 return promise_rejects_js(163 t, RangeError, decoder.decode({frameIndex: 1}));164 });165 });166}, 'Test out of range index returns RangeError');167promise_test(t => {168 var decoder;169 var p1;170 return ImageDecoder.isTypeSupported('image/png').then(support => {171 assert_implements_optional(172 support, 'Optional codec image/png not supported.');173 return fetch('four-colors.png')174 .then(response => {175 return response.arrayBuffer();176 })177 .then(buffer => {178 decoder =179 new ImageDecoder({data: buffer.slice(0, 100), type: 'image/png'});180 return decoder.tracks.ready;181 })182 .then(_ => {183 // Queue two decodes to ensure index verification and decoding are184 // properly ordered.185 p1 = decoder.decode({frameIndex: 0});186 return promise_rejects_js(187 t, RangeError, decoder.decode({frameIndex: 1}));188 })189 .then(_ => {190 return promise_rejects_js(t, RangeError, p1);191 })192 });193}, 'Test partial decoding without a frame results in an error');194promise_test(t => {195 var decoder;196 var p1;197 return ImageDecoder.isTypeSupported('image/png').then(support => {198 assert_implements_optional(199 support, 'Optional codec image/png not supported.');200 return fetch('four-colors.png')201 .then(response => {202 return response.arrayBuffer();203 })204 .then(buffer => {205 decoder =206 new ImageDecoder({data: buffer.slice(0, 100), type: 'image/png'});207 return decoder.completed;208 })209 });210}, 'Test completed property on fully buffered decode');211promise_test(t => {212 var decoder = null;213 return ImageDecoder.isTypeSupported('image/png').then(support => {214 assert_implements_optional(215 support, 'Optional codec image/png not supported.');216 return fetch('four-colors.png')217 .then(response => {218 decoder = new ImageDecoder({data: response.body, type: 'image/png'});219 return decoder.tracks.ready;220 })221 .then(_ => {222 decoder.tracks.selectedTrack.selected = false;223 assert_equals(decoder.tracks.selectedIndex, -1);224 assert_equals(decoder.tracks.selectedTrack, null);225 return decoder.tracks.ready;226 })227 .then(_ => {228 return promise_rejects_dom(t, 'InvalidStateError', decoder.decode());229 })230 .then(_ => {231 decoder.tracks[0].selected = true;232 assert_equals(decoder.tracks.selectedIndex, 0);233 assert_not_equals(decoder.tracks.selected, null);234 return decoder.decode();235 })236 .then(result => {237 assert_equals(result.image.displayWidth, 320);238 assert_equals(result.image.displayHeight, 240);239 });240 });241}, 'Test decode, decodeMetadata after no track selected.');242promise_test(t => {243 var decoder = null;244 return ImageDecoder.isTypeSupported('image/avif').then(support => {245 assert_implements_optional(246 support, 'Optional codec image/avif not supported.');247 return fetch('four-colors-flip.avif')248 .then(response => {249 decoder = new ImageDecoder({250 data: response.body,251 type: 'image/avif',252 preferAnimation: false253 });254 return decoder.tracks.ready;255 })256 .then(_ => {257 assert_equals(decoder.tracks.length, 2);258 assert_false(decoder.tracks[decoder.tracks.selectedIndex].animated)259 assert_false(decoder.tracks.selectedTrack.animated);260 assert_equals(decoder.tracks.selectedTrack.frameCount, 1);261 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 0);262 return decoder.decode();263 })264 .then(result => {265 assert_equals(result.image.displayWidth, 320);266 assert_equals(result.image.displayHeight, 240);267 // Swap to the the other track.268 let newIndex = (decoder.tracks.selectedIndex + 1) % 2;269 decoder.tracks[newIndex].selected = true;270 return decoder.decode()271 })272 .then(result => {273 assert_equals(result.image.displayWidth, 320);274 assert_equals(result.image.displayHeight, 240);275 assert_equals(decoder.tracks.length, 2);276 assert_true(decoder.tracks[decoder.tracks.selectedIndex].animated)277 assert_true(decoder.tracks.selectedTrack.animated);278 assert_equals(decoder.tracks.selectedTrack.frameCount, 7);279 assert_equals(decoder.tracks.selectedTrack.repetitionCount, Infinity);280 });281 });282}, 'Test track selection in multi track image.');283class InfiniteGifSource {284 async load(repetitionCount) {285 let response = await fetch('four-colors-flip.gif');286 let buffer = await response.arrayBuffer();287 // Strip GIF trailer (0x3B) so we can continue to append frames.288 this.baseImage = new Uint8Array(buffer.slice(0, buffer.byteLength - 1));289 this.baseImage[0x23] = repetitionCount;290 this.counter = 0;291 }292 start(controller) {293 this.controller = controller;294 this.controller.enqueue(this.baseImage);295 }296 close() {297 this.controller.enqueue(new Uint8Array([0x3B]));298 this.controller.close();299 }300 addFrame() {301 const FRAME1_START = 0x26;302 const FRAME2_START = 0x553;303 if (this.counter++ % 2 == 0)304 this.controller.enqueue(this.baseImage.slice(FRAME1_START, FRAME2_START));305 else306 this.controller.enqueue(this.baseImage.slice(FRAME2_START));307 }308}309promise_test(async t => {310 let support = await ImageDecoder.isTypeSupported('image/gif');311 assert_implements_optional(312 support, 'Optional codec image/gif not supported.');313 let source = new InfiniteGifSource();314 await source.load(5);315 let stream = new ReadableStream(source, {type: 'bytes'});316 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});317 return decoder.tracks.ready318 .then(_ => {319 assert_equals(decoder.tracks.selectedTrack.frameCount, 2);320 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 5);321 source.addFrame();322 return decoder.decode({frameIndex: 2});323 })324 .then(result => {325 assert_equals(decoder.tracks.selectedTrack.frameCount, 3);326 assert_equals(result.image.displayWidth, 320);327 assert_equals(result.image.displayHeight, 240);328 source.addFrame();329 return decoder.decode({frameIndex: 3});330 })331 .then(result => {332 assert_equals(decoder.tracks.selectedTrack.frameCount, 4);333 assert_equals(result.image.displayWidth, 320);334 assert_equals(result.image.displayHeight, 240);335 // Decode frame not yet available then reset before it comes in.336 let p = decoder.decode({frameIndex: 5});337 decoder.reset();338 return promise_rejects_dom(t, 'AbortError', p);339 })340 .then(_ => {341 // Ensure we can still decode earlier frames.342 assert_equals(decoder.tracks.selectedTrack.frameCount, 4);343 return decoder.decode({frameIndex: 3});344 })345 .then(result => {346 assert_equals(decoder.tracks.selectedTrack.frameCount, 4);347 assert_equals(result.image.displayWidth, 320);348 assert_equals(result.image.displayHeight, 240);349 // Decode frame not yet available then close before it comes in.350 let p = decoder.decode({frameIndex: 5});351 let tracks = decoder.tracks;352 let track = decoder.tracks.selectedTrack;353 decoder.close();354 assert_equals(decoder.type, '');355 assert_equals(decoder.tracks.length, 0);356 assert_equals(tracks.length, 0);357 track.selected = true; // Should do nothing.358 // Previous decode should be aborted.359 return promise_rejects_dom(t, 'AbortError', p);360 })361 .then(_ => {362 // Ensure feeding the source after closing doesn't crash.363 source.addFrame();364 return promise_rejects_dom(t, 'InvalidStateError', decoder.decode());365 });366}, 'Test ReadableStream of gif');367promise_test(async t => {368 let support = await ImageDecoder.isTypeSupported('image/gif');369 assert_implements_optional(370 support, 'Optional codec image/gif not supported.');371 let source = new InfiniteGifSource();372 await source.load(5);373 let stream = new ReadableStream(source, {type: 'bytes'});374 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});375 return decoder.tracks.ready.then(_ => {376 assert_equals(decoder.tracks.selectedTrack.frameCount, 2);377 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 5);378 decoder.decode({frameIndex: 2}).then(t.unreached_func());379 decoder.decode({frameIndex: 1}).then(t.unreached_func());380 return decoder.tracks.ready;381 });382}, 'Test that decode requests are serialized.');383promise_test(async t => {384 let support = await ImageDecoder.isTypeSupported('image/gif');385 assert_implements_optional(386 support, 'Optional codec image/gif not supported.');387 let source = new InfiniteGifSource();388 await source.load(5);389 let stream = new ReadableStream(source, {type: 'bytes'});390 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});391 return decoder.tracks.ready.then(_ => {392 assert_equals(decoder.tracks.selectedTrack.frameCount, 2);393 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 5);394 // Decode frame not yet available then change tracks before it comes in.395 let p = decoder.decode({frameIndex: 5});396 decoder.tracks.selectedTrack.selected = false;397 return promise_rejects_dom(t, 'AbortError', p);398 });399}, 'Test ReadableStream aborts promises on track change');400promise_test(async t => {401 let support = await ImageDecoder.isTypeSupported('image/gif');402 assert_implements_optional(403 support, 'Optional codec image/gif not supported.');404 let source = new InfiniteGifSource();405 await source.load(5);406 let stream = new ReadableStream(source, {type: 'bytes'});407 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});408 return decoder.tracks.ready.then(_ => {409 let p = decoder.completed;410 decoder.close();411 return promise_rejects_dom(t, 'AbortError', p);412 });413}, 'Test ReadableStream aborts completed on close');414promise_test(async t => {415 let support = await ImageDecoder.isTypeSupported('image/gif');416 assert_implements_optional(417 support, 'Optional codec image/gif not supported.');418 let source = new InfiniteGifSource();419 await source.load(5);420 let stream = new ReadableStream(source, {type: 'bytes'});421 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});422 return decoder.tracks.ready.then(_ => {423 source.close();424 return decoder.completed;425 });...
image-decoder.https.any.js
Source:image-decoder.https.any.js
2// META: script=/webcodecs/image-decoder-utils.js3function testFourColorsDecode(filename, mimeType, options = {}) {4 var decoder = null;5 return ImageDecoder.isTypeSupported(mimeType).then(support => {6 assert_implements_optional(7 support, 'Optional codec ' + mimeType + ' not supported.');8 return fetch(filename).then(response => {9 return testFourColorsDecodeBuffer(response.body, mimeType, options);10 });11 });12}13// Note: Requiring all data to do YUV decoding is a Chromium limitation, other14// implementations may support YUV decode with partial ReadableStream data.15function testFourColorsYuvDecode(filename, mimeType, options = {}) {16 var decoder = null;17 return ImageDecoder.isTypeSupported(mimeType).then(support => {18 assert_implements_optional(19 support, 'Optional codec ' + mimeType + ' not supported.');20 return fetch(filename).then(response => {21 return response.arrayBuffer().then(buffer => {22 return testFourColorsDecodeBuffer(buffer, mimeType, options);23 });24 });25 });26}27promise_test(t => {28 return testFourColorsDecode('four-colors.jpg', 'image/jpeg');29}, 'Test JPEG image decoding.');30promise_test(t => {31 return testFourColorDecodeWithExifOrientation(1);32}, 'Test JPEG w/ EXIF orientation top-left.');33promise_test(t => {34 return testFourColorDecodeWithExifOrientation(2);35}, 'Test JPEG w/ EXIF orientation top-right.');36promise_test(t => {37 return testFourColorDecodeWithExifOrientation(3);38}, 'Test JPEG w/ EXIF orientation bottom-right.');39promise_test(t => {40 return testFourColorDecodeWithExifOrientation(4);41}, 'Test JPEG w/ EXIF orientation bottom-left.');42promise_test(t => {43 return testFourColorDecodeWithExifOrientation(5);44}, 'Test JPEG w/ EXIF orientation left-top.');45promise_test(t => {46 return testFourColorDecodeWithExifOrientation(6);47}, 'Test JPEG w/ EXIF orientation right-top.');48promise_test(t => {49 return testFourColorDecodeWithExifOrientation(7);50}, 'Test JPEG w/ EXIF orientation right-bottom.');51promise_test(t => {52 return testFourColorDecodeWithExifOrientation(8);53}, 'Test JPEG w/ EXIF orientation left-bottom.');54promise_test(t => {55 return testFourColorsDecode('four-colors.png', 'image/png');56}, 'Test PNG image decoding.');57promise_test(t => {58 return testFourColorsDecode('four-colors.avif', 'image/avif');59}, 'Test AVIF image decoding.');60promise_test(t => {61 return testFourColorsDecode(62 'four-colors-full-range-bt2020-pq-444-10bpc.avif', 'image/avif',63 { tolerance: 3 });64}, 'Test high bit depth HDR AVIF image decoding.');65promise_test(t => {66 return testFourColorsDecode(67 'four-colors-flip.avif', 'image/avif', {preferAnimation: false});68}, 'Test multi-track AVIF image decoding w/ preferAnimation=false.');69promise_test(t => {70 return testFourColorsDecode(71 'four-colors-flip.avif', 'image/avif', {preferAnimation: true});72}, 'Test multi-track AVIF image decoding w/ preferAnimation=true.');73promise_test(t => {74 return testFourColorsDecode('four-colors.webp', 'image/webp');75}, 'Test WEBP image decoding.');76promise_test(t => {77 return testFourColorsDecode('four-colors.gif', 'image/gif');78}, 'Test GIF image decoding.');79promise_test(t => {80 return testFourColorsYuvDecode(81 'four-colors-limited-range-420-8bpc.jpg', 'image/jpeg',82 {yuvFormat: 'I420', tolerance: 3});83}, 'Test JPEG image YUV 4:2:0 decoding.');84promise_test(t => {85 return testFourColorsYuvDecode(86 'four-colors-limited-range-420-8bpc.avif', 'image/avif',87 {yuvFormat: 'I420', tolerance: 3});88}, 'Test AVIF image YUV 4:2:0 decoding.');89promise_test(t => {90 return testFourColorsYuvDecode(91 'four-colors-limited-range-422-8bpc.avif', 'image/avif',92 {yuvFormat: 'I422', tolerance: 3});93}, 'Test AVIF image YUV 4:2:2 decoding.');94promise_test(t => {95 return testFourColorsYuvDecode(96 'four-colors-limited-range-444-8bpc.avif', 'image/avif',97 {yuvFormat: 'I444', tolerance: 3});98}, 'Test AVIF image YUV 4:4:4 decoding.');99promise_test(t => {100 return testFourColorsYuvDecode(101 'four-colors-limited-range-420-8bpc.webp', 'image/webp',102 {yuvFormat: 'I420', tolerance: 3});103}, 'Test WEBP image YUV 4:2:0 decoding.');104promise_test(t => {105 return fetch('four-colors.png').then(response => {106 let decoder = new ImageDecoder({data: response.body, type: 'junk/type'});107 return promise_rejects_dom(t, 'NotSupportedError', decoder.decode());108 });109}, 'Test invalid mime type rejects decode() requests');110promise_test(t => {111 return fetch('four-colors.png').then(response => {112 let decoder = new ImageDecoder({data: response.body, type: 'junk/type'});113 return promise_rejects_dom(t, 'NotSupportedError', decoder.tracks.ready);114 });115}, 'Test invalid mime type rejects decodeMetadata() requests');116promise_test(t => {117 return ImageDecoder.isTypeSupported('image/png').then(support => {118 assert_implements_optional(119 support, 'Optional codec image/png not supported.');120 return fetch('four-colors.png')121 .then(response => {122 return response.arrayBuffer();123 })124 .then(buffer => {125 let decoder = new ImageDecoder({data: buffer, type: 'image/png'});126 return promise_rejects_js(127 t, RangeError, decoder.decode({frameIndex: 1}));128 });129 });130}, 'Test out of range index returns RangeError');131promise_test(t => {132 var decoder;133 var p1;134 return ImageDecoder.isTypeSupported('image/png').then(support => {135 assert_implements_optional(136 support, 'Optional codec image/png not supported.');137 return fetch('four-colors.png')138 .then(response => {139 return response.arrayBuffer();140 })141 .then(buffer => {142 decoder =143 new ImageDecoder({data: buffer.slice(0, 100), type: 'image/png'});144 return decoder.tracks.ready;145 })146 .then(_ => {147 // Queue two decodes to ensure index verification and decoding are148 // properly ordered.149 p1 = decoder.decode({frameIndex: 0});150 return promise_rejects_js(151 t, RangeError, decoder.decode({frameIndex: 1}));152 })153 .then(_ => {154 return promise_rejects_js(t, RangeError, p1);155 })156 });157}, 'Test partial decoding without a frame results in an error');158promise_test(t => {159 var decoder;160 var p1;161 return ImageDecoder.isTypeSupported('image/png').then(support => {162 assert_implements_optional(163 support, 'Optional codec image/png not supported.');164 return fetch('four-colors.png')165 .then(response => {166 return response.arrayBuffer();167 })168 .then(buffer => {169 decoder =170 new ImageDecoder({data: buffer.slice(0, 100), type: 'image/png'});171 return decoder.completed;172 })173 });174}, 'Test completed property on fully buffered decode');175promise_test(t => {176 var decoder = null;177 return ImageDecoder.isTypeSupported('image/png').then(support => {178 assert_implements_optional(179 support, 'Optional codec image/png not supported.');180 return fetch('four-colors.png')181 .then(response => {182 decoder = new ImageDecoder({data: response.body, type: 'image/png'});183 return decoder.tracks.ready;184 })185 .then(_ => {186 decoder.tracks.selectedTrack.selected = false;187 assert_equals(decoder.tracks.selectedIndex, -1);188 assert_equals(decoder.tracks.selectedTrack, null);189 return decoder.tracks.ready;190 })191 .then(_ => {192 return promise_rejects_dom(t, 'InvalidStateError', decoder.decode());193 })194 .then(_ => {195 decoder.tracks[0].selected = true;196 assert_equals(decoder.tracks.selectedIndex, 0);197 assert_not_equals(decoder.tracks.selected, null);198 return decoder.decode();199 })200 .then(result => {201 assert_equals(result.image.displayWidth, 320);202 assert_equals(result.image.displayHeight, 240);203 });204 });205}, 'Test decode, decodeMetadata after no track selected.');206promise_test(t => {207 var decoder = null;208 return ImageDecoder.isTypeSupported('image/avif').then(support => {209 assert_implements_optional(210 support, 'Optional codec image/avif not supported.');211 return fetch('four-colors-flip.avif')212 .then(response => {213 decoder = new ImageDecoder({214 data: response.body,215 type: 'image/avif',216 preferAnimation: false217 });218 return decoder.tracks.ready;219 })220 .then(_ => {221 assert_equals(decoder.tracks.length, 2);222 assert_false(decoder.tracks[decoder.tracks.selectedIndex].animated)223 assert_false(decoder.tracks.selectedTrack.animated);224 assert_equals(decoder.tracks.selectedTrack.frameCount, 1);225 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 0);226 return decoder.decode();227 })228 .then(result => {229 assert_equals(result.image.displayWidth, 320);230 assert_equals(result.image.displayHeight, 240);231 // Swap to the the other track.232 let newIndex = (decoder.tracks.selectedIndex + 1) % 2;233 decoder.tracks[newIndex].selected = true;234 return decoder.decode()235 })236 .then(result => {237 assert_equals(result.image.displayWidth, 320);238 assert_equals(result.image.displayHeight, 240);239 assert_equals(decoder.tracks.length, 2);240 assert_true(decoder.tracks[decoder.tracks.selectedIndex].animated)241 assert_true(decoder.tracks.selectedTrack.animated);242 assert_equals(decoder.tracks.selectedTrack.frameCount, 7);243 assert_equals(decoder.tracks.selectedTrack.repetitionCount, Infinity);244 });245 });246}, 'Test track selection in multi track image.');247class InfiniteGifSource {248 async load(repetitionCount) {249 let response = await fetch('four-colors-flip.gif');250 let buffer = await response.arrayBuffer();251 // Strip GIF trailer (0x3B) so we can continue to append frames.252 this.baseImage = new Uint8Array(buffer.slice(0, buffer.byteLength - 1));253 this.baseImage[0x23] = repetitionCount;254 this.counter = 0;255 }256 start(controller) {257 this.controller = controller;258 this.controller.enqueue(this.baseImage);259 }260 close() {261 this.controller.enqueue(new Uint8Array([0x3B]));262 this.controller.close();263 }264 addFrame() {265 const FRAME1_START = 0x26;266 const FRAME2_START = 0x553;267 if (this.counter++ % 2 == 0)268 this.controller.enqueue(this.baseImage.slice(FRAME1_START, FRAME2_START));269 else270 this.controller.enqueue(this.baseImage.slice(FRAME2_START));271 }272}273promise_test(async t => {274 let support = await ImageDecoder.isTypeSupported('image/gif');275 assert_implements_optional(276 support, 'Optional codec image/gif not supported.');277 let source = new InfiniteGifSource();278 await source.load(5);279 let stream = new ReadableStream(source, {type: 'bytes'});280 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});281 return decoder.tracks.ready282 .then(_ => {283 assert_equals(decoder.tracks.selectedTrack.frameCount, 2);284 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 5);285 source.addFrame();286 return decoder.decode({frameIndex: 2});287 })288 .then(result => {289 assert_equals(decoder.tracks.selectedTrack.frameCount, 3);290 assert_equals(result.image.displayWidth, 320);291 assert_equals(result.image.displayHeight, 240);292 source.addFrame();293 return decoder.decode({frameIndex: 3});294 })295 .then(result => {296 assert_equals(decoder.tracks.selectedTrack.frameCount, 4);297 assert_equals(result.image.displayWidth, 320);298 assert_equals(result.image.displayHeight, 240);299 // Decode frame not yet available then reset before it comes in.300 let p = decoder.decode({frameIndex: 5});301 decoder.reset();302 return promise_rejects_dom(t, 'AbortError', p);303 })304 .then(_ => {305 // Ensure we can still decode earlier frames.306 assert_equals(decoder.tracks.selectedTrack.frameCount, 4);307 return decoder.decode({frameIndex: 3});308 })309 .then(result => {310 assert_equals(decoder.tracks.selectedTrack.frameCount, 4);311 assert_equals(result.image.displayWidth, 320);312 assert_equals(result.image.displayHeight, 240);313 // Decode frame not yet available then close before it comes in.314 let p = decoder.decode({frameIndex: 5});315 let tracks = decoder.tracks;316 let track = decoder.tracks.selectedTrack;317 decoder.close();318 assert_equals(decoder.type, '');319 assert_equals(decoder.tracks.length, 0);320 assert_equals(tracks.length, 0);321 track.selected = true; // Should do nothing.322 // Previous decode should be aborted.323 return promise_rejects_dom(t, 'AbortError', p);324 })325 .then(_ => {326 // Ensure feeding the source after closing doesn't crash.327 source.addFrame();328 return promise_rejects_dom(t, 'InvalidStateError', decoder.decode());329 });330}, 'Test ReadableStream of gif');331promise_test(async t => {332 let support = await ImageDecoder.isTypeSupported('image/gif');333 assert_implements_optional(334 support, 'Optional codec image/gif not supported.');335 let source = new InfiniteGifSource();336 await source.load(5);337 let stream = new ReadableStream(source, {type: 'bytes'});338 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});339 return decoder.tracks.ready.then(_ => {340 assert_equals(decoder.tracks.selectedTrack.frameCount, 2);341 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 5);342 decoder.decode({frameIndex: 2}).then(t.unreached_func());343 decoder.decode({frameIndex: 1}).then(t.unreached_func());344 return decoder.tracks.ready;345 });346}, 'Test that decode requests are serialized.');347promise_test(async t => {348 let support = await ImageDecoder.isTypeSupported('image/gif');349 assert_implements_optional(350 support, 'Optional codec image/gif not supported.');351 let source = new InfiniteGifSource();352 await source.load(5);353 let stream = new ReadableStream(source, {type: 'bytes'});354 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});355 return decoder.tracks.ready.then(_ => {356 assert_equals(decoder.tracks.selectedTrack.frameCount, 2);357 assert_equals(decoder.tracks.selectedTrack.repetitionCount, 5);358 // Decode frame not yet available then change tracks before it comes in.359 let p = decoder.decode({frameIndex: 5});360 decoder.tracks.selectedTrack.selected = false;361 return promise_rejects_dom(t, 'AbortError', p);362 });363}, 'Test ReadableStream aborts promises on track change');364promise_test(async t => {365 let support = await ImageDecoder.isTypeSupported('image/gif');366 assert_implements_optional(367 support, 'Optional codec image/gif not supported.');368 let source = new InfiniteGifSource();369 await source.load(5);370 let stream = new ReadableStream(source, {type: 'bytes'});371 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});372 return decoder.tracks.ready.then(_ => {373 let p = decoder.completed;374 decoder.close();375 return promise_rejects_dom(t, 'AbortError', p);376 });377}, 'Test ReadableStream aborts completed on close');378promise_test(async t => {379 let support = await ImageDecoder.isTypeSupported('image/gif');380 assert_implements_optional(381 support, 'Optional codec image/gif not supported.');382 let source = new InfiniteGifSource();383 await source.load(5);384 let stream = new ReadableStream(source, {type: 'bytes'});385 let decoder = new ImageDecoder({data: stream, type: 'image/gif'});386 return decoder.tracks.ready.then(_ => {387 source.close();388 return decoder.completed;389 });...
Using AI Code Generation
1const wpt = require('wpt');2const assert_implements_optional = wpt.assert_implements_optional;3const assert_implements = wpt.assert_implements;4const assert_equals = wpt.assert_equals;5const assert_true = wpt.assert_true;6const assert_false = wpt.assert_false;7const assert_throws = wpt.assert_throws;8const assert_array_equals = wpt.assert_array_equals;9const assert_class_string = wpt.assert_class_string;10const assert_own_property = wpt.assert_own_property;11const assert_readonly = wpt.assert_readonly;12const assert_unreached = wpt.assert_unreached;13const wpt = require('wpt');14const assert_implements_optional = wpt.assert_implements_optional;15const assert_implements = wpt.assert_implements;16const assert_equals = wpt.assert_equals;17const assert_true = wpt.assert_true;18const assert_false = wpt.assert_false;19const assert_throws = wpt.assert_throws;20const assert_array_equals = wpt.assert_array_equals;21const assert_class_string = wpt.assert_class_string;22const assert_own_property = wpt.assert_own_property;23const assert_readonly = wpt.assert_readonly;24const assert_unreached = wpt.assert_unreached;25const wpt = require('wpt');26const assert_implements_optional = wpt.assert_implements_optional;27const assert_implements = wpt.assert_implements;28const assert_equals = wpt.assert_equals;29const assert_true = wpt.assert_true;30const assert_false = wpt.assert_false;31const assert_throws = wpt.assert_throws;32const assert_array_equals = wpt.assert_array_equals;33const assert_class_string = wpt.assert_class_string;34const assert_own_property = wpt.assert_own_property;35const assert_readonly = wpt.assert_readonly;36const assert_unreached = wpt.assert_unreached;37const wpt = require('wpt');38const assert_implements_optional = wpt.assert_implements_optional;39const assert_implements = wpt.assert_implements;40const assert_equals = wpt.assert_equals;41const assert_true = wpt.assert_true;42const assert_false = wpt.assert_false;43const assert_throws = wpt.assert_throws;
Using AI Code Generation
1assert_implements_optional = function (object, method) {2 if (object[method] === undefined) {3 assert_unreached("Method " + method + " is not implemented.");4 }5}6assert_implements = function (object, method) {7 if (object[method] === undefined) {8 assert_unreached("Method " + method + " is not implemented.");9 }10}11assert_equals = function (actual, expected, description) {12 if (actual != expected) {13 assert_unreached("Expected: " + expected + " but got: " + actual);14 }15}16assert_throws = function (expected, func, description) {17 try {18 func();19 } catch (e) {20 if (e.name != expected) {21 assert_unreached("Expected: " + expected + " but got: " + e.name);22 }23 }24}25assert_true = function (condition, description) {26 if (!condition) {27 assert_unreached("Expected: " + condition + " to be true");28 }29}30assert_false = function (condition, description) {31 if (condition) {32 assert_unreached("Expected: " + condition + " to be false");33 }34}35assert_array_equals = function (actual, expected, description) {36 if (actual.length != expected.length) {37 assert_unreached("Expected: " + expected + " but got: " + actual);38 }39}40assert_unreached = function (message) {41 throw new Error(message);42}43assert_class_string = function (actual, expected, description) {44 if (actual != expected) {45 assert_unreached("Expected: " + expected + " but got: " + actual);46 }47}48assert_own_property = function (object, property, description) {49 if (object[property] === undefined) {
Using AI Code Generation
1var test = async_test('TestName');2test.step(function() {3 assert_implements_optional(test, "method_name", "interface_name");4 test.done();5});6var test = async_test('TestName');7test.step(function() {8 assert_implements(test, "method_name", "interface_name");9 test.done();10});11var test = async_test('TestName');12test.step(function() {13 assert_throws_dom("type", function() {14 });15 test.done();16});17var test = async_test('TestName');18test.step(function() {19 assert_throws_js("type", function() {20 });21 test.done();22});23var test = async_test('TestName');24test.step(function() {25 assert_throws_exactly("type", "name", function() {26 });27 test.done();28});29var test = async_test('TestName');30test.step(function() {31 assert_throws_exactly("type", "name", function() {32 });33 test.done();34});35var test = async_test('TestName');36test.step(function() {37 assert_throws_exactly("type", "name", function() {38 });39 test.done();40});41var test = async_test('TestName');42test.step(function() {43 assert_throws_exactly("type", "name", function() {44 });45 test.done();46});47var test = async_test('TestName');48test.step(function() {49 assert_throws_exactly("type", "name", function() {50 });51 test.done();52});53var test = async_test('TestName');
Using AI Code Generation
1import {assert_implements_optional} from './assert.js';2interface TestInterface {3 testMethod(): void;4}5assert_implements_optional<TestInterface>('TestInterface', 'testMethod');6assert_implements_optional<TestInterface>('TestInterface', 'testMethod');7assert_implements_optional<TestInterface>('TestInterface', 'testMethod');8assert_implements_optional<TestInterface>('TestInterface', 'testMethod');9assert_implements_optional<TestInterface>('TestInterface', 'testMethod');10assert_implements_optional<TestInterface>('TestInterface', 'testMethod');11assert_implements_optional<TestInterface>('TestInterface', 'testMethod');12assert_implements_optional<TestInterface>('TestInterface', 'testMethod');13assert_implements_optional<TestInterface>('TestInterface', 'testMethod');14assert_implements_optional<TestInterface>('TestInterface', 'testMethod');15assert_implements_optional<TestInterface>('TestInterface', 'testMethod');
Using AI Code Generation
1const assert_implements_optional = (object, interface, message) => {2 if (object === null || object === undefined) {3 return;4 }5 assert_implements(object, interface, message);6};7const assert_unreached = (message) => {8 throw new Error(message);9};10const assert_array_equals = (actual, expected, message) => {11 if (message === undefined) {12 message = "";13 }14 if (actual.length !== expected.length) {15 throw new Error(16 `Arrays have different length: ${actual.length} != ${expected.length}. ${message}`17 );18 }19 for (let i = 0; i < expected.length; i++) {20 if (actual[i] !== expected[i]) {21 throw new Error(22 `Arrays are different at index ${i}: ${actual[i]} != ${expected[i]}. ${message}`23 );24 }25 }26};27const assert_throws = (exception, func, message) => {28 let thrown = false;29 try {30 func();31 } catch (e) {32 if (e instanceof exception) {33 thrown = true;34 } else {35 throw new Error(36 `Function threw ${e.name}, expected ${exception.name} ${message}`37 );38 }39 }40 if (!thrown) {41 throw new Error(`Function did not throw, expected ${exception.name} ${message}`);42 }43};44const assert_throws_dom = (exception, func
Using AI Code Generation
1assert_implements_optional(HTMLMediaElement.prototype, "play",2 "The HTMLMediaElement interface must implement the play method.");3assert_implements_optional(HTMLMediaElement.prototype, "pause",4 "The HTMLMediaElement interface must implement the pause method.");5assert_implements_optional(HTMLMediaElement.prototype, "canPlayType",6 "The HTMLMediaElement interface must implement the canPlayType method.");7assert_implements_optional(HTMLMediaElement.prototype, "load",8 "The HTMLMediaElement interface must implement the load method.");9assert_implements_optional(HTMLMediaElement.prototype, "addTextTrack",10 "The HTMLMediaElement interface must implement the addTextTrack method.");11assert_implements_optional(HTMLMediaElement.prototype, "captureStream",12 "The HTMLMediaElement interface must implement the captureStream method.");13assert_implements_optional(HTMLMediaElement.prototype, "fastSeek",14 "The HTMLMediaElement interface must implement the fastSeek method.");15assert_implements_optional(HTMLMediaElement.prototype, "getStartDate",16 "The HTMLMediaElement interface must implement the getStartDate method.");17assert_implements_optional(HTMLMediaElement.prototype, "playbackRate",18 "The HTMLMediaElement interface must implement the playbackRate property.");19assert_implements_optional(HTMLMediaElement.prototype, "played",20 "The HTMLMediaElement interface must implement the played property.");21assert_implements_optional(HTMLMediaElement.prototype, "seekable",22 "The HTMLMediaElement interface must implement the seekable property.");23assert_implements_optional(HTMLMediaElement.prototype, "seeking",24 "The HTMLMediaElement interface must implement the seeking property.");25assert_implements_optional(HTMLMediaElement.prototype, "played",26 "The HTMLMediaElement interface must implement the played property.");27assert_implements_optional(HTMLMediaElement.prototype, "src",28 "The HTMLMediaElement interface must implement the src property.");29assert_implements_optional(HTMLMediaElement.prototype, "srcObject",30 "The HTMLMediaElement interface must implement the srcObject property.");31assert_implements_optional(HTMLMediaElement.prototype, "textTracks",32 "The HTMLMediaElement interface must implement the textTracks property.");33assert_implements_optional(HTMLMediaElement.prototype, "videoTracks",
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!