OLD | NEW |
| (Empty) |
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 "core/platform/graphics/BitmapImage.h" | |
33 | |
34 #include "platform/SharedBuffer.h" | |
35 #include "platform/graphics/ImageObserver.h" | |
36 #include "public/platform/Platform.h" | |
37 #include "public/platform/WebUnitTestSupport.h" | |
38 | |
39 #include <gtest/gtest.h> | |
40 | |
41 namespace WebCore { | |
42 | |
43 class BitmapImageTest : public ::testing::Test { | |
44 public: | |
45 class FakeImageObserver : public ImageObserver { | |
46 public: | |
47 FakeImageObserver() : m_lastDecodedSizeChangedDelta(0) { } | |
48 | |
49 virtual void decodedSizeChanged(const Image*, int delta) | |
50 { | |
51 m_lastDecodedSizeChangedDelta = delta; | |
52 } | |
53 virtual void didDraw(const Image*) OVERRIDE { } | |
54 virtual bool shouldPauseAnimation(const Image*) OVERRIDE { return false;
} | |
55 virtual void animationAdvanced(const Image*) OVERRIDE { } | |
56 virtual void changedInRect(const Image*, const IntRect&) { } | |
57 | |
58 int m_lastDecodedSizeChangedDelta; | |
59 }; | |
60 | |
61 static PassRefPtr<SharedBuffer> readFile(const char* fileName) | |
62 { | |
63 String filePath = blink::Platform::current()->unitTestSupport()->webKitR
ootDir(); | |
64 filePath.append(fileName); | |
65 return blink::Platform::current()->unitTestSupport()->readFromFile(fileP
ath); | |
66 } | |
67 | |
68 // Accessors to BitmapImage's protected methods. | |
69 void destroyDecodedData(bool destroyAll) { m_image->destroyDecodedData(destr
oyAll); } | |
70 size_t frameCount() { return m_image->frameCount(); } | |
71 void setCurrentFrame(size_t frame) { m_image->m_currentFrame = frame; } | |
72 size_t frameDecodedSize(size_t frame) { return m_image->m_frames[frame].m_fr
ameBytes; } | |
73 size_t decodedFramesCount() const { return m_image->m_frames.size(); } | |
74 | |
75 void loadImage(const char* fileName) | |
76 { | |
77 RefPtr<SharedBuffer> imageData = readFile("/LayoutTests/fast/images/reso
urces/animated-10color.gif"); | |
78 ASSERT_TRUE(imageData.get()); | |
79 | |
80 m_image->setData(imageData, true); | |
81 EXPECT_EQ(0u, decodedSize()); | |
82 | |
83 size_t frameCount = m_image->frameCount(); | |
84 for (size_t i = 0; i < frameCount; ++i) | |
85 m_image->frameAtIndex(i); | |
86 } | |
87 | |
88 size_t decodedSize() | |
89 { | |
90 // In the context of this test, the following loop will give the correct
result, but only because the test | |
91 // forces all frames to be decoded in loadImage() above. There is no gen
eral guarantee that frameDecodedSize() | |
92 // is up-to-date. Because of how multi frame images (like GIF) work, req
uesting one frame to be decoded may | |
93 // require other previous frames to be decoded as well. In those cases f
rameDecodedSize() wouldn't return the | |
94 // correct thing for the previous frame because the decoded size wouldn'
t have propagated upwards to the | |
95 // BitmapImage frame cache. | |
96 size_t size = 0; | |
97 for (size_t i = 0; i < decodedFramesCount(); ++i) | |
98 size += frameDecodedSize(i); | |
99 return size; | |
100 } | |
101 | |
102 protected: | |
103 virtual void SetUp() OVERRIDE | |
104 { | |
105 m_image = BitmapImage::create(&m_imageObserver); | |
106 } | |
107 | |
108 FakeImageObserver m_imageObserver; | |
109 RefPtr<BitmapImage> m_image; | |
110 }; | |
111 | |
112 TEST_F(BitmapImageTest, destroyDecodedDataExceptCurrentFrame) | |
113 { | |
114 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); | |
115 size_t totalSize = decodedSize(); | |
116 size_t frame = frameCount() / 2; | |
117 setCurrentFrame(frame); | |
118 size_t size = frameDecodedSize(frame); | |
119 destroyDecodedData(false); | |
120 EXPECT_LT(m_imageObserver.m_lastDecodedSizeChangedDelta, 0); | |
121 EXPECT_GE(m_imageObserver.m_lastDecodedSizeChangedDelta, -static_cast<int>(t
otalSize - size)); | |
122 } | |
123 | |
124 TEST_F(BitmapImageTest, destroyAllDecodedData) | |
125 { | |
126 loadImage("/LayoutTests/fast/images/resources/animated-10color.gif"); | |
127 size_t totalSize = decodedSize(); | |
128 EXPECT_GT(totalSize, 0u); | |
129 destroyDecodedData(true); | |
130 EXPECT_EQ(-static_cast<int>(totalSize), m_imageObserver.m_lastDecodedSizeCha
ngedDelta); | |
131 EXPECT_EQ(0u, decodedSize()); | |
132 } | |
133 | |
134 } // namespace | |
OLD | NEW |