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

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

Issue 2510603002: Loading: split tests depend on FontResource to loader/resource/FontResourceTest (Closed)
Patch Set: rebase for cache aware loading Created 4 years, 1 month 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 15 matching lines...) Expand all
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/ResourceFetcher.h" 31 #include "core/fetch/ResourceFetcher.h"
32 32
33 #include "core/fetch/FetchInitiatorInfo.h" 33 #include "core/fetch/FetchInitiatorInfo.h"
34 #include "core/fetch/FetchInitiatorTypeNames.h" 34 #include "core/fetch/FetchInitiatorTypeNames.h"
35 #include "core/fetch/FetchRequest.h" 35 #include "core/fetch/FetchRequest.h"
36 #include "core/fetch/FontResource.h"
37 #include "core/fetch/ImageResource.h" 36 #include "core/fetch/ImageResource.h"
38 #include "core/fetch/MemoryCache.h" 37 #include "core/fetch/MemoryCache.h"
39 #include "core/fetch/MockFetchContext.h" 38 #include "core/fetch/MockFetchContext.h"
40 #include "core/fetch/MockResourceClients.h" 39 #include "core/fetch/MockResourceClients.h"
41 #include "core/fetch/RawResource.h" 40 #include "core/fetch/RawResource.h"
42 #include "core/fetch/ResourceLoader.h" 41 #include "core/fetch/ResourceLoader.h"
43 #include "platform/WebTaskRunner.h" 42 #include "platform/WebTaskRunner.h"
44 #include "platform/exported/WrappedResourceResponse.h" 43 #include "platform/exported/WrappedResourceResponse.h"
45 #include "platform/heap/Handle.h" 44 #include "platform/heap/Handle.h"
46 #include "platform/heap/HeapAllocator.h" 45 #include "platform/heap/HeapAllocator.h"
47 #include "platform/heap/Member.h" 46 #include "platform/heap/Member.h"
48 #include "platform/network/ResourceError.h"
49 #include "platform/network/ResourceRequest.h" 47 #include "platform/network/ResourceRequest.h"
50 #include "platform/network/ResourceTimingInfo.h" 48 #include "platform/network/ResourceTimingInfo.h"
51 #include "platform/scheduler/test/fake_web_task_runner.h" 49 #include "platform/scheduler/test/fake_web_task_runner.h"
52 #include "platform/testing/URLTestHelpers.h" 50 #include "platform/testing/URLTestHelpers.h"
53 #include "platform/testing/weburl_loader_mock.h" 51 #include "platform/testing/weburl_loader_mock.h"
54 #include "platform/weborigin/KURL.h" 52 #include "platform/weborigin/KURL.h"
55 #include "public/platform/Platform.h" 53 #include "public/platform/Platform.h"
56 #include "public/platform/WebURLLoaderMockFactory.h" 54 #include "public/platform/WebURLLoaderMockFactory.h"
57 #include "public/platform/WebURLResponse.h" 55 #include "public/platform/WebURLResponse.h"
58 #include "testing/gtest/include/gtest/gtest.h" 56 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 Persistent<RequestSameResourceOnComplete> client = 252 Persistent<RequestSameResourceOnComplete> client =
255 new RequestSameResourceOnComplete(resource1); 253 new RequestSameResourceOnComplete(resource1);
256 resource1->addClient(client); 254 resource1->addClient(client);
257 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); 255 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
258 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); 256 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url);
259 EXPECT_TRUE(client->notifyFinishedCalled()); 257 EXPECT_TRUE(client->notifyFinishedCalled());
260 resource1->removeClient(client); 258 resource1->removeClient(client);
261 memoryCache()->remove(resource1); 259 memoryCache()->remove(resource1);
262 } 260 }
263 261
264 TEST_F(ResourceFetcherTest, RevalidateDeferedResourceFromTwoInitiators) {
265 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff");
266 ResourceResponse response;
267 response.setURL(url);
268 response.setHTTPStatusCode(200);
269 response.setHTTPHeaderField(HTTPNames::ETag, "1234567890");
270 Platform::current()->getURLLoaderMockFactory()->registerURL(
271 url, WrappedResourceResponse(response), "");
272
273 MockFetchContext* context =
274 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource);
275 ResourceFetcher* fetcher = ResourceFetcher::create(context);
276
277 // Fetch to cache a resource.
278 ResourceRequest request1(url);
279 FetchRequest fetchRequest1 = FetchRequest(request1, FetchInitiatorInfo());
280 Resource* resource1 = FontResource::fetch(fetchRequest1, fetcher);
281 ASSERT_TRUE(resource1);
282 fetcher->startLoad(resource1);
283 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
284 EXPECT_TRUE(resource1->isLoaded());
285 EXPECT_FALSE(resource1->errorOccurred());
286
287 // Set the context as it is on reloads.
288 context->setLoadComplete(true);
289 context->setCachePolicy(CachePolicyRevalidate);
290
291 // Revalidate the resource.
292 ResourceRequest request2(url);
293 FetchRequest fetchRequest2 = FetchRequest(request2, FetchInitiatorInfo());
294 Resource* resource2 = FontResource::fetch(fetchRequest2, fetcher);
295 ASSERT_TRUE(resource2);
296 EXPECT_EQ(resource1, resource2);
297 EXPECT_TRUE(resource2->isCacheValidator());
298 EXPECT_TRUE(resource2->stillNeedsLoad());
299
300 // Fetch the same resource again before actual load operation starts.
301 ResourceRequest request3(url);
302 FetchRequest fetchRequest3 = FetchRequest(request3, FetchInitiatorInfo());
303 Resource* resource3 = FontResource::fetch(fetchRequest3, fetcher);
304 ASSERT_TRUE(resource3);
305 EXPECT_EQ(resource2, resource3);
306 EXPECT_TRUE(resource3->isCacheValidator());
307 EXPECT_TRUE(resource3->stillNeedsLoad());
308
309 // startLoad() can be called from any initiator. Here, call it from the
310 // latter.
311 fetcher->startLoad(resource3);
312 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
313 EXPECT_TRUE(resource3->isLoaded());
314 EXPECT_FALSE(resource3->errorOccurred());
315 EXPECT_TRUE(resource2->isLoaded());
316 EXPECT_FALSE(resource2->errorOccurred());
317
318 memoryCache()->remove(resource1);
319 }
320
321 TEST_F(ResourceFetcherTest, DontReuseMediaDataUrl) { 262 TEST_F(ResourceFetcherTest, DontReuseMediaDataUrl) {
322 ResourceFetcher* fetcher = ResourceFetcher::create( 263 ResourceFetcher* fetcher = ResourceFetcher::create(
323 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource)); 264 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource));
324 ResourceRequest request(KURL(ParsedURLString, "data:text/html,foo")); 265 ResourceRequest request(KURL(ParsedURLString, "data:text/html,foo"));
325 request.setRequestContext(WebURLRequest::RequestContextVideo); 266 request.setRequestContext(WebURLRequest::RequestContextVideo);
326 ResourceLoaderOptions options; 267 ResourceLoaderOptions options;
327 options.dataBufferingPolicy = DoNotBufferData; 268 options.dataBufferingPolicy = DoNotBufferData;
328 FetchRequest fetchRequest = 269 FetchRequest fetchRequest =
329 FetchRequest(request, FetchInitiatorTypeNames::internal, options); 270 FetchRequest(request, FetchInitiatorTypeNames::internal, options);
330 Resource* resource1 = RawResource::fetchMedia(fetchRequest, fetcher); 271 Resource* resource1 = RawResource::fetchMedia(fetchRequest, fetcher);
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 FetchRequest(resourceRequest, FetchInitiatorInfo()); 607 FetchRequest(resourceRequest, FetchInitiatorInfo());
667 Platform::current()->getURLLoaderMockFactory()->registerURL( 608 Platform::current()->getURLLoaderMockFactory()->registerURL(
668 url, WebURLResponse(), ""); 609 url, WebURLResponse(), "");
669 Resource* newResource = RawResource::fetch(fetchRequest, fetcher); 610 Resource* newResource = RawResource::fetch(fetchRequest, fetcher);
670 fetcher->stopFetching(); 611 fetcher->stopFetching();
671 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); 612 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url);
672 613
673 EXPECT_NE(resource, newResource); 614 EXPECT_NE(resource, newResource);
674 } 615 }
675 616
676 TEST_F(ResourceFetcherTest, CacheAwareFontLoading) {
677 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff");
678 ResourceResponse response;
679 response.setURL(url);
680 response.setHTTPStatusCode(200);
681 Platform::current()->getURLLoaderMockFactory()->registerURL(
682 url, WrappedResourceResponse(response), "");
683
684 ResourceFetcher* fetcher = ResourceFetcher::create(
685 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource));
686
687 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo());
688 fetchRequest.setCacheAwareLoadingEnabled(IsCacheAwareLoadingEnabled);
689 FontResource* resource = FontResource::fetch(fetchRequest, fetcher);
690 ASSERT_TRUE(resource);
691
692 Persistent<MockFontResourceClient> client =
693 new MockFontResourceClient(resource);
694 fetcher->startLoad(resource);
695 EXPECT_TRUE(resource->loader()->isCacheAwareLoadingActivated());
696 resource->m_loadLimitState = FontResource::UnderLimit;
697
698 // FontResource callbacks should be blocked during cache-aware loading.
699 resource->fontLoadShortLimitCallback(nullptr);
700 EXPECT_FALSE(client->fontLoadShortLimitExceededCalled());
701
702 // Fail first request as disk cache miss.
703 resource->loader()->didFail(ResourceError::cacheMissError(url));
704
705 // Once cache miss error returns, previously blocked callbacks should be
706 // called immediately.
707 EXPECT_FALSE(resource->loader()->isCacheAwareLoadingActivated());
708 EXPECT_TRUE(client->fontLoadShortLimitExceededCalled());
709 EXPECT_FALSE(client->fontLoadLongLimitExceededCalled());
710
711 // Add client now, fontLoadShortLimitExceeded() should be called.
712 Persistent<MockFontResourceClient> client2 =
713 new MockFontResourceClient(resource);
714 EXPECT_TRUE(client2->fontLoadShortLimitExceededCalled());
715 EXPECT_FALSE(client2->fontLoadLongLimitExceededCalled());
716
717 // FontResource callbacks are not blocked now.
718 resource->fontLoadLongLimitCallback(nullptr);
719 EXPECT_TRUE(client->fontLoadLongLimitExceededCalled());
720
721 // Add client now, both callbacks should be called.
722 Persistent<MockFontResourceClient> client3 =
723 new MockFontResourceClient(resource);
724 EXPECT_TRUE(client3->fontLoadShortLimitExceededCalled());
725 EXPECT_TRUE(client3->fontLoadLongLimitExceededCalled());
726
727 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
728 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url);
729 memoryCache()->remove(resource);
730 }
731
732 } // namespace blink 617 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698