| 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 13 matching lines...) Expand all Loading... |
| 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/fetch/MemoryCache.h" | 32 #include "core/fetch/MemoryCache.h" |
| 33 | 33 |
| 34 #include "core/fetch/MockImageResourceClient.h" | |
| 35 #include "core/fetch/RawResource.h" | 34 #include "core/fetch/RawResource.h" |
| 36 #include "core/fetch/ResourcePtr.h" | 35 #include "core/fetch/ResourcePtr.h" |
| 37 #include "platform/network/ResourceRequest.h" | 36 #include "platform/network/ResourceRequest.h" |
| 38 #include "platform/testing/UnitTestHelpers.h" | 37 #include "platform/testing/UnitTestHelpers.h" |
| 39 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
| 40 #include "wtf/OwnPtr.h" | 39 #include "wtf/OwnPtr.h" |
| 41 | 40 |
| 42 #include <gtest/gtest.h> | 41 #include <gtest/gtest.h> |
| 43 | 42 |
| 44 namespace blink { | 43 namespace blink { |
| 45 | 44 |
| 45 class MockResourceClient : public ResourceClient { |
| 46 public: |
| 47 MockResourceClient() : ResourceClient() { } |
| 48 }; |
| 49 |
| 46 class MemoryCacheTest : public ::testing::Test { | 50 class MemoryCacheTest : public ::testing::Test { |
| 47 public: | 51 public: |
| 48 class FakeDecodedResource : public Resource { | 52 class FakeDecodedResource : public Resource { |
| 49 public: | 53 public: |
| 50 FakeDecodedResource(const ResourceRequest& request, Type type) | 54 FakeDecodedResource(const ResourceRequest& request, Type type) |
| 51 : Resource(request, type) | 55 : Resource(request, type) |
| 52 { | 56 { |
| 53 } | 57 } |
| 54 | 58 |
| 55 virtual void appendData(const char* data, unsigned len) | 59 virtual void appendData(const char* data, unsigned len) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 ResourcePtr<FakeResource> cachedResource = | 122 ResourcePtr<FakeResource> cachedResource = |
| 119 new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw)
; | 123 new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw)
; |
| 120 cachedResource->fakeEncodedSize(resourceSize1); | 124 cachedResource->fakeEncodedSize(resourceSize1); |
| 121 | 125 |
| 122 ASSERT_EQ(0u, memoryCache()->deadSize()); | 126 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 123 ASSERT_EQ(0u, memoryCache()->liveSize()); | 127 ASSERT_EQ(0u, memoryCache()->liveSize()); |
| 124 memoryCache()->add(cachedResource.get()); | 128 memoryCache()->add(cachedResource.get()); |
| 125 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); | 129 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); |
| 126 ASSERT_EQ(0u, memoryCache()->liveSize()); | 130 ASSERT_EQ(0u, memoryCache()->liveSize()); |
| 127 | 131 |
| 128 MockImageResourceClient client; | 132 MockResourceClient client; |
| 129 cachedResource->addClient(&client); | 133 cachedResource->addClient(&client); |
| 130 ASSERT_EQ(0u, memoryCache()->deadSize()); | 134 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 131 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); | 135 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); |
| 132 | 136 |
| 133 cachedResource->fakeEncodedSize(resourceSize2); | 137 cachedResource->fakeEncodedSize(resourceSize2); |
| 134 ASSERT_EQ(0u, memoryCache()->deadSize()); | 138 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 135 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); | 139 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); |
| 136 | 140 |
| 137 cachedResource->removeClient(&client); | 141 cachedResource->removeClient(&client); |
| 138 } | 142 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 198 |
| 195 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, co
nst ResourcePtr<Resource>& cachedLiveResource) | 199 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, co
nst ResourcePtr<Resource>& cachedLiveResource) |
| 196 { | 200 { |
| 197 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 201 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
| 198 const unsigned totalCapacity = 1; | 202 const unsigned totalCapacity = 1; |
| 199 const unsigned minDeadCapacity = 0; | 203 const unsigned minDeadCapacity = 0; |
| 200 const unsigned maxDeadCapacity = 0; | 204 const unsigned maxDeadCapacity = 0; |
| 201 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 205 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
| 202 const char data[6] = "abcde"; | 206 const char data[6] = "abcde"; |
| 203 cachedDeadResource->appendData(data, 3u); | 207 cachedDeadResource->appendData(data, 3u); |
| 204 MockImageResourceClient client; | 208 MockResourceClient client; |
| 205 cachedLiveResource->addClient(&client); | 209 cachedLiveResource->addClient(&client); |
| 206 cachedLiveResource->appendData(data, 4u); | 210 cachedLiveResource->appendData(data, 4u); |
| 207 | 211 |
| 208 class Task1 : public WebThread::Task { | 212 class Task1 : public WebThread::Task { |
| 209 public: | 213 public: |
| 210 Task1(const ResourcePtr<Resource>& live, Resource* dead) | 214 Task1(const ResourcePtr<Resource>& live, Resource* dead) |
| 211 : m_live(live) | 215 : m_live(live) |
| 212 , m_dead(dead) | 216 , m_dead(dead) |
| 213 { } | 217 { } |
| 214 | 218 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResour
ce); | 308 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResour
ce); |
| 305 memoryCache()->evictResources(); | 309 memoryCache()->evictResources(); |
| 306 } | 310 } |
| 307 } | 311 } |
| 308 | 312 |
| 309 // Verifies that cached resources are evicted immediately after release when | 313 // Verifies that cached resources are evicted immediately after release when |
| 310 // the total dead resource size is more than double the dead resource capacity. | 314 // the total dead resource size is more than double the dead resource capacity. |
| 311 static void TestClientRemoval(const ResourcePtr<Resource>& resource1, const Reso
urcePtr<Resource>& resource2) | 315 static void TestClientRemoval(const ResourcePtr<Resource>& resource1, const Reso
urcePtr<Resource>& resource2) |
| 312 { | 316 { |
| 313 const char data[6] = "abcde"; | 317 const char data[6] = "abcde"; |
| 314 MockImageResourceClient client1; | 318 MockResourceClient client1; |
| 315 resource1->addClient(&client1); | 319 resource1->addClient(&client1); |
| 316 resource1->appendData(data, 4u); | 320 resource1->appendData(data, 4u); |
| 317 MockImageResourceClient client2; | 321 MockResourceClient client2; |
| 318 resource2->addClient(&client2); | 322 resource2->addClient(&client2); |
| 319 resource2->appendData(data, 4u); | 323 resource2->appendData(data, 4u); |
| 320 | 324 |
| 321 const unsigned minDeadCapacity = 0; | 325 const unsigned minDeadCapacity = 0; |
| 322 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; | 326 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; |
| 323 const unsigned totalCapacity = maxDeadCapacity; | 327 const unsigned totalCapacity = maxDeadCapacity; |
| 324 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 328 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
| 325 memoryCache()->add(resource1.get()); | 329 memoryCache()->add(resource1.get()); |
| 326 memoryCache()->add(resource2.get()); | 330 memoryCache()->add(resource2.get()); |
| 327 // Call prune. There is nothing to prune, but this will initialize | 331 // Call prune. There is nothing to prune, but this will initialize |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 } | 398 } |
| 395 } | 399 } |
| 396 | 400 |
| 397 // Verifies that CachedResources are evicted from the decode cache | 401 // Verifies that CachedResources are evicted from the decode cache |
| 398 // according to their DecodeCachePriority. | 402 // according to their DecodeCachePriority. |
| 399 static void TestDecodeCacheOrder(const ResourcePtr<Resource>& cachedImageLowPrio
rity, const ResourcePtr<Resource>& cachedImageHighPriority) | 403 static void TestDecodeCacheOrder(const ResourcePtr<Resource>& cachedImageLowPrio
rity, const ResourcePtr<Resource>& cachedImageHighPriority) |
| 400 { | 404 { |
| 401 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 405 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
| 402 memoryCache()->setMaxPruneDeferralDelay(0); | 406 memoryCache()->setMaxPruneDeferralDelay(0); |
| 403 | 407 |
| 404 MockImageResourceClient clientLowPriority; | 408 MockResourceClient clientLowPriority; |
| 405 MockImageResourceClient clientHighPriority; | 409 MockResourceClient clientHighPriority; |
| 406 cachedImageLowPriority->addClient(&clientLowPriority); | 410 cachedImageLowPriority->addClient(&clientLowPriority); |
| 407 cachedImageHighPriority->addClient(&clientHighPriority); | 411 cachedImageHighPriority->addClient(&clientHighPriority); |
| 408 | 412 |
| 409 const char data[5] = "abcd"; | 413 const char data[5] = "abcd"; |
| 410 cachedImageLowPriority->appendData(data, 1u); | 414 cachedImageLowPriority->appendData(data, 1u); |
| 411 cachedImageHighPriority->appendData(data, 4u); | 415 cachedImageHighPriority->appendData(data, 4u); |
| 412 const unsigned lowPrioritySize = cachedImageLowPriority->size(); | 416 const unsigned lowPrioritySize = cachedImageLowPriority->size(); |
| 413 const unsigned highPrioritySize = cachedImageHighPriority->size(); | 417 const unsigned highPrioritySize = cachedImageHighPriority->size(); |
| 414 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi
ze(); | 418 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi
ze(); |
| 415 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded
Size(); | 419 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded
Size(); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 | 577 |
| 574 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); | 578 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); |
| 575 EXPECT_EQ(2u, resources.size()); | 579 EXPECT_EQ(2u, resources.size()); |
| 576 | 580 |
| 577 memoryCache()->evictResources(); | 581 memoryCache()->evictResources(); |
| 578 EXPECT_FALSE(memoryCache()->contains(resource1.get())); | 582 EXPECT_FALSE(memoryCache()->contains(resource1.get())); |
| 579 EXPECT_FALSE(memoryCache()->contains(resource3.get())); | 583 EXPECT_FALSE(memoryCache()->contains(resource3.get())); |
| 580 } | 584 } |
| 581 | 585 |
| 582 } // namespace | 586 } // namespace |
| OLD | NEW |