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 "ui/base/page_transition_types.h"
25 TestRenderFrameHostCreationObserver::TestRenderFrameHostCreationObserver(
26 WebContents
* web_contents
)
27 : WebContentsObserver(web_contents
), last_created_frame_(NULL
) {
30 TestRenderFrameHostCreationObserver::~TestRenderFrameHostCreationObserver() {
33 void TestRenderFrameHostCreationObserver::RenderFrameCreated(
34 RenderFrameHost
* render_frame_host
) {
35 last_created_frame_
= render_frame_host
;
38 TestRenderFrameHost::TestRenderFrameHost(SiteInstance
* site_instance
,
39 RenderViewHostImpl
* render_view_host
,
40 RenderFrameHostDelegate
* delegate
,
41 RenderWidgetHostDelegate
* rwh_delegate
,
42 FrameTree
* frame_tree
,
43 FrameTreeNode
* frame_tree_node
,
46 : RenderFrameHostImpl(site_instance
,
54 child_creation_observer_(delegate
? delegate
->GetAsWebContents() : NULL
),
55 contents_mime_type_("text/html"),
56 simulate_history_list_was_cleared_(false) {
57 if (frame_tree_node_
->IsMainFrame())
58 SetRenderFrameCreated(true);
61 TestRenderFrameHost::~TestRenderFrameHost() {
62 SetRenderFrameCreated(false);
65 TestRenderViewHost
* TestRenderFrameHost::GetRenderViewHost() {
66 return static_cast<TestRenderViewHost
*>(
67 RenderFrameHostImpl::GetRenderViewHost());
70 TestRenderFrameHost
* TestRenderFrameHost::AppendChild(
71 const std::string
& frame_name
) {
72 OnCreateChildFrame(GetProcess()->GetNextRoutingID(), frame_name
,
74 return static_cast<TestRenderFrameHost
*>(
75 child_creation_observer_
.last_created_frame());
78 void TestRenderFrameHost::SendNavigateWithTransition(
81 ui::PageTransition transition
) {
82 SendNavigateWithTransitionAndResponseCode(page_id
, url
, transition
, 200);
85 void TestRenderFrameHost::SetContentsMimeType(const std::string
& mime_type
) {
86 contents_mime_type_
= mime_type
;
89 void TestRenderFrameHost::SendBeforeUnloadACK(bool proceed
) {
90 base::TimeTicks now
= base::TimeTicks::Now();
91 OnBeforeUnloadACK(proceed
, now
, now
);
94 void TestRenderFrameHost::SimulateSwapOutACK() {
98 void TestRenderFrameHost::SendNavigate(int page_id
, const GURL
& url
) {
99 SendNavigateWithTransition(page_id
, url
, ui::PAGE_TRANSITION_LINK
);
102 void TestRenderFrameHost::SendFailedNavigate(int page_id
, const GURL
& url
) {
103 SendNavigateWithTransitionAndResponseCode(
104 page_id
, url
, ui::PAGE_TRANSITION_RELOAD
, 500);
107 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode(
109 const GURL
& url
, ui::PageTransition transition
,
111 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
112 // so we keep a copy of it to use in SendNavigateWithParameters.
115 // Ensure that the RenderFrameCreated notification has been sent to observers
116 // before navigating the frame.
117 SetRenderFrameCreated(true);
119 OnDidStartProvisionalLoadForFrame(url_copy
, false);
120 SendNavigateWithParameters(page_id
, url_copy
, transition
, url_copy
,
121 response_code
, 0, std::vector
<GURL
>());
124 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL(
127 const GURL
& original_request_url
) {
128 // Ensure that the RenderFrameCreated notification has been sent to observers
129 // before navigating the frame.
130 SetRenderFrameCreated(true);
132 OnDidStartProvisionalLoadForFrame(url
, false);
133 SendNavigateWithParameters(page_id
, url
, ui::PAGE_TRANSITION_LINK
,
134 original_request_url
, 200, 0, std::vector
<GURL
>());
137 void TestRenderFrameHost::SendNavigateWithFile(
140 const base::FilePath
& file_path
) {
141 SendNavigateWithParameters(page_id
, url
, ui::PAGE_TRANSITION_LINK
, url
, 200,
142 &file_path
, std::vector
<GURL
>());
145 void TestRenderFrameHost::SendNavigateWithParams(
146 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
) {
147 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), *params
);
148 OnDidCommitProvisionalLoad(msg
);
151 void TestRenderFrameHost::SendNavigateWithRedirects(
154 const std::vector
<GURL
>& redirects
) {
155 SendNavigateWithParameters(
156 page_id
, url
, ui::PAGE_TRANSITION_LINK
, url
, 200, 0, redirects
);
159 void TestRenderFrameHost::SendNavigateWithParameters(
162 ui::PageTransition transition
,
163 const GURL
& original_request_url
,
165 const base::FilePath
* file_path_for_history_item
,
166 const std::vector
<GURL
>& redirects
) {
167 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
168 params
.page_id
= page_id
;
170 params
.referrer
= Referrer();
171 params
.transition
= transition
;
172 params
.redirects
= redirects
;
173 params
.should_update_history
= true;
174 params
.searchable_form_url
= GURL();
175 params
.searchable_form_encoding
= std::string();
176 params
.security_info
= std::string();
177 params
.gesture
= NavigationGestureUser
;
178 params
.contents_mime_type
= contents_mime_type_
;
179 params
.is_post
= false;
180 params
.http_status_code
= response_code
;
181 params
.socket_address
.set_host("2001:db8::1");
182 params
.socket_address
.set_port(80);
183 params
.history_list_was_cleared
= simulate_history_list_was_cleared_
;
184 params
.original_request_url
= original_request_url
;
186 url::Replacements
<char> replacements
;
187 replacements
.ClearRef();
188 params
.was_within_same_page
= transition
!= ui::PAGE_TRANSITION_RELOAD
&&
189 transition
!= ui::PAGE_TRANSITION_TYPED
&&
190 url
.ReplaceComponents(replacements
) ==
191 GetLastCommittedURL().ReplaceComponents(replacements
);
193 params
.page_state
= PageState::CreateForTesting(
196 file_path_for_history_item
? "data" : NULL
,
197 file_path_for_history_item
);
199 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), params
);
200 OnDidCommitProvisionalLoad(msg
);
203 void TestRenderFrameHost::SendRendererInitiatedNavigationRequest(
205 bool has_user_gesture
) {
206 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
207 switches::kEnableBrowserSideNavigation
)) {
208 BeginNavigationParams
begin_params("GET", std::string(), net::LOAD_NORMAL
,
210 CommonNavigationParams common_params
;
211 common_params
.url
= url
;
212 common_params
.referrer
= Referrer(GURL(), blink::WebReferrerPolicyDefault
);
213 common_params
.transition
= ui::PAGE_TRANSITION_LINK
;
214 OnBeginNavigation(common_params
, begin_params
,
215 scoped_refptr
<ResourceRequestBody
>());
219 void TestRenderFrameHost::DidDisownOpener() {
223 void TestRenderFrameHost::PrepareForCommit() {
224 PrepareForCommitWithServerRedirect(GURL());
227 void TestRenderFrameHost::PrepareForCommitWithServerRedirect(
228 const GURL
& redirect_url
) {
229 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
230 switches::kEnableBrowserSideNavigation
)) {
232 SendBeforeUnloadACK(true);
237 NavigationRequest
* request
=
238 static_cast<NavigatorImpl
*>(frame_tree_node_
->navigator())
239 ->GetNavigationRequestForNodeForTesting(frame_tree_node_
);
242 // Simulate a beforeUnload ACK from the renderer if the browser is waiting for
243 // it. If it runs it will update the request state.
244 if (request
->state() == NavigationRequest::WAITING_FOR_RENDERER_RESPONSE
)
245 SendBeforeUnloadACK(true);
247 // If a network request is not needed for this URL, just check the request is
248 // in the correct state and return.
249 if (!request
->ShouldMakeNetworkRequest(request
->common_params().url
)) {
250 CHECK(request
->state() == NavigationRequest::RESPONSE_STARTED
);
254 CHECK(request
->state() == NavigationRequest::STARTED
);
256 TestNavigationURLLoader
* url_loader
=
257 static_cast<TestNavigationURLLoader
*>(request
->loader_for_testing());
260 // If a non-empty |redirect_url| was provided, simulate a server redirect.
261 if (!redirect_url
.is_empty())
262 url_loader
->SimulateServerRedirect(redirect_url
);
264 // Simulate the network stack commit.
265 scoped_refptr
<ResourceResponse
> response(new ResourceResponse
);
266 // TODO(carlosk): ideally with PlzNavigate it should be possible someday to
267 // fully commit the navigation at this call to CallOnResponseStarted.
268 url_loader
->CallOnResponseStarted(response
, MakeEmptyStream());
271 } // namespace content