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 "content/browser/frame_host/frame_tree.h"
8 #include "content/browser/frame_host/render_frame_host_delegate.h"
9 #include "content/common/frame_messages.h"
10 #include "content/public/common/page_transition_types.h"
11 #include "content/test/test_render_view_host.h"
12 #include "net/base/load_flags.h"
13 #include "third_party/WebKit/public/web/WebPageVisibilityState.h"
17 TestRenderFrameHostCreationObserver::TestRenderFrameHostCreationObserver(
18 WebContents
* web_contents
)
19 : WebContentsObserver(web_contents
), last_created_frame_(NULL
) {
22 TestRenderFrameHostCreationObserver::~TestRenderFrameHostCreationObserver() {
25 void TestRenderFrameHostCreationObserver::RenderFrameCreated(
26 RenderFrameHost
* render_frame_host
) {
27 last_created_frame_
= render_frame_host
;
30 TestRenderFrameHost::TestRenderFrameHost(RenderViewHostImpl
* render_view_host
,
31 RenderFrameHostDelegate
* delegate
,
32 FrameTree
* frame_tree
,
33 FrameTreeNode
* frame_tree_node
,
36 : RenderFrameHostImpl(render_view_host
,
42 child_creation_observer_(delegate
? delegate
->GetAsWebContents() : NULL
),
43 contents_mime_type_("text/html"),
44 simulate_history_list_was_cleared_(false) {
45 // Allow TestRenderViewHosts to easily access their main frame RFH.
46 if (frame_tree_node
== frame_tree
->root()) {
47 static_cast<TestRenderViewHost
*>(render_view_host
)->
48 set_main_render_frame_host(this);
52 TestRenderFrameHost::~TestRenderFrameHost() {}
54 TestRenderViewHost
* TestRenderFrameHost::GetRenderViewHost() {
55 return static_cast<TestRenderViewHost
*>(
56 RenderFrameHostImpl::GetRenderViewHost());
59 TestRenderFrameHost
* TestRenderFrameHost::AppendChild(
60 const std::string
& frame_name
) {
61 OnCreateChildFrame(GetProcess()->GetNextRoutingID(), frame_name
);
62 return static_cast<TestRenderFrameHost
*>(
63 child_creation_observer_
.last_created_frame());
66 void TestRenderFrameHost::SendNavigateWithTransition(
69 PageTransition transition
) {
70 SendNavigateWithTransitionAndResponseCode(page_id
, url
, transition
, 200);
73 void TestRenderFrameHost::SendNavigate(int page_id
, const GURL
& url
) {
74 SendNavigateWithTransition(page_id
, url
, PAGE_TRANSITION_LINK
);
77 void TestRenderFrameHost::SendFailedNavigate(int page_id
, const GURL
& url
) {
78 SendNavigateWithTransitionAndResponseCode(
79 page_id
, url
, PAGE_TRANSITION_RELOAD
, 500);
82 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode(
84 const GURL
& url
, PageTransition transition
,
86 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
87 // so we keep a copy of it to use in SendNavigateWithParameters.
89 OnDidStartProvisionalLoadForFrame(url_copy
, false);
90 SendNavigateWithParameters(page_id
, url_copy
, transition
, url_copy
,
91 response_code
, 0, std::vector
<GURL
>());
94 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL(
97 const GURL
& original_request_url
) {
98 OnDidStartProvisionalLoadForFrame(url
, false);
99 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
,
100 original_request_url
, 200, 0, std::vector
<GURL
>());
103 void TestRenderFrameHost::SendNavigateWithFile(
106 const base::FilePath
& file_path
) {
107 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
, url
, 200,
108 &file_path
, std::vector
<GURL
>());
111 void TestRenderFrameHost::SendNavigateWithParams(
112 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
) {
113 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), *params
);
114 OnDidCommitProvisionalLoad(msg
);
117 void TestRenderFrameHost::SendNavigateWithRedirects(
120 const std::vector
<GURL
>& redirects
) {
121 SendNavigateWithParameters(
122 page_id
, url
, PAGE_TRANSITION_LINK
, url
, 200, 0, redirects
);
125 void TestRenderFrameHost::SendNavigateWithParameters(
128 PageTransition transition
,
129 const GURL
& original_request_url
,
131 const base::FilePath
* file_path_for_history_item
,
132 const std::vector
<GURL
>& redirects
) {
133 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
134 params
.page_id
= page_id
;
136 params
.referrer
= Referrer();
137 params
.transition
= transition
;
138 params
.redirects
= redirects
;
139 params
.should_update_history
= true;
140 params
.searchable_form_url
= GURL();
141 params
.searchable_form_encoding
= std::string();
142 params
.security_info
= std::string();
143 params
.gesture
= NavigationGestureUser
;
144 params
.contents_mime_type
= contents_mime_type_
;
145 params
.is_post
= false;
146 params
.http_status_code
= response_code
;
147 params
.socket_address
.set_host("2001:db8::1");
148 params
.socket_address
.set_port(80);
149 params
.history_list_was_cleared
= simulate_history_list_was_cleared_
;
150 params
.original_request_url
= original_request_url
;
152 url::Replacements
<char> replacements
;
153 replacements
.ClearRef();
154 params
.was_within_same_page
= transition
!= PAGE_TRANSITION_RELOAD
&&
155 transition
!= PAGE_TRANSITION_TYPED
&&
156 url
.ReplaceComponents(replacements
) ==
157 GetLastCommittedURL().ReplaceComponents(replacements
);
159 params
.page_state
= PageState::CreateForTesting(
162 file_path_for_history_item
? "data" : NULL
,
163 file_path_for_history_item
);
165 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), params
);
166 OnDidCommitProvisionalLoad(msg
);
169 void TestRenderFrameHost::SendBeginNavigationWithURL(const GURL
& url
) {
170 FrameHostMsg_BeginNavigation_Params params
;
171 params
.method
= "GET";
173 params
.referrer
= Referrer(GURL(), blink::WebReferrerPolicyDefault
);
174 params
.load_flags
= net::LOAD_NORMAL
;
175 params
.has_user_gesture
= false;
176 params
.transition_type
= PAGE_TRANSITION_LINK
;
177 params
.should_replace_current_entry
= false;
178 params
.allow_download
= true;
179 OnBeginNavigation(params
);
182 void TestRenderFrameHost::DidDisownOpener() {
186 } // namespace content