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

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

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: Keep a cache of the max fully received frame index Created 3 years, 5 months 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 ImageDecoder::kNoDecodedImageByteLimit)); 52 ImageDecoder::kNoDecodedImageByteLimit));
53 } 53 }
54 54
55 void TestRepetitionCount(const char* dir, 55 void TestRepetitionCount(const char* dir,
56 const char* file, 56 const char* file,
57 int expected_repetition_count) { 57 int expected_repetition_count) {
58 std::unique_ptr<ImageDecoder> decoder = CreateDecoder(); 58 std::unique_ptr<ImageDecoder> decoder = CreateDecoder();
59 RefPtr<SharedBuffer> data = ReadFile(dir, file); 59 RefPtr<SharedBuffer> data = ReadFile(dir, file);
60 ASSERT_TRUE(data.Get()); 60 ASSERT_TRUE(data.Get());
61 decoder->SetData(data.Get(), true); 61 decoder->SetData(data.Get(), true);
62 EXPECT_EQ(kAnimationLoopOnce,
63 decoder->RepetitionCount()); // Default value before decode.
64 62
65 for (size_t i = 0; i < decoder->FrameCount(); ++i) { 63 EXPECT_EQ(expected_repetition_count, decoder->RepetitionCount());
66 ImageFrame* frame = decoder->FrameBufferAtIndex(i);
67 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus());
68 }
69
70 EXPECT_EQ(expected_repetition_count,
71 decoder->RepetitionCount()); // Expected value after decode.
72 } 64 }
73 65
74 } // anonymous namespace 66 } // anonymous namespace
75 67
76 TEST(GIFImageDecoderTest, decodeTwoFrames) { 68 TEST(GIFImageDecoderTest, decodeTwoFrames) {
77 std::unique_ptr<ImageDecoder> decoder = CreateDecoder(); 69 std::unique_ptr<ImageDecoder> decoder = CreateDecoder();
78 70
79 RefPtr<SharedBuffer> data = ReadFile(kLayoutTestResourcesDir, "animated.gif"); 71 RefPtr<SharedBuffer> data = ReadFile(kLayoutTestResourcesDir, "animated.gif");
80 ASSERT_TRUE(data.Get()); 72 ASSERT_TRUE(data.Get());
81 decoder->SetData(data.Get(), true); 73 decoder->SetData(data.Get(), true);
82 EXPECT_EQ(kAnimationLoopOnce, decoder->RepetitionCount());
83 74
84 ImageFrame* frame = decoder->FrameBufferAtIndex(0); 75 ImageFrame* frame = decoder->FrameBufferAtIndex(0);
85 uint32_t generation_id0 = frame->Bitmap().getGenerationID(); 76 uint32_t generation_id0 = frame->Bitmap().getGenerationID();
86 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus()); 77 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus());
87 EXPECT_EQ(16, frame->Bitmap().width()); 78 EXPECT_EQ(16, frame->Bitmap().width());
88 EXPECT_EQ(16, frame->Bitmap().height()); 79 EXPECT_EQ(16, frame->Bitmap().height());
89 80
90 frame = decoder->FrameBufferAtIndex(1); 81 frame = decoder->FrameBufferAtIndex(1);
91 uint32_t generation_id1 = frame->Bitmap().getGenerationID(); 82 uint32_t generation_id1 = frame->Bitmap().getGenerationID();
92 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus()); 83 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus());
93 EXPECT_EQ(16, frame->Bitmap().width()); 84 EXPECT_EQ(16, frame->Bitmap().width());
94 EXPECT_EQ(16, frame->Bitmap().height()); 85 EXPECT_EQ(16, frame->Bitmap().height());
95 EXPECT_TRUE(generation_id0 != generation_id1); 86 EXPECT_TRUE(generation_id0 != generation_id1);
96 87
97 EXPECT_EQ(2u, decoder->FrameCount()); 88 EXPECT_EQ(2u, decoder->FrameCount());
98 EXPECT_EQ(kAnimationLoopInfinite, decoder->RepetitionCount()); 89 EXPECT_EQ(kAnimationLoopInfinite, decoder->RepetitionCount());
99 } 90 }
100 91
101 TEST(GIFImageDecoderTest, parseAndDecode) { 92 TEST(GIFImageDecoderTest, parseAndDecode) {
102 std::unique_ptr<ImageDecoder> decoder = CreateDecoder(); 93 std::unique_ptr<ImageDecoder> decoder = CreateDecoder();
103 94
104 RefPtr<SharedBuffer> data = ReadFile(kLayoutTestResourcesDir, "animated.gif"); 95 RefPtr<SharedBuffer> data = ReadFile(kLayoutTestResourcesDir, "animated.gif");
105 ASSERT_TRUE(data.Get()); 96 ASSERT_TRUE(data.Get());
106 decoder->SetData(data.Get(), true); 97 decoder->SetData(data.Get(), true);
107 EXPECT_EQ(kAnimationLoopOnce, decoder->RepetitionCount());
108
109 // This call will parse the entire file.
110 EXPECT_EQ(2u, decoder->FrameCount());
111 98
112 ImageFrame* frame = decoder->FrameBufferAtIndex(0); 99 ImageFrame* frame = decoder->FrameBufferAtIndex(0);
113 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus()); 100 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus());
114 EXPECT_EQ(16, frame->Bitmap().width()); 101 EXPECT_EQ(16, frame->Bitmap().width());
115 EXPECT_EQ(16, frame->Bitmap().height()); 102 EXPECT_EQ(16, frame->Bitmap().height());
116 103
117 frame = decoder->FrameBufferAtIndex(1); 104 frame = decoder->FrameBufferAtIndex(1);
118 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus()); 105 EXPECT_EQ(ImageFrame::kFrameComplete, frame->GetStatus());
119 EXPECT_EQ(16, frame->Bitmap().width()); 106 EXPECT_EQ(16, frame->Bitmap().width());
120 EXPECT_EQ(16, frame->Bitmap().height()); 107 EXPECT_EQ(16, frame->Bitmap().height());
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 TEST(GIFImageDecoderTest, frameIsCompleteLoading) { 192 TEST(GIFImageDecoderTest, frameIsCompleteLoading) {
206 std::unique_ptr<ImageDecoder> decoder = CreateDecoder(); 193 std::unique_ptr<ImageDecoder> decoder = CreateDecoder();
207 194
208 RefPtr<SharedBuffer> data_buffer = 195 RefPtr<SharedBuffer> data_buffer =
209 ReadFile(kLayoutTestResourcesDir, "animated.gif"); 196 ReadFile(kLayoutTestResourcesDir, "animated.gif");
210 ASSERT_TRUE(data_buffer.Get()); 197 ASSERT_TRUE(data_buffer.Get());
211 const Vector<char> data = data_buffer->Copy(); 198 const Vector<char> data = data_buffer->Copy();
212 199
213 ASSERT_GE(data.size(), 10u); 200 ASSERT_GE(data.size(), 10u);
214 RefPtr<SharedBuffer> temp_data = 201 RefPtr<SharedBuffer> temp_data =
215 SharedBuffer::Create(data.data(), data.size() - 10); 202 SharedBuffer::Create(data.data(), data.size() - 34);
216 decoder->SetData(temp_data.Get(), false); 203 decoder->SetData(temp_data.Get(), false);
217 204
218 EXPECT_EQ(2u, decoder->FrameCount()); 205 EXPECT_EQ(1u, decoder->FrameCount());
219 EXPECT_FALSE(decoder->Failed()); 206 EXPECT_FALSE(decoder->Failed());
220 EXPECT_TRUE(decoder->FrameIsReceivedAtIndex(0)); 207 EXPECT_TRUE(decoder->FrameIsReceivedAtIndex(0));
221 EXPECT_FALSE(decoder->FrameIsReceivedAtIndex(1)); 208 EXPECT_FALSE(decoder->FrameIsReceivedAtIndex(1));
cblume 2017/07/26 07:43:30 You see how I updated to - 34 a few lines above? A
222 209
223 decoder->SetData(data_buffer.Get(), true); 210 decoder->SetData(data_buffer.Get(), true);
224 EXPECT_EQ(2u, decoder->FrameCount()); 211 EXPECT_EQ(2u, decoder->FrameCount());
225 EXPECT_TRUE(decoder->FrameIsReceivedAtIndex(0)); 212 EXPECT_TRUE(decoder->FrameIsReceivedAtIndex(0));
226 EXPECT_TRUE(decoder->FrameIsReceivedAtIndex(1)); 213 EXPECT_TRUE(decoder->FrameIsReceivedAtIndex(1));
227 } 214 }
228 215
229 TEST(GIFImageDecoderTest, badTerminator) { 216 TEST(GIFImageDecoderTest, badTerminator) {
230 RefPtr<SharedBuffer> reference_data = 217 RefPtr<SharedBuffer> reference_data =
231 ReadFile(kDecodersTestingDir, "radient.gif"); 218 ReadFile(kDecodersTestingDir, "radient.gif");
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 ReadFile(kDecodersTestingDir, "bad-code.gif"); 290 ReadFile(kDecodersTestingDir, "bad-code.gif");
304 ASSERT_TRUE(test_data.Get()); 291 ASSERT_TRUE(test_data.Get());
305 292
306 std::unique_ptr<ImageDecoder> test_decoder = CreateDecoder(); 293 std::unique_ptr<ImageDecoder> test_decoder = CreateDecoder();
307 test_decoder->SetData(test_data.Get(), true); 294 test_decoder->SetData(test_data.Get(), true);
308 EXPECT_EQ(1u, test_decoder->FrameCount()); 295 EXPECT_EQ(1u, test_decoder->FrameCount());
309 ASSERT_TRUE(test_decoder->FrameBufferAtIndex(0)); 296 ASSERT_TRUE(test_decoder->FrameBufferAtIndex(0));
310 EXPECT_TRUE(test_decoder->Failed()); 297 EXPECT_TRUE(test_decoder->Failed());
311 } 298 }
312 299
313 TEST(GIFImageDecoderTest, invalidDisposalMethod) {
314 std::unique_ptr<ImageDecoder> decoder = CreateDecoder();
315
316 // The image has 2 frames, with disposal method 4 and 5, respectively.
317 RefPtr<SharedBuffer> data =
318 ReadFile(kDecodersTestingDir, "invalid-disposal-method.gif");
319 ASSERT_TRUE(data.Get());
320 decoder->SetData(data.Get(), true);
321
322 EXPECT_EQ(2u, decoder->FrameCount());
323 // Disposal method 4 is converted to ImageFrame::DisposeOverwritePrevious.
324 EXPECT_EQ(ImageFrame::kDisposeOverwritePrevious,
325 decoder->FrameBufferAtIndex(0)->GetDisposalMethod());
326 // Disposal method 5 is ignored.
327 EXPECT_EQ(ImageFrame::kDisposeNotSpecified,
328 decoder->FrameBufferAtIndex(1)->GetDisposalMethod());
329 }
330
331 TEST(GIFImageDecoderTest, firstFrameHasGreaterSizeThanScreenSize) { 300 TEST(GIFImageDecoderTest, firstFrameHasGreaterSizeThanScreenSize) {
332 const Vector<char> full_data = 301 const Vector<char> full_data =
333 ReadFile(kDecodersTestingDir, 302 ReadFile(kDecodersTestingDir,
334 "first-frame-has-greater-size-than-screen-size.gif") 303 "first-frame-has-greater-size-than-screen-size.gif")
335 ->Copy(); 304 ->Copy();
336 305
337 std::unique_ptr<ImageDecoder> decoder; 306 std::unique_ptr<ImageDecoder> decoder;
338 IntSize frame_size; 307 IntSize frame_size;
339 308
340 // Compute hashes when the file is truncated. 309 // Compute hashes when the file is truncated.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 353
385 // Partially decoded frame => the frame alpha type is unknown and should 354 // Partially decoded frame => the frame alpha type is unknown and should
386 // reflect the requested format. 355 // reflect the requested format.
387 premul_decoder->SetData(partial_data.Get(), false); 356 premul_decoder->SetData(partial_data.Get(), false);
388 ASSERT_TRUE(premul_decoder->FrameCount()); 357 ASSERT_TRUE(premul_decoder->FrameCount());
389 unpremul_decoder->SetData(partial_data.Get(), false); 358 unpremul_decoder->SetData(partial_data.Get(), false);
390 ASSERT_TRUE(unpremul_decoder->FrameCount()); 359 ASSERT_TRUE(unpremul_decoder->FrameCount());
391 ImageFrame* premul_frame = premul_decoder->FrameBufferAtIndex(0); 360 ImageFrame* premul_frame = premul_decoder->FrameBufferAtIndex(0);
392 EXPECT_TRUE(premul_frame && 361 EXPECT_TRUE(premul_frame &&
393 premul_frame->GetStatus() != ImageFrame::kFrameComplete); 362 premul_frame->GetStatus() != ImageFrame::kFrameComplete);
394 EXPECT_EQ(premul_frame->Bitmap().alphaType(), kPremul_SkAlphaType); 363 EXPECT_EQ(kPremul_SkAlphaType, premul_frame->Bitmap().alphaType());
395 ImageFrame* unpremul_frame = unpremul_decoder->FrameBufferAtIndex(0); 364 ImageFrame* unpremul_frame = unpremul_decoder->FrameBufferAtIndex(0);
396 EXPECT_TRUE(unpremul_frame && 365 EXPECT_TRUE(unpremul_frame &&
397 unpremul_frame->GetStatus() != ImageFrame::kFrameComplete); 366 unpremul_frame->GetStatus() != ImageFrame::kFrameComplete);
398 EXPECT_EQ(unpremul_frame->Bitmap().alphaType(), kUnpremul_SkAlphaType); 367 EXPECT_EQ(kUnpremul_SkAlphaType, unpremul_frame->Bitmap().alphaType());
399 368
400 // Fully decoded frame => the frame alpha type is known (opaque). 369 // Fully decoded frame => the frame alpha type is known (opaque).
401 premul_decoder->SetData(full_data_buffer.Get(), true); 370 premul_decoder->SetData(full_data_buffer.Get(), true);
402 ASSERT_TRUE(premul_decoder->FrameCount()); 371 ASSERT_TRUE(premul_decoder->FrameCount());
403 unpremul_decoder->SetData(full_data_buffer.Get(), true); 372 unpremul_decoder->SetData(full_data_buffer.Get(), true);
404 ASSERT_TRUE(unpremul_decoder->FrameCount()); 373 ASSERT_TRUE(unpremul_decoder->FrameCount());
405 premul_frame = premul_decoder->FrameBufferAtIndex(0); 374 premul_frame = premul_decoder->FrameBufferAtIndex(0);
406 EXPECT_TRUE(premul_frame && 375 EXPECT_TRUE(premul_frame &&
407 premul_frame->GetStatus() == ImageFrame::kFrameComplete); 376 premul_frame->GetStatus() == ImageFrame::kFrameComplete);
408 EXPECT_EQ(premul_frame->Bitmap().alphaType(), kOpaque_SkAlphaType); 377 EXPECT_EQ(kOpaque_SkAlphaType, premul_frame->Bitmap().alphaType());
409 unpremul_frame = unpremul_decoder->FrameBufferAtIndex(0); 378 unpremul_frame = unpremul_decoder->FrameBufferAtIndex(0);
410 EXPECT_TRUE(unpremul_frame && 379 EXPECT_TRUE(unpremul_frame &&
411 unpremul_frame->GetStatus() == ImageFrame::kFrameComplete); 380 unpremul_frame->GetStatus() == ImageFrame::kFrameComplete);
412 EXPECT_EQ(unpremul_frame->Bitmap().alphaType(), kOpaque_SkAlphaType); 381 EXPECT_EQ(kOpaque_SkAlphaType, unpremul_frame->Bitmap().alphaType());
413 } 382 }
414 383
415 namespace { 384 namespace {
416 // Needed to exercise ImageDecoder::SetMemoryAllocator, but still does the 385 // Needed to exercise ImageDecoder::SetMemoryAllocator, but still does the
417 // default allocation. 386 // default allocation.
418 class Allocator final : public SkBitmap::Allocator { 387 class Allocator final : public SkBitmap::Allocator {
419 bool allocPixelRef(SkBitmap* dst) override { return dst->tryAllocPixels(); } 388 bool allocPixelRef(SkBitmap* dst) override { return dst->tryAllocPixels(); }
420 }; 389 };
421 } 390 }
422 391
(...skipping 11 matching lines...) Expand all
434 EXPECT_EQ(1u, decoder->FrameCount()); 403 EXPECT_EQ(1u, decoder->FrameCount());
435 ImageFrame* frame = decoder->FrameBufferAtIndex(0); 404 ImageFrame* frame = decoder->FrameBufferAtIndex(0);
436 decoder->SetMemoryAllocator(nullptr); 405 decoder->SetMemoryAllocator(nullptr);
437 406
438 ASSERT_TRUE(frame); 407 ASSERT_TRUE(frame);
439 EXPECT_EQ(IntRect(IntPoint(), decoder->Size()), frame->OriginalFrameRect()); 408 EXPECT_EQ(IntRect(IntPoint(), decoder->Size()), frame->OriginalFrameRect());
440 EXPECT_FALSE(frame->HasAlpha()); 409 EXPECT_FALSE(frame->HasAlpha());
441 } 410 }
442 411
443 } // namespace blink 412 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698