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 14 matching lines...) Expand all Loading... |
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/CachedRawResource.h" | 34 #include "core/loader/cache/CachedRawResource.h" |
| 35 #include "core/loader/cache/MockCachedImageClient.h" |
35 #include "core/loader/cache/ResourcePtr.h" | 36 #include "core/loader/cache/ResourcePtr.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::Resource { |
| 47 public: |
| 48 MockCachedImage(const ResourceRequest& request, Type type) |
| 49 : Resource(request, type) |
| 50 { |
| 51 } |
| 52 |
| 53 virtual void appendData(const char* data, int len) |
| 54 { |
| 55 Resource::appendData(data, len); |
| 56 setDecodedSize(this->size()); |
| 57 } |
| 58 |
| 59 virtual void destroyDecodedData() |
| 60 { |
| 61 setDecodedSize(0); |
| 62 } |
| 63 }; |
| 64 |
44 protected: | 65 protected: |
45 virtual void SetUp() | 66 virtual void SetUp() |
46 { | 67 { |
47 // Save the global memory cache to restore it upon teardown. | 68 // Save the global memory cache to restore it upon teardown. |
48 m_globalMemoryCache = adoptPtr(memoryCache()); | 69 m_globalMemoryCache = adoptPtr(memoryCache()); |
49 // Create the test memory cache instance and hook it in. | 70 // Create the test memory cache instance and hook it in. |
50 m_testingMemoryCache = adoptPtr(new MemoryCache()); | 71 m_testingMemoryCache = adoptPtr(new MemoryCache()); |
51 setMemoryCacheForTesting(m_testingMemoryCache.leakPtr()); | 72 setMemoryCacheForTesting(m_testingMemoryCache.leakPtr()); |
52 } | 73 } |
53 | 74 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 | 120 |
100 memoryCache()->add(cachedResource.get()); | 121 memoryCache()->add(cachedResource.get()); |
101 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); | 122 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); |
102 ASSERT_EQ(0u, memoryCache()->liveSize()); | 123 ASSERT_EQ(0u, memoryCache()->liveSize()); |
103 | 124 |
104 memoryCache()->prune(); | 125 memoryCache()->prune(); |
105 ASSERT_EQ(0u, memoryCache()->deadSize()); | 126 ASSERT_EQ(0u, memoryCache()->deadSize()); |
106 ASSERT_EQ(0u, memoryCache()->liveSize()); | 127 ASSERT_EQ(0u, memoryCache()->liveSize()); |
107 } | 128 } |
108 | 129 |
| 130 // Verifies that CachedResources are evicted from the decode cache |
| 131 // according to their DecodeCachePriority. |
| 132 TEST_F(MemoryCacheTest, DecodeCacheOrder) |
| 133 { |
| 134 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
| 135 ResourcePtr<MockCachedImage> cachedImageLowPriority = |
| 136 new MockCachedImage(ResourceRequest(""), Resource::RawResource); |
| 137 ResourcePtr<MockCachedImage> cachedImageHighPriority = |
| 138 new MockCachedImage(ResourceRequest(""), Resource::RawResource); |
| 139 |
| 140 MockCachedImageClient clientLowPriority; |
| 141 MockCachedImageClient clientHighPriority; |
| 142 cachedImageLowPriority->addClient(&clientLowPriority); |
| 143 cachedImageHighPriority->addClient(&clientHighPriority); |
| 144 |
| 145 const char data[5] = "abcd"; |
| 146 cachedImageLowPriority->appendData(data, 1); |
| 147 cachedImageHighPriority->appendData(data, 4); |
| 148 const unsigned lowPrioritySize = cachedImageLowPriority->size(); |
| 149 const unsigned highPrioritySize = cachedImageHighPriority->size(); |
| 150 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi
ze(); |
| 151 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded
Size(); |
| 152 const unsigned totalSize = lowPrioritySize + highPrioritySize; |
| 153 |
| 154 // Verify that the sizes are different to ensure that we can test eviction o
rder. |
| 155 ASSERT_GT(lowPrioritySize, 0u); |
| 156 ASSERT_NE(lowPrioritySize, highPrioritySize); |
| 157 ASSERT_GT(lowPriorityMockDecodeSize, 0u); |
| 158 ASSERT_NE(lowPriorityMockDecodeSize, highPriorityMockDecodeSize); |
| 159 |
| 160 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| 161 ASSERT_EQ(memoryCache()->liveSize(), 0u); |
| 162 |
| 163 // Add the items. The item added first would normally be evicted first. |
| 164 memoryCache()->add(cachedImageHighPriority.get()); |
| 165 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| 166 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize); |
| 167 |
| 168 memoryCache()->add(cachedImageLowPriority.get()); |
| 169 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| 170 ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + lowPrioritySize); |
| 171 |
| 172 // Insert all items in the decoded items list with the same priority |
| 173 memoryCache()->insertInLiveDecodedResourcesList(cachedImageHighPriority.get(
)); |
| 174 memoryCache()->insertInLiveDecodedResourcesList(cachedImageLowPriority.get()
); |
| 175 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| 176 ASSERT_EQ(memoryCache()->liveSize(), totalSize); |
| 177 |
| 178 // Now we will assign their priority and make sure they are moved to the cor
rect buckets. |
| 179 cachedImageLowPriority->setCacheLiveResourcePriority(Resource::CacheLiveReso
urcePriorityLow); |
| 180 cachedImageHighPriority->setCacheLiveResourcePriority(Resource::CacheLiveRes
ourcePriorityHigh); |
| 181 |
| 182 // Should first prune the LowPriority item. |
| 183 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); |
| 184 memoryCache()->prune(); |
| 185 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| 186 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize); |
| 187 |
| 188 // Should prune the HighPriority item. |
| 189 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); |
| 190 memoryCache()->prune(); |
| 191 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| 192 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize -
highPriorityMockDecodeSize); |
| 193 } |
109 } // namespace | 194 } // namespace |
OLD | NEW |