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 <deque> | 5 #include <deque> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/browsing_data_remover.h" | 14 #include "chrome/browser/browsing_data_remover.h" |
15 #include "chrome/browser/content_settings/host_content_settings_map.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map.h" |
16 #include "chrome/browser/favicon/favicon_tab_helper.h" | 16 #include "chrome/browser/favicon/favicon_tab_helper.h" |
17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
18 #include "chrome/browser/prerender/prerender_contents.h" | 18 #include "chrome/browser/prerender/prerender_contents.h" |
| 19 #include "chrome/browser/prerender/prerender_link_manager.h" |
| 20 #include "chrome/browser/prerender/prerender_link_manager_factory.h" |
19 #include "chrome/browser/prerender/prerender_manager.h" | 21 #include "chrome/browser/prerender/prerender_manager.h" |
20 #include "chrome/browser/prerender/prerender_manager_factory.h" | 22 #include "chrome/browser/prerender/prerender_manager_factory.h" |
21 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 24 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
23 #include "chrome/browser/task_manager/task_manager.h" | 25 #include "chrome/browser/task_manager/task_manager.h" |
24 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | 26 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
25 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
26 #include "chrome/browser/ui/browser_list.h" | 28 #include "chrome/browser/ui/browser_list.h" |
27 #include "chrome/browser/ui/browser_window.h" | 29 #include "chrome/browser/ui/browser_window.h" |
28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 30 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 OpenDestURLWithJSImpl("MetaClick()"); | 619 OpenDestURLWithJSImpl("MetaClick()"); |
618 #else | 620 #else |
619 OpenDestURLWithJSImpl("CtrlClick()"); | 621 OpenDestURLWithJSImpl("CtrlClick()"); |
620 #endif | 622 #endif |
621 } | 623 } |
622 | 624 |
623 void OpenDestURLViaWindowOpen() const { | 625 void OpenDestURLViaWindowOpen() const { |
624 OpenDestURLWithJSImpl("WindowOpen()"); | 626 OpenDestURLWithJSImpl("WindowOpen()"); |
625 } | 627 } |
626 | 628 |
| 629 void RemoveLinkElementAndNavigate() const { |
| 630 OpenDestURLWithJSImpl("RemoveLinkElementAndNavigate()"); |
| 631 } |
| 632 |
627 void ClickToNextPageAfterPrerender(Browser* browser) { | 633 void ClickToNextPageAfterPrerender(Browser* browser) { |
628 ui_test_utils::WindowedNotificationObserver new_page_observer( | 634 ui_test_utils::WindowedNotificationObserver new_page_observer( |
629 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 635 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
630 content::NotificationService::AllSources()); | 636 content::NotificationService::AllSources()); |
631 RenderViewHost* render_view_host = | 637 RenderViewHost* render_view_host = |
632 browser->GetSelectedWebContents()->GetRenderViewHost(); | 638 browser->GetSelectedWebContents()->GetRenderViewHost(); |
633 render_view_host->ExecuteJavascriptInWebFrame( | 639 render_view_host->ExecuteJavascriptInWebFrame( |
634 string16(), | 640 string16(), |
635 ASCIIToUTF16("ClickOpenLink()")); | 641 ASCIIToUTF16("ClickOpenLink()")); |
636 new_page_observer.Wait(); | 642 new_page_observer.Wait(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 } | 726 } |
721 | 727 |
722 PrerenderManager* prerender_manager() const { | 728 PrerenderManager* prerender_manager() const { |
723 Profile* profile = | 729 Profile* profile = |
724 current_browser()->GetSelectedTabContentsWrapper()->profile(); | 730 current_browser()->GetSelectedTabContentsWrapper()->profile(); |
725 PrerenderManager* prerender_manager = | 731 PrerenderManager* prerender_manager = |
726 PrerenderManagerFactory::GetForProfile(profile); | 732 PrerenderManagerFactory::GetForProfile(profile); |
727 return prerender_manager; | 733 return prerender_manager; |
728 } | 734 } |
729 | 735 |
| 736 PrerenderLinkManager* prerender_link_manager() const { |
| 737 Profile* profile = |
| 738 current_browser()->GetSelectedTabContentsWrapper()->profile(); |
| 739 PrerenderLinkManager* prerender_link_manager = |
| 740 PrerenderLinkManagerFactory::GetForProfile(profile); |
| 741 return prerender_link_manager; |
| 742 } |
| 743 |
730 // Returns length of |prerender_manager_|'s history, or -1 on failure. | 744 // Returns length of |prerender_manager_|'s history, or -1 on failure. |
731 int GetHistoryLength() const { | 745 int GetHistoryLength() const { |
732 scoped_ptr<DictionaryValue> prerender_dict( | 746 scoped_ptr<DictionaryValue> prerender_dict( |
733 static_cast<DictionaryValue*>(prerender_manager()->GetAsValue())); | 747 static_cast<DictionaryValue*>(prerender_manager()->GetAsValue())); |
734 if (!prerender_dict.get()) | 748 if (!prerender_dict.get()) |
735 return -1; | 749 return -1; |
736 ListValue* history_list; | 750 ListValue* history_list; |
737 if (!prerender_dict->GetList("history", &history_list)) | 751 if (!prerender_dict->GetList("history", &history_list)) |
738 return -1; | 752 return -1; |
739 return static_cast<int>(history_list->GetSize()); | 753 return static_cast<int>(history_list->GetSize()); |
740 } | 754 } |
741 | 755 |
742 #if defined(ENABLE_SAFE_BROWSING) | 756 #if defined(ENABLE_SAFE_BROWSING) |
743 FakeSafeBrowsingService* GetSafeBrowsingService() { | 757 FakeSafeBrowsingService* GetSafeBrowsingService() { |
744 return safe_browsing_factory_->most_recent_service(); | 758 return safe_browsing_factory_->most_recent_service(); |
745 } | 759 } |
746 #endif | 760 #endif |
747 | 761 |
748 TestPrerenderContents* GetPrerenderContents() const { | 762 TestPrerenderContents* GetPrerenderContents() const { |
749 return static_cast<TestPrerenderContents*>( | 763 return static_cast<TestPrerenderContents*>( |
750 prerender_manager()->FindEntry(dest_url_)); | 764 prerender_manager()->FindEntry(dest_url_)); |
751 } | 765 } |
752 | 766 |
753 void set_loader_path(const std::string& path) { | 767 void set_loader_path(const std::string& path) { |
754 loader_path_ = path; | 768 loader_path_ = path; |
755 } | 769 } |
756 | 770 |
| 771 void set_loader_query_and_fragment(const std::string& query_and_fragment) { |
| 772 loader_query_and_fragment_ = query_and_fragment; |
| 773 } |
| 774 |
757 GURL GetCrossDomainTestUrl(const std::string& path) { | 775 GURL GetCrossDomainTestUrl(const std::string& path) { |
758 static const std::string secondary_domain = "www.foo.com"; | 776 static const std::string secondary_domain = "www.foo.com"; |
759 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | 777 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
760 std::string url_str(base::StringPrintf( | 778 std::string url_str(base::StringPrintf( |
761 "http://%s:%d/%s", | 779 "http://%s:%d/%s", |
762 secondary_domain.c_str(), | 780 secondary_domain.c_str(), |
763 test_server()->host_port_pair().port(), | 781 test_server()->host_port_pair().port(), |
764 path.c_str())); | 782 path.c_str())); |
765 return GURL(url_str); | 783 return GURL(url_str); |
766 } | 784 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 net::TestServer* src_server = test_server(); | 825 net::TestServer* src_server = test_server(); |
808 scoped_ptr<net::TestServer> https_src_server; | 826 scoped_ptr<net::TestServer> https_src_server; |
809 if (use_https_src_server_) { | 827 if (use_https_src_server_) { |
810 https_src_server.reset( | 828 https_src_server.reset( |
811 new net::TestServer(net::TestServer::TYPE_HTTPS, | 829 new net::TestServer(net::TestServer::TYPE_HTTPS, |
812 net::TestServer::kLocalhost, | 830 net::TestServer::kLocalhost, |
813 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); | 831 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); |
814 ASSERT_TRUE(https_src_server->Start()); | 832 ASSERT_TRUE(https_src_server->Start()); |
815 src_server = https_src_server.get(); | 833 src_server = https_src_server.get(); |
816 } | 834 } |
817 GURL src_url = src_server->GetURL(replacement_path); | 835 GURL loader_url = src_server->GetURL(replacement_path + |
| 836 loader_query_and_fragment_); |
818 | 837 |
819 ASSERT_TRUE(prerender_manager()); | 838 ASSERT_TRUE(prerender_manager()); |
820 prerender_manager()->mutable_config().rate_limit_enabled = false; | 839 prerender_manager()->mutable_config().rate_limit_enabled = false; |
821 prerender_manager()->mutable_config().https_allowed = true; | 840 prerender_manager()->mutable_config().https_allowed = true; |
822 ASSERT_TRUE(prerender_contents_factory_ == NULL); | 841 ASSERT_TRUE(prerender_contents_factory_ == NULL); |
823 prerender_contents_factory_ = | 842 prerender_contents_factory_ = |
824 new WaitForLoadPrerenderContentsFactory( | 843 new WaitForLoadPrerenderContentsFactory( |
825 expected_number_of_loads, | 844 expected_number_of_loads, |
826 expected_final_status_queue, | 845 expected_final_status_queue, |
827 prerender_should_wait_for_ready_title); | 846 prerender_should_wait_for_ready_title); |
828 prerender_manager()->SetPrerenderContentsFactory( | 847 prerender_manager()->SetPrerenderContentsFactory( |
829 prerender_contents_factory_); | 848 prerender_contents_factory_); |
830 FinalStatus expected_final_status = expected_final_status_queue.front(); | 849 FinalStatus expected_final_status = expected_final_status_queue.front(); |
831 | 850 |
832 // ui_test_utils::NavigateToURL uses its own observer and message loop. | 851 // ui_test_utils::NavigateToURL uses its own observer and message loop. |
833 // Since the test needs to wait until the prerendered page has stopped | 852 // Since the test needs to wait until the prerendered page has stopped |
834 // loading, rather than the page directly navigated to, need to | 853 // loading, rather than the page directly navigated to, need to |
835 // handle browser navigation directly. | 854 // handle browser navigation directly. |
836 current_browser()->OpenURL(OpenURLParams( | 855 current_browser()->OpenURL(OpenURLParams( |
837 src_url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, | 856 loader_url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, |
838 false)); | 857 false)); |
839 | 858 |
840 ui_test_utils::RunMessageLoop(); | 859 ui_test_utils::RunMessageLoop(); |
841 | 860 |
842 TestPrerenderContents* prerender_contents = GetPrerenderContents(); | 861 TestPrerenderContents* prerender_contents = GetPrerenderContents(); |
843 | 862 |
844 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status)) { | 863 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status)) { |
845 ASSERT_TRUE(prerender_contents != NULL); | 864 ASSERT_TRUE(prerender_contents != NULL); |
846 EXPECT_EQ(FINAL_STATUS_MAX, prerender_contents->final_status()); | 865 EXPECT_EQ(FINAL_STATUS_MAX, prerender_contents->final_status()); |
847 | 866 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
937 } | 956 } |
938 | 957 |
939 WaitForLoadPrerenderContentsFactory* prerender_contents_factory_; | 958 WaitForLoadPrerenderContentsFactory* prerender_contents_factory_; |
940 #if defined(ENABLE_SAFE_BROWSING) | 959 #if defined(ENABLE_SAFE_BROWSING) |
941 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_; | 960 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_; |
942 #endif | 961 #endif |
943 GURL dest_url_; | 962 GURL dest_url_; |
944 bool use_https_src_server_; | 963 bool use_https_src_server_; |
945 bool call_javascript_; | 964 bool call_javascript_; |
946 std::string loader_path_; | 965 std::string loader_path_; |
| 966 std::string loader_query_and_fragment_; |
947 Browser* explicitly_set_browser_; | 967 Browser* explicitly_set_browser_; |
948 }; | 968 }; |
949 | 969 |
950 // Checks that a page is correctly prerendered in the case of a | 970 // Checks that a page is correctly prerendered in the case of a |
951 // <link rel=prerender> tag and then loaded into a tab in response to a | 971 // <link rel=prerender> tag and then loaded into a tab in response to a |
952 // navigation. | 972 // navigation. |
953 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { | 973 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { |
954 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1); | 974 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1); |
955 NavigateToDestURL(); | 975 NavigateToDestURL(); |
| 976 ASSERT_TRUE(prerender_link_manager()->IsEmpty()); |
| 977 } |
| 978 |
| 979 // TODO(gavinp): After https://bugs.webkit.org/show_bug.cgi?id=85005 lands, |
| 980 // enable this test. |
| 981 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, |
| 982 DISABLED_PrerenderPageRemovingLink) { |
| 983 set_loader_path("files/prerender/prerender_loader_removing_links.html"); |
| 984 set_loader_query_and_fragment("?links_to_insert=1&links_to_remove=1"); |
| 985 PrerenderTestURL("files/prerender/prerender_page.html", |
| 986 FINAL_STATUS_CANCELLED, 1); |
| 987 RemoveLinkElementAndNavigate(); |
| 988 } |
| 989 |
| 990 // TODO(gavinp): After https://bugs.webkit.org/show_bug.cgi?id=85005 lands, |
| 991 // enable this test. |
| 992 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, |
| 993 DISABLED_PrerenderPageRemovingLinkWithTwoLinks) { |
| 994 set_loader_path("files/prerender/prerender_loader_removing_links.html"); |
| 995 set_loader_query_and_fragment("?links_to_insert=2&links_to_remove=2"); |
| 996 PrerenderTestURL("files/prerender/prerender_page.html", |
| 997 FINAL_STATUS_CANCELLED, 1); |
| 998 RemoveLinkElementAndNavigate(); |
| 999 } |
| 1000 |
| 1001 // TODO(gavinp): After https://bugs.webkit.org/show_bug.cgi?id=85005 lands, |
| 1002 // enable this test. |
| 1003 IN_PROC_BROWSER_TEST_F( |
| 1004 PrerenderBrowserTest, |
| 1005 DISABLED_PrerenderPageRemovingLinkWithTwoLinksRemovingOne) { |
| 1006 set_loader_path("files/prerender/prerender_loader_removing_links.html"); |
| 1007 set_loader_query_and_fragment("?links_to_insert=2&links_to_remove=1"); |
| 1008 PrerenderTestURL("files/prerender/prerender_page.html", |
| 1009 FINAL_STATUS_USED, 1); |
| 1010 RemoveLinkElementAndNavigate(); |
956 } | 1011 } |
957 | 1012 |
958 // Checks that prerendering works in incognito mode. | 1013 // Checks that prerendering works in incognito mode. |
959 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderIncognito) { | 1014 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderIncognito) { |
960 Profile* normal_profile = current_browser()->profile(); | 1015 Profile* normal_profile = current_browser()->profile(); |
961 ui_test_utils::OpenURLOffTheRecord(normal_profile, GURL("about:blank")); | 1016 ui_test_utils::OpenURLOffTheRecord(normal_profile, GURL("about:blank")); |
962 set_browser(BrowserList::FindBrowserWithProfile( | 1017 set_browser(BrowserList::FindBrowserWithProfile( |
963 normal_profile->GetOffTheRecordProfile())); | 1018 normal_profile->GetOffTheRecordProfile())); |
964 // Increase memory expectations on the incognito PrerenderManager. | 1019 // Increase memory expectations on the incognito PrerenderManager. |
965 IncreasePrerenderMemory(); | 1020 IncreasePrerenderMemory(); |
(...skipping 1181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2147 WebContents* web_contents = browser()->GetSelectedWebContents(); | 2202 WebContents* web_contents = browser()->GetSelectedWebContents(); |
2148 bool display_test_result = false; | 2203 bool display_test_result = false; |
2149 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 2204 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
2150 web_contents->GetRenderViewHost(), L"", | 2205 web_contents->GetRenderViewHost(), L"", |
2151 L"DidDisplayReallyPass()", | 2206 L"DidDisplayReallyPass()", |
2152 &display_test_result)); | 2207 &display_test_result)); |
2153 ASSERT_TRUE(display_test_result); | 2208 ASSERT_TRUE(display_test_result); |
2154 } | 2209 } |
2155 | 2210 |
2156 } // namespace prerender | 2211 } // namespace prerender |
OLD | NEW |