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
;
94 // RenderWidgetHostViewBase implementation.
95 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
96 const gfx::Rect
& bounds
) override
{}
97 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
{}
98 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
{
100 void Focus() override
{}
101 void SetIsLoading(bool is_loading
) override
{}
102 void UpdateCursor(const WebCursor
& cursor
) override
{}
103 void TextInputStateChanged(
104 const ViewHostMsg_TextInputState_Params
& params
) 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 frame route id passed to CreateRenderView().
230 int opener_frame_route_id() const { return opener_frame_route_id_
; }
232 // RenderWidgetHost overrides (same value, but in the Mock* type)
233 MockRenderProcessHost
* GetProcess() const override
;
235 bool CreateTestRenderView(const base::string16
& frame_name
,
236 int opener_frame_route_id
,
239 bool window_was_created_with_opener
) override
;
241 // RenderViewHost overrides --------------------------------------------------
243 bool CreateRenderView(int opener_frame_route_id
,
246 const FrameReplicationState
& replicated_frame_state
,
247 bool window_was_created_with_opener
) override
;
248 bool IsFullscreenGranted() const override
;
251 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
253 void SendNavigateWithTransitionAndResponseCode(int page_id
,
255 ui::PageTransition transition
,
258 // Calls OnNavigate on the RenderViewHost with the given information.
259 // Sets the rest of the parameters in the message to the "typical" values.
260 // This is a helper function for simulating the most common types of loads.
261 void SendNavigateWithParameters(
264 ui::PageTransition transition
,
265 const GURL
& original_request_url
,
267 const base::FilePath
* file_path_for_history_item
);
269 // See set_delete_counter() above. May be NULL.
270 int* delete_counter_
;
272 // See opener_frame_route_id() above.
273 int opener_frame_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_