Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Implement disposal of frames Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "platform/image-decoders/png/PNGImageDecoder.h"
6
7 #include "png.h"
8 #include "platform/image-decoders/ImageDecoderTestHelpers.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include <memory>
11
12 // @TODO(joostouwerling) test decoding failure on frame |n|, and check if the
13 // decoder reports having |n-1| frames.
14 // @TODO(joostouwerling) verify IEND before IDAT fails the decode.
15
16 namespace blink {
17
18 namespace {
19
20 std::unique_ptr<ImageDecoder> createDecoder(ImageDecoder::AlphaOption alphaOptio n)
21 {
22 return wrapUnique(new PNGImageDecoder(alphaOption,
23 ImageDecoder::ColorSpaceApplied,
24 ImageDecoder::noDecodedImageByteLimit));
25 }
26
27 std::unique_ptr<ImageDecoder> createDecoder()
28 {
29 return createDecoder(ImageDecoder::AlphaNotPremultiplied);
30 }
31
32 std::unique_ptr<ImageDecoder> createDecoderWithPngData(const char* pngFile)
33 {
34 auto decoder = createDecoder();
35 auto data = readFile(pngFile);
36 EXPECT_FALSE(data->isEmpty());
37 decoder->setData(data.get(), true);
38 return decoder;
39 }
40
41 void testSize(const char* pngFile, IntSize expectedSize)
42 {
43 auto decoder = createDecoderWithPngData(pngFile);
44 EXPECT_TRUE(decoder->isSizeAvailable());
45 EXPECT_EQ(expectedSize, decoder->size());
46 }
47
48 void writeUint32(uint32_t val, png_byte* data)
49 {
50 data[0] = val >> 24;
51 data[1] = val >> 16;
52 data[2] = val >> 8;
53 data[3] = val;
54 }
55
56 void testRepetitionCount(const char* pngFile, int expectedRepetitionCount)
57 {
58 auto decoder = createDecoderWithPngData(pngFile);
59 // Decode frame count should see the number of repetitions as well.
60 decoder->frameCount();
61 EXPECT_FALSE(decoder->failed());
62 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount());
63 }
64
65 // Test whether querying for the size of the image works if we present the
66 // data byte by byte.
67 void testSizeByteByByte(const char *pngFile, size_t bytesNeededToDecodeSize,
68 IntSize expectedSize)
69 {
70 auto decoder = createDecoder();
71 auto data = readFile(pngFile);
72 ASSERT_FALSE(data->isEmpty());
73 ASSERT_LT(bytesNeededToDecodeSize, data->size());
74
75 for (size_t length = 1; length <= bytesNeededToDecodeSize; length++) {
76 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(),
77 length);
78 decoder->setData(tempData.get(), false);
79
80 if (length < bytesNeededToDecodeSize) {
81 EXPECT_FALSE(decoder->isSizeAvailable());
82 EXPECT_TRUE(decoder->size().isEmpty());
83 EXPECT_FALSE(decoder->failed());
84 } else {
85 EXPECT_TRUE(decoder->isSizeAvailable());
86 EXPECT_EQ(expectedSize, decoder->size());
87 }
88 }
89 EXPECT_FALSE(decoder->failed());
90 }
91
92 struct PublicFrameInfo {
93 size_t duration;
94 IntRect frameRect;
95 ImageFrame::AlphaBlendSource alphaBlend;
96 ImageFrame::DisposalMethod disposalMethod;
97 };
98
99 // This is the frame data for the following PNG image:
100 // /LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png
101 static PublicFrameInfo pngAnimatedFrameInfo[] = {
102 {500, {IntPoint(0, 0), IntSize(5, 5)}, ImageFrame::BlendAtopBgcolor,
103 ImageFrame::DisposeKeep},
104 {900, {IntPoint(1, 1), IntSize(3, 1)}, ImageFrame::BlendAtopBgcolor,
105 ImageFrame::DisposeOverwriteBgcolor},
106 {2000, {IntPoint(1, 2), IntSize(3, 2)}, ImageFrame::BlendAtopPreviousFrame,
107 ImageFrame::DisposeKeep},
108 {1500, {IntPoint(1, 2), IntSize(3, 1)}, ImageFrame::BlendAtopBgcolor,
109 ImageFrame::DisposeKeep},
110 };
111
112 void compareFrameWithExpectation(const PublicFrameInfo& expected,
113 const ImageFrame* frame)
114 {
115 EXPECT_EQ(expected.duration, frame->duration());
116 EXPECT_EQ(expected.frameRect, frame->originalFrameRect());
117 EXPECT_EQ(expected.disposalMethod, frame->getDisposalMethod());
118 EXPECT_EQ(expected.alphaBlend, frame->getAlphaBlendSource());
119 }
120
121 // This function removes |length| bytes at |offset|, and then calls frameCount.
122 // It assumes the missing bytes should result in a failed decode.
123 void testMissingDataBreaksDecoding(const char* pngFile, size_t offset,
124 size_t length)
125 {
126 auto decoder = createDecoder();
127 auto data = readFile(pngFile);
128 ASSERT_FALSE(data->isEmpty());
129
130 RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(),
131 offset);
132 invalidData->append(SharedBuffer::create(data->data() + offset + length,
133 data->size() - offset - length));
134 ASSERT_EQ(data->size() - length, invalidData->size());
135
136 decoder->setData(invalidData, true);
137 decoder->frameCount();
138 EXPECT_TRUE(decoder->failed());
139 }
140
141 // Decoding up to the indicated fcTL offset and then provide an fcTL with
142 // the wrong chunk size (20 instead of 26). It should break the decoder.
143 void testInvalidFctlSize(const char* pngFile, size_t offsetFctl,
144 size_t expectedFrameCountBeforeFail)
145 {
146 auto data = readFile(pngFile);
147 ASSERT_FALSE(data->isEmpty());
148
149 auto decoder = createDecoder();
150 RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(),
151 offsetFctl);
152
153 // Test if this gives the correct frame count, before the fcTL is parsed.
154 decoder->setData(invalidData, false);
155 EXPECT_EQ(expectedFrameCountBeforeFail, decoder->frameCount());
156 ASSERT_FALSE(decoder->failed());
157
158 // Append the wrong size to the data stream
159 png_byte sizeChunk[4];
160 writeUint32(20, sizeChunk);
161 invalidData->append(reinterpret_cast<char*>(sizeChunk), 4u);
162
163 // Skip the size in the original data, but provide the rest of the fcTL,
164 // which is 4B of tag, 26B of data and 4B of CRC, totalling 34B.
165 invalidData->append(data->data() + offsetFctl + 4, 34u);
166
167 decoder->setData(invalidData, false);
168 decoder->frameCount();
169 EXPECT_TRUE(decoder->failed());
170 }
171
172 void testDifferentActlFrameCountIsIgnored(const char* pngFile,
173 size_t offsetActl,
174 size_t injectedFrameCount,
175 size_t expectedFrameCount)
176 {
177 // First make sure that this tests makes sense.
178 ASSERT_NE(injectedFrameCount, expectedFrameCount);
179
180 auto data = readFile(pngFile);
181 auto decoder = createDecoder();
182 ASSERT_FALSE(data->isEmpty());
183
184 RefPtr<SharedBuffer> diffActlData = SharedBuffer::create(data->data(),
185 offsetActl + 8);
186 // Write the injectedFrameCount to the stream
187 png_byte sizeChunk[4];
188 writeUint32(injectedFrameCount, sizeChunk);
189 diffActlData->append(reinterpret_cast<char*>(sizeChunk), 4u);
190 // Append the rest of the data. The first |offsetActl + 12| bytes that are
191 // already in diffActlData should not be appended again.
192 diffActlData->append(data->data() + offsetActl + 12,
193 data->size() - offsetActl - 12);
194
195 decoder->setData(diffActlData, true);
196 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
197 }
198
199 // Test if the frame bitmap hashes of truncated decoding are equal to the
200 // hashes found by incremental decoding.
201 void testProgressiveDecoding(const char *pngFile)
202 {
203 RefPtr<SharedBuffer> fullData = readFile(pngFile);
204 ASSERT_TRUE(fullData.get());
205 const size_t fullLength = fullData->size();
206
207 std::unique_ptr<ImageDecoder> decoder;
208 ImageFrame* frame;
209
210 Vector<unsigned> truncatedHashes;
211 Vector<unsigned> progressiveHashes;
212
213 // Compute hashes when the file is truncated.
214 const size_t increment = 13;
215 for (size_t i = 1; i <= fullLength; i += increment) {
216 decoder = createDecoder();
217 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
218 decoder->setData(data.get(), i == fullLength);
219 size_t frameCount = decoder->frameCount();
220 ASSERT_FALSE(decoder->failed());
221 if (frameCount == 0) {
222 truncatedHashes.append(0);
223 continue;
224 }
225 frame = decoder->frameBufferAtIndex(frameCount - 1);
226 if (!frame) {
227 truncatedHashes.append(0);
228 continue;
229 }
230 truncatedHashes.append(hashBitmap(frame->bitmap()));
231 }
232
233 // Compute hashes when the file is progressively decoded.
234 decoder = createDecoder();
235 for (size_t i = 1; i <= fullLength; i += increment) {
236 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
237 decoder->setData(data.get(), i == fullLength);
238 ASSERT_FALSE(decoder->failed());
239 size_t frameCount = decoder->frameCount();
240 if (frameCount == 0) {
241 progressiveHashes.append(0);
242 continue;
243 }
244 frame = decoder->frameBufferAtIndex(frameCount - 1);
245 if (!frame) {
246 progressiveHashes.append(0);
247 continue;
248 }
249 progressiveHashes.append(hashBitmap(frame->bitmap()));
250 }
251
252 for (size_t i = 0; i < truncatedHashes.size(); ++i)
253 ASSERT_EQ(truncatedHashes[i], progressiveHashes[i]);
254 }
255
256 // Check that providing the full data after the first frame was partially
257 // decoded properly continues where it left off.
258 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile,
259 size_t offsetMidFirstFrame)
260 {
261 auto fullData = readFile(pngFile);
262 auto decoder = createDecoder();
263 ASSERT_FALSE(fullData->isEmpty());
264
265 RefPtr<SharedBuffer> partialData =
266 SharedBuffer::create(fullData->data(), offsetMidFirstFrame);
267 decoder->setData(partialData, false);
268
269 EXPECT_EQ(1u, decoder->frameCount());
270 ImageFrame* frame = decoder->frameBufferAtIndex(0);
271 EXPECT_EQ(frame->getStatus(), ImageFrame::FramePartial);
272 unsigned hashPartial = hashBitmap(frame->bitmap());
273
274 decoder->setData(fullData.get(), true);
275 frame = decoder->frameBufferAtIndex(0);
276 EXPECT_EQ(frame->getStatus(), ImageFrame::FrameComplete);
277 unsigned hashFull = hashBitmap(frame->bitmap());
278
279 EXPECT_FALSE(decoder->failed());
280 EXPECT_NE(hashFull, hashPartial);
281 }
282
283 // This test verifies that the frame buffer contents change when progressively
284 // decoding the first frame. It should change more than 1 time: once for the
285 // first data, and at least once more thereafter. If |offsetFirstFrameEnd| == 0,
286 // the test uses the full data size of the image for its value.
287 void testProgressiveDecodingChangesFrameBuffer(const char* pngFile,
288 size_t offsetFirstFrameEnd,
289 size_t step = 1u)
290 {
291 auto fullData = readFile(pngFile);
292 auto decoder = createDecoder();
293 ASSERT_FALSE(fullData->isEmpty());
294
295 if (offsetFirstFrameEnd == 0)
296 offsetFirstFrameEnd = fullData->size();
297
298 size_t numTimesBufferChanged = 0;
299 unsigned lastHash;
300
301 for (size_t length = 1; length <= offsetFirstFrameEnd; length += step) {
302 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(),
303 length);
304 decoder->setData(data, false);
305 ImageFrame* frame = decoder->frameBufferAtIndex(0);
306 if (!frame)
307 continue;
308 unsigned newHash = hashBitmap(frame->bitmap());
309 if (newHash != lastHash) {
310 lastHash = newHash;
311 numTimesBufferChanged++;
312 }
313 }
314 EXPECT_GT(numTimesBufferChanged, 1u);
315 }
316
317 // @TODO(joostouwerling) Pull this up to ImageDecoderTestHelper since it is also
318 // used in WEBPImageDecoderTest
319 void testRandomDecodeAfterClearFrameBufferCache(const char* pngFile) {
320 SCOPED_TRACE(pngFile);
321
322 RefPtr<SharedBuffer> data = readFile(pngFile);
323 ASSERT_TRUE(data.get());
324 Vector<unsigned> baselineHashes;
325 createDecodingBaseline(&createDecoder, data.get(), &baselineHashes);
326 size_t frameCount = baselineHashes.size();
327
328 std::unique_ptr<ImageDecoder> decoder = createDecoder();
329 decoder->setData(data.get(), true);
330 for (size_t clearExceptFrame = 0; clearExceptFrame < frameCount;
331 ++clearExceptFrame) {
332 decoder->clearCacheExceptFrame(clearExceptFrame);
333 const size_t skippingStep = 2;
334 for (size_t i = 0; i < skippingStep; ++i) {
335 for (size_t j = 0; j < frameCount; j += skippingStep) {
336 SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j);
337 ImageFrame* frame = decoder->frameBufferAtIndex(j);
338 EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap()));
339 }
340 }
341 }
342 }
343
344 } // Anonymous namespace
345
346 // Animated PNG Tests
347
348 TEST(AnimatedPNGTests, sizeTest)
349 {
350 testSize("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animati on.png", IntSize(5, 5));
351 testSize("/LayoutTests/fast/images/resources/png-animated-idat-not-part-of-ani mation.png", IntSize(227, 35));
352 }
353
354 TEST(AnimatedPNGTests, repetitionCountTest)
355 {
356 testRepetitionCount("/LayoutTests/fast/images/resources/png-animated-idat-part -of-animation.png", 7u);
357 // This is an "animated" image with only one frame, that is, the IDAT is
358 // ignored and there is one fdAT frame. so it should be considered
359 // non-animated.
360 testRepetitionCount("/LayoutTests/fast/images/resources/png-animated-idat-not- part-of-animation.png", cAnimationNone);
361 }
362
363 // Test if the decoded metdata corresponds to the defined expectations
364 TEST(AnimatedPNGTests, MetaDataTest)
365 {
366 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa rt-of-animation.png";
367 constexpr size_t expectedFrameCount = 4;
368
369 auto decoder = createDecoderWithPngData(pngFile);
370 ASSERT_EQ(expectedFrameCount, decoder->frameCount());
371 for (size_t i = 0; i < expectedFrameCount; i++)
372 compareFrameWithExpectation(pngAnimatedFrameInfo[i],
373 decoder->frameBufferAtIndex(i));
374 }
375
376 TEST(AnimatedPNGTests, ByteByByteSizeAvailable)
377 {
378 testSizeByteByByte("/LayoutTests/fast/images/resources/png-animated-idat-part- of-animation.png",
379 141u, IntSize(5, 5));
380 testSizeByteByByte("/LayoutTests/fast/images/resources/png-animated-idat-not-p art-of-animation.png",
381 79u, IntSize(227, 35));
382 }
383
384 // Test whether the frame metadata decoding also works when we provide the data
385 // byte by byte. This should cover the case when the client does not provide
386 // all data at once. At given offsets, we expect frames to become available.
387 // This test checks whether that is the case, and if so, if the frame data is
388 // equal to what we expected.
389 TEST(AnimatedPNGTests, ByteByByteMetaData)
390 {
391 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa rt-of-animation.png";
392 constexpr size_t expectedFrameCount = 4;
393
394 // These are the byte offsets where each frame should have been parsed.
395 // It boils down to the offset of the first fcTL / IEND after the last
396 // frame data chunk, plus 8 bytes for recognition. The exception on this is
397 // the first frame, which is reported when its first framedata is seen.
398 size_t frameOffsets[expectedFrameCount] = {141, 249, 322, 430};
399
400 auto decoder = createDecoder();
401 auto data = readFile(pngFile);
402 ASSERT_FALSE(data->isEmpty());
403 size_t framesParsed = 0;
404
405 for (size_t length = 1; length <= frameOffsets[expectedFrameCount - 1]; length ++) {
406 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(),
407 length);
408 decoder->setData(tempData.get(), false);
409 EXPECT_FALSE(decoder->failed());
410 if (length < frameOffsets[framesParsed]) {
411 EXPECT_EQ(framesParsed, decoder->frameCount());
412 } else {
413 ASSERT_EQ(framesParsed + 1, decoder->frameCount());
414 compareFrameWithExpectation(pngAnimatedFrameInfo[framesParsed],
415 decoder->frameBufferAtIndex(framesParsed));
416 framesParsed++;
417 }
418 }
419 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
420 EXPECT_FALSE(decoder->failed());
421 }
422
423 // This tests if the frame count gets set correctly when parsing frameCount
424 // fails in one of the parsing queries.
425 //
426 // First, enough data is provided such that two frames should be registered.
427 // The decoder should at this point not be in the failed status.
428 //
429 // Then, we provide the rest of the data except for the last IEND chunk, but
430 // tell the decoder that this is all the data we have. Now, the decoder should
431 // be in the failed state since all data is provided but no IEND chunk has been
432 // seen. The frame count should be three, since one extra frame should be
433 // discovered. The fourth frame should *not* be registered since the reader
434 // should not be able to determine where the frame ends.
435 TEST(AnimatedPNGTests, FrameCountWithTruncatedData)
436 {
437 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa rt-of-animation.png";
438 auto decoder = createDecoder();
439 auto data = readFile(pngFile);
440 ASSERT_FALSE(data->isEmpty());
441
442 // Parse up to and including the first two frames
443 const size_t offsetTwoFrames = 249;
444 const size_t expectedFramesAfter249Bytes = 2;
445 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(),
446 offsetTwoFrames);
447 decoder->setData(tempData.get(), false);
448 EXPECT_EQ(expectedFramesAfter249Bytes, decoder->frameCount());
449 EXPECT_FALSE(decoder->failed());
450
451 // Provide the rest of the data except for the last IEND chunk.
452 const size_t expectedFramesAfterAllExpect12Bytes = 3;
453 tempData = SharedBuffer::create(data->data(), data->size() - 12);
454 decoder->setData(tempData.get(), true);
455 EXPECT_EQ(expectedFramesAfterAllExpect12Bytes, decoder->frameCount());
456 EXPECT_TRUE(decoder->failed());
457 }
458
459 TEST(AnimatedPNGTests, TestRandomFrameDecode)
460 {
461 RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/p ng-animated-idat-part-of-animation.png");
462 ASSERT_TRUE(fullData.get());
463 Vector<unsigned> baselineHashes;
464 createDecodingBaseline(&createDecoder, fullData.get(), &baselineHashes);
465 size_t frameCount = baselineHashes.size();
466
467 // Random decoding should get the same results as sequential decoding.
468 std::unique_ptr<ImageDecoder> decoder = createDecoder();
469 decoder->setData(fullData.get(), true);
470 const size_t skippingStep = 2;
471 for (size_t i = 0; i < skippingStep; ++i) {
472 for (size_t j = i; j < frameCount; j += skippingStep) {
473 SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j);
474 ImageFrame* frame = decoder->frameBufferAtIndex(j);
475 EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap()));
476 }
477 }
478
479 // Decoding in reverse order.
480 decoder = createDecoder();
481 decoder->setData(fullData.get(), true);
482 for (size_t i = frameCount; i; --i) {
483 SCOPED_TRACE(testing::Message() << "Reverse i:" << i);
484 ImageFrame* frame = decoder->frameBufferAtIndex(i - 1);
485 EXPECT_EQ(baselineHashes[i - 1], hashBitmap(frame->bitmap()));
486 }
487
488 }
489
490 TEST(AnimatedPNGTests, TestDecodeAfterReallocation)
491 {
492 std::unique_ptr<ImageDecoder> decoder = createDecoder();
493 RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/png-a nimated-idat-part-of-animation.png");
494 ASSERT_TRUE(data.get());
495
496 // Parse from |data|.
497 decoder->setData(data.get(), true);
498 size_t frameCount = decoder->frameCount();
499
500 // ... and then decode frames from |reallocatedData|.
501 RefPtr<SharedBuffer> reallocatedData = data.get()->copy();
502 ASSERT_TRUE(reallocatedData.get());
503 data.clear();
504 decoder->setData(reallocatedData.get(), true);
505
506 for (size_t i = 0; i < frameCount; ++i) {
507 const ImageFrame* const frame = decoder->frameBufferAtIndex(i);
508 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus());
509 }
510 }
511
512 TEST(AnimatedPNGTests, ProgressiveDecode)
513 {
514 testProgressiveDecoding("/LayoutTests/fast/images/resources/png-animated-idat- part-of-animation.png");
515 }
516
517 TEST(AnimatedPNGTests, ParseAndDecodeByteByByte)
518 {
519 testByteByByteDecode(&createDecoder,
520 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-anima tion.png",
521 4u, 7u);
522 }
523
524 TEST(AnimatedPNGTests, FctlWrongSizeBreaksDecoding)
525 {
526 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa rt-of-animation.png";
527 auto data = readFile(pngFile);
528 ASSERT_FALSE(data->isEmpty());
529
530 // Test the first fcTL in the stream. Because no frame data has been set
531 // at this point, the expected frame count is zero.
532 testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part -of-animation.png",
533 95u, 0u);
534
535 // Test for the third fcTL in the stream. This should be tested as well,
536 // since the first fcTL is parsed in PNGImageReader::parseSize() whereas
537 // later fcTLs are parsed in PNGImageReader::parse() as part of framecount.
538 // The expected frame count before the fcTL chunk is 1u, since the second
539 // frame is registered when the third fcTL (or IEND) is seen.
540 testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part -of-animation.png",
541 241u, 1u);
542
543 }
544
545 TEST(AnimatedPNGTests, MissingFctlDataBreaksDecoding)
546 {
547 // The fcTL chunk starts at 95u, add 10u to get to the content data, and
548 // remove 5 bytes of data.
549 testMissingDataBreaksDecoding(
550 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png" ,
551 105u, 5u);
552 }
553
554 TEST(AnimatedPNGTests, MissingActlResultsInNonAnimated)
555 {
556 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa rt-of-animation.png";
557 auto data = readFile(pngFile);
558 auto decoder = createDecoder();
559 ASSERT_FALSE(data->isEmpty());
560
561 // Remove the acTL chunk from the stream.
562 size_t offsetActl = 33;
563 RefPtr<SharedBuffer> noActlData = SharedBuffer::create(data->data(),
564 offsetActl);
565 noActlData->append(data->data() + offsetActl + 20,
566 data->size() - offsetActl - 20);
567
568 decoder->setData(noActlData, true);
569 EXPECT_EQ(1u, decoder->frameCount());
570 EXPECT_FALSE(decoder->failed());
571 }
572
573 // Test if the indicated frame count by the acTL is ignored if the actual
574 // number of frames is different. Test for higher and lower indicated number.
575 TEST(AnimatedPNGTests, differentActlFrameCountIsIgnored)
576 {
577 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa rt-of-animation.png";
578 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 2u, 4u);
579 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 8u, 4u);
580 }
581
582 // Check if a frame rectangle, that is larger than the image width, gets
583 // clipped correctly.
584 // @TODO(joostouwerling) use an image that also has data that needs to be
585 // clipped, not just report larger dimensions.
586 // @TODO(joostouwerling) determine how libpng handles larger / smaller images
587 // than indicated.
588 TEST(AnimatedPNGTests, frameRectIsClipped)
589 {
590 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa rt-of-animation.png";
591 auto data = readFile(pngFile);
592 auto decoder = createDecoder();
593 ASSERT_FALSE(data->isEmpty());
594
595 // Change the width and height of the frame so it falls outside the image.
596 size_t offsetThirdFctl = 241 + 12;
597 RefPtr<SharedBuffer> modifiedData = SharedBuffer::create(data->data(),
598 offsetThirdFctl);
599 png_byte sizeChunk[8];
600 writeUint32(10, sizeChunk);
601 writeUint32(15, sizeChunk + 4);
602 modifiedData->append(const_cast<const char*>(
603 reinterpret_cast<char*>(sizeChunk)), 8u);
604 modifiedData->append(data->data() + offsetThirdFctl + 8,
605 data->size() - offsetThirdFctl - 8);
606
607 decoder->setData(modifiedData, true);
608
609 IntSize expectedSize(5, 5);
610 size_t expectedFrameCount = 4;
611 IntRect expectedFrameRect(IntPoint(1, 2), IntSize(4, 3));
612
613 EXPECT_TRUE(decoder->isSizeAvailable());
614 EXPECT_EQ(expectedSize, decoder->size());
615 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
616 ASSERT_FALSE(decoder->failed());
617 EXPECT_EQ(expectedFrameRect,
618 decoder->frameBufferAtIndex(2)->originalFrameRect());
619 }
620
621 TEST(AnimatedPNGTests, ProgressiveDecodingChangesFrameBuffer)
622 {
623 testProgressiveDecodingChangesFrameBuffer(
624 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png" , 249u);
625 }
626
627 TEST(AnimatedPNGTests, ProgressiveDecodingContinuesAfterFullData)
628 {
629 testProgressiveDecodingContinuesAfterFullData(
630 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png" , 160u);
631 }
632
633 TEST(AnimatedPNGTests, RandomDecodeAfterClearFrameBufferCache)
634 {
635 testRandomDecodeAfterClearFrameBufferCache(
636 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png" );
637 }
638
639 // Static PNG tests
640
641 TEST(StaticPNGTests, repetitionCountTest)
642 {
643 testRepetitionCount("/LayoutTests/fast/images/resources/png-simple.png",
644 cAnimationNone);
645 }
646
647 TEST(StaticPNGTests, sizeTest)
648 {
649 testSize("/LayoutTests/fast/images/resources/png-simple.png",
650 IntSize(111, 29));
651 }
652
653 TEST(StaticPNGTests, MetaDataTest)
654 {
655 const size_t expectedFrameCount = 1;
656 const size_t expectedDuration = 0;
657 auto decoder = createDecoderWithPngData("/LayoutTests/fast/images/resources/pn g-simple.png");
658 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
659 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0));
660 }
661
662 TEST(StaticPNGTests, InvalidIHDRChunk)
663 {
664 testMissingDataBreaksDecoding(
665 "/LayoutTests/fast/images/resources/png-simple.png", 20u, 2u);
666 }
667
668 TEST(StaticPNGTests, ProgressiveDecoding)
669 {
670 testProgressiveDecoding("/LayoutTests/fast/images/resources/png-simple.png");
671 }
672
673 TEST(StaticPNGTests, ProgressiveDecodingChangesFrameBuffer)
674 {
675 testProgressiveDecodingChangesFrameBuffer(
676 "/LayoutTests/fast/images/resources/png-simple.png", 0u, 5u);
677 }
678
679 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData)
680 {
681 testProgressiveDecodingContinuesAfterFullData(
682 "/LayoutTests/fast/images/resources/png-simple.png", 1000u);
683 }
684
685 }; // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698