1 /* 2 * Copyright (c) 2013, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "platform/graphics/BitmapImage.h" 33 34 #include "platform/SharedBuffer.h" 35 #include "platform/graphics/DeferredImageDecoder.h" 36 #include "platform/graphics/ImageObserver.h" 37 #include "public/platform/Platform.h" 38 #include "public/platform/WebUnitTestSupport.h" 39 40 #include <gtest/gtest.h> 41 42 namespace WebCore { 43 44 class BitmapImageTest : public ::testing::Test { 45 public: 46 class FakeImageObserver : public ImageObserver { 47 public: 48 FakeImageObserver() : m_lastDecodedSizeChangedDelta(0) { } 49 50 virtual void decodedSizeChanged(const Image*, int delta) 51 { 52 m_lastDecodedSizeChangedDelta = delta; 53 } 54 virtual void didDraw(const Image*) OVERRIDE { } 55 virtual bool shouldPauseAnimation(const Image*) OVERRIDE { return false; } 56 virtual void animationAdvanced(const Image*) OVERRIDE { } 57 virtual void changedInRect(const Image*, const IntRect&) { } 58 59 int m_lastDecodedSizeChangedDelta; 60 }; 61 62 static PassRefPtr<SharedBuffer> readFile(const char* fileName) 63 { 64 String filePath = blink::Platform::current()->unitTestSupport()->webKitRootDir(); 65 filePath.append(fileName); 66 return blink::Platform::current()->unitTestSupport()->readFromFile(filePath); 67 } 68 69 // Accessors to BitmapImage's protected methods. 70 void destroyDecodedData(bool destroyAll) { m_image->destroyDecodedData(destroyAll); } 71 size_t frameCount() { return m_image->frameCount(); } 72 void setCurrentFrame(size_t frame) { m_image->m_currentFrame = frame; } 73 size_t frameDecodedSize(size_t frame) { return m_image->m_frames[frame].m_frameBytes; } 74 size_t decodedFramesCount() const { return m_image->m_frames.size(); } 75 76 void loadImage(const char* fileName) 77 { 78 RefPtr<SharedBuffer> imageData = readFile(fileName); 79 ASSERT_TRUE(imageData.get()); 80 81 m_image->setData(imageData, true); 82 EXPECT_EQ(0u, decodedSize()); 83 84 size_t frameCount = m_image->frameCount(); 85 for (size_t i = 0; i < frameCount; ++i) 86 m_image->frameAtIndex(i); 87 } 88 89 size_t decodedSize() 90 { 91 // In the context of this test, the following loop will give the correct result, but only because the test 92 // forces all frames to be decoded in loadImage() above. There is no general guarantee that frameDecodedSize() 93 // is up-to-date. Because of how multi frame images (like GIF) work, requesting one frame to be decoded may 94 // require other previous frames to be decoded as well. In those cases frameDecodedSize() wouldn't return the 95 // correct thing for the previous frame because the decoded size wouldn't have propagated upwards to the 96 // BitmapImage frame cache. 97 size_t size = 0; 98 for (size_t i = 0; i < decodedFramesCount(); ++i) 99 size += frameDecodedSize(i); 100 return size; 101 } 102 103 void advanceAnimation() 104 { 105 m_image->advanceAnimation(0); 106 } 107 108 protected: 109 virtual void SetUp() OVERRIDE 110 { 111 DeferredImageDecoder::setEnabled(false); 112 m_image = BitmapImage::create(&m_imageObserver); 113 } 114 115 FakeImageObserver m_imageObserver; 116 RefPtr<BitmapImage> m_image; 117 }; 118 119 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104 120 #if OS(WIN) 121 TEST_F(BitmapImageTest, DISABLED_destroyDecodedDataExceptCurrentFrame) 122 #else 123 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame) 124 #endif 125 { 126 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); 127 size_t totalSize = decodedSize(); 128 size_t frame = frameCount() / 2; 129 setCurrentFrame(frame); 130 size_t size = frameDecodedSize(frame); 131 destroyDecodedData(false); 132 EXPECT_LT(m_imageObserver.m_lastDecodedSizeChangedDelta, 0); 133 EXPECT_GE(m_imageObserver.m_lastDecodedSizeChangedDelta, -static_cast<int>(totalSize - size)); 134 } 135 136 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104 137 #if OS(WIN) 138 TEST_F(BitmapImageTest, DISABLED_destroyAllDecodedData) 139 #else 140 TEST_F(BitmapImageTest, destroyAllDecodedData) 141 #endif 142 { 143 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); 144 size_t totalSize = decodedSize(); 145 EXPECT_GT(totalSize, 0u); 146 destroyDecodedData(true); 147 EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver.m_lastDecodedSizeChangedDelta); 148 EXPECT_EQ(0u, decodedSize()); 149 } 150 151 TEST_F(BitmapImageTest, maybeAnimated) 152 { 153 loadImage("/LayoutTests/fast/images/resources/gif-loop-count.gif"); 154 for (size_t i = 0; i < frameCount(); ++i) { 155 EXPECT_TRUE(m_image->maybeAnimated()); 156 advanceAnimation(); 157 } 158 EXPECT_FALSE(m_image->maybeAnimated()); 159 } 160 161 TEST_F(BitmapImageTest, isAllDataReceived) 162 { 163 RefPtr<SharedBuffer> imageData = readFile("/LayoutTests/fast/images/resources/green.jpg"); 164 ASSERT_TRUE(imageData.get()); 165 166 RefPtr<BitmapImage> image = BitmapImage::create(); 167 EXPECT_FALSE(image->isAllDataReceived()); 168 169 image->setData(imageData, false); 170 EXPECT_FALSE(image->isAllDataReceived()); 171 172 image->setData(imageData, true); 173 EXPECT_TRUE(image->isAllDataReceived()); 174 175 image->setData(SharedBuffer::create("data", sizeof("data")), false); 176 EXPECT_FALSE(image->isAllDataReceived()); 177 178 image->setData(imageData, true); 179 EXPECT_TRUE(image->isAllDataReceived()); 180 } 181 182 #if USE(QCMSLIB) 183 184 TEST_F(BitmapImageTest, jpegHasColorProfile) 185 { 186 loadImage("/LayoutTests/fast/images/resources/icc-v2-gbr.jpg"); 187 EXPECT_EQ(1u, decodedFramesCount()); 188 EXPECT_TRUE(m_image->hasColorProfile()); 189 } 190 191 TEST_F(BitmapImageTest, pngHasColorProfile) 192 { 193 loadImage("/LayoutTests/fast/images/resources/palatted-color-png-gamma-one-color-profile.png"); 194 EXPECT_EQ(1u, decodedFramesCount()); 195 EXPECT_TRUE(m_image->hasColorProfile()); 196 } 197 198 TEST_F(BitmapImageTest, webpHasColorProfile) 199 { 200 loadImage("/LayoutTests/fast/images/resources/webp-color-profile-lossy.webp"); 201 EXPECT_EQ(1u, decodedFramesCount()); 202 EXPECT_TRUE(m_image->hasColorProfile()); 203 } 204 205 #endif // USE(QCMSLIB) 206 207 } // namespace 208