Chromium Code Reviews| 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..7ac6ab295e4c34e9ddd8a98478cc7ab9bee6d2ef |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp |
| @@ -0,0 +1,650 @@ |
| +// 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> |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| +std::unique_ptr<ImageDecoder> createDecoder(ImageDecoder::AlphaOption alphaOption) |
| +{ |
| + return wrapUnique(new PNGImageDecoder(alphaOption, |
| + ImageDecoder::GammaAndColorProfileApplied, |
| + 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. |
| +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) |
| +{ |
| + 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_TRUE(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, 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); |
| +} |
| + |
| +} // 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()); |
| +} |
| + |
| +// 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. Now, the decoder should |
| +// be in the failed state since all data is provided but no IEND chunk has been |
| +// seen. 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. |
| +TEST(AnimatedPNGTests, FrameCountWithTruncatedData) |
| +{ |
| + const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"; |
| + auto decoder = createDecoder(); |
| + auto data = readFile(pngFile); |
| + ASSERT_FALSE(data->isEmpty()); |
| + |
| + // Parse up to and including the first two frames |
| + const size_t offsetTwoFrames = 249; |
| + const size_t expectedFramesAfter249Bytes = 2; |
| + RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->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(data->data(), data->size() - 12); |
| + decoder->setData(tempData.get(), true); |
| + EXPECT_EQ(expectedFramesAfterAllExpect12Bytes, decoder->frameCount()); |
| + EXPECT_TRUE(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); |
| + 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); |
| + 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, FctlWrongSizeBreaksDecoding) |
| +{ |
| + 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. |
| + testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
| + 95u, 0u); |
| + |
| + // 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. |
| + testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", |
| + 241u, 1u); |
| + |
| +} |
| + |
| +TEST(AnimatedPNGTests, MissingFctlDataBreaksDecoding) |
| +{ |
| + // The fcTL chunk starts at 95u, add 10u to get to the content data, and |
| + // remove 5 bytes of data. |
| + 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. |
| + 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); |
| +} |
| + |
| +// Check if a frame rectangle, that is larger than the image width, gets |
| +// clipped correctly. |
| +// @TODO(joostouwerling) use an image that also has data that needs to be |
| +// clippedi, not just report larger dimensions. |
|
scroggo_chromium
2016/10/31 13:35:12
clipped*
joostouwerling
2016/10/31 18:40:19
Done.
|
| +TEST(AnimatedPNGTests, frameRectIsClipped) |
| +{ |
| + 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 width and height of the frame so it falls outside the image. |
| + size_t offsetThirdFctl = 241 + 12; |
| + RefPtr<SharedBuffer> modifiedData = SharedBuffer::create(data->data(), |
| + offsetThirdFctl); |
| + png_byte sizeChunk[8]; |
| + writeUint32(10, sizeChunk); |
| + writeUint32(15, sizeChunk + 4); |
| + modifiedData->append(const_cast<const char*>( |
| + reinterpret_cast<char*>(sizeChunk)), 8u); |
| + modifiedData->append(data->data() + offsetThirdFctl + 8, |
| + data->size() - offsetThirdFctl - 8); |
| + |
| + decoder->setData(modifiedData, true); |
| + |
| + IntSize expectedSize(5, 5); |
| + size_t expectedFrameCount = 4; |
| + IntRect expectedFrameRect(IntPoint(1, 2), IntSize(4, 3)); |
| + |
| + EXPECT_TRUE(decoder->isSizeAvailable()); |
| + EXPECT_EQ(expectedSize, decoder->size()); |
| + EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| + ASSERT_FALSE(decoder->failed()); |
| + EXPECT_EQ(expectedFrameRect, |
| + decoder->frameBufferAtIndex(2)->originalFrameRect()); |
| +} |
| + |
| +// For explanation of this test, see the definition of |
| +// testProgressiveDecodingChangesFrameBuffer |
| +TEST(AnimatedPNGTests, ProgressiveDecodingChangesFrameBuffer) |
| +{ |
| + testProgressiveDecodingChangesFrameBuffer( |
| + "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", 249u); |
| +} |
| + |
| +// For explanation of this test, see the definition of |
| +// testProgressiveDecodingContinuesAfterFullData |
| +TEST(AnimatedPNGTests, ProgressiveDecodingContinuesAfterFullData) |
| +{ |
| + testProgressiveDecodingContinuesAfterFullData( |
| + "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png", 160u); |
| +} |
| + |
| +// 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); |
| +} |
| + |
| +}; // namespace blink |