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 | |
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 const GURL& ignore_url) | |
546 : main_frame_url_(main_frame_url), | |
547 subresource_url_(subresource_url), | |
548 iframe_url_(iframe_url), | |
549 ignore_url_(ignore_url), | |
550 main_frame_url_seen_(false), | |
551 subresource_url_seen_(false), | |
552 iframe_url_seen_(false), | |
553 use_lofi_(false), | |
554 should_enable_lofi_mode_called_(false) {} | |
mmenke
2015/10/13 15:42:49
child classes should have explicit override destru
megjablon
2015/10/14 18:09:46
Done.
| |
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 { | |
mmenke
2015/10/13 15:42:49
Should have DCHECK_CURRENTLY_ON(BrowserThread::IO)
megjablon
2015/10/14 18:09:46
Done.
| |
562 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | |
563 if (request->url() == ignore_url_) | |
564 return; | |
mmenke
2015/10/13 15:42:49
Should probably just ignore everything that doesn'
megjablon
2015/10/14 18:09:46
Done.
| |
565 if (request->url() == main_frame_url_) { | |
566 EXPECT_FALSE(main_frame_url_seen_); | |
567 main_frame_url_seen_ = true; | |
568 } else if (request->url() == subresource_url_) { | |
569 EXPECT_TRUE(main_frame_url_seen_); | |
570 EXPECT_FALSE(subresource_url_seen_); | |
571 subresource_url_seen_ = true; | |
572 } else if (request->url() == iframe_url_) { | |
573 EXPECT_TRUE(main_frame_url_seen_); | |
574 EXPECT_FALSE(iframe_url_seen_); | |
575 iframe_url_seen_ = true; | |
576 } | |
577 EXPECT_EQ(use_lofi_, info->IsUsingLoFi()); | |
578 } | |
579 | |
580 void SetDelegate() { | |
581 ResourceDispatcherHost::Get()->SetDelegate(this); | |
582 } | |
583 | |
584 bool ShouldEnableLoFiMode( | |
585 net::URLRequest* request, | |
586 content::ResourceContext* resource_context) override { | |
587 EXPECT_FALSE(should_enable_lofi_mode_called_); | |
588 should_enable_lofi_mode_called_ = true; | |
589 EXPECT_EQ(main_frame_url_, request->url()); | |
590 return use_lofi_; | |
591 } | |
592 | |
593 void Reset(bool use_lofi) { | |
594 main_frame_url_seen_ = false; | |
595 subresource_url_seen_ = false; | |
596 iframe_url_seen_ = false; | |
597 use_lofi_ = use_lofi; | |
598 should_enable_lofi_mode_called_ = false; | |
599 } | |
600 | |
601 void CheckResourcesRequested(bool should_enable_lofi_mode_called) { | |
602 EXPECT_EQ(should_enable_lofi_mode_called, should_enable_lofi_mode_called_); | |
603 EXPECT_TRUE(main_frame_url_seen_); | |
604 EXPECT_TRUE(subresource_url_seen_); | |
605 EXPECT_TRUE(iframe_url_seen_); | |
606 } | |
607 | |
608 private: | |
609 const GURL main_frame_url_; | |
610 const GURL subresource_url_; | |
611 const GURL iframe_url_; | |
612 const GURL ignore_url_; | |
613 | |
614 bool main_frame_url_seen_; | |
615 bool subresource_url_seen_; | |
616 bool iframe_url_seen_; | |
617 bool use_lofi_; | |
618 bool should_enable_lofi_mode_called_; | |
619 | |
620 DISALLOW_COPY_AND_ASSIGN(LoFiModeResourceDispatcherHostDelegate); | |
621 }; | |
622 | |
623 } // namespace | |
624 | |
625 class LoFiResourceDispatcherHostBrowserTest : public ContentBrowserTest { | |
mmenke
2015/10/13 15:42:49
Should have a destructor with override
megjablon
2015/10/14 18:09:46
Done.
| |
626 protected: | |
627 void SetUpOnMainThread() override { | |
628 ContentBrowserTest::SetUpOnMainThread(); | |
629 | |
630 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
631 | |
632 delegate_.reset(new LoFiModeResourceDispatcherHostDelegate( | |
633 embedded_test_server()->GetURL("/page_with_iframe.html"), | |
634 embedded_test_server()->GetURL("/image.jpg"), | |
635 embedded_test_server()->GetURL("/title1.html"), | |
636 GURL("about:blank"))); | |
637 | |
638 content::BrowserThread::PostTask( | |
639 content::BrowserThread::IO, | |
640 FROM_HERE, | |
641 base::Bind(&LoFiModeResourceDispatcherHostDelegate::SetDelegate, | |
642 base::Unretained(delegate_.get()))); | |
643 } | |
644 | |
645 void Reset(bool use_lofi) { | |
646 base::RunLoop run_loop; | |
647 content::BrowserThread::PostTaskAndReply( | |
648 content::BrowserThread::IO, FROM_HERE, | |
649 base::Bind(&LoFiModeResourceDispatcherHostDelegate::Reset, | |
650 base::Unretained(delegate_.get()), use_lofi), | |
651 base::Bind(&base::RunLoop::Quit, base::Unretained(&run_loop))); | |
652 run_loop.Run(); | |
mmenke
2015/10/13 15:42:49
The PostTask SetUpOnMainThread() doesn't need to w
megjablon
2015/10/14 18:09:46
Done.
| |
653 } | |
654 | |
655 void CheckResourcesRequested( | |
656 bool should_enable_lofi_mode_called) { | |
657 base::RunLoop run_loop; | |
658 content::BrowserThread::PostTaskAndReply( | |
659 content::BrowserThread::IO, FROM_HERE, | |
660 base::Bind( | |
661 &LoFiModeResourceDispatcherHostDelegate::CheckResourcesRequested, | |
662 base::Unretained(delegate_.get()), should_enable_lofi_mode_called), | |
663 base::Bind(&base::RunLoop::Quit, base::Unretained(&run_loop))); | |
664 run_loop.Run(); | |
665 } | |
666 | |
667 private: | |
668 scoped_ptr<LoFiModeResourceDispatcherHostDelegate> delegate_; | |
669 }; | |
670 | |
671 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
672 ShouldEnableLoFiModeOn) { | |
673 // Navigate with ShouldEnableLoFiMode true. | |
mmenke
2015/10/13 15:42:49
nit: Maybe "Navigate with ShouldEnableLoFiMode re
megjablon
2015/10/14 18:09:46
Done.
| |
674 Reset(true); | |
675 NavigateToURLBlockUntilNavigationsComplete( | |
676 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
677 CheckResourcesRequested(true); | |
678 } | |
679 | |
680 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
681 ShouldEnableLoFiModeOff) { | |
682 // Navigate with ShouldEnableLoFiMode false. | |
683 NavigateToURLBlockUntilNavigationsComplete( | |
684 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
685 CheckResourcesRequested(true); | |
686 } | |
687 | |
688 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
689 ShouldEnableLoFiModeReload) { | |
690 // Navigate with ShouldEnableLoFiMode false. | |
691 NavigateToURLBlockUntilNavigationsComplete( | |
692 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
693 CheckResourcesRequested(true); | |
694 | |
695 // Reload. ShouldEnableLoFiMode should be called. | |
696 Reset(true); | |
697 ReloadBlockUntilNavigationsComplete(shell(), 1); | |
698 CheckResourcesRequested(true); | |
699 } | |
700 | |
701 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
702 ShouldEnableLoFiModeNavigateBackThenForward) { | |
703 // Navigate with ShouldEnableLoFiMode true. | |
704 Reset(true); | |
705 NavigateToURLBlockUntilNavigationsComplete( | |
706 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
707 CheckResourcesRequested(true); | |
708 | |
709 // Go to a different page. | |
710 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); | |
711 | |
712 // Go back. | |
713 Reset(false); | |
714 WaitForLoadStop(shell()->web_contents()); | |
715 TestNavigationObserver tab_observer(shell()->web_contents(), 1); | |
716 shell()->GoBackOrForward(-1); | |
717 tab_observer.Wait(); | |
718 CheckResourcesRequested(true); | |
719 } | |
720 | |
721 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest, | |
722 ShouldEnableLoFiModeReloadDisableLoFi) { | |
723 // Navigate with ShouldEnableLoFiMode true. | |
724 Reset(true); | |
mmenke
2015/10/13 15:42:49
May want to go from true to false here, just to do
megjablon
2015/10/14 18:09:46
Done.
| |
725 NavigateToURLBlockUntilNavigationsComplete( | |
726 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1); | |
727 CheckResourcesRequested(true); | |
728 | |
729 // Reload with Lo-Fi disabled. | |
730 Reset(false); | |
731 WaitForLoadStop(shell()->web_contents()); | |
732 TestNavigationObserver tab_observer(shell()->web_contents(), 1); | |
733 shell()->web_contents()->GetController().ReloadDisableLoFi(true); | |
734 tab_observer.Wait(); | |
735 CheckResourcesRequested(false); | |
736 } | |
737 | |
529 } // namespace content | 738 } // namespace content |
OLD | NEW |