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

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

Issue 2548863002: Add tests for PNGImageDecoder. (Closed)
Patch Set: Improve progressive PNG decoding tests. 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 ImageFrame* frameUpfront = decoderUpfront->frameBufferAtIndex(0);
66 ASSERT_EQ(ImageFrame::FrameComplete, frameUpfront->getStatus());
67 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 ImageFrame* frame = decoder->frameBufferAtIndex(0);
76 EXPECT_EQ(frame->getStatus(), ImageFrame::FramePartial);
77 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 unsigned hashFull = hashBitmap(frame->bitmap());
83
84 EXPECT_FALSE(decoder->failed());
85 EXPECT_NE(hashFull, hashPartial);
86 EXPECT_EQ(hashFull, hashUpfront);
87 }
88
89 // This test verifies that the frame buffer contents change when progressively
90 // decoding the first frame. It should change once for every row in the frame
91 // rect. If |offsetFirstFrameEnd| == 0, the test uses the full data size of the
92 // image for progressive decoding.
93 void testProgressiveDecodingChangesFrameBuffer(const char* pngFile,
94 size_t offsetFirstFrameEnd = 0) {
95 auto fullData = readFile(pngFile);
96 ASSERT_FALSE(fullData->isEmpty());
97
98 // First find the height of the frame rect of the first frame.
99 auto sizeDecoder = createDecoder();
100 sizeDecoder->setData(fullData.get(), true);
101 EXPECT_GE(1u, sizeDecoder->frameCount());
102 ImageFrame* frame = sizeDecoder->frameBufferAtIndex(0);
103 ASSERT_EQ(ImageFrame::FrameComplete, frame->getStatus());
104 size_t frameRectRows = frame->originalFrameRect().height();
scroggo_chromium 2016/12/05 18:41:29 nit: could be const.
joostouwerling 2016/12/05 19:05:01 Done. (and some others).
105
106 if (offsetFirstFrameEnd == 0)
107 offsetFirstFrameEnd = fullData->size();
108
109 size_t numTimesBufferChanged = 0;
110 unsigned prevHash;
111 auto decoder = createDecoder();
112 bool frameSeen = false;
113
114 for (size_t length = 1; length <= offsetFirstFrameEnd; length++) {
115 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), length);
116 decoder->setData(data, false);
117 frame = decoder->frameBufferAtIndex(0);
118 if (!frame)
119 continue;
120
121 // Initialize |prevHash| at the first time we have a valid frame pointer.
122 // This makes sure we don't count going from "nothing" to an empty frame.
scroggo_chromium 2016/12/05 18:41:29 I don't think that the concern is that we count it
joostouwerling 2016/12/05 19:05:01 Hmm, maybe as well, but doesn't comment about unin
scroggo_chromium 2016/12/05 19:17:20 I don't think so. It explains why the variable fra
123 if (!frameSeen) {
124 prevHash = hashBitmap(frame->bitmap());
125 frameSeen = true;
126 continue;
127 }
128 unsigned newHash = hashBitmap(frame->bitmap());
129 if (newHash != prevHash) {
130 prevHash = newHash;
131 numTimesBufferChanged++;
132 }
133 }
134 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus());
135 EXPECT_EQ(numTimesBufferChanged, frameRectRows);
scroggo_chromium 2016/12/05 18:41:29 This is probably true in a lot of cases (e.g. the
joostouwerling 2016/12/05 19:05:01 Ah, yeah, so we can either make it a precondition
136 }
137
138 // Modify the frame data bytes for frame |frameIndex| so that decoding fails.
139 // Parsing should work fine, and is checked with |expectedFrameCountBefore|. If
140 // the failure should invalidate the decoder, |expectFailure| should be set to
141 // true. If not, |expectedFrameCountAfter| should indicate the new frame count
142 // after the failure.
143 void testFailureDuringDecode(const char* file,
144 size_t idatOffset,
145 size_t frameIndex,
146 bool expectFailure,
147 size_t expectedFrameCountBefore,
148 size_t expectedFrameCountAfter = 0u) {
149 RefPtr<SharedBuffer> fullData = readFile(file);
150 ASSERT_FALSE(fullData->isEmpty());
151
152 // This is the offset where the frame data chunk frame |frameIndex| starts.
153 RefPtr<SharedBuffer> data =
154 SharedBuffer::create(fullData->data(), idatOffset + 8u);
155 // Repeat the first 8 bytes of the frame data. This should result in a
156 // successful parse, since frame data is not analyzed in that step, but
157 // should give an error during decoding.
158 data->append(fullData->data() + idatOffset, 8u);
159 data->append(fullData->data() + idatOffset + 16u,
160 fullData->size() - idatOffset - 16u);
161
162 auto decoder = createDecoder();
163 decoder->setData(data.get(), true);
164
165 EXPECT_EQ(expectedFrameCountBefore, decoder->frameCount());
166
167 const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex);
168 EXPECT_EQ(expectFailure, decoder->failed());
169 if (!expectFailure) {
170 EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount());
171 EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus());
172 }
173 }
174
175 } // Anonymous namespace
176
177 // Static PNG tests
178
179 TEST(StaticPNGTests, repetitionCountTest) {
180 testRepetitionCount("/LayoutTests/images/resources/png-simple.png",
181 cAnimationNone);
182 }
183
184 TEST(StaticPNGTests, sizeTest) {
185 testSize("/LayoutTests/images/resources/png-simple.png", IntSize(111, 29));
186 }
187
188 TEST(StaticPNGTests, MetaDataTest) {
189 const size_t expectedFrameCount = 1;
190 const size_t expectedDuration = 0;
191 auto decoder =
192 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
193 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
194 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0));
195 }
196
197 TEST(StaticPNGTests, ProgressiveDecoding) {
198 testProgressiveDecoding(&createDecoder,
199 "/LayoutTests/images/resources/png-simple.png", 11u);
200 }
201
202 TEST(StaticPNGTests, ProgressiveDecodingChangesFrameBuffer) {
203 testProgressiveDecodingChangesFrameBuffer(
204 "/LayoutTests/images/resources/png-simple.png");
205 }
206
207 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) {
208 testProgressiveDecodingContinuesAfterFullData(
209 "/LayoutTests/images/resources/png-simple.png", 1000u);
210 }
211
212 TEST(StaticPNGTests, FailureDuringDecodingInvalidatesDecoder) {
213 testFailureDuringDecode(
214 "/LayoutTests/images/resources/png-simple.png",
215 85u, // idat offset for frame index 0
216 0u, // try to decode frame index 0
217 true, // expect the decoder to be invalidated after the failure
218 1u); // expected frame count before failure
219 }
220
221 // For static images, frameIsCompleteAtIndex(0) should return true if and only
222 // if the frame is successfully decoded, not when it is fully received.
223 TEST(StaticPNGTests, VerifyFrameCompleteBehavior) {
224 auto decoder =
225 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
226 EXPECT_EQ(1u, decoder->frameCount());
227 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0));
228 EXPECT_EQ(ImageFrame::FrameComplete,
229 decoder->frameBufferAtIndex(0)->getStatus());
230 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
231 }
232
233 }; // 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