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 FrameHostMsg_DidCommitProvisionalLoad_Params
44 // with given parameters.
45 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params
* params
,
48 bool did_create_new_entry
,
50 ui::PageTransition transition_type
);
52 // TestRenderViewHostView ------------------------------------------------------
54 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
55 // without having side-effects.
56 class TestRenderWidgetHostView
: public RenderWidgetHostViewBase
{
58 explicit TestRenderWidgetHostView(RenderWidgetHost
* rwh
);
59 ~TestRenderWidgetHostView() override
;
61 // RenderWidgetHostView implementation.
62 void InitAsChild(gfx::NativeView parent_view
) override
{}
63 RenderWidgetHost
* GetRenderWidgetHost() const override
;
64 void SetSize(const gfx::Size
& size
) override
{}
65 void SetBounds(const gfx::Rect
& rect
) override
{}
66 gfx::Vector2dF
GetLastScrollOffset() const override
;
67 gfx::NativeView
GetNativeView() const override
;
68 gfx::NativeViewId
GetNativeViewId() const override
;
69 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
70 ui::TextInputClient
* GetTextInputClient() override
;
71 bool HasFocus() const override
;
72 bool IsSurfaceAvailableForCopy() const override
;
75 bool IsShowing() override
;
76 void WasUnOccluded() override
;
77 void WasOccluded() override
;
78 gfx::Rect
GetViewBounds() const override
;
79 #if defined(OS_MACOSX)
80 void SetActive(bool active
) override
;
81 void SetWindowVisibility(bool visible
) override
{}
82 void WindowFrameChanged() override
{}
83 void ShowDefinitionForSelection() override
{}
84 bool SupportsSpeech() const override
;
85 void SpeakSelection() override
;
86 bool IsSpeaking() const override
;
87 void StopSpeaking() override
;
88 #endif // defined(OS_MACOSX)
89 void OnSwapCompositorFrame(uint32 output_surface_id
,
90 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
92 // RenderWidgetHostViewBase implementation.
93 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
94 const gfx::Rect
& bounds
) override
{}
95 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
{}
96 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
{
98 void Focus() override
{}
99 void SetIsLoading(bool is_loading
) override
{}
100 void UpdateCursor(const WebCursor
& cursor
) override
{}
101 void TextInputTypeChanged(ui::TextInputType type
,
102 ui::TextInputMode input_mode
,
103 bool can_compose_inline
,
104 int flags
) override
{}
105 void ImeCancelComposition() override
{}
106 void ImeCompositionRangeChanged(
107 const gfx::Range
& range
,
108 const std::vector
<gfx::Rect
>& character_bounds
) override
{}
109 void RenderProcessGone(base::TerminationStatus status
,
110 int error_code
) override
;
111 void Destroy() override
;
112 void SetTooltipText(const base::string16
& tooltip_text
) override
{}
113 void SelectionBoundsChanged(
114 const ViewHostMsg_SelectionBounds_Params
& params
) override
{}
115 void CopyFromCompositingSurface(
116 const gfx::Rect
& src_subrect
,
117 const gfx::Size
& dst_size
,
118 ReadbackRequestCallback
& callback
,
119 const SkColorType preferred_color_type
) override
;
120 void CopyFromCompositingSurfaceToVideoFrame(
121 const gfx::Rect
& src_subrect
,
122 const scoped_refptr
<media::VideoFrame
>& target
,
123 const base::Callback
<void(bool)>& callback
) override
;
124 bool CanCopyToVideoFrame() const override
;
125 bool HasAcceleratedSurface(const gfx::Size
& desired_size
) override
;
126 #if defined(OS_MACOSX)
127 bool PostProcessEventForPluginIme(
128 const NativeWebKeyboardEvent
& event
) override
;
130 #if defined(OS_ANDROID)
131 void LockCompositingSurface() override
{}
132 void UnlockCompositingSurface() override
{}
134 void GetScreenInfo(blink::WebScreenInfo
* results
) override
{}
135 gfx::Rect
GetBoundsInRootWindow() override
;
136 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
137 bool LockMouse() override
;
138 void UnlockMouse() override
;
140 void SetParentNativeViewAccessible(
141 gfx::NativeViewAccessible accessible_parent
) override
;
142 gfx::NativeViewId
GetParentForWindowlessPlugin() const override
;
145 bool is_showing() const { return is_showing_
; }
146 bool is_occluded() const { return is_occluded_
; }
147 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
150 RenderWidgetHostImpl
* rwh_
;
155 bool did_swap_compositor_frame_
;
156 ui::DummyTextInputClient text_input_client_
;
159 #if defined(COMPILER_MSVC)
160 // See comment for same warning on RenderViewHostImpl.
161 #pragma warning(push)
162 #pragma warning(disable: 4250)
165 // TestRenderViewHost ----------------------------------------------------------
167 // TODO(brettw) this should use a TestWebContents which should be generalized
168 // from the WebContentsImpl test. We will probably also need that class' version
169 // of CreateRenderViewForRenderManager when more complicated tests start using
172 // Note that users outside of content must use this class by getting
173 // the separate RenderViewHostTester interface via
174 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
177 // Users within content may directly static_cast from a
178 // RenderViewHost* to a TestRenderViewHost*.
180 // The reasons we do it this way rather than extending the parallel
181 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
182 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
184 // a) Extending the parallel class hierarchy further would require
185 // more classes to use virtual inheritance. This is a complexity that
186 // is better to avoid, especially when it would be introduced in the
187 // production code solely to facilitate testing code.
189 // b) While users outside of content only need to drive tests on a
190 // RenderViewHost, content needs a test version of the full
191 // RenderViewHostImpl so that it can test all methods on that concrete
192 // class (e.g. overriding a method such as
193 // RenderViewHostImpl::CreateRenderView). This would have complicated
194 // the dual class hierarchy even further.
196 // The reason we do it this way instead of using composition is
197 // similar to (b) above, essentially it gets very tricky. By using
198 // the split interface we avoid complexity within content and maintain
199 // reasonable utility for embedders.
200 class TestRenderViewHost
201 : public RenderViewHostImpl
,
202 public RenderViewHostTester
{
204 TestRenderViewHost(SiteInstance
* instance
,
205 RenderViewHostDelegate
* delegate
,
206 RenderWidgetHostDelegate
* widget_delegate
,
208 int main_frame_routing_id
,
210 ~TestRenderViewHost() override
;
212 // RenderViewHostTester implementation. Note that CreateRenderView
213 // is not specified since it is synonymous with the one from
214 // RenderViewHostImpl, see below.
215 void SimulateWasHidden() override
;
216 void SimulateWasShown() override
;
217 WebPreferences
TestComputeWebkitPrefs() override
;
219 void TestOnUpdateStateWithFile(
220 int page_id
, const base::FilePath
& file_path
);
222 void TestOnStartDragging(const DropData
& drop_data
);
224 // If set, *delete_counter is incremented when this object destructs.
225 void set_delete_counter(int* delete_counter
) {
226 delete_counter_
= delete_counter
;
229 // The opener route id passed to CreateRenderView().
230 int opener_route_id() const { return opener_route_id_
; }
232 // RenderWidgetHost overrides (same value, but in the Mock* type)
233 MockRenderProcessHost
* GetProcess() const override
;
235 // RenderViewHost overrides --------------------------------------------------
237 bool CreateRenderView(const base::string16
& frame_name
,
241 bool window_was_created_with_opener
) override
;
242 bool IsFullscreenGranted() 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 // See set_delete_counter() above. May be NULL.
264 int* delete_counter_
;
266 // See opener_route_id() above.
267 int opener_route_id_
;
269 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
272 #if defined(COMPILER_MSVC)
276 // Adds methods to get straight at the impl classes.
277 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
279 RenderViewHostImplTestHarness();
280 ~RenderViewHostImplTestHarness() override
;
282 // contents() is equivalent to static_cast<TestWebContents*>(web_contents())
283 TestWebContents
* contents();
285 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
287 // test_rvh() is equivalent to any of the following:
288 // contents()->GetMainFrame()->GetRenderViewHost()
289 // contents()->GetRenderViewHost()
290 // static_cast<TestRenderViewHost*>(rvh())
292 // Since most functionality will eventually shift from RVH to RFH, you may
293 // prefer to use the GetMainFrame() method in tests.
294 TestRenderViewHost
* test_rvh();
296 // pending_test_rvh() is equivalent to all of the following:
297 // contents()->GetPendingMainFrame()->GetRenderViewHost() [if frame exists]
298 // contents()->GetPendingRenderViewHost()
299 // static_cast<TestRenderViewHost*>(pending_rvh())
301 // Since most functionality will eventually shift from RVH to RFH, you may
302 // prefer to use the GetPendingMainFrame() method in tests.
303 TestRenderViewHost
* pending_test_rvh();
305 // active_test_rvh() is equivalent to:
306 // contents()->GetPendingRenderViewHost() ?
307 // contents()->GetPendingRenderViewHost() :
308 // contents()->GetRenderViewHost();
309 TestRenderViewHost
* active_test_rvh();
311 // main_test_rfh() is equivalent to contents()->GetMainFrame()
312 // TODO(nick): Replace all uses with contents()->GetMainFrame()
313 TestRenderFrameHost
* main_test_rfh();
316 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
317 ScopedSetSupportedScaleFactors
;
318 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
319 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
322 } // namespace content
324 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_