Index: third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp |
diff --git a/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..61af8ae6049e70fda74f181ea6fe2ae08e460231 |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp |
@@ -0,0 +1,959 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "platform/image-decoders/png/PNGImageDecoder.h" |
+ |
+#include "png.h" |
+#include "platform/image-decoders/ImageDecoderTestHelpers.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include <memory> |
+ |
+// The image /LT/f/i/r/png-animated-idat-part-of-animation.png is modified in |
+// multiple tests to simulate erroneous PNGs. As a reference, the table below |
+// shows how the file is structured. |
+// |
+// Offset | 8 33 95 133 172 210 241 279 314 352 422 |
+// ------------------------------------------------------------------------- |
+// Chunk | IHDR acTL fcTL IDAT fcTL fdAT fcTL fdAT fcTL fdAT IEND |
+// |
+// In between the acTL and fcTL there are two other chunks, PLTE and tRNS, but |
+// those are not specifically used in this test suite. The same holds for a |
+// tEXT chunk in between the last fdAT and IEND. |
+// |
+// In the current behavior of PNG image decoders, the 4 frames are detected when |
+// respectively 141, 249, 322 and 430 bytes are received. The first frame should |
+// be detected when the IDAT has been received, and non-first frames when the |
+// next fcTL or IEND chunk has been received. Note that all offsets are +8, |
+// because a chunk is identified by byte 4-7. |
+ |
+// @TODO(joostouwerling) extend test image set with other image encodings, such |
+// as first frame fcTL and multiple chunks per frame. |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+std::unique_ptr<ImageDecoder> createDecoder(ImageDecoder::AlphaOption alphaOption) |
+{ |
+ return wrapUnique(new PNGImageDecoder(alphaOption, |
+ ImageDecoder::ColorSpaceApplied, |
+ ImageDecoder::noDecodedImageByteLimit)); |
+} |
+ |
+std::unique_ptr<ImageDecoder> createDecoder() |
+{ |
+ return createDecoder(ImageDecoder::AlphaNotPremultiplied); |
+} |
+ |
+std::unique_ptr<ImageDecoder> createDecoderWithPngData(const char* pngFile) |
+{ |
+ auto decoder = createDecoder(); |
+ auto data = readFile(pngFile); |
+ EXPECT_FALSE(data->isEmpty()); |
+ decoder->setData(data.get(), true); |
+ return decoder; |
+} |
+ |
+void testSize(const char* pngFile, IntSize expectedSize) |
+{ |
+ auto decoder = createDecoderWithPngData(pngFile); |
+ EXPECT_TRUE(decoder->isSizeAvailable()); |
+ EXPECT_EQ(expectedSize, decoder->size()); |
+} |
+ |
+void writeUint32(uint32_t val, png_byte* data) |
+{ |
+ data[0] = val >> 24; |
+ data[1] = val >> 16; |
+ data[2] = val >> 8; |
+ data[3] = val; |
+} |
+ |
+void testRepetitionCount(const char* pngFile, int expectedRepetitionCount) |
+{ |
+ auto decoder = createDecoderWithPngData(pngFile); |
+ // Decode frame count should see the number of repetitions as well. |
+ decoder->frameCount(); |
+ EXPECT_FALSE(decoder->failed()); |
+ EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount()); |
+} |
+ |
+// Test whether querying for the size of the image works if we present the |
+// data byte by byte. |
+void testSizeByteByByte(const char *pngFile, size_t bytesNeededToDecodeSize, |
+ IntSize expectedSize) |
+{ |
+ auto decoder = createDecoder(); |
+ auto data = readFile(pngFile); |
+ ASSERT_FALSE(data->isEmpty()); |
+ ASSERT_LT(bytesNeededToDecodeSize, data->size()); |
+ |
+ for (size_t length = 1; length <= bytesNeededToDecodeSize; length++) { |
+ RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), |
+ length); |
+ decoder->setData(tempData.get(), false); |
+ |
+ if (length < bytesNeededToDecodeSize) { |
+ EXPECT_FALSE(decoder->isSizeAvailable()); |
+ EXPECT_TRUE(decoder->size().isEmpty()); |
+ EXPECT_FALSE(decoder->failed()); |
+ } else { |
+ EXPECT_TRUE(decoder->isSizeAvailable()); |
+ EXPECT_EQ(expectedSize, decoder->size()); |
+ } |
+ } |
+ EXPECT_FALSE(decoder->failed()); |
+} |
+ |
+struct PublicFrameInfo { |
+ size_t duration; |
+ IntRect frameRect; |
+ ImageFrame::AlphaBlendSource alphaBlend; |
+ ImageFrame::DisposalMethod disposalMethod; |
+}; |
+ |
+// This is the frame data for the following PNG image: |
+// /LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png |
+static PublicFrameInfo pngAnimatedFrameInfo[] = { |
+ {500, {IntPoint(0, 0), IntSize(5, 5)}, ImageFrame::BlendAtopBgcolor, |
+ ImageFrame::DisposeKeep}, |
+ {900, {IntPoint(1, 1), IntSize(3, 1)}, ImageFrame::BlendAtopBgcolor, |
+ ImageFrame::DisposeOverwriteBgcolor}, |
+ {2000, {IntPoint(1, 2), IntSize(3, 2)}, ImageFrame::BlendAtopPreviousFrame, |
+ ImageFrame::DisposeKeep}, |
+ {1500, {IntPoint(1, 2), IntSize(3, 1)}, ImageFrame::BlendAtopBgcolor, |
+ ImageFrame::DisposeKeep}, |
+}; |
+ |
+void compareFrameWithExpectation(const PublicFrameInfo& expected, |
+ const ImageFrame* frame) |
+{ |
+ EXPECT_EQ(expected.duration, frame->duration()); |
+ EXPECT_EQ(expected.frameRect, frame->originalFrameRect()); |
+ EXPECT_EQ(expected.disposalMethod, frame->getDisposalMethod()); |
+ EXPECT_EQ(expected.alphaBlend, frame->getAlphaBlendSource()); |
+} |
+ |
+// This function removes |length| bytes at |offset|, and then calls frameCount. |
+// It assumes the missing bytes should result in a failed decode because the |
+// parser jumps |length| bytes too far in the next chunk. |
+void testMissingDataBreaksDecoding(const char* pngFile, size_t offset, |
+ size_t length) |
+{ |
+ auto decoder = createDecoder(); |
+ auto data = readFile(pngFile); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(), |
+ offset); |
+ invalidData->append(SharedBuffer::create(data->data() + offset + length, |
+ data->size() - offset - length)); |
+ ASSERT_EQ(data->size() - length, invalidData->size()); |
+ |
+ decoder->setData(invalidData, true); |
+ decoder->frameCount(); |
+ EXPECT_TRUE(decoder->failed()); |
+} |
+ |
+// Decoding up to the indicated fcTL offset and then provide an fcTL with |
+// the wrong chunk size (20 instead of 26). It should break the decoder. |
+void testInvalidFctlSize(const char* pngFile, |
+ size_t offsetFctl, |
+ size_t expectedFrameCountBeforeFail, |
+ bool expectedDecoderFailure) |
+{ |
+ auto data = readFile(pngFile); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ auto decoder = createDecoder(); |
+ RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(), |
+ offsetFctl); |
+ |
+ // Test if this gives the correct frame count, before the fcTL is parsed. |
+ decoder->setData(invalidData, false); |
+ EXPECT_EQ(expectedFrameCountBeforeFail, decoder->frameCount()); |
+ ASSERT_FALSE(decoder->failed()); |
+ |
+ // Append the wrong size to the data stream |
+ png_byte sizeChunk[4]; |
+ writeUint32(20, sizeChunk); |
+ invalidData->append(reinterpret_cast<char*>(sizeChunk), 4u); |
+ |
+ // Skip the size in the original data, but provide the rest of the fcTL, |
+ // which is 4B of tag, 26B of data and 4B of CRC, totalling 34B. |
+ invalidData->append(data->data() + offsetFctl + 4, 34u); |
+ |
+ decoder->setData(invalidData, false); |
+ decoder->frameCount(); |
+ EXPECT_EQ(expectedDecoderFailure, decoder->failed()); |
+} |
+ |
+void testDifferentActlFrameCountIsIgnored(const char* pngFile, |
+ size_t offsetActl, |
+ size_t injectedFrameCount, |
+ size_t expectedFrameCount) |
+{ |
+ // First make sure that this tests makes sense. |
+ ASSERT_NE(injectedFrameCount, expectedFrameCount); |
+ |
+ auto data = readFile(pngFile); |
+ auto decoder = createDecoder(); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ RefPtr<SharedBuffer> diffActlData = SharedBuffer::create(data->data(), |
+ offsetActl + 8); |
+ // Write the injectedFrameCount to the stream |
+ png_byte sizeChunk[4]; |
+ writeUint32(injectedFrameCount, sizeChunk); |
+ diffActlData->append(reinterpret_cast<char*>(sizeChunk), 4u); |
+ // Append the rest of the data. The first |offsetActl + 12| bytes that are |
+ // already in diffActlData should not be appended again. |
+ diffActlData->append(data->data() + offsetActl + 12, |
+ data->size() - offsetActl - 12); |
+ |
+ decoder->setData(diffActlData, true); |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+} |
+ |
+// Test if the frame bitmap hashes of truncated decoding are equal to the |
+// hashes found by incremental decoding. |
+void testProgressiveDecoding(const char *pngFile) |
+{ |
+ RefPtr<SharedBuffer> fullData = readFile(pngFile); |
+ ASSERT_TRUE(fullData.get()); |
+ const size_t fullLength = fullData->size(); |
+ |
+ std::unique_ptr<ImageDecoder> decoder; |
+ ImageFrame* frame; |
+ |
+ Vector<unsigned> truncatedHashes; |
+ Vector<unsigned> progressiveHashes; |
+ |
+ // Compute hashes when the file is truncated. |
+ const size_t increment = 13; |
+ for (size_t i = 1; i <= fullLength; i += increment) { |
+ decoder = createDecoder(); |
+ RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i); |
+ decoder->setData(data.get(), i == fullLength); |
+ size_t frameCount = decoder->frameCount(); |
+ ASSERT_FALSE(decoder->failed()); |
+ if (frameCount == 0) { |
+ truncatedHashes.append(0); |
+ continue; |
+ } |
+ frame = decoder->frameBufferAtIndex(frameCount - 1); |
+ if (!frame) { |
+ truncatedHashes.append(0); |
+ continue; |
+ } |
+ truncatedHashes.append(hashBitmap(frame->bitmap())); |
+ } |
+ |
+ // Compute hashes when the file is progressively decoded. |
+ decoder = createDecoder(); |
+ for (size_t i = 1; i <= fullLength; i += increment) { |
+ RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i); |
+ decoder->setData(data.get(), i == fullLength); |
+ ASSERT_FALSE(decoder->failed()); |
+ size_t frameCount = decoder->frameCount(); |
+ if (frameCount == 0) { |
+ progressiveHashes.append(0); |
+ continue; |
+ } |
+ frame = decoder->frameBufferAtIndex(frameCount - 1); |
+ if (!frame) { |
+ progressiveHashes.append(0); |
+ continue; |
+ } |
+ progressiveHashes.append(hashBitmap(frame->bitmap())); |
+ } |
+ |
+ for (size_t i = 0; i < truncatedHashes.size(); ++i) |
+ ASSERT_EQ(truncatedHashes[i], progressiveHashes[i]); |
+} |
+ |
+// Check that providing the full data after the first frame was partially |
+// decoded properly continues where it left off. |
+void testProgressiveDecodingContinuesAfterFullData(const char* pngFile, |
+ size_t offsetMidFirstFrame) |
+{ |
+ auto fullData = readFile(pngFile); |
+ auto decoder = createDecoder(); |
+ ASSERT_FALSE(fullData->isEmpty()); |
+ |
+ RefPtr<SharedBuffer> partialData = |
+ SharedBuffer::create(fullData->data(), offsetMidFirstFrame); |
+ decoder->setData(partialData, false); |
+ |
+ EXPECT_EQ(1u, decoder->frameCount()); |
+ ImageFrame* frame = decoder->frameBufferAtIndex(0); |
+ EXPECT_EQ(frame->getStatus(), ImageFrame::FramePartial); |
+ unsigned hashPartial = hashBitmap(frame->bitmap()); |
+ |
+ decoder->setData(fullData.get(), true); |
+ frame = decoder->frameBufferAtIndex(0); |
+ EXPECT_EQ(frame->getStatus(), ImageFrame::FrameComplete); |
+ unsigned hashFull = hashBitmap(frame->bitmap()); |
+ |
+ EXPECT_FALSE(decoder->failed()); |
+ EXPECT_NE(hashFull, hashPartial); |
+} |
+ |
+// This test verifies that the frame buffer contents change when progressively |
+// decoding the first frame. It should change more than 1 time: once for the |
+// first data, and at least once more thereafter. If |offsetFirstFrameEnd| == 0, |
+// the test uses the full data size of the image for its value. |
+void testProgressiveDecodingChangesFrameBuffer(const char* pngFile, |
+ size_t offsetFirstFrameEnd, |
+ size_t step = 1u) |
+{ |
+ auto fullData = readFile(pngFile); |
+ auto decoder = createDecoder(); |
+ ASSERT_FALSE(fullData->isEmpty()); |
+ |
+ if (offsetFirstFrameEnd == 0) |
+ offsetFirstFrameEnd = fullData->size(); |
+ |
+ size_t numTimesBufferChanged = 0; |
+ unsigned lastHash; |
+ |
+ for (size_t length = 1; length <= offsetFirstFrameEnd; length += step) { |
+ RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), |
+ length); |
+ decoder->setData(data, false); |
+ ImageFrame* frame = decoder->frameBufferAtIndex(0); |
+ if (!frame) |
+ continue; |
+ unsigned newHash = hashBitmap(frame->bitmap()); |
+ if (newHash != lastHash) { |
+ lastHash = newHash; |
+ numTimesBufferChanged++; |
+ } |
+ } |
+ EXPECT_GT(numTimesBufferChanged, 1u); |
+} |
+ |
+// @TODO(joostouwerling) Pull this up to ImageDecoderTestHelper since it is also |
+// used in WEBPImageDecoderTest |
+void testRandomDecodeAfterClearFrameBufferCache(const char* pngFile) { |
+ SCOPED_TRACE(pngFile); |
+ |
+ RefPtr<SharedBuffer> data = readFile(pngFile); |
+ ASSERT_TRUE(data.get()); |
+ Vector<unsigned> baselineHashes; |
+ createDecodingBaseline(&createDecoder, data.get(), &baselineHashes); |
+ const size_t frameCount = baselineHashes.size(); |
+ |
+ std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
+ decoder->setData(data.get(), true); |
+ for (size_t clearExceptFrame = 0; clearExceptFrame < frameCount; |
+ ++clearExceptFrame) { |
+ decoder->clearCacheExceptFrame(clearExceptFrame); |
+ const size_t skippingStep = 2; |
+ for (size_t i = 0; i < skippingStep; ++i) { |
+ for (size_t j = 0; j < frameCount; j += skippingStep) { |
+ SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j); |
+ ImageFrame* frame = decoder->frameBufferAtIndex(j); |
+ EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap())); |
+ } |
+ } |
+ } |
+} |
+ |
+uint32_t premultiplyColor(uint32_t c) { |
+ return SkPremultiplyARGBInline(SkGetPackedA32(c), SkGetPackedR32(c), |
+ SkGetPackedG32(c), SkGetPackedB32(c)); |
+} |
+ |
+void verifyFramesMatch(const char* pngFile, |
+ const ImageFrame* const a, |
+ ImageFrame* const b) { |
+ const SkBitmap& bitmapA = a->bitmap(); |
+ const SkBitmap& bitmapB = b->bitmap(); |
+ ASSERT_EQ(bitmapA.width(), bitmapB.width()); |
+ ASSERT_EQ(bitmapA.height(), bitmapB.height()); |
+ |
+ int maxDifference = 0; |
+ for (int y = 0; y < bitmapA.height(); ++y) { |
+ for (int x = 0; x < bitmapA.width(); ++x) { |
+ uint32_t colorA = *bitmapA.getAddr32(x, y); |
+ if (!a->premultiplyAlpha()) |
+ colorA = premultiplyColor(colorA); |
+ uint32_t colorB = *bitmapB.getAddr32(x, y); |
+ if (!b->premultiplyAlpha()) |
+ colorB = premultiplyColor(colorB); |
+ uint8_t* pixelA = reinterpret_cast<uint8_t*>(&colorA); |
+ uint8_t* pixelB = reinterpret_cast<uint8_t*>(&colorB); |
+ for (int channel = 0; channel < 4; ++channel) { |
+ const int difference = abs(pixelA[channel] - pixelB[channel]); |
+ if (difference > maxDifference) |
+ maxDifference = difference; |
+ } |
+ } |
+ } |
+ |
+ // Pre-multiplication could round the RGBA channel values. So, we declare |
+ // that the frames match if the RGBA channel values differ by at most 2. |
+ EXPECT_GE(2, maxDifference) << pngFile; |
+} |
+ |
+// Verifies that result of alpha blending is similar for AlphaPremultiplied and |
+// AlphaNotPremultiplied cases. |
+void testAlphaBlending(const char* pngFile) { |
+ RefPtr<SharedBuffer> data = readFile(pngFile); |
+ ASSERT_TRUE(data.get()); |
+ |
+ std::unique_ptr<ImageDecoder> decoderA = |
+ createDecoder(ImageDecoder::AlphaPremultiplied); |
+ decoderA->setData(data.get(), true); |
+ |
+ std::unique_ptr<ImageDecoder> decoderB = |
+ createDecoder(ImageDecoder::AlphaNotPremultiplied); |
+ decoderB->setData(data.get(), true); |
+ |
+ size_t frameCount = decoderA->frameCount(); |
+ ASSERT_EQ(frameCount, decoderB->frameCount()); |
+ |
+ for (size_t i = 0; i < frameCount; ++i) |
+ verifyFramesMatch(pngFile, decoderA->frameBufferAtIndex(i), |
+ decoderB->frameBufferAtIndex(i)); |
+} |
+ |
+// Modify the frame data bytes for frame |frameIndex| so that decoding fails. |
+// Parsing should work fine, and is checked with |expectedFrameCountBefore|. If |
+// the failure should invalidate the decoder, |expectFailure| should be set to |
+// true. If not, |expectedFrameCountAfter| should indicate the new frame count |
+// after the failure. |
+void testFailureDuringDecode(const char *file, |
+ size_t idatOffset, |
+ size_t frameIndex, |
+ bool expectFailure, |
+ size_t expectedFrameCountBefore, |
+ size_t expectedFrameCountAfter = 0u) |
+{ |
+ RefPtr<SharedBuffer> fullData = readFile(file); |
+ ASSERT_FALSE(fullData->isEmpty()); |
+ |
+ // This is the offset where the frame data chunk frame |frameIndex| starts. |
+ RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), |
+ idatOffset + 8u); |
+ // Repeat the first 8 bytes of the frame data. This should result in a |
+ // succesfull parse, since frame data is not analyzed in that step, but |
+ // should give an error during decoding. |
+ data->append(fullData->data() + idatOffset, 8u); |
+ data->append(fullData->data() + idatOffset + 16u, |
+ fullData->size() - idatOffset - 16u); |
+ |
+ auto decoder = createDecoder(); |
+ decoder->setData(data.get(), true); |
+ |
+ EXPECT_EQ(expectedFrameCountBefore, decoder->frameCount()); |
+ |
+ const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex); |
+ EXPECT_EQ(expectFailure, decoder->failed()); |
+ if (!expectFailure) { |
+ EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount()); |
+ EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus()); |
+ } |
+} |
+ |
+} // Anonymous namespace |
+ |
+// Animated PNG Tests |
+ |
+TEST(AnimatedPNGTests, sizeTest) |
+{ |
+ testSize("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", IntSize(5, 5)); |
+ testSize("/LayoutTests/fast/images/resources/png-animated-idat-not-part-of-animation.png", IntSize(227, 35)); |
+} |
+ |
+TEST(AnimatedPNGTests, repetitionCountTest) |
+{ |
+ testRepetitionCount("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", 7u); |
+ // This is an "animated" image with only one frame, that is, the IDAT is |
+ // ignored and there is one fdAT frame. so it should be considered |
+ // non-animated. |
+ testRepetitionCount("/LayoutTests/fast/images/resources/png-animated-idat-not-part-of-animation.png", cAnimationNone); |
+} |
+ |
+// Test if the decoded metdata corresponds to the defined expectations |
+TEST(AnimatedPNGTests, MetaDataTest) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ constexpr size_t expectedFrameCount = 4; |
+ |
+ auto decoder = createDecoderWithPngData(pngFile); |
+ ASSERT_EQ(expectedFrameCount, decoder->frameCount()); |
+ for (size_t i = 0; i < expectedFrameCount; i++) |
+ compareFrameWithExpectation(pngAnimatedFrameInfo[i], |
+ decoder->frameBufferAtIndex(i)); |
+} |
+ |
+TEST(AnimatedPNGTests, ByteByByteSizeAvailable) |
+{ |
+ testSizeByteByByte("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
+ 141u, IntSize(5, 5)); |
+ testSizeByteByByte("/LayoutTests/fast/images/resources/png-animated-idat-not-part-of-animation.png", |
+ 79u, IntSize(227, 35)); |
+} |
+ |
+// Test whether the frame metadata decoding also works when we provide the data |
+// byte by byte. This should cover the case when the client does not provide |
+// all data at once. At given offsets, we expect frames to become available. |
+// This test checks whether that is the case, and if so, if the frame data is |
+// equal to what we expected. |
+TEST(AnimatedPNGTests, ByteByByteMetaData) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ constexpr size_t expectedFrameCount = 4; |
+ |
+ // These are the byte offsets where each frame should have been parsed. |
+ // It boils down to the offset of the first fcTL / IEND after the last |
+ // frame data chunk, plus 8 bytes for recognition. The exception on this is |
+ // the first frame, which is reported when its first framedata is seen. |
+ size_t frameOffsets[expectedFrameCount] = {141, 249, 322, 430}; |
+ |
+ auto decoder = createDecoder(); |
+ auto data = readFile(pngFile); |
+ ASSERT_FALSE(data->isEmpty()); |
+ size_t framesParsed = 0; |
+ |
+ for (size_t length = 1; length <= frameOffsets[expectedFrameCount - 1]; length++) { |
+ RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), |
+ length); |
+ decoder->setData(tempData.get(), false); |
+ EXPECT_FALSE(decoder->failed()); |
+ if (length < frameOffsets[framesParsed]) { |
+ EXPECT_EQ(framesParsed, decoder->frameCount()); |
+ } else { |
+ ASSERT_EQ(framesParsed + 1, decoder->frameCount()); |
+ compareFrameWithExpectation(pngAnimatedFrameInfo[framesParsed], |
+ decoder->frameBufferAtIndex(framesParsed)); |
+ framesParsed++; |
+ } |
+ } |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ EXPECT_FALSE(decoder->failed()); |
+} |
+ |
+TEST(AnimatedPNGTests, TestRandomFrameDecode) |
+{ |
+ RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"); |
+ ASSERT_TRUE(fullData.get()); |
+ Vector<unsigned> baselineHashes; |
+ createDecodingBaseline(&createDecoder, fullData.get(), &baselineHashes); |
+ const size_t frameCount = baselineHashes.size(); |
+ |
+ // Random decoding should get the same results as sequential decoding. |
+ std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
+ decoder->setData(fullData.get(), true); |
+ const size_t skippingStep = 2; |
+ for (size_t i = 0; i < skippingStep; ++i) { |
+ for (size_t j = i; j < frameCount; j += skippingStep) { |
+ SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j); |
+ ImageFrame* frame = decoder->frameBufferAtIndex(j); |
+ EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap())); |
+ } |
+ } |
+ |
+ // Decoding in reverse order. |
+ decoder = createDecoder(); |
+ decoder->setData(fullData.get(), true); |
+ for (size_t i = frameCount; i; --i) { |
+ SCOPED_TRACE(testing::Message() << "Reverse i:" << i); |
+ ImageFrame* frame = decoder->frameBufferAtIndex(i - 1); |
+ EXPECT_EQ(baselineHashes[i - 1], hashBitmap(frame->bitmap())); |
+ } |
+ |
+} |
+ |
+TEST(AnimatedPNGTests, TestDecodeAfterReallocation) |
+{ |
+ std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
+ RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"); |
+ ASSERT_TRUE(data.get()); |
+ |
+ // Parse from |data|. |
+ decoder->setData(data.get(), true); |
+ const size_t frameCount = decoder->frameCount(); |
+ |
+ // ... and then decode frames from |reallocatedData|. |
+ RefPtr<SharedBuffer> reallocatedData = data.get()->copy(); |
+ ASSERT_TRUE(reallocatedData.get()); |
+ data.clear(); |
+ decoder->setData(reallocatedData.get(), true); |
+ |
+ for (size_t i = 0; i < frameCount; ++i) { |
+ const ImageFrame* const frame = decoder->frameBufferAtIndex(i); |
+ EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); |
+ } |
+} |
+ |
+TEST(AnimatedPNGTests, ProgressiveDecode) |
+{ |
+ testProgressiveDecoding("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"); |
+} |
+ |
+TEST(AnimatedPNGTests, ParseAndDecodeByteByByte) |
+{ |
+ testByteByByteDecode(&createDecoder, |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
+ 4u, 7u); |
+} |
+ |
+TEST(AnimatedPNGTests, FailureDuringParsing) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ auto data = readFile(pngFile); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ // Test the first fcTL in the stream. Because no frame data has been set at |
+ // this point, the expected frame count is zero. 95 bytes is just before the |
+ // first fcTL chunk, at which the first frame is detected. The decoder should |
+ // be in the failed state since no frames were parsed before this error. |
+ testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
+ 95u, 0u, true); |
+ |
+ // Test for the third fcTL in the stream. This should be tested as well, |
+ // since the first fcTL is parsed in PNGImageReader::parseSize() whereas |
+ // later fcTLs are parsed in PNGImageReader::parse() as part of framecount. |
+ // The expected frame count before the fcTL chunk is 1u, since the second |
+ // frame is registered when the third fcTL (or IEND) is seen. |
+ // |
+ // This should *not* set the decoder to the failed state since the first |
+ // frame was succesfully parsed, and we still want to show that frame. |
+ testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
+ 241u, 1u, false); |
+ |
+} |
+ |
+TEST(AnimatedPNGTests, FailureDuringParsingDueToMissingFctlData) |
+{ |
+ // The fcTL chunk starts at 95u, add 10u to get to the content data, and |
+ // remove 5 bytes of data. This sets the decoder to the failed state since |
+ // no frames were parsed yet. |
+ testMissingDataBreaksDecoding( |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
+ 105u, 5u); |
+} |
+ |
+TEST(AnimatedPNGTests, MissingActlResultsInNonAnimated) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ auto data = readFile(pngFile); |
+ auto decoder = createDecoder(); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ // Remove the acTL chunk from the stream. |
+ const size_t offsetActl = 33; |
+ RefPtr<SharedBuffer> noActlData = SharedBuffer::create(data->data(), |
+ offsetActl); |
+ noActlData->append(data->data() + offsetActl + 20, |
+ data->size() - offsetActl - 20); |
+ |
+ decoder->setData(noActlData, true); |
+ EXPECT_EQ(1u, decoder->frameCount()); |
+ EXPECT_FALSE(decoder->failed()); |
+} |
+ |
+// Test if the indicated frame count by the acTL is ignored if the actual |
+// number of frames is different. Test for higher and lower indicated number. |
+TEST(AnimatedPNGTests, differentActlFrameCountIsIgnored) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ testDifferentActlFrameCountIsIgnored(pngFile, 33u, 2u, 4u); |
+ testDifferentActlFrameCountIsIgnored(pngFile, 33u, 8u, 4u); |
+} |
+ |
+// Originally, the third frame has an offset of (1,2) and a size of (3,2). By |
+// changing the offset to (0,0) and the size to (4,4), a frame with not enough |
+// data is simulated. The expected result is that the decoder will only receive |
+// 1 row of data, since the data contains 6 pixels and each row is 4 pixels. |
+// |
+// When the decoder receives not enough data for a frame, the expected behavior |
+// is that nothing changes: the decoder should not be invalidated, and the |
+// frame should be considered successfully decoded. The reason for this is that |
+// the error could either be an invalid frame size, or an invalid data chunk. |
+// For now, the decoder does not take any action if not enough data is |
+// provided, as described in PNGImageDecoder::complete(). |
+TEST(AnimatedPNGTests, VerifyIncompleteFrameDataDoesNotInvalidateDecoder) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ auto data = readFile(pngFile); |
+ auto decoder = createDecoder(); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ // Change the size and the offset of the frame, as described above. The size |
+ // is stored in byte 12-19 of the fcTL chunk, and the offset in byte 20-27. |
+ const size_t offsetThirdFctl = 241 + 12; |
+ RefPtr<SharedBuffer> modifiedData = SharedBuffer::create(data->data(), |
+ offsetThirdFctl); |
+ png_byte rectChunk[16]; |
+ writeUint32(4, rectChunk); |
+ writeUint32(4, rectChunk + 4); |
+ writeUint32(0, rectChunk + 8); |
+ writeUint32(0, rectChunk + 12); |
+ modifiedData->append(const_cast<const char*>( |
+ reinterpret_cast<char*>(rectChunk)), 16u); |
+ modifiedData->append(data->data() + offsetThirdFctl + 16, |
+ data->size() - offsetThirdFctl - 16); |
+ |
+ decoder->setData(modifiedData, true); |
+ |
+ // Verify that parsing the image results in the expected meta data. |
+ IntSize expectedSize(5, 5); |
+ const size_t expectedFrameCount = 4; |
+ IntRect expectedFrameRect(IntPoint(0, 0), IntSize(4, 4)); |
+ EXPECT_TRUE(decoder->isSizeAvailable()); |
+ EXPECT_EQ(expectedSize, decoder->size()); |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ ASSERT_FALSE(decoder->failed()); |
+ |
+ // Try to decode frame 3. This should not fail the decoder, and the frame |
+ // should be FrameComplete. |
+ const ImageFrame* const buffer = decoder->frameBufferAtIndex(2); |
+ EXPECT_EQ(expectedFrameRect, buffer->originalFrameRect()); |
+ EXPECT_FALSE(decoder->failed()); |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ EXPECT_EQ(ImageFrame::FrameComplete, buffer->getStatus()); |
+} |
+ |
+// Originally, the third frame has an offset of (1,2) and a size of (3,2). By |
+// changing the offset to (4,4), the frame rect will extend the image size of |
+// 5x5. Verify that the frame is correctly clipped to a size of (1,1) without |
+// any decoding errors. Since this is the third frame, a decoding failure would |
+// result in the frame count being adjusted to 2, and the decoder would not be |
+// invalidated. Therefore, it is verified that after decoding frame 3 the |
+// decoder is still in a valid state and the frame count is still 4. |
+TEST(AnimatedPNGTests, VerifyFrameExtendsImageSizeClipsCorrectly) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ auto data = readFile(pngFile); |
+ auto decoder = createDecoder(); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ // The offset of the frame rect is located 20 bytes into the fcTL chunk, after |
+ // the length, tag, sequence number, width and height fields (all 4B). |
+ const size_t offsetThirdFctl = 241 + 20; |
+ RefPtr<SharedBuffer> modifiedData = SharedBuffer::create(data->data(), |
+ offsetThirdFctl); |
+ png_byte offsetChunk[8]; |
+ writeUint32(4, offsetChunk); |
+ writeUint32(4, offsetChunk + 4); |
+ modifiedData->append(const_cast<const char*>( |
+ reinterpret_cast<char*>(offsetChunk)), 8u); |
+ modifiedData->append(data->data() + offsetThirdFctl + 8, |
+ data->size() - offsetThirdFctl - 8); |
+ |
+ decoder->setData(modifiedData, true); |
+ |
+ // Verify that parsing the image results in the expected meta data. |
+ IntSize expectedSize(5, 5); |
+ const size_t expectedFrameCount = 4; |
+ IntRect expectedFrameRect(IntPoint(4, 4), IntSize(1, 1)); |
+ EXPECT_TRUE(decoder->isSizeAvailable()); |
+ EXPECT_EQ(expectedSize, decoder->size()); |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ ASSERT_FALSE(decoder->failed()); |
+ |
+ // Try to decode frame 3. This should not fail the decoder, and the frame |
+ // should be FrameComplete. |
+ const ImageFrame* const buffer = decoder->frameBufferAtIndex(2); |
+ EXPECT_EQ(expectedFrameRect, buffer->originalFrameRect()); |
+ EXPECT_FALSE(decoder->failed()); |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ EXPECT_EQ(ImageFrame::FrameComplete, buffer->getStatus()); |
+} |
+ |
+TEST(AnimatedPNGTests, ProgressiveDecodingChangesFrameBuffer) |
+{ |
+ testProgressiveDecodingChangesFrameBuffer( |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", 249u); |
+} |
+ |
+TEST(AnimatedPNGTests, ProgressiveDecodingContinuesAfterFullData) |
+{ |
+ // 160u is a randomly chosen offset in the IDAT chunk of the first frame. |
+ testProgressiveDecodingContinuesAfterFullData( |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", 160u); |
+} |
+ |
+TEST(AnimatedPNGTests, RandomDecodeAfterClearFrameBufferCache) |
+{ |
+ testRandomDecodeAfterClearFrameBufferCache( |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"); |
+} |
+ |
+TEST(AnimatedPNGTests, VerifyAlphaBlending) { |
+ testAlphaBlending( |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"); |
+} |
+ |
+// This tests if the frame count gets set correctly when parsing frameCount |
+// fails in one of the parsing queries. |
+// |
+// First, enough data is provided such that two frames should be registered. |
+// The decoder should at this point not be in the failed status. |
+// |
+// Then, we provide the rest of the data except for the last IEND chunk, but |
+// tell the decoder that this is all the data we have. The frame count should |
+// be three, since one extra frame should be discovered. The fourth frame |
+// should *not* be registered since the reader should not be able to determine |
+// where the frame ends. The decoder should *not* be in the failed state since |
+// there are three frames which can be shown. |
+TEST(AnimatedPNGTests, FailureMissingIendChunk) |
+{ |
+ RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"); |
+ ASSERT_FALSE(fullData->isEmpty()); |
+ auto decoder = createDecoder(); |
+ |
+ const size_t offsetTwoFrames = 249; |
+ const size_t expectedFramesAfter249Bytes = 2; |
+ RefPtr<SharedBuffer> tempData = SharedBuffer::create(fullData->data(), |
+ offsetTwoFrames); |
+ decoder->setData(tempData.get(), false); |
+ EXPECT_EQ(expectedFramesAfter249Bytes, decoder->frameCount()); |
+ EXPECT_FALSE(decoder->failed()); |
+ |
+ // Provide the rest of the data except for the last IEND chunk. |
+ const size_t expectedFramesAfterAllExpect12Bytes = 3; |
+ tempData = SharedBuffer::create(fullData->data(), fullData->size() - 12); |
+ decoder->setData(tempData.get(), true); |
+ EXPECT_EQ(expectedFramesAfterAllExpect12Bytes, decoder->frameCount()); |
+ EXPECT_FALSE(decoder->failed()); |
+} |
+ |
+TEST(AnimatedPNGTests, VerifyFrameCountChangesOnDecodingFailure) |
+{ |
+ testFailureDuringDecode( |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
+ 279u, // idat offset for frame index 2 |
+ 2u, // try to decode frame index 2 |
+ false, // expect the decoder to *not* be invalidated after the failure |
+ 4u, // expected frame count before failure |
+ 2u); // expected frame count after failure |
+ |
+ testFailureDuringDecode( |
+ "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
+ 133u, // idat offset for frame index 0 |
+ 0u, // try to decode frame index 0 |
+ true, // expect the decoder to be invalidated after the failure |
+ 4u); // expected frame count before failure |
+} |
+ |
+// Verify that a malformatted PNG, where the IEND appears before any frame data |
+// (IDAT), invalidates the decoder. |
+TEST(AnimatedPNGTests, VerifyIENDBeforeIDATInvalidatesDecoder) |
+{ |
+ RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"); |
+ ASSERT_FALSE(fullData->isEmpty()); |
+ auto decoder = createDecoder(); |
+ |
+ const size_t offsetIDAT = 133; |
+ RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), |
+ offsetIDAT); |
+ data->append(fullData->data() + fullData->size() - 12u, 12u); |
+ data->append(fullData->data() + offsetIDAT, fullData->size() - offsetIDAT); |
+ decoder->setData(data.get(), true); |
+ |
+ const size_t expectedFrameCount = 0u; |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ EXPECT_TRUE(decoder->failed()); |
+} |
+ |
+TEST(AnimatedPNGTests, VerifyFrameCompleteBehavior) |
+{ |
+ const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
+ RefPtr<SharedBuffer> fullData = readFile(pngFile); |
+ ASSERT_FALSE(fullData->isEmpty()); |
+ auto decoder = createDecoder(); |
+ decoder->setData(fullData.get(), true); |
+ |
+ const size_t expectedFrameCount = 4; |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ ASSERT_FALSE(decoder->failed()); |
+ |
+ // The first frame is only considered complete if it has been fully decoded. |
scroggo_chromium
2016/11/29 16:30:52
I think this behavior is wrong. I've gone back to
joostouwerling
2016/12/02 16:08:42
Yes, I agree. I think it is for the best to return
|
+ EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0)); |
+ // Non-first frames are considered complete when they're fully received. |
+ for (size_t index = 1; index < expectedFrameCount; index++) |
+ EXPECT_TRUE(decoder->frameIsCompleteAtIndex(1)); |
scroggo_chromium
2016/11/29 16:30:52
I think you meant to pass index to frameIsComplete
joostouwerling
2016/12/02 16:08:42
Done.
|
+ // After decoding, the frame should be considered complete. |
+ decoder->frameBufferAtIndex(0); |
+ EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0)); |
+ |
+ // Also test that a progressive decode of frame 0 only returns true on |
scroggo_chromium
2016/11/29 16:30:52
Again, I think this is the wrong behavior.
joostouwerling
2016/12/02 16:08:42
Acknowledged.
|
+ // frameIsCompleteAtIndex when it is fully decoded. |
+ decoder = createDecoder(); |
+ const size_t offsetMidwayIDAT = 160; |
+ RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), |
+ offsetMidwayIDAT); |
+ decoder->setData(data.get(), false); |
+ const ImageFrame* frame = decoder->frameBufferAtIndex(0); |
+ EXPECT_EQ(ImageFrame::FramePartial, frame->getStatus()); |
+ EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0)); |
+ // With the full data provided, the status should switch to complete. |
+ decoder->setData(fullData.get(), true); |
+ frame = decoder->frameBufferAtIndex(0); |
+ EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); |
+ EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0)); |
+} |
+ |
+// Static PNG tests |
+ |
+TEST(StaticPNGTests, repetitionCountTest) |
+{ |
+ testRepetitionCount("/LayoutTests/fast/images/resources/png-simple.png", |
+ cAnimationNone); |
+} |
+ |
+TEST(StaticPNGTests, sizeTest) |
+{ |
+ testSize("/LayoutTests/fast/images/resources/png-simple.png", |
+ IntSize(111, 29)); |
+} |
+ |
+TEST(StaticPNGTests, MetaDataTest) |
+{ |
+ const size_t expectedFrameCount = 1; |
+ const size_t expectedDuration = 0; |
+ auto decoder = createDecoderWithPngData("/LayoutTests/fast/images/resources/png-simple.png"); |
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
+ EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0)); |
+} |
+ |
+TEST(StaticPNGTests, InvalidIHDRChunk) |
+{ |
+ testMissingDataBreaksDecoding( |
+ "/LayoutTests/fast/images/resources/png-simple.png", 20u, 2u); |
+} |
+ |
+TEST(StaticPNGTests, ProgressiveDecoding) |
+{ |
+ testProgressiveDecoding("/LayoutTests/fast/images/resources/png-simple.png"); |
+} |
+ |
+TEST(StaticPNGTests, ProgressiveDecodingChangesFrameBuffer) |
+{ |
+ testProgressiveDecodingChangesFrameBuffer( |
+ "/LayoutTests/fast/images/resources/png-simple.png", 0u, 5u); |
+} |
+ |
+TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) |
+{ |
+ testProgressiveDecodingContinuesAfterFullData( |
+ "/LayoutTests/fast/images/resources/png-simple.png", 1000u); |
+} |
+ |
+TEST(StaticPNGTests, FailureDuringDecodingInvalidatesDecoder) |
+{ |
+ testFailureDuringDecode( |
+ "/LayoutTests/fast/images/resources/png-simple.png", |
+ 85u, // idat offset for frame index 0 |
+ 0u, // try to decode frame index 0 |
+ true, // expect the decoder to be invalidated after the failure |
+ 1u); // expected frame count before failure |
+} |
+ |
+}; // namespace blink |