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

Side by Side Diff: content/browser/loader/resource_dispatcher_host_browsertest.cc

Issue 1310743003: Consistently use LoFi for an entire page (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mmenke comments Created 5 years, 2 months 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 "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
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 : 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 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
nasko 2015/10/15 16:54:43 Please add a comment above each test describing wh
megjablon 2015/10/19 22:13:49 Done.
673 ShouldEnableLoFiModeOn) {
674 // Navigate with ShouldEnableLoFiMode returning true.
675 Reset(true);
676 NavigateToURLBlockUntilNavigationsComplete(
nasko 2015/10/15 16:54:43 Better alternative is EXPECT_TRUE(NavigateToURL(..
megjablon 2015/10/19 22:13:49 Done.
677 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
678 CheckResourcesRequested(true);
679 }
680
681 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
682 ShouldEnableLoFiModeOff) {
683 // Navigate with ShouldEnableLoFiMode returning false.
684 NavigateToURLBlockUntilNavigationsComplete(
685 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
686 CheckResourcesRequested(true);
687 }
688
689 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
690 ShouldEnableLoFiModeReload) {
691 // Navigate with ShouldEnableLoFiMode returning false.
692 NavigateToURLBlockUntilNavigationsComplete(
693 shell(), embedded_test_server()->GetURL("/page_with_iframe.html"), 1);
694 CheckResourcesRequested(true);
695
696 // Reload. ShouldEnableLoFiMode should be called.
697 Reset(true);
698 ReloadBlockUntilNavigationsComplete(shell(), 1);
699 CheckResourcesRequested(true);
700 }
701
702 IN_PROC_BROWSER_TEST_F(LoFiResourceDispatcherHostBrowserTest,
703 ShouldEnableLoFiModeNavigateBackThenForward) {
704 // Navigate with ShouldEnableLoFiMode returning false.
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 with ShouldEnableLoFiMode returning true.
713 Reset(true);
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 returning true.
724 Reset(true);
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());
nasko 2015/10/15 16:54:43 What is this waiting for? At this point there shou
megjablon 2015/10/19 22:13:49 Done.
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698