81aa6dec27abf71939748b83b29b51797e5c2399
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / 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 // If 'parseErrorExpected' is true, error is expected during parse (frameCount()
162 // call); else error is expected during decode (frameBufferAtIndex() call).
163 void testInvalidImage(const char* webpFile, bool parseErrorExpected)
164 {
165     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
166
167     RefPtr<SharedBuffer> data = readFile(webpFile);
168     ASSERT_TRUE(data.get());
169     decoder->setData(data.get(), true);
170
171     if (parseErrorExpected)
172         EXPECT_EQ(0u, decoder->frameCount());
173     else
174         EXPECT_LT(0u, decoder->frameCount());
175     ImageFrame* frame = decoder->frameBufferAtIndex(0);
176     EXPECT_FALSE(frame);
177     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
178 }
179
180 } // namespace
181
182 class AnimatedWebPTests : public ::testing::Test {
183 protected:
184     virtual void SetUp()
185     {
186         // Enable animated WebP for all the tests.
187         WebCore::RuntimeEnabledFeatures::setAnimatedWebPEnabled(true);
188     }
189 };
190
191 TEST_F(AnimatedWebPTests, uniqueGenerationIDs)
192 {
193     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
194
195     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
196     ASSERT_TRUE(data.get());
197     decoder->setData(data.get(), true);
198
199     ImageFrame* frame = decoder->frameBufferAtIndex(0);
200     uint32_t generationID0 = frame->getSkBitmap().getGenerationID();
201     frame = decoder->frameBufferAtIndex(1);
202     uint32_t generationID1 = frame->getSkBitmap().getGenerationID();
203
204     EXPECT_TRUE(generationID0 != generationID1);
205 }
206
207 TEST_F(AnimatedWebPTests, verifyAnimationParametersTransparentImage)
208 {
209     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
210     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
211
212     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
213     ASSERT_TRUE(data.get());
214     decoder->setData(data.get(), true);
215
216     const int canvasWidth = 11;
217     const int canvasHeight = 29;
218     const struct AnimParam {
219         int xOffset, yOffset, width, height;
220         ImageFrame::DisposalMethod disposalMethod;
221         ImageFrame::AlphaBlendSource alphaBlendSource;
222         unsigned duration;
223         bool hasAlpha;
224     } frameParameters[] = {
225         { 0, 0, 11, 29, ImageFrame::DisposeKeep, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
226         { 2, 10, 7, 17, ImageFrame::DisposeKeep, ImageFrame::BlendAtopPreviousFrame, 500u, true },
227         { 2, 2, 7, 16, ImageFrame::DisposeKeep, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
228     };
229
230     for (size_t i = 0; i < ARRAY_SIZE(frameParameters); ++i) {
231         const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
232         EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
233         EXPECT_EQ(canvasWidth, frame->getSkBitmap().width());
234         EXPECT_EQ(canvasHeight, frame->getSkBitmap().height());
235         EXPECT_EQ(frameParameters[i].xOffset, frame->originalFrameRect().x());
236         EXPECT_EQ(frameParameters[i].yOffset, frame->originalFrameRect().y());
237         EXPECT_EQ(frameParameters[i].width, frame->originalFrameRect().width());
238         EXPECT_EQ(frameParameters[i].height, frame->originalFrameRect().height());
239         EXPECT_EQ(frameParameters[i].disposalMethod, frame->disposalMethod());
240         EXPECT_EQ(frameParameters[i].alphaBlendSource, frame->alphaBlendSource());
241         EXPECT_EQ(frameParameters[i].duration, frame->duration());
242         EXPECT_EQ(frameParameters[i].hasAlpha, frame->hasAlpha());
243     }
244
245     EXPECT_EQ(ARRAY_SIZE(frameParameters), decoder->frameCount());
246     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
247 }
248
249 TEST_F(AnimatedWebPTests, verifyAnimationParametersOpaqueFramesTransparentBackground)
250 {
251     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
252     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
253
254     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated-opaque.webp");
255     ASSERT_TRUE(data.get());
256     decoder->setData(data.get(), true);
257
258     const int canvasWidth = 94;
259     const int canvasHeight = 87;
260     const struct AnimParam {
261         int xOffset, yOffset, width, height;
262         ImageFrame::DisposalMethod disposalMethod;
263         ImageFrame::AlphaBlendSource alphaBlendSource;
264         unsigned duration;
265         bool hasAlpha;
266     } frameParameters[] = {
267         { 4, 10, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
268         { 34, 30, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
269         { 62, 50, 32, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
270         { 10, 54, 32, 33, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopPreviousFrame, 1000u, true },
271     };
272
273     for (size_t i = 0; i < ARRAY_SIZE(frameParameters); ++i) {
274         const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
275         EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
276         EXPECT_EQ(canvasWidth, frame->getSkBitmap().width());
277         EXPECT_EQ(canvasHeight, frame->getSkBitmap().height());
278         EXPECT_EQ(frameParameters[i].xOffset, frame->originalFrameRect().x());
279         EXPECT_EQ(frameParameters[i].yOffset, frame->originalFrameRect().y());
280         EXPECT_EQ(frameParameters[i].width, frame->originalFrameRect().width());
281         EXPECT_EQ(frameParameters[i].height, frame->originalFrameRect().height());
282         EXPECT_EQ(frameParameters[i].disposalMethod, frame->disposalMethod());
283         EXPECT_EQ(frameParameters[i].alphaBlendSource, frame->alphaBlendSource());
284         EXPECT_EQ(frameParameters[i].duration, frame->duration());
285         EXPECT_EQ(frameParameters[i].hasAlpha, frame->hasAlpha());
286     }
287
288     EXPECT_EQ(ARRAY_SIZE(frameParameters), decoder->frameCount());
289     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
290 }
291
292 TEST_F(AnimatedWebPTests, verifyAnimationParametersBlendOverwrite)
293 {
294     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
295     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
296
297     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated-no-blend.webp");
298     ASSERT_TRUE(data.get());
299     decoder->setData(data.get(), true);
300
301     const int canvasWidth = 94;
302     const int canvasHeight = 87;
303     const struct AnimParam {
304         int xOffset, yOffset, width, height;
305         ImageFrame::DisposalMethod disposalMethod;
306         ImageFrame::AlphaBlendSource alphaBlendSource;
307         unsigned duration;
308         bool hasAlpha;
309     } frameParameters[] = {
310         { 4, 10, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
311         { 34, 30, 33, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
312         { 62, 50, 32, 32, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
313         { 10, 54, 32, 33, ImageFrame::DisposeOverwriteBgcolor, ImageFrame::BlendAtopBgcolor, 1000u, true },
314     };
315
316     for (size_t i = 0; i < ARRAY_SIZE(frameParameters); ++i) {
317         const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
318         EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
319         EXPECT_EQ(canvasWidth, frame->getSkBitmap().width());
320         EXPECT_EQ(canvasHeight, frame->getSkBitmap().height());
321         EXPECT_EQ(frameParameters[i].xOffset, frame->originalFrameRect().x());
322         EXPECT_EQ(frameParameters[i].yOffset, frame->originalFrameRect().y());
323         EXPECT_EQ(frameParameters[i].width, frame->originalFrameRect().width());
324         EXPECT_EQ(frameParameters[i].height, frame->originalFrameRect().height());
325         EXPECT_EQ(frameParameters[i].disposalMethod, frame->disposalMethod());
326         EXPECT_EQ(frameParameters[i].alphaBlendSource, frame->alphaBlendSource());
327         EXPECT_EQ(frameParameters[i].duration, frame->duration());
328         EXPECT_EQ(frameParameters[i].hasAlpha, frame->hasAlpha());
329     }
330
331     EXPECT_EQ(ARRAY_SIZE(frameParameters), decoder->frameCount());
332     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
333 }
334
335 TEST_F(AnimatedWebPTests, parseAndDecodeByteByByte)
336 {
337     const struct TestImage {
338         const char* filename;
339         unsigned frameCount;
340         int repetitionCount;
341     } testImages[] = {
342         { "/LayoutTests/fast/images/resources/webp-animated.webp", 3u, cAnimationLoopInfinite },
343         { "/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp", 13u, 32000 },
344     };
345
346     for (size_t i = 0; i < ARRAY_SIZE(testImages); ++i) {
347         OwnPtr<WEBPImageDecoder> decoder = createDecoder();
348         RefPtr<SharedBuffer> data = readFile(testImages[i].filename);
349         ASSERT_TRUE(data.get());
350
351         size_t frameCount = 0;
352         size_t framesDecoded = 0;
353
354         // Pass data to decoder byte by byte.
355         for (size_t length = 1; length <= data->size(); ++length) {
356             RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length);
357             decoder->setData(tempData.get(), length == data->size());
358
359             EXPECT_LE(frameCount, decoder->frameCount());
360             frameCount = decoder->frameCount();
361
362             ImageFrame* frame = decoder->frameBufferAtIndex(frameCount - 1);
363             if (frame && frame->status() == ImageFrame::FrameComplete && framesDecoded < frameCount)
364                 ++framesDecoded;
365         }
366
367         EXPECT_EQ(testImages[i].frameCount, decoder->frameCount());
368         EXPECT_EQ(testImages[i].frameCount, framesDecoded);
369         EXPECT_EQ(testImages[i].repetitionCount, decoder->repetitionCount());
370     }
371 }
372
373 TEST_F(AnimatedWebPTests, invalidImages)
374 {
375     // ANMF chunk size is smaller than ANMF header size.
376     testInvalidImage("/LayoutTests/fast/images/resources/invalid-animated-webp.webp", true);
377     // One of the frame rectangles extends outside the image boundary.
378     testInvalidImage("/LayoutTests/fast/images/resources/invalid-animated-webp3.webp", true);
379 }
380
381 TEST_F(AnimatedWebPTests, truncatedLastFrame)
382 {
383     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
384
385     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/invalid-animated-webp2.webp");
386     ASSERT_TRUE(data.get());
387     decoder->setData(data.get(), true);
388
389     unsigned frameCount = 8;
390     EXPECT_EQ(frameCount, decoder->frameCount());
391     ImageFrame* frame = decoder->frameBufferAtIndex(frameCount - 1);
392     EXPECT_FALSE(frame);
393     frame = decoder->frameBufferAtIndex(0);
394     EXPECT_FALSE(frame);
395 }
396
397 TEST_F(AnimatedWebPTests, truncatedInBetweenFrame)
398 {
399     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
400
401     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/invalid-animated-webp4.webp");
402     ASSERT_TRUE(fullData.get());
403     RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), fullData->size() - 1);
404     decoder->setData(data.get(), false);
405
406     ImageFrame* frame = decoder->frameBufferAtIndex(2);
407     EXPECT_FALSE(frame);
408 }
409
410 // Reproduce a crash that used to happen for a specific file with specific sequence of method calls.
411 TEST_F(AnimatedWebPTests, reproCrash)
412 {
413     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
414
415     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/invalid_vp8_vp8x.webp");
416     ASSERT_TRUE(fullData.get());
417
418     // Parse partial data up to which error in bitstream is not detected.
419     const size_t partialSize = 32768;
420     ASSERT_GT(fullData->size(), partialSize);
421     RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), partialSize);
422     decoder->setData(data.get(), false);
423     EXPECT_EQ(1u, decoder->frameCount());
424
425     // Parse full data now. The error in bitstream should now be detected.
426     decoder->setData(fullData.get(), true);
427     EXPECT_EQ(0u, decoder->frameCount());
428     ImageFrame* frame = decoder->frameBufferAtIndex(0);
429     EXPECT_FALSE(frame);
430     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
431 }
432
433 TEST_F(AnimatedWebPTests, progressiveDecode)
434 {
435     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
436     ASSERT_TRUE(fullData.get());
437     const size_t fullLength = fullData->size();
438
439     OwnPtr<WEBPImageDecoder>  decoder;
440     ImageFrame* frame;
441
442     Vector<unsigned> truncatedHashes;
443     Vector<unsigned> progressiveHashes;
444
445     // Compute hashes when the file is truncated.
446     const size_t increment = 1;
447     for (size_t i = 1; i <= fullLength; i += increment) {
448         decoder = createDecoder();
449         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
450         decoder->setData(data.get(), i == fullLength);
451         frame = decoder->frameBufferAtIndex(0);
452         if (!frame) {
453             truncatedHashes.append(0);
454             continue;
455         }
456         truncatedHashes.append(hashSkBitmap(frame->getSkBitmap()));
457     }
458
459     // Compute hashes when the file is progressively decoded.
460     decoder = createDecoder();
461     for (size_t i = 1; i <= fullLength; i += increment) {
462         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
463         decoder->setData(data.get(), i == fullLength);
464         frame = decoder->frameBufferAtIndex(0);
465         if (!frame) {
466             progressiveHashes.append(0);
467             continue;
468         }
469         progressiveHashes.append(hashSkBitmap(frame->getSkBitmap()));
470     }
471
472     bool match = true;
473     for (size_t i = 0; i < truncatedHashes.size(); ++i) {
474         if (truncatedHashes[i] != progressiveHashes[i]) {
475             match = false;
476             break;
477         }
478     }
479     EXPECT_TRUE(match);
480 }
481
482 TEST_F(AnimatedWebPTests, frameIsCompleteAndDuration)
483 {
484     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
485
486     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
487     ASSERT_TRUE(data.get());
488
489     ASSERT_GE(data->size(), 10u);
490     RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), data->size() - 10);
491     decoder->setData(tempData.get(), false);
492
493     EXPECT_EQ(2u, decoder->frameCount());
494     EXPECT_FALSE(decoder->failed());
495     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
496     EXPECT_EQ(1000, decoder->frameDurationAtIndex(0));
497     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(1));
498     EXPECT_EQ(500, decoder->frameDurationAtIndex(1));
499
500     decoder->setData(data.get(), true);
501     EXPECT_EQ(3u, decoder->frameCount());
502     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
503     EXPECT_EQ(1000, decoder->frameDurationAtIndex(0));
504     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(1));
505     EXPECT_EQ(500, decoder->frameDurationAtIndex(1));
506     EXPECT_TRUE(decoder->frameIsCompleteAtIndex(2));
507     EXPECT_EQ(1000.0, decoder->frameDurationAtIndex(2));
508 }
509
510 TEST_F(AnimatedWebPTests, updateRequiredPreviousFrameAfterFirstDecode)
511 {
512     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
513
514     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/webp-animated.webp");
515     ASSERT_TRUE(fullData.get());
516
517     // Give it data that is enough to parse but not decode in order to check the status
518     // of requiredPreviousFrameIndex before decoding.
519     size_t partialSize = 1;
520     do {
521         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), partialSize);
522         decoder->setData(data.get(), false);
523         ++partialSize;
524     } while (!decoder->frameCount() || decoder->frameBufferAtIndex(0)->status() == ImageFrame::FrameEmpty);
525
526     EXPECT_EQ(kNotFound, decoder->frameBufferAtIndex(0)->requiredPreviousFrameIndex());
527     unsigned frameCount = decoder->frameCount();
528     for (size_t i = 1; i < frameCount; ++i)
529         EXPECT_EQ(i - 1, decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex());
530
531     decoder->setData(fullData.get(), true);
532     for (size_t i = 0; i < frameCount; ++i)
533         EXPECT_EQ(kNotFound, decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex());
534 }
535
536 TEST_F(AnimatedWebPTests, randomFrameDecode)
537 {
538     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated.webp");
539     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated-opaque.webp");
540     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated-large.webp");
541     testRandomFrameDecode("/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp");
542 }
543
544 TEST_F(AnimatedWebPTests, randomDecodeAfterClearFrameBufferCache)
545 {
546     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated.webp");
547     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated-opaque.webp");
548     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated-large.webp");
549     testRandomDecodeAfterClearFrameBufferCache("/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp");
550 }
551
552 TEST_F(AnimatedWebPTests, resumePartialDecodeAfterClearFrameBufferCache)
553 {
554     RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/webp-animated-large.webp");
555     ASSERT_TRUE(fullData.get());
556     Vector<unsigned> baselineHashes;
557     createDecodingBaseline(fullData.get(), &baselineHashes);
558     size_t frameCount = baselineHashes.size();
559
560     OwnPtr<WEBPImageDecoder> decoder = createDecoder();
561
562     // Let frame 0 be partially decoded.
563     size_t partialSize = 1;
564     do {
565         RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), partialSize);
566         decoder->setData(data.get(), false);
567         ++partialSize;
568     } while (!decoder->frameCount() || decoder->frameBufferAtIndex(0)->status() == ImageFrame::FrameEmpty);
569
570     // Skip to the last frame and clear.
571     decoder->setData(fullData.get(), true);
572     EXPECT_EQ(frameCount, decoder->frameCount());
573     ImageFrame* lastFrame = decoder->frameBufferAtIndex(frameCount - 1);
574     EXPECT_EQ(baselineHashes[frameCount - 1], hashSkBitmap(lastFrame->getSkBitmap()));
575     decoder->clearCacheExceptFrame(kNotFound);
576
577     // Resume decoding of the first frame.
578     ImageFrame* firstFrame = decoder->frameBufferAtIndex(0);
579     EXPECT_EQ(ImageFrame::FrameComplete, firstFrame->status());
580     EXPECT_EQ(baselineHashes[0], hashSkBitmap(firstFrame->getSkBitmap()));
581 }
582
583 TEST_F(AnimatedWebPTests, decodeAfterReallocatingData)
584 {
585     testDecodeAfterReallocatingData("/LayoutTests/fast/images/resources/webp-animated.webp");
586     testDecodeAfterReallocatingData("/LayoutTests/fast/images/resources/webp-animated-icc-xmp.webp");
587 }
588
589 TEST(StaticWebPTests, truncatedImage)
590 {
591     // VP8 data is truncated.
592     testInvalidImage("/LayoutTests/fast/images/resources/truncated.webp", false);
593     // Chunk size in RIFF header doesn't match the file size.
594     testInvalidImage("/LayoutTests/fast/images/resources/truncated2.webp", true);
595 }