• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_
6 #define CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_
7 
8 #include <string>
9 #include <vector>
10 
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 "content/test/test_render_frame_host.h"
19 #include "ui/base/ime/dummy_text_input_client.h"
20 #include "ui/base/layout.h"
21 #include "ui/gfx/vector2d_f.h"
22 
23 // This file provides a testing framework for mocking out the RenderProcessHost
24 // layer. It allows you to test RenderViewHost, WebContentsImpl,
25 // NavigationController, and other layers above that without running an actual
26 // renderer process.
27 //
28 // To use, derive your test base class from RenderViewHostImplTestHarness.
29 
30 struct FrameHostMsg_DidCommitProvisionalLoad_Params;
31 struct ViewHostMsg_TextInputState_Params;
32 
33 namespace gfx {
34 class Rect;
35 }
36 
37 namespace content {
38 
39 class SiteInstance;
40 class TestRenderFrameHost;
41 class TestWebContents;
42 
43 // Utility function to initialize ViewHostMsg_NavigateParams_Params
44 // with given |page_id|, |url| and |transition_type|.
45 void InitNavigateParams(FrameHostMsg_DidCommitProvisionalLoad_Params* params,
46                         int page_id,
47                         const GURL& url,
48                         PageTransition transition_type);
49 
50 // TestRenderViewHostView ------------------------------------------------------
51 
52 // Subclass the RenderViewHost's view so that we can call Show(), etc.,
53 // without having side-effects.
54 class TestRenderWidgetHostView : public RenderWidgetHostViewBase {
55  public:
56   explicit TestRenderWidgetHostView(RenderWidgetHost* rwh);
57   virtual ~TestRenderWidgetHostView();
58 
59   // RenderWidgetHostView implementation.
InitAsChild(gfx::NativeView parent_view)60   virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE {}
61   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
SetSize(const gfx::Size & size)62   virtual void SetSize(const gfx::Size& size) OVERRIDE {}
SetBounds(const gfx::Rect & rect)63   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE {}
64   virtual gfx::NativeView GetNativeView() const OVERRIDE;
65   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
66   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
67   virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
68   virtual bool HasFocus() const OVERRIDE;
69   virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
70   virtual void Show() OVERRIDE;
71   virtual void Hide() OVERRIDE;
72   virtual bool IsShowing() OVERRIDE;
73   virtual gfx::Rect GetViewBounds() const OVERRIDE;
74 #if defined(OS_MACOSX)
75   virtual void SetActive(bool active) OVERRIDE;
SetTakesFocusOnlyOnMouseDown(bool flag)76   virtual void SetTakesFocusOnlyOnMouseDown(bool flag) OVERRIDE {}
SetWindowVisibility(bool visible)77   virtual void SetWindowVisibility(bool visible) OVERRIDE {}
WindowFrameChanged()78   virtual void WindowFrameChanged() OVERRIDE {}
ShowDefinitionForSelection()79   virtual void ShowDefinitionForSelection() OVERRIDE {}
80   virtual bool SupportsSpeech() const OVERRIDE;
81   virtual void SpeakSelection() OVERRIDE;
82   virtual bool IsSpeaking() const OVERRIDE;
83   virtual void StopSpeaking() OVERRIDE;
84 #endif  // defined(OS_MACOSX)
85   virtual void OnSwapCompositorFrame(
86       uint32 output_surface_id,
87       scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
88 
89   // RenderWidgetHostViewBase implementation.
InitAsPopup(RenderWidgetHostView * parent_host_view,const gfx::Rect & pos)90   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
91                            const gfx::Rect& pos) OVERRIDE {}
InitAsFullscreen(RenderWidgetHostView * reference_host_view)92   virtual void InitAsFullscreen(
93       RenderWidgetHostView* reference_host_view) OVERRIDE {}
WasShown()94   virtual void WasShown() OVERRIDE {}
WasHidden()95   virtual void WasHidden() OVERRIDE {}
MovePluginWindows(const std::vector<WebPluginGeometry> & moves)96   virtual void MovePluginWindows(
97       const std::vector<WebPluginGeometry>& moves) OVERRIDE {}
Focus()98   virtual void Focus() OVERRIDE {}
Blur()99   virtual void Blur() OVERRIDE {}
SetIsLoading(bool is_loading)100   virtual void SetIsLoading(bool is_loading) OVERRIDE {}
UpdateCursor(const WebCursor & cursor)101   virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE {}
TextInputStateChanged(const ViewHostMsg_TextInputState_Params & params)102   virtual void TextInputStateChanged(
103       const ViewHostMsg_TextInputState_Params& params) OVERRIDE {}
ImeCancelComposition()104   virtual void ImeCancelComposition() OVERRIDE {}
105 #if defined(OS_MACOSX) || defined(USE_AURA)
ImeCompositionRangeChanged(const gfx::Range & range,const std::vector<gfx::Rect> & character_bounds)106   virtual void ImeCompositionRangeChanged(
107       const gfx::Range& range,
108       const std::vector<gfx::Rect>& character_bounds) OVERRIDE {}
109 #endif
110   virtual void RenderProcessGone(base::TerminationStatus status,
111                                  int error_code) OVERRIDE;
112   virtual void Destroy() OVERRIDE;
SetTooltipText(const base::string16 & tooltip_text)113   virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE {}
SelectionBoundsChanged(const ViewHostMsg_SelectionBounds_Params & params)114   virtual void SelectionBoundsChanged(
115       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE {}
ScrollOffsetChanged()116   virtual void ScrollOffsetChanged() OVERRIDE {}
117   virtual void CopyFromCompositingSurface(
118       const gfx::Rect& src_subrect,
119       const gfx::Size& dst_size,
120       const base::Callback<void(bool, const SkBitmap&)>& callback,
121       const SkBitmap::Config config) OVERRIDE;
122   virtual void CopyFromCompositingSurfaceToVideoFrame(
123       const gfx::Rect& src_subrect,
124       const scoped_refptr<media::VideoFrame>& target,
125       const base::Callback<void(bool)>& callback) OVERRIDE;
126   virtual bool CanCopyToVideoFrame() const OVERRIDE;
127   virtual void AcceleratedSurfaceInitialized(int host_id,
128                                              int route_id) OVERRIDE;
129   virtual void AcceleratedSurfaceBuffersSwapped(
130       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
131       int gpu_host_id) OVERRIDE;
132   virtual void AcceleratedSurfacePostSubBuffer(
133       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
134       int gpu_host_id) OVERRIDE;
135   virtual void AcceleratedSurfaceSuspend() OVERRIDE;
AcceleratedSurfaceRelease()136   virtual void AcceleratedSurfaceRelease() OVERRIDE {}
137   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
138 #if defined(OS_MACOSX)
139   virtual bool PostProcessEventForPluginIme(
140       const NativeWebKeyboardEvent& event) OVERRIDE;
141 #elif defined(OS_ANDROID)
ShowDisambiguationPopup(const gfx::Rect & target_rect,const SkBitmap & zoomed_bitmap)142   virtual void ShowDisambiguationPopup(
143       const gfx::Rect& target_rect,
144       const SkBitmap& zoomed_bitmap) OVERRIDE {}
LockCompositingSurface()145   virtual void LockCompositingSurface() OVERRIDE {}
UnlockCompositingSurface()146   virtual void UnlockCompositingSurface() OVERRIDE {}
147 #endif
GetScreenInfo(blink::WebScreenInfo * results)148   virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE {}
149   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
150   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
151   virtual bool LockMouse() OVERRIDE;
152   virtual void UnlockMouse() OVERRIDE;
153 #if defined(OS_WIN)
154   virtual void SetParentNativeViewAccessible(
155       gfx::NativeViewAccessible accessible_parent) OVERRIDE;
156   virtual gfx::NativeViewId GetParentForWindowlessPlugin() const OVERRIDE;
157 #endif
158 
is_showing()159   bool is_showing() const { return is_showing_; }
did_swap_compositor_frame()160   bool did_swap_compositor_frame() const { return did_swap_compositor_frame_; }
161 
162  protected:
163   RenderWidgetHostImpl* rwh_;
164 
165  private:
166   bool is_showing_;
167   bool did_swap_compositor_frame_;
168   ui::DummyTextInputClient text_input_client_;
169 };
170 
171 #if defined(COMPILER_MSVC)
172 // See comment for same warning on RenderViewHostImpl.
173 #pragma warning(push)
174 #pragma warning(disable: 4250)
175 #endif
176 
177 // TestRenderViewHost ----------------------------------------------------------
178 
179 // TODO(brettw) this should use a TestWebContents which should be generalized
180 // from the WebContentsImpl test. We will probably also need that class' version
181 // of CreateRenderViewForRenderManager when more complicated tests start using
182 // this.
183 //
184 // Note that users outside of content must use this class by getting
185 // the separate RenderViewHostTester interface via
186 // RenderViewHostTester::For(rvh) on the RenderViewHost they want to
187 // drive tests on.
188 //
189 // Users within content may directly static_cast from a
190 // RenderViewHost* to a TestRenderViewHost*.
191 //
192 // The reasons we do it this way rather than extending the parallel
193 // inheritance hierarchy we have for RenderWidgetHost/RenderViewHost
194 // vs. RenderWidgetHostImpl/RenderViewHostImpl are:
195 //
196 // a) Extending the parallel class hierarchy further would require
197 // more classes to use virtual inheritance.  This is a complexity that
198 // is better to avoid, especially when it would be introduced in the
199 // production code solely to facilitate testing code.
200 //
201 // b) While users outside of content only need to drive tests on a
202 // RenderViewHost, content needs a test version of the full
203 // RenderViewHostImpl so that it can test all methods on that concrete
204 // class (e.g. overriding a method such as
205 // RenderViewHostImpl::CreateRenderView).  This would have complicated
206 // the dual class hierarchy even further.
207 //
208 // The reason we do it this way instead of using composition is
209 // similar to (b) above, essentially it gets very tricky.  By using
210 // the split interface we avoid complexity within content and maintain
211 // reasonable utility for embedders.
212 class TestRenderViewHost
213     : public RenderViewHostImpl,
214       public RenderViewHostTester {
215  public:
216   TestRenderViewHost(SiteInstance* instance,
217                      RenderViewHostDelegate* delegate,
218                      RenderWidgetHostDelegate* widget_delegate,
219                      int routing_id,
220                      int main_frame_routing_id,
221                      bool swapped_out);
222   virtual ~TestRenderViewHost();
223 
224   // RenderViewHostTester implementation.  Note that CreateRenderView
225   // is not specified since it is synonymous with the one from
226   // RenderViewHostImpl, see below.
227   virtual void SendBeforeUnloadACK(bool proceed) OVERRIDE;
228   virtual void SetContentsMimeType(const std::string& mime_type) OVERRIDE;
229   virtual void SimulateSwapOutACK() OVERRIDE;
230   virtual void SimulateWasHidden() OVERRIDE;
231   virtual void SimulateWasShown() OVERRIDE;
232 
233   // NOTE: These methods are deprecated and the equivalents in
234   // TestRenderFrameHost should be used.
235   virtual void SendNavigate(int page_id, const GURL& url) OVERRIDE;
236   virtual void SendFailedNavigate(int page_id, const GURL& url) OVERRIDE;
237   virtual void SendNavigateWithTransition(int page_id, const GURL& url,
238                                           PageTransition transition) OVERRIDE;
239 
240   // Calls OnNavigate on the RenderViewHost with the given information,
241   // including a custom original request URL.  Sets the rest of the
242   // parameters in the message to the "typical" values.  This is a helper
243   // function for simulating the most common types of loads.
244   void SendNavigateWithOriginalRequestURL(
245       int page_id, const GURL& url, const GURL& original_request_url);
246 
247   void SendNavigateWithFile(
248       int page_id, const GURL& url, const base::FilePath& file_path);
249 
250   void SendNavigateWithParams(
251       FrameHostMsg_DidCommitProvisionalLoad_Params* params);
252 
253   void TestOnUpdateStateWithFile(
254       int process_id, const base::FilePath& file_path);
255 
256   void TestOnStartDragging(const DropData& drop_data);
257 
258   // If set, *delete_counter is incremented when this object destructs.
set_delete_counter(int * delete_counter)259   void set_delete_counter(int* delete_counter) {
260     delete_counter_ = delete_counter;
261   }
262 
263   // Sets whether the RenderView currently exists or not. This controls the
264   // return value from IsRenderViewLive, which the rest of the system uses to
265   // check whether the RenderView has crashed or not.
set_render_view_created(bool created)266   void set_render_view_created(bool created) {
267     render_view_created_ = created;
268   }
269 
270   // Returns whether the RenderViewHost is currently waiting to hear the result
271   // of a before unload handler from the renderer.
is_waiting_for_beforeunload_ack()272   bool is_waiting_for_beforeunload_ack() const {
273     return is_waiting_for_beforeunload_ack_;
274   }
275 
276   // Sets whether the RenderViewHost is currently swapped out, and thus
277   // filtering messages from the renderer.
set_rvh_state(RenderViewHostImplState rvh_state)278   void set_rvh_state(RenderViewHostImplState rvh_state) {
279     rvh_state_ = rvh_state;
280   }
281 
282   // If set, navigations will appear to have loaded through a proxy
283   // (ViewHostMsg_FrameNavigte_Params::was_fetched_via_proxy).
284   // False by default.
285   void set_simulate_fetch_via_proxy(bool proxy);
286 
287   // If set, navigations will appear to have cleared the history list in the
288   // RenderView
289   // (FrameHostMsg_DidCommitProvisionalLoad_Params::history_list_was_cleared).
290   // False by default.
291   void set_simulate_history_list_was_cleared(bool cleared);
292 
293   // The opener route id passed to CreateRenderView().
opener_route_id()294   int opener_route_id() const { return opener_route_id_; }
295 
296   // TODO(creis): Remove the need for these methods.
main_render_frame_host()297   TestRenderFrameHost* main_render_frame_host() const {
298     return main_render_frame_host_;
299   }
set_main_render_frame_host(TestRenderFrameHost * rfh)300   void set_main_render_frame_host(TestRenderFrameHost* rfh) {
301     main_render_frame_host_ = rfh;
302   }
303 
304   // RenderViewHost overrides --------------------------------------------------
305 
306   virtual bool CreateRenderView(const base::string16& frame_name,
307                                 int opener_route_id,
308                                 int proxy_route_id,
309                                 int32 max_page_id,
310                                 bool window_was_created_with_opener) OVERRIDE;
311   virtual bool IsRenderViewLive() const OVERRIDE;
312   virtual bool IsFullscreen() const OVERRIDE;
313 
314  private:
315   FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, FilterNavigate);
316 
317   void SendNavigateWithTransitionAndResponseCode(int page_id,
318                                                  const GURL& url,
319                                                  PageTransition transition,
320                                                  int response_code);
321 
322   // Calls OnNavigate on the RenderViewHost with the given information.
323   // Sets the rest of the parameters in the message to the "typical" values.
324   // This is a helper function for simulating the most common types of loads.
325   void SendNavigateWithParameters(
326       int page_id,
327       const GURL& url,
328       PageTransition transition,
329       const GURL& original_request_url,
330       int response_code,
331       const base::FilePath* file_path_for_history_item);
332 
333   // Tracks if the caller thinks if it created the RenderView. This is so we can
334   // respond to IsRenderViewLive appropriately.
335   bool render_view_created_;
336 
337   // See set_delete_counter() above. May be NULL.
338   int* delete_counter_;
339 
340   // See set_simulate_fetch_via_proxy() above.
341   bool simulate_fetch_via_proxy_;
342 
343   // See set_simulate_history_list_was_cleared() above.
344   bool simulate_history_list_was_cleared_;
345 
346   // See SetContentsMimeType() above.
347   std::string contents_mime_type_;
348 
349   // See opener_route_id() above.
350   int opener_route_id_;
351 
352   TestRenderFrameHost* main_render_frame_host_;
353 
354   DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost);
355 };
356 
357 #if defined(COMPILER_MSVC)
358 #pragma warning(pop)
359 #endif
360 
361 // Adds methods to get straight at the impl classes.
362 class RenderViewHostImplTestHarness : public RenderViewHostTestHarness {
363  public:
364   RenderViewHostImplTestHarness();
365   virtual ~RenderViewHostImplTestHarness();
366 
367   TestRenderViewHost* test_rvh();
368   TestRenderViewHost* pending_test_rvh();
369   TestRenderViewHost* active_test_rvh();
370   TestRenderFrameHost* main_test_rfh();
371   TestWebContents* contents();
372 
373  private:
374   typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
375       ScopedSetSupportedScaleFactors;
376   ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
377   DISALLOW_COPY_AND_ASSIGN(RenderViewHostImplTestHarness);
378 };
379 
380 }  // namespace content
381 
382 #endif  // CONTENT_TEST_TEST_RENDER_VIEW_HOST_H_
383