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

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: use PreviewsState everywhere 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 PreviewsState GetPreviewsState(
645 const net::URLRequest& request, 646 const net::URLRequest& request,
646 content::ResourceContext* resource_context) override { 647 content::ResourceContext* resource_context) override {
647 DCHECK_CURRENTLY_ON(BrowserThread::IO); 648 DCHECK_CURRENTLY_ON(BrowserThread::IO);
648 EXPECT_FALSE(should_enable_lofi_mode_called_); 649 EXPECT_FALSE(should_get_previews_state_called_);
649 should_enable_lofi_mode_called_ = true; 650 should_get_previews_state_called_ = true;
650 EXPECT_EQ(main_frame_url_, request.url()); 651 EXPECT_EQ(main_frame_url_, request.url());
651 return use_lofi_; 652 return previews_state_;
652 } 653 }
653 654
654 void Reset(bool use_lofi) { 655 void Reset(PreviewsState previews_state) {
655 DCHECK_CURRENTLY_ON(BrowserThread::IO); 656 DCHECK_CURRENTLY_ON(BrowserThread::IO);
656 main_frame_url_seen_ = false; 657 main_frame_url_seen_ = false;
657 subresource_url_seen_ = false; 658 subresource_url_seen_ = false;
658 iframe_url_seen_ = false; 659 iframe_url_seen_ = false;
659 use_lofi_ = use_lofi; 660 previews_state_ = previews_state;
660 should_enable_lofi_mode_called_ = false; 661 should_get_previews_state_called_ = false;
661 } 662 }
662 663
663 void CheckResourcesRequested(bool should_enable_lofi_mode_called) { 664 void CheckResourcesRequested(bool should_get_previews_state_called) {
664 DCHECK_CURRENTLY_ON(BrowserThread::IO); 665 DCHECK_CURRENTLY_ON(BrowserThread::IO);
665 EXPECT_EQ(should_enable_lofi_mode_called, should_enable_lofi_mode_called_); 666 EXPECT_EQ(should_get_previews_state_called,
667 should_get_previews_state_called_);
666 EXPECT_TRUE(main_frame_url_seen_); 668 EXPECT_TRUE(main_frame_url_seen_);
667 EXPECT_TRUE(subresource_url_seen_); 669 EXPECT_TRUE(subresource_url_seen_);
668 EXPECT_TRUE(iframe_url_seen_); 670 EXPECT_TRUE(iframe_url_seen_);
669 } 671 }
670 672
671 private: 673 private:
672 const GURL main_frame_url_; 674 const GURL main_frame_url_;
673 const GURL subresource_url_; 675 const GURL subresource_url_;
674 const GURL iframe_url_; 676 const GURL iframe_url_;
675 677
676 bool main_frame_url_seen_; 678 bool main_frame_url_seen_;
677 bool subresource_url_seen_; 679 bool subresource_url_seen_;
678 bool iframe_url_seen_; 680 bool iframe_url_seen_;
679 bool use_lofi_; 681 PreviewsState previews_state_;
680 bool should_enable_lofi_mode_called_; 682 bool should_get_previews_state_called_;
681 683
682 DISALLOW_COPY_AND_ASSIGN(LoFiModeResourceDispatcherHostDelegate); 684 DISALLOW_COPY_AND_ASSIGN(PreviewsStateResourceDispatcherHostDelegate);
683 }; 685 };
684 686
685 } // namespace 687 } // namespace
686 688
687 class LoFiResourceDispatcherHostBrowserTest : public ContentBrowserTest { 689 class PreviewsStateResourceDispatcherHostBrowserTest
690 : public ContentBrowserTest {
688 public: 691 public:
689 ~LoFiResourceDispatcherHostBrowserTest() override {} 692 ~PreviewsStateResourceDispatcherHostBrowserTest() override {}
690 693
691 protected: 694 protected:
692 void SetUpOnMainThread() override { 695 void SetUpOnMainThread() override {
693 ContentBrowserTest::SetUpOnMainThread(); 696 ContentBrowserTest::SetUpOnMainThread();
694 697
695 ASSERT_TRUE(embedded_test_server()->Start()); 698 ASSERT_TRUE(embedded_test_server()->Start());
696 699
697 delegate_.reset(new LoFiModeResourceDispatcherHostDelegate( 700 delegate_.reset(new PreviewsStateResourceDispatcherHostDelegate(
698 embedded_test_server()->GetURL("/page_with_iframe.html"), 701 embedded_test_server()->GetURL("/page_with_iframe.html"),
699 embedded_test_server()->GetURL("/image.jpg"), 702 embedded_test_server()->GetURL("/image.jpg"),
700 embedded_test_server()->GetURL("/title1.html"))); 703 embedded_test_server()->GetURL("/title1.html")));
701 704
702 content::BrowserThread::PostTask( 705 content::BrowserThread::PostTask(
703 content::BrowserThread::IO, 706 content::BrowserThread::IO,
704 FROM_HERE, 707 FROM_HERE,
705 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, 708 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::SetDelegate,
706 base::Unretained(delegate_.get()))); 709 base::Unretained(delegate_.get())));
707 } 710 }
708 711
709 void Reset(bool use_lofi) { 712 void Reset(PreviewsState previews_state) {
710 content::BrowserThread::PostTask( 713 content::BrowserThread::PostTask(
711 content::BrowserThread::IO, FROM_HERE, 714 content::BrowserThread::IO, FROM_HERE,
712 base::Bind(&LoFiModeResourceDispatcherHostDelegate::Reset, 715 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::Reset,
713 base::Unretained(delegate_.get()), use_lofi)); 716 base::Unretained(delegate_.get()), previews_state));
714 } 717 }
715 718
716 void CheckResourcesRequested( 719 void CheckResourcesRequested(
717 bool should_enable_lofi_mode_called) { 720 bool should_get_previews_state_called) {
718 content::BrowserThread::PostTask( 721 content::BrowserThread::PostTask(
719 content::BrowserThread::IO, FROM_HERE, 722 content::BrowserThread::IO, FROM_HERE,
720 base::Bind( 723 base::Bind(&PreviewsStateResourceDispatcherHostDelegate::
721 &LoFiModeResourceDispatcherHostDelegate::CheckResourcesRequested, 724 CheckResourcesRequested,
722 base::Unretained(delegate_.get()), should_enable_lofi_mode_called)); 725 base::Unretained(delegate_.get()),
726 should_get_previews_state_called));
723 } 727 }
724 728
725 private: 729 private:
726 std::unique_ptr<LoFiModeResourceDispatcherHostDelegate> delegate_; 730 std::unique_ptr<PreviewsStateResourceDispatcherHostDelegate> delegate_;
727 }; 731 };
728 732
729 // Test that navigating with ShouldEnableLoFiMode returning true fetches the 733 // Test that navigating calls GetPreviewsState with SERVER_LOFI_ON.
730 // resources with LOFI_ON. 734 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
731 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
732 ShouldEnableLoFiModeOn) { 735 ShouldEnableLoFiModeOn) {
733 // Navigate with ShouldEnableLoFiMode returning true. 736 // Navigate with ShouldEnableLoFiMode returning true.
734 Reset(true); 737 Reset(SERVER_LOFI_ON);
735 NavigateToURLBlockUntilNavigationsComplete( 738 NavigateToURLBlockUntilNavigationsComplete(
736 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 739 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
737 CheckResourcesRequested(true); 740 CheckResourcesRequested(true);
738 } 741 }
739 742
740 // Test that navigating with ShouldEnableLoFiMode returning false fetches the 743 // Test that navigating calls GetPreviewsState returning PREVIEWS_OFF.
741 // resources with LOFI_OFF. 744 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
742 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
743 ShouldEnableLoFiModeOff) { 745 ShouldEnableLoFiModeOff) {
744 // Navigate with ShouldEnableLoFiMode returning false. 746 // Navigate with GetPreviewsState returning false.
745 NavigateToURLBlockUntilNavigationsComplete( 747 NavigateToURLBlockUntilNavigationsComplete(
746 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 748 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
747 CheckResourcesRequested(true); 749 CheckResourcesRequested(true);
748 } 750 }
749 751
750 // Test that reloading calls ShouldEnableLoFiMode again and changes the Lo-Fi 752 // Test that reloading calls GetPreviewsState again and changes the Previews
751 // state. 753 // state.
752 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, 754 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
753 ShouldEnableLoFiModeReload) { 755 ShouldEnableLoFiModeReload) {
754 // Navigate with ShouldEnableLoFiMode returning false. 756 // Navigate with GetPreviewsState returning PREVIEWS_OFF.
755 NavigateToURLBlockUntilNavigationsComplete( 757 NavigateToURLBlockUntilNavigationsComplete(
756 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 758 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
757 CheckResourcesRequested(true); 759 CheckResourcesRequested(true);
758 760
759 // Reload. ShouldEnableLoFiMode should be called. 761 // Reload. GetPreviewsState should be called.
760 Reset(true); 762 Reset(SERVER_LOFI_ON);
761 ReloadBlockUntilNavigationsComplete(shell(), 1); 763 ReloadBlockUntilNavigationsComplete(shell(), 1);
762 CheckResourcesRequested(true); 764 CheckResourcesRequested(true);
763 } 765 }
764 766
765 // Test that navigating backwards calls ShouldEnableLoFiMode again and changes 767 // Test that navigating backwards calls GetPreviewsState again and changes
766 // the Lo-Fi state. 768 // the Previews state.
767 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, 769 IN_PROC_BROWSER_TEST_F(PreviewsStateResourceDispatcherHostBrowserTest,
768 ShouldEnableLoFiModeNavigateBackThenForward) { 770 ShouldEnableLoFiModeNavigateBackThenForward) {
769 // Navigate with ShouldEnableLoFiMode returning false. 771 // Navigate with GetPreviewsState returning false.
770 NavigateToURLBlockUntilNavigationsComplete( 772 NavigateToURLBlockUntilNavigationsComplete(
771 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); 773 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
772 CheckResourcesRequested(true); 774 CheckResourcesRequested(true);
773 775
774 // Go to a different page. 776 // Go to a different page.
775 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); 777 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1);
776 778
777 // Go back with ShouldEnableLoFiMode returning true. 779 // Go back with GetPreviewsState returning SERVER_LOFI_ON.
778 Reset(true); 780 Reset(SERVER_LOFI_ON);
779 TestNavigationObserver tab_observer(shell()->web_contents(), 1); 781 TestNavigationObserver tab_observer(shell()->web_contents(), 1);
780 shell()->GoBackOrForward(-1); 782 shell()->GoBackOrForward(-1);
781 tab_observer.Wait(); 783 tab_observer.Wait();
782 CheckResourcesRequested(true); 784 CheckResourcesRequested(true);
783 } 785 }
784 786
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) {
nasko 2016/12/12 23:05:41 What is the new behavior for when reloading? Can w
megjablon 2016/12/13 03:03:14 Added this back in. It was removed because my prev
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 { 787 namespace {
804 788
805 struct RequestDataForDelegate { 789 struct RequestDataForDelegate {
806 const GURL url; 790 const GURL url;
807 const GURL first_party; 791 const GURL first_party;
808 const base::Optional<url::Origin> initiator; 792 const base::Optional<url::Origin> initiator;
809 793
810 RequestDataForDelegate(const GURL& url, 794 RequestDataForDelegate(const GURL& url,
811 const GURL& first_party, 795 const GURL& first_party,
812 const base::Optional<url::Origin>& initiator) 796 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); 1061 EXPECT_EQ(top_origin, delegate_->data()[2]->initiator);
1078 1062
1079 // Cross-origin subresource requests have a unique first-party, and an 1063 // Cross-origin subresource requests have a unique first-party, and an
1080 // initiator that matches the document in which they're embedded. 1064 // initiator that matches the document in which they're embedded.
1081 EXPECT_EQ(nested_js_url, delegate_->data()[3]->url); 1065 EXPECT_EQ(nested_js_url, delegate_->data()[3]->url);
1082 EXPECT_EQ(kURLWithUniqueOrigin, delegate_->data()[3]->first_party); 1066 EXPECT_EQ(kURLWithUniqueOrigin, delegate_->data()[3]->first_party);
1083 EXPECT_EQ(nested_origin, delegate_->data()[3]->initiator); 1067 EXPECT_EQ(nested_origin, delegate_->data()[3]->initiator);
1084 } 1068 }
1085 1069
1086 } // namespace content 1070 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698