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::SendBeginNavigationWithURL(const GURL
& url
,
204 bool has_user_gesture
) {
205 BeginNavigationParams
begin_params("GET", std::string(), net::LOAD_NORMAL
,
207 CommonNavigationParams common_params
;
208 common_params
.url
= url
;
209 common_params
.referrer
= Referrer(GURL(), blink::WebReferrerPolicyDefault
);
210 common_params
.transition
= ui::PAGE_TRANSITION_LINK
;
211 OnBeginNavigation(common_params
, begin_params
,
212 scoped_refptr
<ResourceRequestBody
>());
215 void TestRenderFrameHost::DidDisownOpener() {
219 void TestRenderFrameHost::PrepareForCommit(const GURL
& url
) {
220 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
221 switches::kEnableBrowserSideNavigation
)) {
222 SendBeforeUnloadACK(true);
227 // Simulate the network stack commit without any redirects.
228 NavigationRequest
* request
=
229 static_cast<NavigatorImpl
*>(frame_tree_node_
->navigator())
230 ->GetNavigationRequestForNodeForTesting(frame_tree_node_
);
232 // We are simulating a renderer-initiated user-initiated navigation.
234 SendBeginNavigationWithURL(url
, true);
235 request
= static_cast<NavigatorImpl
*>(frame_tree_node_
->navigator())
236 ->GetNavigationRequestForNodeForTesting(frame_tree_node_
);
238 ASSERT_TRUE(request
);
240 // We may not have simulated the renderer response to the navigation request.
242 if (request
->state() == NavigationRequest::WAITING_FOR_RENDERER_RESPONSE
)
243 SendBeforeUnloadACK(true);
245 // We have already simulated the IO thread commit. Only the
246 // DidCommitProvisionalLoad from the renderer is missing.
247 if (request
->state() == NavigationRequest::RESPONSE_STARTED
)
250 ASSERT_TRUE(request
->state() == NavigationRequest::STARTED
);
251 TestNavigationURLLoader
* url_loader
=
252 static_cast<TestNavigationURLLoader
*>(request
->loader_for_testing());
253 ASSERT_TRUE(url_loader
);
254 scoped_refptr
<ResourceResponse
> response(new ResourceResponse
);
255 url_loader
->CallOnResponseStarted(response
, MakeEmptyStream());
258 void TestRenderFrameHost::SendBeforeUnloadHandlersPresent(bool present
) {
259 OnBeforeUnloadHandlersPresent(present
);
262 void TestRenderFrameHost::SendUnloadHandlersPresent(bool present
) {
263 OnUnloadHandlersPresent(present
);
266 } // namespace content