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 #include "content/test/test_web_contents.h"
      6 
      7 #include <utility>
      8 
      9 #include "content/browser/browser_url_handler_impl.h"
     10 #include "content/browser/renderer_host/render_view_host_impl.h"
     11 #include "content/browser/renderer_host/test_render_view_host.h"
     12 #include "content/browser/site_instance_impl.h"
     13 #include "content/browser/web_contents/navigation_entry_impl.h"
     14 #include "content/common/view_messages.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "content/public/browser/notification_source.h"
     17 #include "content/public/browser/notification_types.h"
     18 #include "content/public/common/page_state.h"
     19 #include "content/public/common/page_transition_types.h"
     20 #include "content/public/common/password_form.h"
     21 #include "content/public/test/mock_render_process_host.h"
     22 
     23 namespace content {
     24 
     25 TestWebContents::TestWebContents(BrowserContext* browser_context)
     26     : WebContentsImpl(browser_context, NULL),
     27       transition_cross_site(false),
     28       delegate_view_override_(NULL),
     29       expect_set_history_length_and_prune_(false),
     30       expect_set_history_length_and_prune_site_instance_(NULL),
     31       expect_set_history_length_and_prune_history_length_(0),
     32       expect_set_history_length_and_prune_min_page_id_(-1) {
     33 }
     34 
     35 TestWebContents* TestWebContents::Create(BrowserContext* browser_context,
     36                                          SiteInstance* instance) {
     37   TestWebContents* test_web_contents = new TestWebContents(browser_context);
     38   test_web_contents->Init(WebContents::CreateParams(browser_context, instance));
     39   return test_web_contents;
     40 }
     41 
     42 TestWebContents::~TestWebContents() {
     43   EXPECT_FALSE(expect_set_history_length_and_prune_);
     44 }
     45 
     46 RenderViewHost* TestWebContents::GetPendingRenderViewHost() const {
     47   return render_manager_.pending_render_view_host_;
     48 }
     49 
     50 TestRenderViewHost* TestWebContents::pending_test_rvh() const {
     51   return static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
     52 }
     53 
     54 void TestWebContents::TestDidNavigate(RenderViewHost* render_view_host,
     55                                       int page_id,
     56                                       const GURL& url,
     57                                       PageTransition transition) {
     58   TestDidNavigateWithReferrer(render_view_host,
     59                               page_id,
     60                               url,
     61                               Referrer(),
     62                               transition);
     63 }
     64 
     65 void TestWebContents::TestDidNavigateWithReferrer(
     66     RenderViewHost* render_view_host,
     67     int page_id,
     68     const GURL& url,
     69     const Referrer& referrer,
     70     PageTransition transition) {
     71   ViewHostMsg_FrameNavigate_Params params;
     72 
     73   params.page_id = page_id;
     74   params.url = url;
     75   params.referrer = referrer;
     76   params.transition = transition;
     77   params.redirects = std::vector<GURL>();
     78   params.should_update_history = false;
     79   params.searchable_form_url = GURL();
     80   params.searchable_form_encoding = std::string();
     81   params.password_form = PasswordForm();
     82   params.security_info = std::string();
     83   params.gesture = NavigationGestureUser;
     84   params.was_within_same_page = false;
     85   params.is_post = false;
     86   params.page_state = PageState::CreateFromURL(url);
     87 
     88   DidNavigate(render_view_host, params);
     89 }
     90 
     91 WebPreferences TestWebContents::TestGetWebkitPrefs() {
     92   return GetWebkitPrefs();
     93 }
     94 
     95 bool TestWebContents::CreateRenderViewForRenderManager(
     96     RenderViewHost* render_view_host, int opener_route_id) {
     97   // This will go to a TestRenderViewHost.
     98   static_cast<RenderViewHostImpl*>(
     99       render_view_host)->CreateRenderView(string16(),
    100                                           opener_route_id,
    101                                           -1);
    102   return true;
    103 }
    104 
    105 WebContents* TestWebContents::Clone() {
    106   WebContentsImpl* contents =
    107       Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
    108   contents->GetController().CopyStateFrom(controller_);
    109   return contents;
    110 }
    111 
    112 void TestWebContents::NavigateAndCommit(const GURL& url) {
    113   GetController().LoadURL(
    114       url, Referrer(), PAGE_TRANSITION_LINK, std::string());
    115   GURL loaded_url(url);
    116   bool reverse_on_redirect = false;
    117   BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
    118       &loaded_url, GetBrowserContext(), &reverse_on_redirect);
    119 
    120   // LoadURL created a navigation entry, now simulate the RenderView sending
    121   // a notification that it actually navigated.
    122   CommitPendingNavigation();
    123 }
    124 
    125 void TestWebContents::TestSetIsLoading(bool value) {
    126   SetIsLoading(value, NULL);
    127 }
    128 
    129 void TestWebContents::CommitPendingNavigation() {
    130   // If we are doing a cross-site navigation, this simulates the current RVH
    131   // notifying that it has unloaded so the pending RVH is resumed and can
    132   // navigate.
    133   ProceedWithCrossSiteNavigation();
    134   RenderViewHost* old_rvh = render_manager_.current_host();
    135   TestRenderViewHost* rvh =
    136       static_cast<TestRenderViewHost*>(GetPendingRenderViewHost());
    137   if (!rvh)
    138     rvh = static_cast<TestRenderViewHost*>(old_rvh);
    139 
    140   const NavigationEntry* entry = GetController().GetPendingEntry();
    141   DCHECK(entry);
    142   int page_id = entry->GetPageID();
    143   if (page_id == -1) {
    144     // It's a new navigation, assign a never-seen page id to it.
    145     page_id = GetMaxPageIDForSiteInstance(rvh->GetSiteInstance()) + 1;
    146   }
    147 
    148   // Simulate the SwapOut_ACK that happens when we swap out the old
    149   // RVH, before the navigation commits. This is needed when
    150   // cross-site navigation happens (old_rvh != rvh).
    151   if (old_rvh != rvh)
    152     static_cast<RenderViewHostImpl*>(old_rvh)->OnSwappedOut(false);
    153   rvh->SendNavigate(page_id, entry->GetURL());
    154 }
    155 
    156 void TestWebContents::ProceedWithCrossSiteNavigation() {
    157   if (!GetPendingRenderViewHost())
    158     return;
    159   TestRenderViewHost* rvh = static_cast<TestRenderViewHost*>(
    160       render_manager_.current_host());
    161   rvh->SendShouldCloseACK(true);
    162 }
    163 
    164 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
    165   if (delegate_view_override_)
    166     return delegate_view_override_;
    167   return WebContentsImpl::GetDelegateView();
    168 }
    169 
    170 void TestWebContents::SetOpener(TestWebContents* opener) {
    171   // This is normally only set in the WebContents constructor, which also
    172   // registers an observer for when the opener gets closed.
    173   opener_ = opener;
    174   AddDestructionObserver(opener_);
    175 }
    176 
    177 void TestWebContents::AddPendingContents(TestWebContents* contents) {
    178   // This is normally only done in WebContentsImpl::CreateNewWindow.
    179   pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
    180   AddDestructionObserver(contents);
    181 }
    182 
    183 void TestWebContents::ExpectSetHistoryLengthAndPrune(
    184     const SiteInstance* site_instance,
    185     int history_length,
    186     int32 min_page_id) {
    187   expect_set_history_length_and_prune_ = true;
    188   expect_set_history_length_and_prune_site_instance_ =
    189       static_cast<const SiteInstanceImpl*>(site_instance);
    190   expect_set_history_length_and_prune_history_length_ = history_length;
    191   expect_set_history_length_and_prune_min_page_id_ = min_page_id;
    192 }
    193 
    194 void TestWebContents::SetHistoryLengthAndPrune(
    195     const SiteInstance* site_instance, int history_length,
    196     int32 min_page_id) {
    197   EXPECT_TRUE(expect_set_history_length_and_prune_);
    198   expect_set_history_length_and_prune_ = false;
    199   EXPECT_EQ(expect_set_history_length_and_prune_site_instance_, site_instance);
    200   EXPECT_EQ(expect_set_history_length_and_prune_history_length_,
    201             history_length);
    202   EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_, min_page_id);
    203 }
    204 
    205 void TestWebContents::TestDidFinishLoad(int64 frame_id,
    206                                         const GURL& url,
    207                                         bool is_main_frame) {
    208   ViewHostMsg_DidFinishLoad msg(0, frame_id, url, is_main_frame);
    209   OnMessageReceived(render_manager_.current_host(), msg);
    210 }
    211 
    212 void TestWebContents::TestDidFailLoadWithError(
    213     int64 frame_id,
    214     const GURL& url,
    215     bool is_main_frame,
    216     int error_code,
    217     const string16& error_description) {
    218   ViewHostMsg_DidFailLoadWithError msg(
    219       0, frame_id, url, is_main_frame, error_code, error_description);
    220   OnMessageReceived(render_manager_.current_host(), msg);
    221 }
    222 
    223 void TestWebContents::CreateNewWindow(
    224     int route_id,
    225     int main_frame_route_id,
    226     const ViewHostMsg_CreateWindow_Params& params,
    227     SessionStorageNamespace* session_storage_namespace) {
    228 }
    229 
    230 void TestWebContents::CreateNewWidget(int route_id,
    231                                       WebKit::WebPopupType popup_type) {
    232 }
    233 
    234 void TestWebContents::CreateNewFullscreenWidget(int route_id) {
    235 }
    236 
    237 void TestWebContents::ShowCreatedWindow(int route_id,
    238                                         WindowOpenDisposition disposition,
    239                                         const gfx::Rect& initial_pos,
    240                                         bool user_gesture) {
    241 }
    242 
    243 void TestWebContents::ShowCreatedWidget(int route_id,
    244                                         const gfx::Rect& initial_pos) {
    245 }
    246 
    247 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
    248 }
    249 
    250 }  // namespace content
    251