• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
FakeImageObserver()48         FakeImageObserver() : m_lastDecodedSizeChangedDelta(0) { }
49 
decodedSizeChanged(const Image *,int delta)50         virtual void decodedSizeChanged(const Image*, int delta)
51         {
52             m_lastDecodedSizeChangedDelta = delta;
53         }
didDraw(const Image *)54         virtual void didDraw(const Image*) OVERRIDE { }
shouldPauseAnimation(const Image *)55         virtual bool shouldPauseAnimation(const Image*) OVERRIDE { return false; }
animationAdvanced(const Image *)56         virtual void animationAdvanced(const Image*) OVERRIDE { }
changedInRect(const Image *,const IntRect &)57         virtual void changedInRect(const Image*, const IntRect&) { }
58 
59         int m_lastDecodedSizeChangedDelta;
60     };
61 
readFile(const char * fileName)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.
destroyDecodedData(bool destroyAll)70     void destroyDecodedData(bool destroyAll) { m_image->destroyDecodedData(destroyAll); }
frameCount()71     size_t frameCount() { return m_image->frameCount(); }
setCurrentFrame(size_t frame)72     void setCurrentFrame(size_t frame) { m_image->m_currentFrame = frame; }
frameDecodedSize(size_t frame)73     size_t frameDecodedSize(size_t frame) { return m_image->m_frames[frame].m_frameBytes; }
decodedFramesCount() const74     size_t decodedFramesCount() const { return m_image->m_frames.size(); }
75 
loadImage(const char * fileName)76     void loadImage(const char* fileName)
77     {
78         RefPtr<SharedBuffer> imageData = readFile("/LayoutTests/fast/images/resources/animated-10color.gif");
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 
decodedSize()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 protected:
SetUp()104     virtual void SetUp() OVERRIDE
105     {
106         DeferredImageDecoder::setEnabled(false);
107         m_image = BitmapImage::create(&m_imageObserver);
108     }
109 
110     FakeImageObserver m_imageObserver;
111     RefPtr<BitmapImage> m_image;
112 };
113 
114 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104
115 #if OS(WIN)
TEST_F(BitmapImageTest,DISABLED_destroyDecodedDataExceptCurrentFrame)116 TEST_F(BitmapImageTest, DISABLED_destroyDecodedDataExceptCurrentFrame)
117 #else
118 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame)
119 #endif
120 {
121     loadImage("/LayoutTests/fast/images/resources/animated-10color.gif");
122     size_t totalSize = decodedSize();
123     size_t frame = frameCount() / 2;
124     setCurrentFrame(frame);
125     size_t size = frameDecodedSize(frame);
126     destroyDecodedData(false);
127     EXPECT_LT(m_imageObserver.m_lastDecodedSizeChangedDelta, 0);
128     EXPECT_GE(m_imageObserver.m_lastDecodedSizeChangedDelta, -static_cast<int>(totalSize - size));
129 }
130 
131 // Fails on the WebKit XP (deps) bot, see http://crbug.com/327104
132 #if OS(WIN)
TEST_F(BitmapImageTest,DISABLED_destroyAllDecodedData)133 TEST_F(BitmapImageTest, DISABLED_destroyAllDecodedData)
134 #else
135 TEST_F(BitmapImageTest, destroyAllDecodedData)
136 #endif
137 {
138     loadImage("/LayoutTests/fast/images/resources/animated-10color.gif");
139     size_t totalSize = decodedSize();
140     EXPECT_GT(totalSize, 0u);
141     destroyDecodedData(true);
142     EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver.m_lastDecodedSizeChangedDelta);
143     EXPECT_EQ(0u, decodedSize());
144 }
145 
146 } // namespace
147