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

Side by Side Diff: third_party/WebKit/Source/core/fetch/MemoryCacheTest.cpp

Issue 1706083002: Split ImageResourceClient into ResourceClient and ImageResourceObserver [1/2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More consistent Client/Observer Created 4 years, 10 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
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 12 matching lines...) Expand all
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
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 "core/fetch/MemoryCache.h" 31 #include "core/fetch/MemoryCache.h"
32 32
33 #include "core/fetch/MockImageResourceClient.h" 33 #include "core/fetch/MockResourceClients.h"
34 #include "core/fetch/RawResource.h" 34 #include "core/fetch/RawResource.h"
35 #include "platform/network/ResourceRequest.h" 35 #include "platform/network/ResourceRequest.h"
36 #include "platform/testing/UnitTestHelpers.h" 36 #include "platform/testing/UnitTestHelpers.h"
37 #include "public/platform/Platform.h" 37 #include "public/platform/Platform.h"
38 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
39 #include "wtf/OwnPtr.h" 39 #include "wtf/OwnPtr.h"
40 40
41 namespace blink { 41 namespace blink {
42 42
43 class MemoryCacheTest : public ::testing::Test { 43 class MemoryCacheTest : public ::testing::Test {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 RefPtrWillBeRawPtr<FakeResource> cachedResource = 126 RefPtrWillBeRawPtr<FakeResource> cachedResource =
127 FakeResource::create(ResourceRequest("http://test/resource"), Resource:: Raw); 127 FakeResource::create(ResourceRequest("http://test/resource"), Resource:: Raw);
128 cachedResource->fakeEncodedSize(resourceSize1); 128 cachedResource->fakeEncodedSize(resourceSize1);
129 129
130 ASSERT_EQ(0u, memoryCache()->deadSize()); 130 ASSERT_EQ(0u, memoryCache()->deadSize());
131 ASSERT_EQ(0u, memoryCache()->liveSize()); 131 ASSERT_EQ(0u, memoryCache()->liveSize());
132 memoryCache()->add(cachedResource.get()); 132 memoryCache()->add(cachedResource.get());
133 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); 133 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize());
134 ASSERT_EQ(0u, memoryCache()->liveSize()); 134 ASSERT_EQ(0u, memoryCache()->liveSize());
135 135
136 MockImageResourceClient client(cachedResource); 136 MockResourceClient client(cachedResource);
137 ASSERT_EQ(0u, memoryCache()->deadSize()); 137 ASSERT_EQ(0u, memoryCache()->deadSize());
138 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); 138 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize());
139 139
140 cachedResource->fakeEncodedSize(resourceSize2); 140 cachedResource->fakeEncodedSize(resourceSize2);
141 ASSERT_EQ(0u, memoryCache()->deadSize()); 141 ASSERT_EQ(0u, memoryCache()->deadSize());
142 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); 142 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize());
143 } 143 }
144 144
145 // Verifies that dead resources that exceed dead resource capacity are evicted 145 // Verifies that dead resources that exceed dead resource capacity are evicted
146 // from cache when pruning. 146 // from cache when pruning.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 199
200 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, Re source* cachedLiveResource) 200 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, Re source* cachedLiveResource)
201 { 201 {
202 memoryCache()->setDelayBeforeLiveDecodedPrune(0); 202 memoryCache()->setDelayBeforeLiveDecodedPrune(0);
203 const unsigned totalCapacity = 1; 203 const unsigned totalCapacity = 1;
204 const unsigned minDeadCapacity = 0; 204 const unsigned minDeadCapacity = 0;
205 const unsigned maxDeadCapacity = 0; 205 const unsigned maxDeadCapacity = 0;
206 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity ); 206 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity );
207 const char data[6] = "abcde"; 207 const char data[6] = "abcde";
208 cachedDeadResource->appendData(data, 3u); 208 cachedDeadResource->appendData(data, 3u);
209 MockImageResourceClient client(cachedLiveResource); 209 MockResourceClient client(cachedLiveResource);
210 cachedLiveResource->appendData(data, 4u); 210 cachedLiveResource->appendData(data, 4u);
211 211
212 class Task1 : public WebTaskRunner::Task { 212 class Task1 : public WebTaskRunner::Task {
213 public: 213 public:
214 Task1(Resource* live, Resource* dead) 214 Task1(Resource* live, Resource* dead)
215 : m_live(live) 215 : m_live(live)
216 , m_dead(dead) 216 , m_dead(dead)
217 { } 217 { }
218 218
219 void run() override 219 void run() override
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource.get(), cachedLive Resource.get()); 307 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource.get(), cachedLive Resource.get());
308 memoryCache()->evictResources(); 308 memoryCache()->evictResources();
309 } 309 }
310 } 310 }
311 311
312 // Verifies that cached resources are evicted immediately after release when 312 // Verifies that cached resources are evicted immediately after release when
313 // the total dead resource size is more than double the dead resource capacity. 313 // the total dead resource size is more than double the dead resource capacity.
314 static void TestClientRemoval(Resource* resource1, Resource* resource2) 314 static void TestClientRemoval(Resource* resource1, Resource* resource2)
315 { 315 {
316 const char data[6] = "abcde"; 316 const char data[6] = "abcde";
317 MockImageResourceClient client1(resource1); 317 MockResourceClient client1(resource1);
318 resource1->appendData(data, 4u); 318 resource1->appendData(data, 4u);
319 MockImageResourceClient client2(resource2); 319 MockResourceClient client2(resource2);
320 resource2->appendData(data, 4u); 320 resource2->appendData(data, 4u);
321 321
322 const unsigned minDeadCapacity = 0; 322 const unsigned minDeadCapacity = 0;
323 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) / 2) - 1; 323 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) / 2) - 1;
324 const unsigned totalCapacity = maxDeadCapacity; 324 const unsigned totalCapacity = maxDeadCapacity;
325 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity ); 325 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity );
326 memoryCache()->add(resource1); 326 memoryCache()->add(resource1);
327 memoryCache()->add(resource2); 327 memoryCache()->add(resource2);
328 // Call prune. There is nothing to prune, but this will initialize 328 // Call prune. There is nothing to prune, but this will initialize
329 // the prune timestamp, allowing future prunes to be deferred. 329 // the prune timestamp, allowing future prunes to be deferred.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 } 395 }
396 } 396 }
397 397
398 // Verifies that CachedResources are evicted from the decode cache 398 // Verifies that CachedResources are evicted from the decode cache
399 // according to their DecodeCachePriority. 399 // according to their DecodeCachePriority.
400 static void TestDecodeCacheOrder(Resource* cachedImageLowPriority, Resource* cac hedImageHighPriority) 400 static void TestDecodeCacheOrder(Resource* cachedImageLowPriority, Resource* cac hedImageHighPriority)
401 { 401 {
402 memoryCache()->setDelayBeforeLiveDecodedPrune(0); 402 memoryCache()->setDelayBeforeLiveDecodedPrune(0);
403 memoryCache()->setMaxPruneDeferralDelay(0); 403 memoryCache()->setMaxPruneDeferralDelay(0);
404 404
405 MockImageResourceClient clientLowPriority(cachedImageLowPriority); 405 MockResourceClient clientLowPriority(cachedImageLowPriority);
406 MockImageResourceClient clientHighPriority(cachedImageHighPriority); 406 MockResourceClient clientHighPriority(cachedImageHighPriority);
407 407
408 const char data[5] = "abcd"; 408 const char data[5] = "abcd";
409 cachedImageLowPriority->appendData(data, 1u); 409 cachedImageLowPriority->appendData(data, 1u);
410 cachedImageHighPriority->appendData(data, 4u); 410 cachedImageHighPriority->appendData(data, 4u);
411 const unsigned lowPrioritySize = cachedImageLowPriority->size(); 411 const unsigned lowPrioritySize = cachedImageLowPriority->size();
412 const unsigned highPrioritySize = cachedImageHighPriority->size(); 412 const unsigned highPrioritySize = cachedImageHighPriority->size();
413 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi ze(); 413 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi ze();
414 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded Size(); 414 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded Size();
415 const unsigned totalSize = lowPrioritySize + highPrioritySize; 415 const unsigned totalSize = lowPrioritySize + highPrioritySize;
416 416
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 541
542 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re sourcesForURL(url); 542 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re sourcesForURL(url);
543 EXPECT_EQ(2u, resources.size()); 543 EXPECT_EQ(2u, resources.size());
544 544
545 memoryCache()->evictResources(); 545 memoryCache()->evictResources();
546 EXPECT_FALSE(memoryCache()->contains(resource1.get())); 546 EXPECT_FALSE(memoryCache()->contains(resource1.get()));
547 EXPECT_FALSE(memoryCache()->contains(resource3.get())); 547 EXPECT_FALSE(memoryCache()->contains(resource3.get()));
548 } 548 }
549 549
550 } // namespace blink 550 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698