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/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 ViewHostMsg_FrameNavigate_Params
;
38 class TestRenderFrameHost
;
39 class TestWebContents
;
41 // Utility function to initialize ViewHostMsg_NavigateParams_Params
42 // with given |page_id|, |url| and |transition_type|.
43 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params
* params
,
46 PageTransition transition_type
);
48 // TestRenderViewHostView ------------------------------------------------------
50 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
51 // without having side-effects.
52 class TestRenderWidgetHostView
: public RenderWidgetHostViewBase
{
54 explicit TestRenderWidgetHostView(RenderWidgetHost
* rwh
);
55 virtual ~TestRenderWidgetHostView();
57 // RenderWidgetHostView implementation.
58 virtual void InitAsChild(gfx::NativeView parent_view
) OVERRIDE
{}
59 virtual RenderWidgetHost
* GetRenderWidgetHost() const OVERRIDE
;
60 virtual void SetSize(const gfx::Size
& size
) OVERRIDE
{}
61 virtual void SetBounds(const gfx::Rect
& rect
) OVERRIDE
{}
62 virtual gfx::NativeView
GetNativeView() const OVERRIDE
;
63 virtual gfx::NativeViewId
GetNativeViewId() const OVERRIDE
;
64 virtual gfx::NativeViewAccessible
GetNativeViewAccessible() OVERRIDE
;
65 virtual bool HasFocus() const OVERRIDE
;
66 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE
;
67 virtual void Show() OVERRIDE
;
68 virtual void Hide() OVERRIDE
;
69 virtual bool IsShowing() OVERRIDE
;
70 virtual gfx::Rect
GetViewBounds() const OVERRIDE
;
71 #if defined(OS_MACOSX)
72 virtual void SetActive(bool active
) OVERRIDE
;
73 virtual void SetTakesFocusOnlyOnMouseDown(bool flag
) OVERRIDE
{}
74 virtual void SetWindowVisibility(bool visible
) OVERRIDE
{}
75 virtual void WindowFrameChanged() OVERRIDE
{}
76 virtual void ShowDefinitionForSelection() OVERRIDE
{}
77 virtual bool SupportsSpeech() const OVERRIDE
;
78 virtual void SpeakSelection() OVERRIDE
;
79 virtual bool IsSpeaking() const OVERRIDE
;
80 virtual void StopSpeaking() OVERRIDE
;
81 #endif // defined(OS_MACOSX)
82 #if defined(TOOLKIT_GTK)
83 virtual GdkEventButton
* GetLastMouseDown() OVERRIDE
;
84 virtual gfx::NativeView
BuildInputMethodsGtkMenu() OVERRIDE
;
85 #endif // defined(TOOLKIT_GTK)
86 virtual void OnSwapCompositorFrame(
87 uint32 output_surface_id
,
88 scoped_ptr
<cc::CompositorFrame
> frame
) OVERRIDE
;
90 // RenderWidgetHostViewPort implementation.
91 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
92 const gfx::Rect
& pos
) OVERRIDE
{}
93 virtual void InitAsFullscreen(
94 RenderWidgetHostView
* reference_host_view
) OVERRIDE
{}
95 virtual void WasShown() OVERRIDE
{}
96 virtual void WasHidden() OVERRIDE
{}
97 virtual void MovePluginWindows(
98 const gfx::Vector2d
& scroll_offset
,
99 const std::vector
<WebPluginGeometry
>& moves
) OVERRIDE
{}
100 virtual void Focus() OVERRIDE
{}
101 virtual void Blur() OVERRIDE
{}
102 virtual void SetIsLoading(bool is_loading
) OVERRIDE
{}
103 virtual void UpdateCursor(const WebCursor
& cursor
) OVERRIDE
{}
104 virtual void TextInputTypeChanged(ui::TextInputType type
,
105 ui::TextInputMode input_mode
,
106 bool can_compose_inline
) OVERRIDE
{}
107 virtual void ImeCancelComposition() OVERRIDE
{}
108 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
109 virtual void ImeCompositionRangeChanged(
110 const gfx::Range
& range
,
111 const std::vector
<gfx::Rect
>& character_bounds
) OVERRIDE
{}
113 virtual void DidUpdateBackingStore(
114 const gfx::Rect
& scroll_rect
,
115 const gfx::Vector2d
& scroll_delta
,
116 const std::vector
<gfx::Rect
>& rects
,
117 const std::vector
<ui::LatencyInfo
>& latency_info
) OVERRIDE
{}
118 virtual void RenderProcessGone(base::TerminationStatus status
,
119 int error_code
) OVERRIDE
;
120 virtual void WillDestroyRenderWidget(RenderWidgetHost
* rwh
) { }
121 virtual void Destroy() OVERRIDE
;
122 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
{}
123 virtual void SelectionBoundsChanged(
124 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
{}
125 virtual void ScrollOffsetChanged() OVERRIDE
{}
126 virtual BackingStore
* AllocBackingStore(const gfx::Size
& size
) OVERRIDE
;
127 virtual void CopyFromCompositingSurface(
128 const gfx::Rect
& src_subrect
,
129 const gfx::Size
& dst_size
,
130 const base::Callback
<void(bool, const SkBitmap
&)>& callback
) OVERRIDE
;
131 virtual void CopyFromCompositingSurfaceToVideoFrame(
132 const gfx::Rect
& src_subrect
,
133 const scoped_refptr
<media::VideoFrame
>& target
,
134 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
135 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
136 virtual void OnAcceleratedCompositingStateChange() OVERRIDE
;
137 virtual void AcceleratedSurfaceInitialized(int host_id
,
138 int route_id
) OVERRIDE
;
139 virtual void AcceleratedSurfaceBuffersSwapped(
140 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
141 int gpu_host_id
) OVERRIDE
;
142 virtual void AcceleratedSurfacePostSubBuffer(
143 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
144 int gpu_host_id
) OVERRIDE
;
145 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
146 virtual void AcceleratedSurfaceRelease() OVERRIDE
{}
147 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
148 #if defined(OS_MACOSX)
149 virtual bool PostProcessEventForPluginIme(
150 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
151 #elif defined(OS_ANDROID)
152 virtual void ShowDisambiguationPopup(
153 const gfx::Rect
& target_rect
,
154 const SkBitmap
& zoomed_bitmap
) OVERRIDE
{}
155 virtual void HasTouchEventHandlers(bool need_touch_events
) OVERRIDE
{}
156 #elif defined(OS_WIN) && !defined(USE_AURA)
157 virtual void WillWmDestroy() OVERRIDE
;
159 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
{}
160 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
161 virtual void SetHasHorizontalScrollbar(
162 bool has_horizontal_scrollbar
) OVERRIDE
{ }
163 virtual void SetScrollOffsetPinning(
164 bool is_pinned_to_left
, bool is_pinned_to_right
) OVERRIDE
{ }
165 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
166 #if defined(OS_WIN) && !defined(USE_AURA)
167 virtual void SetClickthroughRegion(SkRegion
* region
) OVERRIDE
;
169 virtual bool LockMouse() OVERRIDE
;
170 virtual void UnlockMouse() OVERRIDE
;
171 #if defined(OS_WIN) && defined(USE_AURA)
172 virtual void SetParentNativeViewAccessible(
173 gfx::NativeViewAccessible accessible_parent
) OVERRIDE
;
174 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const OVERRIDE
;
177 bool is_showing() const { return is_showing_
; }
178 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
181 RenderWidgetHostImpl
* rwh_
;
185 bool did_swap_compositor_frame_
;
188 #if defined(COMPILER_MSVC)
189 // See comment for same warning on RenderViewHostImpl.
190 #pragma warning(push)
191 #pragma warning(disable: 4250)
194 // TestRenderViewHost ----------------------------------------------------------
196 // TODO(brettw) this should use a TestWebContents which should be generalized
197 // from the WebContentsImpl test. We will probably also need that class' version
198 // of CreateRenderViewForRenderManager when more complicated tests start using
201 // Note that users outside of content must use this class by getting
202 // the separate RenderViewHostTester interface via
203 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
206 // Users within content may directly static_cast from a
207 // RenderViewHost* to a TestRenderViewHost*.
209 // The reasons we do it this way rather than extending the parallel
210 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
211 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
213 // a) Extending the parallel class hierarchy further would require
214 // more classes to use virtual inheritance. This is a complexity that
215 // is better to avoid, especially when it would be introduced in the
216 // production code solely to facilitate testing code.
218 // b) While users outside of content only need to drive tests on a
219 // RenderViewHost, content needs a test version of the full
220 // RenderViewHostImpl so that it can test all methods on that concrete
221 // class (e.g. overriding a method such as
222 // RenderViewHostImpl::CreateRenderView). This would have complicated
223 // the dual class hierarchy even further.
225 // The reason we do it this way instead of using composition is
226 // similar to (b) above, essentially it gets very tricky. By using
227 // the split interface we avoid complexity within content and maintain
228 // reasonable utility for embedders.
229 class TestRenderViewHost
230 : public RenderViewHostImpl
,
231 public RenderViewHostTester
{
233 TestRenderViewHost(SiteInstance
* instance
,
234 RenderViewHostDelegate
* delegate
,
235 RenderWidgetHostDelegate
* widget_delegate
,
237 int main_frame_routing_id
,
239 virtual ~TestRenderViewHost();
241 // RenderViewHostTester implementation. Note that CreateRenderView
242 // is not specified since it is synonymous with the one from
243 // RenderViewHostImpl, see below.
244 virtual void SendNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
245 virtual void SendFailedNavigate(int page_id
, const GURL
& url
) OVERRIDE
;
246 virtual void SendNavigateWithTransition(int page_id
, const GURL
& url
,
247 PageTransition transition
) OVERRIDE
;
248 virtual void SendShouldCloseACK(bool proceed
) OVERRIDE
;
249 virtual void SetContentsMimeType(const std::string
& mime_type
) OVERRIDE
;
250 virtual void SimulateSwapOutACK() OVERRIDE
;
251 virtual void SimulateWasHidden() OVERRIDE
;
252 virtual void SimulateWasShown() OVERRIDE
;
254 // Calls OnNavigate on the RenderViewHost with the given information,
255 // including a custom original request URL. Sets the rest of the
256 // parameters in the message to the "typical" values. This is a helper
257 // function for simulating the most common types of loads.
258 void SendNavigateWithOriginalRequestURL(
259 int page_id
, const GURL
& url
, const GURL
& original_request_url
);
261 void SendNavigateWithFile(
262 int page_id
, const GURL
& url
, const base::FilePath
& file_path
);
264 void SendNavigateWithParams(ViewHostMsg_FrameNavigate_Params
* params
);
266 void TestOnUpdateStateWithFile(
267 int process_id
, const base::FilePath
& file_path
);
269 void TestOnStartDragging(const DropData
& drop_data
);
271 // If set, *delete_counter is incremented when this object destructs.
272 void set_delete_counter(int* delete_counter
) {
273 delete_counter_
= delete_counter
;
276 // Sets whether the RenderView currently exists or not. This controls the
277 // return value from IsRenderViewLive, which the rest of the system uses to
278 // check whether the RenderView has crashed or not.
279 void set_render_view_created(bool created
) {
280 render_view_created_
= created
;
283 // Returns whether the RenderViewHost is currently waiting to hear the result
284 // of a before unload handler from the renderer.
285 bool is_waiting_for_beforeunload_ack() const {
286 return is_waiting_for_beforeunload_ack_
;
289 // Returns whether the RenderViewHost is currently waiting to hear the result
290 // of an unload handler from the renderer.
291 bool is_waiting_for_unload_ack() const {
292 return is_waiting_for_unload_ack_
;
295 // Sets whether the RenderViewHost is currently swapped out, and thus
296 // filtering messages from the renderer.
297 void set_is_swapped_out(bool is_swapped_out
) {
298 is_swapped_out_
= is_swapped_out
;
301 // If set, navigations will appear to have loaded through a proxy
302 // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy).
304 void set_simulate_fetch_via_proxy(bool proxy
);
306 // If set, navigations will appear to have cleared the history list in the
307 // RenderView (ViewHostMsg_FrameNavigate_Params::history_list_was_cleared).
309 void set_simulate_history_list_was_cleared(bool cleared
);
311 // The opener route id passed to CreateRenderView().
312 int opener_route_id() const { return opener_route_id_
; }
314 // TODO(creis): Remove the need for these methods.
315 TestRenderFrameHost
* main_render_frame_host() const {
316 return main_render_frame_host_
;
318 void set_main_render_frame_host(TestRenderFrameHost
* rfh
) {
319 main_render_frame_host_
= rfh
;
322 // RenderViewHost overrides --------------------------------------------------
324 virtual bool CreateRenderView(const base::string16
& frame_name
,
326 int32 max_page_id
) OVERRIDE
;
327 virtual bool IsRenderViewLive() const OVERRIDE
;
330 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
332 void SendNavigateWithTransitionAndResponseCode(int page_id
,
334 PageTransition transition
,
337 // Calls OnNavigate on the RenderViewHost with the given information.
338 // Sets the rest of the parameters in the message to the "typical" values.
339 // This is a helper function for simulating the most common types of loads.
340 void SendNavigateWithParameters(
343 PageTransition transition
,
344 const GURL
& original_request_url
,
346 const base::FilePath
* file_path_for_history_item
);
348 // Tracks if the caller thinks if it created the RenderView. This is so we can
349 // respond to IsRenderViewLive appropriately.
350 bool render_view_created_
;
352 // See set_delete_counter() above. May be NULL.
353 int* delete_counter_
;
355 // See set_simulate_fetch_via_proxy() above.
356 bool simulate_fetch_via_proxy_
;
358 // See set_simulate_history_list_was_cleared() above.
359 bool simulate_history_list_was_cleared_
;
361 // See SetContentsMimeType() above.
362 std::string contents_mime_type_
;
364 // See opener_route_id() above.
365 int opener_route_id_
;
367 TestRenderFrameHost
* main_render_frame_host_
;
369 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
372 #if defined(COMPILER_MSVC)
376 // Adds methods to get straight at the impl classes.
377 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
379 RenderViewHostImplTestHarness();
380 virtual ~RenderViewHostImplTestHarness();
382 TestRenderViewHost
* test_rvh();
383 TestRenderViewHost
* pending_test_rvh();
384 TestRenderViewHost
* active_test_rvh();
385 TestRenderFrameHost
* main_test_rfh();
386 TestWebContents
* contents();
389 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
390 ScopedSetSupportedScaleFactors
;
391 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
392 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
395 } // namespace content
397 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_