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 "content/browser/browser_url_handler.h" | 5 #include "content/browser/browser_url_handler.h" |
6 #include "content/browser/renderer_host/test_backing_store.h" | 6 #include "content/browser/renderer_host/test_backing_store.h" |
7 #include "content/browser/renderer_host/test_render_view_host.h" | 7 #include "content/browser/renderer_host/test_render_view_host.h" |
8 #include "content/browser/site_instance_impl.h" | 8 #include "content/browser/site_instance_impl.h" |
9 #include "content/browser/tab_contents/navigation_controller_impl.h" | 9 #include "content/browser/tab_contents/navigation_controller_impl.h" |
10 #include "content/browser/tab_contents/navigation_entry_impl.h" | |
11 #include "content/browser/tab_contents/test_tab_contents.h" | 10 #include "content/browser/tab_contents/test_tab_contents.h" |
12 #include "content/common/dom_storage_common.h" | 11 #include "content/common/dom_storage_common.h" |
13 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
14 #include "content/public/browser/navigation_controller.h" | 13 #include "content/public/browser/navigation_controller.h" |
15 #include "content/public/common/content_client.h" | 14 #include "content/public/common/content_client.h" |
16 #include "content/test/test_browser_context.h" | |
17 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
18 #include "webkit/forms/password_form.h" | 16 #include "webkit/forms/password_form.h" |
19 #include "webkit/glue/webkit_glue.h" | 17 #include "webkit/glue/webkit_glue.h" |
20 #include "webkit/glue/webpreferences.h" | 18 #include "webkit/glue/webpreferences.h" |
21 | 19 |
22 #if defined(USE_AURA) | |
23 #include "ui/aura/root_window.h" | |
24 #include "ui/aura/test/test_stacking_client.h" | |
25 #endif | |
26 | |
27 using content::NavigationController; | |
28 using content::NavigationEntry; | |
29 using content::RenderViewHostDelegate; | |
30 using content::RenderWidgetHost; | |
31 using content::SessionStorageNamespace; | |
32 using content::SiteInstance; | |
33 using content::TestRenderViewHost; | |
34 using webkit::forms::PasswordForm; | 20 using webkit::forms::PasswordForm; |
35 | 21 |
36 namespace content { | 22 namespace content { |
37 | 23 |
38 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, | 24 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, |
39 int page_id, | 25 int page_id, |
40 const GURL& url, | 26 const GURL& url, |
41 PageTransition transition) { | 27 PageTransition transition) { |
42 params->page_id = page_id; | 28 params->page_id = page_id; |
43 params->url = url; | 29 params->url = url; |
44 params->referrer = Referrer(); | 30 params->referrer = Referrer(); |
45 params->transition = transition; | 31 params->transition = transition; |
46 params->redirects = std::vector<GURL>(); | 32 params->redirects = std::vector<GURL>(); |
47 params->should_update_history = false; | 33 params->should_update_history = false; |
48 params->searchable_form_url = GURL(); | 34 params->searchable_form_url = GURL(); |
49 params->searchable_form_encoding = std::string(); | 35 params->searchable_form_encoding = std::string(); |
50 params->password_form = PasswordForm(); | 36 params->password_form = PasswordForm(); |
51 params->security_info = std::string(); | 37 params->security_info = std::string(); |
52 params->gesture = NavigationGestureUser; | 38 params->gesture = NavigationGestureUser; |
53 params->was_within_same_page = false; | 39 params->was_within_same_page = false; |
54 params->is_post = false; | 40 params->is_post = false; |
55 params->content_state = webkit_glue::CreateHistoryStateForURL(GURL(url)); | 41 params->content_state = webkit_glue::CreateHistoryStateForURL(GURL(url)); |
56 } | 42 } |
57 | 43 |
58 TestRenderViewHost* TestRenderViewHost::GetPendingForController( | |
59 NavigationController* controller) { | |
60 TabContents* tab_contents = static_cast<TabContents*>( | |
61 controller->GetWebContents()); | |
62 return static_cast<TestRenderViewHost*>( | |
63 tab_contents->GetRenderManagerForTesting()->pending_render_view_host()); | |
64 } | |
65 | |
66 TestRenderViewHost::TestRenderViewHost(SiteInstance* instance, | |
67 RenderViewHostDelegate* delegate, | |
68 int routing_id) | |
69 : RenderViewHostImpl(instance, | |
70 delegate, | |
71 routing_id, | |
72 kInvalidSessionStorageNamespaceId), | |
73 render_view_created_(false), | |
74 delete_counter_(NULL), | |
75 simulate_fetch_via_proxy_(false), | |
76 contents_mime_type_("text/html") { | |
77 // For normal RenderViewHosts, this is freed when |Shutdown()| is called. | |
78 // For TestRenderViewHost, the view is explicitly deleted in the destructor | |
79 // below, because TestRenderWidgetHostView::Destroy() doesn't |delete this|. | |
80 SetView(new TestRenderWidgetHostView(this)); | |
81 } | |
82 | |
83 TestRenderViewHost::~TestRenderViewHost() { | |
84 if (delete_counter_) | |
85 ++*delete_counter_; | |
86 | |
87 // Since this isn't a traditional view, we have to delete it. | |
88 delete GetView(); | |
89 } | |
90 | |
91 bool TestRenderViewHost::CreateRenderView(const string16& frame_name, | |
92 int32 max_page_id) { | |
93 DCHECK(!render_view_created_); | |
94 render_view_created_ = true; | |
95 return true; | |
96 } | |
97 | |
98 bool TestRenderViewHost::IsRenderViewLive() const { | |
99 return render_view_created_; | |
100 } | |
101 | |
102 // static | |
103 bool TestRenderViewHost::IsRenderViewHostSwappedOut(RenderViewHost* rwh) { | |
104 return static_cast<RenderViewHostImpl*>(rwh)->is_swapped_out(); | |
105 } | |
106 | |
107 // static | |
108 void TestRenderViewHost::EnableAccessibilityUpdatedNotifications( | |
109 RenderViewHost* rvh) { | |
110 static_cast<RenderViewHostImpl*>( | |
111 rvh)->set_send_accessibility_updated_notifications(true); | |
112 } | |
113 | |
114 bool TestRenderViewHost::TestOnMessageReceived(const IPC::Message& msg) { | |
115 return OnMessageReceived(msg); | |
116 } | |
117 | |
118 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) { | |
119 SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK); | |
120 } | |
121 | |
122 void TestRenderViewHost::SendNavigateWithTransition( | |
123 int page_id, const GURL& url, PageTransition transition) { | |
124 ViewHostMsg_FrameNavigate_Params params; | |
125 | |
126 params.page_id = page_id; | |
127 params.url = url; | |
128 params.referrer = Referrer(); | |
129 params.transition = transition; | |
130 params.redirects = std::vector<GURL>(); | |
131 params.should_update_history = true; | |
132 params.searchable_form_url = GURL(); | |
133 params.searchable_form_encoding = std::string(); | |
134 params.password_form = PasswordForm(); | |
135 params.security_info = std::string(); | |
136 params.gesture = NavigationGestureUser; | |
137 params.contents_mime_type = contents_mime_type_; | |
138 params.is_post = false; | |
139 params.was_within_same_page = false; | |
140 params.http_status_code = 0; | |
141 params.socket_address.set_host("2001:db8::1"); | |
142 params.socket_address.set_port(80); | |
143 params.was_fetched_via_proxy = simulate_fetch_via_proxy_; | |
144 params.content_state = webkit_glue::CreateHistoryStateForURL(GURL(url)); | |
145 | |
146 ViewHostMsg_FrameNavigate msg(1, params); | |
147 OnMsgNavigate(msg); | |
148 } | |
149 | |
150 void TestRenderViewHost::SendShouldCloseACK(bool proceed) { | |
151 OnMsgShouldCloseACK(proceed, base::TimeTicks(), base::TimeTicks()); | |
152 } | |
153 | |
154 void TestRenderViewHost::TestOnMsgStartDragging(const WebDropData& drop_data) { | |
155 WebKit::WebDragOperationsMask drag_operation = WebKit::WebDragOperationEvery; | |
156 OnMsgStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Point()); | |
157 } | |
158 | |
159 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) { | |
160 simulate_fetch_via_proxy_ = proxy; | |
161 } | |
162 | |
163 void TestRenderViewHost::set_contents_mime_type(const std::string& mime_type) { | |
164 contents_mime_type_ = mime_type; | |
165 } | |
166 | |
167 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost* rwh) | 44 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost* rwh) |
168 : rwh_(RenderWidgetHostImpl::From(rwh)), | 45 : rwh_(RenderWidgetHostImpl::From(rwh)), |
169 is_showing_(false) { | 46 is_showing_(false) { |
170 } | 47 } |
171 | 48 |
172 TestRenderWidgetHostView::~TestRenderWidgetHostView() { | 49 TestRenderWidgetHostView::~TestRenderWidgetHostView() { |
173 } | 50 } |
174 | 51 |
175 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const { | 52 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const { |
176 return NULL; | 53 return NULL; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 return gfx::GLSurfaceHandle(); | 186 return gfx::GLSurfaceHandle(); |
310 } | 187 } |
311 | 188 |
312 bool TestRenderWidgetHostView::LockMouse() { | 189 bool TestRenderWidgetHostView::LockMouse() { |
313 return false; | 190 return false; |
314 } | 191 } |
315 | 192 |
316 void TestRenderWidgetHostView::UnlockMouse() { | 193 void TestRenderWidgetHostView::UnlockMouse() { |
317 } | 194 } |
318 | 195 |
319 } // namespace content | 196 TestRenderViewHost::TestRenderViewHost(SiteInstance* instance, |
320 | 197 RenderViewHostDelegate* delegate, |
321 TestRenderViewHostFactory::TestRenderViewHostFactory( | 198 int routing_id) |
322 content::RenderProcessHostFactory* rph_factory) | 199 : RenderViewHostImpl(instance, |
323 : render_process_host_factory_(rph_factory) { | 200 delegate, |
324 RenderViewHostFactory::RegisterFactory(this); | 201 routing_id, |
| 202 kInvalidSessionStorageNamespaceId), |
| 203 render_view_created_(false), |
| 204 delete_counter_(NULL), |
| 205 simulate_fetch_via_proxy_(false), |
| 206 contents_mime_type_("text/html") { |
| 207 // For normal RenderViewHosts, this is freed when |Shutdown()| is |
| 208 // called. For TestRenderViewHost, the view is explicitly |
| 209 // deleted in the destructor below, because |
| 210 // TestRenderWidgetHostView::Destroy() doesn't |delete this|. |
| 211 SetView(new TestRenderWidgetHostView(this)); |
325 } | 212 } |
326 | 213 |
327 TestRenderViewHostFactory::~TestRenderViewHostFactory() { | 214 TestRenderViewHost::~TestRenderViewHost() { |
328 RenderViewHostFactory::UnregisterFactory(); | 215 if (delete_counter_) |
| 216 ++*delete_counter_; |
| 217 |
| 218 // Since this isn't a traditional view, we have to delete it. |
| 219 delete GetView(); |
329 } | 220 } |
330 | 221 |
331 void TestRenderViewHostFactory::set_render_process_host_factory( | 222 bool TestRenderViewHost::CreateRenderView(const string16& frame_name, |
332 content::RenderProcessHostFactory* rph_factory) { | 223 int32 max_page_id) { |
333 render_process_host_factory_ = rph_factory; | 224 DCHECK(!render_view_created_); |
| 225 render_view_created_ = true; |
| 226 return true; |
334 } | 227 } |
335 | 228 |
336 content::RenderViewHost* TestRenderViewHostFactory::CreateRenderViewHost( | 229 bool TestRenderViewHost::IsRenderViewLive() const { |
337 SiteInstance* instance, | 230 return render_view_created_; |
338 RenderViewHostDelegate* delegate, | |
339 int routing_id, | |
340 SessionStorageNamespace* session_storage) { | |
341 // See declaration of render_process_host_factory_ below. | |
342 static_cast<SiteInstanceImpl*>(instance)-> | |
343 set_render_process_host_factory(render_process_host_factory_); | |
344 return new TestRenderViewHost(instance, delegate, routing_id); | |
345 } | 231 } |
346 | 232 |
347 RenderViewHostTestHarness::RenderViewHostTestHarness() | 233 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) { |
348 : rph_factory_(), | 234 SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK); |
349 rvh_factory_(&rph_factory_), | |
350 contents_(NULL) { | |
351 } | 235 } |
352 | 236 |
353 RenderViewHostTestHarness::~RenderViewHostTestHarness() { | 237 void TestRenderViewHost::SendNavigateWithTransition( |
| 238 int page_id, const GURL& url, PageTransition transition) { |
| 239 ViewHostMsg_FrameNavigate_Params params; |
| 240 |
| 241 params.page_id = page_id; |
| 242 params.url = url; |
| 243 params.referrer = Referrer(); |
| 244 params.transition = transition; |
| 245 params.redirects = std::vector<GURL>(); |
| 246 params.should_update_history = true; |
| 247 params.searchable_form_url = GURL(); |
| 248 params.searchable_form_encoding = std::string(); |
| 249 params.password_form = PasswordForm(); |
| 250 params.security_info = std::string(); |
| 251 params.gesture = NavigationGestureUser; |
| 252 params.contents_mime_type = contents_mime_type_; |
| 253 params.is_post = false; |
| 254 params.was_within_same_page = false; |
| 255 params.http_status_code = 0; |
| 256 params.socket_address.set_host("2001:db8::1"); |
| 257 params.socket_address.set_port(80); |
| 258 params.was_fetched_via_proxy = simulate_fetch_via_proxy_; |
| 259 params.content_state = webkit_glue::CreateHistoryStateForURL(GURL(url)); |
| 260 |
| 261 ViewHostMsg_FrameNavigate msg(1, params); |
| 262 OnMsgNavigate(msg); |
354 } | 263 } |
355 | 264 |
356 NavigationController& RenderViewHostTestHarness::controller() { | 265 void TestRenderViewHost::SendShouldCloseACK(bool proceed) { |
357 return contents()->GetController(); | 266 OnMsgShouldCloseACK(proceed, base::TimeTicks(), base::TimeTicks()); |
358 } | 267 } |
359 | 268 |
360 TestTabContents* RenderViewHostTestHarness::contents() { | 269 void TestRenderViewHost::SetContentsMimeType(const std::string& mime_type) { |
361 return contents_.get(); | 270 contents_mime_type_ = mime_type; |
362 } | 271 } |
363 | 272 |
364 TestRenderViewHost* RenderViewHostTestHarness::rvh() { | 273 void TestRenderViewHost::SimulateSwapOutACK() { |
365 return static_cast<TestRenderViewHost*>(contents()->GetRenderViewHost()); | 274 OnSwapOutACK(); |
366 } | 275 } |
367 | 276 |
368 TestRenderViewHost* RenderViewHostTestHarness::pending_rvh() { | 277 void TestRenderViewHost::SimulateWasHidden() { |
369 return static_cast<TestRenderViewHost*>( | 278 WasHidden(); |
370 contents()->GetRenderManagerForTesting()->pending_render_view_host()); | |
371 } | 279 } |
372 | 280 |
373 TestRenderViewHost* RenderViewHostTestHarness::active_rvh() { | 281 void TestRenderViewHost::SimulateWasRestored() { |
374 return pending_rvh() ? pending_rvh() : rvh(); | 282 WasRestored(); |
375 } | 283 } |
376 | 284 |
377 content::BrowserContext* RenderViewHostTestHarness::browser_context() { | 285 bool TestRenderViewHost::TestOnMessageReceived(const IPC::Message& msg) { |
378 return browser_context_.get(); | 286 return OnMessageReceived(msg); |
379 } | 287 } |
380 | 288 |
381 MockRenderProcessHost* RenderViewHostTestHarness::process() { | 289 void TestRenderViewHost::TestOnMsgStartDragging( |
382 if (pending_rvh()) | 290 const WebDropData& drop_data) { |
383 return static_cast<MockRenderProcessHost*>(pending_rvh()->GetProcess()); | 291 WebKit::WebDragOperationsMask drag_operation = WebKit::WebDragOperationEvery; |
384 return static_cast<MockRenderProcessHost*>(rvh()->GetProcess()); | 292 OnMsgStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Point()); |
385 } | 293 } |
386 | 294 |
387 void RenderViewHostTestHarness::DeleteContents() { | 295 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy) { |
388 SetContents(NULL); | 296 simulate_fetch_via_proxy_ = proxy; |
389 } | 297 } |
390 | 298 |
391 void RenderViewHostTestHarness::SetContents(TestTabContents* contents) { | 299 RenderViewHostImplTestHarness::RenderViewHostImplTestHarness() { |
392 contents_.reset(contents); | |
393 } | 300 } |
394 | 301 |
395 TestTabContents* RenderViewHostTestHarness::CreateTestTabContents() { | 302 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() { |
396 // See comment above browser_context_ decl for why we check for NULL here. | |
397 if (!browser_context_.get()) | |
398 browser_context_.reset(new TestBrowserContext()); | |
399 | |
400 // This will be deleted when the TabContents goes away. | |
401 SiteInstance* instance = SiteInstance::Create(browser_context_.get()); | |
402 | |
403 return new TestTabContents(browser_context_.get(), instance); | |
404 } | 303 } |
405 | 304 |
406 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) { | 305 TestRenderViewHost* RenderViewHostImplTestHarness::test_rvh() { |
407 contents()->NavigateAndCommit(url); | 306 return static_cast<TestRenderViewHost*>(rvh()); |
408 } | 307 } |
409 | 308 |
410 void RenderViewHostTestHarness::Reload() { | 309 TestRenderViewHost* RenderViewHostImplTestHarness::pending_test_rvh() { |
411 NavigationEntry* entry = controller().GetLastCommittedEntry(); | 310 return static_cast<TestRenderViewHost*>(pending_rvh()); |
412 DCHECK(entry); | |
413 controller().Reload(false); | |
414 rvh()->SendNavigate(entry->GetPageID(), entry->GetURL()); | |
415 } | 311 } |
416 | 312 |
417 void RenderViewHostTestHarness::SetUp() { | 313 TestRenderViewHost* RenderViewHostImplTestHarness::active_test_rvh() { |
418 #if defined(USE_AURA) | 314 return static_cast<TestRenderViewHost*>(active_rvh()); |
419 root_window_.reset(new aura::RootWindow); | |
420 test_stacking_client_.reset( | |
421 new aura::test::TestStackingClient(root_window_.get())); | |
422 #endif | |
423 SetContents(CreateTestTabContents()); | |
424 } | 315 } |
425 | 316 |
426 void RenderViewHostTestHarness::TearDown() { | 317 } // namespace content |
427 SetContents(NULL); | |
428 #if defined(USE_AURA) | |
429 test_stacking_client_.reset(); | |
430 root_window_.reset(); | |
431 #endif | |
432 | |
433 // Make sure that we flush any messages related to TabContents destruction | |
434 // before we destroy the browser context. | |
435 MessageLoop::current()->RunAllPending(); | |
436 | |
437 // Release the browser context on the UI thread. | |
438 message_loop_.DeleteSoon(FROM_HERE, browser_context_.release()); | |
439 message_loop_.RunAllPending(); | |
440 } | |
OLD | NEW |