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 "content/test/test_render_frame_host.h"
19 #include "ui/base/ime/dummy_text_input_client.h"
20 #include "ui/base/layout.h"
21 #include "ui/gfx/vector2d_f.h"
23 // This file provides a testing framework for mocking out the RenderProcessHost
24 // layer. It allows you to test RenderViewHost, WebContentsImpl,
25 // NavigationController, and other layers above that without running an actual
28 // To use, derive your test base class from RenderViewHostImplTestHarness.
30 struct FrameHostMsg_DidCommitProvisionalLoad_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 TextInputTypeChanged(ui::TextInputType type
,
102 ui::TextInputMode input_mode
,
103 bool can_compose_inline
) OVERRIDE
{}
104 virtual void ImeCancelComposition() OVERRIDE
{}
105 #if defined(OS_MACOSX) || defined(USE_AURA)
106 virtual void ImeCompositionRangeChanged(
107 const gfx::Range
& range
,
108 const std::vector
<gfx::Rect
>& character_bounds
) OVERRIDE
{}
110 virtual void RenderProcessGone(base::TerminationStatus status
,
111 int error_code
) OVERRIDE
;
112 virtual void WillDestroyRenderWidget(RenderWidgetHost
* rwh
) { }
113 virtual void Destroy() OVERRIDE
;
114 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
{}
115 virtual void SelectionBoundsChanged(
116 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
{}
117 virtual void ScrollOffsetChanged() OVERRIDE
{}
118 virtual void CopyFromCompositingSurface(
119 const gfx::Rect
& src_subrect
,
120 const gfx::Size
& dst_size
,
121 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
122 const SkBitmap::Config config
) OVERRIDE
;
123 virtual void CopyFromCompositingSurfaceToVideoFrame(
124 const gfx::Rect
& src_subrect
,
125 const scoped_refptr
<media::VideoFrame
>& target
,
126 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
127 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
128 virtual void AcceleratedSurfaceInitialized(int host_id
,
129 int route_id
) OVERRIDE
;
130 virtual void AcceleratedSurfaceBuffersSwapped(
131 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
132 int gpu_host_id
) OVERRIDE
;
133 virtual void AcceleratedSurfacePostSubBuffer(
134 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
135 int gpu_host_id
) OVERRIDE
;
136 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
137 virtual void AcceleratedSurfaceRelease() OVERRIDE
{}
138 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
139 #if defined(OS_MACOSX)
140 virtual bool PostProcessEventForPluginIme(
141 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
142 #elif defined(OS_ANDROID)
143 virtual void SelectionRootBoundsChanged(const gfx::Rect
&) OVERRIDE
{}
144 virtual void ShowDisambiguationPopup(
145 const gfx::Rect
& target_rect
,
146 const SkBitmap
& zoomed_bitmap
) OVERRIDE
{}
147 virtual void LockCompositingSurface() OVERRIDE
{}
148 virtual void UnlockCompositingSurface() OVERRIDE
{}
150 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
{}
151 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
152 virtual void SetScrollOffsetPinning(
153 bool is_pinned_to_left
, bool is_pinned_to_right
) OVERRIDE
{ }
154 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
155 virtual bool LockMouse() OVERRIDE
;
156 virtual void UnlockMouse() OVERRIDE
;
158 virtual void SetParentNativeViewAccessible(
159 gfx::NativeViewAccessible accessible_parent
) OVERRIDE
;
160 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const OVERRIDE
;
163 bool is_showing() const { return is_showing_
; }
164 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
167 RenderWidgetHostImpl
* rwh_
;
171 bool did_swap_compositor_frame_
;
172 ui::DummyTextInputClient text_input_client_
;
175 #if defined(COMPILER_MSVC)
176 // See comment for same warning on RenderViewHostImpl.
177 #pragma warning(push)
178 #pragma warning(disable: 4250)
181 // TestRenderViewHost ----------------------------------------------------------
183 // TODO(brettw) this should use a TestWebContents which should be generalized
184 // from the WebContentsImpl test. We will probably also need that class' version
185 // of CreateRenderViewForRenderManager when more complicated tests start using
188 // Note that users outside of content must use this class by getting
189 // the separate RenderViewHostTester interface via
190 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
193 // Users within content may directly static_cast from a
194 // RenderViewHost* to a TestRenderViewHost*.
196 // The reasons we do it this way rather than extending the parallel
197 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
198 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
200 // a) Extending the parallel class hierarchy further would require
201 // more classes to use virtual inheritance. This is a complexity that
202 // is better to avoid, especially when it would be introduced in the
203 // production code solely to facilitate testing code.
205 // b) While users outside of content only need to drive tests on a
206 // RenderViewHost, content needs a test version of the full
207 // RenderViewHostImpl so that it can test all methods on that concrete
208 // class (e.g. overriding a method such as
209 // RenderViewHostImpl::CreateRenderView). This would have complicated
210 // the dual class hierarchy even further.
212 // The reason we do it this way instead of using composition is
213 // similar to (b) above, essentially it gets very tricky. By using
214 // the split interface we avoid complexity within content and maintain
215 // reasonable utility for embedders.
216 class TestRenderViewHost
217 : public RenderViewHostImpl
,
218 public RenderViewHostTester
{
220 TestRenderViewHost(SiteInstance
* instance
,
221 RenderViewHostDelegate
* delegate
,
222 RenderWidgetHostDelegate
* widget_delegate
,
224 int main_frame_routing_id
,
226 virtual ~TestRenderViewHost();
228 // RenderViewHostTester implementation. Note that CreateRenderView
229 // is not specified since it is synonymous with the one from
230 // RenderViewHostImpl, see below.
231 virtual void SendBeforeUnloadACK(bool proceed
) OVERRIDE
;
232 virtual void SetContentsMimeType(const std::string
& mime_type
) OVERRIDE
;
233 virtual void SimulateSwapOutACK() OVERRIDE
;
234 virtual void SimulateWasHidden() OVERRIDE
;
235 virtual void SimulateWasShown() OVERRIDE
;
237 // NOTE: These methods are deprecated and the equivalents in
238 // TestRenderFrameHost should be used.
239 virtual void SendNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
240 virtual void SendFailedNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
241 virtual void SendNavigateWithTransition(int page_id
, const GURL
& url
,
242 PageTransition transition
) OVERRIDE
;
244 // Calls OnNavigate on the RenderViewHost with the given information,
245 // including a custom original request URL. Sets the rest of the
246 // parameters in the message to the "typical" values. This is a helper
247 // function for simulating the most common types of loads.
248 void SendNavigateWithOriginalRequestURL(
249 int page_id
, const GURL
& url
, const GURL
& original_request_url
);
251 void SendNavigateWithFile(
252 int page_id
, const GURL
& url
, const base::FilePath
& file_path
);
254 void SendNavigateWithParams(
255 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
);
257 void TestOnUpdateStateWithFile(
258 int process_id
, const base::FilePath
& file_path
);
260 void TestOnStartDragging(const DropData
& drop_data
);
262 // If set, *delete_counter is incremented when this object destructs.
263 void set_delete_counter(int* delete_counter
) {
264 delete_counter_
= delete_counter
;
267 // Sets whether the RenderView currently exists or not. This controls the
268 // return value from IsRenderViewLive, which the rest of the system uses to
269 // check whether the RenderView has crashed or not.
270 void set_render_view_created(bool created
) {
271 render_view_created_
= created
;
274 // Returns whether the RenderViewHost is currently waiting to hear the result
275 // of a before unload handler from the renderer.
276 bool is_waiting_for_beforeunload_ack() const {
277 return is_waiting_for_beforeunload_ack_
;
280 // Sets whether the RenderViewHost is currently swapped out, and thus
281 // filtering messages from the renderer.
282 void set_rvh_state(RenderViewHostImplState rvh_state
) {
283 rvh_state_
= rvh_state
;
286 // If set, navigations will appear to have loaded through a proxy
287 // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy).
289 void set_simulate_fetch_via_proxy(bool proxy
);
291 // If set, navigations will appear to have cleared the history list in the
293 // (FrameHostMsg_DidCommitProvisionalLoad_Params::history_list_was_cleared).
295 void set_simulate_history_list_was_cleared(bool cleared
);
297 // The opener route id passed to CreateRenderView().
298 int opener_route_id() const { return opener_route_id_
; }
300 // TODO(creis): Remove the need for these methods.
301 TestRenderFrameHost
* main_render_frame_host() const {
302 return main_render_frame_host_
;
304 void set_main_render_frame_host(TestRenderFrameHost
* rfh
) {
305 main_render_frame_host_
= rfh
;
308 // RenderViewHost overrides --------------------------------------------------
310 virtual bool CreateRenderView(const base::string16
& frame_name
,
313 bool window_was_created_with_opener
) OVERRIDE
;
314 virtual bool IsRenderViewLive() const OVERRIDE
;
315 virtual bool IsFullscreen() const OVERRIDE
;
318 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
320 void SendNavigateWithTransitionAndResponseCode(int page_id
,
322 PageTransition transition
,
325 // Calls OnNavigate on the RenderViewHost with the given information.
326 // Sets the rest of the parameters in the message to the "typical" values.
327 // This is a helper function for simulating the most common types of loads.
328 void SendNavigateWithParameters(
331 PageTransition transition
,
332 const GURL
& original_request_url
,
334 const base::FilePath
* file_path_for_history_item
);
336 // Tracks if the caller thinks if it created the RenderView. This is so we can
337 // respond to IsRenderViewLive appropriately.
338 bool render_view_created_
;
340 // See set_delete_counter() above. May be NULL.
341 int* delete_counter_
;
343 // See set_simulate_fetch_via_proxy() above.
344 bool simulate_fetch_via_proxy_
;
346 // See set_simulate_history_list_was_cleared() above.
347 bool simulate_history_list_was_cleared_
;
349 // See SetContentsMimeType() above.
350 std::string contents_mime_type_
;
352 // See opener_route_id() above.
353 int opener_route_id_
;
355 TestRenderFrameHost
* main_render_frame_host_
;
357 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
360 #if defined(COMPILER_MSVC)
364 // Adds methods to get straight at the impl classes.
365 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
367 RenderViewHostImplTestHarness();
368 virtual ~RenderViewHostImplTestHarness();
370 TestRenderViewHost
* test_rvh();
371 TestRenderViewHost
* pending_test_rvh();
372 TestRenderViewHost
* active_test_rvh();
373 TestRenderFrameHost
* main_test_rfh();
374 TestWebContents
* contents();
377 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
378 ScopedSetSupportedScaleFactors
;
379 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
380 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
383 } // namespace content
385 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_