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/test/test_renderer_host.h"
17 #include "ui/base/ime/dummy_text_input_client.h"
18 #include "ui/base/layout.h"
19 #include "ui/base/page_transition_types.h"
20 #include "ui/gfx/geometry/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
;
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(FrameHostMsg_DidCommitProvisionalLoad_Params
* params
,
46 ui::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 ~TestRenderWidgetHostView() override
;
57 // RenderWidgetHostView implementation.
58 void InitAsChild(gfx::NativeView parent_view
) override
{}
59 RenderWidgetHost
* GetRenderWidgetHost() const override
;
60 void SetSize(const gfx::Size
& size
) override
{}
61 void SetBounds(const gfx::Rect
& rect
) override
{}
62 gfx::Vector2dF
GetLastScrollOffset() const override
;
63 gfx::NativeView
GetNativeView() const override
;
64 gfx::NativeViewId
GetNativeViewId() const override
;
65 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
66 ui::TextInputClient
* GetTextInputClient() override
;
67 bool HasFocus() const override
;
68 bool IsSurfaceAvailableForCopy() const override
;
71 bool IsShowing() override
;
72 void WasUnOccluded() override
;
73 void WasOccluded() override
;
74 gfx::Rect
GetViewBounds() const override
;
75 #if defined(OS_MACOSX)
76 void SetActive(bool active
) override
;
77 void SetWindowVisibility(bool visible
) override
{}
78 void WindowFrameChanged() override
{}
79 void ShowDefinitionForSelection() override
{}
80 bool SupportsSpeech() const override
;
81 void SpeakSelection() override
;
82 bool IsSpeaking() const override
;
83 void StopSpeaking() override
;
84 #endif // defined(OS_MACOSX)
85 void OnSwapCompositorFrame(uint32 output_surface_id
,
86 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
88 // RenderWidgetHostViewBase implementation.
89 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
90 const gfx::Rect
& pos
) override
{}
91 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
{}
92 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
{
94 void Focus() override
{}
95 void Blur() override
{}
96 void SetIsLoading(bool is_loading
) override
{}
97 void UpdateCursor(const WebCursor
& cursor
) override
{}
98 void TextInputTypeChanged(ui::TextInputType type
,
99 ui::TextInputMode input_mode
,
100 bool can_compose_inline
,
101 int flags
) override
{}
102 void ImeCancelComposition() override
{}
103 void ImeCompositionRangeChanged(
104 const gfx::Range
& range
,
105 const std::vector
<gfx::Rect
>& character_bounds
) override
{}
106 void RenderProcessGone(base::TerminationStatus status
,
107 int error_code
) override
;
108 void Destroy() override
;
109 void SetTooltipText(const base::string16
& tooltip_text
) override
{}
110 void SelectionBoundsChanged(
111 const ViewHostMsg_SelectionBounds_Params
& params
) override
{}
112 void CopyFromCompositingSurface(const gfx::Rect
& src_subrect
,
113 const gfx::Size
& dst_size
,
114 ReadbackRequestCallback
& callback
,
115 const SkColorType color_type
) override
;
116 void CopyFromCompositingSurfaceToVideoFrame(
117 const gfx::Rect
& src_subrect
,
118 const scoped_refptr
<media::VideoFrame
>& target
,
119 const base::Callback
<void(bool)>& callback
) override
;
120 bool CanCopyToVideoFrame() const override
;
121 bool HasAcceleratedSurface(const gfx::Size
& desired_size
) override
;
122 #if defined(OS_MACOSX)
123 bool PostProcessEventForPluginIme(
124 const NativeWebKeyboardEvent
& event
) override
;
126 #if defined(OS_ANDROID)
127 virtual void LockCompositingSurface() override
{}
128 virtual void UnlockCompositingSurface() override
{}
130 void GetScreenInfo(blink::WebScreenInfo
* results
) override
{}
131 gfx::Rect
GetBoundsInRootWindow() override
;
132 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
133 bool LockMouse() override
;
134 void UnlockMouse() override
;
136 virtual void SetParentNativeViewAccessible(
137 gfx::NativeViewAccessible accessible_parent
) override
;
138 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const override
;
141 bool is_showing() const { return is_showing_
; }
142 bool is_occluded() const { return is_occluded_
; }
143 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
146 RenderWidgetHostImpl
* rwh_
;
151 bool did_swap_compositor_frame_
;
152 ui::DummyTextInputClient text_input_client_
;
155 #if defined(COMPILER_MSVC)
156 // See comment for same warning on RenderViewHostImpl.
157 #pragma warning(push)
158 #pragma warning(disable: 4250)
161 // TestRenderViewHost ----------------------------------------------------------
163 // TODO(brettw) this should use a TestWebContents which should be generalized
164 // from the WebContentsImpl test. We will probably also need that class' version
165 // of CreateRenderViewForRenderManager when more complicated tests start using
168 // Note that users outside of content must use this class by getting
169 // the separate RenderViewHostTester interface via
170 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
173 // Users within content may directly static_cast from a
174 // RenderViewHost* to a TestRenderViewHost*.
176 // The reasons we do it this way rather than extending the parallel
177 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
178 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
180 // a) Extending the parallel class hierarchy further would require
181 // more classes to use virtual inheritance. This is a complexity that
182 // is better to avoid, especially when it would be introduced in the
183 // production code solely to facilitate testing code.
185 // b) While users outside of content only need to drive tests on a
186 // RenderViewHost, content needs a test version of the full
187 // RenderViewHostImpl so that it can test all methods on that concrete
188 // class (e.g. overriding a method such as
189 // RenderViewHostImpl::CreateRenderView). This would have complicated
190 // the dual class hierarchy even further.
192 // The reason we do it this way instead of using composition is
193 // similar to (b) above, essentially it gets very tricky. By using
194 // the split interface we avoid complexity within content and maintain
195 // reasonable utility for embedders.
196 class TestRenderViewHost
197 : public RenderViewHostImpl
,
198 public RenderViewHostTester
{
200 TestRenderViewHost(SiteInstance
* instance
,
201 RenderViewHostDelegate
* delegate
,
202 RenderWidgetHostDelegate
* widget_delegate
,
204 int main_frame_routing_id
,
206 ~TestRenderViewHost() override
;
208 // RenderViewHostTester implementation. Note that CreateRenderView
209 // is not specified since it is synonymous with the one from
210 // RenderViewHostImpl, see below.
211 void SimulateWasHidden() override
;
212 void SimulateWasShown() override
;
214 void TestOnUpdateStateWithFile(
215 int page_id
, const base::FilePath
& file_path
);
217 void TestOnStartDragging(const DropData
& drop_data
);
219 // If set, *delete_counter is incremented when this object destructs.
220 void set_delete_counter(int* delete_counter
) {
221 delete_counter_
= delete_counter
;
224 // Sets whether the RenderView currently exists or not. This controls the
225 // return value from IsRenderViewLive, which the rest of the system uses to
226 // check whether the RenderView has crashed or not.
227 void set_render_view_created(bool created
) {
228 render_view_created_
= created
;
231 // The opener route id passed to CreateRenderView().
232 int opener_route_id() const { return opener_route_id_
; }
234 // RenderViewHost overrides --------------------------------------------------
236 bool CreateRenderView(const base::string16
& frame_name
,
240 bool window_was_created_with_opener
) override
;
241 bool IsRenderViewLive() const override
;
242 bool IsFullscreen() const override
;
245 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
247 void SendNavigateWithTransitionAndResponseCode(int page_id
,
249 ui::PageTransition transition
,
252 // Calls OnNavigate on the RenderViewHost with the given information.
253 // Sets the rest of the parameters in the message to the "typical" values.
254 // This is a helper function for simulating the most common types of loads.
255 void SendNavigateWithParameters(
258 ui::PageTransition transition
,
259 const GURL
& original_request_url
,
261 const base::FilePath
* file_path_for_history_item
);
263 // Tracks if the caller thinks if it created the RenderView. This is so we can
264 // respond to IsRenderViewLive appropriately.
265 bool render_view_created_
;
267 // See set_delete_counter() above. May be NULL.
268 int* delete_counter_
;
270 // See opener_route_id() above.
271 int opener_route_id_
;
273 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
276 #if defined(COMPILER_MSVC)
280 // Adds methods to get straight at the impl classes.
281 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
283 RenderViewHostImplTestHarness();
284 ~RenderViewHostImplTestHarness() override
;
286 // contents() is equivalent to static_cast<TestWebContents*>(web_contents())
287 TestWebContents
* contents();
289 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
291 // test_rvh() is equivalent to any of the following:
292 // contents()->GetMainFrame()->GetRenderViewHost()
293 // contents()->GetRenderViewHost()
294 // static_cast<TestRenderViewHost*>(rvh())
296 // Since most functionality will eventually shift from RVH to RFH, you may
297 // prefer to use the GetMainFrame() method in tests.
298 TestRenderViewHost
* test_rvh();
300 // pending_test_rvh() is equivalent to all of the following:
301 // contents()->GetPendingMainFrame()->GetRenderViewHost() [if frame exists]
302 // contents()->GetPendingRenderViewHost()
303 // static_cast<TestRenderViewHost*>(pending_rvh())
305 // Since most functionality will eventually shift from RVH to RFH, you may
306 // prefer to use the GetPendingMainFrame() method in tests.
307 TestRenderViewHost
* pending_test_rvh();
309 // active_test_rvh() is equivalent to:
310 // contents()->GetPendingRenderViewHost() ?
311 // contents()->GetPendingRenderViewHost() :
312 // contents()->GetRenderViewHost();
313 TestRenderViewHost
* active_test_rvh();
315 // main_test_rfh() is equivalent to contents()->GetMainFrame()
316 // TODO(nick): Replace all uses with contents()->GetMainFrame()
317 TestRenderFrameHost
* main_test_rfh();
320 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
321 ScopedSetSupportedScaleFactors
;
322 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
323 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
326 } // namespace content
328 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_