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

Side by Side Diff: chrome/browser/prerender/prerender_browsertest.cc

Issue 2309443002: NoState Prefetch: Refactor prerender_browsertests.cc (Closed)
Patch Set: comments/rebase Created 4 years, 3 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 <stddef.h> 5 #include <stddef.h>
6 #include <deque> 6 #include <deque>
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <unordered_map> 9 #include <unordered_map>
10 #include <utility> 10 #include <utility>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "chrome/browser/net/prediction_options.h" 42 #include "chrome/browser/net/prediction_options.h"
43 #include "chrome/browser/predictors/autocomplete_action_predictor.h" 43 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
44 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h" 44 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
45 #include "chrome/browser/prerender/prerender_contents.h" 45 #include "chrome/browser/prerender/prerender_contents.h"
46 #include "chrome/browser/prerender/prerender_field_trial.h" 46 #include "chrome/browser/prerender/prerender_field_trial.h"
47 #include "chrome/browser/prerender/prerender_handle.h" 47 #include "chrome/browser/prerender/prerender_handle.h"
48 #include "chrome/browser/prerender/prerender_link_manager.h" 48 #include "chrome/browser/prerender/prerender_link_manager.h"
49 #include "chrome/browser/prerender/prerender_link_manager_factory.h" 49 #include "chrome/browser/prerender/prerender_link_manager_factory.h"
50 #include "chrome/browser/prerender/prerender_manager.h" 50 #include "chrome/browser/prerender/prerender_manager.h"
51 #include "chrome/browser/prerender/prerender_manager_factory.h" 51 #include "chrome/browser/prerender/prerender_manager_factory.h"
52 #include "chrome/browser/prerender/prerender_test_utils.h"
52 #include "chrome/browser/profiles/profile.h" 53 #include "chrome/browser/profiles/profile.h"
53 #include "chrome/browser/profiles/profile_io_data.h" 54 #include "chrome/browser/profiles/profile_io_data.h"
54 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate. h"
55 #include "chrome/browser/safe_browsing/local_database_manager.h"
56 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
57 #include "chrome/browser/task_manager/mock_web_contents_task_manager.h" 55 #include "chrome/browser/task_manager/mock_web_contents_task_manager.h"
58 #include "chrome/browser/task_manager/providers/web_contents/web_contents_tags_m anager.h" 56 #include "chrome/browser/task_manager/providers/web_contents/web_contents_tags_m anager.h"
59 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" 57 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
60 #include "chrome/browser/ui/browser.h" 58 #include "chrome/browser/ui/browser.h"
61 #include "chrome/browser/ui/browser_commands.h" 59 #include "chrome/browser/ui/browser_commands.h"
62 #include "chrome/browser/ui/browser_finder.h" 60 #include "chrome/browser/ui/browser_finder.h"
63 #include "chrome/browser/ui/browser_window.h" 61 #include "chrome/browser/ui/browser_window.h"
64 #include "chrome/browser/ui/location_bar/location_bar.h" 62 #include "chrome/browser/ui/location_bar/location_bar.h"
65 #include "chrome/browser/ui/tabs/tab_strip_model.h" 63 #include "chrome/browser/ui/tabs/tab_strip_model.h"
66 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 64 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
67 #include "chrome/common/chrome_paths.h" 65 #include "chrome/common/chrome_paths.h"
68 #include "chrome/common/chrome_switches.h" 66 #include "chrome/common/chrome_switches.h"
69 #include "chrome/common/pref_names.h"
70 #include "chrome/grit/generated_resources.h" 67 #include "chrome/grit/generated_resources.h"
71 #include "chrome/test/base/in_process_browser_test.h"
72 #include "chrome/test/base/ui_test_utils.h" 68 #include "chrome/test/base/ui_test_utils.h"
73 #include "components/content_settings/core/browser/host_content_settings_map.h" 69 #include "components/content_settings/core/browser/host_content_settings_map.h"
74 #include "components/favicon/content/content_favicon_driver.h" 70 #include "components/favicon/content/content_favicon_driver.h"
75 #include "components/favicon/core/favicon_driver_observer.h" 71 #include "components/favicon/core/favicon_driver_observer.h"
76 #include "components/omnibox/browser/omnibox_edit_model.h" 72 #include "components/omnibox/browser/omnibox_edit_model.h"
77 #include "components/omnibox/browser/omnibox_popup_model.h" 73 #include "components/omnibox/browser/omnibox_popup_model.h"
78 #include "components/omnibox/browser/omnibox_view.h" 74 #include "components/omnibox/browser/omnibox_view.h"
79 #include "components/prefs/pref_service.h"
80 #include "components/safe_browsing_db/database_manager.h" 75 #include "components/safe_browsing_db/database_manager.h"
81 #include "components/safe_browsing_db/test_database_manager.h"
82 #include "components/safe_browsing_db/util.h" 76 #include "components/safe_browsing_db/util.h"
83 #include "components/variations/entropy_provider.h" 77 #include "components/variations/entropy_provider.h"
84 #include "components/variations/variations_associated_data.h" 78 #include "components/variations/variations_associated_data.h"
85 #include "content/public/browser/browser_message_filter.h" 79 #include "content/public/browser/browser_message_filter.h"
86 #include "content/public/browser/devtools_agent_host.h" 80 #include "content/public/browser/devtools_agent_host.h"
87 #include "content/public/browser/navigation_controller.h" 81 #include "content/public/browser/navigation_controller.h"
88 #include "content/public/browser/navigation_entry.h" 82 #include "content/public/browser/navigation_entry.h"
89 #include "content/public/browser/notification_service.h" 83 #include "content/public/browser/notification_service.h"
90 #include "content/public/browser/render_frame_host.h" 84 #include "content/public/browser/render_frame_host.h"
91 #include "content/public/browser/render_process_host.h" 85 #include "content/public/browser/render_process_host.h"
92 #include "content/public/browser/render_view_host.h" 86 #include "content/public/browser/render_view_host.h"
93 #include "content/public/browser/site_instance.h" 87 #include "content/public/browser/site_instance.h"
94 #include "content/public/browser/web_contents.h" 88 #include "content/public/browser/web_contents.h"
95 #include "content/public/browser/web_contents_observer.h" 89 #include "content/public/browser/web_contents_observer.h"
96 #include "content/public/common/content_switches.h" 90 #include "content/public/common/content_switches.h"
97 #include "content/public/common/resource_request_body.h" 91 #include "content/public/common/resource_request_body.h"
98 #include "content/public/common/url_constants.h" 92 #include "content/public/common/url_constants.h"
99 #include "content/public/test/browser_test_utils.h" 93 #include "content/public/test/browser_test_utils.h"
100 #include "content/public/test/ppapi_test_utils.h"
101 #include "content/public/test/test_navigation_observer.h" 94 #include "content/public/test/test_navigation_observer.h"
102 #include "content/public/test/test_utils.h" 95 #include "content/public/test/test_utils.h"
103 #include "extensions/common/constants.h" 96 #include "extensions/common/constants.h"
104 #include "extensions/common/extension_urls.h" 97 #include "extensions/common/extension_urls.h"
105 #include "extensions/common/manifest_handlers/mime_types_handler.h" 98 #include "extensions/common/manifest_handlers/mime_types_handler.h"
106 #include "extensions/common/switches.h" 99 #include "extensions/common/switches.h"
107 #include "extensions/test/result_catcher.h" 100 #include "extensions/test/result_catcher.h"
108 #include "net/base/escape.h" 101 #include "net/base/escape.h"
109 #include "net/cert/x509_certificate.h" 102 #include "net/cert/x509_certificate.h"
110 #include "net/dns/mock_host_resolver.h" 103 #include "net/dns/mock_host_resolver.h"
111 #include "net/ssl/client_cert_store.h" 104 #include "net/ssl/client_cert_store.h"
112 #include "net/ssl/ssl_cert_request_info.h" 105 #include "net/ssl/ssl_cert_request_info.h"
113 #include "net/ssl/ssl_server_config.h" 106 #include "net/ssl/ssl_server_config.h"
114 #include "net/test/cert_test_util.h" 107 #include "net/test/cert_test_util.h"
115 #include "net/test/embedded_test_server/embedded_test_server.h" 108 #include "net/test/embedded_test_server/embedded_test_server.h"
116 #include "net/test/embedded_test_server/request_handler_util.h" 109 #include "net/test/embedded_test_server/request_handler_util.h"
117 #include "net/test/test_data_directory.h" 110 #include "net/test/test_data_directory.h"
118 #include "net/test/url_request/url_request_mock_http_job.h"
119 #include "net/url_request/url_request_context.h" 111 #include "net/url_request/url_request_context.h"
120 #include "net/url_request/url_request_context_getter.h" 112 #include "net/url_request/url_request_context_getter.h"
121 #include "net/url_request/url_request_filter.h" 113 #include "net/url_request/url_request_filter.h"
122 #include "net/url_request/url_request_interceptor.h"
123 #include "net/url_request/url_request_job.h" 114 #include "net/url_request/url_request_job.h"
124 #include "ppapi/shared_impl/ppapi_switches.h"
125 #include "testing/gmock/include/gmock/gmock.h" 115 #include "testing/gmock/include/gmock/gmock.h"
126 #include "testing/gtest/include/gtest/gtest.h" 116 #include "testing/gtest/include/gtest/gtest.h"
127 #include "ui/base/l10n/l10n_util.h" 117 #include "ui/base/l10n/l10n_util.h"
128 #include "url/gurl.h" 118 #include "url/gurl.h"
129 119
130 using chrome_browser_net::NetworkPredictionOptions; 120 using chrome_browser_net::NetworkPredictionOptions;
131 using content::BrowserThread; 121 using content::BrowserThread;
132 using content::DevToolsAgentHost; 122 using content::DevToolsAgentHost;
133 using content::NavigationController; 123 using content::NavigationController;
134 using content::OpenURLParams; 124 using content::OpenURLParams;
135 using content::Referrer; 125 using content::Referrer;
136 using content::RenderFrameHost; 126 using content::RenderFrameHost;
137 using content::RenderViewHost; 127 using content::RenderViewHost;
138 using content::RenderWidgetHost; 128 using content::RenderWidgetHost;
139 using content::TestNavigationObserver; 129 using content::TestNavigationObserver;
140 using content::WebContents; 130 using content::WebContents;
141 using content::WebContentsObserver; 131 using content::WebContentsObserver;
142 using net::NetworkChangeNotifier; 132 using net::NetworkChangeNotifier;
143 using safe_browsing::LocalSafeBrowsingDatabaseManager; 133 using prerender::test_utils::RequestCounter;
144 using safe_browsing::SafeBrowsingService; 134 using prerender::test_utils::CreateCountingInterceptorOnIO;
145 using safe_browsing::SBThreatType; 135 using prerender::test_utils::CreateMockInterceptorOnIO;
136 using prerender::test_utils::TestPrerender;
137 using prerender::test_utils::TestPrerenderContents;
146 using task_manager::browsertest_util::WaitForTaskManagerRows; 138 using task_manager::browsertest_util::WaitForTaskManagerRows;
147 139
148 // Prerender tests work as follows: 140 // Prerender tests work as follows:
149 // 141 //
150 // A page with a prefetch link to the test page is loaded. Once prerendered, 142 // A page with a prefetch link to the test page is loaded. Once prerendered,
151 // its Javascript function DidPrerenderPass() is called, which returns true if 143 // its Javascript function DidPrerenderPass() is called, which returns true if
152 // the page behaves as expected when prerendered. 144 // the page behaves as expected when prerendered.
153 // 145 //
154 // The prerendered page is then displayed on a tab. The Javascript function 146 // The prerendered page is then displayed on a tab. The Javascript function
155 // DidDisplayPass() is called, and returns true if the page behaved as it 147 // DidDisplayPass() is called, and returns true if the page behaved as it
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 new_tab)); 458 new_tab));
467 swap_observer_->set_did_start_loading(); 459 swap_observer_->set_did_start_loading();
468 return true; 460 return true;
469 } 461 }
470 462
471 private: 463 private:
472 content::WindowedNotificationObserver new_tab_observer_; 464 content::WindowedNotificationObserver new_tab_observer_;
473 std::unique_ptr<NavigationOrSwapObserver> swap_observer_; 465 std::unique_ptr<NavigationOrSwapObserver> swap_observer_;
474 }; 466 };
475 467
476 // PrerenderContents that stops the UI message loop on DidStopLoading().
477 class TestPrerenderContents : public PrerenderContents {
478 public:
479 TestPrerenderContents(PrerenderManager* prerender_manager,
480 Profile* profile,
481 const GURL& url,
482 const content::Referrer& referrer,
483 Origin origin,
484 FinalStatus expected_final_status)
485 : PrerenderContents(prerender_manager, profile, url, referrer, origin),
486 expected_final_status_(expected_final_status),
487 new_render_view_host_(nullptr),
488 was_hidden_(false),
489 was_shown_(false),
490 should_be_shown_(expected_final_status == FINAL_STATUS_USED),
491 skip_final_checks_(false) {}
492
493 ~TestPrerenderContents() override {
494 if (skip_final_checks_)
495 return;
496
497 EXPECT_EQ(expected_final_status_, final_status())
498 << " when testing URL " << prerender_url().path()
499 << " (Expected: " << NameFromFinalStatus(expected_final_status_)
500 << ", Actual: " << NameFromFinalStatus(final_status()) << ")";
501
502 // Prerendering RenderViewHosts should be hidden before the first
503 // navigation, so this should be happen for every PrerenderContents for
504 // which a RenderViewHost is created, regardless of whether or not it's
505 // used.
506 if (new_render_view_host_)
507 EXPECT_TRUE(was_hidden_);
508
509 // A used PrerenderContents will only be destroyed when we swap out
510 // WebContents, at the end of a navigation caused by a call to
511 // NavigateToURLImpl().
512 if (final_status() == FINAL_STATUS_USED)
513 EXPECT_TRUE(new_render_view_host_);
514
515 EXPECT_EQ(should_be_shown_, was_shown_);
516 }
517
518 void RenderProcessGone(base::TerminationStatus status) override {
519 // On quit, it's possible to end up here when render processes are closed
520 // before the PrerenderManager is destroyed. As a result, it's possible to
521 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
522 // on quit.
523 //
524 // It's also possible for this to be called after we've been notified of
525 // app termination, but before we've been deleted, which is why the second
526 // check is needed.
527 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING &&
528 final_status() != expected_final_status_) {
529 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED;
530 }
531
532 PrerenderContents::RenderProcessGone(status);
533 }
534
535 bool CheckURL(const GURL& url) override {
536 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
537 // the PrerenderRendererCrash test.
538 if (url.spec() != content::kChromeUICrashURL)
539 return PrerenderContents::CheckURL(url);
540 return true;
541 }
542
543 // For tests that open the prerender in a new background tab, the RenderView
544 // will not have been made visible when the PrerenderContents is destroyed
545 // even though it is used.
546 void set_should_be_shown(bool value) { should_be_shown_ = value; }
547
548 // For tests which do not know whether the prerender will be used.
549 void set_skip_final_checks(bool value) { skip_final_checks_ = value; }
550
551 FinalStatus expected_final_status() const { return expected_final_status_; }
552
553 private:
554 void OnRenderViewHostCreated(RenderViewHost* new_render_view_host) override {
555 // Used to make sure the RenderViewHost is hidden and, if used,
556 // subsequently shown.
557 notification_registrar().Add(
558 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
559 content::Source<RenderWidgetHost>(new_render_view_host->GetWidget()));
560
561 new_render_view_host_ = new_render_view_host;
562
563 PrerenderContents::OnRenderViewHostCreated(new_render_view_host);
564 }
565
566 void Observe(int type,
567 const content::NotificationSource& source,
568 const content::NotificationDetails& details) override {
569 if (type ==
570 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED) {
571 EXPECT_EQ(new_render_view_host_->GetWidget(),
572 content::Source<RenderWidgetHost>(source).ptr());
573 bool is_visible = *content::Details<bool>(details).ptr();
574
575 if (!is_visible) {
576 was_hidden_ = true;
577 } else if (is_visible && was_hidden_) {
578 // Once hidden, a prerendered RenderViewHost should only be shown after
579 // being removed from the PrerenderContents for display.
580 EXPECT_FALSE(GetRenderViewHost());
581 was_shown_ = true;
582 }
583 return;
584 }
585 PrerenderContents::Observe(type, source, details);
586 }
587
588 FinalStatus expected_final_status_;
589
590 // The RenderViewHost created for the prerender, if any.
591 RenderViewHost* new_render_view_host_;
592 // Set to true when the prerendering RenderWidget is hidden.
593 bool was_hidden_;
594 // Set to true when the prerendering RenderWidget is shown, after having been
595 // hidden.
596 bool was_shown_;
597 // Expected final value of was_shown_. Defaults to true for
598 // FINAL_STATUS_USED, and false otherwise.
599 bool should_be_shown_;
600 // If true, |expected_final_status_| and other shutdown checks are skipped.
601 bool skip_final_checks_;
602 };
603
604 // A handle to a TestPrerenderContents whose lifetime is under the caller's
605 // control. A PrerenderContents may be destroyed at any point. This allows
606 // tracking the final status, etc.
607 class TestPrerender : public PrerenderContents::Observer,
608 public base::SupportsWeakPtr<TestPrerender> {
609 public:
610 TestPrerender()
611 : contents_(nullptr), number_of_loads_(0), expected_number_of_loads_(0) {}
612 ~TestPrerender() override {
613 if (contents_)
614 contents_->RemoveObserver(this);
615 }
616
617 TestPrerenderContents* contents() const { return contents_; }
618 int number_of_loads() const { return number_of_loads_; }
619
620 void WaitForCreate() { create_loop_.Run(); }
621 void WaitForStart() { start_loop_.Run(); }
622 void WaitForStop() { stop_loop_.Run(); }
623
624 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR
625 // for the prerender to stop running (just to avoid a timeout if the prerender
626 // dies). Note: this does not assert equality on the number of loads; the
627 // caller must do it instead.
628 void WaitForLoads(int expected_number_of_loads) {
629 DCHECK(!load_waiter_);
630 DCHECK(!expected_number_of_loads_);
631 if (number_of_loads_ < expected_number_of_loads) {
632 load_waiter_.reset(new base::RunLoop);
633 expected_number_of_loads_ = expected_number_of_loads;
634 load_waiter_->Run();
635 load_waiter_.reset();
636 expected_number_of_loads_ = 0;
637 }
638 EXPECT_LE(expected_number_of_loads, number_of_loads_);
639 }
640
641 void OnPrerenderCreated(TestPrerenderContents* contents) {
642 DCHECK(!contents_);
643 contents_ = contents;
644 contents_->AddObserver(this);
645 create_loop_.Quit();
646 }
647
648 // PrerenderContents::Observer implementation:
649 void OnPrerenderStart(PrerenderContents* contents) override {
650 start_loop_.Quit();
651 }
652
653 void OnPrerenderStopLoading(PrerenderContents* contents) override {
654 number_of_loads_++;
655 if (load_waiter_ && number_of_loads_ >= expected_number_of_loads_)
656 load_waiter_->Quit();
657 }
658
659 void OnPrerenderStop(PrerenderContents* contents) override {
660 DCHECK(contents_);
661 contents_ = nullptr;
662 stop_loop_.Quit();
663 // If there is a WaitForLoads call and it has yet to see the expected number
664 // of loads, stop the loop so the test fails instead of timing out.
665 if (load_waiter_)
666 load_waiter_->Quit();
667 }
668
669 private:
670 TestPrerenderContents* contents_;
671 int number_of_loads_;
672
673 int expected_number_of_loads_;
674 std::unique_ptr<base::RunLoop> load_waiter_;
675
676 base::RunLoop create_loop_;
677 base::RunLoop start_loop_;
678 base::RunLoop stop_loop_;
679
680 DISALLOW_COPY_AND_ASSIGN(TestPrerender);
681 };
682
683 // PrerenderManager that uses TestPrerenderContents.
684 class TestPrerenderContentsFactory : public PrerenderContents::Factory {
685 public:
686 TestPrerenderContentsFactory() {}
687
688 ~TestPrerenderContentsFactory() override {
689 EXPECT_TRUE(expected_contents_queue_.empty());
690 }
691
692 std::unique_ptr<TestPrerender> ExpectPrerenderContents(
693 FinalStatus final_status) {
694 std::unique_ptr<TestPrerender> handle(new TestPrerender());
695 expected_contents_queue_.push_back(
696 ExpectedContents(final_status, handle->AsWeakPtr()));
697 return handle;
698 }
699
700 PrerenderContents* CreatePrerenderContents(
701 PrerenderManager* prerender_manager,
702 Profile* profile,
703 const GURL& url,
704 const content::Referrer& referrer,
705 Origin origin) override {
706 ExpectedContents expected;
707 if (!expected_contents_queue_.empty()) {
708 expected = expected_contents_queue_.front();
709 expected_contents_queue_.pop_front();
710 }
711 VLOG(1) << "Creating prerender contents for " << url.path() <<
712 " with expected final status " << expected.final_status;
713 VLOG(1) << expected_contents_queue_.size() << " left in the queue.";
714 TestPrerenderContents* contents =
715 new TestPrerenderContents(prerender_manager,
716 profile, url, referrer, origin,
717 expected.final_status);
718 if (expected.handle)
719 expected.handle->OnPrerenderCreated(contents);
720 return contents;
721 }
722
723 private:
724 struct ExpectedContents {
725 ExpectedContents() : final_status(FINAL_STATUS_MAX) { }
726 ExpectedContents(FinalStatus final_status,
727 const base::WeakPtr<TestPrerender>& handle)
728 : final_status(final_status),
729 handle(handle) {
730 }
731
732 FinalStatus final_status;
733 base::WeakPtr<TestPrerender> handle;
734 };
735
736 std::deque<ExpectedContents> expected_contents_queue_;
737 };
738
739 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
740 // a given URL.
741 class FakeSafeBrowsingDatabaseManager
742 : public safe_browsing::TestSafeBrowsingDatabaseManager {
743 public:
744 FakeSafeBrowsingDatabaseManager() {}
745
746 // Called on the IO thread to check if the given url is safe or not. If we
747 // can synchronously determine that the url is safe, CheckUrl returns true.
748 // Otherwise it returns false, and "client" is called asynchronously with the
749 // result when it is ready.
750 // Returns true, indicating a SAFE result, unless the URL is the fixed URL
751 // specified by the user, and the user-specified result is not SAFE
752 // (in which that result will be communicated back via a call into the
753 // client, and false will be returned).
754 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl.
755 bool CheckBrowseUrl(const GURL& gurl, Client* client) override {
756 if (bad_urls_.find(gurl.spec()) == bad_urls_.end() ||
757 bad_urls_[gurl.spec()] == safe_browsing::SB_THREAT_TYPE_SAFE) {
758 return true;
759 }
760
761 BrowserThread::PostTask(
762 BrowserThread::IO, FROM_HERE,
763 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone,
764 this, gurl, client));
765 return false;
766 }
767
768 void SetThreatTypeForUrl(const GURL& url, SBThreatType threat_type) {
769 bad_urls_[url.spec()] = threat_type;
770 }
771
772 // These are called when checking URLs, so we implement them.
773 bool IsSupported() const override { return true; }
774 bool ChecksAreAlwaysAsync() const override { return false; }
775 bool CanCheckResourceType(
776 content::ResourceType /* resource_type */) const override {
777 return true;
778 }
779
780 bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
781 Client* client) override {
782 return true;
783 }
784
785 private:
786 ~FakeSafeBrowsingDatabaseManager() override {}
787
788 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) {
789 std::vector<SBThreatType> expected_threats;
790 expected_threats.push_back(safe_browsing::SB_THREAT_TYPE_URL_MALWARE);
791 expected_threats.push_back(safe_browsing::SB_THREAT_TYPE_URL_PHISHING);
792 // TODO(nparker): Replace SafeBrowsingCheck w/ a call to
793 // client->OnCheckBrowseUrlResult()
794 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check(
795 std::vector<GURL>(1, gurl),
796 std::vector<safe_browsing::SBFullHash>(),
797 client,
798 safe_browsing::MALWARE,
799 expected_threats);
800 sb_check.url_results[0] = bad_urls_[gurl.spec()];
801 sb_check.OnSafeBrowsingResult();
802 }
803
804 std::unordered_map<std::string, SBThreatType> bad_urls_;
805 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
806 };
807
808 class FakeDevToolsClient : public content::DevToolsAgentHostClient { 468 class FakeDevToolsClient : public content::DevToolsAgentHostClient {
809 public: 469 public:
810 FakeDevToolsClient() {} 470 FakeDevToolsClient() {}
811 ~FakeDevToolsClient() override {} 471 ~FakeDevToolsClient() override {}
812 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, 472 void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
813 const std::string& message) override {} 473 const std::string& message) override {}
814 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {} 474 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {}
815 }; 475 };
816 476
817 class RestorePrerenderMode { 477 class RestorePrerenderMode {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 // the UI thread. 538 // the UI thread.
879 void CreateHangingFirstRequestInterceptorOnIO( 539 void CreateHangingFirstRequestInterceptorOnIO(
880 const GURL& url, const base::FilePath& file, base::Closure callback) { 540 const GURL& url, const base::FilePath& file, base::Closure callback) {
881 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 541 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
882 std::unique_ptr<net::URLRequestInterceptor> never_respond_handler( 542 std::unique_ptr<net::URLRequestInterceptor> never_respond_handler(
883 new HangingFirstRequestInterceptor(file, callback)); 543 new HangingFirstRequestInterceptor(file, callback));
884 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 544 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
885 url, std::move(never_respond_handler)); 545 url, std::move(never_respond_handler));
886 } 546 }
887 547
888 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks.
889 class MockHTTPJob : public net::URLRequestMockHTTPJob {
890 public:
891 MockHTTPJob(net::URLRequest* request,
892 net::NetworkDelegate* delegate,
893 const base::FilePath& file)
894 : net::URLRequestMockHTTPJob(
895 request,
896 delegate,
897 file,
898 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
899 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
900
901 void set_start_callback(const base::Closure& start_callback) {
902 start_callback_ = start_callback;
903 }
904
905 void Start() override {
906 if (!start_callback_.is_null())
907 start_callback_.Run();
908 net::URLRequestMockHTTPJob::Start();
909 }
910
911 private:
912 ~MockHTTPJob() override {}
913
914 base::Closure start_callback_;
915 };
916
917 // Dummy counter class to live on the UI thread for counting requests.
918 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> {
919 public:
920 RequestCounter() : count_(0), expected_count_(-1) {}
921 int count() const { return count_; }
922
923 void RequestStarted() {
924 count_++;
925 if (loop_ && count_ == expected_count_)
926 loop_->Quit();
927 }
928
929 void WaitForCount(int expected_count) {
930 ASSERT_TRUE(!loop_);
931 ASSERT_EQ(-1, expected_count_);
932 if (count_ < expected_count) {
933 expected_count_ = expected_count;
934 loop_.reset(new base::RunLoop);
935 loop_->Run();
936 expected_count_ = -1;
937 loop_.reset();
938 }
939
940 EXPECT_EQ(expected_count, count_);
941 }
942
943 private:
944 int count_;
945 int expected_count_;
946 std::unique_ptr<base::RunLoop> loop_;
947 };
948
949 // Protocol handler which counts the number of requests that start.
950 class CountingInterceptor : public net::URLRequestInterceptor {
951 public:
952 CountingInterceptor(const base::FilePath& file,
953 const base::WeakPtr<RequestCounter>& counter)
954 : file_(file),
955 counter_(counter),
956 weak_factory_(this) {
957 }
958 ~CountingInterceptor() override {}
959
960 net::URLRequestJob* MaybeInterceptRequest(
961 net::URLRequest* request,
962 net::NetworkDelegate* network_delegate) const override {
963 MockHTTPJob* job = new MockHTTPJob(request, network_delegate, file_);
964 job->set_start_callback(base::Bind(&CountingInterceptor::RequestStarted,
965 weak_factory_.GetWeakPtr()));
966 return job;
967 }
968
969 void RequestStarted() {
970 BrowserThread::PostTask(
971 BrowserThread::UI, FROM_HERE,
972 base::Bind(&RequestCounter::RequestStarted, counter_));
973 }
974
975 private:
976 base::FilePath file_;
977 base::WeakPtr<RequestCounter> counter_;
978 mutable base::WeakPtrFactory<CountingInterceptor> weak_factory_;
979 };
980
981 // Makes |url| respond to requests with the contents of |file|, counting the
982 // number that start in |counter|.
983 void CreateCountingInterceptorOnIO(
984 const GURL& url,
985 const base::FilePath& file,
986 const base::WeakPtr<RequestCounter>& counter) {
987 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
988 std::unique_ptr<net::URLRequestInterceptor> request_interceptor(
989 new CountingInterceptor(file, counter));
990 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
991 url, std::move(request_interceptor));
992 }
993
994 // Makes |url| respond to requests with the contents of |file|.
995 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
996 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
997 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
998 url,
999 net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
1000 file, BrowserThread::GetBlockingPool()));
1001 }
1002
1003 // A ContentBrowserClient that cancels all prerenderers on OpenURL. 548 // A ContentBrowserClient that cancels all prerenderers on OpenURL.
1004 class TestContentBrowserClient : public ChromeContentBrowserClient { 549 class TestContentBrowserClient : public ChromeContentBrowserClient {
1005 public: 550 public:
1006 TestContentBrowserClient() {} 551 TestContentBrowserClient() {}
1007 ~TestContentBrowserClient() override {} 552 ~TestContentBrowserClient() override {}
1008 553
1009 // ChromeContentBrowserClient: 554 // ChromeContentBrowserClient:
1010 bool ShouldAllowOpenURL(content::SiteInstance* site_instance, 555 bool ShouldAllowOpenURL(content::SiteInstance* site_instance,
1011 const GURL& url) override { 556 const GURL& url) override {
1012 PrerenderManagerFactory::GetForProfile( 557 PrerenderManagerFactory::GetForProfile(
(...skipping 18 matching lines...) Expand all
1031 content::ResourceContext* resource_context, 576 content::ResourceContext* resource_context,
1032 const GURL& current_url, 577 const GURL& current_url,
1033 const GURL& new_url) override { 578 const GURL& new_url) override {
1034 return true; 579 return true;
1035 } 580 }
1036 581
1037 private: 582 private:
1038 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient); 583 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient);
1039 }; 584 };
1040 585
1041 // An ExternalProtocolHandler that blocks everything and asserts it never is
1042 // called.
1043 class NeverRunsExternalProtocolHandlerDelegate
1044 : public ExternalProtocolHandler::Delegate {
1045 public:
1046 // ExternalProtocolHandler::Delegate implementation.
1047 scoped_refptr<shell_integration::DefaultProtocolClientWorker>
1048 CreateShellWorker(
1049 const shell_integration::DefaultWebClientWorkerCallback& callback,
1050 const std::string& protocol) override {
1051 NOTREACHED();
1052 // This will crash, but it shouldn't get this far with BlockState::BLOCK
1053 // anyway.
1054 return nullptr;
1055 }
1056 ExternalProtocolHandler::BlockState GetBlockState(
1057 const std::string& scheme) override {
1058 // Block everything and fail the test.
1059 ADD_FAILURE();
1060 return ExternalProtocolHandler::BLOCK;
1061 }
1062 void BlockRequest() override {}
1063 void RunExternalProtocolDialog(const GURL& url,
1064 int render_process_host_id,
1065 int routing_id,
1066 ui::PageTransition page_transition,
1067 bool has_user_gesture) override {
1068 NOTREACHED();
1069 }
1070 void LaunchUrlWithoutSecurityCheck(const GURL& url) override { NOTREACHED(); }
1071 void FinishedProcessingCheck() override { NOTREACHED(); }
1072 };
1073
1074 base::FilePath GetTestPath(const std::string& file_name) { 586 base::FilePath GetTestPath(const std::string& file_name) {
1075 return ui_test_utils::GetTestFilePath( 587 return ui_test_utils::GetTestFilePath(
1076 base::FilePath(FILE_PATH_LITERAL("prerender")), 588 base::FilePath(FILE_PATH_LITERAL("prerender")),
1077 base::FilePath().AppendASCII(file_name)); 589 base::FilePath().AppendASCII(file_name));
1078 } 590 }
1079 591
1080 } // namespace 592 } // namespace
1081 593
1082 class PrerenderBrowserTest : virtual public InProcessBrowserTest { 594 class PrerenderBrowserTest : public test_utils::PrerenderInProcessBrowserTest {
1083 public: 595 public:
1084 PrerenderBrowserTest() 596 PrerenderBrowserTest()
1085 : autostart_test_server_(true), 597 : call_javascript_(true),
1086 prerender_contents_factory_(nullptr),
1087 safe_browsing_factory_(
1088 new safe_browsing::TestSafeBrowsingServiceFactory()),
1089 call_javascript_(true),
1090 check_load_events_(true), 598 check_load_events_(true),
1091 loader_path_("/prerender/prerender_loader.html"), 599 loader_path_("/prerender/prerender_loader.html") {}
1092 explicitly_set_browser_(nullptr) {}
1093 600
1094 ~PrerenderBrowserTest() override {} 601 ~PrerenderBrowserTest() override {}
1095 602
1096 content::SessionStorageNamespace* GetSessionStorageNamespace() const {
1097 WebContents* web_contents = GetActiveWebContents();
1098 if (!web_contents)
1099 return nullptr;
1100 return web_contents->GetController().GetDefaultSessionStorageNamespace();
1101 }
1102
1103 void SetUpInProcessBrowserTestFixture() override {
1104 safe_browsing_factory_->SetTestDatabaseManager(
1105 new FakeSafeBrowsingDatabaseManager());
1106 SafeBrowsingService::RegisterFactory(safe_browsing_factory_);
1107 }
1108
1109 void TearDownInProcessBrowserTestFixture() override {
1110 SafeBrowsingService::RegisterFactory(nullptr);
1111 }
1112
1113 void SetUpCommandLine(base::CommandLine* command_line) override { 603 void SetUpCommandLine(base::CommandLine* command_line) override {
604 PrerenderInProcessBrowserTest::SetUpCommandLine(command_line);
1114 command_line->AppendSwitchASCII(switches::kPrerenderMode, 605 command_line->AppendSwitchASCII(switches::kPrerenderMode,
1115 switches::kPrerenderModeSwitchValueEnabled); 606 switches::kPrerenderModeSwitchValueEnabled);
1116 command_line->AppendSwitch(switches::kEnablePepperTesting);
1117 command_line->AppendSwitchASCII(
1118 switches::kOverridePluginPowerSaverForTesting, "ignore-list");
1119
1120 ASSERT_TRUE(ppapi::RegisterPowerSaverTestPlugin(command_line));
1121 }
1122
1123 void SetUpOnMainThread() override {
1124 current_browser()->profile()->GetPrefs()->SetBoolean(
1125 prefs::kPromptForDownload, false);
1126 IncreasePrerenderMemory();
1127 if (autostart_test_server_)
1128 ASSERT_TRUE(embedded_test_server()->Start());
1129 ChromeResourceDispatcherHostDelegate::
1130 SetExternalProtocolHandlerDelegateForTesting(
1131 &external_protocol_handler_delegate_);
1132
1133 PrerenderManager* prerender_manager = GetPrerenderManager();
1134 ASSERT_TRUE(prerender_manager);
1135 prerender_manager->mutable_config().rate_limit_enabled = false;
1136 ASSERT_FALSE(prerender_contents_factory_);
1137 prerender_contents_factory_ = new TestPrerenderContentsFactory;
1138 prerender_manager->SetPrerenderContentsFactoryForTest(
1139 prerender_contents_factory_);
1140 ASSERT_TRUE(safe_browsing_factory_->test_safe_browsing_service());
1141 }
1142
1143 // Convenience function to get the currently active WebContents in
1144 // current_browser().
1145 WebContents* GetActiveWebContents() const {
1146 return current_browser()->tab_strip_model()->GetActiveWebContents();
1147 }
1148
1149 // Overload for a single expected final status
1150 std::unique_ptr<TestPrerender> PrerenderTestURL(
1151 const std::string& html_file,
1152 FinalStatus expected_final_status,
1153 int expected_number_of_loads) {
1154 GURL url = embedded_test_server()->GetURL(html_file);
1155 return PrerenderTestURL(url,
1156 expected_final_status,
1157 expected_number_of_loads);
1158 }
1159
1160 ScopedVector<TestPrerender> PrerenderTestURL(
1161 const std::string& html_file,
1162 const std::vector<FinalStatus>& expected_final_status_queue,
1163 int expected_number_of_loads) {
1164 GURL url = embedded_test_server()->GetURL(html_file);
1165 return PrerenderTestURLImpl(url,
1166 expected_final_status_queue,
1167 expected_number_of_loads);
1168 }
1169
1170 std::unique_ptr<TestPrerender> PrerenderTestURL(
1171 const GURL& url,
1172 FinalStatus expected_final_status,
1173 int expected_number_of_loads) {
1174 std::vector<FinalStatus> expected_final_status_queue(
1175 1, expected_final_status);
1176 std::vector<TestPrerender*> prerenders;
1177 PrerenderTestURLImpl(url,
1178 expected_final_status_queue,
1179 expected_number_of_loads).release(&prerenders);
1180 CHECK_EQ(1u, prerenders.size());
1181 return std::unique_ptr<TestPrerender>(prerenders[0]);
1182 } 607 }
1183 608
1184 void NavigateToDestURL() const { 609 void NavigateToDestURL() const {
1185 NavigateToDestURLWithDisposition(WindowOpenDisposition::CURRENT_TAB, true); 610 NavigateToDestURLWithDisposition(WindowOpenDisposition::CURRENT_TAB, true);
1186 } 611 }
1187 612
1188 // Opens the url in a new tab, with no opener. 613 // Opens the url in a new tab, with no opener.
1189 void NavigateToDestURLWithDisposition( 614 void NavigateToDestURLWithDisposition(
1190 WindowOpenDisposition disposition, 615 WindowOpenDisposition disposition,
1191 bool expect_swap_to_succeed) const { 616 bool expect_swap_to_succeed) const {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 chrome::GoBack(current_browser(), WindowOpenDisposition::CURRENT_TAB); 726 chrome::GoBack(current_browser(), WindowOpenDisposition::CURRENT_TAB);
1302 back_nav_observer.Wait(); 727 back_nav_observer.Wait();
1303 bool js_result; 728 bool js_result;
1304 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 729 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1305 tab, 730 tab,
1306 "window.domAutomationController.send(DidBackToOriginalPagePass())", 731 "window.domAutomationController.send(DidBackToOriginalPagePass())",
1307 &js_result)); 732 &js_result));
1308 EXPECT_TRUE(js_result); 733 EXPECT_TRUE(js_result);
1309 } 734 }
1310 735
1311 bool UrlIsInPrerenderManager(const std::string& html_file) const {
1312 return UrlIsInPrerenderManager(embedded_test_server()->GetURL(html_file));
1313 }
1314
1315 bool UrlIsInPrerenderManager(const GURL& url) const {
1316 return GetPrerenderManager()->FindPrerenderData(
1317 url, GetSessionStorageNamespace()) != nullptr;
1318 }
1319
1320 void UseHttpsSrcServer() { 736 void UseHttpsSrcServer() {
1321 if (https_src_server_) 737 if (https_src_server_)
1322 return; 738 return;
1323 https_src_server_.reset( 739 https_src_server_.reset(
1324 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); 740 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
1325 https_src_server_->ServeFilesFromSourceDirectory("chrome/test/data"); 741 https_src_server_->ServeFilesFromSourceDirectory("chrome/test/data");
1326 CHECK(https_src_server_->Start()); 742 CHECK(https_src_server_->Start());
1327 } 743 }
1328 744
1329 void DisableJavascriptCalls() { 745 void DisableJavascriptCalls() {
1330 call_javascript_ = false; 746 call_javascript_ = false;
1331 } 747 }
1332 748
1333 void DisableLoadEventCheck() { 749 void DisableLoadEventCheck() {
1334 check_load_events_ = false; 750 check_load_events_ = false;
1335 } 751 }
1336 752
1337 PrerenderManager* GetPrerenderManager() const {
1338 PrerenderManager* prerender_manager =
1339 PrerenderManagerFactory::GetForProfile(current_browser()->profile());
1340 return prerender_manager;
1341 }
1342
1343 const PrerenderLinkManager* GetPrerenderLinkManager() const { 753 const PrerenderLinkManager* GetPrerenderLinkManager() const {
1344 PrerenderLinkManager* prerender_link_manager = 754 PrerenderLinkManager* prerender_link_manager =
1345 PrerenderLinkManagerFactory::GetForProfile( 755 PrerenderLinkManagerFactory::GetForProfile(
1346 current_browser()->profile()); 756 current_browser()->profile());
1347 return prerender_link_manager; 757 return prerender_link_manager;
1348 } 758 }
1349 759
1350 int GetPrerenderEventCount(int index, const std::string& type) const { 760 int GetPrerenderEventCount(int index, const std::string& type) const {
1351 int event_count; 761 int event_count;
1352 std::string expression = base::StringPrintf( 762 std::string expression = base::StringPrintf(
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 std::unique_ptr<base::DictionaryValue> prerender_dict = 833 std::unique_ptr<base::DictionaryValue> prerender_dict =
1424 GetPrerenderManager()->GetAsValue(); 834 GetPrerenderManager()->GetAsValue();
1425 if (!prerender_dict) 835 if (!prerender_dict)
1426 return std::numeric_limits<size_t>::max(); 836 return std::numeric_limits<size_t>::max();
1427 base::ListValue* history_list; 837 base::ListValue* history_list;
1428 if (!prerender_dict->GetList("history", &history_list)) 838 if (!prerender_dict->GetList("history", &history_list))
1429 return std::numeric_limits<size_t>::max(); 839 return std::numeric_limits<size_t>::max();
1430 return history_list->GetSize(); 840 return history_list->GetSize();
1431 } 841 }
1432 842
1433 FakeSafeBrowsingDatabaseManager* GetFakeSafeBrowsingDatabaseManager() { 843 test_utils::FakeSafeBrowsingDatabaseManager*
1434 return static_cast<FakeSafeBrowsingDatabaseManager*>( 844 GetFakeSafeBrowsingDatabaseManager() {
1435 safe_browsing_factory_->test_safe_browsing_service() 845 return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>(
846 safe_browsing_factory()->test_safe_browsing_service()
1436 ->database_manager() 847 ->database_manager()
1437 .get()); 848 .get());
1438 } 849 }
1439 850
1440 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const {
1441 PrerenderManager::PrerenderData* prerender_data =
1442 GetPrerenderManager()->FindPrerenderData(url, nullptr);
1443 return static_cast<TestPrerenderContents*>(
1444 prerender_data ? prerender_data->contents() : nullptr);
1445 }
1446
1447 void SetLoaderHostOverride(const std::string& host) { 851 void SetLoaderHostOverride(const std::string& host) {
1448 loader_host_override_ = host; 852 loader_host_override_ = host;
1449 host_resolver()->AddRule(host, "127.0.0.1"); 853 host_resolver()->AddRule(host, "127.0.0.1");
1450 } 854 }
1451 855
1452 void set_loader_path(const std::string& path) { 856 void set_loader_path(const std::string& path) {
1453 loader_path_ = path; 857 loader_path_ = path;
1454 } 858 }
1455 859
1456 void set_loader_query(const std::string& query) { 860 void set_loader_query(const std::string& query) {
1457 loader_query_ = query; 861 loader_query_ = query;
1458 } 862 }
1459 863
1460 GURL GetCrossDomainTestUrl(const std::string& path) { 864 GURL GetCrossDomainTestUrl(const std::string& path) {
1461 static const std::string secondary_domain = "www.foo.com"; 865 static const std::string secondary_domain = "www.foo.com";
1462 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); 866 host_resolver()->AddRule(secondary_domain, "127.0.0.1");
1463 std::string url_str(base::StringPrintf( 867 std::string url_str(base::StringPrintf(
1464 "http://%s:%d/%s", secondary_domain.c_str(), 868 "http://%s:%d/%s", secondary_domain.c_str(),
1465 embedded_test_server()->host_port_pair().port(), path.c_str())); 869 embedded_test_server()->host_port_pair().port(), path.c_str()));
1466 return GURL(url_str); 870 return GURL(url_str);
1467 } 871 }
1468 872
1469 void set_browser(Browser* browser) {
1470 explicitly_set_browser_ = browser;
1471 }
1472
1473 Browser* current_browser() const {
1474 return explicitly_set_browser_ ? explicitly_set_browser_ : browser();
1475 }
1476
1477 const GURL& dest_url() const { 873 const GURL& dest_url() const {
1478 return dest_url_; 874 return dest_url_;
1479 } 875 }
1480 876
1481 void IncreasePrerenderMemory() {
1482 // Increase the memory allowed in a prerendered page above normal settings.
1483 // Debug build bots occasionally run against the default limit, and tests
1484 // were failing because the prerender was canceled due to memory exhaustion.
1485 // http://crbug.com/93076
1486 GetPrerenderManager()->mutable_config().max_bytes = 2000 * 1024 * 1024;
1487 }
1488
1489 bool DidPrerenderPass(WebContents* web_contents) const { 877 bool DidPrerenderPass(WebContents* web_contents) const {
1490 bool prerender_test_result = false; 878 bool prerender_test_result = false;
1491 if (!content::ExecuteScriptAndExtractBool( 879 if (!content::ExecuteScriptAndExtractBool(
1492 web_contents, 880 web_contents,
1493 "window.domAutomationController.send(DidPrerenderPass())", 881 "window.domAutomationController.send(DidPrerenderPass())",
1494 &prerender_test_result)) 882 &prerender_test_result))
1495 return false; 883 return false;
1496 return prerender_test_result; 884 return prerender_test_result;
1497 } 885 }
1498 886
1499 bool DidDisplayPass(WebContents* web_contents) const { 887 bool DidDisplayPass(WebContents* web_contents) const {
1500 bool display_test_result = false; 888 bool display_test_result = false;
1501 if (!content::ExecuteScriptAndExtractBool( 889 if (!content::ExecuteScriptAndExtractBool(
1502 web_contents, 890 web_contents,
1503 "window.domAutomationController.send(DidDisplayPass())", 891 "window.domAutomationController.send(DidDisplayPass())",
1504 &display_test_result)) 892 &display_test_result))
1505 return false; 893 return false;
1506 return display_test_result; 894 return display_test_result;
1507 } 895 }
1508 896
1509 std::unique_ptr<TestPrerender> ExpectPrerender( 897 std::unique_ptr<TestPrerender> ExpectPrerender(
1510 FinalStatus expected_final_status) { 898 FinalStatus expected_final_status) {
1511 return prerender_contents_factory_->ExpectPrerenderContents( 899 return prerender_contents_factory()->ExpectPrerenderContents(
1512 expected_final_status); 900 expected_final_status);
1513 } 901 }
1514 902
1515 void AddPrerender(const GURL& url, int index) { 903 void AddPrerender(const GURL& url, int index) {
1516 std::string javascript = base::StringPrintf( 904 std::string javascript = base::StringPrintf(
1517 "AddPrerender('%s', %d)", url.spec().c_str(), index); 905 "AddPrerender('%s', %d)", url.spec().c_str(), index);
1518 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame(); 906 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame();
1519 render_frame_host->ExecuteJavaScriptForTests( 907 render_frame_host->ExecuteJavaScriptForTests(
1520 base::ASCIIToUTF16(javascript)); 908 base::ASCIIToUTF16(javascript));
1521 } 909 }
1522 910
1523 // Returns a string for pattern-matching TaskManager tab entries. 911 // Returns a string for pattern-matching TaskManager tab entries.
1524 base::string16 MatchTaskManagerTab(const char* page_title) { 912 base::string16 MatchTaskManagerTab(const char* page_title) {
1525 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, 913 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX,
1526 base::ASCIIToUTF16(page_title)); 914 base::ASCIIToUTF16(page_title));
1527 } 915 }
1528 916
1529 // Returns a string for pattern-matching TaskManager prerender entries. 917 // Returns a string for pattern-matching TaskManager prerender entries.
1530 base::string16 MatchTaskManagerPrerender(const char* page_title) { 918 base::string16 MatchTaskManagerPrerender(const char* page_title) {
1531 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX, 919 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
1532 base::ASCIIToUTF16(page_title)); 920 base::ASCIIToUTF16(page_title));
1533 } 921 }
1534 922
1535 const base::HistogramTester& histogram_tester() { return histogram_tester_; } 923 const base::HistogramTester& histogram_tester() { return histogram_tester_; }
1536 924
1537 protected:
1538 bool autostart_test_server_;
1539
1540 private: 925 private:
1541 // TODO(davidben): Remove this altogether so the tests don't globally assume 926 // TODO(davidben): Remove this altogether so the tests don't globally assume
1542 // only one prerender. 927 // only one prerender.
1543 TestPrerenderContents* GetPrerenderContents() const { 928 TestPrerenderContents* GetPrerenderContents() const {
1544 return GetPrerenderContentsFor(dest_url_); 929 return GetPrerenderContentsFor(dest_url_);
1545 } 930 }
1546 931
1547 ScopedVector<TestPrerender> PrerenderTestURLImpl( 932 ScopedVector<TestPrerender> PrerenderTestURLImpl(
1548 const GURL& prerender_url, 933 const GURL& prerender_url,
1549 const std::vector<FinalStatus>& expected_final_status_queue, 934 const std::vector<FinalStatus>& expected_final_status_queue,
1550 int expected_number_of_loads) { 935 int expected_number_of_loads) override {
1551 dest_url_ = prerender_url; 936 dest_url_ = prerender_url;
1552 937
1553 base::StringPairs replacement_text; 938 base::StringPairs replacement_text;
1554 replacement_text.push_back( 939 replacement_text.push_back(
1555 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec())); 940 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec()));
1556 std::string replacement_path; 941 std::string replacement_path;
1557 net::test_server::GetFilePathWithReplacements( 942 net::test_server::GetFilePathWithReplacements(
1558 loader_path_, replacement_text, &replacement_path); 943 loader_path_, replacement_text, &replacement_path);
1559 944
1560 const net::EmbeddedTestServer* src_server = embedded_test_server(); 945 const net::EmbeddedTestServer* src_server = embedded_test_server();
1561 if (https_src_server_) 946 if (https_src_server_)
1562 src_server = https_src_server_.get(); 947 src_server = https_src_server_.get();
1563 GURL loader_url = src_server->GetURL( 948 GURL loader_url = src_server->GetURL(
1564 replacement_path + "&" + loader_query_); 949 replacement_path + "&" + loader_query_);
1565 950
1566 GURL::Replacements loader_replacements; 951 GURL::Replacements loader_replacements;
1567 if (!loader_host_override_.empty()) 952 if (!loader_host_override_.empty())
1568 loader_replacements.SetHostStr(loader_host_override_); 953 loader_replacements.SetHostStr(loader_host_override_);
1569 loader_url = loader_url.ReplaceComponents(loader_replacements); 954 loader_url = loader_url.ReplaceComponents(loader_replacements);
1570 955
1571 VLOG(1) << "Running test with queue length " <<
1572 expected_final_status_queue.size();
1573 CHECK(!expected_final_status_queue.empty()); 956 CHECK(!expected_final_status_queue.empty());
1574 ScopedVector<TestPrerender> prerenders; 957 ScopedVector<TestPrerender> prerenders;
1575 for (size_t i = 0; i < expected_final_status_queue.size(); i++) { 958 for (size_t i = 0; i < expected_final_status_queue.size(); i++) {
1576 prerenders.push_back( 959 prerenders.push_back(
1577 prerender_contents_factory_->ExpectPrerenderContents( 960 prerender_contents_factory()->ExpectPrerenderContents(
pasko 2016/09/05 13:46:27 this is doing very similar things to oStatePrefetc
mattcary 2016/09/06 08:06:20 At this point, I don't think it is possible to ext
pasko 2016/09/06 14:40:56 OK, let's address the question of code sharing in
1578 expected_final_status_queue[i]).release()); 961 expected_final_status_queue[i]).release());
1579 } 962 }
1580 963
1581 FinalStatus expected_final_status = expected_final_status_queue.front(); 964 FinalStatus expected_final_status = expected_final_status_queue.front();
1582 965
1583 // Navigate to the loader URL and then wait for the first prerender to be 966 // Navigate to the loader URL and then wait for the first prerender to be
1584 // created. 967 // created.
1585 ui_test_utils::NavigateToURL(current_browser(), loader_url); 968 ui_test_utils::NavigateToURL(current_browser(), loader_url);
1586 prerenders[0]->WaitForCreate(); 969 prerenders[0]->WaitForCreate();
1587 prerenders[0]->WaitForLoads(expected_number_of_loads); 970 prerenders[0]->WaitForLoads(expected_number_of_loads);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1663 observer.Wait(); 1046 observer.Wait();
1664 } else { 1047 } else {
1665 NavigationOrSwapObserver observer(current_browser()->tab_strip_model(), 1048 NavigationOrSwapObserver observer(current_browser()->tab_strip_model(),
1666 web_contents); 1049 web_contents);
1667 render_frame_host->ExecuteJavaScriptForTests( 1050 render_frame_host->ExecuteJavaScriptForTests(
1668 base::ASCIIToUTF16(javascript)); 1051 base::ASCIIToUTF16(javascript));
1669 observer.Wait(); 1052 observer.Wait();
1670 } 1053 }
1671 } 1054 }
1672 1055
1673 TestPrerenderContentsFactory* prerender_contents_factory_;
1674 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory_;
1675 NeverRunsExternalProtocolHandlerDelegate external_protocol_handler_delegate_;
1676 GURL dest_url_; 1056 GURL dest_url_;
1677 std::unique_ptr<net::EmbeddedTestServer> https_src_server_; 1057 std::unique_ptr<net::EmbeddedTestServer> https_src_server_;
1678 bool call_javascript_; 1058 bool call_javascript_;
1679 bool check_load_events_; 1059 bool check_load_events_;
1680 std::string loader_host_override_; 1060 std::string loader_host_override_;
1681 std::string loader_path_; 1061 std::string loader_path_;
1682 std::string loader_query_; 1062 std::string loader_query_;
1683 Browser* explicitly_set_browser_;
1684 base::HistogramTester histogram_tester_; 1063 base::HistogramTester histogram_tester_;
1685 }; 1064 };
1686 1065
1687 // Checks that a page is correctly prerendered in the case of a 1066 // Checks that a page is correctly prerendered in the case of a
1688 // <link rel=prerender> tag and then loaded into a tab in response to a 1067 // <link rel=prerender> tag and then loaded into a tab in response to a
1689 // navigation. 1068 // navigation.
1690 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { 1069 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) {
1691 PrerenderTestURL("/prerender/prerender_page.html", FINAL_STATUS_USED, 1); 1070 PrerenderTestURL("/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
1692 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0)); 1071 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
1693 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1); 1072 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
(...skipping 1620 matching lines...) Expand 10 before | Expand all | Expand 10 after
3314 content::SetBrowserClientForTesting(original_browser_client); 2693 content::SetBrowserClientForTesting(original_browser_client);
3315 } 2694 }
3316 2695
3317 // Test interaction of the webNavigation and tabs API with prerender. 2696 // Test interaction of the webNavigation and tabs API with prerender.
3318 class PrerenderBrowserTestWithExtensions : public PrerenderBrowserTest, 2697 class PrerenderBrowserTestWithExtensions : public PrerenderBrowserTest,
3319 public ExtensionApiTest { 2698 public ExtensionApiTest {
3320 public: 2699 public:
3321 PrerenderBrowserTestWithExtensions() { 2700 PrerenderBrowserTestWithExtensions() {
3322 // The individual tests start the test server through ExtensionApiTest, so 2701 // The individual tests start the test server through ExtensionApiTest, so
3323 // the port number can be passed through to the extension. 2702 // the port number can be passed through to the extension.
3324 autostart_test_server_ = false; 2703 set_autostart_test_server(false);
3325 } 2704 }
3326 2705
3327 void SetUp() override { PrerenderBrowserTest::SetUp(); } 2706 void SetUp() override { PrerenderBrowserTest::SetUp(); }
3328 2707
3329 void SetUpCommandLine(base::CommandLine* command_line) override { 2708 void SetUpCommandLine(base::CommandLine* command_line) override {
3330 PrerenderBrowserTest::SetUpCommandLine(command_line); 2709 PrerenderBrowserTest::SetUpCommandLine(command_line);
3331 ExtensionApiTest::SetUpCommandLine(command_line); 2710 ExtensionApiTest::SetUpCommandLine(command_line);
3332 } 2711 }
3333 2712
3334 void SetUpInProcessBrowserTestFixture() override { 2713 void SetUpInProcessBrowserTestFixture() override {
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
3980 browser()->tab_strip_model()->GetActiveWebContents(); 3359 browser()->tab_strip_model()->GetActiveWebContents();
3981 bool display_test_result = false; 3360 bool display_test_result = false;
3982 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents, 3361 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents,
3983 "DidDisplayReallyPass()", 3362 "DidDisplayReallyPass()",
3984 &display_test_result)); 3363 &display_test_result));
3985 ASSERT_TRUE(display_test_result); 3364 ASSERT_TRUE(display_test_result);
3986 } 3365 }
3987 #endif // !defined(DISABLE_NACL) 3366 #endif // !defined(DISABLE_NACL)
3988 3367
3989 } // namespace prerender 3368 } // namespace prerender
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/prerender/prerender_manager.h » ('j') | chrome/browser/prerender/prerender_test_utils.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698