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 #ifndef CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_
6 #define CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "build/build_config.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/browser/renderer_host/render_widget_host_view_base.h"
16 #include "content/public/common/page_transition_types.h"
17 #include "content/public/test/test_renderer_host.h"
18 #include "ui/base/ime/dummy_text_input_client.h"
19 #include "ui/base/layout.h"
20 #include "ui/gfx/vector2d_f.h"
22 // This file provides a testing framework for mocking out the RenderProcessHost
23 // layer. It allows you to test RenderViewHost, WebContentsImpl,
24 // NavigationController, and other layers above that without running an actual
27 // To use, derive your test base class from RenderViewHostImplTestHarness.
29 struct FrameHostMsg_DidCommitProvisionalLoad_Params
;
30 struct ViewHostMsg_TextInputState_Params
;
39 class TestRenderFrameHost
;
40 class TestWebContents
;
42 // Utility function to initialize ViewHostMsg_NavigateParams_Params
43 // with given |page_id|, |url| and |transition_type|.
44 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params
* params
,
47 PageTransition transition_type
);
49 // TestRenderViewHostView ------------------------------------------------------
51 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
52 // without having side-effects.
53 class TestRenderWidgetHostView
: public RenderWidgetHostViewBase
{
55 explicit TestRenderWidgetHostView(RenderWidgetHost
* rwh
);
56 virtual ~TestRenderWidgetHostView();
58 // RenderWidgetHostView implementation.
59 virtual void InitAsChild(gfx::NativeView parent_view
) OVERRIDE
{}
60 virtual RenderWidgetHost
* GetRenderWidgetHost() const OVERRIDE
;
61 virtual void SetSize(const gfx::Size
& size
) OVERRIDE
{}
62 virtual void SetBounds(const gfx::Rect
& rect
) OVERRIDE
{}
63 virtual gfx::NativeView
GetNativeView() const OVERRIDE
;
64 virtual gfx::NativeViewId
GetNativeViewId() const OVERRIDE
;
65 virtual gfx::NativeViewAccessible
GetNativeViewAccessible() OVERRIDE
;
66 virtual ui::TextInputClient
* GetTextInputClient() OVERRIDE
;
67 virtual bool HasFocus() const OVERRIDE
;
68 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE
;
69 virtual void Show() OVERRIDE
;
70 virtual void Hide() OVERRIDE
;
71 virtual bool IsShowing() OVERRIDE
;
72 virtual gfx::Rect
GetViewBounds() const OVERRIDE
;
73 #if defined(OS_MACOSX)
74 virtual void SetActive(bool active
) OVERRIDE
;
75 virtual void SetTakesFocusOnlyOnMouseDown(bool flag
) OVERRIDE
{}
76 virtual void SetWindowVisibility(bool visible
) OVERRIDE
{}
77 virtual void WindowFrameChanged() OVERRIDE
{}
78 virtual void ShowDefinitionForSelection() OVERRIDE
{}
79 virtual bool SupportsSpeech() const OVERRIDE
;
80 virtual void SpeakSelection() OVERRIDE
;
81 virtual bool IsSpeaking() const OVERRIDE
;
82 virtual void StopSpeaking() OVERRIDE
;
83 #endif // defined(OS_MACOSX)
84 virtual void OnSwapCompositorFrame(
85 uint32 output_surface_id
,
86 scoped_ptr
<cc::CompositorFrame
> frame
) OVERRIDE
;
88 // RenderWidgetHostViewBase implementation.
89 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
90 const gfx::Rect
& pos
) OVERRIDE
{}
91 virtual void InitAsFullscreen(
92 RenderWidgetHostView
* reference_host_view
) OVERRIDE
{}
93 virtual void WasShown() OVERRIDE
{}
94 virtual void WasHidden() OVERRIDE
{}
95 virtual void MovePluginWindows(
96 const std::vector
<WebPluginGeometry
>& moves
) OVERRIDE
{}
97 virtual void Focus() OVERRIDE
{}
98 virtual void Blur() OVERRIDE
{}
99 virtual void SetIsLoading(bool is_loading
) OVERRIDE
{}
100 virtual void UpdateCursor(const WebCursor
& cursor
) OVERRIDE
{}
101 virtual void TextInputStateChanged(
102 const ViewHostMsg_TextInputState_Params
& params
) OVERRIDE
{}
103 virtual void ImeCancelComposition() OVERRIDE
{}
104 #if defined(OS_MACOSX) || defined(USE_AURA)
105 virtual void ImeCompositionRangeChanged(
106 const gfx::Range
& range
,
107 const std::vector
<gfx::Rect
>& character_bounds
) OVERRIDE
{}
109 virtual void RenderProcessGone(base::TerminationStatus status
,
110 int error_code
) OVERRIDE
;
111 virtual void Destroy() OVERRIDE
;
112 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
{}
113 virtual void SelectionBoundsChanged(
114 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
{}
115 virtual void CopyFromCompositingSurface(
116 const gfx::Rect
& src_subrect
,
117 const gfx::Size
& dst_size
,
118 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
119 const SkColorType color_type
) OVERRIDE
;
120 virtual void CopyFromCompositingSurfaceToVideoFrame(
121 const gfx::Rect
& src_subrect
,
122 const scoped_refptr
<media::VideoFrame
>& target
,
123 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
124 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
125 virtual void AcceleratedSurfaceInitialized(int host_id
,
126 int route_id
) OVERRIDE
;
127 virtual void AcceleratedSurfaceBuffersSwapped(
128 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
129 int gpu_host_id
) OVERRIDE
;
130 virtual void AcceleratedSurfacePostSubBuffer(
131 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
132 int gpu_host_id
) OVERRIDE
;
133 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
134 virtual void AcceleratedSurfaceRelease() OVERRIDE
{}
135 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
136 #if defined(OS_MACOSX)
137 virtual bool PostProcessEventForPluginIme(
138 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
139 #elif defined(OS_ANDROID)
140 virtual void ShowDisambiguationPopup(
141 const gfx::Rect
& target_rect
,
142 const SkBitmap
& zoomed_bitmap
) OVERRIDE
{}
143 virtual void LockCompositingSurface() OVERRIDE
{}
144 virtual void UnlockCompositingSurface() OVERRIDE
{}
146 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
{}
147 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
148 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
149 virtual bool LockMouse() OVERRIDE
;
150 virtual void UnlockMouse() OVERRIDE
;
152 virtual void SetParentNativeViewAccessible(
153 gfx::NativeViewAccessible accessible_parent
) OVERRIDE
;
154 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const OVERRIDE
;
157 bool is_showing() const { return is_showing_
; }
158 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
161 RenderWidgetHostImpl
* rwh_
;
165 bool did_swap_compositor_frame_
;
166 ui::DummyTextInputClient text_input_client_
;
169 #if defined(COMPILER_MSVC)
170 // See comment for same warning on RenderViewHostImpl.
171 #pragma warning(push)
172 #pragma warning(disable: 4250)
175 // TestRenderViewHost ----------------------------------------------------------
177 // TODO(brettw) this should use a TestWebContents which should be generalized
178 // from the WebContentsImpl test. We will probably also need that class' version
179 // of CreateRenderViewForRenderManager when more complicated tests start using
182 // Note that users outside of content must use this class by getting
183 // the separate RenderViewHostTester interface via
184 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
187 // Users within content may directly static_cast from a
188 // RenderViewHost* to a TestRenderViewHost*.
190 // The reasons we do it this way rather than extending the parallel
191 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
192 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
194 // a) Extending the parallel class hierarchy further would require
195 // more classes to use virtual inheritance. This is a complexity that
196 // is better to avoid, especially when it would be introduced in the
197 // production code solely to facilitate testing code.
199 // b) While users outside of content only need to drive tests on a
200 // RenderViewHost, content needs a test version of the full
201 // RenderViewHostImpl so that it can test all methods on that concrete
202 // class (e.g. overriding a method such as
203 // RenderViewHostImpl::CreateRenderView). This would have complicated
204 // the dual class hierarchy even further.
206 // The reason we do it this way instead of using composition is
207 // similar to (b) above, essentially it gets very tricky. By using
208 // the split interface we avoid complexity within content and maintain
209 // reasonable utility for embedders.
210 class TestRenderViewHost
211 : public RenderViewHostImpl
,
212 public RenderViewHostTester
{
214 TestRenderViewHost(SiteInstance
* instance
,
215 RenderViewHostDelegate
* delegate
,
216 RenderWidgetHostDelegate
* widget_delegate
,
218 int main_frame_routing_id
,
220 virtual ~TestRenderViewHost();
222 // RenderViewHostTester implementation. Note that CreateRenderView
223 // is not specified since it is synonymous with the one from
224 // RenderViewHostImpl, see below.
225 virtual void SendBeforeUnloadACK(bool proceed
) OVERRIDE
;
226 virtual void SetContentsMimeType(const std::string
& mime_type
) OVERRIDE
;
227 virtual void SimulateSwapOutACK() OVERRIDE
;
228 virtual void SimulateWasHidden() OVERRIDE
;
229 virtual void SimulateWasShown() OVERRIDE
;
231 // NOTE: These methods are deprecated and the equivalents in
232 // TestRenderFrameHost should be used.
233 virtual void SendNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
234 virtual void SendFailedNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
235 virtual void SendNavigateWithTransition(int page_id
, const GURL
& url
,
236 PageTransition transition
) OVERRIDE
;
238 // Calls OnNavigate on the RenderViewHost with the given information,
239 // including a custom original request URL. Sets the rest of the
240 // parameters in the message to the "typical" values. This is a helper
241 // function for simulating the most common types of loads.
242 void SendNavigateWithOriginalRequestURL(
243 int page_id
, const GURL
& url
, const GURL
& original_request_url
);
245 void SendNavigateWithFile(
246 int page_id
, const GURL
& url
, const base::FilePath
& file_path
);
248 void SendNavigateWithParams(
249 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
);
251 void TestOnUpdateStateWithFile(
252 int process_id
, const base::FilePath
& file_path
);
254 void TestOnStartDragging(const DropData
& drop_data
);
256 // If set, *delete_counter is incremented when this object destructs.
257 void set_delete_counter(int* delete_counter
) {
258 delete_counter_
= delete_counter
;
261 // Sets whether the RenderView currently exists or not. This controls the
262 // return value from IsRenderViewLive, which the rest of the system uses to
263 // check whether the RenderView has crashed or not.
264 void set_render_view_created(bool created
) {
265 render_view_created_
= created
;
268 // Returns whether the RenderViewHost is currently waiting to hear the result
269 // of a before unload handler from the renderer.
270 bool is_waiting_for_beforeunload_ack() const {
271 return is_waiting_for_beforeunload_ack_
;
274 // Sets whether the RenderViewHost is currently swapped out, and thus
275 // filtering messages from the renderer.
276 void set_rvh_state(RenderViewHostImplState rvh_state
) {
277 rvh_state_
= rvh_state
;
280 // If set, navigations will appear to have loaded through a proxy
281 // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy).
283 void set_simulate_fetch_via_proxy(bool proxy
);
285 // If set, navigations will appear to have cleared the history list in the
287 // (FrameHostMsg_DidCommitProvisionalLoad_Params::history_list_was_cleared).
289 void set_simulate_history_list_was_cleared(bool cleared
);
291 // The opener route id passed to CreateRenderView().
292 int opener_route_id() const { return opener_route_id_
; }
294 // TODO(creis): Remove the need for these methods.
295 TestRenderFrameHost
* main_render_frame_host() const {
296 return main_render_frame_host_
;
298 void set_main_render_frame_host(TestRenderFrameHost
* rfh
) {
299 main_render_frame_host_
= rfh
;
302 // RenderViewHost overrides --------------------------------------------------
304 virtual bool CreateRenderView(const base::string16
& frame_name
,
308 bool window_was_created_with_opener
) OVERRIDE
;
309 virtual bool IsRenderViewLive() const OVERRIDE
;
310 virtual bool IsFullscreen() const OVERRIDE
;
313 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
315 void SendNavigateWithTransitionAndResponseCode(int page_id
,
317 PageTransition transition
,
320 // Calls OnNavigate on the RenderViewHost with the given information.
321 // Sets the rest of the parameters in the message to the "typical" values.
322 // This is a helper function for simulating the most common types of loads.
323 void SendNavigateWithParameters(
326 PageTransition transition
,
327 const GURL
& original_request_url
,
329 const base::FilePath
* file_path_for_history_item
);
331 // Tracks if the caller thinks if it created the RenderView. This is so we can
332 // respond to IsRenderViewLive appropriately.
333 bool render_view_created_
;
335 // See set_delete_counter() above. May be NULL.
336 int* delete_counter_
;
338 // See set_simulate_fetch_via_proxy() above.
339 bool simulate_fetch_via_proxy_
;
341 // See set_simulate_history_list_was_cleared() above.
342 bool simulate_history_list_was_cleared_
;
344 // See SetContentsMimeType() above.
345 std::string contents_mime_type_
;
347 // See opener_route_id() above.
348 int opener_route_id_
;
350 TestRenderFrameHost
* main_render_frame_host_
;
352 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
355 #if defined(COMPILER_MSVC)
359 // Adds methods to get straight at the impl classes.
360 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
362 RenderViewHostImplTestHarness();
363 virtual ~RenderViewHostImplTestHarness();
365 // contents() is equivalent to static_cast<TestWebContents*>(web_contents())
366 TestWebContents
* contents();
368 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
370 // test_rvh() is equivalent to any of the following:
371 // contents()->GetMainFrame()->GetRenderViewHost()
372 // contents()->GetRenderViewHost()
373 // static_cast<TestRenderViewHost*>(rvh())
375 // Since most functionality will eventually shift from RVH to RFH, you may
376 // prefer to use the GetMainFrame() method in tests.
377 TestRenderViewHost
* test_rvh();
379 // pending_test_rvh() is equivalent to all of the following:
380 // contents()->GetPendingMainFrame()->GetRenderViewHost() [if frame exists]
381 // contents()->GetPendingRenderViewHost()
382 // static_cast<TestRenderViewHost*>(pending_rvh())
384 // Since most functionality will eventually shift from RVH to RFH, you may
385 // prefer to use the GetPendingMainFrame() method in tests.
386 TestRenderViewHost
* pending_test_rvh();
388 // active_test_rvh() is equivalent to:
389 // contents()->GetPendingRenderViewHost() ?
390 // contents()->GetPendingRenderViewHost() :
391 // contents()->GetRenderViewHost();
392 TestRenderViewHost
* active_test_rvh();
394 // main_test_rfh() is equivalent to contents()->GetMainFrame()
395 // TODO(nick): Replace all uses with contents()->GetMainFrame()
396 TestRenderFrameHost
* main_test_rfh();
399 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
400 ScopedSetSupportedScaleFactors
;
401 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
402 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
405 } // namespace content
407 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_