Remove unused code calling WebMediaPlayerClient::requestFullscreen()
[chromium-blink-merge.git] / content / test / test_render_frame_host.cc
blob504ac771aefe0cfa26e1893f526a826ad075dab6
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 "third_party/WebKit/public/web/WebSandboxFlags.h"
22 #include "third_party/WebKit/public/web/WebTreeScopeType.h"
23 #include "ui/base/page_transition_types.h"
25 namespace content {
27 TestRenderFrameHostCreationObserver::TestRenderFrameHostCreationObserver(
28 WebContents* web_contents)
29 : WebContentsObserver(web_contents), last_created_frame_(NULL) {
32 TestRenderFrameHostCreationObserver::~TestRenderFrameHostCreationObserver() {
35 void TestRenderFrameHostCreationObserver::RenderFrameCreated(
36 RenderFrameHost* render_frame_host) {
37 last_created_frame_ = render_frame_host;
40 TestRenderFrameHost::TestRenderFrameHost(SiteInstance* site_instance,
41 RenderViewHostImpl* render_view_host,
42 RenderFrameHostDelegate* delegate,
43 RenderWidgetHostDelegate* rwh_delegate,
44 FrameTree* frame_tree,
45 FrameTreeNode* frame_tree_node,
46 int routing_id,
47 int flags)
48 : RenderFrameHostImpl(site_instance,
49 render_view_host,
50 delegate,
51 rwh_delegate,
52 frame_tree,
53 frame_tree_node,
54 routing_id,
55 flags),
56 child_creation_observer_(delegate ? delegate->GetAsWebContents() : NULL),
57 contents_mime_type_("text/html"),
58 simulate_history_list_was_cleared_(false) {
61 TestRenderFrameHost::~TestRenderFrameHost() {
64 TestRenderViewHost* TestRenderFrameHost::GetRenderViewHost() {
65 return static_cast<TestRenderViewHost*>(
66 RenderFrameHostImpl::GetRenderViewHost());
69 MockRenderProcessHost* TestRenderFrameHost::GetProcess() {
70 return static_cast<MockRenderProcessHost*>(RenderFrameHostImpl::GetProcess());
73 void TestRenderFrameHost::InitializeRenderFrameIfNeeded() {
74 if (!render_view_host()->IsRenderViewLive()) {
75 RenderViewHostTester::For(render_view_host())->CreateTestRenderView(
76 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false);
80 TestRenderFrameHost* TestRenderFrameHost::AppendChild(
81 const std::string& frame_name) {
82 OnCreateChildFrame(GetProcess()->GetNextRoutingID(),
83 blink::WebTreeScopeType::Document, frame_name,
84 blink::WebSandboxFlags::None);
85 return static_cast<TestRenderFrameHost*>(
86 child_creation_observer_.last_created_frame());
89 void TestRenderFrameHost::SetContentsMimeType(const std::string& mime_type) {
90 contents_mime_type_ = mime_type;
93 void TestRenderFrameHost::SendBeforeUnloadACK(bool proceed) {
94 base::TimeTicks now = base::TimeTicks::Now();
95 OnBeforeUnloadACK(proceed, now, now);
98 void TestRenderFrameHost::SimulateSwapOutACK() {
99 OnSwappedOut();
102 void TestRenderFrameHost::SendNavigate(int page_id,
103 int nav_entry_id,
104 bool did_create_new_entry,
105 const GURL& url) {
106 SendNavigateWithTransition(page_id, nav_entry_id, did_create_new_entry, url,
107 ui::PAGE_TRANSITION_LINK);
110 void TestRenderFrameHost::SendFailedNavigate(int page_id,
111 int nav_entry_id,
112 bool did_create_new_entry,
113 const GURL& url) {
114 SendNavigateWithTransitionAndResponseCode(page_id, nav_entry_id,
115 did_create_new_entry, url,
116 ui::PAGE_TRANSITION_RELOAD, 500);
119 void TestRenderFrameHost::SendNavigateWithTransition(
120 int page_id,
121 int nav_entry_id,
122 bool did_create_new_entry,
123 const GURL& url,
124 ui::PageTransition transition) {
125 SendNavigateWithTransitionAndResponseCode(
126 page_id, nav_entry_id, did_create_new_entry, url, transition, 200);
129 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode(
130 int page_id,
131 int nav_entry_id,
132 bool did_create_new_entry,
133 const GURL& url,
134 ui::PageTransition transition,
135 int response_code) {
136 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
137 // so we keep a copy of it to use in SendNavigateWithParameters.
138 GURL url_copy(url);
139 OnDidStartProvisionalLoadForFrame(url_copy);
140 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry,
141 url_copy, transition, url_copy, response_code, 0,
142 std::vector<GURL>());
145 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL(
146 int page_id,
147 int nav_entry_id,
148 bool did_create_new_entry,
149 const GURL& url,
150 const GURL& original_request_url) {
151 OnDidStartProvisionalLoadForFrame(url);
152 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry, url,
153 ui::PAGE_TRANSITION_LINK, original_request_url,
154 200, 0, std::vector<GURL>());
157 void TestRenderFrameHost::SendNavigateWithFile(
158 int page_id,
159 int nav_entry_id,
160 bool did_create_new_entry,
161 const GURL& url,
162 const base::FilePath& file_path) {
163 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry, url,
164 ui::PAGE_TRANSITION_LINK, url, 200, &file_path,
165 std::vector<GURL>());
168 void TestRenderFrameHost::SendNavigateWithParams(
169 FrameHostMsg_DidCommitProvisionalLoad_Params* params) {
170 FrameHostMsg_DidCommitProvisionalLoad msg(GetRoutingID(), *params);
171 OnDidCommitProvisionalLoad(msg);
174 void TestRenderFrameHost::SendNavigateWithRedirects(
175 int page_id,
176 int nav_entry_id,
177 bool did_create_new_entry,
178 const GURL& url,
179 const std::vector<GURL>& redirects) {
180 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry, url,
181 ui::PAGE_TRANSITION_LINK, url, 200, 0, redirects);
184 void TestRenderFrameHost::SendNavigateWithParameters(
185 int page_id,
186 int nav_entry_id,
187 bool did_create_new_entry,
188 const GURL& url,
189 ui::PageTransition transition,
190 const GURL& original_request_url,
191 int response_code,
192 const base::FilePath* file_path_for_history_item,
193 const std::vector<GURL>& redirects) {
194 FrameHostMsg_DidCommitProvisionalLoad_Params params;
195 params.page_id = page_id;
196 params.nav_entry_id = nav_entry_id;
197 params.url = url;
198 params.referrer = Referrer();
199 params.transition = transition;
200 params.redirects = redirects;
201 params.should_update_history = true;
202 params.searchable_form_url = GURL();
203 params.searchable_form_encoding = std::string();
204 params.did_create_new_entry = did_create_new_entry;
205 params.security_info = std::string();
206 params.gesture = NavigationGestureUser;
207 params.contents_mime_type = contents_mime_type_;
208 params.is_post = false;
209 params.http_status_code = response_code;
210 params.socket_address.set_host("2001:db8::1");
211 params.socket_address.set_port(80);
212 params.history_list_was_cleared = simulate_history_list_was_cleared_;
213 params.original_request_url = original_request_url;
215 url::Replacements<char> replacements;
216 replacements.ClearRef();
217 params.was_within_same_page = transition != ui::PAGE_TRANSITION_RELOAD &&
218 transition != ui::PAGE_TRANSITION_TYPED &&
219 url.ReplaceComponents(replacements) ==
220 GetLastCommittedURL().ReplaceComponents(replacements);
222 params.page_state = PageState::CreateForTesting(
223 url,
224 false,
225 file_path_for_history_item ? "data" : NULL,
226 file_path_for_history_item);
228 FrameHostMsg_DidCommitProvisionalLoad msg(GetRoutingID(), params);
229 OnDidCommitProvisionalLoad(msg);
232 void TestRenderFrameHost::NavigateAndCommitRendererInitiated(
233 int page_id,
234 bool did_create_new_entry,
235 const GURL& url) {
236 SendRendererInitiatedNavigationRequest(url, false);
237 PrepareForCommit();
238 SendNavigate(page_id, 0, did_create_new_entry, url);
241 void TestRenderFrameHost::SendRendererInitiatedNavigationRequest(
242 const GURL& url,
243 bool has_user_gesture) {
244 // Since this is renderer-initiated navigation, the RenderFrame must be
245 // initialized. Do it if it hasn't happened yet.
246 InitializeRenderFrameIfNeeded();
248 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
249 switches::kEnableBrowserSideNavigation)) {
250 BeginNavigationParams begin_params("GET", std::string(), net::LOAD_NORMAL,
251 has_user_gesture);
252 CommonNavigationParams common_params;
253 common_params.url = url;
254 common_params.referrer = Referrer(GURL(), blink::WebReferrerPolicyDefault);
255 common_params.transition = ui::PAGE_TRANSITION_LINK;
256 OnBeginNavigation(common_params, begin_params,
257 scoped_refptr<ResourceRequestBody>());
261 void TestRenderFrameHost::DidDisownOpener() {
262 OnDidDisownOpener();
265 void TestRenderFrameHost::PrepareForCommit() {
266 PrepareForCommitWithServerRedirect(GURL());
269 void TestRenderFrameHost::PrepareForCommitWithServerRedirect(
270 const GURL& redirect_url) {
271 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
272 switches::kEnableBrowserSideNavigation)) {
273 // Non PlzNavigate
274 if (IsWaitingForBeforeUnloadACK())
275 SendBeforeUnloadACK(true);
276 return;
279 // PlzNavigate
280 NavigationRequest* request = frame_tree_node_->navigation_request();
281 CHECK(request);
283 // Simulate a beforeUnload ACK from the renderer if the browser is waiting for
284 // it. If it runs it will update the request state.
285 if (request->state() == NavigationRequest::WAITING_FOR_RENDERER_RESPONSE)
286 SendBeforeUnloadACK(true);
288 // If a network request is not needed for this URL, just check the request is
289 // in the correct state and return.
290 if (!request->ShouldMakeNetworkRequest(request->common_params().url)) {
291 CHECK(request->state() == NavigationRequest::RESPONSE_STARTED);
292 return;
295 CHECK(request->state() == NavigationRequest::STARTED);
297 TestNavigationURLLoader* url_loader =
298 static_cast<TestNavigationURLLoader*>(request->loader_for_testing());
299 CHECK(url_loader);
301 // If a non-empty |redirect_url| was provided, simulate a server redirect.
302 if (!redirect_url.is_empty())
303 url_loader->SimulateServerRedirect(redirect_url);
305 // Simulate the network stack commit.
306 scoped_refptr<ResourceResponse> response(new ResourceResponse);
307 // TODO(carlosk): ideally with PlzNavigate it should be possible someday to
308 // fully commit the navigation at this call to CallOnResponseStarted.
309 url_loader->CallOnResponseStarted(response, MakeEmptyStream());
312 } // namespace content