OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | |
6 | |
7 #include "chrome/browser/browser_url_handler.h" | |
8 #include "chrome/browser/renderer_host/site_instance.h" | |
9 #include "chrome/browser/renderer_host/test/test_backing_store.h" | |
10 #include "chrome/browser/tab_contents/navigation_controller.h" | |
11 #include "chrome/browser/tab_contents/test_tab_contents.h" | |
12 #include "chrome/common/dom_storage_common.h" | |
13 #include "chrome/common/render_messages.h" | |
14 #include "chrome/common/render_messages_params.h" | |
15 #include "chrome/test/testing_profile.h" | |
16 #include "ui/gfx/rect.h" | |
17 #include "webkit/glue/webpreferences.h" | |
18 #include "webkit/glue/password_form.h" | |
19 | |
20 using webkit_glue::PasswordForm; | |
21 | |
22 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params, | |
23 int page_id, | |
24 const GURL& url, | |
25 PageTransition::Type transition) { | |
26 params->page_id = page_id; | |
27 params->url = url; | |
28 params->referrer = GURL(); | |
29 params->transition = transition; | |
30 params->redirects = std::vector<GURL>(); | |
31 params->should_update_history = false; | |
32 params->searchable_form_url = GURL(); | |
33 params->searchable_form_encoding = std::string(); | |
34 params->password_form = PasswordForm(); | |
35 params->security_info = std::string(); | |
36 params->gesture = NavigationGestureUser; | |
37 params->was_within_same_page = false; | |
38 params->is_post = false; | |
39 } | |
40 | |
41 TestRenderViewHost::TestRenderViewHost(SiteInstance* instance, | |
42 RenderViewHostDelegate* delegate, | |
43 int routing_id) | |
44 : RenderViewHost(instance, delegate, routing_id, | |
45 kInvalidSessionStorageNamespaceId), | |
46 render_view_created_(false), | |
47 delete_counter_(NULL) { | |
48 // For normal RenderViewHosts, this is freed when |Shutdown()| is called. | |
49 // For TestRenderViewHost, the view is explicitly deleted in the destructor | |
50 // below, because TestRenderWidgetHostView::Destroy() doesn't |delete this|. | |
51 set_view(new TestRenderWidgetHostView(this)); | |
52 } | |
53 | |
54 TestRenderViewHost::~TestRenderViewHost() { | |
55 if (delete_counter_) | |
56 ++*delete_counter_; | |
57 | |
58 // Since this isn't a traditional view, we have to delete it. | |
59 delete view(); | |
60 } | |
61 | |
62 bool TestRenderViewHost::CreateRenderView(const string16& frame_name) { | |
63 DCHECK(!render_view_created_); | |
64 render_view_created_ = true; | |
65 process()->ViewCreated(); | |
66 return true; | |
67 } | |
68 | |
69 bool TestRenderViewHost::IsRenderViewLive() const { | |
70 return render_view_created_; | |
71 } | |
72 | |
73 bool TestRenderViewHost::TestOnMessageReceived(const IPC::Message& msg) { | |
74 return OnMessageReceived(msg); | |
75 } | |
76 | |
77 void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) { | |
78 SendNavigateWithTransition(page_id, url, PageTransition::LINK); | |
79 } | |
80 | |
81 void TestRenderViewHost::SendNavigateWithTransition( | |
82 int page_id, const GURL& url, PageTransition::Type transition) { | |
83 ViewHostMsg_FrameNavigate_Params params; | |
84 | |
85 params.page_id = page_id; | |
86 params.url = url; | |
87 params.referrer = GURL(); | |
88 params.transition = transition; | |
89 params.redirects = std::vector<GURL>(); | |
90 params.should_update_history = true; | |
91 params.searchable_form_url = GURL(); | |
92 params.searchable_form_encoding = std::string(); | |
93 params.password_form = PasswordForm(); | |
94 params.security_info = std::string(); | |
95 params.gesture = NavigationGestureUser; | |
96 params.contents_mime_type = std::string(); | |
97 params.is_post = false; | |
98 params.is_content_filtered = false; | |
99 params.was_within_same_page = false; | |
100 params.http_status_code = 0; | |
101 params.socket_address.set_host("2001:db8::1"); | |
102 params.socket_address.set_port(80); | |
103 | |
104 ViewHostMsg_FrameNavigate msg(1, params); | |
105 OnMsgNavigate(msg); | |
106 } | |
107 | |
108 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost* rwh) | |
109 : rwh_(rwh), | |
110 is_showing_(false) { | |
111 } | |
112 | |
113 TestRenderWidgetHostView::~TestRenderWidgetHostView() { | |
114 } | |
115 | |
116 RenderWidgetHost* TestRenderWidgetHostView::GetRenderWidgetHost() const { | |
117 return NULL; | |
118 } | |
119 | |
120 gfx::NativeView TestRenderWidgetHostView::GetNativeView() { | |
121 return NULL; | |
122 } | |
123 | |
124 bool TestRenderWidgetHostView::HasFocus() { | |
125 return true; | |
126 } | |
127 | |
128 void TestRenderWidgetHostView::Show() { | |
129 is_showing_ = true; | |
130 } | |
131 | |
132 void TestRenderWidgetHostView::Hide() { | |
133 is_showing_ = false; | |
134 } | |
135 | |
136 bool TestRenderWidgetHostView::IsShowing() { | |
137 return is_showing_; | |
138 } | |
139 | |
140 void TestRenderWidgetHostView::RenderViewGone(base::TerminationStatus status, | |
141 int error_code) { | |
142 delete this; | |
143 } | |
144 | |
145 gfx::Rect TestRenderWidgetHostView::GetViewBounds() const { | |
146 return gfx::Rect(); | |
147 } | |
148 | |
149 BackingStore* TestRenderWidgetHostView::AllocBackingStore( | |
150 const gfx::Size& size) { | |
151 return new TestBackingStore(rwh_, size); | |
152 } | |
153 | |
154 #if defined(OS_MACOSX) | |
155 | |
156 void TestRenderWidgetHostView::ShowPopupWithItems( | |
157 gfx::Rect bounds, | |
158 int item_height, | |
159 double item_font_size, | |
160 int selected_item, | |
161 const std::vector<WebMenuItem>& items, | |
162 bool right_aligned) { | |
163 } | |
164 | |
165 gfx::Rect TestRenderWidgetHostView::GetViewCocoaBounds() const { | |
166 return gfx::Rect(); | |
167 } | |
168 | |
169 gfx::Rect TestRenderWidgetHostView::GetRootWindowRect() { | |
170 return gfx::Rect(); | |
171 } | |
172 | |
173 void TestRenderWidgetHostView::SetActive(bool active) { | |
174 // <viettrungluu@gmail.com>: Do I need to do anything here? | |
175 } | |
176 | |
177 void TestRenderWidgetHostView::PluginFocusChanged(bool focused, | |
178 int plugin_id) { | |
179 } | |
180 | |
181 void TestRenderWidgetHostView::StartPluginIme() { | |
182 } | |
183 | |
184 bool TestRenderWidgetHostView::PostProcessEventForPluginIme( | |
185 const NativeWebKeyboardEvent& event) { | |
186 return false; | |
187 } | |
188 | |
189 gfx::PluginWindowHandle | |
190 TestRenderWidgetHostView::AllocateFakePluginWindowHandle( | |
191 bool opaque, | |
192 bool root) { | |
193 return NULL; | |
194 } | |
195 | |
196 void TestRenderWidgetHostView::DestroyFakePluginWindowHandle( | |
197 gfx::PluginWindowHandle window) { | |
198 } | |
199 | |
200 void TestRenderWidgetHostView::AcceleratedSurfaceSetIOSurface( | |
201 gfx::PluginWindowHandle window, | |
202 int32 width, | |
203 int32 height, | |
204 uint64 surface_id) { | |
205 } | |
206 | |
207 void TestRenderWidgetHostView::AcceleratedSurfaceSetTransportDIB( | |
208 gfx::PluginWindowHandle window, | |
209 int32 width, | |
210 int32 height, | |
211 TransportDIB::Handle transport_dib) { | |
212 } | |
213 | |
214 void TestRenderWidgetHostView::AcceleratedSurfaceBuffersSwapped( | |
215 gfx::PluginWindowHandle window, | |
216 uint64 surface_id, | |
217 int renderer_id, | |
218 int32 route_id, | |
219 int gpu_host_id, | |
220 uint64 swap_buffers_count) { | |
221 } | |
222 | |
223 void TestRenderWidgetHostView::GpuRenderingStateDidChange() { | |
224 } | |
225 #elif defined(OS_WIN) | |
226 void TestRenderWidgetHostView::WillWmDestroy() { | |
227 } | |
228 | |
229 void TestRenderWidgetHostView::ShowCompositorHostWindow(bool show) { | |
230 } | |
231 #endif | |
232 | |
233 gfx::PluginWindowHandle TestRenderWidgetHostView::AcquireCompositingSurface() { | |
234 return gfx::kNullPluginWindow; | |
235 } | |
236 | |
237 bool TestRenderWidgetHostView::ContainsNativeView( | |
238 gfx::NativeView native_view) const { | |
239 return false; | |
240 } | |
241 | |
242 TestRenderViewHostFactory::TestRenderViewHostFactory( | |
243 RenderProcessHostFactory* rph_factory) | |
244 : render_process_host_factory_(rph_factory) { | |
245 RenderViewHostFactory::RegisterFactory(this); | |
246 } | |
247 | |
248 TestRenderViewHostFactory::~TestRenderViewHostFactory() { | |
249 RenderViewHostFactory::UnregisterFactory(); | |
250 } | |
251 | |
252 void TestRenderViewHostFactory::set_render_process_host_factory( | |
253 RenderProcessHostFactory* rph_factory) { | |
254 render_process_host_factory_ = rph_factory; | |
255 } | |
256 | |
257 RenderViewHost* TestRenderViewHostFactory::CreateRenderViewHost( | |
258 SiteInstance* instance, | |
259 RenderViewHostDelegate* delegate, | |
260 int routing_id, | |
261 SessionStorageNamespace* session_storage) { | |
262 // See declaration of render_process_host_factory_ below. | |
263 instance->set_render_process_host_factory(render_process_host_factory_); | |
264 return new TestRenderViewHost(instance, delegate, routing_id); | |
265 } | |
266 | |
267 RenderViewHostTestHarness::RenderViewHostTestHarness() | |
268 : rph_factory_(), | |
269 rvh_factory_(&rph_factory_), | |
270 contents_(NULL) { | |
271 } | |
272 | |
273 RenderViewHostTestHarness::~RenderViewHostTestHarness() { | |
274 } | |
275 | |
276 NavigationController& RenderViewHostTestHarness::controller() { | |
277 return contents_->controller(); | |
278 } | |
279 | |
280 TestTabContents* RenderViewHostTestHarness::contents() { | |
281 return contents_.get(); | |
282 } | |
283 | |
284 TestRenderViewHost* RenderViewHostTestHarness::rvh() { | |
285 return static_cast<TestRenderViewHost*>(contents_->render_view_host()); | |
286 } | |
287 | |
288 TestRenderViewHost* RenderViewHostTestHarness::pending_rvh() { | |
289 return static_cast<TestRenderViewHost*>( | |
290 contents_->render_manager()->pending_render_view_host()); | |
291 } | |
292 | |
293 TestRenderViewHost* RenderViewHostTestHarness::active_rvh() { | |
294 return pending_rvh() ? pending_rvh() : rvh(); | |
295 } | |
296 | |
297 TestingProfile* RenderViewHostTestHarness::profile() { | |
298 return profile_.get(); | |
299 } | |
300 | |
301 MockRenderProcessHost* RenderViewHostTestHarness::process() { | |
302 if (pending_rvh()) | |
303 return static_cast<MockRenderProcessHost*>(pending_rvh()->process()); | |
304 return static_cast<MockRenderProcessHost*>(rvh()->process()); | |
305 } | |
306 | |
307 void RenderViewHostTestHarness::DeleteContents() { | |
308 contents_.reset(); | |
309 } | |
310 | |
311 TestTabContents* RenderViewHostTestHarness::CreateTestTabContents() { | |
312 // See comment above profile_ decl for why we check for NULL here. | |
313 if (!profile_.get()) | |
314 profile_.reset(new TestingProfile()); | |
315 | |
316 // This will be deleted when the TabContents goes away. | |
317 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get()); | |
318 | |
319 return new TestTabContents(profile_.get(), instance); | |
320 } | |
321 | |
322 void RenderViewHostTestHarness::NavigateAndCommit(const GURL& url) { | |
323 contents()->NavigateAndCommit(url); | |
324 } | |
325 | |
326 void RenderViewHostTestHarness::Reload() { | |
327 NavigationEntry* entry = controller().GetLastCommittedEntry(); | |
328 DCHECK(entry); | |
329 controller().Reload(false); | |
330 rvh()->SendNavigate(entry->page_id(), entry->url()); | |
331 } | |
332 | |
333 void RenderViewHostTestHarness::SetUp() { | |
334 contents_.reset(CreateTestTabContents()); | |
335 } | |
336 | |
337 void RenderViewHostTestHarness::TearDown() { | |
338 contents_.reset(); | |
339 | |
340 // Make sure that we flush any messages related to TabContents destruction | |
341 // before we destroy the profile. | |
342 MessageLoop::current()->RunAllPending(); | |
343 | |
344 // Release the profile on the UI thread. | |
345 message_loop_.DeleteSoon(FROM_HERE, profile_.release()); | |
346 message_loop_.RunAllPending(); | |
347 } | |
OLD | NEW |