| OLD | NEW |
| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 } | 138 } |
| 139 | 139 |
| 140 EXPECT_EQ(2u, decoder->frameCount()); | 140 EXPECT_EQ(2u, decoder->frameCount()); |
| 141 | 141 |
| 142 decoder->frameBufferAtIndex(0); | 142 decoder->frameBufferAtIndex(0); |
| 143 decoder->frameBufferAtIndex(1); | 143 decoder->frameBufferAtIndex(1); |
| 144 EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount()); | 144 EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount()); |
| 145 } | 145 } |
| 146 | 146 |
| 147 TEST(GIFImageDecoderTest, parseAndDecodeByteByByte) { | 147 TEST(GIFImageDecoderTest, parseAndDecodeByteByByte) { |
| 148 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | 148 testByteByByteDecode(&createDecoder, layoutTestResourcesDir, |
| 149 | 149 "animated-gif-with-offsets.gif", 5u, |
| 150 RefPtr<SharedBuffer> data = | 150 cAnimationLoopInfinite); |
| 151 readFile(layoutTestResourcesDir, "animated-gif-with-offsets.gif"); | |
| 152 ASSERT_TRUE(data.get()); | |
| 153 | |
| 154 size_t frameCount = 0; | |
| 155 size_t framesDecoded = 0; | |
| 156 | |
| 157 // Pass data to decoder byte by byte. | |
| 158 for (size_t length = 1; length <= data->size(); ++length) { | |
| 159 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length); | |
| 160 decoder->setData(tempData.get(), length == data->size()); | |
| 161 | |
| 162 EXPECT_LE(frameCount, decoder->frameCount()); | |
| 163 frameCount = decoder->frameCount(); | |
| 164 | |
| 165 ImageFrame* frame = decoder->frameBufferAtIndex(frameCount - 1); | |
| 166 if (frame && frame->getStatus() == ImageFrame::FrameComplete && | |
| 167 framesDecoded < frameCount) | |
| 168 ++framesDecoded; | |
| 169 } | |
| 170 | |
| 171 EXPECT_EQ(5u, decoder->frameCount()); | |
| 172 EXPECT_EQ(5u, framesDecoded); | |
| 173 EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount()); | |
| 174 } | 151 } |
| 175 | 152 |
| 176 TEST(GIFImageDecoderTest, brokenSecondFrame) { | 153 TEST(GIFImageDecoderTest, brokenSecondFrame) { |
| 177 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | 154 std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
| 178 | 155 |
| 179 RefPtr<SharedBuffer> data = readFile(decodersTestingDir, "broken.gif"); | 156 RefPtr<SharedBuffer> data = readFile(decodersTestingDir, "broken.gif"); |
| 180 ASSERT_TRUE(data.get()); | 157 ASSERT_TRUE(data.get()); |
| 181 decoder->setData(data.get(), true); | 158 decoder->setData(data.get(), true); |
| 182 | 159 |
| 183 // One frame is detected but cannot be decoded. | 160 // One frame is detected but cannot be decoded. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 testDecoder->setData(testData.get(), true); | 241 testDecoder->setData(testData.get(), true); |
| 265 EXPECT_EQ(1u, testDecoder->frameCount()); | 242 EXPECT_EQ(1u, testDecoder->frameCount()); |
| 266 ImageFrame* testFrame = testDecoder->frameBufferAtIndex(0); | 243 ImageFrame* testFrame = testDecoder->frameBufferAtIndex(0); |
| 267 ASSERT(testFrame); | 244 ASSERT(testFrame); |
| 268 | 245 |
| 269 EXPECT_EQ(hashBitmap(referenceFrame->bitmap()), | 246 EXPECT_EQ(hashBitmap(referenceFrame->bitmap()), |
| 270 hashBitmap(testFrame->bitmap())); | 247 hashBitmap(testFrame->bitmap())); |
| 271 } | 248 } |
| 272 | 249 |
| 273 TEST(GIFImageDecoderTest, updateRequiredPreviousFrameAfterFirstDecode) { | 250 TEST(GIFImageDecoderTest, updateRequiredPreviousFrameAfterFirstDecode) { |
| 274 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | 251 testUpdateRequiredPreviousFrameAfterFirstDecode( |
| 275 | 252 &createDecoder, layoutTestResourcesDir, "animated-10color.gif"); |
| 276 RefPtr<SharedBuffer> fullData = | |
| 277 readFile(layoutTestResourcesDir, "animated-10color.gif"); | |
| 278 ASSERT_TRUE(fullData.get()); | |
| 279 | |
| 280 // Give it data that is enough to parse but not decode in order to check the | |
| 281 // status. | |
| 282 // of requiredPreviousFrameIndex before decoding. | |
| 283 size_t partialSize = 1; | |
| 284 do { | |
| 285 RefPtr<SharedBuffer> data = | |
| 286 SharedBuffer::create(fullData->data(), partialSize); | |
| 287 decoder->setData(data.get(), false); | |
| 288 ++partialSize; | |
| 289 } while (!decoder->frameCount() || | |
| 290 decoder->frameBufferAtIndex(0)->getStatus() == | |
| 291 ImageFrame::FrameEmpty); | |
| 292 | |
| 293 EXPECT_EQ(kNotFound, | |
| 294 decoder->frameBufferAtIndex(0)->requiredPreviousFrameIndex()); | |
| 295 unsigned frameCount = decoder->frameCount(); | |
| 296 for (size_t i = 1; i < frameCount; ++i) | |
| 297 EXPECT_EQ(i - 1, | |
| 298 decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex()); | |
| 299 | |
| 300 decoder->setData(fullData.get(), true); | |
| 301 for (size_t i = 0; i < frameCount; ++i) | |
| 302 EXPECT_EQ(kNotFound, | |
| 303 decoder->frameBufferAtIndex(i)->requiredPreviousFrameIndex()); | |
| 304 } | 253 } |
| 305 | 254 |
| 306 TEST(GIFImageDecoderTest, randomFrameDecode) { | 255 TEST(GIFImageDecoderTest, randomFrameDecode) { |
| 307 // Single frame image. | 256 // Single frame image. |
| 308 testRandomFrameDecode(&createDecoder, decodersTestingDir, "radient.gif"); | 257 testRandomFrameDecode(&createDecoder, decodersTestingDir, "radient.gif"); |
| 309 // Multiple frame images. | 258 // Multiple frame images. |
| 310 testRandomFrameDecode(&createDecoder, layoutTestResourcesDir, | 259 testRandomFrameDecode(&createDecoder, layoutTestResourcesDir, |
| 311 "animated-gif-with-offsets.gif"); | 260 "animated-gif-with-offsets.gif"); |
| 312 testRandomFrameDecode(&createDecoder, layoutTestResourcesDir, | 261 testRandomFrameDecode(&createDecoder, layoutTestResourcesDir, |
| 313 "animated-10color.gif"); | 262 "animated-10color.gif"); |
| 314 } | 263 } |
| 315 | 264 |
| 316 TEST(GIFImageDecoderTest, randomDecodeAfterClearFrameBufferCache) { | 265 TEST(GIFImageDecoderTest, randomDecodeAfterClearFrameBufferCache) { |
| 317 // Single frame image. | 266 // Single frame image. |
| 318 testRandomDecodeAfterClearFrameBufferCache(&createDecoder, decodersTestingDir, | 267 testRandomDecodeAfterClearFrameBufferCache(&createDecoder, decodersTestingDir, |
| 319 "radient.gif"); | 268 "radient.gif"); |
| 320 // Multiple frame images. | 269 // Multiple frame images. |
| 321 testRandomDecodeAfterClearFrameBufferCache( | 270 testRandomDecodeAfterClearFrameBufferCache( |
| 322 &createDecoder, layoutTestResourcesDir, "animated-gif-with-offsets.gif"); | 271 &createDecoder, layoutTestResourcesDir, "animated-gif-with-offsets.gif"); |
| 323 testRandomDecodeAfterClearFrameBufferCache( | 272 testRandomDecodeAfterClearFrameBufferCache( |
| 324 &createDecoder, layoutTestResourcesDir, "animated-10color.gif"); | 273 &createDecoder, layoutTestResourcesDir, "animated-10color.gif"); |
| 325 } | 274 } |
| 326 | 275 |
| 327 TEST(GIFImageDecoderTest, resumePartialDecodeAfterClearFrameBufferCache) { | 276 TEST(GIFImageDecoderTest, resumePartialDecodeAfterClearFrameBufferCache) { |
| 328 RefPtr<SharedBuffer> fullData = | 277 testResumePartialDecodeAfterClearFrameBufferCache( |
| 329 readFile(layoutTestResourcesDir, "animated-10color.gif"); | 278 &createDecoder, layoutTestResourcesDir, "animated-10color.gif"); |
| 330 ASSERT_TRUE(fullData.get()); | |
| 331 Vector<unsigned> baselineHashes; | |
| 332 createDecodingBaseline(&createDecoder, fullData.get(), &baselineHashes); | |
| 333 size_t frameCount = baselineHashes.size(); | |
| 334 | |
| 335 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | |
| 336 | |
| 337 // Let frame 0 be partially decoded. | |
| 338 size_t partialSize = 1; | |
| 339 do { | |
| 340 RefPtr<SharedBuffer> data = | |
| 341 SharedBuffer::create(fullData->data(), partialSize); | |
| 342 decoder->setData(data.get(), false); | |
| 343 ++partialSize; | |
| 344 } while (!decoder->frameCount() || | |
| 345 decoder->frameBufferAtIndex(0)->getStatus() == | |
| 346 ImageFrame::FrameEmpty); | |
| 347 | |
| 348 // Skip to the last frame and clear. | |
| 349 decoder->setData(fullData.get(), true); | |
| 350 EXPECT_EQ(frameCount, decoder->frameCount()); | |
| 351 ImageFrame* lastFrame = decoder->frameBufferAtIndex(frameCount - 1); | |
| 352 EXPECT_EQ(baselineHashes[frameCount - 1], hashBitmap(lastFrame->bitmap())); | |
| 353 decoder->clearCacheExceptFrame(kNotFound); | |
| 354 | |
| 355 // Resume decoding of the first frame. | |
| 356 ImageFrame* firstFrame = decoder->frameBufferAtIndex(0); | |
| 357 EXPECT_EQ(ImageFrame::FrameComplete, firstFrame->getStatus()); | |
| 358 EXPECT_EQ(baselineHashes[0], hashBitmap(firstFrame->bitmap())); | |
| 359 } | 279 } |
| 360 | 280 |
| 361 // The first LZW codes in the image are invalid values that try to create a loop | 281 // The first LZW codes in the image are invalid values that try to create a loop |
| 362 // in the dictionary. Decoding should fail, but not infinitely loop or corrupt | 282 // in the dictionary. Decoding should fail, but not infinitely loop or corrupt |
| 363 // memory. | 283 // memory. |
| 364 TEST(GIFImageDecoderTest, badInitialCode) { | 284 TEST(GIFImageDecoderTest, badInitialCode) { |
| 365 RefPtr<SharedBuffer> testData = | 285 RefPtr<SharedBuffer> testData = |
| 366 readFile(decodersTestingDir, "bad-initial-code.gif"); | 286 readFile(decodersTestingDir, "bad-initial-code.gif"); |
| 367 ASSERT_TRUE(testData.get()); | 287 ASSERT_TRUE(testData.get()); |
| 368 | 288 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 EXPECT_TRUE(premulFrame && | 398 EXPECT_TRUE(premulFrame && |
| 479 premulFrame->getStatus() == ImageFrame::FrameComplete); | 399 premulFrame->getStatus() == ImageFrame::FrameComplete); |
| 480 EXPECT_EQ(premulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); | 400 EXPECT_EQ(premulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); |
| 481 unpremulFrame = unpremulDecoder->frameBufferAtIndex(0); | 401 unpremulFrame = unpremulDecoder->frameBufferAtIndex(0); |
| 482 EXPECT_TRUE(unpremulFrame && | 402 EXPECT_TRUE(unpremulFrame && |
| 483 unpremulFrame->getStatus() == ImageFrame::FrameComplete); | 403 unpremulFrame->getStatus() == ImageFrame::FrameComplete); |
| 484 EXPECT_EQ(unpremulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); | 404 EXPECT_EQ(unpremulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); |
| 485 } | 405 } |
| 486 | 406 |
| 487 } // namespace blink | 407 } // namespace blink |
| OLD | NEW |