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.h" |
| 6 #include "base/bind_helpers.h" |
5 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/run_loop.h" |
6 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
7 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
9 #include "content/browser/download/download_manager_impl.h" | 12 #include "content/browser/download/download_manager_impl.h" |
10 #include "content/browser/web_contents/web_contents_impl.h" | 13 #include "content/browser/web_contents/web_contents_impl.h" |
11 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/browser/resource_dispatcher_host.h" | 16 #include "content/public/browser/resource_dispatcher_host.h" |
14 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 17 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
15 #include "content/public/browser/resource_request_info.h" | 18 #include "content/public/browser/resource_request_info.h" |
16 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
17 #include "content/public/common/url_constants.h" | 20 #include "content/public/common/url_constants.h" |
18 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
19 #include "content/public/test/content_browser_test.h" | 22 #include "content/public/test/content_browser_test.h" |
20 #include "content/public/test/content_browser_test_utils.h" | 23 #include "content/public/test/content_browser_test_utils.h" |
| 24 #include "content/public/test/test_navigation_observer.h" |
21 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
22 #include "content/shell/browser/shell.h" | 26 #include "content/shell/browser/shell.h" |
23 #include "content/shell/browser/shell_content_browser_client.h" | 27 #include "content/shell/browser/shell_content_browser_client.h" |
24 #include "content/shell/browser/shell_network_delegate.h" | 28 #include "content/shell/browser/shell_network_delegate.h" |
25 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
26 #include "net/test/embedded_test_server/embedded_test_server.h" | 30 #include "net/test/embedded_test_server/embedded_test_server.h" |
27 #include "net/test/embedded_test_server/http_request.h" | 31 #include "net/test/embedded_test_server/http_request.h" |
28 #include "net/test/embedded_test_server/http_response.h" | 32 #include "net/test/embedded_test_server/http_response.h" |
29 #include "net/test/url_request/url_request_failed_job.h" | 33 #include "net/test/url_request/url_request_failed_job.h" |
30 #include "net/test/url_request/url_request_mock_http_job.h" | 34 #include "net/test/url_request/url_request_mock_http_job.h" |
| 35 #include "net/url_request/url_request.h" |
| 36 #include "url/gurl.h" |
31 | 37 |
32 using base::ASCIIToUTF16; | 38 using base::ASCIIToUTF16; |
33 | 39 |
34 namespace content { | 40 namespace content { |
35 | 41 |
36 class ResourceDispatcherHostBrowserTest : public ContentBrowserTest, | 42 class ResourceDispatcherHostBrowserTest : public ContentBrowserTest, |
37 public DownloadManager::Observer { | 43 public DownloadManager::Observer { |
38 public: | 44 public: |
39 ResourceDispatcherHostBrowserTest() : got_downloads_(false) {} | 45 ResourceDispatcherHostBrowserTest() : got_downloads_(false) {} |
40 | 46 |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 | 532 |
527 NavigateToURLBlockUntilNavigationsComplete( | 533 NavigateToURLBlockUntilNavigationsComplete( |
528 shell(), | 534 shell(), |
529 embedded_test_server()->GetURL("/client_redirect.html"), | 535 embedded_test_server()->GetURL("/client_redirect.html"), |
530 2); | 536 2); |
531 | 537 |
532 EXPECT_TRUE( | 538 EXPECT_TRUE( |
533 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); | 539 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); |
534 } | 540 } |
535 | 541 |
| 542 namespace { |
| 543 |
| 544 // Checks whether the given urls are requested, and that IsUsingLofi() returns |
| 545 // the appropriate value when the Lo-Fi state is set. |
| 546 class LoFiModeResourceDispatcherHostDelegate |
| 547 : public ResourceDispatcherHostDelegate { |
| 548 public: |
| 549 LoFiModeResourceDispatcherHostDelegate(const GURL& main_frame_url, |
| 550 const GURL& subresource_url, |
| 551 const GURL& iframe_url) |
| 552 : main_frame_url_(main_frame_url), |
| 553 subresource_url_(subresource_url), |
| 554 iframe_url_(iframe_url), |
| 555 main_frame_url_seen_(false), |
| 556 subresource_url_seen_(false), |
| 557 iframe_url_seen_(false), |
| 558 use_lofi_(false), |
| 559 should_enable_lofi_mode_called_(false) {} |
| 560 |
| 561 ~LoFiModeResourceDispatcherHostDelegate() override {} |
| 562 |
| 563 // ResourceDispatcherHostDelegate implementation: |
| 564 void RequestBeginning(net::URLRequest* request, |
| 565 ResourceContext* resource_context, |
| 566 AppCacheService* appcache_service, |
| 567 ResourceType resource_type, |
| 568 ScopedVector<ResourceThrottle>* throttles) override { |
| 569 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 570 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 571 if (request->url() != main_frame_url_ && request->url() != subresource_url_ |
| 572 && request->url() != iframe_url_) |
| 573 return; |
| 574 if (request->url() == main_frame_url_) { |
| 575 EXPECT_FALSE(main_frame_url_seen_); |
| 576 main_frame_url_seen_ = true; |
| 577 } else if (request->url() == subresource_url_) { |
| 578 EXPECT_TRUE(main_frame_url_seen_); |
| 579 EXPECT_FALSE(subresource_url_seen_); |
| 580 subresource_url_seen_ = true; |
| 581 } else if (request->url() == iframe_url_) { |
| 582 EXPECT_TRUE(main_frame_url_seen_); |
| 583 EXPECT_FALSE(iframe_url_seen_); |
| 584 iframe_url_seen_ = true; |
| 585 } |
| 586 EXPECT_EQ(use_lofi_, info->IsUsingLoFi()); |
| 587 } |
| 588 |
| 589 void SetDelegate() { |
| 590 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 591 ResourceDispatcherHost::Get()->SetDelegate(this); |
| 592 } |
| 593 |
| 594 bool ShouldEnableLoFiMode( |
| 595 const net::URLRequest& request, |
| 596 content::ResourceContext* resource_context) override { |
| 597 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 598 EXPECT_FALSE(should_enable_lofi_mode_called_); |
| 599 should_enable_lofi_mode_called_ = true; |
| 600 EXPECT_EQ(main_frame_url_, request.url()); |
| 601 return use_lofi_; |
| 602 } |
| 603 |
| 604 void Reset(bool use_lofi) { |
| 605 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 606 main_frame_url_seen_ = false; |
| 607 subresource_url_seen_ = false; |
| 608 iframe_url_seen_ = false; |
| 609 use_lofi_ = use_lofi; |
| 610 should_enable_lofi_mode_called_ = false; |
| 611 } |
| 612 |
| 613 void CheckResourcesRequested(bool should_enable_lofi_mode_called) { |
| 614 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 615 EXPECT_EQ(should_enable_lofi_mode_called, should_enable_lofi_mode_called_); |
| 616 EXPECT_TRUE(main_frame_url_seen_); |
| 617 EXPECT_TRUE(subresource_url_seen_); |
| 618 EXPECT_TRUE(iframe_url_seen_); |
| 619 } |
| 620 |
| 621 private: |
| 622 const GURL main_frame_url_; |
| 623 const GURL subresource_url_; |
| 624 const GURL iframe_url_; |
| 625 |
| 626 bool main_frame_url_seen_; |
| 627 bool subresource_url_seen_; |
| 628 bool iframe_url_seen_; |
| 629 bool use_lofi_; |
| 630 bool should_enable_lofi_mode_called_; |
| 631 |
| 632 DISALLOW_COPY_AND_ASSIGN(LoFiModeResourceDispatcherHostDelegate); |
| 633 }; |
| 634 |
| 635 } // namespace |
| 636 |
| 637 class LoFiResourceDispatcherHostBrowserTest : public ContentBrowserTest { |
| 638 public: |
| 639 ~LoFiResourceDispatcherHostBrowserTest() override {} |
| 640 |
| 641 protected: |
| 642 void SetUpOnMainThread() override { |
| 643 ContentBrowserTest::SetUpOnMainThread(); |
| 644 |
| 645 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 646 |
| 647 delegate_.reset(new LoFiModeResourceDispatcherHostDelegate( |
| 648 embedded_test_server()->GetURL("/page_with_iframe.html"), |
| 649 embedded_test_server()->GetURL("/image.jpg"), |
| 650 embedded_test_server()->GetURL("/title1.html"))); |
| 651 |
| 652 content::BrowserThread::PostTask( |
| 653 content::BrowserThread::IO, |
| 654 FROM_HERE, |
| 655 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, |
| 656 base::Unretained(delegate_.get()))); |
| 657 } |
| 658 |
| 659 void Reset(bool use_lofi) { |
| 660 content::BrowserThread::PostTask( |
| 661 content::BrowserThread::IO, FROM_HERE, |
| 662 base::Bind(&LoFiModeResourceDispatcherHostDelegate::Reset, |
| 663 base::Unretained(delegate_.get()), use_lofi)); |
| 664 } |
| 665 |
| 666 void CheckResourcesRequested( |
| 667 bool should_enable_lofi_mode_called) { |
| 668 content::BrowserThread::PostTask( |
| 669 content::BrowserThread::IO, FROM_HERE, |
| 670 base::Bind( |
| 671 &LoFiModeResourceDispatcherHostDelegate::CheckResourcesRequested, |
| 672 base::Unretained(delegate_.get()), should_enable_lofi_mode_called)); |
| 673 } |
| 674 |
| 675 private: |
| 676 scoped_ptr<LoFiModeResourceDispatcherHostDelegate> delegate_; |
| 677 }; |
| 678 |
| 679 // Test that navigating with ShouldEnableLoFiMode returning true fetches the |
| 680 // resources with LOFI_ON. |
| 681 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, |
| 682 ShouldEnableLoFiModeOn) { |
| 683 // Navigate with ShouldEnableLoFiMode returning true. |
| 684 Reset(true); |
| 685 NavigateToURLBlockUntilNavigationsComplete( |
| 686 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); |
| 687 CheckResourcesRequested(true); |
| 688 } |
| 689 |
| 690 // Test that navigating with ShouldEnableLoFiMode returning false fetches the |
| 691 // resources with LOFI_OFF. |
| 692 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, |
| 693 ShouldEnableLoFiModeOff) { |
| 694 // Navigate with ShouldEnableLoFiMode returning false. |
| 695 NavigateToURLBlockUntilNavigationsComplete( |
| 696 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); |
| 697 CheckResourcesRequested(true); |
| 698 } |
| 699 |
| 700 // Test that reloading calls ShouldEnableLoFiMode again and changes the Lo-Fi |
| 701 // state. |
| 702 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, |
| 703 ShouldEnableLoFiModeReload) { |
| 704 // Navigate with ShouldEnableLoFiMode returning false. |
| 705 NavigateToURLBlockUntilNavigationsComplete( |
| 706 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); |
| 707 CheckResourcesRequested(true); |
| 708 |
| 709 // Reload. ShouldEnableLoFiMode should be called. |
| 710 Reset(true); |
| 711 ReloadBlockUntilNavigationsComplete(shell(), 1); |
| 712 CheckResourcesRequested(true); |
| 713 } |
| 714 |
| 715 // Test that navigating backwards calls ShouldEnableLoFiMode again and changes |
| 716 // the Lo-Fi state. |
| 717 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, |
| 718 ShouldEnableLoFiModeNavigateBackThenForward) { |
| 719 // Navigate with ShouldEnableLoFiMode returning false. |
| 720 NavigateToURLBlockUntilNavigationsComplete( |
| 721 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); |
| 722 CheckResourcesRequested(true); |
| 723 |
| 724 // Go to a different page. |
| 725 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 726 |
| 727 // Go back with ShouldEnableLoFiMode returning true. |
| 728 Reset(true); |
| 729 TestNavigationObserver tab_observer(shell()->web_contents(), 1); |
| 730 shell()->GoBackOrForward(-1); |
| 731 tab_observer.Wait(); |
| 732 CheckResourcesRequested(true); |
| 733 } |
| 734 |
| 735 // Test that reloading with Lo-Fi disabled doesn't call ShouldEnableLoFiMode and |
| 736 // already has LOFI_OFF. |
| 737 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, |
| 738 ShouldEnableLoFiModeReloadDisableLoFi) { |
| 739 // Navigate with ShouldEnableLoFiMode returning true. |
| 740 Reset(true); |
| 741 NavigateToURLBlockUntilNavigationsComplete( |
| 742 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); |
| 743 CheckResourcesRequested(true); |
| 744 |
| 745 // Reload with Lo-Fi disabled. |
| 746 Reset(false); |
| 747 TestNavigationObserver tab_observer(shell()->web_contents(), 1); |
| 748 shell()->web_contents()->GetController().ReloadDisableLoFi(true); |
| 749 tab_observer.Wait(); |
| 750 CheckResourcesRequested(false); |
| 751 } |
| 752 |
536 } // namespace content | 753 } // namespace content |
OLD | NEW |