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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/test_render_view_host.h"
7 #include "content/browser/dom_storage/dom_storage_context_impl.h"
8 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
9 #include "content/browser/renderer_host/test_backing_store.h"
10 #include "content/browser/site_instance_impl.h"
11 #include "content/common/view_messages.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/storage_partition.h"
15 #include "content/public/common/content_client.h"
16 #include "content/public/common/page_state.h"
17 #include "content/public/common/password_form.h"
18 #include "content/test/test_web_contents.h"
19 #include "media/base/video_frame.h"
20 #include "ui/gfx/rect.h"
21 #include "webkit/common/dom_storage/dom_storage_types.h"
22 #include "webkit/common/webpreferences.h"
27 // Normally this is done by the NavigationController, but we'll fake it out
29 SessionStorageNamespaceImpl
* CreateSessionStorageNamespace(
30 SiteInstance
* instance
) {
31 RenderProcessHost
* process_host
= instance
->GetProcess();
32 DOMStorageContext
* dom_storage_context
=
33 BrowserContext::GetStoragePartition(process_host
->GetBrowserContext(),
34 instance
)->GetDOMStorageContext();
35 return new SessionStorageNamespaceImpl(
36 static_cast<DOMStorageContextImpl
*>(dom_storage_context
));
39 const int64 kFrameId
= 13UL;
44 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params
* params
,
47 PageTransition transition
) {
48 params
->page_id
= page_id
;
50 params
->referrer
= Referrer();
51 params
->transition
= transition
;
52 params
->redirects
= std::vector
<GURL
>();
53 params
->should_update_history
= false;
54 params
->searchable_form_url
= GURL();
55 params
->searchable_form_encoding
= std::string();
56 params
->password_form
= PasswordForm();
57 params
->security_info
= std::string();
58 params
->gesture
= NavigationGestureUser
;
59 params
->was_within_same_page
= false;
60 params
->is_post
= false;
61 params
->page_state
= PageState::CreateFromURL(url
);
64 TestRenderWidgetHostView::TestRenderWidgetHostView(RenderWidgetHost
* rwh
)
65 : rwh_(RenderWidgetHostImpl::From(rwh
)),
67 did_swap_compositor_frame_(false) {
71 TestRenderWidgetHostView::~TestRenderWidgetHostView() {
74 RenderWidgetHost
* TestRenderWidgetHostView::GetRenderWidgetHost() const {
78 gfx::NativeView
TestRenderWidgetHostView::GetNativeView() const {
82 gfx::NativeViewId
TestRenderWidgetHostView::GetNativeViewId() const {
86 gfx::NativeViewAccessible
TestRenderWidgetHostView::GetNativeViewAccessible() {
90 bool TestRenderWidgetHostView::HasFocus() const {
94 bool TestRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
98 void TestRenderWidgetHostView::Show() {
102 void TestRenderWidgetHostView::Hide() {
106 bool TestRenderWidgetHostView::IsShowing() {
110 void TestRenderWidgetHostView::RenderViewGone(base::TerminationStatus status
,
115 void TestRenderWidgetHostView::Destroy() { delete this; }
117 gfx::Rect
TestRenderWidgetHostView::GetViewBounds() const {
121 BackingStore
* TestRenderWidgetHostView::AllocBackingStore(
122 const gfx::Size
& size
) {
123 return new TestBackingStore(rwh_
, size
);
126 void TestRenderWidgetHostView::CopyFromCompositingSurface(
127 const gfx::Rect
& src_subrect
,
128 const gfx::Size
& dst_size
,
129 const base::Callback
<void(bool, const SkBitmap
&)>& callback
) {
130 callback
.Run(false, SkBitmap());
133 void TestRenderWidgetHostView::CopyFromCompositingSurfaceToVideoFrame(
134 const gfx::Rect
& src_subrect
,
135 const scoped_refptr
<media::VideoFrame
>& target
,
136 const base::Callback
<void(bool)>& callback
) {
140 bool TestRenderWidgetHostView::CanCopyToVideoFrame() const {
144 void TestRenderWidgetHostView::OnAcceleratedCompositingStateChange() {
147 void TestRenderWidgetHostView::AcceleratedSurfaceBuffersSwapped(
148 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
152 void TestRenderWidgetHostView::AcceleratedSurfacePostSubBuffer(
153 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
157 void TestRenderWidgetHostView::AcceleratedSurfaceSuspend() {
160 bool TestRenderWidgetHostView::HasAcceleratedSurface(
161 const gfx::Size
& desired_size
) {
165 #if defined(OS_MACOSX)
167 void TestRenderWidgetHostView::AboutToWaitForBackingStoreMsg() {
170 void TestRenderWidgetHostView::SetActive(bool active
) {
171 // <viettrungluu@gmail.com>: Do I need to do anything here?
174 bool TestRenderWidgetHostView::SupportsSpeech() const {
178 void TestRenderWidgetHostView::SpeakSelection() {
181 bool TestRenderWidgetHostView::IsSpeaking() const {
185 void TestRenderWidgetHostView::StopSpeaking() {
188 bool TestRenderWidgetHostView::PostProcessEventForPluginIme(
189 const NativeWebKeyboardEvent
& event
) {
193 #elif defined(OS_WIN) && !defined(USE_AURA)
194 void TestRenderWidgetHostView::WillWmDestroy() {
198 gfx::Rect
TestRenderWidgetHostView::GetBoundsInRootWindow() {
202 #if defined(TOOLKIT_GTK)
203 GdkEventButton
* TestRenderWidgetHostView::GetLastMouseDown() {
207 gfx::NativeView
TestRenderWidgetHostView::BuildInputMethodsGtkMenu() {
210 #endif // defined(TOOLKIT_GTK)
212 void TestRenderWidgetHostView::OnSwapCompositorFrame(
213 scoped_ptr
<cc::CompositorFrame
> frame
) {
214 did_swap_compositor_frame_
= true;
218 gfx::GLSurfaceHandle
TestRenderWidgetHostView::GetCompositingSurface() {
219 return gfx::GLSurfaceHandle();
222 #if defined(OS_WIN) && !defined(USE_AURA)
223 void TestRenderWidgetHostView::SetClickthroughRegion(SkRegion
* region
) {
227 #if defined(OS_WIN) && defined(USE_AURA)
228 gfx::NativeViewAccessible
229 TestRenderWidgetHostView::AccessibleObjectFromChildId(long child_id
) {
235 bool TestRenderWidgetHostView::LockMouse() {
239 void TestRenderWidgetHostView::UnlockMouse() {
242 #if defined(OS_WIN) && defined(USE_AURA)
243 void TestRenderWidgetHostView::SetParentNativeViewAccessible(
244 gfx::NativeViewAccessible accessible_parent
) {
248 TestRenderViewHost::TestRenderViewHost(
249 SiteInstance
* instance
,
250 RenderViewHostDelegate
* delegate
,
251 RenderWidgetHostDelegate
* widget_delegate
,
253 int main_frame_routing_id
,
255 : RenderViewHostImpl(instance
,
259 main_frame_routing_id
,
261 CreateSessionStorageNamespace(instance
)),
262 render_view_created_(false),
263 delete_counter_(NULL
),
264 simulate_fetch_via_proxy_(false),
265 simulate_history_list_was_cleared_(false),
266 contents_mime_type_("text/html") {
267 // TestRenderWidgetHostView installs itself into this->view_ in its
268 // constructor, and deletes itself when TestRenderWidgetHostView::Destroy() is
270 new TestRenderWidgetHostView(this);
272 main_frame_id_
= kFrameId
;
275 TestRenderViewHost::~TestRenderViewHost() {
280 bool TestRenderViewHost::CreateRenderView(
281 const string16
& frame_name
,
284 DCHECK(!render_view_created_
);
285 render_view_created_
= true;
289 bool TestRenderViewHost::IsRenderViewLive() const {
290 return render_view_created_
;
293 void TestRenderViewHost::SendNavigate(int page_id
, const GURL
& url
) {
294 SendNavigateWithTransition(page_id
, url
, PAGE_TRANSITION_LINK
);
297 void TestRenderViewHost::SendFailedNavigate(int page_id
, const GURL
& url
) {
298 SendNavigateWithTransitionAndResponseCode(
299 page_id
, url
, PAGE_TRANSITION_LINK
, 500);
302 void TestRenderViewHost::SendNavigateWithTransition(
303 int page_id
, const GURL
& url
, PageTransition transition
) {
304 SendNavigateWithTransitionAndResponseCode(page_id
, url
, transition
, 200);
307 void TestRenderViewHost::SendNavigateWithOriginalRequestURL(
308 int page_id
, const GURL
& url
, const GURL
& original_request_url
) {
309 OnDidStartProvisionalLoadForFrame(kFrameId
, -1, true, url
);
310 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
,
311 original_request_url
, 200, 0);
314 void TestRenderViewHost::SendNavigateWithFile(
315 int page_id
, const GURL
& url
, const base::FilePath
& file_path
) {
316 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
,
317 url
, 200, &file_path
);
320 void TestRenderViewHost::SendNavigateWithTransitionAndResponseCode(
321 int page_id
, const GURL
& url
, PageTransition transition
,
323 OnDidStartProvisionalLoadForFrame(kFrameId
, -1, true, url
);
324 SendNavigateWithParameters(page_id
, url
, transition
, url
, response_code
, 0);
327 void TestRenderViewHost::SendNavigateWithParameters(
328 int page_id
, const GURL
& url
, PageTransition transition
,
329 const GURL
& original_request_url
, int response_code
,
330 const base::FilePath
* file_path_for_history_item
) {
331 ViewHostMsg_FrameNavigate_Params params
;
332 params
.page_id
= page_id
;
333 params
.frame_id
= kFrameId
;
335 params
.referrer
= Referrer();
336 params
.transition
= transition
;
337 params
.redirects
= std::vector
<GURL
>();
338 params
.should_update_history
= true;
339 params
.searchable_form_url
= GURL();
340 params
.searchable_form_encoding
= std::string();
341 params
.password_form
= PasswordForm();
342 params
.security_info
= std::string();
343 params
.gesture
= NavigationGestureUser
;
344 params
.contents_mime_type
= contents_mime_type_
;
345 params
.is_post
= false;
346 params
.was_within_same_page
= false;
347 params
.http_status_code
= response_code
;
348 params
.socket_address
.set_host("2001:db8::1");
349 params
.socket_address
.set_port(80);
350 params
.was_fetched_via_proxy
= simulate_fetch_via_proxy_
;
351 params
.history_list_was_cleared
= simulate_history_list_was_cleared_
;
352 params
.original_request_url
= original_request_url
;
354 params
.page_state
= PageState::CreateForTesting(
357 file_path_for_history_item
? "data" : NULL
,
358 file_path_for_history_item
);
360 ViewHostMsg_FrameNavigate
msg(1, params
);
364 void TestRenderViewHost::SendShouldCloseACK(bool proceed
) {
365 base::TimeTicks now
= base::TimeTicks::Now();
366 OnShouldCloseACK(proceed
, now
, now
);
369 void TestRenderViewHost::SetContentsMimeType(const std::string
& mime_type
) {
370 contents_mime_type_
= mime_type
;
373 void TestRenderViewHost::SimulateSwapOutACK() {
377 void TestRenderViewHost::SimulateWasHidden() {
381 void TestRenderViewHost::SimulateWasShown() {
385 void TestRenderViewHost::TestOnStartDragging(
386 const WebDropData
& drop_data
) {
387 WebKit::WebDragOperationsMask drag_operation
= WebKit::WebDragOperationEvery
;
388 DragEventSourceInfo event_info
;
389 OnStartDragging(drop_data
, drag_operation
, SkBitmap(), gfx::Vector2d(),
393 void TestRenderViewHost::TestOnUpdateStateWithFile(
395 const base::FilePath
& file_path
) {
396 OnUpdateState(process_id
,
397 PageState::CreateForTesting(GURL("http://www.google.com"),
403 void TestRenderViewHost::set_simulate_fetch_via_proxy(bool proxy
) {
404 simulate_fetch_via_proxy_
= proxy
;
407 void TestRenderViewHost::set_simulate_history_list_was_cleared(bool cleared
) {
408 simulate_history_list_was_cleared_
= cleared
;
411 RenderViewHostImplTestHarness::RenderViewHostImplTestHarness() {
414 RenderViewHostImplTestHarness::~RenderViewHostImplTestHarness() {
417 TestRenderViewHost
* RenderViewHostImplTestHarness::test_rvh() {
418 return static_cast<TestRenderViewHost
*>(rvh());
421 TestRenderViewHost
* RenderViewHostImplTestHarness::pending_test_rvh() {
422 return static_cast<TestRenderViewHost
*>(pending_rvh());
425 TestRenderViewHost
* RenderViewHostImplTestHarness::active_test_rvh() {
426 return static_cast<TestRenderViewHost
*>(active_rvh());
429 TestWebContents
* RenderViewHostImplTestHarness::contents() {
430 return static_cast<TestWebContents
*>(web_contents());
433 } // namespace content