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/mock_render_process_host.h"
18 #include "content/public/test/test_renderer_host.h"
19 #include "ui/base/ime/dummy_text_input_client.h"
20 #include "ui/base/layout.h"
21 #include "ui/base/page_transition_types.h"
22 #include "ui/gfx/geometry/vector2d_f.h"
24 // This file provides a testing framework for mocking out the RenderProcessHost
25 // layer. It allows you to test RenderViewHost, WebContentsImpl,
26 // NavigationController, and other layers above that without running an actual
29 // To use, derive your test base class from RenderViewHostImplTestHarness.
31 struct FrameHostMsg_DidCommitProvisionalLoad_Params
;
40 class TestRenderFrameHost
;
41 class TestWebContents
;
43 // Utility function to initialize ViewHostMsg_NavigateParams_Params
44 // with given |page_id|, |url| and |transition_type|.
45 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params
* params
,
48 ui::PageTransition transition_type
);
50 // TestRenderViewHostView ------------------------------------------------------
52 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
53 // without having side-effects.
54 class TestRenderWidgetHostView
: public RenderWidgetHostViewBase
{
56 explicit TestRenderWidgetHostView(RenderWidgetHost
* rwh
);
57 ~TestRenderWidgetHostView() override
;
59 // RenderWidgetHostView implementation.
60 void InitAsChild(gfx::NativeView parent_view
) override
{}
61 RenderWidgetHost
* GetRenderWidgetHost() const override
;
62 void SetSize(const gfx::Size
& size
) override
{}
63 void SetBounds(const gfx::Rect
& rect
) override
{}
64 gfx::Vector2dF
GetLastScrollOffset() const override
;
65 gfx::NativeView
GetNativeView() const override
;
66 gfx::NativeViewId
GetNativeViewId() const override
;
67 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
68 ui::TextInputClient
* GetTextInputClient() override
;
69 bool HasFocus() const override
;
70 bool IsSurfaceAvailableForCopy() const override
;
73 bool IsShowing() override
;
74 void WasUnOccluded() override
;
75 void WasOccluded() override
;
76 gfx::Rect
GetViewBounds() const override
;
77 #if defined(OS_MACOSX)
78 void SetActive(bool active
) override
;
79 void SetWindowVisibility(bool visible
) override
{}
80 void WindowFrameChanged() override
{}
81 void ShowDefinitionForSelection() override
{}
82 bool SupportsSpeech() const override
;
83 void SpeakSelection() override
;
84 bool IsSpeaking() const override
;
85 void StopSpeaking() override
;
86 #endif // defined(OS_MACOSX)
87 void OnSwapCompositorFrame(uint32 output_surface_id
,
88 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
90 // RenderWidgetHostViewBase implementation.
91 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
92 const gfx::Rect
& bounds
) override
{}
93 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
{}
94 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
{
96 void Focus() override
{}
97 void Blur() override
{}
98 void SetIsLoading(bool is_loading
) override
{}
99 void UpdateCursor(const WebCursor
& cursor
) override
{}
100 void TextInputTypeChanged(ui::TextInputType type
,
101 ui::TextInputMode input_mode
,
102 bool can_compose_inline
,
103 int flags
) override
{}
104 void ImeCancelComposition() override
{}
105 void ImeCompositionRangeChanged(
106 const gfx::Range
& range
,
107 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 ReadbackRequestCallback
& 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 void LockCompositingSurface() override
{}
130 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 void SetParentNativeViewAccessible(
139 gfx::NativeViewAccessible accessible_parent
) override
;
140 gfx::NativeViewId
GetParentForWindowlessPlugin() const override
;
143 bool is_showing() const { return is_showing_
; }
144 bool is_occluded() const { return is_occluded_
; }
145 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
148 RenderWidgetHostImpl
* rwh_
;
153 bool did_swap_compositor_frame_
;
154 ui::DummyTextInputClient text_input_client_
;
157 #if defined(COMPILER_MSVC)
158 // See comment for same warning on RenderViewHostImpl.
159 #pragma warning(push)
160 #pragma warning(disable: 4250)
163 // TestRenderViewHost ----------------------------------------------------------
165 // TODO(brettw) this should use a TestWebContents which should be generalized
166 // from the WebContentsImpl test. We will probably also need that class' version
167 // of CreateRenderViewForRenderManager when more complicated tests start using
170 // Note that users outside of content must use this class by getting
171 // the separate RenderViewHostTester interface via
172 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
175 // Users within content may directly static_cast from a
176 // RenderViewHost* to a TestRenderViewHost*.
178 // The reasons we do it this way rather than extending the parallel
179 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
180 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
182 // a) Extending the parallel class hierarchy further would require
183 // more classes to use virtual inheritance. This is a complexity that
184 // is better to avoid, especially when it would be introduced in the
185 // production code solely to facilitate testing code.
187 // b) While users outside of content only need to drive tests on a
188 // RenderViewHost, content needs a test version of the full
189 // RenderViewHostImpl so that it can test all methods on that concrete
190 // class (e.g. overriding a method such as
191 // RenderViewHostImpl::CreateRenderView). This would have complicated
192 // the dual class hierarchy even further.
194 // The reason we do it this way instead of using composition is
195 // similar to (b) above, essentially it gets very tricky. By using
196 // the split interface we avoid complexity within content and maintain
197 // reasonable utility for embedders.
198 class TestRenderViewHost
199 : public RenderViewHostImpl
,
200 public RenderViewHostTester
{
202 TestRenderViewHost(SiteInstance
* instance
,
203 RenderViewHostDelegate
* delegate
,
204 RenderWidgetHostDelegate
* widget_delegate
,
206 int main_frame_routing_id
,
208 ~TestRenderViewHost() override
;
210 // RenderViewHostTester implementation. Note that CreateRenderView
211 // is not specified since it is synonymous with the one from
212 // RenderViewHostImpl, see below.
213 void SimulateWasHidden() override
;
214 void SimulateWasShown() override
;
215 WebPreferences
TestComputeWebkitPrefs() override
;
217 void TestOnUpdateStateWithFile(
218 int page_id
, const base::FilePath
& file_path
);
220 void TestOnStartDragging(const DropData
& drop_data
);
222 // If set, *delete_counter is incremented when this object destructs.
223 void set_delete_counter(int* delete_counter
) {
224 delete_counter_
= delete_counter
;
227 // The opener route id passed to CreateRenderView().
228 int opener_route_id() const { return opener_route_id_
; }
230 // RenderWidgetHost overrides (same value, but in the Mock* type)
231 MockRenderProcessHost
* GetProcess() const override
;
233 // RenderViewHost overrides --------------------------------------------------
235 bool CreateRenderView(const base::string16
& frame_name
,
239 bool window_was_created_with_opener
) override
;
240 bool IsFullscreenGranted() const override
;
243 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
245 void SendNavigateWithTransitionAndResponseCode(int page_id
,
247 ui::PageTransition transition
,
250 // Calls OnNavigate on the RenderViewHost with the given information.
251 // Sets the rest of the parameters in the message to the "typical" values.
252 // This is a helper function for simulating the most common types of loads.
253 void SendNavigateWithParameters(
256 ui::PageTransition transition
,
257 const GURL
& original_request_url
,
259 const base::FilePath
* file_path_for_history_item
);
261 // See set_delete_counter() above. May be NULL.
262 int* delete_counter_
;
264 // See opener_route_id() above.
265 int opener_route_id_
;
267 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
270 #if defined(COMPILER_MSVC)
274 // Adds methods to get straight at the impl classes.
275 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
277 RenderViewHostImplTestHarness();
278 ~RenderViewHostImplTestHarness() override
;
280 // contents() is equivalent to static_cast<TestWebContents*>(web_contents())
281 TestWebContents
* contents();
283 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
285 // test_rvh() is equivalent to any of the following:
286 // contents()->GetMainFrame()->GetRenderViewHost()
287 // contents()->GetRenderViewHost()
288 // static_cast<TestRenderViewHost*>(rvh())
290 // Since most functionality will eventually shift from RVH to RFH, you may
291 // prefer to use the GetMainFrame() method in tests.
292 TestRenderViewHost
* test_rvh();
294 // pending_test_rvh() is equivalent to all of the following:
295 // contents()->GetPendingMainFrame()->GetRenderViewHost() [if frame exists]
296 // contents()->GetPendingRenderViewHost()
297 // static_cast<TestRenderViewHost*>(pending_rvh())
299 // Since most functionality will eventually shift from RVH to RFH, you may
300 // prefer to use the GetPendingMainFrame() method in tests.
301 TestRenderViewHost
* pending_test_rvh();
303 // active_test_rvh() is equivalent to:
304 // contents()->GetPendingRenderViewHost() ?
305 // contents()->GetPendingRenderViewHost() :
306 // contents()->GetRenderViewHost();
307 TestRenderViewHost
* active_test_rvh();
309 // main_test_rfh() is equivalent to contents()->GetMainFrame()
310 // TODO(nick): Replace all uses with contents()->GetMainFrame()
311 TestRenderFrameHost
* main_test_rfh();
314 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
315 ScopedSetSupportedScaleFactors
;
316 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
317 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
320 } // namespace content
322 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_