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 16 matching lines...) Expand all Loading... |
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/MemoryCache.h" | 36 #include "core/fetch/MemoryCache.h" |
| 37 #include "core/fetch/MockResourceClients.h" |
37 #include "core/fetch/RawResource.h" | 38 #include "core/fetch/RawResource.h" |
38 #include "core/fetch/ResourceLoader.h" | 39 #include "core/fetch/ResourceLoader.h" |
39 #include "platform/exported/WrappedResourceResponse.h" | 40 #include "platform/exported/WrappedResourceResponse.h" |
40 #include "platform/heap/Handle.h" | 41 #include "platform/heap/Handle.h" |
41 #include "platform/heap/HeapAllocator.h" | 42 #include "platform/heap/HeapAllocator.h" |
42 #include "platform/heap/Member.h" | 43 #include "platform/heap/Member.h" |
43 #include "platform/network/ResourceRequest.h" | 44 #include "platform/network/ResourceRequest.h" |
44 #include "platform/network/ResourceTimingInfo.h" | 45 #include "platform/network/ResourceTimingInfo.h" |
45 #include "platform/scheduler/test/fake_web_task_runner.h" | 46 #include "platform/scheduler/test/fake_web_task_runner.h" |
46 #include "platform/testing/URLTestHelpers.h" | 47 #include "platform/testing/URLTestHelpers.h" |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 response.setHTTPStatusCode(200); | 507 response.setHTTPStatusCode(200); |
507 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, testImageFilena
me, WebString::fromUTF8(""), WrappedResourceResponse(response)); | 508 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, testImageFilena
me, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
508 | 509 |
509 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); | 510 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
510 FetchRequest request(url, FetchInitiatorInfo()); | 511 FetchRequest request(url, FetchInitiatorInfo()); |
511 request.makeSynchronous(); | 512 request.makeSynchronous(); |
512 Resource* resource = fetcher->requestResource(request, TestResourceFactory()
); | 513 Resource* resource = fetcher->requestResource(request, TestResourceFactory()
); |
513 EXPECT_TRUE(resource->isLoaded()); | 514 EXPECT_TRUE(resource->isLoaded()); |
514 EXPECT_EQ(ResourceLoadPriorityHighest, resource->resourceRequest().priority(
)); | 515 EXPECT_EQ(ResourceLoadPriorityHighest, resource->resourceRequest().priority(
)); |
515 | 516 |
| 517 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
516 memoryCache()->remove(resource); | 518 memoryCache()->remove(resource); |
517 } | 519 } |
518 | 520 |
| 521 TEST_F(ResourceFetcherTest, PreloadImageTwice) |
| 522 { |
| 523 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
| 524 |
| 525 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); |
| 526 ResourceResponse response; |
| 527 response.setURL(url); |
| 528 response.setHTTPStatusCode(200); |
| 529 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, testImageFilena
me, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
| 530 |
| 531 FetchRequest fetchRequestOriginal = FetchRequest(url, FetchInitiatorInfo()); |
| 532 Resource* resource = fetcher->requestResource(fetchRequestOriginal, TestReso
urceFactory(Resource::Image)); |
| 533 ASSERT_TRUE(resource); |
| 534 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 535 fetcher->preloadStarted(resource); |
| 536 |
| 537 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 538 Resource* newResource = fetcher->requestResource(fetchRequest, TestResourceF
actory(Resource::Image)); |
| 539 EXPECT_EQ(resource, newResource); |
| 540 fetcher->preloadStarted(resource); |
| 541 |
| 542 fetcher->clearPreloads(ResourceFetcher::ClearAllPreloads); |
| 543 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 544 EXPECT_FALSE(memoryCache()->contains(resource)); |
| 545 EXPECT_FALSE(resource->isPreloaded()); |
| 546 } |
| 547 |
| 548 |
| 549 TEST_F(ResourceFetcherTest, LinkPreloadImageAndUse) |
| 550 { |
| 551 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
| 552 |
| 553 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); |
| 554 ResourceResponse response; |
| 555 response.setURL(url); |
| 556 response.setHTTPStatusCode(200); |
| 557 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, testImageFilena
me, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
| 558 |
| 559 // Link preload preload scanner |
| 560 FetchRequest fetchRequestOriginal = FetchRequest(url, FetchInitiatorInfo()); |
| 561 fetchRequestOriginal.setLinkPreload(true); |
| 562 Resource* resource = fetcher->requestResource(fetchRequestOriginal, TestReso
urceFactory(Resource::Image)); |
| 563 ASSERT_TRUE(resource); |
| 564 EXPECT_TRUE(resource->isLinkPreload()); |
| 565 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 566 fetcher->preloadStarted(resource); |
| 567 |
| 568 // Image preload scanner |
| 569 FetchRequest fetchRequestPreloadScanner = FetchRequest(url, FetchInitiatorIn
fo()); |
| 570 Resource* imgPreloadScannerResource = fetcher->requestResource(fetchRequestP
reloadScanner, TestResourceFactory(Resource::Image)); |
| 571 EXPECT_EQ(resource, imgPreloadScannerResource); |
| 572 EXPECT_FALSE(resource->isLinkPreload()); |
| 573 fetcher->preloadStarted(resource); |
| 574 |
| 575 // Image created by parser |
| 576 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 577 Resource* newResource = fetcher->requestResource(fetchRequest, TestResourceF
actory(Resource::Image)); |
| 578 newResource->testOnlyWillAddClientOrObserver(Resource::MarkAsReferenced); |
| 579 EXPECT_EQ(resource, newResource); |
| 580 EXPECT_FALSE(resource->isLinkPreload()); |
| 581 |
| 582 // DCL reached |
| 583 fetcher->clearPreloads(ResourceFetcher::ClearSpeculativeMarkupPreloads); |
| 584 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 585 EXPECT_TRUE(memoryCache()->contains(resource)); |
| 586 EXPECT_FALSE(resource->isPreloaded()); |
| 587 } |
| 588 |
| 589 TEST_F(ResourceFetcherTest, LinkPreloadImageMultipleFetchersAndUse) |
| 590 { |
| 591 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFe
tchContext::create()); |
| 592 ResourceFetcher* fetcher2 = ResourceFetcher::create(ResourceFetcherTestMockF
etchContext::create()); |
| 593 |
| 594 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); |
| 595 ResourceResponse response; |
| 596 response.setURL(url); |
| 597 response.setHTTPStatusCode(200); |
| 598 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, testImageFilena
me, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
| 599 |
| 600 FetchRequest fetchRequestOriginal = FetchRequest(url, FetchInitiatorInfo()); |
| 601 fetchRequestOriginal.setLinkPreload(true); |
| 602 Resource* resource = fetcher->requestResource(fetchRequestOriginal, TestReso
urceFactory(Resource::Image)); |
| 603 ASSERT_TRUE(resource); |
| 604 EXPECT_TRUE(resource->isLinkPreload()); |
| 605 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 606 fetcher->preloadStarted(resource); |
| 607 |
| 608 FetchRequest fetchRequestSecond = FetchRequest(url, FetchInitiatorInfo()); |
| 609 fetchRequestSecond.setLinkPreload(true); |
| 610 Resource* secondResource = fetcher2->requestResource(fetchRequestSecond, Tes
tResourceFactory(Resource::Image)); |
| 611 ASSERT_TRUE(secondResource); |
| 612 EXPECT_TRUE(secondResource->isLinkPreload()); |
| 613 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 614 fetcher2->preloadStarted(secondResource); |
| 615 |
| 616 // Image preload scanner |
| 617 FetchRequest fetchRequestPreloadScanner = FetchRequest(url, FetchInitiatorIn
fo()); |
| 618 Resource* imgPreloadScannerResource = fetcher->requestResource(fetchRequestP
reloadScanner, TestResourceFactory(Resource::Image)); |
| 619 EXPECT_EQ(resource, imgPreloadScannerResource); |
| 620 EXPECT_FALSE(resource->isLinkPreload()); |
| 621 fetcher->preloadStarted(resource); |
| 622 |
| 623 // Image created by parser |
| 624 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
| 625 Resource* newResource = fetcher->requestResource(fetchRequest, TestResourceF
actory(Resource::Image)); |
| 626 newResource->testOnlyWillAddClientOrObserver(Resource::MarkAsReferenced); |
| 627 EXPECT_EQ(resource, newResource); |
| 628 EXPECT_FALSE(resource->isLinkPreload()); |
| 629 |
| 630 // DCL reached on first fetcher |
| 631 EXPECT_TRUE(resource->isPreloaded()); |
| 632 fetcher->clearPreloads(ResourceFetcher::ClearSpeculativeMarkupPreloads); |
| 633 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 634 EXPECT_TRUE(memoryCache()->contains(resource)); |
| 635 EXPECT_TRUE(resource->isPreloaded()); |
| 636 |
| 637 // DCL reached on second fetcher |
| 638 fetcher2->clearPreloads(ResourceFetcher::ClearSpeculativeMarkupPreloads); |
| 639 EXPECT_TRUE(memoryCache()->contains(resource)); |
| 640 EXPECT_FALSE(resource->isPreloaded()); |
| 641 } |
| 642 |
519 } // namespace blink | 643 } // namespace blink |
OLD | NEW |