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_TEST_TEST_WEB_CONTENTS_H_
      6 #define CONTENT_TEST_TEST_WEB_CONTENTS_H_
      7 
      8 #include "content/browser/web_contents/web_contents_impl.h"
      9 #include "content/public/common/page_transition_types.h"
     10 #include "content/public/test/web_contents_tester.h"
     11 #include "webkit/common/webpreferences.h"
     12 
     13 class SiteInstanceImpl;
     14 
     15 namespace content {
     16 
     17 class RenderViewHost;
     18 class TestRenderViewHost;
     19 class WebContentsTester;
     20 
     21 // Subclass WebContentsImpl to ensure it creates TestRenderViewHosts
     22 // and does not do anything involving views.
     23 class TestWebContents : public WebContentsImpl, public WebContentsTester {
     24  public:
     25   virtual ~TestWebContents();
     26 
     27   static TestWebContents* Create(BrowserContext* browser_context,
     28                                  SiteInstance* instance);
     29 
     30   // WebContentsTester implementation.
     31   virtual void CommitPendingNavigation() OVERRIDE;
     32   virtual RenderViewHost* GetPendingRenderViewHost() const OVERRIDE;
     33   virtual void NavigateAndCommit(const GURL& url) OVERRIDE;
     34   virtual void TestSetIsLoading(bool value) OVERRIDE;
     35   virtual void ProceedWithCrossSiteNavigation() OVERRIDE;
     36   virtual void TestDidNavigate(RenderViewHost* render_view_host,
     37                                int page_id,
     38                                const GURL& url,
     39                                PageTransition transition) OVERRIDE;
     40   virtual void TestDidNavigateWithReferrer(RenderViewHost* render_view_host,
     41                                            int page_id,
     42                                            const GURL& url,
     43                                            const Referrer& referrer,
     44                                            PageTransition transition) OVERRIDE;
     45   virtual WebPreferences TestGetWebkitPrefs() OVERRIDE;
     46 
     47   TestRenderViewHost* pending_test_rvh() const;
     48 
     49   // State accessor.
     50   bool cross_navigation_pending() {
     51     return GetRenderManager()->cross_navigation_pending_;
     52   }
     53 
     54   // Prevent interaction with views.
     55   virtual bool CreateRenderViewForRenderManager(
     56       RenderViewHost* render_view_host,
     57       int opener_route_id,
     58       int proxy_routing_id,
     59       bool for_main_frame) OVERRIDE;
     60   virtual void UpdateRenderViewSizeForRenderManager() OVERRIDE {}
     61 
     62   // Returns a clone of this TestWebContents. The returned object is also a
     63   // TestWebContents. The caller owns the returned object.
     64   virtual WebContents* Clone() OVERRIDE;
     65 
     66   // Allow mocking of the RenderViewHostDelegateView.
     67   virtual RenderViewHostDelegateView* GetDelegateView() OVERRIDE;
     68   void set_delegate_view(RenderViewHostDelegateView* view) {
     69     delegate_view_override_ = view;
     70   }
     71 
     72   // Allows us to simulate this tab having an opener.
     73   void SetOpener(TestWebContents* opener);
     74 
     75   // Allows us to simulate that a contents was created via CreateNewWindow.
     76   void AddPendingContents(TestWebContents* contents);
     77 
     78   // Establish expected arguments for |SetHistoryLengthAndPrune()|. When
     79   // |SetHistoryLengthAndPrune()| is called, the arguments are compared
     80   // with the expected arguments specified here.
     81   void ExpectSetHistoryLengthAndPrune(const SiteInstance* site_instance,
     82                                       int history_length,
     83                                       int32 min_page_id);
     84 
     85   // Compares the arguments passed in with the expected arguments passed in
     86   // to |ExpectSetHistoryLengthAndPrune()|.
     87   virtual void SetHistoryLengthAndPrune(const SiteInstance* site_instance,
     88                                         int history_length,
     89                                         int32 min_page_id) OVERRIDE;
     90 
     91   void TestDidFinishLoad(const GURL& url);
     92   void TestDidFailLoadWithError(const GURL& url,
     93                                 int error_code,
     94                                 const base::string16& error_description);
     95 
     96  protected:
     97   // The deprecated WebContentsTester still needs to subclass this.
     98   explicit TestWebContents(BrowserContext* browser_context);
     99 
    100  private:
    101   // WebContentsImpl overrides
    102   virtual void CreateNewWindow(
    103       int render_process_id,
    104       int route_id,
    105       int main_frame_route_id,
    106       const ViewHostMsg_CreateWindow_Params& params,
    107       SessionStorageNamespace* session_storage_namespace) OVERRIDE;
    108   virtual void CreateNewWidget(int render_process_id,
    109                                int route_id,
    110                                blink::WebPopupType popup_type) OVERRIDE;
    111   virtual void CreateNewFullscreenWidget(int render_process_id,
    112                                          int route_id) OVERRIDE;
    113   virtual void ShowCreatedWindow(int route_id,
    114                                  WindowOpenDisposition disposition,
    115                                  const gfx::Rect& initial_pos,
    116                                  bool user_gesture) OVERRIDE;
    117   virtual void ShowCreatedWidget(int route_id,
    118                                  const gfx::Rect& initial_pos) OVERRIDE;
    119   virtual void ShowCreatedFullscreenWidget(int route_id) OVERRIDE;
    120 
    121 
    122   RenderViewHostDelegateView* delegate_view_override_;
    123 
    124   // Expectations for arguments of |SetHistoryLengthAndPrune()|.
    125   bool expect_set_history_length_and_prune_;
    126   scoped_refptr<const SiteInstanceImpl>
    127     expect_set_history_length_and_prune_site_instance_;
    128   int expect_set_history_length_and_prune_history_length_;
    129   int32 expect_set_history_length_and_prune_min_page_id_;
    130 };
    131 
    132 }  // namespace content
    133 
    134 #endif  // CONTENT_TEST_TEST_WEB_CONTENTS_H_
    135