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

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

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Fix feedback on previous patches Created 4 years 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 side-by-side diff with in-line comments
Download patch
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
index 5281c1ba065c9005ab9e27924de31e316852d74c..e59ed8a0b8fc41f4192b25ff0d447c40e491cce3 100644
--- a/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp
+++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoderTest.cpp
@@ -5,9 +5,31 @@
#include "platform/image-decoders/png/PNGImageDecoder.h"
#include "platform/image-decoders/ImageDecoderTestHelpers.h"
+#include "png.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <memory>
+// /LayoutTests/images/resources/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 {
@@ -37,14 +59,161 @@ void testSize(const char* pngFile, IntSize expectedSize) {
EXPECT_EQ(expectedSize, decoder->size());
}
+// 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());
+}
+
+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);
- // Decoding the frame count sets the repetition count as well.
+ // Decode frame count should see the number of repetitions as well.
decoder->frameCount();
EXPECT_FALSE(decoder->failed());
EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount());
}
+struct PublicFrameInfo {
+ size_t duration;
+ IntRect frameRect;
+ ImageFrame::AlphaBlendSource alphaBlend;
+ ImageFrame::DisposalMethod disposalMethod;
+};
+
+// This is the frame data for the following PNG image:
+// /LayoutTests/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(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());
+}
+
// Verify that the decoder can successfully decode the first frame when
// initially only half of the frame data is received, resulting in a partially
// decoded image, and then the rest of the image data is received. Verify that
@@ -60,7 +229,7 @@ void testProgressiveDecodingContinuesAfterFullData(const char* pngFile,
auto decoderUpfront = createDecoder();
decoderUpfront->setData(fullData.get(), true);
- EXPECT_GE(1u, decoderUpfront->frameCount());
+ EXPECT_GE(decoderUpfront->frameCount(), 1u);
const ImageFrame* const frameUpfront = decoderUpfront->frameBufferAtIndex(0);
ASSERT_EQ(ImageFrame::FrameComplete, frameUpfront->getStatus());
const unsigned hashUpfront = hashBitmap(frameUpfront->bitmap());
@@ -114,16 +283,507 @@ void testFailureDuringDecode(const char* file,
EXPECT_EQ(expectedFrameCountBefore, decoder->frameCount());
- const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex);
- EXPECT_EQ(expectFailure, decoder->failed());
+ decoder->frameBufferAtIndex(frameIndex);
+ ASSERT_EQ(expectFailure, decoder->failed());
if (!expectFailure) {
EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount());
- EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus());
+ EXPECT_EQ(nullptr, decoder->frameBufferAtIndex(frameIndex));
}
}
} // Anonymous namespace
+// Animated PNG Tests
+
+TEST(AnimatedPNGTests, sizeTest) {
+ testSize(
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ IntSize(5, 5));
+ testSize(
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-not-part-of-animation.png",
+ IntSize(227, 35));
+}
+
+TEST(AnimatedPNGTests, repetitionCountTest) {
+ testRepetitionCount(
+ "/LayoutTests/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/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/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/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ 141u, IntSize(5, 5));
+ testSizeByteByByte(
+ "/LayoutTests/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/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) {
+ testRandomFrameDecode(&createDecoder,
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ 2u);
+}
+
+TEST(AnimatedPNGTests, TestDecodeAfterReallocation) {
+ testDecodeAfterReallocatingData(&createDecoder,
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png");
+}
+
+TEST(AnimatedPNGTests, ProgressiveDecode) {
+ testProgressiveDecoding(&createDecoder,
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ 13u);
+}
+
+TEST(AnimatedPNGTests, ParseAndDecodeByteByByte) {
+ testByteByByteDecode(&createDecoder,
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ 4u, 7u);
+}
+
+TEST(AnimatedPNGTests, FailureDuringParsing) {
+ // 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/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/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/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ 105u, 5u);
+}
+
+TEST(AnimatedPNGTests, MissingActlResultsInNonAnimated) {
+ const char* pngFile =
+ "/LayoutTests/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/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/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/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, ProgressiveDecodingContinuesAfterFullData) {
+ // 160u is a randomly chosen offset in the IDAT chunk of the first frame.
+ testProgressiveDecodingContinuesAfterFullData(
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ 160u);
+}
+
+TEST(AnimatedPNGTests, RandomDecodeAfterClearFrameBufferCache) {
+ testRandomDecodeAfterClearFrameBufferCache(
+ &createDecoder,
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png",
+ 2u);
+}
+
+TEST(AnimatedPNGTests, VerifyAlphaBlending) {
+ testAlphaBlending(&createDecoder,
+ "/LayoutTests/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/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/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/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/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());
+}
+
+// For animated images, frameIsCompleteAtIndex(i) should return true if the
+// i-th frame is fully received. The frames don't need to be successfully
+// decoded.
+TEST(AnimatedPNGTests, VerifyFrameCompleteBehavior) {
+ const char* pngFile =
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png";
+ RefPtr<SharedBuffer> fullData = readFile(pngFile);
+ ASSERT_FALSE(fullData->isEmpty());
+ auto decoder = createDecoder();
+
+ // When not all data for the first frame has been received,
+ // frameIsCompleteAtIndex(0) should return false.
+ const size_t offsetMidwayFirstFrame = 160;
+ RefPtr<SharedBuffer> data =
+ SharedBuffer::create(fullData->data(), offsetMidwayFirstFrame);
+ decoder->setData(data.get(), false);
+ EXPECT_EQ(1u, decoder->frameCount());
+ ASSERT_FALSE(decoder->failed());
+ EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0));
+
+ // When all image data is received, every frame should be complete.
+ const size_t expectedFrameCount = 4;
+ decoder->setData(fullData.get(), true);
+ EXPECT_EQ(expectedFrameCount, decoder->frameCount());
+ for (size_t index = 0; index < expectedFrameCount; index++)
+ EXPECT_TRUE(decoder->frameIsCompleteAtIndex(index));
+}
+
+// Verify that erroneous values for the disposal method and alpha blending
+// are parsed into the expected default values, respectively DisposeNotSpecified
+// and BlendAtopBgcolor.
+TEST(AnimatedPNGTests, VerifyInvalidDisposalAndBlendingDefaultCorrectly) {
+ const char* pngFile =
+ "/LayoutTests/images/resources/"
+ "png-animated-idat-part-of-animation.png";
+ RefPtr<SharedBuffer> fullData = readFile(pngFile);
+ ASSERT_FALSE(fullData->isEmpty());
+ auto decoder = createDecoder();
+
+ // The disposal byte in the frame control chunk is the 24th byte, alpha
+ // blending the 25th. |offsetDisposalOp| is 241 bytes to get to the third
+ // fctl chunk, 8 bytes to skip the length and tag bytes, and 24 bytes to get
+ // to the disposal op.
+ //
+ // Write random values to the disposal and alpha blending byte, and append
+ // the rest of the buffer.
+ const size_t offsetDisposalOp = 241 + 8 + 24;
+ RefPtr<SharedBuffer> data =
+ SharedBuffer::create(fullData->data(), offsetDisposalOp);
+ png_byte disposalAndBlending[2];
+ disposalAndBlending[0] = 7;
+ disposalAndBlending[1] = 9;
+ data->append(reinterpret_cast<char*>(disposalAndBlending), 2u);
+ data->append(fullData->data() + offsetDisposalOp + 2u,
+ fullData->size() - offsetDisposalOp - 2u);
+
+ decoder->setData(data.get(), true);
+ decoder->frameCount();
+ ASSERT_FALSE(decoder->failed());
+ EXPECT_EQ(ImageFrame::DisposalMethod::DisposeNotSpecified,
+ decoder->frameBufferAtIndex(2)->getDisposalMethod());
+ EXPECT_EQ(ImageFrame::AlphaBlendSource::BlendAtopBgcolor,
+ decoder->frameBufferAtIndex(2)->getAlphaBlendSource());
+}
+
+// This test verifies that the following situation does not invalidate the
+// decoder:
+// - Frame 0 is decoded progressively, but there's not enough data to fully
+// decode it.
+// - The rest of the image data is received.
+// - Frame X, with X > 0, and X does not depend on frame 0, is decoded.
+// - Frame 0 is decoded.
+// This is a tricky case since the decoder resets the png struct for each frame,
+// and this test verifies that it does not break the decoding of frame 0, even
+// though it already started in the first call.
+TEST(AnimatedPNGTests, VerifySuccessfulFirstFrameDecodeAfterLaterFrame) {
+ const char* pngFile =
+ "/LayoutTests/images/resources/"
+ "png-animated-three-independent-frames.png";
+ auto decoder = createDecoder();
+ auto fullData = readFile(pngFile);
+ ASSERT_FALSE(fullData->isEmpty());
+
+ // 160u is a randomly chosen offset in the IDAT chunk of the first frame.
+ const size_t middleFirstFrame = 160u;
+ RefPtr<SharedBuffer> data =
+ SharedBuffer::create(fullData->data(), middleFirstFrame);
+ decoder->setData(data.get(), false);
+
+ ASSERT_EQ(1u, decoder->frameCount());
+ ASSERT_EQ(ImageFrame::FramePartial,
+ decoder->frameBufferAtIndex(0)->getStatus());
+
+ decoder->setData(fullData.get(), true);
+ ASSERT_EQ(3u, decoder->frameCount());
+ ASSERT_EQ(ImageFrame::FrameComplete,
+ decoder->frameBufferAtIndex(1)->getStatus());
+ // The point is that this call does not decode frame 0, which it won't do if
+ // it does not have it as its required previous frame.
+ ASSERT_EQ(kNotFound,
+ decoder->frameBufferAtIndex(1)->requiredPreviousFrameIndex());
+
+ EXPECT_EQ(ImageFrame::FrameComplete,
+ decoder->frameBufferAtIndex(0)->getStatus());
+ EXPECT_FALSE(decoder->failed());
+}
+
// Static PNG tests
TEST(StaticPNGTests, repetitionCountTest) {
@@ -144,6 +804,11 @@ TEST(StaticPNGTests, MetaDataTest) {
EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0));
}
+TEST(StaticPNGTests, InvalidIHDRChunk) {
+ testMissingDataBreaksDecoding("/LayoutTests/images/resources/png-simple.png",
+ 20u, 2u);
+}
+
TEST(StaticPNGTests, ProgressiveDecoding) {
testProgressiveDecoding(&createDecoder,
"/LayoutTests/images/resources/png-simple.png", 11u);

Powered by Google App Engine
This is Rietveld 408576698