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::Vector2dF
GetLastScrollOffset() const OVERRIDE
;
64 virtual gfx::NativeView
GetNativeView() const OVERRIDE
;
65 virtual gfx::NativeViewId
GetNativeViewId() const OVERRIDE
;
66 virtual gfx::NativeViewAccessible
GetNativeViewAccessible() OVERRIDE
;
67 virtual ui::TextInputClient
* GetTextInputClient() OVERRIDE
;
68 virtual bool HasFocus() const OVERRIDE
;
69 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE
;
70 virtual void Show() OVERRIDE
;
71 virtual void Hide() OVERRIDE
;
72 virtual bool IsShowing() OVERRIDE
;
73 virtual gfx::Rect
GetViewBounds() const OVERRIDE
;
74 #if defined(OS_MACOSX)
75 virtual void SetActive(bool active
) OVERRIDE
;
76 virtual void SetTakesFocusOnlyOnMouseDown(bool flag
) OVERRIDE
{}
77 virtual void SetWindowVisibility(bool visible
) OVERRIDE
{}
78 virtual void WindowFrameChanged() OVERRIDE
{}
79 virtual void ShowDefinitionForSelection() OVERRIDE
{}
80 virtual bool SupportsSpeech() const OVERRIDE
;
81 virtual void SpeakSelection() OVERRIDE
;
82 virtual bool IsSpeaking() const OVERRIDE
;
83 virtual void StopSpeaking() OVERRIDE
;
84 #endif // defined(OS_MACOSX)
85 virtual void OnSwapCompositorFrame(
86 uint32 output_surface_id
,
87 scoped_ptr
<cc::CompositorFrame
> frame
) OVERRIDE
;
89 // RenderWidgetHostViewBase implementation.
90 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
91 const gfx::Rect
& pos
) OVERRIDE
{}
92 virtual void InitAsFullscreen(
93 RenderWidgetHostView
* reference_host_view
) OVERRIDE
{}
94 virtual void WasShown() OVERRIDE
{}
95 virtual void WasHidden() OVERRIDE
{}
96 virtual void MovePluginWindows(
97 const std::vector
<WebPluginGeometry
>& moves
) OVERRIDE
{}
98 virtual void Focus() OVERRIDE
{}
99 virtual void Blur() OVERRIDE
{}
100 virtual void SetIsLoading(bool is_loading
) OVERRIDE
{}
101 virtual void UpdateCursor(const WebCursor
& cursor
) OVERRIDE
{}
102 virtual void TextInputStateChanged(
103 const ViewHostMsg_TextInputState_Params
& params
) 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 Destroy() OVERRIDE
;
113 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
{}
114 virtual void SelectionBoundsChanged(
115 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
{}
116 virtual void CopyFromCompositingSurface(
117 const gfx::Rect
& src_subrect
,
118 const gfx::Size
& dst_size
,
119 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
120 const SkColorType color_type
) OVERRIDE
;
121 virtual void CopyFromCompositingSurfaceToVideoFrame(
122 const gfx::Rect
& src_subrect
,
123 const scoped_refptr
<media::VideoFrame
>& target
,
124 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
125 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
126 virtual void AcceleratedSurfaceInitialized(int host_id
,
127 int route_id
) OVERRIDE
;
128 virtual void AcceleratedSurfaceBuffersSwapped(
129 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
130 int gpu_host_id
) OVERRIDE
;
131 virtual void AcceleratedSurfacePostSubBuffer(
132 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
133 int gpu_host_id
) OVERRIDE
;
134 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
135 virtual void AcceleratedSurfaceRelease() OVERRIDE
{}
136 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
137 #if defined(OS_MACOSX)
138 virtual bool PostProcessEventForPluginIme(
139 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
140 #elif defined(OS_ANDROID)
141 virtual void ShowDisambiguationPopup(
142 const gfx::Rect
& target_rect
,
143 const SkBitmap
& zoomed_bitmap
) OVERRIDE
{}
144 virtual void LockCompositingSurface() OVERRIDE
{}
145 virtual void UnlockCompositingSurface() OVERRIDE
{}
147 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
{}
148 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
149 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
150 virtual bool LockMouse() OVERRIDE
;
151 virtual void UnlockMouse() OVERRIDE
;
153 virtual void SetParentNativeViewAccessible(
154 gfx::NativeViewAccessible accessible_parent
) OVERRIDE
;
155 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const OVERRIDE
;
158 bool is_showing() const { return is_showing_
; }
159 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
162 RenderWidgetHostImpl
* rwh_
;
166 bool did_swap_compositor_frame_
;
167 ui::DummyTextInputClient text_input_client_
;
170 #if defined(COMPILER_MSVC)
171 // See comment for same warning on RenderViewHostImpl.
172 #pragma warning(push)
173 #pragma warning(disable: 4250)
176 // TestRenderViewHost ----------------------------------------------------------
178 // TODO(brettw) this should use a TestWebContents which should be generalized
179 // from the WebContentsImpl test. We will probably also need that class' version
180 // of CreateRenderViewForRenderManager when more complicated tests start using
183 // Note that users outside of content must use this class by getting
184 // the separate RenderViewHostTester interface via
185 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
188 // Users within content may directly static_cast from a
189 // RenderViewHost* to a TestRenderViewHost*.
191 // The reasons we do it this way rather than extending the parallel
192 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
193 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
195 // a) Extending the parallel class hierarchy further would require
196 // more classes to use virtual inheritance. This is a complexity that
197 // is better to avoid, especially when it would be introduced in the
198 // production code solely to facilitate testing code.
200 // b) While users outside of content only need to drive tests on a
201 // RenderViewHost, content needs a test version of the full
202 // RenderViewHostImpl so that it can test all methods on that concrete
203 // class (e.g. overriding a method such as
204 // RenderViewHostImpl::CreateRenderView). This would have complicated
205 // the dual class hierarchy even further.
207 // The reason we do it this way instead of using composition is
208 // similar to (b) above, essentially it gets very tricky. By using
209 // the split interface we avoid complexity within content and maintain
210 // reasonable utility for embedders.
211 class TestRenderViewHost
212 : public RenderViewHostImpl
,
213 public RenderViewHostTester
{
215 TestRenderViewHost(SiteInstance
* instance
,
216 RenderViewHostDelegate
* delegate
,
217 RenderWidgetHostDelegate
* widget_delegate
,
219 int main_frame_routing_id
,
221 virtual ~TestRenderViewHost();
223 // RenderViewHostTester implementation. Note that CreateRenderView
224 // is not specified since it is synonymous with the one from
225 // RenderViewHostImpl, see below.
226 virtual void SendBeforeUnloadACK(bool proceed
) OVERRIDE
;
227 virtual void SetContentsMimeType(const std::string
& mime_type
) OVERRIDE
;
228 virtual void SimulateSwapOutACK() OVERRIDE
;
229 virtual void SimulateWasHidden() OVERRIDE
;
230 virtual void SimulateWasShown() OVERRIDE
;
232 // NOTE: These methods are deprecated and the equivalents in
233 // TestRenderFrameHost should be used.
234 virtual void SendNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
235 virtual void SendFailedNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
236 virtual void SendNavigateWithTransition(int page_id
, const GURL
& url
,
237 PageTransition transition
) OVERRIDE
;
239 // Calls OnNavigate on the RenderViewHost with the given information,
240 // including a custom original request URL. Sets the rest of the
241 // parameters in the message to the "typical" values. This is a helper
242 // function for simulating the most common types of loads.
243 void SendNavigateWithOriginalRequestURL(
244 int page_id
, const GURL
& url
, const GURL
& original_request_url
);
246 void SendNavigateWithFile(
247 int page_id
, const GURL
& url
, const base::FilePath
& file_path
);
249 void SendNavigateWithParams(
250 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
);
252 void TestOnUpdateStateWithFile(
253 int process_id
, const base::FilePath
& file_path
);
255 void TestOnStartDragging(const DropData
& drop_data
);
257 // If set, *delete_counter is incremented when this object destructs.
258 void set_delete_counter(int* delete_counter
) {
259 delete_counter_
= delete_counter
;
262 // Sets whether the RenderView currently exists or not. This controls the
263 // return value from IsRenderViewLive, which the rest of the system uses to
264 // check whether the RenderView has crashed or not.
265 void set_render_view_created(bool created
) {
266 render_view_created_
= created
;
269 // Returns whether the RenderViewHost is currently waiting to hear the result
270 // of a before unload handler from the renderer.
271 bool is_waiting_for_beforeunload_ack() const {
272 return is_waiting_for_beforeunload_ack_
;
275 // Sets whether the RenderViewHost is currently swapped out, and thus
276 // filtering messages from the renderer.
277 void set_rvh_state(RenderViewHostImplState rvh_state
) {
278 rvh_state_
= rvh_state
;
281 // If set, navigations will appear to have loaded through a proxy
282 // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy).
284 void set_simulate_fetch_via_proxy(bool proxy
);
286 // If set, navigations will appear to have cleared the history list in the
288 // (FrameHostMsg_DidCommitProvisionalLoad_Params::history_list_was_cleared).
290 void set_simulate_history_list_was_cleared(bool cleared
);
292 // The opener route id passed to CreateRenderView().
293 int opener_route_id() const { return opener_route_id_
; }
295 // TODO(creis): Remove the need for these methods.
296 TestRenderFrameHost
* main_render_frame_host() const {
297 return main_render_frame_host_
;
299 void set_main_render_frame_host(TestRenderFrameHost
* rfh
) {
300 main_render_frame_host_
= rfh
;
303 // RenderViewHost overrides --------------------------------------------------
305 virtual bool CreateRenderView(const base::string16
& frame_name
,
309 bool window_was_created_with_opener
) OVERRIDE
;
310 virtual bool IsRenderViewLive() const OVERRIDE
;
311 virtual bool IsFullscreen() const OVERRIDE
;
314 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
316 void SendNavigateWithTransitionAndResponseCode(int page_id
,
318 PageTransition transition
,
321 // Calls OnNavigate on the RenderViewHost with the given information.
322 // Sets the rest of the parameters in the message to the "typical" values.
323 // This is a helper function for simulating the most common types of loads.
324 void SendNavigateWithParameters(
327 PageTransition transition
,
328 const GURL
& original_request_url
,
330 const base::FilePath
* file_path_for_history_item
);
332 // Tracks if the caller thinks if it created the RenderView. This is so we can
333 // respond to IsRenderViewLive appropriately.
334 bool render_view_created_
;
336 // See set_delete_counter() above. May be NULL.
337 int* delete_counter_
;
339 // See set_simulate_fetch_via_proxy() above.
340 bool simulate_fetch_via_proxy_
;
342 // See set_simulate_history_list_was_cleared() above.
343 bool simulate_history_list_was_cleared_
;
345 // See SetContentsMimeType() above.
346 std::string contents_mime_type_
;
348 // See opener_route_id() above.
349 int opener_route_id_
;
351 TestRenderFrameHost
* main_render_frame_host_
;
353 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
356 #if defined(COMPILER_MSVC)
360 // Adds methods to get straight at the impl classes.
361 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
363 RenderViewHostImplTestHarness();
364 virtual ~RenderViewHostImplTestHarness();
366 // contents() is equivalent to static_cast<TestWebContents*>(web_contents())
367 TestWebContents
* contents();
369 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
371 // test_rvh() is equivalent to any of the following:
372 // contents()->GetMainFrame()->GetRenderViewHost()
373 // contents()->GetRenderViewHost()
374 // static_cast<TestRenderViewHost*>(rvh())
376 // Since most functionality will eventually shift from RVH to RFH, you may
377 // prefer to use the GetMainFrame() method in tests.
378 TestRenderViewHost
* test_rvh();
380 // pending_test_rvh() is equivalent to all of the following:
381 // contents()->GetPendingMainFrame()->GetRenderViewHost() [if frame exists]
382 // contents()->GetPendingRenderViewHost()
383 // static_cast<TestRenderViewHost*>(pending_rvh())
385 // Since most functionality will eventually shift from RVH to RFH, you may
386 // prefer to use the GetPendingMainFrame() method in tests.
387 TestRenderViewHost
* pending_test_rvh();
389 // active_test_rvh() is equivalent to:
390 // contents()->GetPendingRenderViewHost() ?
391 // contents()->GetPendingRenderViewHost() :
392 // contents()->GetRenderViewHost();
393 TestRenderViewHost
* active_test_rvh();
395 // main_test_rfh() is equivalent to contents()->GetMainFrame()
396 // TODO(nick): Replace all uses with contents()->GetMainFrame()
397 TestRenderFrameHost
* main_test_rfh();
400 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
401 ScopedSetSupportedScaleFactors
;
402 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
403 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
406 } // namespace content
408 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_