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