Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "base/bind_helpers.h" | |
| 5 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
| 6 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 9 #include "content/browser/download/download_manager_impl.h" | 10 #include "content/browser/download/download_manager_impl.h" |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | 11 #include "content/browser/web_contents/web_contents_impl.h" |
| 11 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/resource_dispatcher_host.h" | 14 #include "content/public/browser/resource_dispatcher_host.h" |
| 14 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 15 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 15 #include "content/public/browser/resource_request_info.h" | 16 #include "content/public/browser/resource_request_info.h" |
| 16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/url_constants.h" | 18 #include "content/public/common/url_constants.h" |
| 18 #include "content/public/test/browser_test_utils.h" | 19 #include "content/public/test/browser_test_utils.h" |
| 19 #include "content/public/test/content_browser_test.h" | 20 #include "content/public/test/content_browser_test.h" |
| 20 #include "content/public/test/content_browser_test_utils.h" | 21 #include "content/public/test/content_browser_test_utils.h" |
| 22 #include "content/public/test/test_navigation_observer.h" | |
| 21 #include "content/public/test/test_utils.h" | 23 #include "content/public/test/test_utils.h" |
| 22 #include "content/shell/browser/shell.h" | 24 #include "content/shell/browser/shell.h" |
| 23 #include "content/shell/browser/shell_content_browser_client.h" | 25 #include "content/shell/browser/shell_content_browser_client.h" |
| 24 #include "content/shell/browser/shell_network_delegate.h" | 26 #include "content/shell/browser/shell_network_delegate.h" |
| 25 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 27 #include "net/test/embedded_test_server/http_request.h" | 29 #include "net/test/embedded_test_server/http_request.h" |
| 28 #include "net/test/embedded_test_server/http_response.h" | 30 #include "net/test/embedded_test_server/http_response.h" |
| 29 #include "net/test/url_request/url_request_failed_job.h" | 31 #include "net/test/url_request/url_request_failed_job.h" |
| 30 #include "net/test/url_request/url_request_mock_http_job.h" | 32 #include "net/test/url_request/url_request_mock_http_job.h" |
| (...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 519 | 521 |
| 520 NavigateToURLBlockUntilNavigationsComplete( | 522 NavigateToURLBlockUntilNavigationsComplete( |
| 521 shell(), | 523 shell(), |
| 522 embedded_test_server()->GetURL("/client_redirect.html"), | 524 embedded_test_server()->GetURL("/client_redirect.html"), |
| 523 2); | 525 2); |
| 524 | 526 |
| 525 EXPECT_TRUE( | 527 EXPECT_TRUE( |
| 526 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); | 528 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); |
| 527 } | 529 } |
| 528 | 530 |
| 531 class LoFiModeResourceDispatcherHostDelegate | |
|
mmenke
2015/10/12 20:19:28
Should document this class.
mmenke
2015/10/12 20:19:29
This needs to be in an anonymous namespace.
megjablon
2015/10/12 22:06:06
Done.
megjablon
2015/10/12 22:06:06
Done.
| |
| 532 : public ResourceDispatcherHostDelegate { | |
| 533 public: | |
| 534 LoFiModeResourceDispatcherHostDelegate(const GURL& mainframe_url, | |
| 535 const GURL& subresource_url, | |
| 536 const GURL& iframe_url) | |
|
mmenke
2015/10/12 20:19:29
Should include gurl.h (Yea, should have already be
megjablon
2015/10/12 22:06:06
Done.
| |
| 537 : mainframe_url_(mainframe_url), | |
| 538 subresource_url_(subresource_url), | |
| 539 iframe_url_(iframe_url), | |
| 540 use_lofi_(false), | |
| 541 should_enable_lofi_mode_called_(false) {} | |
| 542 | |
| 543 // ResourceDispatcherHostDelegate implementation: | |
| 544 void RequestBeginning(net::URLRequest* request, | |
|
mmenke
2015/10/12 20:19:29
Should include net/url_request/url_request.h
megjablon
2015/10/12 22:06:06
Done.
| |
| 545 ResourceContext* resource_context, | |
| 546 AppCacheService* appcache_service, | |
| 547 ResourceType resource_type, | |
| 548 ScopedVector<ResourceThrottle>* throttles) override { | |
| 549 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | |
| 550 if (request->url() == mainframe_url_) { | |
| 551 EXPECT_FALSE(mainframe_url_seen_); | |
| 552 mainframe_url_seen_ = true; | |
|
mmenke
2015/10/12 20:19:28
nit: main_frame_...
megjablon
2015/10/12 22:06:06
Done.
| |
| 553 } else if (request->url() == subresource_url_) { | |
|
mmenke
2015/10/12 20:19:29
EXPECT_TRUE(mainframe_url_seen);?
megjablon
2015/10/12 22:06:06
Done.
| |
| 554 EXPECT_FALSE(subresource_url_seen_); | |
| 555 subresource_url_seen_ = true; | |
| 556 } else if (request->url() == iframe_url_) { | |
|
mmenke
2015/10/12 20:19:28
EXPECT_TRUE(mainframe_url_seen);?
megjablon
2015/10/12 22:06:05
Done.
| |
| 557 EXPECT_FALSE(iframe_url_seen_); | |
| 558 iframe_url_seen_ = true; | |
| 559 } | |
| 560 EXPECT_EQ(use_lofi_, info->IsUsingLoFi()); | |
| 561 } | |
| 562 | |
| 563 void SetDelegate() { | |
| 564 ResourceDispatcherHost::Get()->SetDelegate(this); | |
| 565 } | |
| 566 | |
| 567 bool ShouldEnableLoFiMode( | |
| 568 net::URLRequest* request, | |
| 569 content::ResourceContext* resource_context) override { | |
| 570 EXPECT_FALSE(should_enable_lofi_mode_called_); | |
| 571 should_enable_lofi_mode_called_ = true; | |
| 572 EXPECT_EQ(mainframe_url_, request->url()); | |
| 573 return use_lofi_; | |
| 574 } | |
| 575 | |
| 576 void Reset(bool use_lofi) { | |
| 577 mainframe_url_seen_ = false; | |
| 578 subresource_url_seen_ = false; | |
| 579 iframe_url_seen_ = false; | |
| 580 use_lofi_ = use_lofi; | |
| 581 should_enable_lofi_mode_called_ = false; | |
| 582 } | |
| 583 | |
| 584 void SetUrls(const GURL& mainframe_url, | |
| 585 const GURL& subresource_url, | |
| 586 const GURL& iframe_url) { | |
| 587 mainframe_url_ = mainframe_url; | |
| 588 subresource_url_ = subresource_url; | |
| 589 iframe_url_ = iframe_url; | |
| 590 } | |
| 591 | |
| 592 void CheckResourcesRequested() { | |
|
mmenke
2015/10/12 20:19:29
const
megjablon
2015/10/12 22:06:05
This doesn't work with binding, do you know why?
| |
| 593 EXPECT_TRUE(mainframe_url_seen_); | |
| 594 if (!subresource_url_.is_empty()) | |
| 595 EXPECT_TRUE(subresource_url_seen_); | |
| 596 if (!iframe_url_.is_empty()) | |
|
mmenke
2015/10/12 20:19:29
Don't think we need to handle empty urls (See comm
megjablon
2015/10/12 22:06:05
Done.
| |
| 597 EXPECT_TRUE(iframe_url_seen_); | |
| 598 } | |
| 599 | |
| 600 bool should_enable_lofi_mode_called() { | |
|
mmenke
2015/10/12 20:19:29
const
megjablon
2015/10/12 22:06:06
Done.
| |
| 601 return should_enable_lofi_mode_called_; | |
| 602 } | |
| 603 | |
| 604 private: | |
| 605 GURL mainframe_url_; | |
| 606 GURL subresource_url_; | |
| 607 GURL iframe_url_; | |
|
mmenke
2015/10/12 20:19:28
Suggest a blank line between the URLs and the bool
megjablon
2015/10/12 22:06:05
Done.
| |
| 608 bool mainframe_url_seen_; | |
| 609 bool subresource_url_seen_; | |
| 610 bool iframe_url_seen_; | |
| 611 bool use_lofi_; | |
| 612 bool should_enable_lofi_mode_called_; | |
|
mmenke
2015/10/12 20:19:28
You need to initialize all of the bools in the con
mmenke
2015/10/12 20:19:29
DISALLOW_COPY_AND_ASSIGN
megjablon
2015/10/12 22:06:06
Done.
megjablon
2015/10/12 22:06:06
Done.
| |
| 613 }; | |
| 614 | |
| 615 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 616 ShouldEnableLoFiModeOn) { | |
| 617 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 618 | |
| 619 LoFiModeResourceDispatcherHostDelegate delegate( | |
|
mmenke
2015/10/12 20:19:29
Should making a new test fixture, and installing t
megjablon
2015/10/12 22:06:06
Done.
| |
| 620 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
| 621 embedded_test_server()->GetURL("/image.jpg"), | |
| 622 embedded_test_server()->GetURL("/title1.html")); | |
| 623 | |
| 624 content::BrowserThread::PostTask( | |
| 625 content::BrowserThread::IO, | |
| 626 FROM_HERE, | |
| 627 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, | |
| 628 base::Unretained(&delegate))); | |
|
mmenke
2015/10/12 20:19:29
include bind.h
megjablon
2015/10/12 22:06:05
Done.
| |
| 629 | |
| 630 // Navigate with ShouldEnableLoFiMode true. | |
| 631 delegate.Reset(true); | |
| 632 NavigateToURLBlockUntilNavigationsComplete( | |
| 633 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
| 634 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 635 delegate.CheckResourcesRequested(); | |
| 636 } | |
| 637 | |
| 638 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 639 ShouldEnableLoFiModeOff) { | |
| 640 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 641 | |
| 642 LoFiModeResourceDispatcherHostDelegate delegate( | |
| 643 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
| 644 embedded_test_server()->GetURL("/image.jpg"), | |
| 645 embedded_test_server()->GetURL("/title1.html")); | |
| 646 | |
| 647 content::BrowserThread::PostTask( | |
| 648 content::BrowserThread::IO, | |
| 649 FROM_HERE, | |
| 650 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, | |
| 651 base::Unretained(&delegate))); | |
| 652 | |
| 653 // Navigate with ShouldEnableLoFiMode true. | |
| 654 delegate.Reset(false); | |
|
mmenke
2015/10/12 20:19:29
not needed.
megjablon
2015/10/12 22:06:06
Done.
| |
| 655 NavigateToURLBlockUntilNavigationsComplete( | |
| 656 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
| 657 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 658 delegate.CheckResourcesRequested(); | |
|
mmenke
2015/10/12 20:19:28
Not really safe to call this on the UI thread - sh
megjablon
2015/10/12 22:06:06
Done.
| |
| 659 } | |
| 660 | |
| 661 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 662 ShouldEnableLoFiModeReload) { | |
| 663 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 664 | |
| 665 LoFiModeResourceDispatcherHostDelegate delegate( | |
| 666 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
| 667 embedded_test_server()->GetURL("/image.jpg"), | |
| 668 embedded_test_server()->GetURL("/title1.html")); | |
| 669 | |
| 670 content::BrowserThread::PostTask( | |
| 671 content::BrowserThread::IO, | |
| 672 FROM_HERE, | |
| 673 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, | |
| 674 base::Unretained(&delegate))); | |
| 675 | |
| 676 // Navigate with ShouldEnableLoFiMode false. | |
| 677 delegate.Reset(false); | |
|
mmenke
2015/10/12 20:19:29
Not needed.
megjablon
2015/10/12 22:06:05
Done.
| |
| 678 NavigateToURLBlockUntilNavigationsComplete( | |
| 679 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
| 680 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 681 delegate.CheckResourcesRequested(); | |
| 682 | |
| 683 // Reload. ShouldEnableLoFiMode should be called. | |
| 684 delegate.Reset(true); | |
|
mmenke
2015/10/12 20:19:29
Again, should be called on the other thread, or us
megjablon
2015/10/12 22:06:05
Done.
| |
| 685 ReloadBlockUntilNavigationsComplete(shell(), 1); | |
| 686 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 687 delegate.CheckResourcesRequested(); | |
| 688 } | |
| 689 | |
| 690 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 691 ShouldEnableLoFiModeNavigateBackThenForward) { | |
| 692 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 693 | |
| 694 LoFiModeResourceDispatcherHostDelegate delegate( | |
| 695 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
| 696 embedded_test_server()->GetURL("/image.jpg"), | |
| 697 embedded_test_server()->GetURL("/title1.html")); | |
| 698 | |
| 699 content::BrowserThread::PostTask( | |
| 700 content::BrowserThread::IO, | |
| 701 FROM_HERE, | |
| 702 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, | |
| 703 base::Unretained(&delegate))); | |
| 704 | |
| 705 // Navigate with ShouldEnableLoFiMode true. | |
| 706 delegate.Reset(true); | |
| 707 NavigateToURLBlockUntilNavigationsComplete( | |
| 708 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
| 709 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 710 delegate.CheckResourcesRequested(); | |
| 711 | |
| 712 // Go to a different page. | |
| 713 delegate.Reset(false); | |
| 714 delegate.SetUrls(embedded_test_server()->GetURL("/title1.html"), GURL(), | |
| 715 GURL()); | |
|
mmenke
2015/10/12 20:19:29
Maybe just navigate to about:blank, so we don't ha
megjablon
2015/10/12 22:06:06
Done.
| |
| 716 NavigateToURLBlockUntilNavigationsComplete( | |
| 717 shell(), embedded_test_server()->GetURL("/title1.html"), 1); | |
| 718 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 719 delegate.CheckResourcesRequested(); | |
| 720 | |
| 721 // Go back. | |
| 722 delegate.Reset(true); | |
| 723 delegate.SetUrls( | |
| 724 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
| 725 embedded_test_server()->GetURL("/image.jpg"), | |
| 726 embedded_test_server()->GetURL("/title1.html")); | |
| 727 WaitForLoadStop(shell()->web_contents()); | |
| 728 TestNavigationObserver tab_observer(shell()->web_contents(), 1); | |
| 729 shell()->GoBackOrForward(-1); | |
| 730 tab_observer.Wait(); | |
| 731 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 732 delegate.CheckResourcesRequested(); | |
| 733 | |
| 734 // Go forward. | |
| 735 delegate.Reset(false); | |
| 736 delegate.SetUrls(embedded_test_server()->GetURL("/title1.html"), GURL(), | |
| 737 GURL()); | |
| 738 WaitForLoadStop(shell()->web_contents()); | |
| 739 TestNavigationObserver tab_observer2(shell()->web_contents(), 1); | |
| 740 shell()->GoBackOrForward(1); | |
| 741 tab_observer2.Wait(); | |
| 742 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 743 delegate.CheckResourcesRequested(); | |
|
mmenke
2015/10/12 20:19:29
I don't think this last navigation is needed. If
megjablon
2015/10/12 22:06:06
Done.
| |
| 744 } | |
| 745 | |
| 746 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 747 ShouldEnableLoFiModeReloadDisableLoFi) { | |
| 748 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 749 | |
| 750 LoFiModeResourceDispatcherHostDelegate delegate( | |
| 751 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
| 752 embedded_test_server()->GetURL("/image.jpg"), | |
| 753 embedded_test_server()->GetURL("/title1.html")); | |
| 754 | |
| 755 content::BrowserThread::PostTask( | |
| 756 content::BrowserThread::IO, | |
| 757 FROM_HERE, | |
| 758 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, | |
| 759 base::Unretained(&delegate))); | |
| 760 | |
| 761 // Navigate with ShouldEnableLoFiMode true. | |
| 762 delegate.Reset(true); | |
| 763 NavigateToURLBlockUntilNavigationsComplete( | |
| 764 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
| 765 EXPECT_TRUE(delegate.should_enable_lofi_mode_called()); | |
| 766 delegate.CheckResourcesRequested(); | |
| 767 | |
| 768 // Reload with Lo-Fi disabled. | |
| 769 delegate.Reset(false); | |
| 770 WaitForLoadStop(shell()->web_contents()); | |
| 771 TestNavigationObserver tab_observer(shell()->web_contents(), 1); | |
| 772 shell()->web_contents()->GetController().ReloadDisableLoFi(true); | |
| 773 tab_observer.Wait(); | |
| 774 EXPECT_FALSE(delegate.should_enable_lofi_mode_called()); | |
| 775 delegate.CheckResourcesRequested(); | |
| 776 } | |
| 777 | |
| 529 } // namespace content | 778 } // namespace content |
| OLD | NEW |