Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(910)

Side by Side Diff: Source/core/loader/cache/MemoryCacheTest.cpp

Issue 19393004: Allow eviction of ImageBitmaps that are created from ImageElements. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved. 2 * Copyright (c) 2013, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "core/loader/cache/MemoryCache.h" 32 #include "core/loader/cache/MemoryCache.h"
33 33
34 #include "core/loader/cache/CachedImageTest.h"
34 #include "core/loader/cache/CachedRawResource.h" 35 #include "core/loader/cache/CachedRawResource.h"
35 #include "core/loader/cache/CachedResourceHandle.h" 36 #include "core/loader/cache/CachedResourceHandle.h"
36 #include "core/platform/network/ResourceRequest.h" 37 #include "core/platform/network/ResourceRequest.h"
37 #include "wtf/OwnPtr.h" 38 #include "wtf/OwnPtr.h"
38 39
39 #include <gtest/gtest.h> 40 #include <gtest/gtest.h>
40 41
41 namespace WebCore { 42 namespace WebCore {
42 43
43 class MemoryCacheTest : public ::testing::Test { 44 class MemoryCacheTest : public ::testing::Test {
45 public:
46 class MockCachedImage : public WebCore::CachedResource {
47 public:
48 MockCachedImage(const ResourceRequest& request, Type type, DecodeCachePr iority decodeCachePriority)
49 : CachedResource(request, type, decodeCachePriority)
50 {
51 }
52
53 virtual void appendData(const char* data, int len)
54 {
55 CachedResource::appendData(data, len);
56 setDecodedSize(this->size());
57 }
58
59 virtual void destroyDecodedData()
60 {
61 setDecodedSize(0);
62 makePurgeable(true);
63 }
64 };
65
44 protected: 66 protected:
45 virtual void SetUp() 67 virtual void SetUp()
46 { 68 {
47 // Save the global memory cache to restore it upon teardown. 69 // Save the global memory cache to restore it upon teardown.
48 m_globalMemoryCache = adoptPtr(memoryCache()); 70 m_globalMemoryCache = adoptPtr(memoryCache());
49 // Create the test memory cache instance and hook it in. 71 // Create the test memory cache instance and hook it in.
50 m_testingMemoryCache = adoptPtr(new MemoryCache()); 72 m_testingMemoryCache = adoptPtr(new MemoryCache());
51 setMemoryCacheForTesting(m_testingMemoryCache.leakPtr()); 73 setMemoryCacheForTesting(m_testingMemoryCache.leakPtr());
52 } 74 }
53 75
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 121
100 memoryCache()->add(cachedResource.get()); 122 memoryCache()->add(cachedResource.get());
101 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); 123 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize());
102 ASSERT_EQ(0u, memoryCache()->liveSize()); 124 ASSERT_EQ(0u, memoryCache()->liveSize());
103 125
104 memoryCache()->prune(); 126 memoryCache()->prune();
105 ASSERT_EQ(0u, memoryCache()->deadSize()); 127 ASSERT_EQ(0u, memoryCache()->deadSize());
106 ASSERT_EQ(0u, memoryCache()->liveSize()); 128 ASSERT_EQ(0u, memoryCache()->liveSize());
107 } 129 }
108 130
131 // Verifies that CachedResources are eviced from the decode catch
Justin Novosad 2013/07/19 21:09:53 catch -> cache
132 // according to their DecodeCachePriority
133 TEST_F(MemoryCacheTest, DecodeCacheOrder)
134 {
135 CachedResourceHandle<MockCachedImage> cachedImageLowPriority =
136 new MockCachedImage(ResourceRequest(""), CachedResource::RawResource, Ca chedResource::DecodeCachePriorityLow);
137 CachedResourceHandle<MockCachedImage> cachedImageMediumPriority =
138 new MockCachedImage(ResourceRequest(""), CachedResource::RawResource, Ca chedResource::DecodeCachePriorityMedium);
139 CachedResourceHandle<MockCachedImage> cachedImageHighPriority =
140 new MockCachedImage(ResourceRequest(""), CachedResource::RawResource, Ca chedResource::DecodeCachePriorityHigh);
141
142 MockCachedImageClient clientLowPriority;
143 MockCachedImageClient clientMediumPriority;
144 MockCachedImageClient clientHighPriority;
145 cachedImageLowPriority->addClient(&clientLowPriority);
146 cachedImageMediumPriority->addClient(&clientMediumPriority);
147 cachedImageHighPriority->addClient(&clientHighPriority);
148
149 const char data[5] = "abcd";
150 cachedImageLowPriority->appendData(data, 1);
151 cachedImageMediumPriority->appendData(data, 2);
152 cachedImageHighPriority->appendData(data, 4);
153 const unsigned lowPrioritySize = cachedImageLowPriority->size();
154 const unsigned mediumPrioritySize = cachedImageMediumPriority->size();
155 const unsigned highPrioritySize = cachedImageHighPriority->size();
156 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi ze();
157 const unsigned mediumPriorityMockDecodeSize = cachedImageMediumPriority->dec odedSize();
158 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded Size();
159 const unsigned totalSize = lowPrioritySize + mediumPrioritySize + highPriori tySize;
160
161 // Verify all the sizes to be different in order to properly test eviction o rder
162 ASSERT_GT(lowPrioritySize, 0u);
163 ASSERT_NE(lowPrioritySize, mediumPrioritySize);
164 ASSERT_NE(mediumPrioritySize, highPrioritySize);
165 ASSERT_GT(lowPriorityMockDecodeSize, 0u);
166 ASSERT_NE(lowPriorityMockDecodeSize, mediumPriorityMockDecodeSize);
167 ASSERT_NE(mediumPriorityMockDecodeSize, highPriorityMockDecodeSize);
168
169 ASSERT_EQ(memoryCache()->deadSize(), 0u);
170 ASSERT_EQ(memoryCache()->liveSize(), 0u);
171
172 // Add the three items in a random order
173 memoryCache()->add(cachedImageHighPriority.get());
174 ASSERT_EQ(memoryCache()->deadSize(), 0u);
175 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize);
176
177 memoryCache()->add(cachedImageLowPriority.get());
178 ASSERT_EQ(memoryCache()->deadSize(), 0u);
179 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + lowPrioritySize);
180
181 memoryCache()->add(cachedImageMediumPriority.get());
182 ASSERT_EQ(memoryCache()->deadSize(), 0u);
183 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + mediumPrioritySize + lowPrioritySize);
184
185 memoryCache()->insertInLiveDecodedResourcesList(cachedImageHighPriority.get( ));
186 memoryCache()->insertInLiveDecodedResourcesList(cachedImageLowPriority.get() );
187 memoryCache()->insertInLiveDecodedResourcesList(cachedImageMediumPriority.ge t());
188 ASSERT_EQ(memoryCache()->deadSize(), 0u);
189 ASSERT_EQ(memoryCache()->liveSize(), totalSize);
190
191 // Should prune the LowPriority item
192 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize());
193 memoryCache()->pruneToPercentage(0.90f);
194 ASSERT_EQ(memoryCache()->deadSize(), 0u);
195 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize);
196
197 // Should prune the MediumPriority item
198 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize());
199 memoryCache()->pruneToPercentage(0.90f);
200 ASSERT_EQ(memoryCache()->deadSize(), 0u);
201 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - mediumPriorityMockDecodeSize);
202
203 // Should prune the HighPriority item
204 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize());
205 memoryCache()->pruneToPercentage(0.90f);
206 ASSERT_EQ(memoryCache()->deadSize(), 0u);
207 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - mediumPriorityMockDecodeSize - highPriorityMockDecodeSize);
208 }
109 } // namespace 209 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698