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 UpdateMaxPageIDIfNecessary(render_view_host
);
168 // This will go to a TestRenderViewHost.
169 static_cast<RenderViewHostImpl
*>(render_view_host
)
170 ->CreateRenderView(opener_frame_routing_id
, proxy_routing_id
, -1,
171 replicated_frame_state
, false);
175 WebContents
* TestWebContents::Clone() {
176 WebContentsImpl
* contents
=
177 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
178 contents
->GetController().CopyStateFrom(controller_
);
182 void TestWebContents::NavigateAndCommit(const GURL
& url
) {
183 GetController().LoadURL(url
, Referrer(), ui::PAGE_TRANSITION_LINK
,
185 GURL
loaded_url(url
);
186 bool reverse_on_redirect
= false;
187 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
188 &loaded_url
, GetBrowserContext(), &reverse_on_redirect
);
189 // LoadURL created a navigation entry, now simulate the RenderView sending
190 // a notification that it actually navigated.
191 CommitPendingNavigation();
194 void TestWebContents::TestSetIsLoading(bool value
) {
195 SetIsLoading(value
, true, nullptr);
198 void TestWebContents::CommitPendingNavigation() {
199 const NavigationEntry
* entry
= GetController().GetPendingEntry();
202 // If we are doing a cross-site navigation, this simulates the current RFH
203 // notifying that it has unloaded so the pending RFH is resumed and can
205 // PlzNavigate: the pending RFH is not created before the navigation commit,
206 // so it is necessary to simulate the IO thread response here to commit in the
207 // proper renderer. It is necessary to call PrepareForCommit before getting
208 // the main and the pending frame because when we are trying to navigate to a
209 // webui from a new tab, a RenderFrameHost is created to display it that is
210 // committed immediately (since it is a new tab). Therefore the main frame is
211 // replaced without a pending frame being created, and we don't get the right
212 // values for the RFH to navigate: we try to use the old one that has been
213 // deleted in the meantime.
214 // Note that for some synchronous navigations (about:blank, javascript
215 // urls, etc.) there will be no NavigationRequest, and no simulation of the
216 // network stack is required.
217 bool browser_side_navigation
=
218 base::CommandLine::ForCurrentProcess()->HasSwitch(
219 switches::kEnableBrowserSideNavigation
);
220 if (!browser_side_navigation
||
221 GetMainFrame()->frame_tree_node()->navigation_request()) {
222 GetMainFrame()->PrepareForCommit();
225 TestRenderFrameHost
* old_rfh
= GetMainFrame();
226 TestRenderFrameHost
* rfh
= GetPendingMainFrame();
229 CHECK_IMPLIES(browser_side_navigation
, rfh
->is_loading());
230 CHECK_IMPLIES(browser_side_navigation
,
231 !rfh
->frame_tree_node()->navigation_request());
233 int page_id
= entry
->GetPageID();
235 // It's a new navigation, assign a never-seen page id to it.
236 page_id
= GetMaxPageIDForSiteInstance(rfh
->GetSiteInstance()) + 1;
239 rfh
->SendNavigate(page_id
, entry
->GetUniqueID(),
240 GetController().GetPendingEntryIndex() == -1,
242 // Simulate the SwapOut_ACK. This is needed when cross-site navigation
245 old_rfh
->OnSwappedOut();
248 void TestWebContents::ProceedWithCrossSiteNavigation() {
249 if (!GetPendingMainFrame())
251 GetMainFrame()->SendBeforeUnloadACK(true);
254 RenderViewHostDelegateView
* TestWebContents::GetDelegateView() {
255 if (delegate_view_override_
)
256 return delegate_view_override_
;
257 return WebContentsImpl::GetDelegateView();
260 void TestWebContents::SetOpener(TestWebContents
* opener
) {
261 frame_tree_
.root()->SetOpener(opener
->GetFrameTree()->root());
264 void TestWebContents::AddPendingContents(TestWebContents
* contents
) {
265 // This is normally only done in WebContentsImpl::CreateNewWindow.
266 pending_contents_
[contents
->GetRenderViewHost()->GetRoutingID()] = contents
;
267 AddDestructionObserver(contents
);
270 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset
,
271 int history_length
) {
272 expect_set_history_offset_and_length_
= true;
273 expect_set_history_offset_and_length_history_offset_
= history_offset
;
274 expect_set_history_offset_and_length_history_length_
= history_length
;
277 void TestWebContents::SetHistoryOffsetAndLength(int history_offset
,
278 int history_length
) {
279 EXPECT_TRUE(expect_set_history_offset_and_length_
);
280 expect_set_history_offset_and_length_
= false;
281 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_
,
283 EXPECT_EQ(expect_set_history_offset_and_length_history_length_
,
287 void TestWebContents::TestDidFinishLoad(const GURL
& url
) {
288 FrameHostMsg_DidFinishLoad
msg(0, url
);
289 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
292 void TestWebContents::TestDidFailLoadWithError(
295 const base::string16
& error_description
,
296 bool was_ignored_by_handler
) {
297 FrameHostMsg_DidFailLoadWithError
msg(
298 0, url
, error_code
, error_description
, was_ignored_by_handler
);
299 frame_tree_
.root()->current_frame_host()->OnMessageReceived(msg
);
302 void TestWebContents::CreateNewWindow(
303 SiteInstance
* source_site_instance
,
305 int main_frame_route_id
,
306 const ViewHostMsg_CreateWindow_Params
& params
,
307 SessionStorageNamespace
* session_storage_namespace
) {
310 void TestWebContents::CreateNewWidget(int32 render_process_id
,
313 blink::WebPopupType popup_type
) {}
315 void TestWebContents::CreateNewFullscreenWidget(int32 render_process_id
,
319 void TestWebContents::ShowCreatedWindow(int route_id
,
320 WindowOpenDisposition disposition
,
321 const gfx::Rect
& initial_rect
,
325 void TestWebContents::ShowCreatedWidget(int route_id
,
326 const gfx::Rect
& initial_rect
) {
329 void TestWebContents::ShowCreatedFullscreenWidget(int route_id
) {
332 void TestWebContents::SaveFrameWithHeaders(const GURL
& url
,
333 const Referrer
& referrer
,
334 const std::string
& headers
) {
335 save_frame_headers_
= headers
;
338 } // namespace content