Home | History | Annotate | Download | only in test
      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_PUBLIC_TEST_TEST_RENDERER_HOST_H_
      6 #define CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "content/public/browser/render_frame_host.h"
     11 #include "content/public/browser/render_view_host.h"
     12 #include "content/public/common/page_transition_types.h"
     13 #include "content/public/test/test_browser_thread_bundle.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 #if defined(USE_AURA)
     17 #include "ui/aura/test/aura_test_helper.h"
     18 #endif
     19 
     20 namespace aura {
     21 namespace test {
     22 class AuraTestHelper;
     23 }
     24 }
     25 
     26 namespace ui {
     27 class ScopedOleInitializer;
     28 }
     29 
     30 namespace content {
     31 
     32 class BrowserContext;
     33 class MockRenderProcessHost;
     34 class MockRenderProcessHostFactory;
     35 class NavigationController;
     36 class RenderProcessHostFactory;
     37 class RenderViewHostDelegate;
     38 class TestRenderFrameHostFactory;
     39 class TestRenderViewHostFactory;
     40 class WebContents;
     41 
     42 // An interface and utility for driving tests of RenderViewHost.
     43 class RenderViewHostTester {
     44  public:
     45   // Retrieves the RenderViewHostTester that drives the specified
     46   // RenderViewHost.  The RenderViewHost must have been created while
     47   // RenderViewHost testing was enabled; use a
     48   // RenderViewHostTestEnabler instance (see below) to do this.
     49   static RenderViewHostTester* For(RenderViewHost* host);
     50 
     51   // If the given WebContentsImpl has a pending RVH, returns it, otherwise NULL.
     52   static RenderViewHost* GetPendingForController(
     53       NavigationController* controller);
     54 
     55   // This removes the need to expose
     56   // RenderViewHostImpl::is_swapped_out() outside of content.
     57   //
     58   // This is safe to call on any RenderViewHost, not just ones
     59   // constructed while a RenderViewHostTestEnabler is in play.
     60   static bool IsRenderViewHostSwappedOut(RenderViewHost* rvh);
     61 
     62   // Calls the RenderViewHosts' private OnMessageReceived function with the
     63   // given message.
     64   static bool TestOnMessageReceived(RenderViewHost* rvh,
     65                                     const IPC::Message& msg);
     66 
     67   // Returns whether the underlying web-page has any touch-event handlers.
     68   static bool HasTouchEventHandler(RenderViewHost* rvh);
     69 
     70   virtual ~RenderViewHostTester() {}
     71 
     72   // Gives tests access to RenderViewHostImpl::CreateRenderView.
     73   virtual bool CreateRenderView(const base::string16& frame_name,
     74                                 int opener_route_id,
     75                                 int proxy_routing_id,
     76                                 int32 max_page_id,
     77                                 bool created_with_opener) = 0;
     78 
     79   // Calls OnMsgNavigate on the RenderViewHost with the given information,
     80   // setting the rest of the parameters in the message to the "typical" values.
     81   // This is a helper function for simulating the most common types of loads.
     82   virtual void SendNavigate(int page_id, const GURL& url) = 0;
     83   virtual void SendFailedNavigate(int page_id, const GURL& url) = 0;
     84 
     85   // Calls OnMsgNavigate on the RenderViewHost with the given information,
     86   // including a custom PageTransition.  Sets the rest of the
     87   // parameters in the message to the "typical" values. This is a helper
     88   // function for simulating the most common types of loads.
     89   virtual void SendNavigateWithTransition(int page_id, const GURL& url,
     90                                           PageTransition transition) = 0;
     91 
     92   // Calls OnBeforeUnloadACK on the main RenderFrameHost with the given
     93   // parameter.
     94   virtual void SendBeforeUnloadACK(bool proceed) = 0;
     95 
     96   // If set, future loads will have |mime_type| set as the mime type.
     97   // If not set, the mime type will default to "text/html".
     98   virtual void SetContentsMimeType(const std::string& mime_type) = 0;
     99 
    100   // Simulates the SwapOut_ACK that fires if you commit a cross-site
    101   // navigation without making any network requests.
    102   virtual void SimulateSwapOutACK() = 0;
    103 
    104   // Makes the WasHidden/WasShown calls to the RenderWidget that
    105   // tell it it has been hidden or restored from having been hidden.
    106   virtual void SimulateWasHidden() = 0;
    107   virtual void SimulateWasShown() = 0;
    108 };
    109 
    110 // You can instantiate only one class like this at a time.  During its
    111 // lifetime, RenderViewHost objects created may be used via
    112 // RenderViewHostTester.
    113 class RenderViewHostTestEnabler {
    114  public:
    115   RenderViewHostTestEnabler();
    116   ~RenderViewHostTestEnabler();
    117 
    118  private:
    119   DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestEnabler);
    120   friend class RenderViewHostTestHarness;
    121 
    122   scoped_ptr<MockRenderProcessHostFactory> rph_factory_;
    123   scoped_ptr<TestRenderViewHostFactory> rvh_factory_;
    124   scoped_ptr<TestRenderFrameHostFactory> rfh_factory_;
    125 };
    126 
    127 // RenderViewHostTestHarness ---------------------------------------------------
    128 class RenderViewHostTestHarness : public testing::Test {
    129  public:
    130   RenderViewHostTestHarness();
    131   virtual ~RenderViewHostTestHarness();
    132 
    133   NavigationController& controller();
    134   WebContents* web_contents();
    135   RenderViewHost* rvh();
    136   RenderViewHost* pending_rvh();
    137   RenderViewHost* active_rvh();
    138   RenderFrameHost* main_rfh();
    139   BrowserContext* browser_context();
    140   MockRenderProcessHost* process();
    141 
    142   // Frees the current WebContents for tests that want to test destruction.
    143   void DeleteContents();
    144 
    145   // Sets the current WebContents for tests that want to alter it. Takes
    146   // ownership of the WebContents passed.
    147   void SetContents(WebContents* contents);
    148 
    149   // Creates a new test-enabled WebContents. Ownership passes to the
    150   // caller.
    151   WebContents* CreateTestWebContents();
    152 
    153   // Cover for |contents()->NavigateAndCommit(url)|. See
    154   // WebContentsTester::NavigateAndCommit for details.
    155   void NavigateAndCommit(const GURL& url);
    156 
    157   // Simulates a reload of the current page.
    158   void Reload();
    159   void FailedReload();
    160 
    161  protected:
    162   // testing::Test
    163   virtual void SetUp() OVERRIDE;
    164   virtual void TearDown() OVERRIDE;
    165 
    166   // Derived classes should override this method to use a custom BrowserContext.
    167   // It is invoked by SetUp after threads were started.
    168   // RenderViewHostTestHarness will take ownership of the returned
    169   // BrowserContext.
    170   virtual BrowserContext* CreateBrowserContext();
    171 
    172   // Configures which TestBrowserThreads inside |thread_bundle| are backed by
    173   // real threads. Must be called before SetUp().
    174   void SetThreadBundleOptions(int options) {
    175     DCHECK(thread_bundle_.get() == NULL);
    176     thread_bundle_options_ = options;
    177   }
    178 
    179   TestBrowserThreadBundle* thread_bundle() { return thread_bundle_.get(); }
    180 
    181 #if defined(USE_AURA)
    182   aura::Window* root_window() { return aura_test_helper_->root_window(); }
    183 #endif
    184 
    185   // Replaces the RPH being used.
    186   void SetRenderProcessHostFactory(RenderProcessHostFactory* factory);
    187 
    188  private:
    189   scoped_ptr<BrowserContext> browser_context_;
    190 
    191   // It is important not to use this directly in the implementation as
    192   // web_contents() and SetContents() are virtual and may be
    193   // overridden by subclasses.
    194   scoped_ptr<WebContents> contents_;
    195 #if defined(OS_WIN)
    196   scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
    197 #endif
    198 #if defined(USE_AURA)
    199   scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
    200 #endif
    201   RenderViewHostTestEnabler rvh_test_enabler_;
    202 
    203   int thread_bundle_options_;
    204   scoped_ptr<TestBrowserThreadBundle> thread_bundle_;
    205 
    206   DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness);
    207 };
    208 
    209 }  // namespace content
    210 
    211 #endif  // CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
    212