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

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: Name change. Created 7 years, 4 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 15 matching lines...) Expand all
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/CachedRawResource.h" 34 #include "core/loader/cache/CachedRawResource.h"
35 #include "core/loader/cache/CachedResourceHandle.h" 35 #include "core/loader/cache/CachedResourceHandle.h"
36 #include "core/loader/cache/MockCachedImageClient.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)
49 : CachedResource(request, type)
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 evicted from the decode cache
132 // according to their DecodeCachePriority.
133 TEST_F(MemoryCacheTest, DecodeCacheOrder)
134 {
135 memoryCache()->setDelayBeforeLiveDecodedPrune(0);
136 CachedResourceHandle<MockCachedImage> cachedImageLowPriority =
137 new MockCachedImage(ResourceRequest(""), CachedResource::RawResource);
138 CachedResourceHandle<MockCachedImage> cachedImageHighPriority =
139 new MockCachedImage(ResourceRequest(""), CachedResource::RawResource);
140
141 MockCachedImageClient clientLowPriority;
142 MockCachedImageClient clientHighPriority;
143 cachedImageLowPriority->addClient(&clientLowPriority);
144 cachedImageHighPriority->addClient(&clientHighPriority);
145
146 const char data[5] = "abcd";
147 cachedImageLowPriority->appendData(data, 1);
148 cachedImageHighPriority->appendData(data, 4);
149 const unsigned lowPrioritySize = cachedImageLowPriority->size();
150 const unsigned highPrioritySize = cachedImageHighPriority->size();
151 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi ze();
152 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded Size();
153 const unsigned totalSize = lowPrioritySize + highPrioritySize;
154
155 // Verify that the sizes are different to ensure that we can test eviction o rder.
156 ASSERT_GT(lowPrioritySize, 0u);
157 ASSERT_NE(lowPrioritySize, highPrioritySize);
158 ASSERT_GT(lowPriorityMockDecodeSize, 0u);
159 ASSERT_NE(lowPriorityMockDecodeSize, highPriorityMockDecodeSize);
160
161 ASSERT_EQ(memoryCache()->deadSize(), 0u);
162 ASSERT_EQ(memoryCache()->liveSize(), 0u);
163
164 // Add the items. The item added first would normally be evicted first.
165 memoryCache()->add(cachedImageHighPriority.get());
166 ASSERT_EQ(memoryCache()->deadSize(), 0u);
167 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize);
168
169 memoryCache()->add(cachedImageLowPriority.get());
170 ASSERT_EQ(memoryCache()->deadSize(), 0u);
171 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + lowPrioritySize);
172
173 // Insert all items in the decoded items list with the same priority
174 memoryCache()->insertInLiveDecodedResourcesList(cachedImageHighPriority.get( ));
175 memoryCache()->insertInLiveDecodedResourcesList(cachedImageLowPriority.get() );
176 ASSERT_EQ(memoryCache()->deadSize(), 0u);
177 ASSERT_EQ(memoryCache()->liveSize(), totalSize);
178
179 // Now we will assign their priority and make sure they are moved to the cor rect buckets.
180 cachedImageLowPriority->setCacheLiveResourcePriority(CachedResource::CacheLi veResourcePriorityLow);
181 cachedImageHighPriority->setCacheLiveResourcePriority(CachedResource::CacheL iveResourcePriorityHigh);
182
183 // Should first prune the LowPriority item.
184 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize() - 10);
185 memoryCache()->prune();
186 ASSERT_EQ(memoryCache()->deadSize(), 0u);
187 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize);
188
189 // Should prune the HighPriority item.
190 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize() - 10);
191 memoryCache()->prune();
192 ASSERT_EQ(memoryCache()->deadSize(), 0u);
193 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - highPriorityMockDecodeSize);
194 }
109 } // namespace 195 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698