| 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/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "content/browser/renderer_host/render_view_host_impl.h" | 7 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 8 #include "content/browser/renderer_host/test_render_view_host.h" | 8 #include "content/browser/renderer_host/test_render_view_host.h" |
| 9 #include "content/browser/site_instance_impl.h" | 9 #include "content/browser/site_instance_impl.h" |
| 10 #include "content/browser/web_contents/interstitial_page_impl.h" | 10 #include "content/browser/web_contents/interstitial_page_impl.h" |
| 11 #include "content/browser/web_contents/navigation_entry_impl.h" | 11 #include "content/browser/web_contents/navigation_entry_impl.h" |
| 12 #include "content/browser/web_contents/test_web_contents.h" | 12 #include "content/browser/web_contents/test_web_contents.h" |
| 13 #include "content/browser/webui/web_ui_controller_factory_registry.h" | |
| 14 #include "content/common/view_messages.h" | 13 #include "content/common/view_messages.h" |
| 15 #include "content/public/browser/interstitial_page_delegate.h" | 14 #include "content/public/browser/interstitial_page_delegate.h" |
| 16 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
| 17 #include "content/public/browser/notification_details.h" | 16 #include "content/public/browser/notification_details.h" |
| 18 #include "content/public/browser/notification_source.h" | 17 #include "content/public/browser/notification_source.h" |
| 19 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/render_widget_host_view.h" | 19 #include "content/public/browser/render_widget_host_view.h" |
| 21 #include "content/public/browser/web_contents_observer.h" | 20 #include "content/public/browser/web_contents_observer.h" |
| 22 #include "content/public/browser/web_ui_controller.h" | 21 #include "content/public/browser/web_ui_controller.h" |
| 22 #include "content/public/browser/web_ui_controller_factory.h" |
| 23 #include "content/public/common/bindings_policy.h" | 23 #include "content/public/common/bindings_policy.h" |
| 24 #include "content/public/common/content_constants.h" | 24 #include "content/public/common/content_constants.h" |
| 25 #include "content/public/common/url_constants.h" | 25 #include "content/public/common/url_constants.h" |
| 26 #include "content/public/test/mock_render_process_host.h" | 26 #include "content/public/test/mock_render_process_host.h" |
| 27 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread.h" |
| 28 #include "content/public/test/test_utils.h" | 28 #include "content/public/test/test_utils.h" |
| 29 #include "content/test/test_content_browser_client.h" | 29 #include "content/test/test_content_browser_client.h" |
| 30 #include "content/test/test_content_client.h" | 30 #include "content/test/test_content_client.h" |
| 31 #include "googleurl/src/url_util.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "webkit/glue/webkit_glue.h" | 33 #include "webkit/glue/webkit_glue.h" |
| 33 | 34 |
| 34 namespace content { | 35 namespace content { |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 const char kTestWebUIUrl[] = "chrome://blah"; | |
| 38 | |
| 39 class WebContentsImplTestWebUIControllerFactory | 38 class WebContentsImplTestWebUIControllerFactory |
| 40 : public WebUIControllerFactory { | 39 : public WebUIControllerFactory { |
| 41 public: | 40 public: |
| 42 virtual WebUIController* CreateWebUIControllerForURL( | 41 virtual WebUIController* CreateWebUIControllerForURL( |
| 43 WebUI* web_ui, const GURL& url) const OVERRIDE { | 42 WebUI* web_ui, const GURL& url) const OVERRIDE { |
| 44 if (!UseWebUI(url)) | 43 if (!GetContentClient()->HasWebUIScheme(url)) |
| 45 return NULL; | 44 return NULL; |
| 46 | 45 |
| 47 return new WebUIController(web_ui); | 46 return new WebUIController(web_ui); |
| 48 } | 47 } |
| 49 | 48 |
| 50 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, | 49 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, |
| 51 const GURL& url) const OVERRIDE { | 50 const GURL& url) const OVERRIDE { |
| 52 return WebUI::kNoWebUI; | 51 return WebUI::kNoWebUI; |
| 53 } | 52 } |
| 54 | 53 |
| 55 virtual bool UseWebUIForURL(BrowserContext* browser_context, | 54 virtual bool UseWebUIForURL(BrowserContext* browser_context, |
| 56 const GURL& url) const OVERRIDE { | 55 const GURL& url) const OVERRIDE { |
| 57 return UseWebUI(url); | 56 return GetContentClient()->HasWebUIScheme(url); |
| 58 } | 57 } |
| 59 | 58 |
| 60 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, | 59 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, |
| 61 const GURL& url) const OVERRIDE { | 60 const GURL& url) const OVERRIDE { |
| 62 return UseWebUI(url); | 61 return GetContentClient()->HasWebUIScheme(url); |
| 63 } | 62 } |
| 64 | 63 |
| 65 virtual bool IsURLAcceptableForWebUI( | 64 virtual bool IsURLAcceptableForWebUI( |
| 66 BrowserContext* browser_context, | 65 BrowserContext* browser_context, |
| 67 const GURL& url, | 66 const GURL& url, |
| 68 bool data_urls_allowed) const { | 67 bool data_urls_allowed) const { |
| 69 return UseWebUI(url); | 68 return GetContentClient()->HasWebUIScheme(url); |
| 69 } |
| 70 }; |
| 71 |
| 72 class WebContentsImplTestContentClient : public TestContentClient { |
| 73 public: |
| 74 WebContentsImplTestContentClient() { |
| 75 } |
| 76 |
| 77 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE { |
| 78 return url.SchemeIs("webcontentsimpltest"); |
| 79 } |
| 80 }; |
| 81 |
| 82 class WebContentsImplTestBrowserClient : public TestContentBrowserClient { |
| 83 public: |
| 84 WebContentsImplTestBrowserClient() { |
| 85 } |
| 86 |
| 87 virtual WebUIControllerFactory* GetWebUIControllerFactory() OVERRIDE { |
| 88 return &factory_; |
| 70 } | 89 } |
| 71 | 90 |
| 72 private: | 91 private: |
| 73 bool UseWebUI(const GURL& url) const { | 92 WebContentsImplTestWebUIControllerFactory factory_; |
| 74 return url == GURL(kTestWebUIUrl); | |
| 75 } | |
| 76 }; | 93 }; |
| 77 | 94 |
| 78 class TestInterstitialPage; | 95 class TestInterstitialPage; |
| 79 | 96 |
| 80 class TestInterstitialPageDelegate : public InterstitialPageDelegate { | 97 class TestInterstitialPageDelegate : public InterstitialPageDelegate { |
| 81 public: | 98 public: |
| 82 TestInterstitialPageDelegate(TestInterstitialPage* interstitial_page) | 99 TestInterstitialPageDelegate(TestInterstitialPage* interstitial_page) |
| 83 : interstitial_page_(interstitial_page) {} | 100 : interstitial_page_(interstitial_page) {} |
| 84 virtual void CommandReceived(const std::string& command) OVERRIDE; | 101 virtual void CommandReceived(const std::string& command) OVERRIDE; |
| 85 virtual std::string GetHTMLContents() OVERRIDE { return std::string(); } | 102 virtual std::string GetHTMLContents() OVERRIDE { return std::string(); } |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 interstitial_page_ = NULL; | 250 interstitial_page_ = NULL; |
| 234 } | 251 } |
| 235 | 252 |
| 236 private: | 253 private: |
| 237 TestInterstitialPage* interstitial_page_; | 254 TestInterstitialPage* interstitial_page_; |
| 238 }; | 255 }; |
| 239 | 256 |
| 240 class WebContentsImplTest : public RenderViewHostImplTestHarness { | 257 class WebContentsImplTest : public RenderViewHostImplTestHarness { |
| 241 public: | 258 public: |
| 242 WebContentsImplTest() | 259 WebContentsImplTest() |
| 243 : ui_thread_(BrowserThread::UI, &message_loop_), | 260 : old_client_(NULL), |
| 261 old_browser_client_(NULL), |
| 262 ui_thread_(BrowserThread::UI, &message_loop_), |
| 244 file_user_blocking_thread_( | 263 file_user_blocking_thread_( |
| 245 BrowserThread::FILE_USER_BLOCKING, &message_loop_), | 264 BrowserThread::FILE_USER_BLOCKING, &message_loop_), |
| 246 io_thread_(BrowserThread::IO, &message_loop_) { | 265 io_thread_(BrowserThread::IO, &message_loop_) { |
| 247 } | 266 } |
| 248 | 267 |
| 249 virtual void SetUp() { | 268 virtual void SetUp() { |
| 269 // These tests treat webcontentsimpltest as a privileged WebUI scheme. |
| 270 // We must register it similarly to kChromeUIScheme. |
| 271 url_util::AddStandardScheme("webcontentsimpltest"); |
| 272 |
| 273 old_client_ = GetContentClient(); |
| 274 old_browser_client_ = GetContentClient()->browser(); |
| 275 SetContentClient(&client_); |
| 276 GetContentClient()->set_browser_for_testing(&browser_client_); |
| 250 RenderViewHostImplTestHarness::SetUp(); | 277 RenderViewHostImplTestHarness::SetUp(); |
| 251 WebUIControllerFactory::RegisterFactory(&factory_); | |
| 252 } | 278 } |
| 253 | 279 |
| 254 virtual void TearDown() { | 280 virtual void TearDown() { |
| 281 GetContentClient()->set_browser_for_testing(old_browser_client_); |
| 282 SetContentClient(old_client_); |
| 255 RenderViewHostImplTestHarness::TearDown(); | 283 RenderViewHostImplTestHarness::TearDown(); |
| 256 WebUIControllerFactoryRegistry::UnregisterFactoryForTesting(&factory_); | |
| 257 } | 284 } |
| 258 | 285 |
| 259 private: | 286 private: |
| 260 WebContentsImplTestWebUIControllerFactory factory_; | 287 WebContentsImplTestContentClient client_; |
| 288 WebContentsImplTestBrowserClient browser_client_; |
| 289 ContentClient* old_client_; |
| 290 ContentBrowserClient* old_browser_client_; |
| 261 TestBrowserThread ui_thread_; | 291 TestBrowserThread ui_thread_; |
| 262 TestBrowserThread file_user_blocking_thread_; | 292 TestBrowserThread file_user_blocking_thread_; |
| 263 TestBrowserThread io_thread_; | 293 TestBrowserThread io_thread_; |
| 264 }; | 294 }; |
| 265 | 295 |
| 266 class TestWebContentsObserver : public WebContentsObserver { | 296 class TestWebContentsObserver : public WebContentsObserver { |
| 267 public: | 297 public: |
| 268 TestWebContentsObserver(WebContents* contents) | 298 TestWebContentsObserver(WebContents* contents) |
| 269 : WebContentsObserver(contents) { | 299 : WebContentsObserver(contents) { |
| 270 } | 300 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 | 338 |
| 309 contents()->UpdateTitle(rvh(), 0, ASCIIToUTF16(" Lots O' Whitespace\n"), | 339 contents()->UpdateTitle(rvh(), 0, ASCIIToUTF16(" Lots O' Whitespace\n"), |
| 310 base::i18n::LEFT_TO_RIGHT); | 340 base::i18n::LEFT_TO_RIGHT); |
| 311 EXPECT_EQ(ASCIIToUTF16("Lots O' Whitespace"), contents()->GetTitle()); | 341 EXPECT_EQ(ASCIIToUTF16("Lots O' Whitespace"), contents()->GetTitle()); |
| 312 } | 342 } |
| 313 | 343 |
| 314 // Test view source mode for a webui page. | 344 // Test view source mode for a webui page. |
| 315 TEST_F(WebContentsImplTest, NTPViewSource) { | 345 TEST_F(WebContentsImplTest, NTPViewSource) { |
| 316 NavigationControllerImpl& cont = | 346 NavigationControllerImpl& cont = |
| 317 static_cast<NavigationControllerImpl&>(controller()); | 347 static_cast<NavigationControllerImpl&>(controller()); |
| 318 const char kUrl[] = "view-source:chrome://blah"; | 348 const char kUrl[] = "view-source:webcontentsimpltest://blah"; |
| 319 const GURL kGURL(kUrl); | 349 const GURL kGURL(kUrl); |
| 320 | 350 |
| 321 process()->sink().ClearMessages(); | 351 process()->sink().ClearMessages(); |
| 322 | 352 |
| 323 cont.LoadURL( | 353 cont.LoadURL( |
| 324 kGURL, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 354 kGURL, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
| 325 rvh()->GetDelegate()->RenderViewCreated(rvh()); | 355 rvh()->GetDelegate()->RenderViewCreated(rvh()); |
| 326 // Did we get the expected message? | 356 // Did we get the expected message? |
| 327 EXPECT_TRUE(process()->sink().GetFirstMessageMatching( | 357 EXPECT_TRUE(process()->sink().GetFirstMessageMatching( |
| 328 ViewMsg_EnableViewSourceMode::ID)); | 358 ViewMsg_EnableViewSourceMode::ID)); |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 EXPECT_FALSE(contents()->cross_navigation_pending()); | 752 EXPECT_FALSE(contents()->cross_navigation_pending()); |
| 723 EXPECT_EQ(orig_rvh, rvh()); | 753 EXPECT_EQ(orig_rvh, rvh()); |
| 724 EXPECT_EQ(instance1, instance2); | 754 EXPECT_EQ(instance1, instance2); |
| 725 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL); | 755 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL); |
| 726 } | 756 } |
| 727 | 757 |
| 728 TEST_F(WebContentsImplTest, CrossSiteNavigationBackPreempted) { | 758 TEST_F(WebContentsImplTest, CrossSiteNavigationBackPreempted) { |
| 729 contents()->transition_cross_site = true; | 759 contents()->transition_cross_site = true; |
| 730 | 760 |
| 731 // Start with a web ui page, which gets a new RVH with WebUI bindings. | 761 // Start with a web ui page, which gets a new RVH with WebUI bindings. |
| 732 const GURL url1("chrome://blah"); | 762 const GURL url1("webcontentsimpltest://blah"); |
| 733 controller().LoadURL( | 763 controller().LoadURL( |
| 734 url1, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 764 url1, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
| 735 TestRenderViewHost* ntp_rvh = test_rvh(); | 765 TestRenderViewHost* ntp_rvh = test_rvh(); |
| 736 contents()->TestDidNavigate(ntp_rvh, 1, url1, PAGE_TRANSITION_TYPED); | 766 contents()->TestDidNavigate(ntp_rvh, 1, url1, PAGE_TRANSITION_TYPED); |
| 737 NavigationEntry* entry1 = controller().GetLastCommittedEntry(); | 767 NavigationEntry* entry1 = controller().GetLastCommittedEntry(); |
| 738 SiteInstance* instance1 = contents()->GetSiteInstance(); | 768 SiteInstance* instance1 = contents()->GetSiteInstance(); |
| 739 | 769 |
| 740 EXPECT_FALSE(contents()->cross_navigation_pending()); | 770 EXPECT_FALSE(contents()->cross_navigation_pending()); |
| 741 EXPECT_EQ(ntp_rvh, contents()->GetRenderViewHost()); | 771 EXPECT_EQ(ntp_rvh, contents()->GetRenderViewHost()); |
| 742 EXPECT_EQ(url1, entry1->GetURL()); | 772 EXPECT_EQ(url1, entry1->GetURL()); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 } | 889 } |
| 860 | 890 |
| 861 // Test that a cross-site navigation is not preempted if the previous | 891 // Test that a cross-site navigation is not preempted if the previous |
| 862 // renderer sends a FrameNavigate message just before being told to stop. | 892 // renderer sends a FrameNavigate message just before being told to stop. |
| 863 // We should only preempt the cross-site navigation if the previous renderer | 893 // We should only preempt the cross-site navigation if the previous renderer |
| 864 // has started a new navigation. See http://crbug.com/79176. | 894 // has started a new navigation. See http://crbug.com/79176. |
| 865 TEST_F(WebContentsImplTest, CrossSiteNotPreemptedDuringBeforeUnload) { | 895 TEST_F(WebContentsImplTest, CrossSiteNotPreemptedDuringBeforeUnload) { |
| 866 contents()->transition_cross_site = true; | 896 contents()->transition_cross_site = true; |
| 867 | 897 |
| 868 // Navigate to NTP URL. | 898 // Navigate to NTP URL. |
| 869 const GURL url("chrome://blah"); | 899 const GURL url("webcontentsimpltest://blah"); |
| 870 controller().LoadURL( | 900 controller().LoadURL( |
| 871 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 901 url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
| 872 TestRenderViewHost* orig_rvh = test_rvh(); | 902 TestRenderViewHost* orig_rvh = test_rvh(); |
| 873 EXPECT_FALSE(contents()->cross_navigation_pending()); | 903 EXPECT_FALSE(contents()->cross_navigation_pending()); |
| 874 | 904 |
| 875 // Navigate to new site, with the beforeunload request in flight. | 905 // Navigate to new site, with the beforeunload request in flight. |
| 876 const GURL url2("http://www.yahoo.com"); | 906 const GURL url2("http://www.yahoo.com"); |
| 877 controller().LoadURL( | 907 controller().LoadURL( |
| 878 url2, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 908 url2, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
| 879 TestRenderViewHost* pending_rvh = | 909 TestRenderViewHost* pending_rvh = |
| 880 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost()); | 910 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost()); |
| 881 EXPECT_TRUE(contents()->cross_navigation_pending()); | 911 EXPECT_TRUE(contents()->cross_navigation_pending()); |
| 882 EXPECT_TRUE(orig_rvh->is_waiting_for_beforeunload_ack()); | 912 EXPECT_TRUE(orig_rvh->is_waiting_for_beforeunload_ack()); |
| 883 | 913 |
| 884 // Suppose the first navigation tries to commit now, with a | 914 // Suppose the first navigation tries to commit now, with a |
| 885 // ViewMsg_Stop in flight. This should not cancel the pending navigation, | 915 // ViewMsg_Stop in flight. This should not cancel the pending navigation, |
| 886 // but it should act as if the beforeunload ack arrived. | 916 // but it should act as if the beforeunload ack arrived. |
| 887 orig_rvh->SendNavigate(1, GURL("chrome://blah")); | 917 orig_rvh->SendNavigate(1, GURL("webcontentsimpltest://blah")); |
| 888 EXPECT_TRUE(contents()->cross_navigation_pending()); | 918 EXPECT_TRUE(contents()->cross_navigation_pending()); |
| 889 EXPECT_EQ(orig_rvh, contents()->GetRenderViewHost()); | 919 EXPECT_EQ(orig_rvh, contents()->GetRenderViewHost()); |
| 890 EXPECT_FALSE(orig_rvh->is_waiting_for_beforeunload_ack()); | 920 EXPECT_FALSE(orig_rvh->is_waiting_for_beforeunload_ack()); |
| 891 | 921 |
| 892 // The pending navigation should be able to commit successfully. | 922 // The pending navigation should be able to commit successfully. |
| 893 contents()->TestDidNavigate(pending_rvh, 1, url2, PAGE_TRANSITION_TYPED); | 923 contents()->TestDidNavigate(pending_rvh, 1, url2, PAGE_TRANSITION_TYPED); |
| 894 EXPECT_FALSE(contents()->cross_navigation_pending()); | 924 EXPECT_FALSE(contents()->cross_navigation_pending()); |
| 895 EXPECT_EQ(pending_rvh, contents()->GetRenderViewHost()); | 925 EXPECT_EQ(pending_rvh, contents()->GetRenderViewHost()); |
| 896 } | 926 } |
| 897 | 927 |
| (...skipping 1079 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1977 other_contents->NavigateAndCommit(url_normalized); | 2007 other_contents->NavigateAndCommit(url_normalized); |
| 1978 | 2008 |
| 1979 // Check that an IPC with about:whatever is correctly normalized. | 2009 // Check that an IPC with about:whatever is correctly normalized. |
| 1980 other_contents->TestDidFailLoadWithError( | 2010 other_contents->TestDidFailLoadWithError( |
| 1981 1, url_from_ipc, true, 1, string16()); | 2011 1, url_from_ipc, true, 1, string16()); |
| 1982 EXPECT_EQ(url_normalized, other_observer.last_url()); | 2012 EXPECT_EQ(url_normalized, other_observer.last_url()); |
| 1983 } | 2013 } |
| 1984 | 2014 |
| 1985 | 2015 |
| 1986 } // namespace content | 2016 } // namespace content |
| OLD | NEW |