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

Side by Side Diff: content/browser/loader/resource_dispatcher_host_browsertest.cc

Issue 2484633004: Change Lo-Fi bool to bitmask to support multiple Previews types (Closed)
Patch Set: clean up comments Created 4 years 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 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/public/browser/resource_dispatcher_host.h" 5 #include "content/public/browser/resource_dispatcher_host.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "content/browser/download/download_manager_impl.h" 19 #include "content/browser/download/download_manager_impl.h"
20 #include "content/browser/loader/resource_dispatcher_host_impl.h" 20 #include "content/browser/loader/resource_dispatcher_host_impl.h"
21 #include "content/browser/web_contents/web_contents_impl.h" 21 #include "content/browser/web_contents/web_contents_impl.h"
22 #include "content/public/browser/browser_context.h" 22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/resource_dispatcher_host_delegate.h" 24 #include "content/public/browser/resource_dispatcher_host_delegate.h"
25 #include "content/public/browser/resource_request_info.h" 25 #include "content/public/browser/resource_request_info.h"
26 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/previews_state.h"
27 #include "content/public/common/url_constants.h" 28 #include "content/public/common/url_constants.h"
28 #include "content/public/test/browser_test_utils.h" 29 #include "content/public/test/browser_test_utils.h"
29 #include "content/public/test/content_browser_test.h" 30 #include "content/public/test/content_browser_test.h"
30 #include "content/public/test/content_browser_test_utils.h" 31 #include "content/public/test/content_browser_test_utils.h"
31 #include "content/public/test/test_navigation_observer.h" 32 #include "content/public/test/test_navigation_observer.h"
32 #include "content/public/test/test_utils.h" 33 #include "content/public/test/test_utils.h"
33 #include "content/shell/browser/shell.h" 34 #include "content/shell/browser/shell.h"
34 #include "content/shell/browser/shell_content_browser_client.h" 35 #include "content/shell/browser/shell_content_browser_client.h"
35 #include "content/shell/browser/shell_network_delegate.h" 36 #include "content/shell/browser/shell_network_delegate.h"
36 #include "net/base/net_errors.h" 37 #include "net/base/net_errors.h"
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 shell(), 585 shell(),
585 embedded_test_server()->GetURL("/client_redirect.html"), 586 embedded_test_server()->GetURL("/client_redirect.html"),
586 2); 587 2);
587 588
588 EXPECT_TRUE( 589 EXPECT_TRUE(
589 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); 590 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT);
590 } 591 }
591 592
592 namespace { 593 namespace {
593 594
594 // Checks whether the given urls are requested, and that IsUsingLofi() returns 595 // Checks whether the given urls are requested, and that GetPreviewsState()
595 // the appropriate value when the Lo-Fi state is set. 596 // returns the appropriate value when the Previews are set.
596 class LoFiModeResourceDispatcherHostDelegate 597 class PreviewsStateResourceDispatcherHostDelegate
597 : public ResourceDispatcherHostDelegate { 598 : public ResourceDispatcherHostDelegate {
598 public: 599 public:
599 LoFiModeResourceDispatcherHostDelegate(const GURL& main_frame_url, 600 PreviewsStateResourceDispatcherHostDelegate(const GURL& main_frame_url,
600 const GURL& subresource_url, 601 const GURL& subresource_url,
601 const GURL& iframe_url) 602 const GURL& iframe_url)
602 : main_frame_url_(main_frame_url), 603 : main_frame_url_(main_frame_url),
603 subresource_url_(subresource_url), 604 subresource_url_(subresource_url),
604 iframe_url_(iframe_url), 605 iframe_url_(iframe_url),
605 main_frame_url_seen_(false), 606 main_frame_url_seen_(false),
606 subresource_url_seen_(false), 607 subresource_url_seen_(false),
607 iframe_url_seen_(false), 608 iframe_url_seen_(false),
608 use_lofi_(false), 609 previews_state_(PREVIEWS_OFF),
609 should_enable_lofi_mode_called_(false) {} 610 should_get_previews_state_called_(false) {}
610 611
611 ~LoFiModeResourceDispatcherHostDelegate() override {} 612 ~PreviewsStateResourceDispatcherHostDelegate() override {}
612 613
613 // ResourceDispatcherHostDelegate implementation: 614 // ResourceDispatcherHostDelegate implementation:
614 void RequestBeginning(net::URLRequest* request, 615 void RequestBeginning(net::URLRequest* request,
615 ResourceContext* resource_context, 616 ResourceContext* resource_context,
616 AppCacheService* appcache_service, 617 AppCacheService* appcache_service,
617 ResourceType resource_type, 618 ResourceType resource_type,
618 ScopedVector<ResourceThrottle>* throttles) override { 619 ScopedVector<ResourceThrottle>* throttles) override {
619 DCHECK_CURRENTLY_ON(BrowserThread::IO); 620 DCHECK_CURRENTLY_ON(BrowserThread::IO);
620 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); 621 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
621 if (request->url() != main_frame_url_ && request->url() != subresource_url_ 622 if (request->url() != main_frame_url_ && request->url() != subresource_url_
622 && request->url() != iframe_url_) 623 && request->url() != iframe_url_)
623 return; 624 return;
624 if (request->url() == main_frame_url_) { 625 if (request->url() == main_frame_url_) {
625 EXPECT_FALSE(main_frame_url_seen_); 626 EXPECT_FALSE(main_frame_url_seen_);
626 main_frame_url_seen_ = true; 627 main_frame_url_seen_ = true;
627 } else if (request->url() == subresource_url_) { 628 } else if (request->url() == subresource_url_) {
628 EXPECT_TRUE(main_frame_url_seen_); 629 EXPECT_TRUE(main_frame_url_seen_);
629 EXPECT_FALSE(subresource_url_seen_); 630 EXPECT_FALSE(subresource_url_seen_);
630 subresource_url_seen_ = true; 631 subresource_url_seen_ = true;
631 } else if (request->url() == iframe_url_) { 632 } else if (request->url() == iframe_url_) {
632 EXPECT_TRUE(main_frame_url_seen_); 633 EXPECT_TRUE(main_frame_url_seen_);
633 EXPECT_FALSE(iframe_url_seen_); 634 EXPECT_FALSE(iframe_url_seen_);
634 iframe_url_seen_ = true; 635 iframe_url_seen_ = true;
635 } 636 }
636 EXPECT_EQ(use_lofi_, info->IsUsingLoFi()); 637 EXPECT_EQ(previews_state_, info->GetPreviewsState());
637 } 638 }
638 639
639 void SetDelegate() { 640 void SetDelegate() {
640 DCHECK_CURRENTLY_ON(BrowserThread::IO); 641 DCHECK_CURRENTLY_ON(BrowserThread::IO);
641 ResourceDispatcherHost::Get()->SetDelegate(this); 642 ResourceDispatcherHost::Get()->SetDelegate(this);
642 } 643 }
643 644
644 bool ShouldEnableLoFiMode( 645 int GetPreviewsState(
646 int previews_state,
645 const net::URLRequest& request, 647 const net::URLRequest& request,
646 content::ResourceContext* resource_context) override { 648 content::ResourceContext* resource_context) override {
647 DCHECK_CURRENTLY_ON(BrowserThread::IO); 649 DCHECK_CURRENTLY_ON(BrowserThread::IO);
648 EXPECT_FALSE(should_enable_lofi_mode_called_); 650 if (previews_state & content::PREVIEWS_UNSPECIFIED) {
649 should_enable_lofi_mode_called_ = true; 651 EXPECT_FALSE(should_get_previews_state_called_);
650 EXPECT_EQ(main_frame_url_, request.url()); 652 should_get_previews_state_called_ = true;
651 return use_lofi_; 653 EXPECT_EQ(main_frame_url_, request.url());
654 return previews_state_;
655 }
656 return previews_state;
652 } 657 }
653 658
654 void Reset(bool use_lofi) { 659 void Reset(int previews_state) {
655 DCHECK_CURRENTLY_ON(BrowserThread::IO); 660 DCHECK_CURRENTLY_ON(BrowserThread::IO);
656 main_frame_url_seen_ = false; 661 main_frame_url_seen_ = false;
657 subresource_url_seen_ = false; 662 subresource_url_seen_ = false;
658 iframe_url_seen_ = false; 663 iframe_url_seen_ = false;
659 use_lofi_ = use_lofi; 664 previews_state_ = previews_state;
660 should_enable_lofi_mode_called_ = false; 665 should_get_previews_state_called_ = false;
661 } 666 }
662 667
663 void CheckResourcesRequested(bool should_enable_lofi_mode_called) { 668 void CheckResourcesRequested(bool should_get_previews_state_called) {
664 DCHECK_CURRENTLY_ON(BrowserThread::IO); 669 DCHECK_CURRENTLY_ON(BrowserThread::IO);
665 EXPECT_EQ(should_enable_lofi_mode_called, should_enable_lofi_mode_called_); 670 EXPECT_EQ(should_get_previews_state_called,
671 should_get_previews_state_called_);
666 EXPECT_TRUE(main_frame_url_seen_); 672 EXPECT_TRUE(main_frame_url_seen_);
667 EXPECT_TRUE(subresource_url_seen_); 673 EXPECT_TRUE(subresource_url_seen_);
668 EXPECT_TRUE(iframe_url_seen_); 674 EXPECT_TRUE(iframe_url_seen_);
669 } 675 }
670 676
671 private: 677 private:
672 const GURL main_frame_url_; 678 const GURL main_frame_url_;
673 const GURL subresource_url_; 679 const GURL subresource_url_;
674 const GURL iframe_url_; 680 const GURL iframe_url_;
675 681
676 bool main_frame_url_seen_; 682 bool main_frame_url_seen_;
677 bool subresource_url_seen_; 683 bool subresource_url_seen_;
678 bool iframe_url_seen_; 684 bool iframe_url_seen_;
679 bool use_lofi_; 685 int previews_state_;
680 bool should_enable_lofi_mode_called_; 686 bool should_get_previews_state_called_;
681 687
682 DISALLOW_COPY_AND_ASSIGN(LoFiModeResourceDispatcherHostDelegate); 688 DISALLOW_COPY_AND_ASSIGN(PreviewsStateResourceDispatcherHostDelegate);
683 }; 689 };
684 690
685 } // namespace 691 } // namespace
686 692
687 class LoFiResourceDispatcherHostBrowserTest : public ContentBrowserTest { 693 class PreviewsStateResourceDispatcherHostBrowserTest
694 : public ContentBrowserTest {
688 public: 695 public:
689 ~LoFiResourceDispatcherHostBrowserTest() override {} 696 ~PreviewsStateResourceDispatcherHostBrowserTest() override {}
690 697
691 protected: 698 protected:
692 void SetUpOnMainThread() override { 699 void SetUpOnMainThread() override {
693 ContentBrowserTest::SetUpOnMainThread(); 700 ContentBrowserTest::SetUpOnMainThread();
694 701
695 ASSERT_TRUE(embedded_test_server()->Start()); 702 ASSERT_TRUE(embedded_test_server()->Start());
696 703
697 delegate_.reset(new LoFiModeResourceDispatcherHostDelegate( 704 delegate_.reset(new PreviewsStateResourceDispatcherHostDelegate(
698 embedded_test_server()->GetURL("/page_with_iframe.html"), 705 embedded_test_server()->GetURL("/page_with_iframe.html"),
699 embedded_test_server()->GetURL("/image.jpg"), 706 embedded_test_server()->GetURL("/image.jpg"),
700 embedded_test_server()->GetURL("/title1.html"))); 707 embedded_test_server()->GetURL("/title1.html")));
701 708
702 content::BrowserThread::PostTask( 709 content::BrowserThread::PostTask(
703 content::BrowserThread::IO, 710 content::BrowserThread::IO,
704 FROM_HERE, 711 FROM_HERE,
705 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, 712 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::SetDelegate,
706 base::Unretained(delegate_.get()))); 713 base::Unretained(delegate_.get())));
707 } 714 }
708 715
709 void Reset(bool use_lofi) { 716 void Reset(int previews_state) {
710 content::BrowserThread::PostTask( 717 content::BrowserThread::PostTask(
711 content::BrowserThread::IO, FROM_HERE, 718 content::BrowserThread::IO, FROM_HERE,
712 base::Bind(&LoFiModeResourceDispatcherHostDelegate::Reset, 719 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::Reset,
713 base::Unretained(delegate_.get()), use_lofi)); 720 base::Unretained(delegate_.get()), previews_state));
714 } 721 }
715 722
716 void CheckResourcesRequested( 723 void CheckResourcesRequested(
717 bool should_enable_lofi_mode_called) { 724 bool should_get_previews_state_called) {
718 content::BrowserThread::PostTask( 725 content::BrowserThread::PostTask(
719 content::BrowserThread::IO, FROM_HERE, 726 content::BrowserThread::IO, FROM_HERE,
720 base::Bind( 727 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::
721 &LoFiModeResourceDispatcherHostDelegate::CheckResourcesRequested, 728 CheckResourcesRequested,
722 base::Unretained(delegate_.get()), should_enable_lofi_mode_called)); 729 base::Unretained(delegate_.get()),
730 should_get_previews_state_called));
723 } 731 }
724 732
725 private: 733 private:
726 std::unique_ptr<LoFiModeResourceDispatcherHostDelegate> delegate_; 734 std::unique_ptr<PreviewsStateResourceDispatcherHostDelegate> delegate_;
727 }; 735 };
728 736
729 // Test that navigating with ShouldEnableLoFiMode returning true fetches the 737 // Test that navigating calls GetPreviewsState with SERVER_LOFI_ON.
730 // resources with LOFI_ON. 738 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
731 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
732 ShouldEnableLoFiModeOn) { 739 ShouldEnableLoFiModeOn) {
733 // Navigate with ShouldEnableLoFiMode returning true. 740 // Navigate with ShouldEnableLoFiMode returning true.
734 Reset(true); 741 Reset(SERVER_LOFI_ON);
735 NavigateToURLBlockUntilNavigationsComplete( 742 NavigateToURLBlockUntilNavigationsComplete(
736 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 743 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
737 CheckResourcesRequested(true); 744 CheckResourcesRequested(true);
738 } 745 }
739 746
740 // Test that navigating with ShouldEnableLoFiMode returning false fetches the 747 // Test that navigating calls GetPreviewsState returning PREVIEWS_UNSPECIFIED.
741 // resources with LOFI_OFF. 748 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
742 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
743 ShouldEnableLoFiModeOff) { 749 ShouldEnableLoFiModeOff) {
744 // Navigate with ShouldEnableLoFiMode returning false. 750 // Navigate with GetPreviewsState returning false.
745 NavigateToURLBlockUntilNavigationsComplete( 751 NavigateToURLBlockUntilNavigationsComplete(
746 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 752 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
747 CheckResourcesRequested(true); 753 CheckResourcesRequested(true);
748 } 754 }
749 755
750 // Test that reloading calls ShouldEnableLoFiMode again and changes the Lo-Fi 756 // Test that reloading calls GetPreviewsState again and changes the Previews
751 // state. 757 // state.
752 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, 758 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
753 ShouldEnableLoFiModeReload) { 759 ShouldEnableLoFiModeReload) {
754 // Navigate with ShouldEnableLoFiMode returning false. 760 // Navigate with GetPreviewsState returning PREVIEWS_UNSPECIFIED.
755 NavigateToURLBlockUntilNavigationsComplete( 761 NavigateToURLBlockUntilNavigationsComplete(
756 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 762 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
757 CheckResourcesRequested(true); 763 CheckResourcesRequested(true);
758 764
759 // Reload. ShouldEnableLoFiMode should be called. 765 // Reload. GetPreviewsState should be called.
760 Reset(true); 766 Reset(SERVER_LOFI_ON);
761 ReloadBlockUntilNavigationsComplete(shell(), 1); 767 ReloadBlockUntilNavigationsComplete(shell(), 1);
762 CheckResourcesRequested(true); 768 CheckResourcesRequested(true);
763 } 769 }
764 770
765 // Test that navigating backwards calls ShouldEnableLoFiMode again and changes 771 // Test that navigating backwards calls GetPreviewsState again and changes
766 // the Lo-Fi state. 772 // the Previews state.
767 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, 773 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
768 ShouldEnableLoFiModeNavigateBackThenForward) { 774 ShouldEnableLoFiModeNavigateBackThenForward) {
769 // Navigate with ShouldEnableLoFiMode returning false. 775 // Navigate with GetPreviewsState returning false.
770 NavigateToURLBlockUntilNavigationsComplete( 776 NavigateToURLBlockUntilNavigationsComplete(
771 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 777 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
772 CheckResourcesRequested(true); 778 CheckResourcesRequested(true);
773 779
774 // Go to a different page. 780 // Go to a different page.
775 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); 781 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1);
776 782
777 // Go back with ShouldEnableLoFiMode returning true. 783 // Go back with GetPreviewsState returning SERVER_LOFI_ON.
778 Reset(true); 784 Reset(SERVER_LOFI_ON);
779 TestNavigationObserver tab_observer(shell()->web_contents(), 1); 785 TestNavigationObserver tab_observer(shell()->web_contents(), 1);
780 shell()->GoBackOrForward(-1); 786 shell()->GoBackOrForward(-1);
781 tab_observer.Wait(); 787 tab_observer.Wait();
782 CheckResourcesRequested(true); 788 CheckResourcesRequested(true);
783 } 789 }
784 790
785 // Test that reloading with Lo-Fi disabled doesn't call ShouldEnableLoFiMode and
786 // already has LOFI_OFF.
787 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
788 ShouldEnableLoFiModeReloadDisableLoFi) {
789 // Navigate with ShouldEnableLoFiMode returning true.
790 Reset(true);
791 NavigateToURLBlockUntilNavigationsComplete(
792 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
793 CheckResourcesRequested(true);
794
795 // Reload with Lo-Fi disabled.
796 Reset(false);
797 TestNavigationObserver tab_observer(shell()->web_contents(), 1);
798 shell()->web_contents()->GetController().ReloadDisableLoFi(true);
799 tab_observer.Wait();
800 CheckResourcesRequested(false);
801 }
802
803 namespace { 791 namespace {
804 792
805 struct RequestDataForDelegate { 793 struct RequestDataForDelegate {
806 const GURL url; 794 const GURL url;
807 const GURL first_party; 795 const GURL first_party;
808 const base::Optional<url::Origin> initiator; 796 const base::Optional<url::Origin> initiator;
809 797
810 RequestDataForDelegate(const GURL& url, 798 RequestDataForDelegate(const GURL& url,
811 const GURL& first_party, 799 const GURL& first_party,
812 const base::Optional<url::Origin>& initiator) 800 const base::Optional<url::Origin>& initiator)
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 EXPECT_EQ(top_origin, delegate_->data()[2]->initiator); 1065 EXPECT_EQ(top_origin, delegate_->data()[2]->initiator);
1078 1066
1079 // Cross-origin subresource requests have a unique first-party, and an 1067 // Cross-origin subresource requests have a unique first-party, and an
1080 // initiator that matches the document in which they're embedded. 1068 // initiator that matches the document in which they're embedded.
1081 EXPECT_EQ(nested_js_url, delegate_->data()[3]->url); 1069 EXPECT_EQ(nested_js_url, delegate_->data()[3]->url);
1082 EXPECT_EQ(kURLWithUniqueOrigin, delegate_->data()[3]->first_party); 1070 EXPECT_EQ(kURLWithUniqueOrigin, delegate_->data()[3]->first_party);
1083 EXPECT_EQ(nested_origin, delegate_->data()[3]->initiator); 1071 EXPECT_EQ(nested_origin, delegate_->data()[3]->initiator);
1084 } 1072 }
1085 1073
1086 } // namespace content 1074 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698