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/common/frame_messages.h"
9 #include "content/test/test_render_view_host.h"
13 TestRenderFrameHost::TestRenderFrameHost(RenderViewHostImpl
* render_view_host
,
14 RenderFrameHostDelegate
* delegate
,
15 FrameTree
* frame_tree
,
16 FrameTreeNode
* frame_tree_node
,
19 : RenderFrameHostImpl(render_view_host
,
25 contents_mime_type_("text/html"),
26 simulate_history_list_was_cleared_(false) {
27 // Allow TestRenderViewHosts to easily access their main frame RFH.
28 if (frame_tree_node
== frame_tree
->root()) {
29 static_cast<TestRenderViewHost
*>(render_view_host
)->
30 set_main_render_frame_host(this);
34 TestRenderFrameHost::~TestRenderFrameHost() {}
36 void TestRenderFrameHost::SendNavigate(int page_id
, const GURL
& url
) {
37 SendNavigateWithTransition(page_id
, url
, PAGE_TRANSITION_LINK
);
40 void TestRenderFrameHost::SendNavigateWithTransition(
43 PageTransition transition
) {
44 SendNavigateWithTransitionAndResponseCode(page_id
, url
, transition
, 200);
47 void TestRenderFrameHost::SendFailedNavigate(int page_id
, const GURL
& url
) {
48 SendNavigateWithTransitionAndResponseCode(
49 page_id
, url
, PAGE_TRANSITION_RELOAD
, 500);
52 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode(
54 const GURL
& url
, PageTransition transition
,
56 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
57 // so we keep a copy of it to use in SendNavigateWithParameters.
59 OnDidStartProvisionalLoadForFrame(-1, url_copy
);
60 SendNavigateWithParameters(page_id
, url_copy
, transition
, url_copy
,
61 response_code
, 0, std::vector
<GURL
>());
64 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL(
67 const GURL
& original_request_url
) {
68 OnDidStartProvisionalLoadForFrame(-1, url
);
69 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
,
70 original_request_url
, 200, 0, std::vector
<GURL
>());
73 void TestRenderFrameHost::SendNavigateWithFile(
76 const base::FilePath
& file_path
) {
77 SendNavigateWithParameters(page_id
, url
, PAGE_TRANSITION_LINK
, url
, 200,
78 &file_path
, std::vector
<GURL
>());
81 void TestRenderFrameHost::SendNavigateWithParams(
82 FrameHostMsg_DidCommitProvisionalLoad_Params
* params
) {
83 FrameHostMsg_DidCommitProvisionalLoad
msg(1, *params
);
87 void TestRenderFrameHost::SendNavigateWithRedirects(
90 const std::vector
<GURL
>& redirects
) {
91 SendNavigateWithParameters(
92 page_id
, url
, PAGE_TRANSITION_LINK
, url
, 200, 0, redirects
);
95 void TestRenderFrameHost::SendNavigateWithParameters(
98 PageTransition transition
,
99 const GURL
& original_request_url
,
101 const base::FilePath
* file_path_for_history_item
,
102 const std::vector
<GURL
>& redirects
) {
103 FrameHostMsg_DidCommitProvisionalLoad_Params params
;
104 params
.page_id
= page_id
;
106 params
.referrer
= Referrer();
107 params
.transition
= transition
;
108 params
.redirects
= redirects
;
109 params
.should_update_history
= true;
110 params
.searchable_form_url
= GURL();
111 params
.searchable_form_encoding
= std::string();
112 params
.security_info
= std::string();
113 params
.gesture
= NavigationGestureUser
;
114 params
.contents_mime_type
= contents_mime_type_
;
115 params
.is_post
= false;
116 params
.http_status_code
= response_code
;
117 params
.socket_address
.set_host("2001:db8::1");
118 params
.socket_address
.set_port(80);
119 params
.history_list_was_cleared
= simulate_history_list_was_cleared_
;
120 params
.original_request_url
= original_request_url
;
122 url::Replacements
<char> replacements
;
123 replacements
.ClearRef();
124 params
.was_within_same_page
= transition
!= PAGE_TRANSITION_RELOAD
&&
125 transition
!= PAGE_TRANSITION_TYPED
&&
126 url
.ReplaceComponents(replacements
) ==
127 GetLastCommittedURL().ReplaceComponents(replacements
);
129 params
.page_state
= PageState::CreateForTesting(
132 file_path_for_history_item
? "data" : NULL
,
133 file_path_for_history_item
);
135 FrameHostMsg_DidCommitProvisionalLoad
msg(1, params
);
139 } // namespace content