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_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_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/page_transition_types.h"
17 #include "content/public/test/test_renderer_host.h"
18 #include "ui/base/layout.h"
19 #include "ui/gfx/vector2d_f.h"
21 // This file provides a testing framework for mocking out the RenderProcessHost
22 // layer. It allows you to test RenderViewHost, WebContentsImpl,
23 // NavigationController, and other layers above that without running an actual
26 // To use, derive your test base class from RenderViewHostImplTestHarness.
28 struct ViewHostMsg_FrameNavigate_Params;
37 class TestWebContents;
39 // Utility function to initialize ViewHostMsg_NavigateParams_Params
40 // with given |page_id|, |url| and |transition_type|.
41 void InitNavigateParams(ViewHostMsg_FrameNavigate_Params* params,
44 PageTransition transition_type);
46 // TestRenderViewHostView ------------------------------------------------------
48 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
49 // without having side-effects.
50 class TestRenderWidgetHostView : public RenderWidgetHostViewBase {
52 explicit TestRenderWidgetHostView(RenderWidgetHost* rwh);
53 virtual ~TestRenderWidgetHostView();
55 // RenderWidgetHostView implementation.
56 virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE {}
57 virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
58 virtual void SetSize(const gfx::Size& size) OVERRIDE {}
59 virtual void SetBounds(const gfx::Rect& rect) OVERRIDE {}
60 virtual gfx::NativeView GetNativeView() const OVERRIDE;
61 virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
62 virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
63 virtual bool HasFocus() const OVERRIDE;
64 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
65 virtual void Show() OVERRIDE;
66 virtual void Hide() OVERRIDE;
67 virtual bool IsShowing() OVERRIDE;
68 virtual gfx::Rect GetViewBounds() const OVERRIDE;
69 #if defined(OS_MACOSX)
70 virtual void SetActive(bool active) OVERRIDE;
71 virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE {}
72 virtual void SetWindowVisibility(bool visible) OVERRIDE {}
73 virtual void WindowFrameChanged() OVERRIDE {}
74 virtual void ShowDefinitionForSelection() OVERRIDE {}
75 virtual bool SupportsSpeech() const OVERRIDE;
76 virtual void SpeakSelection() OVERRIDE;
77 virtual bool IsSpeaking() const OVERRIDE;
78 virtual void StopSpeaking() OVERRIDE;
79 #endif // defined(OS_MACOSX)
80 #if defined(TOOLKIT_GTK)
81 virtual GdkEventButton* GetLastMouseDown() OVERRIDE;
82 virtual gfx::NativeView BuildInputMethodsGtkMenu() OVERRIDE;
83 #endif // defined(TOOLKIT_GTK)
84 virtual void OnSwapCompositorFrame(
85 uint32 output_surface_id,
86 scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
88 // RenderWidgetHostViewPort implementation.
89 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
90 const gfx::Rect& pos) OVERRIDE {}
91 virtual void InitAsFullscreen(
92 RenderWidgetHostView* reference_host_view) OVERRIDE {}
93 virtual void WasShown() OVERRIDE {}
94 virtual void WasHidden() OVERRIDE {}
95 virtual void MovePluginWindows(
96 const gfx::Vector2d& scroll_offset,
97 const std::vector<WebPluginGeometry>& moves) OVERRIDE {}
98 virtual void Focus() OVERRIDE {}
99 virtual void Blur() OVERRIDE {}
100 virtual void SetIsLoading(bool is_loading) OVERRIDE {}
101 virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE {}
102 virtual void TextInputTypeChanged(ui::TextInputType type,
103 ui::TextInputMode input_mode,
104 bool can_compose_inline) OVERRIDE {}
105 virtual void ImeCancelComposition() OVERRIDE {}
106 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
107 virtual void ImeCompositionRangeChanged(
108 const gfx::Range& range,
109 const std::vector<gfx::Rect>& character_bounds) OVERRIDE {}
111 virtual void DidUpdateBackingStore(
112 const gfx::Rect& scroll_rect,
113 const gfx::Vector2d& scroll_delta,
114 const std::vector<gfx::Rect>& rects,
115 const ui::LatencyInfo& latency_info) OVERRIDE {}
116 virtual void RenderProcessGone(base::TerminationStatus status,
117 int error_code) OVERRIDE;
118 virtual void WillDestroyRenderWidget(RenderWidgetHost* rwh) { }
119 virtual void Destroy() OVERRIDE;
120 virtual void SetTooltipText(const string16& tooltip_text) OVERRIDE {}
121 virtual void SelectionBoundsChanged(
122 const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE {}
123 virtual void ScrollOffsetChanged() OVERRIDE {}
124 virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
125 virtual void CopyFromCompositingSurface(
126 const gfx::Rect& src_subrect,
127 const gfx::Size& dst_size,
128 const base::Callback<void(bool, const SkBitmap&)>& callback) OVERRIDE;
129 virtual void CopyFromCompositingSurfaceToVideoFrame(
130 const gfx::Rect& src_subrect,
131 const scoped_refptr<media::VideoFrame>& target,
132 const base::Callback<void(bool)>& callback) OVERRIDE;
133 virtual bool CanCopyToVideoFrame() const OVERRIDE;
134 virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
135 virtual void AcceleratedSurfaceBuffersSwapped(
136 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
137 int gpu_host_id) OVERRIDE;
138 virtual void AcceleratedSurfacePostSubBuffer(
139 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
140 int gpu_host_id) OVERRIDE;
141 virtual void AcceleratedSurfaceSuspend() OVERRIDE;
142 virtual void AcceleratedSurfaceRelease() OVERRIDE {}
143 virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
144 #if defined(OS_MACOSX)
145 virtual void AboutToWaitForBackingStoreMsg() OVERRIDE;
146 virtual bool PostProcessEventForPluginIme(
147 const NativeWebKeyboardEvent& event) OVERRIDE;
148 #elif defined(OS_ANDROID)
149 virtual void ShowDisambiguationPopup(
150 const gfx::Rect& target_rect,
151 const SkBitmap& zoomed_bitmap) OVERRIDE {}
152 virtual void HasTouchEventHandlers(bool need_touch_events) OVERRIDE {}
153 #elif defined(OS_WIN) && !defined(USE_AURA)
154 virtual void WillWmDestroy() OVERRIDE;
156 virtual void GetScreenInfo(WebKit::WebScreenInfo* results) OVERRIDE {}
157 virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
158 virtual void SetHasHorizontalScrollbar(
159 bool has_horizontal_scrollbar) OVERRIDE { }
160 virtual void SetScrollOffsetPinning(
161 bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE { }
162 virtual void OnAccessibilityEvents(
163 const std::vector<AccessibilityHostMsg_EventParams>& params) OVERRIDE {}
164 virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
165 #if defined(OS_WIN) && !defined(USE_AURA)
166 virtual void SetClickthroughRegion(SkRegion* region) OVERRIDE;
168 virtual bool LockMouse() OVERRIDE;
169 virtual void UnlockMouse() OVERRIDE;
170 #if defined(OS_WIN) && defined(USE_AURA)
171 virtual void SetParentNativeViewAccessible(
172 gfx::NativeViewAccessible accessible_parent) OVERRIDE;
175 bool is_showing() const { return is_showing_; }
176 bool did_swap_compositor_frame() const { return did_swap_compositor_frame_; }
179 RenderWidgetHostImpl* rwh_;
183 bool did_swap_compositor_frame_;
186 #if defined(COMPILER_MSVC)
187 // See comment for same warning on RenderViewHostImpl.
188 #pragma warning(push)
189 #pragma warning(disable: 4250)
192 // TestRenderViewHost ----------------------------------------------------------
194 // TODO(brettw) this should use a TestWebContents which should be generalized
195 // from the WebContentsImpl test. We will probably also need that class' version
196 // of CreateRenderViewForRenderManager when more complicated tests start using
199 // Note that users outside of content must use this class by getting
200 // the separate RenderViewHostTester interface via
201 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
204 // Users within content may directly static_cast from a
205 // RenderViewHost* to a TestRenderViewHost*.
207 // The reasons we do it this way rather than extending the parallel
208 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
209 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
211 // a) Extending the parallel class hierarchy further would require
212 // more classes to use virtual inheritance. This is a complexity that
213 // is better to avoid, especially when it would be introduced in the
214 // production code solely to facilitate testing code.
216 // b) While users outside of content only need to drive tests on a
217 // RenderViewHost, content needs a test version of the full
218 // RenderViewHostImpl so that it can test all methods on that concrete
219 // class (e.g. overriding a method such as
220 // RenderViewHostImpl::CreateRenderView). This would have complicated
221 // the dual class hierarchy even further.
223 // The reason we do it this way instead of using composition is
224 // similar to (b) above, essentially it gets very tricky. By using
225 // the split interface we avoid complexity within content and maintain
226 // reasonable utility for embedders.
227 class TestRenderViewHost
228 : public RenderViewHostImpl,
229 public RenderViewHostTester {
231 TestRenderViewHost(SiteInstance* instance,
232 RenderViewHostDelegate* delegate,
233 RenderWidgetHostDelegate* widget_delegate,
235 int main_frame_routing_id,
237 virtual ~TestRenderViewHost();
239 // RenderViewHostTester implementation. Note that CreateRenderView
240 // is not specified since it is synonymous with the one from
241 // RenderViewHostImpl, see below.
242 virtual void SendNavigate(int page_id, const GURL& url) OVERRIDE;
243 virtual void SendFailedNavigate(int page_id, const GURL& url) OVERRIDE;
244 virtual void SendNavigateWithTransition(int page_id, const GURL& url,
245 PageTransition transition) OVERRIDE;
246 virtual void SendShouldCloseACK(bool proceed) OVERRIDE;
247 virtual void SetContentsMimeType(const std::string& mime_type) OVERRIDE;
248 virtual void SimulateSwapOutACK() OVERRIDE;
249 virtual void SimulateWasHidden() OVERRIDE;
250 virtual void SimulateWasShown() OVERRIDE;
252 // Calls OnNavigate on the RenderViewHost with the given information,
253 // including a custom original request URL. Sets the rest of the
254 // parameters in the message to the "typical" values. This is a helper
255 // function for simulating the most common types of loads.
256 void SendNavigateWithOriginalRequestURL(
257 int page_id, const GURL& url, const GURL& original_request_url);
259 void SendNavigateWithFile(
260 int page_id, const GURL& url, const base::FilePath& file_path);
262 void TestOnUpdateStateWithFile(
263 int process_id, const base::FilePath& file_path);
265 void TestOnStartDragging(const DropData& drop_data);
267 // If set, *delete_counter is incremented when this object destructs.
268 void set_delete_counter(int* delete_counter) {
269 delete_counter_ = delete_counter;
272 // Sets whether the RenderView currently exists or not. This controls the
273 // return value from IsRenderViewLive, which the rest of the system uses to
274 // check whether the RenderView has crashed or not.
275 void set_render_view_created(bool created) {
276 render_view_created_ = created;
279 // Returns whether the RenderViewHost is currently waiting to hear the result
280 // of a before unload handler from the renderer.
281 bool is_waiting_for_beforeunload_ack() const {
282 return is_waiting_for_beforeunload_ack_;
285 // Returns whether the RenderViewHost is currently waiting to hear the result
286 // of an unload handler from the renderer.
287 bool is_waiting_for_unload_ack() const {
288 return is_waiting_for_unload_ack_;
291 // Sets whether the RenderViewHost is currently swapped out, and thus
292 // filtering messages from the renderer.
293 void set_is_swapped_out(bool is_swapped_out) {
294 is_swapped_out_ = is_swapped_out;
297 // If set, navigations will appear to have loaded through a proxy
298 // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy).
300 void set_simulate_fetch_via_proxy(bool proxy);
302 // If set, navigations will appear to have cleared the history list in the
303 // RenderView (ViewHostMsg_FrameNavigate_Params::history_list_was_cleared).
305 void set_simulate_history_list_was_cleared(bool cleared);
307 // The opener route id passed to CreateRenderView().
308 int opener_route_id() const { return opener_route_id_; }
310 // RenderViewHost overrides --------------------------------------------------
312 virtual bool CreateRenderView(const string16& frame_name,
314 int32 max_page_id) OVERRIDE;
315 virtual bool IsRenderViewLive() const OVERRIDE;
318 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, FilterNavigate);
320 void SendNavigateWithTransitionAndResponseCode(int page_id,
322 PageTransition transition,
325 // Calls OnNavigate on the RenderViewHost with the given information.
326 // Sets the rest of the parameters in the message to the "typical" values.
327 // This is a helper function for simulating the most common types of loads.
328 void SendNavigateWithParameters(
331 PageTransition transition,
332 const GURL& original_request_url,
334 const base::FilePath* file_path_for_history_item);
336 // Tracks if the caller thinks if it created the RenderView. This is so we can
337 // respond to IsRenderViewLive appropriately.
338 bool render_view_created_;
340 // See set_delete_counter() above. May be NULL.
341 int* delete_counter_;
343 // See set_simulate_fetch_via_proxy() above.
344 bool simulate_fetch_via_proxy_;
346 // See set_simulate_history_list_was_cleared() above.
347 bool simulate_history_list_was_cleared_;
349 // See SetContentsMimeType() above.
350 std::string contents_mime_type_;
352 // See opener_route_id() above.
353 int opener_route_id_;
355 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost);
358 #if defined(COMPILER_MSVC)
362 // Adds methods to get straight at the impl classes.
363 class RenderViewHostImplTestHarness : public RenderViewHostTestHarness {
365 RenderViewHostImplTestHarness();
366 virtual ~RenderViewHostImplTestHarness();
368 TestRenderViewHost* test_rvh();
369 TestRenderViewHost* pending_test_rvh();
370 TestRenderViewHost* active_test_rvh();
371 TestWebContents* contents();
374 typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
375 ScopedSetSupportedScaleFactors;
376 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
377 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness);
380 } // namespace content
382 #endif // CONTENT_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_