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 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
519 | 525 |
520 NavigateToURLBlockUntilNavigationsComplete( | 526 NavigateToURLBlockUntilNavigationsComplete( |
521 shell(), | 527 shell(), |
522 embedded_test_server()->GetURL("/client_redirect.html"), | 528 embedded_test_server()->GetURL("/client_redirect.html"), |
523 2); | 529 2); |
524 | 530 |
525 EXPECT_TRUE( | 531 EXPECT_TRUE( |
526 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); | 532 delegate.page_transition() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); |
527 } | 533 } |
528 | 534 |
535 namespace { | |
536 | |
537 // Checks whether the given urls are requested, set the Lo-Fi state, and have | |
bengr
2015/10/20 16:30:51
The comment is a little awkward. How about:
Check
megjablon
2015/10/20 17:46:35
Done.
| |
538 // the proper value for IsUsingLoFi. | |
539 class LoFiModeResourceDispatcherHostDelegate | |
540 : public ResourceDispatcherHostDelegate { | |
541 public: | |
542 LoFiModeResourceDispatcherHostDelegate(const GURL& main_frame_url, | |
543 const GURL& subresource_url, | |
544 const GURL& iframe_url) | |
545 : main_frame_url_(main_frame_url), | |
546 subresource_url_(subresource_url), | |
547 iframe_url_(iframe_url), | |
548 main_frame_url_seen_(false), | |
549 subresource_url_seen_(false), | |
550 iframe_url_seen_(false), | |
551 use_lofi_(false), | |
552 should_enable_lofi_mode_called_(false) {} | |
553 | |
554 ~LoFiModeResourceDispatcherHostDelegate() override {} | |
555 | |
556 // ResourceDispatcherHostDelegate implementation: | |
557 void RequestBeginning(net::URLRequest* request, | |
558 ResourceContext* resource_context, | |
559 AppCacheService* appcache_service, | |
560 ResourceType resource_type, | |
561 ScopedVector<ResourceThrottle>* throttles) override { | |
562 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
563 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | |
564 if (request->url() != main_frame_url_ && request->url() != subresource_url_ | |
565 && request->url() != iframe_url_) | |
566 return; | |
567 if (request->url() == main_frame_url_) { | |
568 EXPECT_FALSE(main_frame_url_seen_); | |
569 main_frame_url_seen_ = true; | |
570 } else if (request->url() == subresource_url_) { | |
571 EXPECT_TRUE(main_frame_url_seen_); | |
572 EXPECT_FALSE(subresource_url_seen_); | |
573 subresource_url_seen_ = true; | |
574 } else if (request->url() == iframe_url_) { | |
575 EXPECT_TRUE(main_frame_url_seen_); | |
576 EXPECT_FALSE(iframe_url_seen_); | |
577 iframe_url_seen_ = true; | |
578 } | |
579 EXPECT_EQ(use_lofi_, info->IsUsingLoFi()); | |
580 } | |
581 | |
582 void SetDelegate() { | |
583 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
584 ResourceDispatcherHost::Get()->SetDelegate(this); | |
585 } | |
586 | |
587 bool ShouldEnableLoFiMode( | |
588 net::URLRequest* request, | |
589 content::ResourceContext* resource_context) override { | |
590 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
591 EXPECT_FALSE(should_enable_lofi_mode_called_); | |
592 should_enable_lofi_mode_called_ = true; | |
593 EXPECT_EQ(main_frame_url_, request->url()); | |
594 return use_lofi_; | |
595 } | |
596 | |
597 void Reset(bool use_lofi) { | |
598 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
599 main_frame_url_seen_ = false; | |
600 subresource_url_seen_ = false; | |
601 iframe_url_seen_ = false; | |
602 use_lofi_ = use_lofi; | |
603 should_enable_lofi_mode_called_ = false; | |
604 } | |
605 | |
606 void CheckResourcesRequested(bool should_enable_lofi_mode_called) { | |
607 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
608 EXPECT_EQ(should_enable_lofi_mode_called, should_enable_lofi_mode_called_); | |
609 EXPECT_TRUE(main_frame_url_seen_); | |
610 EXPECT_TRUE(subresource_url_seen_); | |
611 EXPECT_TRUE(iframe_url_seen_); | |
612 } | |
613 | |
614 private: | |
615 const GURL main_frame_url_; | |
616 const GURL subresource_url_; | |
617 const GURL iframe_url_; | |
618 | |
619 bool main_frame_url_seen_; | |
620 bool subresource_url_seen_; | |
621 bool iframe_url_seen_; | |
622 bool use_lofi_; | |
623 bool should_enable_lofi_mode_called_; | |
624 | |
625 DISALLOW_COPY_AND_ASSIGN(LoFiModeResourceDispatcherHostDelegate); | |
626 }; | |
627 | |
628 } // namespace | |
629 | |
630 class LoFiResourceDispatcherHostBrowserTest : public ContentBrowserTest { | |
631 public: | |
632 ~LoFiResourceDispatcherHostBrowserTest() override {} | |
633 | |
634 protected: | |
635 void SetUpOnMainThread() override { | |
636 ContentBrowserTest::SetUpOnMainThread(); | |
637 | |
638 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
639 | |
640 delegate_.reset(new LoFiModeResourceDispatcherHostDelegate( | |
641 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
642 embedded_test_server()->GetURL("/image.jpg"), | |
643 embedded_test_server()->GetURL("/title1.html"))); | |
644 | |
645 content::BrowserThread::PostTask( | |
646 content::BrowserThread::IO, | |
647 FROM_HERE, | |
648 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, | |
649 base::Unretained(delegate_.get()))); | |
650 } | |
651 | |
652 void Reset(bool use_lofi) { | |
653 content::BrowserThread::PostTask( | |
654 content::BrowserThread::IO, FROM_HERE, | |
655 base::Bind(&LoFiModeResourceDispatcherHostDelegate::Reset, | |
656 base::Unretained(delegate_.get()), use_lofi)); | |
657 } | |
658 | |
659 void CheckResourcesRequested( | |
660 bool should_enable_lofi_mode_called) { | |
661 content::BrowserThread::PostTask( | |
662 content::BrowserThread::IO, FROM_HERE, | |
663 base::Bind( | |
664 &LoFiModeResourceDispatcherHostDelegate::CheckResourcesRequested, | |
665 base::Unretained(delegate_.get()), should_enable_lofi_mode_called)); | |
666 } | |
667 | |
668 private: | |
669 scoped_ptr<LoFiModeResourceDispatcherHostDelegate> delegate_; | |
670 }; | |
671 | |
672 // Test that navigating with ShouldEnableLoFiMode returning true. | |
bengr
2015/10/20 16:30:51
navigating does what?
megjablon
2015/10/20 17:46:35
Done.
| |
673 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
674 ShouldEnableLoFiModeOn) { | |
675 // Navigate with ShouldEnableLoFiMode returning true. | |
676 Reset(true); | |
677 EXPECT_TRUE(NavigateToURLBlockUntilNavigationsComplete( | |
678 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1)); | |
679 CheckResourcesRequested(true); | |
680 } | |
681 | |
682 // Test that navigating with ShouldEnableLoFiMode returning false. | |
bengr
2015/10/20 16:30:51
that navigating does what?
megjablon
2015/10/20 17:46:35
Done.
| |
683 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
684 ShouldEnableLoFiModeOff) { | |
685 // Navigate with ShouldEnableLoFiMode returning false. | |
686 EXPECT_TRUE(NavigateToURLBlockUntilNavigationsComplete( | |
687 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1)); | |
688 CheckResourcesRequested(true); | |
689 } | |
690 | |
691 // Test that reloading calls ShouldEnableLoFiMode again and changes the Lo-Fi | |
692 // state. | |
693 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
694 ShouldEnableLoFiModeReload) { | |
695 // Navigate with ShouldEnableLoFiMode returning false. | |
696 EXPECT_TRUE(NavigateToURLBlockUntilNavigationsComplete( | |
697 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1)); | |
698 CheckResourcesRequested(true); | |
699 | |
700 // Reload. ShouldEnableLoFiMode should be called. | |
701 Reset(true); | |
702 ReloadBlockUntilNavigationsComplete(shell(), 1); | |
703 CheckResourcesRequested(true); | |
704 } | |
705 | |
706 // Test that navigating backwards calls ShouldEnableLoFiMode again and changes | |
707 // the Lo-Fi state. | |
708 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
709 ShouldEnableLoFiModeNavigateBackThenForward) { | |
710 // Navigate with ShouldEnableLoFiMode returning false. | |
711 EXPECT_TRUE(NavigateToURLBlockUntilNavigationsComplete( | |
712 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1)); | |
713 CheckResourcesRequested(true); | |
714 | |
715 // Go to a different page. | |
716 EXPECT_TRUE(NavigateToURLBlockUntilNavigationsComplete( | |
717 shell(), GURL("about:blank"), 1)); | |
718 | |
719 // Go back with ShouldEnableLoFiMode returning true. | |
720 Reset(true); | |
721 TestNavigationObserver tab_observer(shell()->web_contents(), 1); | |
722 shell()->GoBackOrForward(-1); | |
723 tab_observer.Wait(); | |
724 CheckResourcesRequested(true); | |
725 } | |
726 | |
727 // Test that reloading with Lo-Fi disabled doesn't call ShouldEnableLoFiMode and | |
728 // already has LOFI_OFF. | |
729 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
730 ShouldEnableLoFiModeReloadDisableLoFi) { | |
731 // Navigate with ShouldEnableLoFiMode returning true. | |
732 Reset(true); | |
733 EXPECT_TRUE(NavigateToURLBlockUntilNavigationsComplete( | |
734 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1)); | |
735 CheckResourcesRequested(true); | |
736 | |
737 // Reload with Lo-Fi disabled. | |
738 Reset(false); | |
739 TestNavigationObserver tab_observer(shell()->web_contents(), 1); | |
740 shell()->web_contents()->GetController().ReloadDisableLoFi(true); | |
741 tab_observer.Wait(); | |
742 CheckResourcesRequested(false); | |
743 } | |
744 | |
529 } // namespace content | 745 } // namespace content |
OLD | NEW |