2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "platform/graphics/BitmapImage.h"
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"
40 #include <gtest/gtest.h>
44 class BitmapImageTest : public ::testing::Test {
46 class FakeImageObserver : public ImageObserver {
48 FakeImageObserver() : m_lastDecodedSizeChangedDelta(0) { }
50 virtual void decodedSizeChanged(const Image*, int delta)
52 m_lastDecodedSizeChangedDelta = delta;
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&) { }
59 int m_lastDecodedSizeChangedDelta;
62 static PassRefPtr<SharedBuffer> readFile(const char* fileName)
64 String filePath = Platform::current()->unitTestSupport()->webKitRootDir();
65 filePath.append(fileName);
66 return Platform::current()->unitTestSupport()->readFromFile(filePath);
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 void resetDecoder() { return m_image->resetDecoder(); }
77 void loadImage(const char* fileName)
79 RefPtr<SharedBuffer> imageData = readFile(fileName);
80 ASSERT_TRUE(imageData.get());
82 m_image->setData(imageData, true);
83 EXPECT_EQ(0u, decodedSize());
85 size_t frameCount = m_image->frameCount();
86 for (size_t i = 0; i < frameCount; ++i)
87 m_image->frameAtIndex(i);
92 // In the context of this test, the following loop will give the correct result, but only because the test
93 // forces all frames to be decoded in loadImage() above. There is no general guarantee that frameDecodedSize()
94 // is up-to-date. Because of how multi frame images (like GIF) work, requesting one frame to be decoded may
95 // require other previous frames to be decoded as well. In those cases frameDecodedSize() wouldn't return the
96 // correct thing for the previous frame because the decoded size wouldn't have propagated upwards to the
97 // BitmapImage frame cache.
99 for (size_t i = 0; i < decodedFramesCount(); ++i)
100 size += frameDecodedSize(i);
104 void advanceAnimation()
106 m_image->advanceAnimation(0);
109 PassRefPtr<Image> imageForDefaultFrame()
111 return m_image->imageForDefaultFrame();
115 virtual void SetUp() OVERRIDE
117 DeferredImageDecoder::setEnabled(false);
118 m_image = BitmapImage::create(&m_imageObserver);
121 FakeImageObserver m_imageObserver;
122 RefPtr<BitmapImage> m_image;
125 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104
127 TEST_F(BitmapImageTest, DISABLED_destroyDecodedDataExceptCurrentFrame)
129 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame)
132 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif");
133 size_t totalSize = decodedSize();
134 size_t frame = frameCount() / 2;
135 setCurrentFrame(frame);
136 size_t size = frameDecodedSize(frame);
137 destroyDecodedData(false);
138 EXPECT_LT(m_imageObserver.m_lastDecodedSizeChangedDelta, 0);
139 EXPECT_GE(m_imageObserver.m_lastDecodedSizeChangedDelta, -static_cast<int>(totalSize - size));
142 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104
144 TEST_F(BitmapImageTest, DISABLED_destroyAllDecodedData)
146 TEST_F(BitmapImageTest, destroyAllDecodedData)
149 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif");
150 size_t totalSize = decodedSize();
151 EXPECT_GT(totalSize, 0u);
152 destroyDecodedData(true);
153 EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver.m_lastDecodedSizeChangedDelta);
154 EXPECT_EQ(0u, decodedSize());
157 TEST_F(BitmapImageTest, maybeAnimated)
159 loadImage("/LayoutTests/fast/images/resources/gif-loop-count.gif");
160 for (size_t i = 0; i < frameCount(); ++i) {
161 EXPECT_TRUE(m_image->maybeAnimated());
164 EXPECT_FALSE(m_image->maybeAnimated());
167 TEST_F(BitmapImageTest, isAllDataReceived)
169 RefPtr<SharedBuffer> imageData = readFile("/LayoutTests/fast/images/resources/green.jpg");
170 ASSERT_TRUE(imageData.get());
172 RefPtr<BitmapImage> image = BitmapImage::create();
173 EXPECT_FALSE(image->isAllDataReceived());
175 image->setData(imageData, false);
176 EXPECT_FALSE(image->isAllDataReceived());
178 image->setData(imageData, true);
179 EXPECT_TRUE(image->isAllDataReceived());
181 image->setData(SharedBuffer::create("data", sizeof("data")), false);
182 EXPECT_FALSE(image->isAllDataReceived());
184 image->setData(imageData, true);
185 EXPECT_TRUE(image->isAllDataReceived());
190 TEST_F(BitmapImageTest, jpegHasColorProfile)
192 loadImage("/LayoutTests/fast/images/resources/icc-v2-gbr.jpg");
193 EXPECT_EQ(1u, decodedFramesCount());
194 EXPECT_EQ(227700u, decodedSize());
195 EXPECT_TRUE(m_image->hasColorProfile());
198 destroyDecodedData(true);
200 loadImage("/LayoutTests/fast/images/resources/green.jpg");
201 EXPECT_EQ(1u, decodedFramesCount());
202 EXPECT_EQ(1024u, decodedSize());
203 EXPECT_FALSE(m_image->hasColorProfile());
206 TEST_F(BitmapImageTest, pngHasColorProfile)
208 loadImage("/LayoutTests/fast/images/resources/palatted-color-png-gamma-one-color-profile.png");
209 EXPECT_EQ(1u, decodedFramesCount());
210 EXPECT_EQ(65536u, decodedSize());
211 EXPECT_TRUE(m_image->hasColorProfile());
214 destroyDecodedData(true);
216 loadImage("/LayoutTests/fast/images/resources/green.jpg");
217 EXPECT_EQ(1u, decodedFramesCount());
218 EXPECT_EQ(1024u, decodedSize());
219 EXPECT_FALSE(m_image->hasColorProfile());
222 TEST_F(BitmapImageTest, webpHasColorProfile)
224 loadImage("/LayoutTests/fast/images/resources/webp-color-profile-lossy.webp");
225 EXPECT_EQ(1u, decodedFramesCount());
226 EXPECT_EQ(2560000u, decodedSize());
227 EXPECT_TRUE(m_image->hasColorProfile());
229 destroyDecodedData(true);
232 loadImage("/LayoutTests/fast/images/resources/test.webp");
233 EXPECT_EQ(1u, decodedFramesCount());
234 EXPECT_EQ(65536u, decodedSize());
235 EXPECT_FALSE(m_image->hasColorProfile());
238 TEST_F(BitmapImageTest, icoHasWrongFrameDimensions)
240 loadImage("/LayoutTests/fast/images/resources/wrong-frame-dimensions.ico");
241 // This call would cause crash without fix for 408026
242 imageForDefaultFrame();
245 #endif // USE(QCMSLIB)