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/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 gfx::Rect
GetViewBounds() const override
;
73 #if defined(OS_MACOSX)
74 void SetActive(bool active
) override
;
75 void SetWindowVisibility(bool visible
) override
{}
76 void WindowFrameChanged() override
{}
77 void ShowDefinitionForSelection() override
{}
78 bool SupportsSpeech() const override
;
79 void SpeakSelection() override
;
80 bool IsSpeaking() const override
;
81 void StopSpeaking() override
;
82 #endif // defined(OS_MACOSX)
83 void OnSwapCompositorFrame(uint32 output_surface_id
,
84 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
86 // RenderWidgetHostViewBase implementation.
87 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
88 const gfx::Rect
& pos
) override
{}
89 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
{}
90 void WasShown() override
{}
91 void WasHidden() 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 #if defined(OS_MACOSX) || defined(USE_AURA) || defined(OS_ANDROID)
104 void ImeCompositionRangeChanged(
105 const gfx::Range
& range
,
106 const std::vector
<gfx::Rect
>& character_bounds
) override
{}
108 void RenderProcessGone(base::TerminationStatus status
,
109 int error_code
) override
;
110 void Destroy() override
;
111 void SetTooltipText(const base::string16
& tooltip_text
) override
{}
112 void SelectionBoundsChanged(
113 const ViewHostMsg_SelectionBounds_Params
& params
) override
{}
114 void CopyFromCompositingSurface(const gfx::Rect
& src_subrect
,
115 const gfx::Size
& dst_size
,
116 CopyFromCompositingSurfaceCallback
& callback
,
117 const SkColorType color_type
) override
;
118 void CopyFromCompositingSurfaceToVideoFrame(
119 const gfx::Rect
& src_subrect
,
120 const scoped_refptr
<media::VideoFrame
>& target
,
121 const base::Callback
<void(bool)>& callback
) override
;
122 bool CanCopyToVideoFrame() const override
;
123 bool HasAcceleratedSurface(const gfx::Size
& desired_size
) override
;
124 #if defined(OS_MACOSX)
125 bool PostProcessEventForPluginIme(
126 const NativeWebKeyboardEvent
& event
) override
;
128 #if defined(OS_ANDROID)
129 virtual void LockCompositingSurface() override
{}
130 virtual void UnlockCompositingSurface() override
{}
132 void GetScreenInfo(blink::WebScreenInfo
* results
) override
{}
133 gfx::Rect
GetBoundsInRootWindow() override
;
134 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
135 bool LockMouse() override
;
136 void UnlockMouse() override
;
138 virtual void SetParentNativeViewAccessible(
139 gfx::NativeViewAccessible accessible_parent
) override
;
140 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const override
;
143 bool is_showing() const { return is_showing_
; }
144 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
147 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_