Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / image-decoders / webp / WEBPImageDecoderTest.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #include "platform/image-decoders/webp/WEBPImageDecoder.h"
34
35 #include "RuntimeEnabledFeatures.h"
36 #include "platform/SharedBuffer.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebData.h"
39 #include "public/platform/WebSize.h"
40 #include "public/platform/WebUnitTestSupport.h"
41 #include "wtf/OwnPtr.h"
42 #include "wtf/PassOwnPtr.h"
43 #include "wtf/StringHasher.h"
44 #include "wtf/Vector.h"
45 #include "wtf/dtoa/utils.h"
46 #include <gtest/gtest.h>
47
48 using namespace WebCore;
49 using namespace blink;
50
51 namespace {
52
53 PassRefPtr<SharedBuffer> readFile(const char* fileName)
54 {
55     String filePath = Platform::current()->unitTestSupport()->webKitRootDir();
56     filePath.append(fileName);
57
58     return Platform::current()->unitTestSupport()->readFromFile(filePath);
59 }
60
61 PassOwnPtr<WEBPImageDecoder> createDecoder()
62 {
63     return adoptPtr(new WEBPImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied, ImageDecoder::noDecodedImageByteLimit));
64 }
65
66 unsigned hashSkBitmap(const SkBitmap& bitmap)
67 {
68     return StringHasher::hashMemory(bitmap.getPixels(), bitmap.getSize());
69 }
70
71 void createDecodingBaseline(SharedBuffer* data, Vector<unsigned>* baselineHashes)
72 {
73     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
74     decoder->setData(data, true);
75     size_t frameCount = decoder->frameCount();
76     for (size_t i = 0; i < frameCount; ++i) {
77         ImageFrame* frame = decoder->frameBufferAtIndex(i);
78         baselineHashes->append(hashSkBitmap(frame->getSkBitmap()));
79     }
80 }
81
82 void testRandomFrameDecode(const char* webpFile)
83 {
84     SCOPED_TRACE(webpFile);
85
86     RefPtr<SharedBuffer> fullData = readFile(webpFile);
87     ASSERT_TRUE(fullData.get());
88     Vector<unsigned> baselineHashes;
89     createDecodingBaseline(fullData.get(), &baselineHashes);
90     size_t frameCount = baselineHashes.size();
91
92     // Random decoding should get the same results as sequential decoding.
93     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
94     decoder->setData(fullData.get(), true);
95     const size_t skippingStep = 5;
96     for (size_t i = 0; i < skippingStep; ++i) {
97         for (size_t j = i; j < frameCount; j += skippingStep) {
98             SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j);
99             ImageFrame* frame = decoder->frameBufferAtIndex(j);
100             EXPECT_EQ(baselineHashes[j], hashSkBitmap(frame->getSkBitmap()));
101         }
102     }
103
104     // Decoding in reverse order.
105     decoder = createDecoder();
106     decoder->setData(fullData.get(), true);
107     for (size_t i = frameCount; i; --i) {
108         SCOPED_TRACE(testing::Message() << "Reverse i:" << i);
109         ImageFrame* frame = decoder->frameBufferAtIndex(i - 1);
110         EXPECT_EQ(baselineHashes[i - 1], hashSkBitmap(frame->getSkBitmap()));
111     }
112 }
113
114 void testRandomDecodeAfterClearFrameBufferCache(const char* webpFile)
115 {
116     SCOPED_TRACE(webpFile);
117
118     RefPtr<SharedBuffer> data = readFile(webpFile);
119     ASSERT_TRUE(data.get());
120     Vector<unsigned> baselineHashes;
121     createDecodingBaseline(data.get(), &baselineHashes);
122     size_t frameCount = baselineHashes.size();
123
124     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
125     decoder->setData(data.get(), true);
126     for (size_t clearExceptFrame = 0; clearExceptFrame < frameCount; ++clearExceptFrame) {
127         decoder->clearCacheExceptFrame(clearExceptFrame);
128         const size_t skippingStep = 5;
129         for (size_t i = 0; i < skippingStep; ++i) {
130             for (size_t j = 0; j < frameCount; j += skippingStep) {
131                 SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j);
132                 ImageFrame* frame = decoder->frameBufferAtIndex(j);
133                 EXPECT_EQ(baselineHashes[j], hashSkBitmap(frame->getSkBitmap()));
134             }
135         }
136     }
137 }
138
139 void testDecodeAfterReallocatingData(const char* webpFile)
140 {
141     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
142     RefPtr<SharedBuffer> data = readFile(webpFile);
143     ASSERT_TRUE(data.get());
144
145     // Parse from 'data'.
146     decoder->setData(data.get(), true);
147     size_t frameCount = decoder->frameCount();
148
149     // ... and then decode frames from 'reallocatedData'.
150     RefPtr<SharedBuffer> reallocatedData = data.get()->copy();
151     ASSERT_TRUE(reallocatedData.get());
152     data.clear();
153     decoder->setData(reallocatedData.get(), true);
154
155     for (size_t i = 0; i < frameCount; ++i) {
156         const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
157         EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
158     }
159 }
160
161 void testByteByByteDecode(const char* webpFile, size_t expectedFrameCount, int expectedRepetitionCount)
162 {
163     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
164     RefPtr<SharedBuffer> data = readFile(webpFile);
165     ASSERT_TRUE(data.get());
166
167     size_t frameCount = 0;
168     size_t framesDecoded = 0;
169
170     // Pass data to decoder byte by byte.
171     for (size_t length = 1; length <= data->size(); ++length) {
172         RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length);
173         decoder->setData(tempData.get(), length == data->size());
174
175         EXPECT_LE(frameCount, decoder->frameCount());
176         frameCount = decoder->frameCount();
177
178         ImageFrame* frame = decoder->frameBufferAtIndex(frameCount - 1);
179         if (frame && frame->status() == ImageFrame::FrameComplete && framesDecoded < frameCount)
180             ++framesDecoded;
181
182         if (decoder->failed())
183             break;
184     }
185
186     EXPECT_FALSE(decoder->failed());
187     EXPECT_EQ(expectedFrameCount, decoder->frameCount());
188     EXPECT_EQ(expectedFrameCount, framesDecoded);
189     EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount());
190 }
191
192 // If 'parseErrorExpected' is true, error is expected during parse (frameCount()
193 // call); else error is expected during decode (frameBufferAtIndex() call).
194 void testInvalidImage(const char* webpFile, bool parseErrorExpected)
195 {
196     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
197
198     RefPtr<SharedBuffer> data = readFile(webpFile);
199     ASSERT_TRUE(data.get());
200     decoder->setData(data.get(), true);
201
202     if (parseErrorExpected)
203         EXPECT_EQ(0u, decoder->frameCount());
204     else
205         EXPECT_LT(0u, decoder->frameCount());
206     ImageFrame* frame = decoder->frameBufferAtIndex(0);
207     EXPECT_FALSE(frame);
208     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
209 }
210
211 } // namespace
212
213 TEST(AnimatedWebPTests, uniqueGenerationIDs)
214 {
215     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
216
217     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
218     ASSERT_TRUE(data.get());
219     decoder->setData(data.get(), true);
220
221     ImageFrame* frame = decoder->frameBufferAtIndex(0);
222     uint32_t generationID0 = frame->getSkBitmap().getGenerationID();
223     frame = decoder->frameBufferAtIndex(1);
224     uint32_t generationID1 = frame->getSkBitmap().getGenerationID();
225
226     EXPECT_TRUE(generationID0 != generationID1);
227 }
228
229 TEST(AnimatedWebPTests, verifyAnimationParametersTransparentImage)
230 {
231     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
232     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
233
234     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
235     ASSERT_TRUE(data.get());
236     decoder->setData(data.get(), true);
237
238     const int canvasWidth = 11;
239     const int canvasHeight = 29;
240     const struct AnimParam {
241         int xOffset, yOffset, width, height;
242         ImageFrame::DisposalMethod disposalMethod;
243         ImageFrame::AlphaBlendSource alphaBlendSource;
244         unsigned duration;
245         bool hasAlpha;
246     } frameParameters[] = {
247         { 0, 0, 11, 29, ImageFrame::DisposeKeep, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
248         { 2, 10, 7, 17, ImageFrame::DisposeKeep, ImageFrame::BlendAtopPreviousFrame, 500u, true },
249         { 2, 2, 7, 16, ImageFrame::DisposeKeep, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
250     };
251
252     for (size_t i = 0; i < ARRAY_SIZE(frameParameters); ++i) {
253         const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
254         EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
255         EXPECT_EQ(canvasWidth, frame->getSkBitmap().width());
256         EXPECT_EQ(canvasHeight, frame->getSkBitmap().height());
257         EXPECT_EQ(frameParameters[i].xOffset, frame->originalFrameRect().x());
258         EXPECT_EQ(frameParameters[i].yOffset, frame->originalFrameRect().y());
259         EXPECT_EQ(frameParameters[i].width, frame->originalFrameRect().width());
260         EXPECT_EQ(frameParameters[i].height, frame->originalFrameRect().height());
261         EXPECT_EQ(frameParameters[i].disposalMethod, frame->disposalMethod());
262         EXPECT_EQ(frameParameters[i].alphaBlendSource, frame->alphaBlendSource());
263         EXPECT_EQ(frameParameters[i].duration, frame->duration());
264         EXPECT_EQ(frameParameters[i].hasAlpha, frame->hasAlpha());
265     }
266
267     EXPECT_EQ(ARRAY_SIZE(frameParameters), decoder->frameCount());
268     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
269 }
270
271 TEST(AnimatedWebPTests, verifyAnimationParametersOpaqueFramesTransparentBackground)
272 {
273     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
274     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
275
276     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated-opaque.webp");
277     ASSERT_TRUE(data.get());
278     decoder->setData(data.get(), true);
279
280     const int canvasWidth = 94;
281     const int canvasHeight = 87;
282     const struct AnimParam {
283         int xOffset, yOffset, width, height;
284         ImageFrame::DisposalMethod disposalMethod;
285         ImageFrame::AlphaBlendSource alphaBlendSource;
286         unsigned duration;
287         bool hasAlpha;
288     } frameParameters[] = {
289         { 4, 10, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
290         { 34, 30, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
291         { 62, 50, 32, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
292         { 10, 54, 32, 33, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
293     };
294
295     for (size_t i = 0; i < ARRAY_SIZE(frameParameters); ++i) {
296         const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
297         EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
298         EXPECT_EQ(canvasWidth, frame->getSkBitmap().width());
299         EXPECT_EQ(canvasHeight, frame->getSkBitmap().height());
300         EXPECT_EQ(frameParameters[i].xOffset, frame->originalFrameRect().x());
301         EXPECT_EQ(frameParameters[i].yOffset, frame->originalFrameRect().y());
302         EXPECT_EQ(frameParameters[i].width, frame->originalFrameRect().width());
303         EXPECT_EQ(frameParameters[i].height, frame->originalFrameRect().height());
304         EXPECT_EQ(frameParameters[i].disposalMethod, frame->disposalMethod());
305         EXPECT_EQ(frameParameters[i].alphaBlendSource, frame->alphaBlendSource());
306         EXPECT_EQ(frameParameters[i].duration, frame->duration());
307         EXPECT_EQ(frameParameters[i].hasAlpha, frame->hasAlpha());
308     }
309
310     EXPECT_EQ(ARRAY_SIZE(frameParameters), decoder->frameCount());
311     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
312 }
313
314 TEST(AnimatedWebPTests, verifyAnimationParametersBlendOverwrite)
315 {
316     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
317     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
318
319     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated-no-blend.webp");
320     ASSERT_TRUE(data.get());
321     decoder->setData(data.get(), true);
322
323     const int canvasWidth = 94;
324     const int canvasHeight = 87;
325     const struct AnimParam {
326         int xOffset, yOffset, width, height;
327         ImageFrame::DisposalMethod disposalMethod;
328         ImageFrame::AlphaBlendSource alphaBlendSource;
329         unsigned duration;
330         bool hasAlpha;
331     } frameParameters[] = {
332         { 4, 10, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
333         { 34, 30, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
334         { 62, 50, 32, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
335         { 10, 54, 32, 33, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
336     };
337
338     for (size_t i = 0; i < ARRAY_SIZE(frameParameters); ++i) {
339         const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
340         EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
341         EXPECT_EQ(canvasWidth, frame->getSkBitmap().width());
342         EXPECT_EQ(canvasHeight, frame->getSkBitmap().height());
343         EXPECT_EQ(frameParameters[i].xOffset, frame->originalFrameRect().x());
344         EXPECT_EQ(frameParameters[i].yOffset, frame->originalFrameRect().y());
345         EXPECT_EQ(frameParameters[i].width, frame->originalFrameRect().width());
346         EXPECT_EQ(frameParameters[i].height, frame->originalFrameRect().height());
347         EXPECT_EQ(frameParameters[i].disposalMethod, frame->disposalMethod());
348         EXPECT_EQ(frameParameters[i].alphaBlendSource, frame->alphaBlendSource());
349         EXPECT_EQ(frameParameters[i].duration, frame->duration());
350         EXPECT_EQ(frameParameters[i].hasAlpha, frame->hasAlpha());
351     }
352
353     EXPECT_EQ(ARRAY_SIZE(frameParameters), decoder->frameCount());
354     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
355 }
356
357 TEST(AnimatedWebPTests, parseAndDecodeByteByByte)
358 {
359     testByteByByteDecode("/LayoutTests/fast/images/resources/webp-animated.webp", 3u, cAnimationLoopInfinite);
360     testByteByByteDecode("/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp", 13u, 32000);
361 }
362
363 TEST(AnimatedWebPTests, invalidImages)
364 {
365     // ANMF chunk size is smaller than ANMF header size.
366     testInvalidImage("/LayoutTests/fast/images/resources/invalid-animated-webp.webp", true);
367     // One of the frame rectangles extends outside the image boundary.
368     testInvalidImage("/LayoutTests/fast/images/resources/invalid-animated-webp3.webp", true);
369 }
370
371 TEST(AnimatedWebPTests, truncatedLastFrame)
372 {
373     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
374
375     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/invalid-animated-webp2.webp");
376     ASSERT_TRUE(data.get());
377     decoder->setData(data.get(), true);
378
379     size_t frameCount = 8;
380     EXPECT_EQ(frameCount, decoder->frameCount());
381     ImageFrame* frame = decoder->frameBufferAtIndex(frameCount - 1);
382     EXPECT_FALSE(frame);
383     frame = decoder->frameBufferAtIndex(0);
384     EXPECT_FALSE(frame);
385 }
386
387 TEST(AnimatedWebPTests, truncatedInBetweenFrame)
388 {
389     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
390
391     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/invalid-animated-webp4.webp");
392     ASSERT_TRUE(fullData.get());
393     RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), fullData->size() - 1);
394     decoder->setData(data.get(), false);
395
396     ImageFrame* frame = decoder->frameBufferAtIndex(2);
397     EXPECT_FALSE(frame);
398 }
399
400 // Reproduce a crash that used to happen for a specific file with specific sequence of method calls.
401 TEST(AnimatedWebPTests, reproCrash)
402 {
403     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
404
405     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/invalid_vp8_vp8x.webp");
406     ASSERT_TRUE(fullData.get());
407
408     // Parse partial data up to which error in bitstream is not detected.
409     const size_t partialSize = 32768;
410     ASSERT_GT(fullData->size(), partialSize);
411     RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), partialSize);
412     decoder->setData(data.get(), false);
413     EXPECT_EQ(1u, decoder->frameCount());
414
415     // Parse full data now. The error in bitstream should now be detected.
416     decoder->setData(fullData.get(), true);
417     EXPECT_EQ(0u, decoder->frameCount());
418     ImageFrame* frame = decoder->frameBufferAtIndex(0);
419     EXPECT_FALSE(frame);
420     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
421 }
422
423 TEST(AnimatedWebPTests, progressiveDecode)
424 {
425     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
426     ASSERT_TRUE(fullData.get());
427     const size_t fullLength = fullData->size();
428
429     OwnPtr<WEBPImageDecoder>  decoder;
430     ImageFrame* frame;
431
432     Vector<unsigned> truncatedHashes;
433     Vector<unsigned> progressiveHashes;
434
435     // Compute hashes when the file is truncated.
436     const size_t increment = 1;
437     for (size_t i = 1; i <= fullLength; i += increment) {
438         decoder = createDecoder();
439         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
440         decoder->setData(data.get(), i == fullLength);
441         frame = decoder->frameBufferAtIndex(0);
442         if (!frame) {
443             truncatedHashes.append(0);
444             continue;
445         }
446         truncatedHashes.append(hashSkBitmap(frame->getSkBitmap()));
447     }
448
449     // Compute hashes when the file is progressively decoded.
450     decoder = createDecoder();
451     for (size_t i = 1; i <= fullLength; i += increment) {
452         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
453         decoder->setData(data.get(), i == fullLength);
454         frame = decoder->frameBufferAtIndex(0);
455         if (!frame) {
456             progressiveHashes.append(0);
457             continue;
458         }
459         progressiveHashes.append(hashSkBitmap(frame->getSkBitmap()));
460     }
461
462     bool match = true;
463     for (size_t i = 0; i < truncatedHashes.size(); ++i) {
464         if (truncatedHashes[i] != progressiveHashes[i]) {
465             match = false;
466             break;
467         }
468     }
469     EXPECT_TRUE(match);
470 }
471
472 TEST(AnimatedWebPTests, frameIsCompleteAndDuration)
473 {
474     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
475
476     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
477     ASSERT_TRUE(data.get());
478
479     ASSERT_GE(data->size(), 10u);
480     RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), data->size() - 10);
481     decoder->setData(tempData.get(), false);
482
483     EXPECT_EQ(2u, decoder->frameCount());
484     EXPECT_FALSE(decoder->failed());
485     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
486     EXPECT_EQ(1000, decoder->frameDurationAtIndex(0));
487     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(1));
488     EXPECT_EQ(500, decoder->frameDurationAtIndex(1));
489
490     decoder->setData(data.get(), true);
491     EXPECT_EQ(3u, decoder->frameCount());
492     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
493     EXPECT_EQ(1000, decoder->frameDurationAtIndex(0));
494     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(1));
495     EXPECT_EQ(500, decoder->frameDurationAtIndex(1));
496     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(2));
497     EXPECT_EQ(1000.0, decoder->frameDurationAtIndex(2));
498 }
499
500 TEST(AnimatedWebPTests, updateRequiredPreviousFrameAfterFirstDecode)
501 {
502     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
503
504     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
505     ASSERT_TRUE(fullData.get());
506
507     // Give it data that is enough to parse but not decode in order to check the status
508     // of requiredPreviousFrameIndex before decoding.
509     size_t partialSize = 1;
510     do {
511         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), partialSize);
512         decoder->setData(data.get(), false);
513         ++partialSize;
514     } while (!decoder->frameCount() || decoder->frameBufferAtIndex(0)->status() == ImageFrame::FrameEmpty);
515
516     EXPECT_EQ(kNotFound, decoder->frameBufferAtIndex(0)->requiredPreviousFrameIndex());
517     size_t frameCount = decoder->frameCount();
518     for (size_t i = 1; i < frameCount; ++i)
519         EXPECT_EQ(i - 1, decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex());
520
521     decoder->setData(fullData.get(), true);
522     for (size_t i = 0; i < frameCount; ++i)
523         EXPECT_EQ(kNotFound, decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex());
524 }
525
526 TEST(AnimatedWebPTests, randomFrameDecode)
527 {
528     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated.webp");
529     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated-opaque.webp");
530     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated-large.webp");
531     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp");
532 }
533
534 TEST(AnimatedWebPTests, randomDecodeAfterClearFrameBufferCache)
535 {
536     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated.webp");
537     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated-opaque.webp");
538     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated-large.webp");
539     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp");
540 }
541
542 TEST(AnimatedWebPTests, resumePartialDecodeAfterClearFrameBufferCache)
543 {
544     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/webp-animated-large.webp");
545     ASSERT_TRUE(fullData.get());
546     Vector<unsigned> baselineHashes;
547     createDecodingBaseline(fullData.get(), &baselineHashes);
548     size_t frameCount = baselineHashes.size();
549
550     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
551
552     // Let frame 0 be partially decoded.
553     size_t partialSize = 1;
554     do {
555         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), partialSize);
556         decoder->setData(data.get(), false);
557         ++partialSize;
558     } while (!decoder->frameCount() || decoder->frameBufferAtIndex(0)->status() == ImageFrame::FrameEmpty);
559
560     // Skip to the last frame and clear.
561     decoder->setData(fullData.get(), true);
562     EXPECT_EQ(frameCount, decoder->frameCount());
563     ImageFrame* lastFrame = decoder->frameBufferAtIndex(frameCount - 1);
564     EXPECT_EQ(baselineHashes[frameCount - 1], hashSkBitmap(lastFrame->getSkBitmap()));
565     decoder->clearCacheExceptFrame(kNotFound);
566
567     // Resume decoding of the first frame.
568     ImageFrame* firstFrame = decoder->frameBufferAtIndex(0);
569     EXPECT_EQ(ImageFrame::FrameComplete, firstFrame->status());
570     EXPECT_EQ(baselineHashes[0], hashSkBitmap(firstFrame->getSkBitmap()));
571 }
572
573 TEST(AnimatedWebPTests, decodeAfterReallocatingData)
574 {
575     testDecodeAfterReallocatingData("/LayoutTests/fast/images/resources/webp-animated.webp");
576     testDecodeAfterReallocatingData("/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp");
577 }
578
579 TEST(StaticWebPTests, truncatedImage)
580 {
581     // VP8 data is truncated.
582     testInvalidImage("/LayoutTests/fast/images/resources/truncated.webp", false);
583     // Chunk size in RIFF header doesn't match the file size.
584     testInvalidImage("/LayoutTests/fast/images/resources/truncated2.webp", true);
585 }
586
587 TEST(StaticWebPTests, incrementalDecode)
588 {
589     // Regression test for a bug where some valid images were failing to decode incrementally.
590     testByteByByteDecode("/LayoutTests/fast/images/resources/crbug.364830.webp", 1u, cAnimationNone);
591 }
592
593 TEST(StaticWebPTests, notAnimated)
594 {
595     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
596     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-color-profile-lossy.webp");
597     ASSERT_TRUE(data.get());
598     decoder->setData(data.get(), true);
599     EXPECT_EQ(1u, decoder->frameCount());
600     EXPECT_EQ(cAnimationNone, decoder->repetitionCount());
601 }