Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / BitmapImageTest.cpp
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 blink {
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 = Platform::current()->unitTestSupport()->webKitRootDir();
65         filePath.append(fileName);
66         return 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     void resetDecoder() { return m_image->resetDecoder(); }
76
77     void loadImage(const char* fileName)
78     {
79         RefPtr<SharedBuffer> imageData = readFile(fileName);
80         ASSERT_TRUE(imageData.get());
81
82         m_image->setData(imageData, true);
83         EXPECT_EQ(0u, decodedSize());
84
85         size_t frameCount = m_image->frameCount();
86         for (size_t i = 0; i < frameCount; ++i)
87             m_image->frameAtIndex(i);
88     }
89
90     size_t decodedSize()
91     {
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.
98         size_t size = 0;
99         for (size_t i = 0; i < decodedFramesCount(); ++i)
100             size += frameDecodedSize(i);
101         return size;
102     }
103
104     void advanceAnimation()
105     {
106         m_image->advanceAnimation(0);
107     }
108
109     PassRefPtr<Image> imageForDefaultFrame()
110     {
111         return m_image->imageForDefaultFrame();
112     }
113
114 protected:
115     virtual void SetUp() OVERRIDE
116     {
117         DeferredImageDecoder::setEnabled(false);
118         m_image = BitmapImage::create(&m_imageObserver);
119     }
120
121     FakeImageObserver m_imageObserver;
122     RefPtr<BitmapImage> m_image;
123 };
124
125 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104
126 #if OS(WIN)
127 TEST_F(BitmapImageTest, DISABLED_destroyDecodedDataExceptCurrentFrame)
128 #else
129 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame)
130 #endif
131 {
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));
140 }
141
142 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104
143 #if OS(WIN)
144 TEST_F(BitmapImageTest, DISABLED_destroyAllDecodedData)
145 #else
146 TEST_F(BitmapImageTest, destroyAllDecodedData)
147 #endif
148 {
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());
155 }
156
157 TEST_F(BitmapImageTest, maybeAnimated)
158 {
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());
162         advanceAnimation();
163     }
164     EXPECT_FALSE(m_image->maybeAnimated());
165 }
166
167 TEST_F(BitmapImageTest, isAllDataReceived)
168 {
169     RefPtr<SharedBuffer> imageData = readFile("/LayoutTests/fast/images/resources/green.jpg");
170     ASSERT_TRUE(imageData.get());
171
172     RefPtr<BitmapImage> image = BitmapImage::create();
173     EXPECT_FALSE(image->isAllDataReceived());
174
175     image->setData(imageData, false);
176     EXPECT_FALSE(image->isAllDataReceived());
177
178     image->setData(imageData, true);
179     EXPECT_TRUE(image->isAllDataReceived());
180
181     image->setData(SharedBuffer::create("data", sizeof("data")), false);
182     EXPECT_FALSE(image->isAllDataReceived());
183
184     image->setData(imageData, true);
185     EXPECT_TRUE(image->isAllDataReceived());
186 }
187
188 #if USE(QCMSLIB)
189
190 TEST_F(BitmapImageTest, jpegHasColorProfile)
191 {
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());
196
197     resetDecoder();
198     destroyDecodedData(true);
199
200     loadImage("/LayoutTests/fast/images/resources/green.jpg");
201     EXPECT_EQ(1u, decodedFramesCount());
202     EXPECT_EQ(1024u, decodedSize());
203     EXPECT_FALSE(m_image->hasColorProfile());
204 }
205
206 TEST_F(BitmapImageTest, pngHasColorProfile)
207 {
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());
212
213     resetDecoder();
214     destroyDecodedData(true);
215
216     loadImage("/LayoutTests/fast/images/resources/green.jpg");
217     EXPECT_EQ(1u, decodedFramesCount());
218     EXPECT_EQ(1024u, decodedSize());
219     EXPECT_FALSE(m_image->hasColorProfile());
220 }
221
222 TEST_F(BitmapImageTest, webpHasColorProfile)
223 {
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());
228
229     destroyDecodedData(true);
230     resetDecoder();
231
232     loadImage("/LayoutTests/fast/images/resources/test.webp");
233     EXPECT_EQ(1u, decodedFramesCount());
234     EXPECT_EQ(65536u, decodedSize());
235     EXPECT_FALSE(m_image->hasColorProfile());
236 }
237
238 TEST_F(BitmapImageTest, icoHasWrongFrameDimensions)
239 {
240     loadImage("/LayoutTests/fast/images/resources/wrong-frame-dimensions.ico");
241     // This call would cause crash without fix for 408026
242     imageForDefaultFrame();
243 }
244
245 #endif // USE(QCMSLIB)
246
247 } // namespace blink