test_web_contents.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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 23namespace content { 24 25TestWebContents::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 35TestWebContents* 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 42TestWebContents::~TestWebContents() { 43 EXPECT_FALSE(expect_set_history_length_and_prune_); 44} 45 46RenderViewHost* TestWebContents::GetPendingRenderViewHost() const { 47 return render_manager_.pending_render_view_host_; 48} 49 50TestRenderViewHost* TestWebContents::pending_test_rvh() const { 51 return static_cast<TestRenderViewHost*>(GetPendingRenderViewHost()); 52} 53 54void 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 65void 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 91WebPreferences TestWebContents::TestGetWebkitPrefs() { 92 return GetWebkitPrefs(); 93} 94 95bool 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 105WebContents* TestWebContents::Clone() { 106 WebContentsImpl* contents = 107 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext())); 108 contents->GetController().CopyStateFrom(controller_); 109 return contents; 110} 111 112void 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 125void TestWebContents::TestSetIsLoading(bool value) { 126 SetIsLoading(value, NULL); 127} 128 129void 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 rvh->SendNavigate(page_id, entry->GetURL()); 148 149 // Simulate the SwapOut_ACK that fires if you commit a cross-site navigation 150 // without making any network requests. 151 if (old_rvh != rvh) 152 static_cast<RenderViewHostImpl*>(old_rvh)->OnSwapOutACK(false); 153} 154 155void TestWebContents::ProceedWithCrossSiteNavigation() { 156 if (!GetPendingRenderViewHost()) 157 return; 158 TestRenderViewHost* rvh = static_cast<TestRenderViewHost*>( 159 render_manager_.current_host()); 160 rvh->SendShouldCloseACK(true); 161} 162 163RenderViewHostDelegateView* TestWebContents::GetDelegateView() { 164 if (delegate_view_override_) 165 return delegate_view_override_; 166 return WebContentsImpl::GetDelegateView(); 167} 168 169void TestWebContents::SetOpener(TestWebContents* opener) { 170 // This is normally only set in the WebContents constructor, which also 171 // registers an observer for when the opener gets closed. 172 opener_ = opener; 173 AddDestructionObserver(opener_); 174} 175 176void TestWebContents::AddPendingContents(TestWebContents* contents) { 177 // This is normally only done in WebContentsImpl::CreateNewWindow. 178 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents; 179 AddDestructionObserver(contents); 180} 181 182void TestWebContents::ExpectSetHistoryLengthAndPrune( 183 const SiteInstance* site_instance, 184 int history_length, 185 int32 min_page_id) { 186 expect_set_history_length_and_prune_ = true; 187 expect_set_history_length_and_prune_site_instance_ = 188 static_cast<const SiteInstanceImpl*>(site_instance); 189 expect_set_history_length_and_prune_history_length_ = history_length; 190 expect_set_history_length_and_prune_min_page_id_ = min_page_id; 191} 192 193void TestWebContents::SetHistoryLengthAndPrune( 194 const SiteInstance* site_instance, int history_length, 195 int32 min_page_id) { 196 EXPECT_TRUE(expect_set_history_length_and_prune_); 197 expect_set_history_length_and_prune_ = false; 198 EXPECT_EQ(expect_set_history_length_and_prune_site_instance_, site_instance); 199 EXPECT_EQ(expect_set_history_length_and_prune_history_length_, 200 history_length); 201 EXPECT_EQ(expect_set_history_length_and_prune_min_page_id_, min_page_id); 202} 203 204void TestWebContents::TestDidFinishLoad(int64 frame_id, 205 const GURL& url, 206 bool is_main_frame) { 207 ViewHostMsg_DidFinishLoad msg(0, frame_id, url, is_main_frame); 208 OnMessageReceived(render_manager_.current_host(), msg); 209} 210 211void TestWebContents::TestDidFailLoadWithError( 212 int64 frame_id, 213 const GURL& url, 214 bool is_main_frame, 215 int error_code, 216 const string16& error_description) { 217 ViewHostMsg_DidFailLoadWithError msg( 218 0, frame_id, url, is_main_frame, error_code, error_description); 219 OnMessageReceived(render_manager_.current_host(), msg); 220} 221 222void TestWebContents::CreateNewWindow( 223 int route_id, 224 int main_frame_route_id, 225 const ViewHostMsg_CreateWindow_Params& params, 226 SessionStorageNamespace* session_storage_namespace) { 227} 228 229void TestWebContents::CreateNewWidget(int route_id, 230 WebKit::WebPopupType popup_type) { 231} 232 233void TestWebContents::CreateNewFullscreenWidget(int route_id) { 234} 235 236void TestWebContents::ShowCreatedWindow(int route_id, 237 WindowOpenDisposition disposition, 238 const gfx::Rect& initial_pos, 239 bool user_gesture) { 240} 241 242void TestWebContents::ShowCreatedWidget(int route_id, 243 const gfx::Rect& initial_pos) { 244} 245 246void TestWebContents::ShowCreatedFullscreenWidget(int route_id) { 247} 248 249} // namespace content 250