| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 115 |
| 115 PassRefPtr<Image> imageForDefaultFrame() | 116 PassRefPtr<Image> imageForDefaultFrame() |
| 116 { | 117 { |
| 117 return m_image->imageForDefaultFrame(); | 118 return m_image->imageForDefaultFrame(); |
| 118 } | 119 } |
| 119 | 120 |
| 120 protected: | 121 protected: |
| 121 void SetUp() override | 122 void SetUp() override |
| 122 { | 123 { |
| 123 DeferredImageDecoder::setEnabled(m_enableDeferredDecoding); | 124 DeferredImageDecoder::setEnabled(m_enableDeferredDecoding); |
| 124 m_image = BitmapImage::create(&m_imageObserver); | 125 m_imageObserver = adoptPtrWillBeNoop(new FakeImageObserver); |
| 126 m_image = BitmapImage::create(m_imageObserver.get()); |
| 125 } | 127 } |
| 126 | 128 |
| 127 FakeImageObserver m_imageObserver; | 129 OwnPtrWillBePersistent<FakeImageObserver> m_imageObserver; |
| 128 RefPtr<BitmapImage> m_image; | 130 RefPtr<BitmapImage> m_image; |
| 129 | 131 |
| 130 private: | 132 private: |
| 131 bool m_enableDeferredDecoding; | 133 bool m_enableDeferredDecoding; |
| 132 }; | 134 }; |
| 133 | 135 |
| 134 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame) | 136 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame) |
| 135 { | 137 { |
| 136 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); | 138 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); |
| 137 size_t totalSize = decodedSize(); | 139 size_t totalSize = decodedSize(); |
| 138 size_t frame = frameCount() / 2; | 140 size_t frame = frameCount() / 2; |
| 139 setCurrentFrame(frame); | 141 setCurrentFrame(frame); |
| 140 size_t size = frameDecodedSize(frame); | 142 size_t size = frameDecodedSize(frame); |
| 141 destroyDecodedData(false); | 143 destroyDecodedData(false); |
| 142 EXPECT_LT(m_imageObserver.m_lastDecodedSizeChangedDelta, 0); | 144 EXPECT_LT(m_imageObserver->m_lastDecodedSizeChangedDelta, 0); |
| 143 EXPECT_GE(m_imageObserver.m_lastDecodedSizeChangedDelta, -static_cast<int>(t
otalSize - size)); | 145 EXPECT_GE(m_imageObserver->m_lastDecodedSizeChangedDelta, -static_cast<int>(
totalSize - size)); |
| 144 } | 146 } |
| 145 | 147 |
| 146 TEST_F(BitmapImageTest, destroyAllDecodedData) | 148 TEST_F(BitmapImageTest, destroyAllDecodedData) |
| 147 { | 149 { |
| 148 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); | 150 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); |
| 149 size_t totalSize = decodedSize(); | 151 size_t totalSize = decodedSize(); |
| 150 EXPECT_GT(totalSize, 0u); | 152 EXPECT_GT(totalSize, 0u); |
| 151 destroyDecodedData(true); | 153 destroyDecodedData(true); |
| 152 EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver.m_lastDecodedSizeCha
ngedDelta); | 154 EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver->m_lastDecodedSizeCh
angedDelta); |
| 153 EXPECT_EQ(0u, decodedSize()); | 155 EXPECT_EQ(0u, decodedSize()); |
| 154 } | 156 } |
| 155 | 157 |
| 156 TEST_F(BitmapImageTest, maybeAnimated) | 158 TEST_F(BitmapImageTest, maybeAnimated) |
| 157 { | 159 { |
| 158 loadImage("/LayoutTests/fast/images/resources/gif-loop-count.gif"); | 160 loadImage("/LayoutTests/fast/images/resources/gif-loop-count.gif"); |
| 159 for (size_t i = 0; i < frameCount(); ++i) { | 161 for (size_t i = 0; i < frameCount(); ++i) { |
| 160 EXPECT_TRUE(m_image->maybeAnimated()); | 162 EXPECT_TRUE(m_image->maybeAnimated()); |
| 161 advanceAnimation(); | 163 advanceAnimation(); |
| 162 } | 164 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 230 } |
| 229 | 231 |
| 230 TEST_F(BitmapImageTest, correctDecodedDataSize) | 232 TEST_F(BitmapImageTest, correctDecodedDataSize) |
| 231 { | 233 { |
| 232 // When requesting a frame of a multi-frame GIF causes another frame to be | 234 // When requesting a frame of a multi-frame GIF causes another frame to be |
| 233 // decoded as well, both frames' sizes should be included in the decoded | 235 // decoded as well, both frames' sizes should be included in the decoded |
| 234 // size changed notification. | 236 // size changed notification. |
| 235 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); | 237 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); |
| 236 frameAtIndex(1); | 238 frameAtIndex(1); |
| 237 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); | 239 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); |
| 238 EXPECT_EQ(frameSize * 2, m_imageObserver.m_lastDecodedSizeChangedDelta); | 240 EXPECT_EQ(frameSize * 2, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 239 | 241 |
| 240 // Trying to destroy all data except an undecoded frame should cause the | 242 // Trying to destroy all data except an undecoded frame should cause the |
| 241 // decoder to seek backwards and preserve the most recent previous frame | 243 // decoder to seek backwards and preserve the most recent previous frame |
| 242 // necessary to decode that undecoded frame, and destroy all other frames. | 244 // necessary to decode that undecoded frame, and destroy all other frames. |
| 243 setCurrentFrame(2); | 245 setCurrentFrame(2); |
| 244 destroyDecodedData(false); | 246 destroyDecodedData(false); |
| 245 EXPECT_EQ(-frameSize, m_imageObserver.m_lastDecodedSizeChangedDelta); | 247 EXPECT_EQ(-frameSize, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 246 } | 248 } |
| 247 | 249 |
| 248 class BitmapImageDeferredDecodingTest : public BitmapImageTest { | 250 class BitmapImageDeferredDecodingTest : public BitmapImageTest { |
| 249 public: | 251 public: |
| 250 BitmapImageDeferredDecodingTest() : BitmapImageTest(true) { } | 252 BitmapImageDeferredDecodingTest() : BitmapImageTest(true) { } |
| 251 }; | 253 }; |
| 252 | 254 |
| 253 TEST_F(BitmapImageDeferredDecodingTest, correctDecodedDataSize) | 255 TEST_F(BitmapImageDeferredDecodingTest, correctDecodedDataSize) |
| 254 { | 256 { |
| 255 // When deferred decoding is enabled, requesting any one frame shouldn't | 257 // When deferred decoding is enabled, requesting any one frame shouldn't |
| 256 // result in decoding any other frames. | 258 // result in decoding any other frames. |
| 257 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); | 259 loadImage("/LayoutTests/fast/images/resources/anim_none.gif", false); |
| 258 frameAtIndex(1); | 260 frameAtIndex(1); |
| 259 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); | 261 int frameSize = static_cast<int>(m_image->size().area() * sizeof(ImageFrame:
:PixelData)); |
| 260 EXPECT_EQ(frameSize, m_imageObserver.m_lastDecodedSizeChangedDelta); | 262 EXPECT_EQ(frameSize, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 261 frameAtIndex(0); | 263 frameAtIndex(0); |
| 262 | 264 |
| 263 // Trying to destroy all data except an undecoded frame should go ahead and | 265 // Trying to destroy all data except an undecoded frame should go ahead and |
| 264 // destroy all other frames. | 266 // destroy all other frames. |
| 265 setCurrentFrame(2); | 267 setCurrentFrame(2); |
| 266 destroyDecodedData(false); | 268 destroyDecodedData(false); |
| 267 EXPECT_EQ(-frameSize * 2, m_imageObserver.m_lastDecodedSizeChangedDelta); | 269 EXPECT_EQ(-frameSize * 2, m_imageObserver->m_lastDecodedSizeChangedDelta); |
| 268 } | 270 } |
| 269 | 271 |
| 270 } // namespace blink | 272 } // namespace blink |
| OLD | NEW |