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