| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/graphics/DeferredImageDecoder.h" | 5 #include "platform/graphics/DeferredImageDecoder.h" |
| 6 | 6 |
| 7 #include "platform/SharedBuffer.h" | 7 #include "platform/SharedBuffer.h" |
| 8 #include "platform/image-decoders/ImageDecoderTestHelpers.h" | 8 #include "platform/image-decoders/ImageDecoderTestHelpers.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/skia/include/core/SkImage.h" | 10 #include "third_party/skia/include/core/SkImage.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 * | 28 * |
| 29 * @param fileName File to decode | 29 * @param fileName File to decode |
| 30 * @param bytesForFirstFrame Number of bytes needed to return an SkImage | 30 * @param bytesForFirstFrame Number of bytes needed to return an SkImage |
| 31 * @param laterFrame Frame to decode with almost complete data. Can be 0. | 31 * @param laterFrame Frame to decode with almost complete data. Can be 0. |
| 32 */ | 32 */ |
| 33 static void mixImages(const char* fileName, size_t bytesForFirstFrame, size_t la
terFrame) | 33 static void mixImages(const char* fileName, size_t bytesForFirstFrame, size_t la
terFrame) |
| 34 { | 34 { |
| 35 RefPtr<SharedBuffer> file = readFile(fileName); | 35 RefPtr<SharedBuffer> file = readFile(fileName); |
| 36 ASSERT_NE(file, nullptr); | 36 ASSERT_NE(file, nullptr); |
| 37 | 37 |
| 38 std::unique_ptr<DeferredImageDecoder> decoder = DeferredImageDecoder::create
(ImageDecoder::determineImageType(*file.get()), ImageDecoder::AlphaPremultiplied
, ImageDecoder::GammaAndColorProfileIgnored); | |
| 39 ASSERT_TRUE(decoder.get()); | |
| 40 | |
| 41 RefPtr<SharedBuffer> partialFile = SharedBuffer::create(file->data(), bytesF
orFirstFrame); | 38 RefPtr<SharedBuffer> partialFile = SharedBuffer::create(file->data(), bytesF
orFirstFrame); |
| 42 decoder->setData(*partialFile.get(), false); | 39 std::unique_ptr<DeferredImageDecoder> decoder = DeferredImageDecoder::create
(partialFile, false, |
| 40 ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileIgno
red); |
| 41 ASSERT_NE(decoder, nullptr); |
| 43 RefPtr<SkImage> partialImage = decoder->createFrameAtIndex(0); | 42 RefPtr<SkImage> partialImage = decoder->createFrameAtIndex(0); |
| 44 | 43 |
| 45 RefPtr<SharedBuffer> almostCompleteFile = SharedBuffer::create(file->data(),
file->size() - 1); | 44 RefPtr<SharedBuffer> almostCompleteFile = SharedBuffer::create(file->data(),
file->size() - 1); |
| 46 decoder->setData(*almostCompleteFile.get(), false); | 45 decoder->setData(almostCompleteFile, false); |
| 47 RefPtr<SkImage> imageWithMoreData = decoder->createFrameAtIndex(laterFrame); | 46 RefPtr<SkImage> imageWithMoreData = decoder->createFrameAtIndex(laterFrame); |
| 48 | 47 |
| 49 imageWithMoreData->preroll(); | 48 imageWithMoreData->preroll(); |
| 50 partialImage->preroll(); | 49 partialImage->preroll(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 TEST(DeferredImageDecoderTestWoPlatform, mixImagesGif) | 52 TEST(DeferredImageDecoderTestWoPlatform, mixImagesGif) |
| 54 { | 53 { |
| 55 mixImages("/LayoutTests/fast/images/resources/animated.gif", 818u, 1u); | 54 mixImages("/LayoutTests/fast/images/resources/animated.gif", 818u, 1u); |
| 56 } | 55 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 75 mixImages("/LayoutTests/fast/images/resources/lenna.bmp", 122u, 0u); | 74 mixImages("/LayoutTests/fast/images/resources/lenna.bmp", 122u, 0u); |
| 76 } | 75 } |
| 77 | 76 |
| 78 TEST(DeferredImageDecoderTestWoPlatform, mixImagesIco) | 77 TEST(DeferredImageDecoderTestWoPlatform, mixImagesIco) |
| 79 { | 78 { |
| 80 mixImages("/LayoutTests/fast/images/resources/wrong-frame-dimensions.ico", 1
376u, 1u); | 79 mixImages("/LayoutTests/fast/images/resources/wrong-frame-dimensions.ico", 1
376u, 1u); |
| 81 } | 80 } |
| 82 | 81 |
| 83 TEST(DeferredImageDecoderTestWoPlatform, fragmentedSignature) | 82 TEST(DeferredImageDecoderTestWoPlatform, fragmentedSignature) |
| 84 { | 83 { |
| 85 struct { | 84 const char* testFiles[] = { |
| 86 const char* m_file; | 85 "/LayoutTests/fast/images/resources/animated.gif", |
| 87 ImageDecoder::SniffResult m_expectedResult; | 86 "/LayoutTests/fast/images/resources/mu.png", |
| 88 } tests[] = { | 87 "/LayoutTests/fast/images/resources/2-dht.jpg", |
| 89 { "/LayoutTests/fast/images/resources/animated.gif", ImageDecoder::Sniff
Result::GIF }, | 88 "/LayoutTests/fast/images/resources/webp-animated.webp", |
| 90 { "/LayoutTests/fast/images/resources/mu.png", ImageDecoder::SniffResult
::PNG }, | 89 "/LayoutTests/fast/images/resources/lenna.bmp", |
| 91 { "/LayoutTests/fast/images/resources/2-dht.jpg", ImageDecoder::SniffRes
ult::JPEG }, | 90 "/LayoutTests/fast/images/resources/wrong-frame-dimensions.ico", |
| 92 { "/LayoutTests/fast/images/resources/webp-animated.webp", ImageDecoder:
:SniffResult::WEBP }, | |
| 93 { "/LayoutTests/fast/images/resources/lenna.bmp", ImageDecoder::SniffRes
ult::BMP }, | |
| 94 { "/LayoutTests/fast/images/resources/wrong-frame-dimensions.ico", Image
Decoder::SniffResult::ICO }, | |
| 95 }; | 91 }; |
| 96 | 92 |
| 97 for (size_t i = 0; i < SK_ARRAY_COUNT(tests); ++i) { | 93 for (size_t i = 0; i < SK_ARRAY_COUNT(testFiles); ++i) { |
| 98 RefPtr<SharedBuffer> fileBuffer = readFile(tests[i].m_file); | 94 RefPtr<SharedBuffer> fileBuffer = readFile(testFiles[i]); |
| 99 ASSERT_NE(fileBuffer, nullptr); | 95 ASSERT_NE(fileBuffer, nullptr); |
| 100 // We need contiguous data, which SharedBuffer doesn't guarantee. | 96 // We need contiguous data, which SharedBuffer doesn't guarantee. |
| 101 sk_sp<SkData> skData = fileBuffer->getAsSkData(); | 97 sk_sp<SkData> skData = fileBuffer->getAsSkData(); |
| 102 ASSERT_EQ(skData->size(), fileBuffer->size()); | 98 EXPECT_EQ(skData->size(), fileBuffer->size()); |
| 103 const char* data = reinterpret_cast<const char*>(skData->bytes()); | 99 const char* data = reinterpret_cast<const char*>(skData->bytes()); |
| 104 | 100 |
| 105 // Truncated signature (only 1 byte) | 101 // Truncated signature (only 1 byte). Decoder instantiation should fail
. |
| 106 RefPtr<SharedBuffer> buffer = SharedBuffer::create<size_t>(data, 1u); | 102 RefPtr<SharedBuffer> buffer = SharedBuffer::create<size_t>(data, 1u); |
| 107 ASSERT_EQ(ImageDecoder::determineImageType(*buffer), ImageDecoder::Sniff
Result::InsufficientData); | 103 EXPECT_FALSE(ImageDecoder::hasSufficientDataToSniffImageType(*buffer)); |
| 104 EXPECT_EQ(nullptr, DeferredImageDecoder::create(buffer, false, |
| 105 ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfile
Ignored)); |
| 108 | 106 |
| 109 // Append the rest of the data. We should be able to sniff the signatur
e now, even if segmented. | 107 // Append the rest of the data. We should be able to sniff the signatur
e now, even if segmented. |
| 110 buffer->append<size_t>(data + 1, skData->size() - 1); | 108 buffer->append<size_t>(data + 1, skData->size() - 1); |
| 111 ASSERT_EQ(ImageDecoder::determineImageType(*buffer), tests[i].m_expected
Result); | 109 EXPECT_TRUE(ImageDecoder::hasSufficientDataToSniffImageType(*buffer)); |
| 110 std::unique_ptr<DeferredImageDecoder> decoder = DeferredImageDecoder::cr
eate(buffer, false, |
| 111 ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfile
Ignored); |
| 112 ASSERT_NE(decoder, nullptr); |
| 113 EXPECT_TRUE(String(testFiles[i]).endsWith(decoder->filenameExtension()))
; |
| 112 } | 114 } |
| 113 } | 115 } |
| 114 | 116 |
| 115 } // namespace blink | 117 } // namespace blink |
| OLD | NEW |