| 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 22 matching lines...) Expand all Loading... |
| 33 #include "platform/SharedBuffer.h" | 33 #include "platform/SharedBuffer.h" |
| 34 #include "platform/graphics/DeferredImageDecoder.h" | 34 #include "platform/graphics/DeferredImageDecoder.h" |
| 35 #include "platform/graphics/ImageObserver.h" | 35 #include "platform/graphics/ImageObserver.h" |
| 36 #include "platform/testing/UnitTestHelpers.h" | 36 #include "platform/testing/UnitTestHelpers.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 38 | 38 |
| 39 namespace blink { | 39 namespace blink { |
| 40 | 40 |
| 41 class BitmapImageTest : public ::testing::Test { | 41 class BitmapImageTest : public ::testing::Test { |
| 42 public: | 42 public: |
| 43 class FakeImageObserver : public ImageObserver { | 43 class FakeImageObserver : public NoBaseWillBeGarbageCollectedFinalized<FakeI
mageObserver>, public ImageObserver { |
| 44 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(FakeImageObserver); |
| 44 public: | 45 public: |
| 45 FakeImageObserver() : m_lastDecodedSizeChangedDelta(0) { } | 46 FakeImageObserver() : m_lastDecodedSizeChangedDelta(0) { } |
| 46 | 47 |
| 47 virtual void decodedSizeChanged(const Image*, int delta) | 48 virtual void decodedSizeChanged(const Image*, int delta) |
| 48 { | 49 { |
| 49 m_lastDecodedSizeChangedDelta = delta; | 50 m_lastDecodedSizeChangedDelta = delta; |
| 50 } | 51 } |
| 51 void didDraw(const Image*) override { } | 52 void didDraw(const Image*) override { } |
| 52 bool shouldPauseAnimation(const Image*) override { return false; } | 53 bool shouldPauseAnimation(const Image*) override { return false; } |
| 53 void animationAdvanced(const Image*) override { } | 54 void animationAdvanced(const Image*) override { } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 117 |
| 117 PassRefPtr<Image> imageForDefaultFrame() | 118 PassRefPtr<Image> imageForDefaultFrame() |
| 118 { | 119 { |
| 119 return m_image->imageForDefaultFrame(); | 120 return m_image->imageForDefaultFrame(); |
| 120 } | 121 } |
| 121 | 122 |
| 122 protected: | 123 protected: |
| 123 void SetUp() override | 124 void SetUp() override |
| 124 { | 125 { |
| 125 DeferredImageDecoder::setEnabled(m_enableDeferredDecoding); | 126 DeferredImageDecoder::setEnabled(m_enableDeferredDecoding); |
| 126 m_image = BitmapImage::create(&m_imageObserver); | 127 m_imageObserver = adoptPtrWillBeNoop(new FakeImageObserver); |
| 128 m_image = BitmapImage::create(m_imageObserver.get()); |
| 127 } | 129 } |
| 128 | 130 |
| 129 FakeImageObserver m_imageObserver; | 131 OwnPtrWillBePersistent<FakeImageObserver> m_imageObserver; |
| 130 RefPtr<BitmapImage> m_image; | 132 RefPtr<BitmapImage> m_image; |
| 131 | 133 |
| 132 private: | 134 private: |
| 133 bool m_enableDeferredDecoding; | 135 bool m_enableDeferredDecoding; |
| 134 }; | 136 }; |
| 135 | 137 |
| 136 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame) | 138 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame) |
| 137 { | 139 { |
| 138 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); | 140 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); |
| 139 size_t totalSize = decodedSize(); | 141 size_t totalSize = decodedSize(); |
| 140 size_t frame = frameCount() / 2; | 142 size_t frame = frameCount() / 2; |
| 141 setCurrentFrame(frame); | 143 setCurrentFrame(frame); |
| 142 size_t size = frameDecodedSize(frame); | 144 size_t size = frameDecodedSize(frame); |
| 143 destroyDecodedData(false); | 145 destroyDecodedData(false); |
| 144 EXPECT_LT(m_imageObserver.m_lastDecodedSizeChangedDelta, 0); | 146 EXPECT_LT(m_imageObserver->m_lastDecodedSizeChangedDelta, 0); |
| 145 EXPECT_GE(m_imageObserver.m_lastDecodedSizeChangedDelta, -static_cast<int>(t
otalSize - size)); | 147 EXPECT_GE(m_imageObserver->m_lastDecodedSizeChangedDelta, -static_cast<int>(
totalSize - size)); |
| 146 } | 148 } |
| 147 | 149 |
| 148 TEST_F(BitmapImageTest, destroyAllDecodedData) | 150 TEST_F(BitmapImageTest, destroyAllDecodedData) |
| 149 { | 151 { |
| 150 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); | 152 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); |
| 151 size_t totalSize = decodedSize(); | 153 size_t totalSize = decodedSize(); |
| 152 EXPECT_GT(totalSize, 0u); | 154 EXPECT_GT(totalSize, 0u); |
| 153 destroyDecodedData(true); | 155 destroyDecodedData(true); |
| 154 EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver.m_lastDecodedSizeCha
ngedDelta); | 156 EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver->m_lastDecodedSizeCh
angedDelta); |
| 155 EXPECT_EQ(0u, decodedSize()); | 157 EXPECT_EQ(0u, decodedSize()); |
| 156 } | 158 } |
| 157 | 159 |
| 158 TEST_F(BitmapImageTest, maybeAnimated) | 160 TEST_F(BitmapImageTest, maybeAnimated) |
| 159 { | 161 { |
| 160 loadImage("/LayoutTests/fast/images/resources/gif-loop-count.gif"); | 162 loadImage("/LayoutTests/fast/images/resources/gif-loop-count.gif"); |
| 161 for (size_t i = 0; i < frameCount(); ++i) { | 163 for (size_t i = 0; i < frameCount(); ++i) { |
| 162 EXPECT_TRUE(m_image->maybeAnimated()); | 164 EXPECT_TRUE(m_image->maybeAnimated()); |
| 163 advanceAnimation(); | 165 advanceAnimation(); |
| 164 } | 166 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 232 } |
| 231 | 233 |
| 232 TEST_F(BitmapImageTest, correctDecodedDataSize) | 234 TEST_F(BitmapImageTest, correctDecodedDataSize) |
| 233 { | 235 { |
| 234 // When requesting a frame of a multi-frame GIF causes another frame to be | 236 // When requesting a frame of a multi-frame GIF causes another frame to be |
| 235 // decoded as well, both frames' sizes should be reported by the source and | 237 // decoded as well, both frames' sizes should be reported by the source and |
| 236 // thus included in the decoded size changed notification. | 238 // thus included in the decoded size changed notification. |
| 237 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); | 239 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); |
| 238 frameAtIndex(1); | 240 frameAtIndex(1); |
| 239 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); | 241 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); |
| 240 EXPECT_EQ(frameSize * 2, m_imageObserver.m_lastDecodedSizeChangedDelta); | 242 EXPECT_EQ(frameSize * 2, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 241 | 243 |
| 242 // Trying to destroy all data except an undecoded frame should cause the | 244 // Trying to destroy all data except an undecoded frame should cause the |
| 243 // decoder to seek backwards and preserve the most recent previous frame | 245 // decoder to seek backwards and preserve the most recent previous frame |
| 244 // necessary to decode that undecoded frame, and destroy all other frames. | 246 // necessary to decode that undecoded frame, and destroy all other frames. |
| 245 setCurrentFrame(2); | 247 setCurrentFrame(2); |
| 246 destroyDecodedData(false); | 248 destroyDecodedData(false); |
| 247 EXPECT_EQ(-frameSize, m_imageObserver.m_lastDecodedSizeChangedDelta); | 249 EXPECT_EQ(-frameSize, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 248 } | 250 } |
| 249 | 251 |
| 250 TEST_F(BitmapImageTest, recachingFrameAfterDataChanged) | 252 TEST_F(BitmapImageTest, recachingFrameAfterDataChanged) |
| 251 { | 253 { |
| 252 loadImage("/LayoutTests/fast/images/resources/green.jpg"); | 254 loadImage("/LayoutTests/fast/images/resources/green.jpg"); |
| 253 setFirstFrameNotComplete(); | 255 setFirstFrameNotComplete(); |
| 254 EXPECT_GT(m_imageObserver.m_lastDecodedSizeChangedDelta, 0); | 256 EXPECT_GT(m_imageObserver->m_lastDecodedSizeChangedDelta, 0); |
| 255 m_imageObserver.m_lastDecodedSizeChangedDelta = 0; | 257 m_imageObserver->m_lastDecodedSizeChangedDelta = 0; |
| 256 | 258 |
| 257 // Calling dataChanged causes the cache to flush, but doesn't affect the | 259 // Calling dataChanged causes the cache to flush, but doesn't affect the |
| 258 // source's decoded frames. It shouldn't affect decoded size. | 260 // source's decoded frames. It shouldn't affect decoded size. |
| 259 m_image->dataChanged(true); | 261 m_image->dataChanged(true); |
| 260 EXPECT_EQ(0, m_imageObserver.m_lastDecodedSizeChangedDelta); | 262 EXPECT_EQ(0, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 261 // Recaching the first frame also shouldn't affect decoded size. | 263 // Recaching the first frame also shouldn't affect decoded size. |
| 262 m_image->imageForCurrentFrame(); | 264 m_image->imageForCurrentFrame(); |
| 263 EXPECT_EQ(0, m_imageObserver.m_lastDecodedSizeChangedDelta); | 265 EXPECT_EQ(0, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 264 } | 266 } |
| 265 | 267 |
| 266 class BitmapImageDeferredDecodingTest : public BitmapImageTest { | 268 class BitmapImageDeferredDecodingTest : public BitmapImageTest { |
| 267 public: | 269 public: |
| 268 BitmapImageDeferredDecodingTest() : BitmapImageTest(true) { } | 270 BitmapImageDeferredDecodingTest() : BitmapImageTest(true) { } |
| 269 }; | 271 }; |
| 270 | 272 |
| 271 TEST_F(BitmapImageDeferredDecodingTest, correctDecodedDataSize) | 273 TEST_F(BitmapImageDeferredDecodingTest, correctDecodedDataSize) |
| 272 { | 274 { |
| 273 // When deferred decoding is enabled, requesting any one frame shouldn't | 275 // When deferred decoding is enabled, requesting any one frame shouldn't |
| 274 // result in decoding any other frames. | 276 // result in decoding any other frames. |
| 275 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); | 277 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); |
| 276 frameAtIndex(1); | 278 frameAtIndex(1); |
| 277 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); | 279 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); |
| 278 EXPECT_EQ(frameSize, m_imageObserver.m_lastDecodedSizeChangedDelta); | 280 EXPECT_EQ(frameSize, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 279 frameAtIndex(0); | 281 frameAtIndex(0); |
| 280 | 282 |
| 281 // Trying to destroy all data except an undecoded frame should go ahead and | 283 // Trying to destroy all data except an undecoded frame should go ahead and |
| 282 // destroy all other frames. | 284 // destroy all other frames. |
| 283 setCurrentFrame(2); | 285 setCurrentFrame(2); |
| 284 destroyDecodedData(false); | 286 destroyDecodedData(false); |
| 285 EXPECT_EQ(-frameSize * 2, m_imageObserver.m_lastDecodedSizeChangedDelta); | 287 EXPECT_EQ(-frameSize * 2, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 286 } | 288 } |
| 287 | 289 |
| 288 } // namespace blink | 290 } // namespace blink |
| OLD | NEW |