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 RenderFrameHost
* TestRenderFrameHost::AppendChild(
55 const std::string
& frame_name
) {
56 OnCreateChildFrame(GetProcess()->GetNextRoutingID(), frame_name
);
57 return child_creation_observer_
.last_created_frame();
60 void TestRenderFrameHost::SendNavigateWithTransition(
63 PageTransition transition
) {
64 SendNavigateWithTransitionAndResponseCode(page_id
, url
, transition
, 200);
67 void TestRenderFrameHost::SendNavigate(int page_id
, const GURL
& url
) {
68 SendNavigateWithTransition(page_id
, url
, PAGE_TRANSITION_LINK
);
71 void TestRenderFrameHost::SendFailedNavigate(int page_id
, const GURL
& url
) {
72 SendNavigateWithTransitionAndResponseCode(
73 page_id
, url
, PAGE_TRANSITION_RELOAD
, 500);
76 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode(
78 const GURL
& url
, PageTransition transition
,
80 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
81 // so we keep a copy of it to use in SendNavigateWithParameters.
83 OnDidStartProvisionalLoadForFrame(url_copy
);
84 SendNavigateWithParameters(page_id
, url_copy
, transition
, url_copy
,
85 response_code
, 0, std::vector
<GURL
>());
88 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL(
91 const GURL
& original_request_url
) {
92 OnDidStartProvisionalLoadForFrame(url
);
93 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
,
94 original_request_url
, 200, 0, std::vector
<GURL
>());
97 void TestRenderFrameHost::SendNavigateWithFile(
100 const base::FilePath
& file_path
) {
101 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
, url
, 200,
102 &file_path
, std::vector
<GURL
>());
105 void TestRenderFrameHost::SendNavigateWithParams(
106 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
) {
107 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), *params
);
111 void TestRenderFrameHost::SendNavigateWithRedirects(
114 const std::vector
<GURL
>& redirects
) {
115 SendNavigateWithParameters(
116 page_id
, url
, PAGE_TRANSITION_LINK
, url
, 200, 0, redirects
);
119 void TestRenderFrameHost::SendNavigateWithParameters(
122 PageTransition transition
,
123 const GURL
& original_request_url
,
125 const base::FilePath
* file_path_for_history_item
,
126 const std::vector
<GURL
>& redirects
) {
127 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
128 params
.page_id
= page_id
;
130 params
.referrer
= Referrer();
131 params
.transition
= transition
;
132 params
.redirects
= redirects
;
133 params
.should_update_history
= true;
134 params
.searchable_form_url
= GURL();
135 params
.searchable_form_encoding
= std::string();
136 params
.security_info
= std::string();
137 params
.gesture
= NavigationGestureUser
;
138 params
.contents_mime_type
= contents_mime_type_
;
139 params
.is_post
= false;
140 params
.http_status_code
= response_code
;
141 params
.socket_address
.set_host("2001:db8::1");
142 params
.socket_address
.set_port(80);
143 params
.history_list_was_cleared
= simulate_history_list_was_cleared_
;
144 params
.original_request_url
= original_request_url
;
146 url::Replacements
<char> replacements
;
147 replacements
.ClearRef();
148 params
.was_within_same_page
= transition
!= PAGE_TRANSITION_RELOAD
&&
149 transition
!= PAGE_TRANSITION_TYPED
&&
150 url
.ReplaceComponents(replacements
) ==
151 GetLastCommittedURL().ReplaceComponents(replacements
);
153 params
.page_state
= PageState::CreateForTesting(
156 file_path_for_history_item
? "data" : NULL
,
157 file_path_for_history_item
);
159 FrameHostMsg_DidCommitProvisionalLoad
msg(GetRoutingID(), params
);
163 void TestRenderFrameHost::SendBeginNavigationWithURL(const GURL
& url
) {
164 FrameHostMsg_BeginNavigation_Params params
;
165 params
.method
= "GET";
167 params
.referrer_policy
= blink::WebReferrerPolicyDefault
;
168 params
.load_flags
= net::LOAD_NORMAL
| net::LOAD_ENABLE_LOAD_TIMING
;
169 params
.has_user_gesture
= false;
170 params
.transition_type
= PAGE_TRANSITION_LINK
;
171 params
.should_replace_current_entry
= false;
172 params
.allow_download
= true;
173 // TODO(clamy): When the BeginNavigation handler is no longer compiled out,
174 // call OnBeginNavigation directly.
175 frame_tree_node()->render_manager()->OnBeginNavigation(params
);
177 } // namespace content