Chromium Code Reviews| Index: Source/core/loader/cache/MemoryCacheTest.cpp |
| diff --git a/Source/core/loader/cache/MemoryCacheTest.cpp b/Source/core/loader/cache/MemoryCacheTest.cpp |
| index 021c3751d5ca6ef31f9cd944ff4e71b69a76304c..5543a29a6fe1e331a9da31b72a1f7796173ffb95 100644 |
| --- a/Source/core/loader/cache/MemoryCacheTest.cpp |
| +++ b/Source/core/loader/cache/MemoryCacheTest.cpp |
| @@ -31,6 +31,7 @@ |
| #include "config.h" |
| #include "core/loader/cache/MemoryCache.h" |
| +#include "core/loader/cache/CachedImageTest.h" |
| #include "core/loader/cache/CachedRawResource.h" |
| #include "core/loader/cache/CachedResourceHandle.h" |
| #include "core/platform/network/ResourceRequest.h" |
| @@ -41,6 +42,27 @@ |
| namespace WebCore { |
| class MemoryCacheTest : public ::testing::Test { |
| +public: |
| + class MockCachedImage : public WebCore::CachedResource { |
| + public: |
| + MockCachedImage(const ResourceRequest& request, Type type, DecodeCachePriority decodeCachePriority) |
| + : CachedResource(request, type, decodeCachePriority) |
| + { |
| + } |
| + |
| + virtual void appendData(const char* data, int len) |
| + { |
| + CachedResource::appendData(data, len); |
| + setDecodedSize(this->size()); |
| + } |
| + |
| + virtual void destroyDecodedData() |
| + { |
| + setDecodedSize(0); |
| + makePurgeable(true); |
| + } |
| + }; |
| + |
| protected: |
| virtual void SetUp() |
| { |
| @@ -106,4 +128,82 @@ TEST_F(MemoryCacheTest, DeadResourceEviction) |
| ASSERT_EQ(0u, memoryCache()->liveSize()); |
| } |
| +// Verifies that CachedResources are eviced from the decode catch |
|
Justin Novosad
2013/07/19 21:09:53
catch -> cache
|
| +// according to their DecodeCachePriority |
| +TEST_F(MemoryCacheTest, DecodeCacheOrder) |
| +{ |
| + CachedResourceHandle<MockCachedImage> cachedImageLowPriority = |
| + new MockCachedImage(ResourceRequest(""), CachedResource::RawResource, CachedResource::DecodeCachePriorityLow); |
| + CachedResourceHandle<MockCachedImage> cachedImageMediumPriority = |
| + new MockCachedImage(ResourceRequest(""), CachedResource::RawResource, CachedResource::DecodeCachePriorityMedium); |
| + CachedResourceHandle<MockCachedImage> cachedImageHighPriority = |
| + new MockCachedImage(ResourceRequest(""), CachedResource::RawResource, CachedResource::DecodeCachePriorityHigh); |
| + |
| + MockCachedImageClient clientLowPriority; |
| + MockCachedImageClient clientMediumPriority; |
| + MockCachedImageClient clientHighPriority; |
| + cachedImageLowPriority->addClient(&clientLowPriority); |
| + cachedImageMediumPriority->addClient(&clientMediumPriority); |
| + cachedImageHighPriority->addClient(&clientHighPriority); |
| + |
| + const char data[5] = "abcd"; |
| + cachedImageLowPriority->appendData(data, 1); |
| + cachedImageMediumPriority->appendData(data, 2); |
| + cachedImageHighPriority->appendData(data, 4); |
| + const unsigned lowPrioritySize = cachedImageLowPriority->size(); |
| + const unsigned mediumPrioritySize = cachedImageMediumPriority->size(); |
| + const unsigned highPrioritySize = cachedImageHighPriority->size(); |
| + const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSize(); |
| + const unsigned mediumPriorityMockDecodeSize = cachedImageMediumPriority->decodedSize(); |
| + const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decodedSize(); |
| + const unsigned totalSize = lowPrioritySize + mediumPrioritySize + highPrioritySize; |
| + |
| + // Verify all the sizes to be different in order to properly test eviction order |
| + ASSERT_GT(lowPrioritySize, 0u); |
| + ASSERT_NE(lowPrioritySize, mediumPrioritySize); |
| + ASSERT_NE(mediumPrioritySize, highPrioritySize); |
| + ASSERT_GT(lowPriorityMockDecodeSize, 0u); |
| + ASSERT_NE(lowPriorityMockDecodeSize, mediumPriorityMockDecodeSize); |
| + ASSERT_NE(mediumPriorityMockDecodeSize, highPriorityMockDecodeSize); |
| + |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), 0u); |
| + |
| + // Add the three items in a random order |
| + memoryCache()->add(cachedImageHighPriority.get()); |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize); |
| + |
| + memoryCache()->add(cachedImageLowPriority.get()); |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + lowPrioritySize); |
| + |
| + memoryCache()->add(cachedImageMediumPriority.get()); |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + mediumPrioritySize + lowPrioritySize); |
| + |
| + memoryCache()->insertInLiveDecodedResourcesList(cachedImageHighPriority.get()); |
| + memoryCache()->insertInLiveDecodedResourcesList(cachedImageLowPriority.get()); |
| + memoryCache()->insertInLiveDecodedResourcesList(cachedImageMediumPriority.get()); |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), totalSize); |
| + |
| + // Should prune the LowPriority item |
| + memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()->maxDeadCapacity(), memoryCache()->liveSize()); |
| + memoryCache()->pruneToPercentage(0.90f); |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize); |
| + |
| + // Should prune the MediumPriority item |
| + memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()->maxDeadCapacity(), memoryCache()->liveSize()); |
| + memoryCache()->pruneToPercentage(0.90f); |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - mediumPriorityMockDecodeSize); |
| + |
| + // Should prune the HighPriority item |
| + memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()->maxDeadCapacity(), memoryCache()->liveSize()); |
| + memoryCache()->pruneToPercentage(0.90f); |
| + ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| + ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - mediumPriorityMockDecodeSize - highPriorityMockDecodeSize); |
| +} |
| } // namespace |