Chromium Code Reviews| 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 14 matching lines...) Expand all Loading... | |
| 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/loader/resource/ImageResource.h" | 31 #include "core/loader/resource/ImageResource.h" |
| 32 | 32 |
| 33 #include <memory> | 33 #include <memory> |
| 34 #include "core/loader/resource/MockImageResourceObserver.h" | 34 #include "core/loader/resource/MockImageResourceObserver.h" |
| 35 #include "platform/RuntimeEnabledFeatures.h" | |
| 35 #include "platform/SharedBuffer.h" | 36 #include "platform/SharedBuffer.h" |
| 36 #include "platform/exported/WrappedResourceResponse.h" | 37 #include "platform/exported/WrappedResourceResponse.h" |
| 37 #include "platform/graphics/BitmapImage.h" | 38 #include "platform/graphics/BitmapImage.h" |
| 38 #include "platform/graphics/Image.h" | 39 #include "platform/graphics/Image.h" |
| 39 #include "platform/loader/fetch/FetchInitiatorInfo.h" | 40 #include "platform/loader/fetch/FetchInitiatorInfo.h" |
| 40 #include "platform/loader/fetch/FetchParameters.h" | 41 #include "platform/loader/fetch/FetchParameters.h" |
| 41 #include "platform/loader/fetch/MemoryCache.h" | 42 #include "platform/loader/fetch/MemoryCache.h" |
| 42 #include "platform/loader/fetch/ResourceFetcher.h" | 43 #include "platform/loader/fetch/ResourceFetcher.h" |
| 43 #include "platform/loader/fetch/ResourceLoader.h" | 44 #include "platform/loader/fetch/ResourceLoader.h" |
| 44 #include "platform/loader/fetch/UniqueIdentifier.h" | 45 #include "platform/loader/fetch/UniqueIdentifier.h" |
| 45 #include "platform/loader/testing/MockFetchContext.h" | 46 #include "platform/loader/testing/MockFetchContext.h" |
| 46 #include "platform/loader/testing/MockResourceClient.h" | 47 #include "platform/loader/testing/MockResourceClient.h" |
| 47 #include "platform/scheduler/test/fake_web_task_runner.h" | 48 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 49 #include "platform/testing/RuntimeEnabledFeaturesTestHelpers.h" | |
| 48 #include "platform/testing/ScopedMockedURL.h" | 50 #include "platform/testing/ScopedMockedURL.h" |
| 49 #include "platform/testing/TestingPlatformSupport.h" | 51 #include "platform/testing/TestingPlatformSupport.h" |
| 50 #include "platform/testing/UnitTestHelpers.h" | 52 #include "platform/testing/UnitTestHelpers.h" |
| 51 #include "platform/wtf/PtrUtil.h" | 53 #include "platform/wtf/PtrUtil.h" |
| 52 #include "platform/wtf/text/Base64.h" | 54 #include "platform/wtf/text/Base64.h" |
| 53 #include "public/platform/Platform.h" | 55 #include "public/platform/Platform.h" |
| 54 #include "public/platform/WebCachePolicy.h" | 56 #include "public/platform/WebCachePolicy.h" |
| 55 #include "public/platform/WebURL.h" | 57 #include "public/platform/WebURL.h" |
| 56 #include "public/platform/WebURLLoaderMockFactory.h" | 58 #include "public/platform/WebURLLoaderMockFactory.h" |
| 57 #include "public/platform/WebURLResponse.h" | 59 #include "public/platform/WebURLResponse.h" |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); | 338 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); |
| 337 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsSVGImage()); | 339 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsSVGImage()); |
| 338 } | 340 } |
| 339 | 341 |
| 340 ResourceFetcher* CreateFetcher() { | 342 ResourceFetcher* CreateFetcher() { |
| 341 auto* context = | 343 auto* context = |
| 342 MockFetchContext::Create(MockFetchContext::kShouldLoadNewResource); | 344 MockFetchContext::Create(MockFetchContext::kShouldLoadNewResource); |
| 343 return ResourceFetcher::Create(context, context->GetTaskRunner()); | 345 return ResourceFetcher::Create(context, context->GetTaskRunner()); |
| 344 } | 346 } |
| 345 | 347 |
| 348 using ScopedClientPlaceholderForServerLoFiForTest = | |
| 349 ScopedRuntimeEnabledFeatureForTest< | |
| 350 RuntimeEnabledFeatures::ClientPlaceholdersForServerLoFiEnabled, | |
| 351 RuntimeEnabledFeatures::SetClientPlaceholdersForServerLoFiEnabled>; | |
| 352 | |
| 346 TEST(ImageResourceTest, MultipartImage) { | 353 TEST(ImageResourceTest, MultipartImage) { |
| 347 ResourceFetcher* fetcher = CreateFetcher(); | 354 ResourceFetcher* fetcher = CreateFetcher(); |
| 348 KURL test_url(kParsedURLString, kTestURL); | 355 KURL test_url(kParsedURLString, kTestURL); |
| 349 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | 356 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); |
| 350 | 357 |
| 351 // Emulate starting a real load, but don't expect any "real" | 358 // Emulate starting a real load, but don't expect any "real" |
| 352 // WebURLLoaderClient callbacks. | 359 // WebURLLoaderClient callbacks. |
| 353 ImageResource* image_resource = ImageResource::CreateForTest(test_url); | 360 ImageResource* image_resource = ImageResource::CreateForTest(test_url); |
| 354 image_resource->SetIdentifier(CreateUniqueIdentifier()); | 361 image_resource->SetIdentifier(CreateUniqueIdentifier()); |
| 355 fetcher->StartLoad(image_resource); | 362 fetcher->StartLoad(image_resource); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 552 image_resource->Finish(); | 559 image_resource->Finish(); |
| 553 EXPECT_FALSE(image_resource->ErrorOccurred()); | 560 EXPECT_FALSE(image_resource->ErrorOccurred()); |
| 554 ASSERT_TRUE(image_resource->GetContent()->HasImage()); | 561 ASSERT_TRUE(image_resource->GetContent()->HasImage()); |
| 555 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); | 562 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); |
| 556 EXPECT_EQ(2, observer->ImageChangedCount()); | 563 EXPECT_EQ(2, observer->ImageChangedCount()); |
| 557 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); | 564 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); |
| 558 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); | 565 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); |
| 559 } | 566 } |
| 560 | 567 |
| 561 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { | 568 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { |
| 562 KURL test_url(kParsedURLString, kTestURL); | 569 const bool kBools[] = {false, true}; |
|
kouhei (in TOK)
2017/07/07 01:38:03
Use TEST_P and INSTANTIATE_TEST_CASE_P(..., ::test
sclittle
2017/07/07 02:46:41
Done.
| |
| 563 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | 570 for (bool enable_show_placeholder_for_server_lofi : kBools) { |
| 564 ImageResource* image_resource = ImageResource::CreateForTest(test_url); | 571 ScopedClientPlaceholderForServerLoFiForTest scoped_show_placeholder( |
| 565 image_resource->SetStatus(ResourceStatus::kPending); | 572 enable_show_placeholder_for_server_lofi); |
| 566 image_resource->NotifyStartLoad(); | 573 KURL test_url(kParsedURLString, kTestURL); |
| 574 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | |
| 575 ImageResource* image_resource = ImageResource::CreateForTest(test_url); | |
| 576 image_resource->SetStatus(ResourceStatus::kPending); | |
| 577 image_resource->NotifyStartLoad(); | |
| 567 | 578 |
| 568 std::unique_ptr<MockImageResourceObserver> observer = | 579 std::unique_ptr<MockImageResourceObserver> observer = |
| 569 MockImageResourceObserver::Create(image_resource->GetContent()); | 580 MockImageResourceObserver::Create(image_resource->GetContent()); |
| 570 ResourceFetcher* fetcher = CreateFetcher(); | 581 ResourceFetcher* fetcher = CreateFetcher(); |
| 571 | 582 |
| 572 // Send the image response. | 583 // Send the image response. |
| 573 ResourceResponse resource_response(KURL(), "image/jpeg", sizeof(kJpegImage), | 584 ResourceResponse resource_response(KURL(), "image/jpeg", sizeof(kJpegImage), |
| 574 g_null_atom); | 585 g_null_atom); |
| 575 resource_response.AddHTTPHeaderField("chrome-proxy-content-transform", | 586 resource_response.AddHTTPHeaderField("chrome-proxy-content-transform", |
| 576 "empty-image"); | 587 "empty-image"); |
| 577 | 588 |
| 578 image_resource->ResponseReceived(resource_response, nullptr); | 589 image_resource->ResponseReceived(resource_response, nullptr); |
| 579 image_resource->AppendData(reinterpret_cast<const char*>(kJpegImage), | 590 image_resource->AppendData(reinterpret_cast<const char*>(kJpegImage), |
| 580 sizeof(kJpegImage)); | 591 sizeof(kJpegImage)); |
| 581 image_resource->Finish(); | 592 image_resource->Finish(); |
| 582 EXPECT_FALSE(image_resource->ErrorOccurred()); | 593 EXPECT_FALSE(image_resource->ErrorOccurred()); |
| 583 ASSERT_TRUE(image_resource->GetContent()->HasImage()); | 594 ASSERT_TRUE(image_resource->GetContent()->HasImage()); |
| 584 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); | 595 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); |
| 585 EXPECT_EQ(2, observer->ImageChangedCount()); | 596 EXPECT_EQ(2, observer->ImageChangedCount()); |
| 586 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnLastImageChanged()); | 597 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnLastImageChanged()); |
| 587 // The observer should have been notified that the image load completed. | 598 // The observer should have been notified that the image load completed. |
| 588 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); | 599 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); |
| 589 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnImageNotifyFinished()); | 600 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnImageNotifyFinished()); |
| 590 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); | 601 EXPECT_NE(enable_show_placeholder_for_server_lofi, |
| 591 EXPECT_EQ(kJpegImageWidth, image_resource->GetContent()->GetImage()->width()); | 602 image_resource->GetContent()->GetImage()->IsBitmapImage()); |
| 592 EXPECT_EQ(kJpegImageHeight, | 603 EXPECT_EQ(enable_show_placeholder_for_server_lofi, |
| 593 image_resource->GetContent()->GetImage()->height()); | 604 image_resource->ShouldShowPlaceholder()); |
| 605 EXPECT_EQ(kJpegImageWidth, | |
| 606 image_resource->GetContent()->GetImage()->width()); | |
| 607 EXPECT_EQ(kJpegImageHeight, | |
| 608 image_resource->GetContent()->GetImage()->height()); | |
| 594 | 609 |
| 595 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. | 610 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished |
| 596 image_resource->ReloadIfLoFiOrPlaceholderImage(fetcher, | 611 // loading. |
| 597 Resource::kReloadAlways); | 612 image_resource->ReloadIfLoFiOrPlaceholderImage(fetcher, |
| 613 Resource::kReloadAlways); | |
| 598 | 614 |
| 599 EXPECT_EQ(3, observer->ImageChangedCount()); | 615 EXPECT_EQ(3, observer->ImageChangedCount()); |
| 600 TestThatReloadIsStartedThenServeReload( | 616 TestThatReloadIsStartedThenServeReload( |
| 601 test_url, image_resource, image_resource->GetContent(), observer.get(), | 617 test_url, image_resource, image_resource->GetContent(), observer.get(), |
| 602 WebCachePolicy::kBypassingCache, false); | 618 WebCachePolicy::kBypassingCache, false); |
| 619 } | |
| 603 } | 620 } |
| 604 | 621 |
| 605 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinishedWithOldHeaders) { | 622 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinishedWithOldHeaders) { |
| 606 KURL test_url(kParsedURLString, kTestURL); | 623 const bool kBools[] = {false, true}; |
| 607 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | 624 for (bool enable_show_placeholder_for_server_lofi : kBools) { |
| 608 ImageResource* image_resource = ImageResource::CreateForTest(test_url); | 625 ScopedClientPlaceholderForServerLoFiForTest scoped_show_placeholder( |
| 609 image_resource->SetStatus(ResourceStatus::kPending); | 626 enable_show_placeholder_for_server_lofi); |
| 610 image_resource->NotifyStartLoad(); | 627 KURL test_url(kParsedURLString, kTestURL); |
| 628 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | |
| 629 ImageResource* image_resource = ImageResource::CreateForTest(test_url); | |
| 630 image_resource->SetStatus(ResourceStatus::kPending); | |
| 631 image_resource->NotifyStartLoad(); | |
| 611 | 632 |
| 612 std::unique_ptr<MockImageResourceObserver> observer = | 633 std::unique_ptr<MockImageResourceObserver> observer = |
| 613 MockImageResourceObserver::Create(image_resource->GetContent()); | 634 MockImageResourceObserver::Create(image_resource->GetContent()); |
| 614 ResourceFetcher* fetcher = CreateFetcher(); | 635 ResourceFetcher* fetcher = CreateFetcher(); |
| 615 | 636 |
| 616 // Send the image response. | 637 // Send the image response. |
| 617 ResourceResponse resource_response(KURL(), "image/jpeg", sizeof(kJpegImage), | 638 ResourceResponse resource_response(KURL(), "image/jpeg", sizeof(kJpegImage), |
| 618 g_null_atom); | 639 g_null_atom); |
| 619 resource_response.AddHTTPHeaderField("chrome-proxy", "q=low"); | 640 resource_response.AddHTTPHeaderField("chrome-proxy", "q=low"); |
| 620 | 641 |
| 621 image_resource->ResponseReceived(resource_response, nullptr); | 642 image_resource->ResponseReceived(resource_response, nullptr); |
| 622 image_resource->AppendData(reinterpret_cast<const char*>(kJpegImage), | 643 image_resource->AppendData(reinterpret_cast<const char*>(kJpegImage), |
| 623 sizeof(kJpegImage)); | 644 sizeof(kJpegImage)); |
| 624 image_resource->Finish(); | 645 image_resource->Finish(); |
| 625 EXPECT_FALSE(image_resource->ErrorOccurred()); | 646 EXPECT_FALSE(image_resource->ErrorOccurred()); |
| 626 ASSERT_TRUE(image_resource->GetContent()->HasImage()); | 647 ASSERT_TRUE(image_resource->GetContent()->HasImage()); |
| 627 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); | 648 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); |
| 628 EXPECT_EQ(2, observer->ImageChangedCount()); | 649 EXPECT_EQ(2, observer->ImageChangedCount()); |
| 629 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnLastImageChanged()); | 650 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnLastImageChanged()); |
| 630 // The observer should have been notified that the image load completed. | 651 // The observer should have been notified that the image load completed. |
| 631 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); | 652 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); |
| 632 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnImageNotifyFinished()); | 653 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnImageNotifyFinished()); |
| 633 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); | 654 EXPECT_NE(enable_show_placeholder_for_server_lofi, |
| 634 EXPECT_EQ(kJpegImageWidth, image_resource->GetContent()->GetImage()->width()); | 655 image_resource->GetContent()->GetImage()->IsBitmapImage()); |
| 635 EXPECT_EQ(kJpegImageHeight, | 656 EXPECT_EQ(enable_show_placeholder_for_server_lofi, |
| 636 image_resource->GetContent()->GetImage()->height()); | 657 image_resource->ShouldShowPlaceholder()); |
| 658 EXPECT_EQ(kJpegImageWidth, | |
| 659 image_resource->GetContent()->GetImage()->width()); | |
| 660 EXPECT_EQ(kJpegImageHeight, | |
| 661 image_resource->GetContent()->GetImage()->height()); | |
| 637 | 662 |
| 638 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. | 663 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished |
| 639 image_resource->ReloadIfLoFiOrPlaceholderImage(fetcher, | 664 // loading. |
| 640 Resource::kReloadAlways); | 665 image_resource->ReloadIfLoFiOrPlaceholderImage(fetcher, |
| 666 Resource::kReloadAlways); | |
| 641 | 667 |
| 642 EXPECT_EQ(3, observer->ImageChangedCount()); | 668 EXPECT_EQ(3, observer->ImageChangedCount()); |
| 643 TestThatReloadIsStartedThenServeReload( | 669 TestThatReloadIsStartedThenServeReload( |
| 644 test_url, image_resource, image_resource->GetContent(), observer.get(), | 670 test_url, image_resource, image_resource->GetContent(), observer.get(), |
| 645 WebCachePolicy::kBypassingCache, false); | 671 WebCachePolicy::kBypassingCache, false); |
| 672 } | |
| 646 } | 673 } |
| 647 | 674 |
| 648 TEST(ImageResourceTest, | 675 TEST(ImageResourceTest, |
| 649 ReloadIfLoFiOrPlaceholderAfterFinishedWithoutLoFiHeaders) { | 676 ReloadIfLoFiOrPlaceholderAfterFinishedWithoutLoFiHeaders) { |
| 650 KURL test_url(kParsedURLString, kTestURL); | 677 KURL test_url(kParsedURLString, kTestURL); |
| 651 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | 678 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); |
| 652 ResourceRequest request(test_url); | 679 ResourceRequest request(test_url); |
| 653 request.SetPreviewsState(WebURLRequest::kServerLoFiOn); | 680 request.SetPreviewsState(WebURLRequest::kServerLoFiOn); |
| 654 request.SetFetchCredentialsMode(WebURLRequest::kFetchCredentialsModeOmit); | 681 request.SetFetchCredentialsMode(WebURLRequest::kFetchCredentialsModeOmit); |
| 655 ImageResource* image_resource = ImageResource::Create(request); | 682 ImageResource* image_resource = ImageResource::Create(request); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 725 | 752 |
| 726 EXPECT_EQ(3, observer->ImageChangedCount()); | 753 EXPECT_EQ(3, observer->ImageChangedCount()); |
| 727 | 754 |
| 728 TestThatReloadIsStartedThenServeReload( | 755 TestThatReloadIsStartedThenServeReload( |
| 729 test_url, image_resource, content, observer.get(), | 756 test_url, image_resource, content, observer.get(), |
| 730 WebCachePolicy::kBypassingCache, false); | 757 WebCachePolicy::kBypassingCache, false); |
| 731 | 758 |
| 732 GetMemoryCache()->Remove(image_resource); | 759 GetMemoryCache()->Remove(image_resource); |
| 733 } | 760 } |
| 734 | 761 |
| 735 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { | 762 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderBeforeResponse) { |
| 736 KURL test_url(kParsedURLString, kTestURL); | 763 const bool kBools[] = {false, true}; |
| 737 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | 764 for (bool enable_show_placeholder_for_server_lofi : kBools) { |
| 765 ScopedClientPlaceholderForServerLoFiForTest scoped_show_placeholder( | |
| 766 enable_show_placeholder_for_server_lofi); | |
| 767 KURL test_url(kParsedURLString, kTestURL); | |
| 768 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | |
| 738 | 769 |
| 739 ResourceRequest request(test_url); | 770 ResourceRequest request(test_url); |
| 740 request.SetPreviewsState(WebURLRequest::kServerLoFiOn); | 771 request.SetPreviewsState(WebURLRequest::kServerLoFiOn); |
| 741 FetchParameters fetch_params(request); | 772 FetchParameters fetch_params(request); |
| 742 ResourceFetcher* fetcher = CreateFetcher(); | 773 ResourceFetcher* fetcher = CreateFetcher(); |
| 743 | 774 |
| 744 ImageResource* image_resource = ImageResource::Fetch(fetch_params, fetcher); | 775 ImageResource* image_resource = ImageResource::Fetch(fetch_params, fetcher); |
| 745 std::unique_ptr<MockImageResourceObserver> observer = | 776 std::unique_ptr<MockImageResourceObserver> observer = |
| 746 MockImageResourceObserver::Create(image_resource->GetContent()); | 777 MockImageResourceObserver::Create(image_resource->GetContent()); |
| 747 | 778 |
| 748 // Send the image response. | 779 EXPECT_FALSE(image_resource->ErrorOccurred()); |
| 749 image_resource->Loader()->DidReceiveResponse( | 780 EXPECT_EQ(enable_show_placeholder_for_server_lofi, |
| 750 WrappedResourceResponse(ResourceResponse( | 781 image_resource->ShouldShowPlaceholder()); |
| 751 test_url, "image/jpeg", sizeof(kJpegImage), g_null_atom))); | |
| 752 image_resource->Loader()->DidReceiveData( | |
| 753 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | |
| 754 | 782 |
| 755 EXPECT_FALSE(image_resource->ErrorOccurred()); | 783 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. |
| 756 ASSERT_TRUE(image_resource->GetContent()->HasImage()); | 784 image_resource->ReloadIfLoFiOrPlaceholderImage(fetcher, |
| 757 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); | 785 Resource::kReloadAlways); |
| 758 EXPECT_EQ(1, observer->ImageChangedCount()); | |
| 759 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnLastImageChanged()); | |
| 760 EXPECT_FALSE(observer->ImageNotifyFinishedCalled()); | |
| 761 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); | |
| 762 EXPECT_EQ(kJpegImageWidth, image_resource->GetContent()->GetImage()->width()); | |
| 763 EXPECT_EQ(kJpegImageHeight, | |
| 764 image_resource->GetContent()->GetImage()->height()); | |
| 765 | 786 |
| 766 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. | 787 EXPECT_EQ(1, observer->ImageChangedCount()); |
| 767 image_resource->ReloadIfLoFiOrPlaceholderImage(fetcher, | 788 EXPECT_EQ(0, observer->ImageWidthOnLastImageChanged()); |
| 768 Resource::kReloadAlways); | 789 // The observer should not have been notified of completion yet, since the |
| 790 // image is still loading. | |
| 791 EXPECT_FALSE(observer->ImageNotifyFinishedCalled()); | |
| 769 | 792 |
| 770 EXPECT_EQ(2, observer->ImageChangedCount()); | 793 TestThatReloadIsStartedThenServeReload( |
| 771 EXPECT_EQ(0, observer->ImageWidthOnLastImageChanged()); | 794 test_url, image_resource, image_resource->GetContent(), observer.get(), |
| 772 // The observer should not have been notified of completion yet, since the | 795 WebCachePolicy::kBypassingCache, false); |
| 773 // image is still loading. | 796 } |
| 774 EXPECT_FALSE(observer->ImageNotifyFinishedCalled()); | 797 } |
| 775 | 798 |
| 776 TestThatReloadIsStartedThenServeReload( | 799 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringResponse) { |
| 777 test_url, image_resource, image_resource->GetContent(), observer.get(), | 800 const bool kBools[] = {false, true}; |
| 778 WebCachePolicy::kBypassingCache, false); | 801 for (bool enable_show_placeholder_for_server_lofi : kBools) { |
| 802 ScopedClientPlaceholderForServerLoFiForTest scoped_show_placeholder( | |
| 803 enable_show_placeholder_for_server_lofi); | |
| 804 KURL test_url(kParsedURLString, kTestURL); | |
| 805 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | |
| 806 | |
| 807 ResourceRequest request(test_url); | |
| 808 request.SetPreviewsState(WebURLRequest::kServerLoFiOn); | |
| 809 FetchParameters fetch_params(request); | |
| 810 ResourceFetcher* fetcher = CreateFetcher(); | |
| 811 | |
| 812 ImageResource* image_resource = ImageResource::Fetch(fetch_params, fetcher); | |
| 813 std::unique_ptr<MockImageResourceObserver> observer = | |
| 814 MockImageResourceObserver::Create(image_resource->GetContent()); | |
| 815 | |
| 816 // Send the image response. | |
| 817 ResourceResponse resource_response(test_url, "image/jpeg", | |
| 818 sizeof(kJpegImage), g_null_atom); | |
| 819 resource_response.AddHTTPHeaderField("chrome-proxy-content-transform", | |
| 820 "empty-image"); | |
| 821 | |
| 822 image_resource->Loader()->DidReceiveResponse( | |
| 823 WrappedResourceResponse(resource_response)); | |
| 824 image_resource->Loader()->DidReceiveData( | |
| 825 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | |
| 826 | |
| 827 EXPECT_FALSE(image_resource->ErrorOccurred()); | |
| 828 ASSERT_TRUE(image_resource->GetContent()->HasImage()); | |
| 829 EXPECT_FALSE(image_resource->GetContent()->GetImage()->IsNull()); | |
| 830 EXPECT_EQ(1, observer->ImageChangedCount()); | |
| 831 EXPECT_EQ(kJpegImageWidth, observer->ImageWidthOnLastImageChanged()); | |
| 832 EXPECT_FALSE(observer->ImageNotifyFinishedCalled()); | |
| 833 EXPECT_EQ(enable_show_placeholder_for_server_lofi, | |
| 834 image_resource->ShouldShowPlaceholder()); | |
| 835 EXPECT_EQ(kJpegImageWidth, | |
| 836 image_resource->GetContent()->GetImage()->width()); | |
| 837 EXPECT_EQ(kJpegImageHeight, | |
| 838 image_resource->GetContent()->GetImage()->height()); | |
| 839 | |
| 840 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. | |
| 841 image_resource->ReloadIfLoFiOrPlaceholderImage(fetcher, | |
| 842 Resource::kReloadAlways); | |
| 843 | |
| 844 EXPECT_EQ(2, observer->ImageChangedCount()); | |
| 845 EXPECT_EQ(0, observer->ImageWidthOnLastImageChanged()); | |
| 846 // The observer should not have been notified of completion yet, since the | |
| 847 // image is still loading. | |
| 848 EXPECT_FALSE(observer->ImageNotifyFinishedCalled()); | |
| 849 | |
| 850 TestThatReloadIsStartedThenServeReload( | |
| 851 test_url, image_resource, image_resource->GetContent(), observer.get(), | |
| 852 WebCachePolicy::kBypassingCache, false); | |
| 853 } | |
| 779 } | 854 } |
| 780 | 855 |
| 781 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { | 856 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { |
| 782 KURL test_url(kParsedURLString, kTestURL); | 857 KURL test_url(kParsedURLString, kTestURL); |
| 783 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); | 858 ScopedMockedURLLoad scoped_mocked_url_load(test_url, GetTestFilePath()); |
| 784 | 859 |
| 785 ResourceFetcher* fetcher = CreateFetcher(); | 860 ResourceFetcher* fetcher = CreateFetcher(); |
| 786 FetchParameters params{ResourceRequest(test_url)}; | 861 FetchParameters params{ResourceRequest(test_url)}; |
| 787 params.SetAllowImagePlaceholder(); | 862 params.SetAllowImagePlaceholder(); |
| 788 ImageResource* image_resource = ImageResource::Fetch(params, fetcher); | 863 ImageResource* image_resource = ImageResource::Fetch(params, fetcher); |
| (...skipping 897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1686 ImageResource* image_resource = ImageResource::CreateForTest(test_url); | 1761 ImageResource* image_resource = ImageResource::CreateForTest(test_url); |
| 1687 image_resource->SetStatus(ResourceStatus::kPending); | 1762 image_resource->SetStatus(ResourceStatus::kPending); |
| 1688 image_resource->NotifyStartLoad(); | 1763 image_resource->NotifyStartLoad(); |
| 1689 | 1764 |
| 1690 std::unique_ptr<MockImageResourceObserver> observer = | 1765 std::unique_ptr<MockImageResourceObserver> observer = |
| 1691 MockImageResourceObserver::Create(image_resource->GetContent()); | 1766 MockImageResourceObserver::Create(image_resource->GetContent()); |
| 1692 | 1767 |
| 1693 // Send the image response. | 1768 // Send the image response. |
| 1694 ResourceResponse resource_response(KURL(), "image/jpeg", sizeof(kJpegImage2), | 1769 ResourceResponse resource_response(KURL(), "image/jpeg", sizeof(kJpegImage2), |
| 1695 g_null_atom); | 1770 g_null_atom); |
| 1696 resource_response.AddHTTPHeaderField("chrome-proxy", "q=low"); | |
| 1697 | 1771 |
| 1698 image_resource->ResponseReceived(resource_response, nullptr); | 1772 image_resource->ResponseReceived(resource_response, nullptr); |
| 1699 | 1773 |
| 1700 // This is number is sufficiently large amount of bytes necessary for the | 1774 // This is number is sufficiently large amount of bytes necessary for the |
| 1701 // image to be created (since the size is known). This was determined by | 1775 // image to be created (since the size is known). This was determined by |
| 1702 // appending one byte at a time (with flushes) until the image was decoded. | 1776 // appending one byte at a time (with flushes) until the image was decoded. |
| 1703 size_t meaningful_image_size = 280; | 1777 size_t meaningful_image_size = 280; |
| 1704 image_resource->AppendData(reinterpret_cast<const char*>(kJpegImage2), | 1778 image_resource->AppendData(reinterpret_cast<const char*>(kJpegImage2), |
| 1705 meaningful_image_size); | 1779 meaningful_image_size); |
| 1706 size_t bytes_sent = meaningful_image_size; | 1780 size_t bytes_sent = meaningful_image_size; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1759 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); | 1833 EXPECT_TRUE(observer->ImageNotifyFinishedCalled()); |
| 1760 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); | 1834 EXPECT_TRUE(image_resource->GetContent()->GetImage()->IsBitmapImage()); |
| 1761 EXPECT_EQ(50, image_resource->GetContent()->GetImage()->width()); | 1835 EXPECT_EQ(50, image_resource->GetContent()->GetImage()->width()); |
| 1762 EXPECT_EQ(50, image_resource->GetContent()->GetImage()->height()); | 1836 EXPECT_EQ(50, image_resource->GetContent()->GetImage()->height()); |
| 1763 | 1837 |
| 1764 WTF::SetTimeFunctionsForTesting(nullptr); | 1838 WTF::SetTimeFunctionsForTesting(nullptr); |
| 1765 } | 1839 } |
| 1766 | 1840 |
| 1767 } // namespace | 1841 } // namespace |
| 1768 } // namespace blink | 1842 } // namespace blink |
| OLD | NEW |