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

Side by Side Diff: chrome/browser/tab_contents/web_contents_unittest.cc

Issue 18351: Put back render view host tests (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "chrome/browser/render_view_host.h" 6 #include "chrome/browser/render_view_host.h"
7 #include "chrome/browser/renderer_host/test_render_view_host.h"
7 #include "chrome/browser/render_widget_host_view.h" 8 #include "chrome/browser/render_widget_host_view.h"
8 #include "chrome/browser/tab_contents/interstitial_page.h" 9 #include "chrome/browser/tab_contents/interstitial_page.h"
9 #include "chrome/browser/tab_contents/navigation_controller.h" 10 #include "chrome/browser/tab_contents/navigation_controller.h"
10 #include "chrome/browser/tab_contents/navigation_entry.h" 11 #include "chrome/browser/tab_contents/navigation_entry.h"
11 #include "chrome/browser/tab_contents/web_contents.h" 12 #include "chrome/browser/tab_contents/web_contents.h"
12 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/ipc_channel.h" 14 #include "chrome/common/ipc_channel.h"
14 #include "chrome/common/pref_service.h" 15 #include "chrome/common/pref_service.h"
15 #include "chrome/common/render_messages.h" 16 #include "chrome/common/render_messages.h"
16 #include "chrome/test/testing_profile.h" 17 #include "chrome/test/testing_profile.h"
(...skipping 10 matching lines...) Expand all
27 params->should_update_history = false; 28 params->should_update_history = false;
28 params->searchable_form_url = GURL::EmptyGURL(); 29 params->searchable_form_url = GURL::EmptyGURL();
29 params->searchable_form_element_name = std::wstring(); 30 params->searchable_form_element_name = std::wstring();
30 params->searchable_form_encoding = std::string(); 31 params->searchable_form_encoding = std::string();
31 params->password_form = PasswordForm(); 32 params->password_form = PasswordForm();
32 params->security_info = std::string(); 33 params->security_info = std::string();
33 params->gesture = NavigationGestureUser; 34 params->gesture = NavigationGestureUser;
34 params->is_post = false; 35 params->is_post = false;
35 } 36 }
36 37
37 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
38 // without having side-effects.
39 class TestRenderWidgetHostView : public RenderWidgetHostView {
40 public:
41 TestRenderWidgetHostView() : is_showing_(false) {}
42
43 virtual RenderWidgetHost* GetRenderWidgetHost() const { return NULL; }
44 virtual void DidBecomeSelected() {}
45 virtual void WasHidden() {}
46 virtual void SetSize(const gfx::Size& size) {}
47 virtual HWND GetPluginHWND() { return NULL; }
48 virtual HANDLE ModalDialogEvent() { return NULL; }
49 virtual void ForwardMouseEventToRenderer(UINT message,
50 WPARAM wparam,
51 LPARAM lparam) {}
52 virtual void Focus() {}
53 virtual void Blur() {}
54 virtual bool HasFocus() { return true; }
55 virtual void AdvanceFocus(bool reverse) {}
56 virtual void Show() { is_showing_ = true; }
57 virtual void Hide() { is_showing_ = false; }
58 virtual gfx::Rect GetViewBounds() const { return gfx::Rect(); }
59 virtual void UpdateCursor(const WebCursor& cursor) {}
60 virtual void UpdateCursorIfOverSelf() {}
61 // Indicates if the page has finished loading.
62 virtual void SetIsLoading(bool is_loading) {}
63 virtual void IMEUpdateStatus(ViewHostMsg_ImeControl control,
64 const gfx::Rect& caret_rect) {}
65 virtual void DidPaintRect(const gfx::Rect& rect) {}
66 virtual void DidScrollRect(const gfx::Rect& rect, int dx, int dy) {}
67 virtual void RendererGone() {}
68 virtual void Destroy() {}
69 virtual void PrepareToDestroy() {}
70 virtual void SetTooltipText(const std::wstring& tooltip_text) {}
71
72 bool is_showing() const { return is_showing_; }
73
74 private:
75 bool is_showing_;
76 };
77
78 // Subclass RenderViewHost so that it does not create a process.
79 class TestRenderViewHost : public RenderViewHost {
80 public:
81 TestRenderViewHost(
82 SiteInstance* instance,
83 RenderViewHostDelegate* delegate,
84 int routing_id,
85 base::WaitableEvent* modal_dialog_event)
86 : RenderViewHost(instance, delegate, routing_id, modal_dialog_event),
87 is_loading(false),
88 is_created(false),
89 immediate_before_unload(true),
90 delete_counter_(NULL) {
91 set_view(new TestRenderWidgetHostView());
92 }
93 ~TestRenderViewHost() {
94 // Track the delete if we've been asked to.
95 if (delete_counter_)
96 ++*delete_counter_;
97
98 // Since this isn't a traditional view, we have to delete it.
99 delete view_;
100 }
101
102 // If set, *delete_counter is incremented when this object destructs.
103 void set_delete_counter(int* delete_counter) {
104 delete_counter_ = delete_counter;
105 }
106
107 bool CreateRenderView() {
108 is_created = true;
109 return true;
110 }
111
112 bool IsRenderViewLive() const { return is_created; }
113
114 bool IsNavigationSuspended() { return navigations_suspended_; }
115
116 void NavigateToEntry(const NavigationEntry& entry, bool is_reload) {
117 is_loading = true;
118 }
119
120 void LoadAlternateHTMLString(const std::string& html_text,
121 bool new_navigation,
122 const GURL& display_url,
123 const std::string& security_info) {
124 is_loading = true;
125 }
126
127 // Support for onbeforeunload, onunload
128 void FirePageBeforeUnload() {
129 is_waiting_for_unload_ack_ = true;
130 if (immediate_before_unload)
131 delegate()->ShouldClosePage(true);
132 }
133 void ClosePage(int new_render_process_host_id, int new_request_id) {
134 // Nothing to do here... This would cause a ClosePage_ACK to be sent to
135 // ResourceDispatcherHost, so we can simulate that manually.
136 }
137 void TestOnMsgShouldClose(bool proceed) {
138 OnMsgShouldCloseACK(proceed);
139 }
140
141 bool is_loading;
142 bool is_created;
143 bool immediate_before_unload;
144 int* delete_counter_;
145 };
146
147 // Factory to create TestRenderViewHosts.
148 class TestRenderViewHostFactory : public RenderViewHostFactory {
149 public:
150 static TestRenderViewHostFactory* GetInstance() {
151 static TestRenderViewHostFactory instance;
152 return &instance;
153 }
154
155 virtual RenderViewHost* CreateRenderViewHost(
156 SiteInstance* instance,
157 RenderViewHostDelegate* delegate,
158 int routing_id,
159 base::WaitableEvent* modal_dialog_event) {
160 return new TestRenderViewHost(
161 instance, delegate, routing_id, modal_dialog_event);
162 }
163
164 private:
165 TestRenderViewHostFactory() {}
166 };
167
168 // Subclass the TestingProfile so that it can return certain services we need. 38 // Subclass the TestingProfile so that it can return certain services we need.
169 class WebContentsTestingProfile : public TestingProfile { 39 class WebContentsTestingProfile : public TestingProfile {
170 public: 40 public:
171 WebContentsTestingProfile() : TestingProfile() { } 41 WebContentsTestingProfile() : TestingProfile() { }
172 42
173 virtual PrefService* GetPrefs() { 43 virtual PrefService* GetPrefs() {
174 if (!prefs_.get()) { 44 if (!prefs_.get()) {
175 std::wstring source_path; 45 std::wstring source_path;
176 PathService::Get(chrome::DIR_TEST_DATA, &source_path); 46 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
177 file_util::AppendToPath(&source_path, L"profiles"); 47 file_util::AppendToPath(&source_path, L"profiles");
178 file_util::AppendToPath(&source_path, L"chrome_prefs"); 48 file_util::AppendToPath(&source_path, L"chrome_prefs");
179 file_util::AppendToPath(&source_path, L"Preferences"); 49 file_util::AppendToPath(&source_path, L"Preferences");
180 50
181 prefs_.reset(new PrefService(source_path)); 51 prefs_.reset(new PrefService(source_path));
182 Profile::RegisterUserPrefs(prefs_.get()); 52 Profile::RegisterUserPrefs(prefs_.get());
183 browser::RegisterAllPrefs(prefs_.get(), prefs_.get()); 53 browser::RegisterAllPrefs(prefs_.get(), prefs_.get());
184 } 54 }
185 return prefs_.get(); 55 return prefs_.get();
186 } 56 }
187 }; 57 };
188 58
189 // Subclass WebContents to ensure it creates TestRenderViewHosts and does 59 // Subclass WebContents to ensure it creates TestRenderViewHosts and does
190 // not do anything involving views. 60 // not do anything involving views.
61 //
62 // TODO(brettw) merge this with the TestRenderProcessHost to it can be used by
63 // other tests as well.
191 class TestWebContents : public WebContents { 64 class TestWebContents : public WebContents {
192 public: 65 public:
193 TestWebContents(Profile* profile, SiteInstance* instance) 66 TestWebContents(Profile* profile, SiteInstance* instance)
194 : WebContents(profile, 67 : WebContents(profile,
195 instance, 68 instance,
196 TestRenderViewHostFactory::GetInstance(), 69 TestRenderViewHostFactory::GetInstance(),
197 MSG_ROUTING_NONE, 70 MSG_ROUTING_NONE,
198 NULL), 71 NULL),
199 transition_cross_site(false) {} 72 transition_cross_site(false) {}
200 73
201 // Accessors for interesting fields 74 // Accessors for interesting fields
202 TestRenderViewHost* rvh() { 75 TestRenderViewHost* rvh() {
203 return static_cast<TestRenderViewHost*>( 76 return static_cast<TestRenderViewHost*>(
204 render_manager_.render_view_host_); 77 render_manager_.render_view_host_);
205 } 78 }
206 TestRenderViewHost* pending_rvh() { 79 TestRenderViewHost* pending_rvh() {
207 return static_cast<TestRenderViewHost*>( 80 return static_cast<TestRenderViewHost*>(
208 render_manager_.pending_render_view_host_); 81 render_manager_.pending_render_view_host_);
209 } 82 }
210 83
211 // State accessor. 84 // State accessor.
212 bool cross_navigation_pending() { 85 bool cross_navigation_pending() {
213 return render_manager_.cross_navigation_pending_; 86 return render_manager_.cross_navigation_pending_;
214 } 87 }
215 88
216 // Ensure we create TestRenderViewHosts that don't spawn processes.
217 RenderViewHost* CreateRenderViewHost(SiteInstance* instance,
218 RenderViewHostDelegate* delegate,
219 int routing_id,
220 base::WaitableEvent* modal_dialog_event) {
221 return new TestRenderViewHost(
222 instance, delegate, routing_id, modal_dialog_event);
223 }
224
225 // Overrides WebContents::ShouldTransitionCrossSite so that we can test both 89 // Overrides WebContents::ShouldTransitionCrossSite so that we can test both
226 // alternatives without using command-line switches. 90 // alternatives without using command-line switches.
227 bool ShouldTransitionCrossSite() { return transition_cross_site; } 91 bool ShouldTransitionCrossSite() { return transition_cross_site; }
228 92
229 // Promote DidNavigate to public. 93 // Promote DidNavigate to public.
230 void TestDidNavigate(TestRenderViewHost* render_view_host, 94 void TestDidNavigate(TestRenderViewHost* render_view_host,
231 const ViewHostMsg_FrameNavigate_Params& params) { 95 const ViewHostMsg_FrameNavigate_Params& params) {
232 DidNavigate(render_view_host, params); 96 DidNavigate(render_view_host, params);
233 render_view_host->is_loading = false;
234 } 97 }
235 98
236 // Promote GetWebkitPrefs to public. 99 // Promote GetWebkitPrefs to public.
237 WebPreferences TestGetWebkitPrefs() { 100 WebPreferences TestGetWebkitPrefs() {
238 return GetWebkitPrefs(); 101 return GetWebkitPrefs();
239 } 102 }
240 103
241 // Prevent interaction with views. 104 // Prevent interaction with views.
242 bool CreateRenderViewForRenderManager(RenderViewHost* render_view_host) { 105 bool CreateRenderViewForRenderManager(RenderViewHost* render_view_host) {
243 // This will go to a TestRenderViewHost. 106 // This will go to a TestRenderViewHost.
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 291
429 contents->UpdateTitle(contents->rvh(), 0, L" Lots O' Whitespace\n"); 292 contents->UpdateTitle(contents->rvh(), 0, L" Lots O' Whitespace\n");
430 EXPECT_EQ(std::wstring(L"Lots O' Whitespace"), contents->GetTitle()); 293 EXPECT_EQ(std::wstring(L"Lots O' Whitespace"), contents->GetTitle());
431 } 294 }
432 295
433 // Test simple same-SiteInstance navigation. 296 // Test simple same-SiteInstance navigation.
434 TEST_F(WebContentsTest, SimpleNavigation) { 297 TEST_F(WebContentsTest, SimpleNavigation) {
435 TestRenderViewHost* orig_rvh = contents->rvh(); 298 TestRenderViewHost* orig_rvh = contents->rvh();
436 SiteInstance* instance1 = contents->GetSiteInstance(); 299 SiteInstance* instance1 = contents->GetSiteInstance();
437 EXPECT_TRUE(contents->pending_rvh() == NULL); 300 EXPECT_TRUE(contents->pending_rvh() == NULL);
438 EXPECT_FALSE(orig_rvh->is_loading);
439 301
440 // Navigate to URL 302 // Navigate to URL
441 const GURL url("http://www.google.com"); 303 const GURL url("http://www.google.com");
442 contents->controller()->LoadURL(url, GURL(), PageTransition::TYPED); 304 contents->controller()->LoadURL(url, GURL(), PageTransition::TYPED);
443 EXPECT_FALSE(contents->cross_navigation_pending()); 305 EXPECT_FALSE(contents->cross_navigation_pending());
444 EXPECT_TRUE(orig_rvh->is_loading);
445 EXPECT_EQ(instance1, orig_rvh->site_instance()); 306 EXPECT_EQ(instance1, orig_rvh->site_instance());
446 // Controller's pending entry will have a NULL site instance until we assign 307 // Controller's pending entry will have a NULL site instance until we assign
447 // it in DidNavigate. 308 // it in DidNavigate.
448 EXPECT_TRUE( 309 EXPECT_TRUE(
449 contents->controller()->GetActiveEntry()->site_instance() == NULL); 310 contents->controller()->GetActiveEntry()->site_instance() == NULL);
450 311
451 // DidNavigate from the page 312 // DidNavigate from the page
452 ViewHostMsg_FrameNavigate_Params params; 313 ViewHostMsg_FrameNavigate_Params params;
453 InitNavigateParams(&params, 1, url); 314 InitNavigateParams(&params, 1, url);
454 contents->TestDidNavigate(orig_rvh, params); 315 contents->TestDidNavigate(orig_rvh, params);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 const GURL url("http://www.google.com"); 388 const GURL url("http://www.google.com");
528 contents->controller()->LoadURL(url, GURL(), PageTransition::TYPED); 389 contents->controller()->LoadURL(url, GURL(), PageTransition::TYPED);
529 ViewHostMsg_FrameNavigate_Params params1; 390 ViewHostMsg_FrameNavigate_Params params1;
530 InitNavigateParams(&params1, 1, url); 391 InitNavigateParams(&params1, 1, url);
531 contents->TestDidNavigate(orig_rvh, params1); 392 contents->TestDidNavigate(orig_rvh, params1);
532 393
533 EXPECT_FALSE(contents->cross_navigation_pending()); 394 EXPECT_FALSE(contents->cross_navigation_pending());
534 EXPECT_EQ(orig_rvh, contents->render_view_host()); 395 EXPECT_EQ(orig_rvh, contents->render_view_host());
535 396
536 // Crash the renderer. 397 // Crash the renderer.
537 orig_rvh->is_created = false; 398 orig_rvh->set_render_view_created(false);
538 399
539 // Navigate to new site. We should not go into PENDING. 400 // Navigate to new site. We should not go into PENDING.
540 const GURL url2("http://www.yahoo.com"); 401 const GURL url2("http://www.yahoo.com");
541 contents->controller()->LoadURL(url2, GURL(), PageTransition::TYPED); 402 contents->controller()->LoadURL(url2, GURL(), PageTransition::TYPED);
542 TestRenderViewHost* new_rvh = contents->rvh(); 403 TestRenderViewHost* new_rvh = contents->rvh();
543 EXPECT_FALSE(contents->cross_navigation_pending()); 404 EXPECT_FALSE(contents->cross_navigation_pending());
544 EXPECT_TRUE(contents->pending_rvh() == NULL); 405 EXPECT_TRUE(contents->pending_rvh() == NULL);
545 EXPECT_NE(orig_rvh, new_rvh); 406 EXPECT_NE(orig_rvh, new_rvh);
546 EXPECT_EQ(orig_rvh_delete_count, 1); 407 EXPECT_EQ(orig_rvh_delete_count, 1);
547 408
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 const GURL url("http://www.google.com"); 540 const GURL url("http://www.google.com");
680 contents->controller()->LoadURL(url, GURL(), PageTransition::TYPED); 541 contents->controller()->LoadURL(url, GURL(), PageTransition::TYPED);
681 ViewHostMsg_FrameNavigate_Params params1; 542 ViewHostMsg_FrameNavigate_Params params1;
682 InitNavigateParams(&params1, 1, url); 543 InitNavigateParams(&params1, 1, url);
683 contents->TestDidNavigate(orig_rvh, params1); 544 contents->TestDidNavigate(orig_rvh, params1);
684 EXPECT_FALSE(contents->cross_navigation_pending()); 545 EXPECT_FALSE(contents->cross_navigation_pending());
685 EXPECT_EQ(orig_rvh, contents->render_view_host()); 546 EXPECT_EQ(orig_rvh, contents->render_view_host());
686 547
687 // Navigate to new site, but simulate an onbeforeunload denial. 548 // Navigate to new site, but simulate an onbeforeunload denial.
688 const GURL url2("http://www.yahoo.com"); 549 const GURL url2("http://www.yahoo.com");
689 orig_rvh->immediate_before_unload = false;
690 contents->controller()->LoadURL(url2, GURL(), PageTransition::TYPED); 550 contents->controller()->LoadURL(url2, GURL(), PageTransition::TYPED);
691 orig_rvh->TestOnMsgShouldClose(false); 551 orig_rvh->TestOnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, false));
692 EXPECT_FALSE(contents->cross_navigation_pending()); 552 EXPECT_FALSE(contents->cross_navigation_pending());
693 EXPECT_EQ(orig_rvh, contents->render_view_host()); 553 EXPECT_EQ(orig_rvh, contents->render_view_host());
694 554
695 // Navigate again, but simulate an onbeforeunload approval. 555 // Navigate again, but simulate an onbeforeunload approval.
696 contents->controller()->LoadURL(url2, GURL(), PageTransition::TYPED); 556 contents->controller()->LoadURL(url2, GURL(), PageTransition::TYPED);
697 orig_rvh->TestOnMsgShouldClose(true); 557 orig_rvh->TestOnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true));
698 EXPECT_TRUE(contents->cross_navigation_pending()); 558 EXPECT_TRUE(contents->cross_navigation_pending());
699 TestRenderViewHost* pending_rvh = contents->pending_rvh(); 559 TestRenderViewHost* pending_rvh = contents->pending_rvh();
700 560
701 // We won't hear DidNavigate until the onunload handler has finished running. 561 // We won't hear DidNavigate until the onunload handler has finished running.
702 // (No way to simulate that here, but it involves a call from RDH to 562 // (No way to simulate that here, but it involves a call from RDH to
703 // WebContents::OnCrossSiteResponse.) 563 // WebContents::OnCrossSiteResponse.)
704 564
705 // DidNavigate from the pending page 565 // DidNavigate from the pending page
706 ViewHostMsg_FrameNavigate_Params params2; 566 ViewHostMsg_FrameNavigate_Params params2;
707 InitNavigateParams(&params2, 1, url2); 567 InitNavigateParams(&params2, 1, url2);
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
1262 interstitial = 1122 interstitial =
1263 new TestInterstitialPage(contents, true, url, &state, &deleted); 1123 new TestInterstitialPage(contents, true, url, &state, &deleted);
1264 interstitial->Show(); 1124 interstitial->Show();
1265 interstitial->TestDidNavigate(1, url); 1125 interstitial->TestDidNavigate(1, url);
1266 // Simulate a renderer crash. 1126 // Simulate a renderer crash.
1267 interstitial->TestRendererGone(); 1127 interstitial->TestRendererGone();
1268 // The interstitial should have been dismissed. 1128 // The interstitial should have been dismissed.
1269 EXPECT_TRUE(deleted); 1129 EXPECT_TRUE(deleted);
1270 EXPECT_EQ(TestInterstitialPage::CANCELED, state); 1130 EXPECT_EQ(TestInterstitialPage::CANCELED, state);
1271 } 1131 }
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/test_render_view_host.cc ('k') | chrome/test/unit/unittests.vcproj » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698