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 bool GetScreenColorProfile(std::vector
<char>* color_profile
) 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
,
210 int32 main_frame_routing_id
,
212 ~TestRenderViewHost() override
;
214 // RenderViewHostTester implementation. Note that CreateRenderView
215 // is not specified since it is synonymous with the one from
216 // RenderViewHostImpl, see below.
217 void SimulateWasHidden() override
;
218 void SimulateWasShown() override
;
219 WebPreferences
TestComputeWebkitPrefs() override
;
221 void TestOnUpdateStateWithFile(
222 int page_id
, const base::FilePath
& file_path
);
224 void TestOnStartDragging(const DropData
& drop_data
);
226 // If set, *delete_counter is incremented when this object destructs.
227 void set_delete_counter(int* delete_counter
) {
228 delete_counter_
= delete_counter
;
231 // The opener frame route id passed to CreateRenderView().
232 int opener_frame_route_id() const { return opener_frame_route_id_
; }
234 // RenderWidgetHost overrides (same value, but in the Mock* type)
235 MockRenderProcessHost
* GetProcess() const override
;
237 bool CreateTestRenderView(const base::string16
& frame_name
,
238 int opener_frame_route_id
,
241 bool window_was_created_with_opener
) override
;
243 // RenderViewHost overrides --------------------------------------------------
245 bool CreateRenderView(int opener_frame_route_id
,
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_frame_route_id() above.
275 int opener_frame_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_