Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / test / test_render_frame_host.cc
blob950aa19856f0f1ba2a6326fe3d339468ee484357
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 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry, url,
107 ui::PAGE_TRANSITION_LINK, 200,
108 ModificationCallback());
111 void TestRenderFrameHost::SendFailedNavigate(int page_id,
112 int nav_entry_id,
113 bool did_create_new_entry,
114 const GURL& url) {
115 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry, url,
116 ui::PAGE_TRANSITION_RELOAD, 500,
117 ModificationCallback());
120 void TestRenderFrameHost::SendNavigateWithTransition(
121 int page_id,
122 int nav_entry_id,
123 bool did_create_new_entry,
124 const GURL& url,
125 ui::PageTransition transition) {
126 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry, url,
127 transition, 200, ModificationCallback());
130 void TestRenderFrameHost::SendNavigateWithModificationCallback(
131 int page_id,
132 int nav_entry_id,
133 bool did_create_new_entry,
134 const GURL& url,
135 const ModificationCallback& callback) {
136 SendNavigateWithParameters(page_id, nav_entry_id, did_create_new_entry, url,
137 ui::PAGE_TRANSITION_LINK, 200, callback);
140 void TestRenderFrameHost::SendNavigateWithParameters(
141 int page_id,
142 int nav_entry_id,
143 bool did_create_new_entry,
144 const GURL& url,
145 ui::PageTransition transition,
146 int response_code,
147 const ModificationCallback& callback) {
148 // DidStartProvisionalLoad may delete the pending entry that holds |url|,
149 // so we keep a copy of it to use below.
150 GURL url_copy(url);
151 OnDidStartProvisionalLoadForFrame(url_copy);
153 FrameHostMsg_DidCommitProvisionalLoad_Params params;
154 params.page_id = page_id;
155 params.nav_entry_id = nav_entry_id;
156 params.url = url_copy;
157 params.transition = transition;
158 params.should_update_history = true;
159 params.did_create_new_entry = did_create_new_entry;
160 params.gesture = NavigationGestureUser;
161 params.contents_mime_type = contents_mime_type_;
162 params.is_post = false;
163 params.http_status_code = response_code;
164 params.socket_address.set_host("2001:db8::1");
165 params.socket_address.set_port(80);
166 params.history_list_was_cleared = simulate_history_list_was_cleared_;
167 params.original_request_url = url_copy;
169 url::Replacements<char> replacements;
170 replacements.ClearRef();
171 params.was_within_same_page =
172 transition != ui::PAGE_TRANSITION_RELOAD &&
173 transition != ui::PAGE_TRANSITION_TYPED &&
174 url_copy.ReplaceComponents(replacements) ==
175 GetLastCommittedURL().ReplaceComponents(replacements);
177 params.page_state =
178 PageState::CreateForTesting(url_copy, false, nullptr, nullptr);
180 if (!callback.is_null())
181 callback.Run(&params);
183 SendNavigateWithParams(&params);
186 void TestRenderFrameHost::SendNavigateWithParams(
187 FrameHostMsg_DidCommitProvisionalLoad_Params* params) {
188 FrameHostMsg_DidCommitProvisionalLoad msg(GetRoutingID(), *params);
189 OnDidCommitProvisionalLoad(msg);
192 void TestRenderFrameHost::NavigateAndCommitRendererInitiated(
193 int page_id,
194 bool did_create_new_entry,
195 const GURL& url) {
196 SendRendererInitiatedNavigationRequest(url, false);
197 // PlzNavigate: If no network request is needed by the navigation, then there
198 // will be no NavigationRequest, nor is it necessary to simulate the network
199 // stack commit.
200 if (frame_tree_node()->navigation_request())
201 PrepareForCommit();
202 bool browser_side_navigation =
203 base::CommandLine::ForCurrentProcess()->HasSwitch(
204 switches::kEnableBrowserSideNavigation);
205 CHECK_IMPLIES(browser_side_navigation, is_loading());
206 CHECK_IMPLIES(browser_side_navigation,
207 !frame_tree_node()->navigation_request());
208 SendNavigate(page_id, 0, did_create_new_entry, url);
211 void TestRenderFrameHost::SendRendererInitiatedNavigationRequest(
212 const GURL& url,
213 bool has_user_gesture) {
214 // Since this is renderer-initiated navigation, the RenderFrame must be
215 // initialized. Do it if it hasn't happened yet.
216 InitializeRenderFrameIfNeeded();
218 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
219 switches::kEnableBrowserSideNavigation)) {
220 BeginNavigationParams begin_params("GET", std::string(), net::LOAD_NORMAL,
221 has_user_gesture);
222 CommonNavigationParams common_params;
223 common_params.url = url;
224 common_params.referrer = Referrer(GURL(), blink::WebReferrerPolicyDefault);
225 common_params.transition = ui::PAGE_TRANSITION_LINK;
226 OnBeginNavigation(common_params, begin_params,
227 scoped_refptr<ResourceRequestBody>());
231 void TestRenderFrameHost::DidDisownOpener() {
232 OnDidDisownOpener();
235 void TestRenderFrameHost::PrepareForCommit() {
236 PrepareForCommitWithServerRedirect(GURL());
239 void TestRenderFrameHost::PrepareForCommitWithServerRedirect(
240 const GURL& redirect_url) {
241 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
242 switches::kEnableBrowserSideNavigation)) {
243 // Non PlzNavigate
244 if (is_waiting_for_beforeunload_ack())
245 SendBeforeUnloadACK(true);
246 return;
249 // PlzNavigate
250 NavigationRequest* request = frame_tree_node_->navigation_request();
251 CHECK(request);
253 // Simulate a beforeUnload ACK from the renderer if the browser is waiting for
254 // it. If it runs it will update the request state.
255 if (request->state() == NavigationRequest::WAITING_FOR_RENDERER_RESPONSE)
256 SendBeforeUnloadACK(true);
258 CHECK(request->state() == NavigationRequest::STARTED);
260 TestNavigationURLLoader* url_loader =
261 static_cast<TestNavigationURLLoader*>(request->loader_for_testing());
262 CHECK(url_loader);
264 // If a non-empty |redirect_url| was provided, simulate a server redirect.
265 if (!redirect_url.is_empty())
266 url_loader->SimulateServerRedirect(redirect_url);
268 // Simulate the network stack commit.
269 scoped_refptr<ResourceResponse> response(new ResourceResponse);
270 // TODO(carlosk): ideally with PlzNavigate it should be possible someday to
271 // fully commit the navigation at this call to CallOnResponseStarted.
272 url_loader->CallOnResponseStarted(response, MakeEmptyStream());
275 } // namespace content