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/common/frame_messages.h" 9 #include "content/test/test_render_view_host.h" 10 11 namespace content { 12 13 TestRenderFrameHost::TestRenderFrameHost(RenderViewHostImpl* render_view_host, 14 RenderFrameHostDelegate* delegate, 15 FrameTree* frame_tree, 16 FrameTreeNode* frame_tree_node, 17 int routing_id, 18 bool is_swapped_out) 19 : RenderFrameHostImpl(render_view_host, 20 delegate, 21 frame_tree, 22 frame_tree_node, 23 routing_id, 24 is_swapped_out), 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); 31 } 32 } 33 34 TestRenderFrameHost::~TestRenderFrameHost() {} 35 36 void TestRenderFrameHost::SendNavigate(int page_id, const GURL& url) { 37 SendNavigateWithTransition(page_id, url, PAGE_TRANSITION_LINK); 38 } 39 40 void TestRenderFrameHost::SendNavigateWithTransition( 41 int page_id, 42 const GURL& url, 43 PageTransition transition) { 44 SendNavigateWithTransitionAndResponseCode(page_id, url, transition, 200); 45 } 46 47 void TestRenderFrameHost::SendFailedNavigate(int page_id, const GURL& url) { 48 SendNavigateWithTransitionAndResponseCode( 49 page_id, url, PAGE_TRANSITION_RELOAD, 500); 50 } 51 52 void TestRenderFrameHost::SendNavigateWithTransitionAndResponseCode( 53 int page_id, 54 const GURL& url, PageTransition transition, 55 int response_code) { 56 // DidStartProvisionalLoad may delete the pending entry that holds |url|, 57 // so we keep a copy of it to use in SendNavigateWithParameters. 58 GURL url_copy(url); 59 OnDidStartProvisionalLoadForFrame(-1, url_copy); 60 SendNavigateWithParameters(page_id, url_copy, transition, url_copy, 61 response_code, 0, std::vector<GURL>()); 62 } 63 64 void TestRenderFrameHost::SendNavigateWithOriginalRequestURL( 65 int page_id, 66 const GURL& url, 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>()); 71 } 72 73 void TestRenderFrameHost::SendNavigateWithFile( 74 int page_id, 75 const GURL& url, 76 const base::FilePath& file_path) { 77 SendNavigateWithParameters(page_id, url, PAGE_TRANSITION_LINK, url, 200, 78 &file_path, std::vector<GURL>()); 79 } 80 81 void TestRenderFrameHost::SendNavigateWithParams( 82 FrameHostMsg_DidCommitProvisionalLoad_Params* params) { 83 FrameHostMsg_DidCommitProvisionalLoad msg(1, *params); 84 OnNavigate(msg); 85 } 86 87 void TestRenderFrameHost::SendNavigateWithRedirects( 88 int page_id, 89 const GURL& url, 90 const std::vector<GURL>& redirects) { 91 SendNavigateWithParameters( 92 page_id, url, PAGE_TRANSITION_LINK, url, 200, 0, redirects); 93 } 94 95 void TestRenderFrameHost::SendNavigateWithParameters( 96 int page_id, 97 const GURL& url, 98 PageTransition transition, 99 const GURL& original_request_url, 100 int response_code, 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; 105 params.url = url; 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; 121 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); 128 129 params.page_state = PageState::CreateForTesting( 130 url, 131 false, 132 file_path_for_history_item ? "data" : NULL, 133 file_path_for_history_item); 134 135 FrameHostMsg_DidCommitProvisionalLoad msg(1, params); 136 OnNavigate(msg); 137 } 138 139 } // namespace content 140