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

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoderTest.cpp

Issue 2490473005: Pull up equivalent image decoding tests (Closed)
Patch Set: Fixed feedback on patch 1 and ran WebKit formnatting Created 4 years, 1 month 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 19 matching lines...) Expand all
30 30
31 #include "platform/image-decoders/gif/GIFImageDecoder.h" 31 #include "platform/image-decoders/gif/GIFImageDecoder.h"
32 32
33 #include "platform/SharedBuffer.h" 33 #include "platform/SharedBuffer.h"
34 #include "platform/image-decoders/ImageDecoderTestHelpers.h" 34 #include "platform/image-decoders/ImageDecoderTestHelpers.h"
35 #include "public/platform/WebData.h" 35 #include "public/platform/WebData.h"
36 #include "public/platform/WebSize.h" 36 #include "public/platform/WebSize.h"
37 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "wtf/PtrUtil.h" 38 #include "wtf/PtrUtil.h"
39 #include "wtf/Vector.h" 39 #include "wtf/Vector.h"
40 #include "wtf/text/StringBuilder.h"
40 #include <memory> 41 #include <memory>
41 42
42 namespace blink { 43 namespace blink {
43 44
44 namespace { 45 namespace {
45 46
47 const char* concatDirAndFile(const char* dir, const char* file) {
48 StringBuilder filePath;
49 filePath.append(dir);
50 filePath.append('/');
51 filePath.append(file);
52 return reinterpret_cast<const char*>(filePath.characters8());
scroggo_chromium 2016/11/10 17:34:39 I missed this in patch set 2, where I think we hav
joostouwerling 2016/11/10 23:47:18 Done. From what I've seen in e.g. [1] it is fine t
53 }
54
46 const char layoutTestResourcesDir[] = "LayoutTests/fast/images/resources"; 55 const char layoutTestResourcesDir[] = "LayoutTests/fast/images/resources";
47 56
48 std::unique_ptr<ImageDecoder> createDecoder() { 57 std::unique_ptr<ImageDecoder> createDecoder() {
49 return wrapUnique(new GIFImageDecoder(ImageDecoder::AlphaNotPremultiplied, 58 return wrapUnique(new GIFImageDecoder(ImageDecoder::AlphaNotPremultiplied,
50 ImageDecoder::ColorSpaceApplied, 59 ImageDecoder::ColorSpaceApplied,
51 ImageDecoder::noDecodedImageByteLimit)); 60 ImageDecoder::noDecodedImageByteLimit));
52 } 61 }
53 62
54 void testRandomFrameDecode(const char* dir, const char* gifFile) { 63 void testRepetitionCount(const char* dir,
55 SCOPED_TRACE(gifFile); 64 const char* file,
65 int expectedRepetitionCount) {
66 std::unique_ptr<ImageDecoder> decoder = createDecoder();
67 RefPtr<SharedBuffer> data = readFile(dir, file);
68 ASSERT_TRUE(data.get());
69 decoder->setData(data.get(), true);
70 EXPECT_EQ(cAnimationLoopOnce,
71 decoder->repetitionCount()); // Default value before decode.
56 72
57 RefPtr<SharedBuffer> fullData = readFile(dir, gifFile); 73 for (size_t i = 0; i < decoder->frameCount(); ++i) {
58 ASSERT_TRUE(fullData.get()); 74 ImageFrame* frame = decoder->frameBufferAtIndex(i);
59 Vector<unsigned> baselineHashes; 75 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus());
60 createDecodingBaseline(&createDecoder, fullData.get(), &baselineHashes);
61 size_t frameCount = baselineHashes.size();
62
63 // Random decoding should get the same results as sequential decoding.
64 std::unique_ptr<ImageDecoder> decoder = createDecoder();
65 decoder->setData(fullData.get(), true);
66 const size_t skippingStep = 5;
67 for (size_t i = 0; i < skippingStep; ++i) {
68 for (size_t j = i; j < frameCount; j += skippingStep) {
69 SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j);
70 ImageFrame* frame = decoder->frameBufferAtIndex(j);
71 EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap()));
72 }
73 } 76 }
74 77
75 // Decoding in reverse order. 78 EXPECT_EQ(expectedRepetitionCount,
76 decoder = createDecoder(); 79 decoder->repetitionCount()); // Expected value after decode.
77 decoder->setData(fullData.get(), true);
78 for (size_t i = frameCount; i; --i) {
79 SCOPED_TRACE(testing::Message() << "Reverse i:" << i);
80 ImageFrame* frame = decoder->frameBufferAtIndex(i - 1);
81 EXPECT_EQ(baselineHashes[i - 1], hashBitmap(frame->bitmap()));
82 }
83 }
84
85 void testRandomDecodeAfterClearFrameBufferCache(const char* dir,
86 const char* gifFile) {
87 SCOPED_TRACE(gifFile);
88
89 RefPtr<SharedBuffer> data = readFile(dir, gifFile);
90 ASSERT_TRUE(data.get());
91 Vector<unsigned> baselineHashes;
92 createDecodingBaseline(&createDecoder, data.get(), &baselineHashes);
93 size_t frameCount = baselineHashes.size();
94
95 std::unique_ptr<ImageDecoder> decoder = createDecoder();
96 decoder->setData(data.get(), true);
97 for (size_t clearExceptFrame = 0; clearExceptFrame < frameCount;
98 ++clearExceptFrame) {
99 decoder->clearCacheExceptFrame(clearExceptFrame);
100 const size_t skippingStep = 5;
101 for (size_t i = 0; i < skippingStep; ++i) {
102 for (size_t j = 0; j < frameCount; j += skippingStep) {
103 SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j);
104 ImageFrame* frame = decoder->frameBufferAtIndex(j);
105 EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap()));
106 }
107 }
108 }
109 } 80 }
110 81
111 } // anonymous namespace 82 } // anonymous namespace
112 83
113 TEST(GIFImageDecoderTest, decodeTwoFrames) { 84 TEST(GIFImageDecoderTest, decodeTwoFrames) {
114 std::unique_ptr<ImageDecoder> decoder = createDecoder(); 85 std::unique_ptr<ImageDecoder> decoder = createDecoder();
115 86
116 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif"); 87 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif");
117 ASSERT_TRUE(data.get()); 88 ASSERT_TRUE(data.get());
118 decoder->setData(data.get(), true); 89 decoder->setData(data.get(), true);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 ASSERT_TRUE(data.get()); 189 ASSERT_TRUE(data.get());
219 decoder->setData(data.get(), true); 190 decoder->setData(data.get(), true);
220 191
221 // One frame is detected but cannot be decoded. 192 // One frame is detected but cannot be decoded.
222 EXPECT_EQ(1u, decoder->frameCount()); 193 EXPECT_EQ(1u, decoder->frameCount());
223 ImageFrame* frame = decoder->frameBufferAtIndex(1); 194 ImageFrame* frame = decoder->frameBufferAtIndex(1);
224 EXPECT_FALSE(frame); 195 EXPECT_FALSE(frame);
225 } 196 }
226 197
227 TEST(GIFImageDecoderTest, progressiveDecode) { 198 TEST(GIFImageDecoderTest, progressiveDecode) {
228 RefPtr<SharedBuffer> fullData = readFile(decodersTestingDir, "radient.gif"); 199 testProgressiveDecoding(&createDecoder,
229 ASSERT_TRUE(fullData.get()); 200 concatDirAndFile(decodersTestingDir, "radient.gif"));
230 const size_t fullLength = fullData->size();
231
232 std::unique_ptr<ImageDecoder> decoder;
233 ImageFrame* frame;
234
235 Vector<unsigned> truncatedHashes;
236 Vector<unsigned> progressiveHashes;
237
238 // Compute hashes when the file is truncated.
239 const size_t increment = 1;
240 for (size_t i = 1; i <= fullLength; i += increment) {
241 decoder = createDecoder();
242 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
243 decoder->setData(data.get(), i == fullLength);
244 frame = decoder->frameBufferAtIndex(0);
245 if (!frame) {
246 truncatedHashes.append(0);
247 continue;
248 }
249 truncatedHashes.append(hashBitmap(frame->bitmap()));
250 }
251
252 // Compute hashes when the file is progressively decoded.
253 decoder = createDecoder();
254 EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
255 for (size_t i = 1; i <= fullLength; i += increment) {
256 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i);
257 decoder->setData(data.get(), i == fullLength);
258 frame = decoder->frameBufferAtIndex(0);
259 if (!frame) {
260 progressiveHashes.append(0);
261 continue;
262 }
263 progressiveHashes.append(hashBitmap(frame->bitmap()));
264 }
265 EXPECT_EQ(cAnimationNone, decoder->repetitionCount());
266
267 bool match = true;
268 for (size_t i = 0; i < truncatedHashes.size(); ++i) {
269 if (truncatedHashes[i] != progressiveHashes[i]) {
270 match = false;
271 break;
272 }
273 }
274 EXPECT_TRUE(match);
275 } 201 }
276 202
277 TEST(GIFImageDecoderTest, allDataReceivedTruncation) { 203 TEST(GIFImageDecoderTest, allDataReceivedTruncation) {
278 std::unique_ptr<ImageDecoder> decoder = createDecoder(); 204 std::unique_ptr<ImageDecoder> decoder = createDecoder();
279 205
280 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif"); 206 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif");
281 ASSERT_TRUE(data.get()); 207 ASSERT_TRUE(data.get());
282 208
283 ASSERT_GE(data->size(), 10u); 209 ASSERT_GE(data->size(), 10u);
284 RefPtr<SharedBuffer> tempData = 210 RefPtr<SharedBuffer> tempData =
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex()); 308 decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex());
383 309
384 decoder->setData(fullData.get(), true); 310 decoder->setData(fullData.get(), true);
385 for (size_t i = 0; i < frameCount; ++i) 311 for (size_t i = 0; i < frameCount; ++i)
386 EXPECT_EQ(kNotFound, 312 EXPECT_EQ(kNotFound,
387 decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex()); 313 decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex());
388 } 314 }
389 315
390 TEST(GIFImageDecoderTest, randomFrameDecode) { 316 TEST(GIFImageDecoderTest, randomFrameDecode) {
391 // Single frame image. 317 // Single frame image.
392 testRandomFrameDecode(decodersTestingDir, "radient.gif"); 318 testRandomFrameDecode(&createDecoder,
319 concatDirAndFile(decodersTestingDir, "radient.gif"));
393 // Multiple frame images. 320 // Multiple frame images.
394 testRandomFrameDecode(layoutTestResourcesDir, 321 testRandomFrameDecode(&createDecoder,
395 "animated-gif-with-offsets.gif"); 322 concatDirAndFile(layoutTestResourcesDir,
396 testRandomFrameDecode(layoutTestResourcesDir, "animated-10color.gif"); 323 "animated-gif-with-offsets.gif"));
324 testRandomFrameDecode(
325 &createDecoder,
326 concatDirAndFile(layoutTestResourcesDir, "animated-10color.gif"));
397 } 327 }
398 328
399 TEST(GIFImageDecoderTest, randomDecodeAfterClearFrameBufferCache) { 329 TEST(GIFImageDecoderTest, randomDecodeAfterClearFrameBufferCache) {
400 // Single frame image. 330 // Single frame image.
401 testRandomDecodeAfterClearFrameBufferCache(decodersTestingDir, "radient.gif"); 331 testRandomDecodeAfterClearFrameBufferCache(
332 &createDecoder, concatDirAndFile(decodersTestingDir, "radient.gif"));
402 // Multiple frame images. 333 // Multiple frame images.
403 testRandomDecodeAfterClearFrameBufferCache(layoutTestResourcesDir, 334 testRandomDecodeAfterClearFrameBufferCache(
404 "animated-gif-with-offsets.gif"); 335 &createDecoder, concatDirAndFile(layoutTestResourcesDir,
405 testRandomDecodeAfterClearFrameBufferCache(layoutTestResourcesDir, 336 "animated-gif-with-offsets.gif"));
406 "animated-10color.gif"); 337 testRandomDecodeAfterClearFrameBufferCache(
338 &createDecoder,
339 concatDirAndFile(layoutTestResourcesDir, "animated-10color.gif"));
407 } 340 }
408 341
409 TEST(GIFImageDecoderTest, resumePartialDecodeAfterClearFrameBufferCache) { 342 TEST(GIFImageDecoderTest, resumePartialDecodeAfterClearFrameBufferCache) {
410 RefPtr<SharedBuffer> fullData = 343 RefPtr<SharedBuffer> fullData =
411 readFile(layoutTestResourcesDir, "animated-10color.gif"); 344 readFile(layoutTestResourcesDir, "animated-10color.gif");
412 ASSERT_TRUE(fullData.get()); 345 ASSERT_TRUE(fullData.get());
413 Vector<unsigned> baselineHashes; 346 Vector<unsigned> baselineHashes;
414 createDecodingBaseline(&createDecoder, fullData.get(), &baselineHashes); 347 createDecodingBaseline(&createDecoder, fullData.get(), &baselineHashes);
415 size_t frameCount = baselineHashes.size(); 348 size_t frameCount = baselineHashes.size();
416 349
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 frameSize = decoder->decodedSize(); 438 frameSize = decoder->decodedSize();
506 continue; 439 continue;
507 } 440 }
508 441
509 ASSERT_EQ(frameSize.width(), decoder->decodedSize().width()); 442 ASSERT_EQ(frameSize.width(), decoder->decodedSize().width());
510 ASSERT_EQ(frameSize.height(), decoder->decodedSize().height()); 443 ASSERT_EQ(frameSize.height(), decoder->decodedSize().height());
511 } 444 }
512 } 445 }
513 446
514 TEST(GIFImageDecoderTest, verifyRepetitionCount) { 447 TEST(GIFImageDecoderTest, verifyRepetitionCount) {
515 const int expectedRepetitionCount = 2; 448 testRepetitionCount(layoutTestResourcesDir, "full2loop.gif", 2);
516 std::unique_ptr<ImageDecoder> decoder = createDecoder(); 449 testRepetitionCount(decodersTestingDir, "radient.gif", cAnimationNone);
517 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "full2loop.gif");
518 ASSERT_TRUE(data.get());
519 decoder->setData(data.get(), true);
520 EXPECT_EQ(cAnimationLoopOnce,
521 decoder->repetitionCount()); // Default value before decode.
522
523 for (size_t i = 0; i < decoder->frameCount(); ++i) {
524 ImageFrame* frame = decoder->frameBufferAtIndex(i);
525 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus());
526 }
527
528 EXPECT_EQ(expectedRepetitionCount,
529 decoder->repetitionCount()); // Expected value after decode.
530 } 450 }
531 451
532 TEST(GIFImageDecoderTest, bitmapAlphaType) { 452 TEST(GIFImageDecoderTest, bitmapAlphaType) {
533 RefPtr<SharedBuffer> fullData = readFile(decodersTestingDir, "radient.gif"); 453 RefPtr<SharedBuffer> fullData = readFile(decodersTestingDir, "radient.gif");
534 ASSERT_TRUE(fullData.get()); 454 ASSERT_TRUE(fullData.get());
535 455
536 // Empirically chosen truncation size: 456 // Empirically chosen truncation size:
537 // a) large enough to produce a partial frame && 457 // a) large enough to produce a partial frame &&
538 // b) small enough to not fully decode the frame 458 // b) small enough to not fully decode the frame
539 const size_t kTruncateSize = 800; 459 const size_t kTruncateSize = 800;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 EXPECT_TRUE(premulFrame && 493 EXPECT_TRUE(premulFrame &&
574 premulFrame->getStatus() == ImageFrame::FrameComplete); 494 premulFrame->getStatus() == ImageFrame::FrameComplete);
575 EXPECT_EQ(premulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); 495 EXPECT_EQ(premulFrame->bitmap().alphaType(), kOpaque_SkAlphaType);
576 unpremulFrame = unpremulDecoder->frameBufferAtIndex(0); 496 unpremulFrame = unpremulDecoder->frameBufferAtIndex(0);
577 EXPECT_TRUE(unpremulFrame && 497 EXPECT_TRUE(unpremulFrame &&
578 unpremulFrame->getStatus() == ImageFrame::FrameComplete); 498 unpremulFrame->getStatus() == ImageFrame::FrameComplete);
579 EXPECT_EQ(unpremulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); 499 EXPECT_EQ(unpremulFrame->bitmap().alphaType(), kOpaque_SkAlphaType);
580 } 500 }
581 501
582 } // namespace blink 502 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698