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.
5 #include "content/test/test_web_contents.h"
9 #include "base/command_line.h"
10 #include "content/browser/browser_url_handler_impl.h"
11 #include "content/browser/frame_host/cross_process_frame_connector.h"
12 #include "content/browser/frame_host/navigation_entry_impl.h"
13 #include "content/browser/frame_host/navigator.h"
14 #include "content/browser/frame_host/navigator_impl.h"
15 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/site_instance_impl.h"
17 #include "content/common/frame_messages.h"
18 #include "content/common/view_messages.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/page_state.h"
24 #include "content/public/test/mock_render_process_host.h"
25 #include "content/test/test_render_view_host.h"
26 #include "ui/base/page_transition_types.h"
30 TestWebContents::TestWebContents(BrowserContext
* browser_context
)
31 : WebContentsImpl(browser_context
),
32 delegate_view_override_(NULL
),
33 expect_set_history_offset_and_length_(false),
34 expect_set_history_offset_and_length_history_length_(0) {
37 TestWebContents
* TestWebContents::Create(BrowserContext
* browser_context
,
38 SiteInstance
* instance
) {
39 TestWebContents
* test_web_contents
= new TestWebContents(browser_context
);
40 test_web_contents
->Init(WebContents::CreateParams(browser_context
, instance
));
41 return test_web_contents
;
44 TestWebContents::~TestWebContents() {
45 EXPECT_FALSE(expect_set_history_offset_and_length_
);
48 TestRenderFrameHost
* TestWebContents::GetMainFrame() {
49 return static_cast<TestRenderFrameHost
*>(WebContentsImpl::GetMainFrame());
52 TestRenderViewHost
* TestWebContents::GetRenderViewHost() const {
53 return static_cast<TestRenderViewHost
*>(
54 WebContentsImpl::GetRenderViewHost());
57 TestRenderFrameHost
* TestWebContents::GetPendingMainFrame() const {
58 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
59 switches::kEnableBrowserSideNavigation
)) {
60 return static_cast<TestRenderFrameHost
*>(
61 GetRenderManager()->speculative_render_frame_host_
.get());
63 return static_cast<TestRenderFrameHost
*>(
64 GetRenderManager()->pending_frame_host());
67 void TestWebContents::StartNavigation(const GURL
& url
) {
68 GetController().LoadURL(url
, Referrer(), ui::PAGE_TRANSITION_LINK
,
71 bool reverse_on_redirect
= false;
72 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
73 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
75 // This will simulate receiving the DidStartProvisionalLoad IPC from the
77 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
78 switches::kEnableBrowserSideNavigation
)) {
79 if (GetMainFrame()->is_waiting_for_beforeunload_ack())
80 GetMainFrame()->SendBeforeUnloadACK(true);
81 TestRenderFrameHost
* rfh
=
82 GetPendingMainFrame() ? GetPendingMainFrame() : GetMainFrame();
83 rfh
->SimulateNavigationStart(url
);
87 int TestWebContents::DownloadImage(const GURL
& url
,
89 uint32_t max_bitmap_size
,
91 const ImageDownloadCallback
& callback
) {
92 static int g_next_image_download_id
= 0;
93 return ++g_next_image_download_id
;
96 void TestWebContents::TestDidNavigate(RenderFrameHost
* render_frame_host
,
99 bool did_create_new_entry
,
101 ui::PageTransition transition
) {
102 TestDidNavigateWithReferrer(render_frame_host
,
105 did_create_new_entry
,
111 void TestWebContents::TestDidNavigateWithReferrer(
112 RenderFrameHost
* render_frame_host
,
115 bool did_create_new_entry
,
117 const Referrer
& referrer
,
118 ui::PageTransition transition
) {
119 TestRenderFrameHost
* rfh
=
120 static_cast<TestRenderFrameHost
*>(render_frame_host
);
121 rfh
->InitializeRenderFrameIfNeeded();
123 if (!rfh
->is_loading())
124 rfh
->SimulateNavigationStart(url
);
126 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
128 params
.page_id
= page_id
;
129 params
.nav_entry_id
= nav_entry_id
;
131 params
.referrer
= referrer
;
132 params
.transition
= transition
;
133 params
.redirects
= std::vector
<GURL
>();
134 params
.should_update_history
= false;
135 params
.searchable_form_url
= GURL();
136 params
.searchable_form_encoding
= std::string();
137 params
.did_create_new_entry
= did_create_new_entry
;
138 params
.security_info
= std::string();
139 params
.gesture
= NavigationGestureUser
;
140 params
.was_within_same_page
= false;
141 params
.is_post
= false;
142 params
.page_state
= PageState::CreateFromURL(url
);
144 rfh
->SendNavigateWithParams(¶ms
);
147 const std::string
& TestWebContents::GetSaveFrameHeaders() {
148 return save_frame_headers_
;
151 bool TestWebContents::CrossProcessNavigationPending() {
152 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kEnableBrowserSideNavigation
)) {
154 return GetRenderManager()->speculative_render_frame_host_
&&
155 static_cast<TestRenderFrameHost
*>(
156 GetRenderManager()->speculative_render_frame_host_
.get())
159 return GetRenderManager()->pending_frame_host() != nullptr;
162 bool TestWebContents::CreateRenderViewForRenderManager(
163 RenderViewHost
* render_view_host
,
164 int opener_frame_routing_id
,
165 int proxy_routing_id
,
166 const FrameReplicationState
& replicated_frame_state
,
167 bool for_main_frame
) {
168 UpdateMaxPageIDIfNecessary(render_view_host
);
169 // This will go to a TestRenderViewHost.
170 static_cast<RenderViewHostImpl
*>(render_view_host
)
171 ->CreateRenderView(opener_frame_routing_id
, proxy_routing_id
, -1,
172 replicated_frame_state
, false);
176 WebContents
* TestWebContents::Clone() {
177 WebContentsImpl
* contents
=
178 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
179 contents
->GetController().CopyStateFrom(controller_
);
183 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
184 GetController().LoadURL(url
, Referrer(), ui::PAGE_TRANSITION_LINK
,
186 GURL
loaded_url(url
);
187 bool reverse_on_redirect
= false;
188 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
189 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
190 // LoadURL created a navigation entry, now simulate the RenderView sending
191 // a notification that it actually navigated.
192 CommitPendingNavigation();
195 void TestWebContents::TestSetIsLoading(bool value
) {
196 SetIsLoading(value
, true, nullptr);
199 void TestWebContents::CommitPendingNavigation() {
200 const NavigationEntry
* entry
= GetController().GetPendingEntry();
203 // If we are doing a cross-site navigation, this simulates the current RFH
204 // notifying that it has unloaded so the pending RFH is resumed and can
206 // PlzNavigate: the pending RFH is not created before the navigation commit,
207 // so it is necessary to simulate the IO thread response here to commit in the
208 // proper renderer. It is necessary to call PrepareForCommit before getting
209 // the main and the pending frame because when we are trying to navigate to a
210 // webui from a new tab, a RenderFrameHost is created to display it that is
211 // committed immediately (since it is a new tab). Therefore the main frame is
212 // replaced without a pending frame being created, and we don't get the right
213 // values for the RFH to navigate: we try to use the old one that has been
214 // deleted in the meantime.
215 // Note that for some synchronous navigations (about:blank, javascript
216 // urls, etc.) there will be no NavigationRequest, and no simulation of the
217 // network stack is required.
218 bool browser_side_navigation
=
219 base::CommandLine::ForCurrentProcess()->HasSwitch(
220 switches::kEnableBrowserSideNavigation
);
221 if (!browser_side_navigation
||
222 GetMainFrame()->frame_tree_node()->navigation_request()) {
223 GetMainFrame()->PrepareForCommit();
226 TestRenderFrameHost
* old_rfh
= GetMainFrame();
227 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
230 CHECK_IMPLIES(browser_side_navigation
, rfh
->is_loading());
231 CHECK_IMPLIES(browser_side_navigation
,
232 !rfh
->frame_tree_node()->navigation_request());
234 int page_id
= entry
->GetPageID();
236 // It's a new navigation, assign a never-seen page id to it.
237 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
240 rfh
->SendNavigate(page_id
, entry
->GetUniqueID(),
241 GetController().GetPendingEntryIndex() == -1,
243 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
246 old_rfh
->OnSwappedOut();
249 void TestWebContents::ProceedWithCrossSiteNavigation() {
250 if (!GetPendingMainFrame())
252 GetMainFrame()->SendBeforeUnloadACK(true);
255 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
256 if (delegate_view_override_
)
257 return delegate_view_override_
;
258 return WebContentsImpl::GetDelegateView();
261 void TestWebContents::SetOpener(TestWebContents
* opener
) {
262 frame_tree_
.root()->SetOpener(opener
->GetFrameTree()->root());
265 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
266 // This is normally only done in WebContentsImpl::CreateNewWindow.
267 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
268 AddDestructionObserver(contents
);
271 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
272 int history_length
) {
273 expect_set_history_offset_and_length_
= true;
274 expect_set_history_offset_and_length_history_offset_
= history_offset
;
275 expect_set_history_offset_and_length_history_length_
= history_length
;
278 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
279 int history_length
) {
280 EXPECT_TRUE(expect_set_history_offset_and_length_
);
281 expect_set_history_offset_and_length_
= false;
282 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
284 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
288 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
289 FrameHostMsg_DidFinishLoad
msg(0, url
);
290 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
293 void TestWebContents::TestDidFailLoadWithError(
296 const base::string16
& error_description
,
297 bool was_ignored_by_handler
) {
298 FrameHostMsg_DidFailLoadWithError
msg(
299 0, url
, error_code
, error_description
, was_ignored_by_handler
);
300 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
303 void TestWebContents::CreateNewWindow(
304 SiteInstance
* source_site_instance
,
306 int main_frame_route_id
,
307 const ViewHostMsg_CreateWindow_Params
& params
,
308 SessionStorageNamespace
* session_storage_namespace
) {
311 void TestWebContents::CreateNewWidget(int32 render_process_id
,
314 blink::WebPopupType popup_type
) {}
316 void TestWebContents::CreateNewFullscreenWidget(int32 render_process_id
,
320 void TestWebContents::ShowCreatedWindow(int route_id
,
321 WindowOpenDisposition disposition
,
322 const gfx::Rect
& initial_rect
,
326 void TestWebContents::ShowCreatedWidget(int route_id
,
327 const gfx::Rect
& initial_rect
) {
330 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
333 void TestWebContents::SaveFrameWithHeaders(const GURL
& url
,
334 const Referrer
& referrer
,
335 const std::string
& headers
) {
336 save_frame_headers_
= headers
;
339 } // namespace content