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