Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 cache | |
|
Stephen White
2013/07/25 18:01:07
Nit: eviced.
| |
| 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> cachedImageMediumPriority = | |
| 139 new MockCachedImage(ResourceRequest(""), CachedResource::RawResource); | |
| 140 CachedResourceHandle<MockCachedImage> cachedImageHighPriority = | |
| 141 new MockCachedImage(ResourceRequest(""), CachedResource::RawResource); | |
| 142 | |
| 143 MockCachedImageClient clientLowPriority; | |
| 144 MockCachedImageClient clientMediumPriority; | |
| 145 MockCachedImageClient clientHighPriority; | |
| 146 cachedImageLowPriority->addClient(&clientLowPriority); | |
| 147 cachedImageMediumPriority->addClient(&clientMediumPriority); | |
| 148 cachedImageHighPriority->addClient(&clientHighPriority); | |
| 149 | |
| 150 const char data[5] = "abcd"; | |
| 151 cachedImageLowPriority->appendData(data, 1); | |
| 152 cachedImageMediumPriority->appendData(data, 2); | |
| 153 cachedImageHighPriority->appendData(data, 4); | |
| 154 const unsigned lowPrioritySize = cachedImageLowPriority->size(); | |
| 155 const unsigned mediumPrioritySize = cachedImageMediumPriority->size(); | |
| 156 const unsigned highPrioritySize = cachedImageHighPriority->size(); | |
| 157 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi ze(); | |
| 158 const unsigned mediumPriorityMockDecodeSize = cachedImageMediumPriority->dec odedSize(); | |
| 159 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded Size(); | |
| 160 const unsigned totalSize = lowPrioritySize + mediumPrioritySize + highPriori tySize; | |
| 161 | |
| 162 // Verify all the sizes to be different in order to properly test eviction o rder. | |
| 163 ASSERT_GT(lowPrioritySize, 0u); | |
| 164 ASSERT_NE(lowPrioritySize, mediumPrioritySize); | |
| 165 ASSERT_NE(mediumPrioritySize, highPrioritySize); | |
| 166 ASSERT_GT(lowPriorityMockDecodeSize, 0u); | |
| 167 ASSERT_NE(lowPriorityMockDecodeSize, mediumPriorityMockDecodeSize); | |
| 168 ASSERT_NE(mediumPriorityMockDecodeSize, highPriorityMockDecodeSize); | |
| 169 | |
| 170 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 171 ASSERT_EQ(memoryCache()->liveSize(), 0u); | |
| 172 | |
| 173 // Add the three items in a random order. | |
| 174 memoryCache()->add(cachedImageHighPriority.get()); | |
| 175 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 176 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize); | |
| 177 | |
| 178 memoryCache()->add(cachedImageLowPriority.get()); | |
| 179 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 180 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + lowPrioritySize); | |
| 181 | |
| 182 memoryCache()->add(cachedImageMediumPriority.get()); | |
| 183 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 184 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + mediumPrioritySize + lowPrioritySize); | |
| 185 | |
| 186 // Insert all items in the decoded items list with the same priority | |
| 187 memoryCache()->insertInLiveDecodedResourcesList(cachedImageHighPriority.get( )); | |
| 188 memoryCache()->insertInLiveDecodedResourcesList(cachedImageLowPriority.get() ); | |
| 189 memoryCache()->insertInLiveDecodedResourcesList(cachedImageMediumPriority.ge t()); | |
| 190 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 191 ASSERT_EQ(memoryCache()->liveSize(), totalSize); | |
| 192 | |
| 193 // Now we will assign their priority and make sure they are moved to the cor rect buckets. | |
| 194 cachedImageLowPriority->setCachePriority(CachedResource::CachePriorityLow); | |
| 195 cachedImageMediumPriority->setCachePriority(CachedResource::CachePriorityMed ium); | |
| 196 cachedImageHighPriority->setCachePriority(CachedResource::CachePriorityHigh) ; | |
| 197 | |
| 198 // Should prune the LowPriority item. | |
| 199 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize() - 10); | |
| 200 memoryCache()->prune(); | |
| 201 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 202 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize); | |
| 203 | |
| 204 // Should prune the MediumPriority item. | |
| 205 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize() - 10); | |
| 206 memoryCache()->prune(); | |
| 207 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 208 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - mediumPriorityMockDecodeSize); | |
| 209 | |
| 210 // Should prune the HighPriority item. | |
| 211 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache() ->maxDeadCapacity(), memoryCache()->liveSize() - 10); | |
| 212 memoryCache()->prune(); | |
| 213 ASSERT_EQ(memoryCache()->deadSize(), 0u); | |
| 214 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - mediumPriorityMockDecodeSize - highPriorityMockDecodeSize); | |
| 215 } | |
| 109 } // namespace | 216 } // namespace |
| OLD | NEW |