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

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

Issue 2548863002: Add tests for PNGImageDecoder. (Closed)
Patch Set: Remove testProgressiveDecodingChangesFrameBuffer and make const-correct. 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 unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "platform/image-decoders/png/PNGImageDecoder.h"
6
7 #include "platform/image-decoders/ImageDecoderTestHelpers.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include <memory>
10
11 namespace blink {
12
13 namespace {
14
15 std::unique_ptr<ImageDecoder> createDecoder(
16 ImageDecoder::AlphaOption alphaOption) {
17 return wrapUnique(
18 new PNGImageDecoder(alphaOption, ImageDecoder::ColorSpaceTransformed,
19 ImageDecoder::targetColorSpaceForTesting(),
20 ImageDecoder::noDecodedImageByteLimit));
21 }
22
23 std::unique_ptr<ImageDecoder> createDecoder() {
24 return createDecoder(ImageDecoder::AlphaNotPremultiplied);
25 }
26
27 std::unique_ptr<ImageDecoder> createDecoderWithPngData(const char* pngFile) {
28 auto decoder = createDecoder();
29 auto data = readFile(pngFile);
30 EXPECT_FALSE(data->isEmpty());
31 decoder->setData(data.get(), true);
32 return decoder;
33 }
34
35 void testSize(const char* pngFile, IntSize expectedSize) {
36 auto decoder = createDecoderWithPngData(pngFile);
37 EXPECT_TRUE(decoder->isSizeAvailable());
38 EXPECT_EQ(expectedSize, decoder->size());
39 }
40
41 void testRepetitionCount(const char* pngFile, int expectedRepetitionCount) {
42 auto decoder = createDecoderWithPngData(pngFile);
43 // Decoding the frame count sets the repetition count as well.
44 decoder->frameCount();
45 EXPECT_FALSE(decoder->failed());
46 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount());
47 }
48
49 // Verify that the decoder can successfully decode the first frame when
50 // initially only half of the frame data is received, resulting in a partially
51 // decoded image, and then the rest of the image data is received. Verify that
52 // the bitmap hashes of the two stages are different. Also verify that the final
53 // bitmap hash is equivalent to the hash when all data is provided at once.
54 //
55 // This verifies that decoder correctly keeps track of where it stopped
56 // decoding when the image was not yet fully received.
57 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile,
58 size_t offsetMidFirstFrame) {
59 auto fullData = readFile(pngFile);
60 ASSERT_FALSE(fullData->isEmpty());
61
62 auto decoderUpfront = createDecoder();
63 decoderUpfront->setData(fullData.get(), true);
64 EXPECT_GE(1u, decoderUpfront->frameCount());
65 const ImageFrame* const frameUpfront = decoderUpfront->frameBufferAtIndex(0);
66 ASSERT_EQ(ImageFrame::FrameComplete, frameUpfront->getStatus());
67 const unsigned hashUpfront = hashBitmap(frameUpfront->bitmap());
68
69 auto decoder = createDecoder();
70 RefPtr<SharedBuffer> partialData =
71 SharedBuffer::create(fullData->data(), offsetMidFirstFrame);
72 decoder->setData(partialData, false);
73
74 EXPECT_EQ(1u, decoder->frameCount());
75 const ImageFrame* frame = decoder->frameBufferAtIndex(0);
76 EXPECT_EQ(frame->getStatus(), ImageFrame::FramePartial);
77 const unsigned hashPartial = hashBitmap(frame->bitmap());
78
79 decoder->setData(fullData.get(), true);
80 frame = decoder->frameBufferAtIndex(0);
81 EXPECT_EQ(frame->getStatus(), ImageFrame::FrameComplete);
82 const unsigned hashFull = hashBitmap(frame->bitmap());
83
84 EXPECT_FALSE(decoder->failed());
85 EXPECT_NE(hashFull, hashPartial);
86 EXPECT_EQ(hashFull, hashUpfront);
87 }
88
89 // Modify the frame data bytes for frame |frameIndex| so that decoding fails.
90 // Parsing should work fine, and is checked with |expectedFrameCountBefore|. If
91 // the failure should invalidate the decoder, |expectFailure| should be set to
92 // true. If not, |expectedFrameCountAfter| should indicate the new frame count
93 // after the failure.
94 void testFailureDuringDecode(const char* file,
95 size_t idatOffset,
96 size_t frameIndex,
97 bool expectFailure,
98 size_t expectedFrameCountBefore,
99 size_t expectedFrameCountAfter = 0u) {
100 RefPtr<SharedBuffer> fullData = readFile(file);
101 ASSERT_FALSE(fullData->isEmpty());
102
103 // This is the offset where the frame data chunk frame |frameIndex| starts.
104 RefPtr<SharedBuffer> data =
105 SharedBuffer::create(fullData->data(), idatOffset + 8u);
106 // Repeat the first 8 bytes of the frame data. This should result in a
107 // successful parse, since frame data is not analyzed in that step, but
108 // should give an error during decoding.
109 data->append(fullData->data() + idatOffset, 8u);
110 data->append(fullData->data() + idatOffset + 16u,
111 fullData->size() - idatOffset - 16u);
112
113 auto decoder = createDecoder();
114 decoder->setData(data.get(), true);
115
116 EXPECT_EQ(expectedFrameCountBefore, decoder->frameCount());
117
118 const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex);
119 EXPECT_EQ(expectFailure, decoder->failed());
120 if (!expectFailure) {
121 EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount());
122 EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus());
123 }
124 }
125
126 } // Anonymous namespace
127
128 // Static PNG tests
129
130 TEST(StaticPNGTests, repetitionCountTest) {
131 testRepetitionCount("/LayoutTests/images/resources/png-simple.png",
132 cAnimationNone);
133 }
134
135 TEST(StaticPNGTests, sizeTest) {
136 testSize("/LayoutTests/images/resources/png-simple.png", IntSize(111, 29));
137 }
138
139 TEST(StaticPNGTests, MetaDataTest) {
140 const size_t expectedFrameCount = 1;
141 const size_t expectedDuration = 0;
142 auto decoder =
143 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
144 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
145 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0));
146 }
147
148 TEST(StaticPNGTests, ProgressiveDecoding) {
149 testProgressiveDecoding(&createDecoder,
150 "/LayoutTests/images/resources/png-simple.png", 11u);
151 }
152
153 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) {
154 testProgressiveDecodingContinuesAfterFullData(
155 "/LayoutTests/images/resources/png-simple.png", 1000u);
156 }
157
158 TEST(StaticPNGTests, FailureDuringDecodingInvalidatesDecoder) {
159 testFailureDuringDecode(
160 "/LayoutTests/images/resources/png-simple.png",
161 85u, // idat offset for frame index 0
162 0u, // try to decode frame index 0
163 true, // expect the decoder to be invalidated after the failure
164 1u); // expected frame count before failure
165 }
166
167 // For static images, frameIsCompleteAtIndex(0) should return true if and only
168 // if the frame is successfully decoded, not when it is fully received.
169 TEST(StaticPNGTests, VerifyFrameCompleteBehavior) {
170 auto decoder =
171 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
172 EXPECT_EQ(1u, decoder->frameCount());
173 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0));
174 EXPECT_EQ(ImageFrame::FrameComplete,
175 decoder->frameBufferAtIndex(0)->getStatus());
176 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
177 }
178
179 }; // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698