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

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

Issue 2548863002: Add tests for PNGImageDecoder. (Closed)
Patch Set: Modify comments for 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. This verifies that decoder
53 // correctly keeps track of where it stopped decoding when the image was not
54 // yet fully received.
55 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile,
56 size_t offsetMidFirstFrame) {
57 auto fullData = readFile(pngFile);
58 auto decoder = createDecoder();
59 ASSERT_FALSE(fullData->isEmpty());
60
61 RefPtr<SharedBuffer> partialData =
62 SharedBuffer::create(fullData->data(), offsetMidFirstFrame);
63 decoder->setData(partialData, false);
64
65 EXPECT_EQ(1u, decoder->frameCount());
66 ImageFrame* frame = decoder->frameBufferAtIndex(0);
67 EXPECT_EQ(frame->getStatus(), ImageFrame::FramePartial);
68 unsigned hashPartial = hashBitmap(frame->bitmap());
69
70 decoder->setData(fullData.get(), true);
71 frame = decoder->frameBufferAtIndex(0);
72 EXPECT_EQ(frame->getStatus(), ImageFrame::FrameComplete);
73 unsigned hashFull = hashBitmap(frame->bitmap());
74
75 EXPECT_FALSE(decoder->failed());
76 EXPECT_NE(hashFull, hashPartial);
scroggo_chromium 2016/12/05 14:40:17 It might be more interesting to verify that hashFu
joostouwerling 2016/12/05 18:29:32 Done.
77 }
78
79 // This test verifies that the frame buffer contents change when progressively
80 // decoding the first frame. It should change more than one time: once for the
81 // first data, and at least once more thereafter. If |offsetFirstFrameEnd| == 0,
82 // the test uses the full data size of the image for progressive decoding.
83 void testProgressiveDecodingChangesFrameBuffer(const char* pngFile,
scroggo_chromium 2016/12/05 14:40:17 This seems like a similar test to the one above. D
joostouwerling 2016/12/05 18:29:32 They're somewhat similar, but especially with the
84 size_t offsetFirstFrameEnd,
85 size_t step = 1u) {
86 auto fullData = readFile(pngFile);
87 auto decoder = createDecoder();
88 ASSERT_FALSE(fullData->isEmpty());
89
90 if (offsetFirstFrameEnd == 0)
91 offsetFirstFrameEnd = fullData->size();
92
93 size_t numTimesBufferChanged = 0;
94 unsigned lastHash;
95
96 for (size_t length = 1; length <= offsetFirstFrameEnd; length += step) {
97 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), length);
98 decoder->setData(data, false);
99 ImageFrame* frame = decoder->frameBufferAtIndex(0);
100 if (!frame)
101 continue;
102 unsigned newHash = hashBitmap(frame->bitmap());
103 if (newHash != lastHash) {
scroggo_chromium 2016/12/05 14:40:17 The first time through the loop, lastHash will be
joostouwerling 2016/12/05 18:29:32 Done.
104 lastHash = newHash;
105 numTimesBufferChanged++;
scroggo_chromium 2016/12/05 14:40:17 After we hit this the second time, do we really ne
joostouwerling 2016/12/05 18:29:32 I changed it to check whether it changes for every
106 }
107 }
108 EXPECT_GT(numTimesBufferChanged, 1u);
109 }
110
111 // Modify the frame data bytes for frame |frameIndex| so that decoding fails.
112 // Parsing should work fine, and is checked with |expectedFrameCountBefore|. If
113 // the failure should invalidate the decoder, |expectFailure| should be set to
114 // true. If not, |expectedFrameCountAfter| should indicate the new frame count
115 // after the failure.
116 void testFailureDuringDecode(const char* file,
117 size_t idatOffset,
118 size_t frameIndex,
119 bool expectFailure,
120 size_t expectedFrameCountBefore,
121 size_t expectedFrameCountAfter = 0u) {
122 RefPtr<SharedBuffer> fullData = readFile(file);
123 ASSERT_FALSE(fullData->isEmpty());
124
125 // This is the offset where the frame data chunk frame |frameIndex| starts.
126 RefPtr<SharedBuffer> data =
127 SharedBuffer::create(fullData->data(), idatOffset + 8u);
128 // Repeat the first 8 bytes of the frame data. This should result in a
129 // successful parse, since frame data is not analyzed in that step, but
130 // should give an error during decoding.
131 data->append(fullData->data() + idatOffset, 8u);
132 data->append(fullData->data() + idatOffset + 16u,
133 fullData->size() - idatOffset - 16u);
134
135 auto decoder = createDecoder();
136 decoder->setData(data.get(), true);
137
138 EXPECT_EQ(expectedFrameCountBefore, decoder->frameCount());
139
140 const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex);
141 EXPECT_EQ(expectFailure, decoder->failed());
142 if (!expectFailure) {
143 EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount());
144 EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus());
145 }
146 }
147
148 } // Anonymous namespace
149
150 // Static PNG tests
151
152 TEST(StaticPNGTests, repetitionCountTest) {
153 testRepetitionCount("/LayoutTests/images/resources/png-simple.png",
154 cAnimationNone);
155 }
156
157 TEST(StaticPNGTests, sizeTest) {
158 testSize("/LayoutTests/images/resources/png-simple.png", IntSize(111, 29));
159 }
160
161 TEST(StaticPNGTests, MetaDataTest) {
162 const size_t expectedFrameCount = 1;
163 const size_t expectedDuration = 0;
164 auto decoder =
165 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
166 EXPECT_EQ(expectedFrameCount, decoder->frameCount());
167 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0));
168 }
169
170 TEST(StaticPNGTests, ProgressiveDecoding) {
171 testProgressiveDecoding(&createDecoder,
172 "/LayoutTests/images/resources/png-simple.png", 11u);
173 }
174
175 TEST(StaticPNGTests, ProgressiveDecodingChangesFrameBuffer) {
176 testProgressiveDecodingChangesFrameBuffer(
177 "/LayoutTests/images/resources/png-simple.png", 0u, 5u);
178 }
179
180 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) {
181 testProgressiveDecodingContinuesAfterFullData(
182 "/LayoutTests/images/resources/png-simple.png", 1000u);
183 }
184
185 TEST(StaticPNGTests, FailureDuringDecodingInvalidatesDecoder) {
186 testFailureDuringDecode(
187 "/LayoutTests/images/resources/png-simple.png",
188 85u, // idat offset for frame index 0
189 0u, // try to decode frame index 0
190 true, // expect the decoder to be invalidated after the failure
191 1u); // expected frame count before failure
192 }
193
194 // For static images, frameIsCompleteAtIndex(0) should return true if and only
195 // if the frame is successfully decoded, not when it is fully received.
196 TEST(StaticPNGTests, VerifyFrameCompleteBehavior) {
197 auto decoder =
198 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png");
199 EXPECT_EQ(1u, decoder->frameCount());
200 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0));
201 EXPECT_EQ(ImageFrame::FrameComplete,
202 decoder->frameBufferAtIndex(0)->getStatus());
203 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0));
204 }
205
206 }; // 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