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..37b87f18fcd4c28f337e4e383b93daedf459caf6 |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp |
@@ -0,0 +1,269 @@ |
+// 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 "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 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()); |
+} |
+ |
+} // 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); |
+ testRepetitionCount("/LayoutTests/fast/images/resources/png-animated-idat-not-part-of-animation.png", cAnimationLoopInfinite); |
+} |
+ |
+// 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. |
+ size_t frameOffsets[expectedFrameCount] = {180, 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 |
scroggo_chromium
2016/10/17 15:27:06
This is the natural extension of what we discussed
|
+// 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()); |
+} |
+ |
+ |
+ |
+// 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)); |
+} |
+ |
+// This test removes two random byets from the IHDR chunk. The decoder should |
+// fail on this since all IHDR data is necessary to decode a PNG image. |
+TEST(StaticPNGTests, InvalidIHDRChunk) |
+{ |
+ const char* pngFile = "//LayoutTests/fast/images/resources/png-simple.png"; |
+ auto decoder = createDecoder(); |
+ auto data = readFile(pngFile); |
+ ASSERT_FALSE(data->isEmpty()); |
+ |
+ const size_t offsetHalfwayIHDR = 20; |
+ // Give the first part of the PNG stream: 8 signature, 8 IHDR indicator |
+ // and the first four bytes of IHDR content. |
+ RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(), |
+ offsetHalfwayIHDR); |
+ // Omit the 21st and 22nd byte, and append the rest. |
+ invalidData->append(SharedBuffer::create(data->data() + 22, |
+ data->size() - 22)); |
+ ASSERT_EQ(data->size() - 2, invalidData->size()); |
+ |
+ decoder->setData(invalidData, true); |
+ EXPECT_FALSE(decoder->isSizeAvailable()); |
+ EXPECT_TRUE(decoder->failed()); |
+ |
+} |
+}; // namespace blink |