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.
4
5 #include "content/test/test_render_frame_host.h"
6
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/test/test_render_view_host.h"
11 #include "net/base/load_flags.h"
12 #include "third_party/WebKit/public/web/WebPageVisibilityState.h"
13 #include "ui/base/page_transition_types.h"
14
15 namespace content {
16
TestRenderFrameHostCreationObserver(WebContents * web_contents)17 TestRenderFrameHostCreationObserver::TestRenderFrameHostCreationObserver(
18 WebContents* web_contents)
19 : WebContentsObserver(web_contents), last_created_frame_(NULL) {
20 }
21
~TestRenderFrameHostCreationObserver()22 TestRenderFrameHostCreationObserver::~TestRenderFrameHostCreationObserver() {
23 }
24
RenderFrameCreated(RenderFrameHost * render_frame_host)25 void TestRenderFrameHostCreationObserver::RenderFrameCreated(
26 RenderFrameHost* render_frame_host) {
27 last_created_frame_ = render_frame_host;
28 }
29
TestRenderFrameHost(RenderViewHostImpl * render_view_host,RenderFrameHostDelegate * delegate,FrameTree * frame_tree,FrameTreeNode * frame_tree_node,int routing_id,bool is_swapped_out)30 TestRenderFrameHost::TestRenderFrameHost(RenderViewHostImpl* render_view_host,
31 RenderFrameHostDelegate* delegate,
32 FrameTree* frame_tree,
33 FrameTreeNode* frame_tree_node,
34 int routing_id,
35 bool is_swapped_out)
36 : RenderFrameHostImpl(render_view_host,
37 delegate,
38 frame_tree,
39 frame_tree_node,
40 routing_id,
41 is_swapped_out),
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);
49 }
50 }
51
~TestRenderFrameHost()52 TestRenderFrameHost::~TestRenderFrameHost() {}
53
GetRenderViewHost()54 TestRenderViewHost* TestRenderFrameHost::GetRenderViewHost() {
55 return static_cast<TestRenderViewHost*>(
56 RenderFrameHostImpl::GetRenderViewHost());
57 }
58
AppendChild(const std::string & frame_name)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());
64 }
65
SendNavigateWithTransition(int page_id,const GURL & url,ui::PageTransition transition)66 void TestRenderFrameHost::SendNavigateWithTransition(
67 int page_id,
68 const GURL& url,
69 ui::PageTransition transition) {
70 SendNavigateWithTransitionAndResponseCode(page_id, url, transition, 200);
71 }
72
SendNavigate(int page_id,const GURL & url)73 void TestRenderFrameHost::SendNavigate(int page_id, const GURL& url) {
74 SendNavigateWithTransition(page_id, url, ui::PAGE_TRANSITION_LINK);
75 }
76
SendFailedNavigate(int page_id,const GURL & url)77 void TestRenderFrameHost::SendFailedNavigate(int page_id, const GURL& url) {
78 SendNavigateWithTransitionAndResponseCode(
79 page_id, url, ui::PAGE_TRANSITION_RELOAD, 500);
80 }
81
SendNavigateWithTransitionAndResponseCode(int page_id,const GURL & url,ui::PageTransition transition,int response_code)82 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode(
83 int page_id,
84 const GURL& url, ui::PageTransition transition,
85 int response_code) {
86 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
87 // so we keep a copy of it to use in SendNavigateWithParameters.
88 GURL url_copy(url);
89 OnDidStartProvisionalLoadForFrame(url_copy, false);
90 SendNavigateWithParameters(page_id, url_copy, transition, url_copy,
91 response_code, 0, std::vector<GURL>());
92 }
93
SendNavigateWithOriginalRequestURL(int page_id,const GURL & url,const GURL & original_request_url)94 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL(
95 int page_id,
96 const GURL& url,
97 const GURL& original_request_url) {
98 OnDidStartProvisionalLoadForFrame(url, false);
99 SendNavigateWithParameters(page_id, url, ui::PAGE_TRANSITION_LINK,
100 original_request_url, 200, 0, std::vector<GURL>());
101 }
102
SendNavigateWithFile(int page_id,const GURL & url,const base::FilePath & file_path)103 void TestRenderFrameHost::SendNavigateWithFile(
104 int page_id,
105 const GURL& url,
106 const base::FilePath& file_path) {
107 SendNavigateWithParameters(page_id, url, ui::PAGE_TRANSITION_LINK, url, 200,
108 &file_path, std::vector<GURL>());
109 }
110
SendNavigateWithParams(FrameHostMsg_DidCommitProvisionalLoad_Params * params)111 void TestRenderFrameHost::SendNavigateWithParams(
112 FrameHostMsg_DidCommitProvisionalLoad_Params* params) {
113 FrameHostMsg_DidCommitProvisionalLoad msg(GetRoutingID(), *params);
114 OnDidCommitProvisionalLoad(msg);
115 }
116
SendNavigateWithRedirects(int page_id,const GURL & url,const std::vector<GURL> & redirects)117 void TestRenderFrameHost::SendNavigateWithRedirects(
118 int page_id,
119 const GURL& url,
120 const std::vector<GURL>& redirects) {
121 SendNavigateWithParameters(
122 page_id, url, ui::PAGE_TRANSITION_LINK, url, 200, 0, redirects);
123 }
124
SendNavigateWithParameters(int page_id,const GURL & url,ui::PageTransition transition,const GURL & original_request_url,int response_code,const base::FilePath * file_path_for_history_item,const std::vector<GURL> & redirects)125 void TestRenderFrameHost::SendNavigateWithParameters(
126 int page_id,
127 const GURL& url,
128 ui::PageTransition transition,
129 const GURL& original_request_url,
130 int response_code,
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;
135 params.url = url;
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;
151
152 url::Replacements<char> replacements;
153 replacements.ClearRef();
154 params.was_within_same_page = transition != ui::PAGE_TRANSITION_RELOAD &&
155 transition != ui::PAGE_TRANSITION_TYPED &&
156 url.ReplaceComponents(replacements) ==
157 GetLastCommittedURL().ReplaceComponents(replacements);
158
159 params.page_state = PageState::CreateForTesting(
160 url,
161 false,
162 file_path_for_history_item ? "data" : NULL,
163 file_path_for_history_item);
164
165 FrameHostMsg_DidCommitProvisionalLoad msg(GetRoutingID(), params);
166 OnDidCommitProvisionalLoad(msg);
167 }
168
SendBeginNavigationWithURL(const GURL & url)169 void TestRenderFrameHost::SendBeginNavigationWithURL(const GURL& url) {
170 FrameHostMsg_BeginNavigation_Params params;
171 params.method = "GET";
172 params.url = url;
173 params.referrer = Referrer(GURL(), blink::WebReferrerPolicyDefault);
174 params.load_flags = net::LOAD_NORMAL;
175 params.has_user_gesture = false;
176 params.transition_type = ui::PAGE_TRANSITION_LINK;
177 params.should_replace_current_entry = false;
178 params.allow_download = true;
179 OnBeginNavigation(params);
180 }
181
DidDisownOpener()182 void TestRenderFrameHost::DidDisownOpener() {
183 OnDidDisownOpener();
184 }
185
186 } // namespace content
187