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

Side by Side Diff: content/browser/web_contents/web_contents_impl_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698