1 // Copyright 2013 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_render_frame_host.h"
7 #include "base/command_line.h"
8 #include "content/browser/frame_host/frame_tree.h"
9 #include "content/browser/frame_host/navigation_request.h"
10 #include "content/browser/frame_host/navigator.h"
11 #include "content/browser/frame_host/navigator_impl.h"
12 #include "content/browser/frame_host/render_frame_host_delegate.h"
13 #include "content/common/frame_messages.h"
14 #include "content/public/browser/stream_handle.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/test/browser_side_navigation_test_utils.h"
17 #include "content/test/test_navigation_url_loader.h"
18 #include "content/test/test_render_view_host.h"
19 #include "net/base/load_flags.h"
20 #include "third_party/WebKit/public/platform/WebPageVisibilityState.h"
21 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
22 #include "third_party/WebKit/public/web/WebTreeScopeType.h"
23 #include "ui/base/page_transition_types.h"
27 TestRenderFrameHostCreationObserver::TestRenderFrameHostCreationObserver(
28 WebContents
* web_contents
)
29 : WebContentsObserver(web_contents
), last_created_frame_(NULL
) {
32 TestRenderFrameHostCreationObserver::~TestRenderFrameHostCreationObserver() {
35 void TestRenderFrameHostCreationObserver::RenderFrameCreated(
36 RenderFrameHost
* render_frame_host
) {
37 last_created_frame_
= render_frame_host
;
40 TestRenderFrameHost::TestRenderFrameHost(SiteInstance
* site_instance
,
41 RenderViewHostImpl
* render_view_host
,
42 RenderFrameHostDelegate
* delegate
,
43 RenderWidgetHostDelegate
* rwh_delegate
,
44 FrameTree
* frame_tree
,
45 FrameTreeNode
* frame_tree_node
,
48 : RenderFrameHostImpl(site_instance
,
56 child_creation_observer_(delegate
? delegate
->GetAsWebContents() : NULL
),
57 contents_mime_type_("text/html"),
58 simulate_history_list_was_cleared_(false) {
61 TestRenderFrameHost::~TestRenderFrameHost() {
64 TestRenderViewHost
* TestRenderFrameHost::GetRenderViewHost() {
65 return static_cast<TestRenderViewHost
*>(
66 RenderFrameHostImpl::GetRenderViewHost());
69 MockRenderProcessHost
* TestRenderFrameHost::GetProcess() {
70 return static_cast<MockRenderProcessHost
*>(RenderFrameHostImpl::GetProcess());
73 void TestRenderFrameHost::InitializeRenderFrameIfNeeded() {
74 if (!render_view_host()->IsRenderViewLive()) {
75 RenderViewHostTester::For(render_view_host())->CreateTestRenderView(
76 base::string16(), MSG_ROUTING_NONE
, MSG_ROUTING_NONE
, -1, false);
80 TestRenderFrameHost
* TestRenderFrameHost::AppendChild(
81 const std::string
& frame_name
) {
82 OnCreateChildFrame(GetProcess()->GetNextRoutingID(),
83 blink::WebTreeScopeType::Document
, frame_name
,
84 blink::WebSandboxFlags::None
);
85 return static_cast<TestRenderFrameHost
*>(
86 child_creation_observer_
.last_created_frame());
89 void TestRenderFrameHost::SetContentsMimeType(const std::string
& mime_type
) {
90 contents_mime_type_
= mime_type
;
93 void TestRenderFrameHost::SendBeforeUnloadACK(bool proceed
) {
94 base::TimeTicks now
= base::TimeTicks::Now();
95 OnBeforeUnloadACK(proceed
, now
, now
);
98 void TestRenderFrameHost::SimulateSwapOutACK() {
102 void TestRenderFrameHost::SendNavigate(int page_id
,
104 bool did_create_new_entry
,
106 SendNavigateWithTransition(page_id
, nav_entry_id
, did_create_new_entry
, url
,
107 ui::PAGE_TRANSITION_LINK
);
110 void TestRenderFrameHost::SendFailedNavigate(int page_id
,
112 bool did_create_new_entry
,
114 SendNavigateWithTransitionAndResponseCode(page_id
, nav_entry_id
,
115 did_create_new_entry
, url
,
116 ui::PAGE_TRANSITION_RELOAD
, 500);
119 void TestRenderFrameHost::SendNavigateWithTransition(
122 bool did_create_new_entry
,
124 ui::PageTransition transition
) {
125 SendNavigateWithTransitionAndResponseCode(
126 page_id
, nav_entry_id
, did_create_new_entry
, url
, transition
, 200);
129 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode(
132 bool did_create_new_entry
,
134 ui::PageTransition transition
,
136 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
137 // so we keep a copy of it to use in SendNavigateWithParameters.
139 OnDidStartProvisionalLoadForFrame(url_copy
);
140 SendNavigateWithParameters(page_id
, nav_entry_id
, did_create_new_entry
,
141 url_copy
, transition
, url_copy
, response_code
, 0,
142 std::vector
<GURL
>());
145 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL(
148 bool did_create_new_entry
,
150 const GURL
& original_request_url
) {
151 OnDidStartProvisionalLoadForFrame(url
);
152 SendNavigateWithParameters(page_id
, nav_entry_id
, did_create_new_entry
, url
,
153 ui::PAGE_TRANSITION_LINK
, original_request_url
,
154 200, 0, std::vector
<GURL
>());
157 void TestRenderFrameHost::SendNavigateWithFile(
160 bool did_create_new_entry
,
162 const base::FilePath
& file_path
) {
163 SendNavigateWithParameters(page_id
, nav_entry_id
, did_create_new_entry
, url
,
164 ui::PAGE_TRANSITION_LINK
, url
, 200, &file_path
,
165 std::vector
<GURL
>());
168 void TestRenderFrameHost::SendNavigateWithParams(
169 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
) {
170 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), *params
);
171 OnDidCommitProvisionalLoad(msg
);
174 void TestRenderFrameHost::SendNavigateWithRedirects(
177 bool did_create_new_entry
,
179 const std::vector
<GURL
>& redirects
) {
180 SendNavigateWithParameters(page_id
, nav_entry_id
, did_create_new_entry
, url
,
181 ui::PAGE_TRANSITION_LINK
, url
, 200, 0, redirects
);
184 void TestRenderFrameHost::SendNavigateWithParameters(
187 bool did_create_new_entry
,
189 ui::PageTransition transition
,
190 const GURL
& original_request_url
,
192 const base::FilePath
* file_path_for_history_item
,
193 const std::vector
<GURL
>& redirects
) {
194 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
195 params
.page_id
= page_id
;
196 params
.nav_entry_id
= nav_entry_id
;
198 params
.referrer
= Referrer();
199 params
.transition
= transition
;
200 params
.redirects
= redirects
;
201 params
.should_update_history
= true;
202 params
.searchable_form_url
= GURL();
203 params
.searchable_form_encoding
= std::string();
204 params
.did_create_new_entry
= did_create_new_entry
;
205 params
.security_info
= std::string();
206 params
.gesture
= NavigationGestureUser
;
207 params
.contents_mime_type
= contents_mime_type_
;
208 params
.is_post
= false;
209 params
.http_status_code
= response_code
;
210 params
.socket_address
.set_host("2001:db8::1");
211 params
.socket_address
.set_port(80);
212 params
.history_list_was_cleared
= simulate_history_list_was_cleared_
;
213 params
.original_request_url
= original_request_url
;
215 url::Replacements
<char> replacements
;
216 replacements
.ClearRef();
217 params
.was_within_same_page
= transition
!= ui::PAGE_TRANSITION_RELOAD
&&
218 transition
!= ui::PAGE_TRANSITION_TYPED
&&
219 url
.ReplaceComponents(replacements
) ==
220 GetLastCommittedURL().ReplaceComponents(replacements
);
222 params
.page_state
= PageState::CreateForTesting(
225 file_path_for_history_item
? "data" : NULL
,
226 file_path_for_history_item
);
228 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), params
);
229 OnDidCommitProvisionalLoad(msg
);
232 void TestRenderFrameHost::NavigateAndCommitRendererInitiated(
234 bool did_create_new_entry
,
236 SendRendererInitiatedNavigationRequest(url
, false);
238 SendNavigate(page_id
, 0, did_create_new_entry
, url
);
241 void TestRenderFrameHost::SendRendererInitiatedNavigationRequest(
243 bool has_user_gesture
) {
244 // Since this is renderer-initiated navigation, the RenderFrame must be
245 // initialized. Do it if it hasn't happened yet.
246 InitializeRenderFrameIfNeeded();
248 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
249 switches::kEnableBrowserSideNavigation
)) {
250 BeginNavigationParams
begin_params("GET", std::string(), net::LOAD_NORMAL
,
252 CommonNavigationParams common_params
;
253 common_params
.url
= url
;
254 common_params
.referrer
= Referrer(GURL(), blink::WebReferrerPolicyDefault
);
255 common_params
.transition
= ui::PAGE_TRANSITION_LINK
;
256 OnBeginNavigation(common_params
, begin_params
,
257 scoped_refptr
<ResourceRequestBody
>());
261 void TestRenderFrameHost::DidDisownOpener() {
265 void TestRenderFrameHost::PrepareForCommit() {
266 PrepareForCommitWithServerRedirect(GURL());
269 void TestRenderFrameHost::PrepareForCommitWithServerRedirect(
270 const GURL
& redirect_url
) {
271 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
272 switches::kEnableBrowserSideNavigation
)) {
274 if (IsWaitingForBeforeUnloadACK())
275 SendBeforeUnloadACK(true);
280 NavigationRequest
* request
= frame_tree_node_
->navigation_request();
283 // Simulate a beforeUnload ACK from the renderer if the browser is waiting for
284 // it. If it runs it will update the request state.
285 if (request
->state() == NavigationRequest::WAITING_FOR_RENDERER_RESPONSE
)
286 SendBeforeUnloadACK(true);
288 // If a network request is not needed for this URL, just check the request is
289 // in the correct state and return.
290 if (!request
->ShouldMakeNetworkRequest(request
->common_params().url
)) {
291 CHECK(request
->state() == NavigationRequest::RESPONSE_STARTED
);
295 CHECK(request
->state() == NavigationRequest::STARTED
);
297 TestNavigationURLLoader
* url_loader
=
298 static_cast<TestNavigationURLLoader
*>(request
->loader_for_testing());
301 // If a non-empty |redirect_url| was provided, simulate a server redirect.
302 if (!redirect_url
.is_empty())
303 url_loader
->SimulateServerRedirect(redirect_url
);
305 // Simulate the network stack commit.
306 scoped_refptr
<ResourceResponse
> response(new ResourceResponse
);
307 // TODO(carlosk): ideally with PlzNavigate it should be possible someday to
308 // fully commit the navigation at this call to CallOnResponseStarted.
309 url_loader
->CallOnResponseStarted(response
, MakeEmptyStream());
312 } // namespace content