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
;
32 struct ViewHostMsg_TextInputState_Params
;
41 class TestRenderFrameHost
;
42 class TestWebContents
;
43 struct FrameReplicationState
;
45 // Utility function to initialize FrameHostMsg_DidCommitProvisionalLoad_Params
46 // with given parameters.
47 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params
* params
,
50 bool did_create_new_entry
,
52 ui::PageTransition transition_type
);
54 // TestRenderViewHostView ------------------------------------------------------
56 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
57 // without having side-effects.
58 class TestRenderWidgetHostView
: public RenderWidgetHostViewBase
{
60 explicit TestRenderWidgetHostView(RenderWidgetHost
* rwh
);
61 ~TestRenderWidgetHostView() override
;
63 // RenderWidgetHostView implementation.
64 void InitAsChild(gfx::NativeView parent_view
) override
{}
65 RenderWidgetHost
* GetRenderWidgetHost() const override
;
66 void SetSize(const gfx::Size
& size
) override
{}
67 void SetBounds(const gfx::Rect
& rect
) override
{}
68 gfx::Vector2dF
GetLastScrollOffset() const override
;
69 gfx::NativeView
GetNativeView() const override
;
70 gfx::NativeViewId
GetNativeViewId() const override
;
71 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
72 ui::TextInputClient
* GetTextInputClient() override
;
73 bool HasFocus() const override
;
74 bool IsSurfaceAvailableForCopy() const override
;
77 bool IsShowing() override
;
78 void WasUnOccluded() override
;
79 void WasOccluded() override
;
80 gfx::Rect
GetViewBounds() const override
;
81 #if defined(OS_MACOSX)
82 void SetActive(bool active
) override
;
83 void SetWindowVisibility(bool visible
) override
{}
84 void WindowFrameChanged() override
{}
85 void ShowDefinitionForSelection() override
{}
86 bool SupportsSpeech() const override
;
87 void SpeakSelection() override
;
88 bool IsSpeaking() const override
;
89 void StopSpeaking() override
;
90 #endif // defined(OS_MACOSX)
91 void OnSwapCompositorFrame(uint32 output_surface_id
,
92 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
93 void ClearCompositorFrame() override
{}
95 // RenderWidgetHostViewBase implementation.
96 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
97 const gfx::Rect
& bounds
) override
{}
98 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
{}
99 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
{
101 void Focus() override
{}
102 void SetIsLoading(bool is_loading
) override
{}
103 void UpdateCursor(const WebCursor
& cursor
) override
{}
104 void TextInputStateChanged(
105 const ViewHostMsg_TextInputState_Params
& params
) override
{}
106 void ImeCancelComposition() override
{}
107 void ImeCompositionRangeChanged(
108 const gfx::Range
& range
,
109 const std::vector
<gfx::Rect
>& character_bounds
) override
{}
110 void RenderProcessGone(base::TerminationStatus status
,
111 int error_code
) override
;
112 void Destroy() override
;
113 void SetTooltipText(const base::string16
& tooltip_text
) override
{}
114 void SelectionBoundsChanged(
115 const ViewHostMsg_SelectionBounds_Params
& params
) override
{}
116 void CopyFromCompositingSurface(
117 const gfx::Rect
& src_subrect
,
118 const gfx::Size
& dst_size
,
119 ReadbackRequestCallback
& callback
,
120 const SkColorType preferred_color_type
) override
;
121 void CopyFromCompositingSurfaceToVideoFrame(
122 const gfx::Rect
& src_subrect
,
123 const scoped_refptr
<media::VideoFrame
>& target
,
124 const base::Callback
<void(bool)>& callback
) override
;
125 bool CanCopyToVideoFrame() const override
;
126 bool HasAcceleratedSurface(const gfx::Size
& desired_size
) override
;
127 #if defined(OS_MACOSX)
128 bool PostProcessEventForPluginIme(
129 const NativeWebKeyboardEvent
& event
) override
;
131 #if defined(OS_ANDROID)
132 void LockCompositingSurface() override
{}
133 void UnlockCompositingSurface() override
{}
135 void GetScreenInfo(blink::WebScreenInfo
* results
) override
{}
136 bool GetScreenColorProfile(std::vector
<char>* color_profile
) override
;
137 gfx::Rect
GetBoundsInRootWindow() override
;
138 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
139 bool LockMouse() override
;
140 void UnlockMouse() override
;
142 void SetParentNativeViewAccessible(
143 gfx::NativeViewAccessible accessible_parent
) override
;
144 gfx::NativeViewId
GetParentForWindowlessPlugin() const override
;
147 bool is_showing() const { return is_showing_
; }
148 bool is_occluded() const { return is_occluded_
; }
149 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
152 RenderWidgetHostImpl
* rwh_
;
157 bool did_swap_compositor_frame_
;
158 ui::DummyTextInputClient text_input_client_
;
161 #if defined(COMPILER_MSVC)
162 // See comment for same warning on RenderViewHostImpl.
163 #pragma warning(push)
164 #pragma warning(disable: 4250)
167 // TestRenderViewHost ----------------------------------------------------------
169 // TODO(brettw) this should use a TestWebContents which should be generalized
170 // from the WebContentsImpl test. We will probably also need that class' version
171 // of CreateRenderViewForRenderManager when more complicated tests start using
174 // Note that users outside of content must use this class by getting
175 // the separate RenderViewHostTester interface via
176 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
179 // Users within content may directly static_cast from a
180 // RenderViewHost* to a TestRenderViewHost*.
182 // The reasons we do it this way rather than extending the parallel
183 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
184 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
186 // a) Extending the parallel class hierarchy further would require
187 // more classes to use virtual inheritance. This is a complexity that
188 // is better to avoid, especially when it would be introduced in the
189 // production code solely to facilitate testing code.
191 // b) While users outside of content only need to drive tests on a
192 // RenderViewHost, content needs a test version of the full
193 // RenderViewHostImpl so that it can test all methods on that concrete
194 // class (e.g. overriding a method such as
195 // RenderViewHostImpl::CreateRenderView). This would have complicated
196 // the dual class hierarchy even further.
198 // The reason we do it this way instead of using composition is
199 // similar to (b) above, essentially it gets very tricky. By using
200 // the split interface we avoid complexity within content and maintain
201 // reasonable utility for embedders.
202 class TestRenderViewHost
203 : public RenderViewHostImpl
,
204 public RenderViewHostTester
{
206 TestRenderViewHost(SiteInstance
* instance
,
207 RenderViewHostDelegate
* delegate
,
208 RenderWidgetHostDelegate
* widget_delegate
,
211 int32 main_frame_routing_id
,
213 ~TestRenderViewHost() override
;
215 // RenderViewHostTester implementation. Note that CreateRenderView
216 // is not specified since it is synonymous with the one from
217 // RenderViewHostImpl, see below.
218 void SimulateWasHidden() override
;
219 void SimulateWasShown() override
;
220 WebPreferences
TestComputeWebkitPrefs() override
;
222 void TestOnUpdateStateWithFile(
223 int page_id
, const base::FilePath
& file_path
);
225 void TestOnStartDragging(const DropData
& drop_data
);
227 // If set, *delete_counter is incremented when this object destructs.
228 void set_delete_counter(int* delete_counter
) {
229 delete_counter_
= delete_counter
;
232 // The opener frame route id passed to CreateRenderView().
233 int opener_frame_route_id() const { return opener_frame_route_id_
; }
235 // RenderWidgetHost overrides (same value, but in the Mock* type)
236 MockRenderProcessHost
* GetProcess() const override
;
238 bool CreateTestRenderView(const base::string16
& frame_name
,
239 int opener_frame_route_id
,
242 bool window_was_created_with_opener
) override
;
244 // RenderViewHost overrides --------------------------------------------------
246 bool CreateRenderView(int opener_frame_route_id
,
249 const FrameReplicationState
& replicated_frame_state
,
250 bool window_was_created_with_opener
) override
;
251 bool IsFullscreenGranted() const override
;
254 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
256 void SendNavigateWithTransitionAndResponseCode(int page_id
,
258 ui::PageTransition transition
,
261 // Calls OnNavigate on the RenderViewHost with the given information.
262 // Sets the rest of the parameters in the message to the "typical" values.
263 // This is a helper function for simulating the most common types of loads.
264 void SendNavigateWithParameters(
267 ui::PageTransition transition
,
268 const GURL
& original_request_url
,
270 const base::FilePath
* file_path_for_history_item
);
272 // See set_delete_counter() above. May be NULL.
273 int* delete_counter_
;
275 // See opener_frame_route_id() above.
276 int opener_frame_route_id_
;
278 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
281 #if defined(COMPILER_MSVC)
285 // Adds methods to get straight at the impl classes.
286 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
288 RenderViewHostImplTestHarness();
289 ~RenderViewHostImplTestHarness() override
;
291 // contents() is equivalent to static_cast<TestWebContents*>(web_contents())
292 TestWebContents
* contents();
294 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
296 // test_rvh() is equivalent to any of the following:
297 // contents()->GetMainFrame()->GetRenderViewHost()
298 // contents()->GetRenderViewHost()
299 // static_cast<TestRenderViewHost*>(rvh())
301 // Since most functionality will eventually shift from RVH to RFH, you may
302 // prefer to use the GetMainFrame() method in tests.
303 TestRenderViewHost
* test_rvh();
305 // pending_test_rvh() is equivalent to all of the following:
306 // contents()->GetPendingMainFrame()->GetRenderViewHost() [if frame exists]
307 // contents()->GetPendingRenderViewHost()
308 // static_cast<TestRenderViewHost*>(pending_rvh())
310 // Since most functionality will eventually shift from RVH to RFH, you may
311 // prefer to use the GetPendingMainFrame() method in tests.
312 TestRenderViewHost
* pending_test_rvh();
314 // active_test_rvh() is equivalent to:
315 // contents()->GetPendingRenderViewHost() ?
316 // contents()->GetPendingRenderViewHost() :
317 // contents()->GetRenderViewHost();
318 TestRenderViewHost
* active_test_rvh();
320 // main_test_rfh() is equivalent to contents()->GetMainFrame()
321 // TODO(nick): Replace all uses with contents()->GetMainFrame()
322 TestRenderFrameHost
* main_test_rfh();
325 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
326 ScopedSetSupportedScaleFactors
;
327 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
328 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
331 } // namespace content
333 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_