An animated PNG plays the frames one time more than the image loopCount
[WebKit-https.git] / LayoutTests / fast / images / resources / animated-image-loop-count.js
1 function drawFrame(image, canvasId) {
2     return new Promise((resolve) => {
3         let canvas = document.getElementById("canvas-" + canvasId);
4         let context = canvas.getContext("2d");
5         context.drawImage(image, 0, 0, canvas.width, canvas.height);
6         setTimeout(() => {
7             resolve(String.fromCharCode(canvasId.charCodeAt() + 1));
8         }, 30);
9     });
10 }
11
12 function drawImage(image, canvasId, frameCount) {
13     let promise = drawFrame(image, canvasId);
14     for (let frame = 1; frame < frameCount; ++frame) {
15         promise = promise.then((canvasId) => {
16             return drawFrame(image, canvasId);
17         });
18     }
19     return promise;
20 }
21
22 function loadImage(src, canvasId, frameCount) {
23     return new Promise((resolve) => {
24         let image = new Image;
25         image.onload = (() => {
26             drawImage(image, canvasId, frameCount).then(resolve);
27         });
28         image.src = src;
29     });
30 }
31
32 function runTest(images) {
33     if (window.internals) {
34         internals.clearMemoryCache();
35         internals.settings.setAnimatedImageDebugCanvasDrawingEnabled(true);
36     }
37
38     if (window.testRunner)
39         testRunner.waitUntilDone();
40
41     var promises = [];
42
43     for (let image of images)
44         promises.push(loadImage(image.src, image.canvasId, image.frameCount));
45             
46     Promise.all(promises).then(() => {
47         if (window.testRunner)
48             testRunner.notifyDone();
49     });
50 }