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
;
42 struct FrameReplicationState
;
44 // Utility function to initialize FrameHostMsg_DidCommitProvisionalLoad_Params
45 // with given parameters.
46 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params
* params
,
49 bool did_create_new_entry
,
51 ui::PageTransition transition_type
);
53 // TestRenderViewHostView ------------------------------------------------------
55 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
56 // without having side-effects.
57 class TestRenderWidgetHostView
: public RenderWidgetHostViewBase
{
59 explicit TestRenderWidgetHostView(RenderWidgetHost
* rwh
);
60 ~TestRenderWidgetHostView() override
;
62 // RenderWidgetHostView implementation.
63 void InitAsChild(gfx::NativeView parent_view
) override
{}
64 RenderWidgetHost
* GetRenderWidgetHost() const override
;
65 void SetSize(const gfx::Size
& size
) override
{}
66 void SetBounds(const gfx::Rect
& rect
) override
{}
67 gfx::Vector2dF
GetLastScrollOffset() const override
;
68 gfx::NativeView
GetNativeView() const override
;
69 gfx::NativeViewId
GetNativeViewId() const override
;
70 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
71 ui::TextInputClient
* GetTextInputClient() override
;
72 bool HasFocus() const override
;
73 bool IsSurfaceAvailableForCopy() const override
;
76 bool IsShowing() override
;
77 void WasUnOccluded() override
;
78 void WasOccluded() override
;
79 gfx::Rect
GetViewBounds() const override
;
80 #if defined(OS_MACOSX)
81 void SetActive(bool active
) override
;
82 void SetWindowVisibility(bool visible
) override
{}
83 void WindowFrameChanged() override
{}
84 void ShowDefinitionForSelection() override
{}
85 bool SupportsSpeech() const override
;
86 void SpeakSelection() override
;
87 bool IsSpeaking() const override
;
88 void StopSpeaking() override
;
89 #endif // defined(OS_MACOSX)
90 void OnSwapCompositorFrame(uint32 output_surface_id
,
91 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
93 // RenderWidgetHostViewBase implementation.
94 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
95 const gfx::Rect
& bounds
) override
{}
96 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
{}
97 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
{
99 void Focus() override
{}
100 void SetIsLoading(bool is_loading
) override
{}
101 void UpdateCursor(const WebCursor
& cursor
) override
{}
102 void TextInputTypeChanged(ui::TextInputType type
,
103 ui::TextInputMode input_mode
,
104 bool can_compose_inline
,
105 int flags
) 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 gfx::Rect
GetBoundsInRootWindow() override
;
137 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
138 bool LockMouse() override
;
139 void UnlockMouse() override
;
141 void SetParentNativeViewAccessible(
142 gfx::NativeViewAccessible accessible_parent
) override
;
143 gfx::NativeViewId
GetParentForWindowlessPlugin() const override
;
146 bool is_showing() const { return is_showing_
; }
147 bool is_occluded() const { return is_occluded_
; }
148 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_
; }
151 RenderWidgetHostImpl
* rwh_
;
156 bool did_swap_compositor_frame_
;
157 ui::DummyTextInputClient text_input_client_
;
160 #if defined(COMPILER_MSVC)
161 // See comment for same warning on RenderViewHostImpl.
162 #pragma warning(push)
163 #pragma warning(disable: 4250)
166 // TestRenderViewHost ----------------------------------------------------------
168 // TODO(brettw) this should use a TestWebContents which should be generalized
169 // from the WebContentsImpl test. We will probably also need that class' version
170 // of CreateRenderViewForRenderManager when more complicated tests start using
173 // Note that users outside of content must use this class by getting
174 // the separate RenderViewHostTester interface via
175 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
178 // Users within content may directly static_cast from a
179 // RenderViewHost* to a TestRenderViewHost*.
181 // The reasons we do it this way rather than extending the parallel
182 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
183 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
185 // a) Extending the parallel class hierarchy further would require
186 // more classes to use virtual inheritance. This is a complexity that
187 // is better to avoid, especially when it would be introduced in the
188 // production code solely to facilitate testing code.
190 // b) While users outside of content only need to drive tests on a
191 // RenderViewHost, content needs a test version of the full
192 // RenderViewHostImpl so that it can test all methods on that concrete
193 // class (e.g. overriding a method such as
194 // RenderViewHostImpl::CreateRenderView). This would have complicated
195 // the dual class hierarchy even further.
197 // The reason we do it this way instead of using composition is
198 // similar to (b) above, essentially it gets very tricky. By using
199 // the split interface we avoid complexity within content and maintain
200 // reasonable utility for embedders.
201 class TestRenderViewHost
202 : public RenderViewHostImpl
,
203 public RenderViewHostTester
{
205 TestRenderViewHost(SiteInstance
* instance
,
206 RenderViewHostDelegate
* delegate
,
207 RenderWidgetHostDelegate
* widget_delegate
,
209 int main_frame_routing_id
,
211 ~TestRenderViewHost() override
;
213 // RenderViewHostTester implementation. Note that CreateRenderView
214 // is not specified since it is synonymous with the one from
215 // RenderViewHostImpl, see below.
216 void SimulateWasHidden() override
;
217 void SimulateWasShown() override
;
218 WebPreferences
TestComputeWebkitPrefs() override
;
220 void TestOnUpdateStateWithFile(
221 int page_id
, const base::FilePath
& file_path
);
223 void TestOnStartDragging(const DropData
& drop_data
);
225 // If set, *delete_counter is incremented when this object destructs.
226 void set_delete_counter(int* delete_counter
) {
227 delete_counter_
= delete_counter
;
230 // The opener route id passed to CreateRenderView().
231 int opener_route_id() const { return opener_route_id_
; }
233 // RenderWidgetHost overrides (same value, but in the Mock* type)
234 MockRenderProcessHost
* GetProcess() const override
;
236 bool CreateTestRenderView(const base::string16
& frame_name
,
240 bool window_was_created_with_opener
) override
;
242 // RenderViewHost overrides --------------------------------------------------
244 bool CreateRenderView(const base::string16
& frame_name
,
248 const FrameReplicationState
& replicated_frame_state
,
249 bool window_was_created_with_opener
) override
;
250 bool IsFullscreenGranted() const override
;
253 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, FilterNavigate
);
255 void SendNavigateWithTransitionAndResponseCode(int page_id
,
257 ui::PageTransition transition
,
260 // Calls OnNavigate on the RenderViewHost with the given information.
261 // Sets the rest of the parameters in the message to the "typical" values.
262 // This is a helper function for simulating the most common types of loads.
263 void SendNavigateWithParameters(
266 ui::PageTransition transition
,
267 const GURL
& original_request_url
,
269 const base::FilePath
* file_path_for_history_item
);
271 // See set_delete_counter() above. May be NULL.
272 int* delete_counter_
;
274 // See opener_route_id() above.
275 int opener_route_id_
;
277 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost
);
280 #if defined(COMPILER_MSVC)
284 // Adds methods to get straight at the impl classes.
285 class RenderViewHostImplTestHarness
: public RenderViewHostTestHarness
{
287 RenderViewHostImplTestHarness();
288 ~RenderViewHostImplTestHarness() override
;
290 // contents() is equivalent to static_cast<TestWebContents*>(web_contents())
291 TestWebContents
* contents();
293 // RVH/RFH getters are shorthand for oft-used bits of web_contents().
295 // test_rvh() is equivalent to any of the following:
296 // contents()->GetMainFrame()->GetRenderViewHost()
297 // contents()->GetRenderViewHost()
298 // static_cast<TestRenderViewHost*>(rvh())
300 // Since most functionality will eventually shift from RVH to RFH, you may
301 // prefer to use the GetMainFrame() method in tests.
302 TestRenderViewHost
* test_rvh();
304 // pending_test_rvh() is equivalent to all of the following:
305 // contents()->GetPendingMainFrame()->GetRenderViewHost() [if frame exists]
306 // contents()->GetPendingRenderViewHost()
307 // static_cast<TestRenderViewHost*>(pending_rvh())
309 // Since most functionality will eventually shift from RVH to RFH, you may
310 // prefer to use the GetPendingMainFrame() method in tests.
311 TestRenderViewHost
* pending_test_rvh();
313 // active_test_rvh() is equivalent to:
314 // contents()->GetPendingRenderViewHost() ?
315 // contents()->GetPendingRenderViewHost() :
316 // contents()->GetRenderViewHost();
317 TestRenderViewHost
* active_test_rvh();
319 // main_test_rfh() is equivalent to contents()->GetMainFrame()
320 // TODO(nick): Replace all uses with contents()->GetMainFrame()
321 TestRenderFrameHost
* main_test_rfh();
324 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
325 ScopedSetSupportedScaleFactors
;
326 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
327 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness
);
330 } // namespace content
332 #endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_