Avoid stale navigation requests without excessive page id knowledge in the renderer...
[chromium-blink-merge.git] / content / test / test_web_contents.cc
blobe4f4846f67b1e909fa6168de59350f80d62a87f6
1 // Copyright (c) 2012 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_web_contents.h"
7 #include <utility>
9 #include "base/command_line.h"
10 #include "content/browser/browser_url_handler_impl.h"
11 #include "content/browser/frame_host/cross_process_frame_connector.h"
12 #include "content/browser/frame_host/navigation_entry_impl.h"
13 #include "content/browser/frame_host/navigation_request.h"
14 #include "content/browser/frame_host/navigator.h"
15 #include "content/browser/frame_host/navigator_impl.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/site_instance_impl.h"
18 #include "content/common/view_messages.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/browser/stream_handle.h"
23 #include "content/public/common/content_switches.h"
24 #include "content/public/common/page_state.h"
25 #include "content/public/test/mock_render_process_host.h"
26 #include "content/test/browser_side_navigation_test_utils.h"
27 #include "content/test/test_navigation_url_loader.h"
28 #include "content/test/test_render_view_host.h"
29 #include "ui/base/page_transition_types.h"
31 namespace content {
33 TestWebContents::TestWebContents(BrowserContext* browser_context)
34 : WebContentsImpl(browser_context, NULL),
35 delegate_view_override_(NULL),
36 expect_set_history_offset_and_length_(false),
37 expect_set_history_offset_and_length_history_length_(0) {
40 TestWebContents* TestWebContents::Create(BrowserContext* browser_context,
41 SiteInstance* instance) {
42 TestWebContents* test_web_contents = new TestWebContents(browser_context);
43 test_web_contents->Init(WebContents::CreateParams(browser_context, instance));
44 return test_web_contents;
47 TestWebContents::~TestWebContents() {
48 EXPECT_FALSE(expect_set_history_offset_and_length_);
51 TestRenderFrameHost* TestWebContents::GetMainFrame() {
52 return static_cast<TestRenderFrameHost*>(WebContentsImpl::GetMainFrame());
55 TestRenderViewHost* TestWebContents::GetRenderViewHost() const {
56 return static_cast<TestRenderViewHost*>(
57 WebContentsImpl::GetRenderViewHost());
60 TestRenderFrameHost* TestWebContents::GetPendingMainFrame() const {
61 return static_cast<TestRenderFrameHost*>(
62 GetRenderManager()->pending_frame_host());
65 void TestWebContents::TestDidNavigate(RenderFrameHost* render_frame_host,
66 int page_id,
67 const GURL& url,
68 ui::PageTransition transition) {
69 TestDidNavigateWithReferrer(render_frame_host,
70 page_id,
71 url,
72 Referrer(),
73 transition);
76 void TestWebContents::TestDidNavigateWithReferrer(
77 RenderFrameHost* render_frame_host,
78 int page_id,
79 const GURL& url,
80 const Referrer& referrer,
81 ui::PageTransition transition) {
82 FrameHostMsg_DidCommitProvisionalLoad_Params params;
84 params.page_id = page_id;
85 params.url = url;
86 params.referrer = referrer;
87 params.transition = transition;
88 params.redirects = std::vector<GURL>();
89 params.should_update_history = false;
90 params.searchable_form_url = GURL();
91 params.searchable_form_encoding = std::string();
92 params.security_info = std::string();
93 params.gesture = NavigationGestureUser;
94 params.was_within_same_page = false;
95 params.is_post = false;
96 params.page_state = PageState::CreateFromURL(url);
98 RenderFrameHostImpl* rfhi =
99 static_cast<RenderFrameHostImpl*>(render_frame_host);
100 rfhi->frame_tree_node()->navigator()->DidNavigate(rfhi, params);
103 WebPreferences TestWebContents::TestComputeWebkitPrefs() {
104 return ComputeWebkitPrefs();
107 bool TestWebContents::CreateRenderViewForRenderManager(
108 RenderViewHost* render_view_host,
109 int opener_route_id,
110 int proxy_routing_id,
111 bool for_main_frame) {
112 UpdateMaxPageIDIfNecessary(render_view_host);
113 // This will go to a TestRenderViewHost.
114 static_cast<RenderViewHostImpl*>(
115 render_view_host)->CreateRenderView(base::string16(),
116 opener_route_id,
117 proxy_routing_id,
118 -1, false);
119 return true;
122 WebContents* TestWebContents::Clone() {
123 WebContentsImpl* contents =
124 Create(GetBrowserContext(), SiteInstance::Create(GetBrowserContext()));
125 contents->GetController().CopyStateFrom(controller_);
126 return contents;
129 void TestWebContents::NavigateAndCommit(const GURL& url) {
130 bool has_live_renderer = GetMainFrame()->IsRenderFrameLive();
131 GetController().LoadURL(
132 url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
133 GURL loaded_url(url);
134 bool reverse_on_redirect = false;
135 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
136 &loaded_url, GetBrowserContext(), &reverse_on_redirect);
138 // PlzNavigate
139 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
140 switches::kEnableBrowserSideNavigation)) {
141 // Simulate the renderer response if there was a live renderer when the
142 // navigation started. Otherwise, it will have been sent directly to the
143 // network stack.
144 if (has_live_renderer)
145 GetMainFrame()->SendBeginNavigationWithURL(url);
147 // Now simulate the network stack commit without any redirects. This will
148 // cause the navigation to commit at the same url.
149 FrameTreeNode* frame_tree_node = GetMainFrame()->frame_tree_node();
150 NavigationRequest* request =
151 static_cast<NavigatorImpl*>(frame_tree_node->navigator())
152 ->GetNavigationRequestForNodeForTesting(frame_tree_node);
153 TestNavigationURLLoader* url_loader =
154 static_cast<TestNavigationURLLoader*>(request->loader_for_testing());
155 scoped_refptr<ResourceResponse> response(new ResourceResponse);
156 url_loader->CallOnResponseStarted(response, MakeEmptyStream());
159 // LoadURL created a navigation entry, now simulate the RenderView sending
160 // a notification that it actually navigated.
161 CommitPendingNavigation();
164 void TestWebContents::TestSetIsLoading(bool value) {
165 SetIsLoading(GetRenderViewHost(), value, true, NULL);
168 void TestWebContents::CommitPendingNavigation() {
169 // If we are doing a cross-site navigation, this simulates the current RVH
170 // notifying that it has unloaded so the pending RVH is resumed and can
171 // navigate.
172 ProceedWithCrossSiteNavigation();
173 TestRenderFrameHost* old_rfh = GetMainFrame();
174 TestRenderFrameHost* rfh = GetPendingMainFrame();
175 if (!rfh)
176 rfh = old_rfh;
178 const NavigationEntry* entry = GetController().GetPendingEntry();
179 DCHECK(entry);
180 int page_id = entry->GetPageID();
181 if (page_id == -1) {
182 // It's a new navigation, assign a never-seen page id to it.
183 page_id = GetMaxPageIDForSiteInstance(rfh->GetSiteInstance()) + 1;
186 rfh->SendNavigate(page_id, entry->GetURL());
187 // Simulate the SwapOut_ACK. This is needed when cross-site navigation happens
188 // (old_rfh != rfh).
189 if (old_rfh != rfh)
190 old_rfh->OnSwappedOut();
193 void TestWebContents::ProceedWithCrossSiteNavigation() {
194 if (!GetPendingMainFrame())
195 return;
196 GetMainFrame()->SendBeforeUnloadACK(true);
199 RenderViewHostDelegateView* TestWebContents::GetDelegateView() {
200 if (delegate_view_override_)
201 return delegate_view_override_;
202 return WebContentsImpl::GetDelegateView();
205 void TestWebContents::SetOpener(TestWebContents* opener) {
206 // This is normally only set in the WebContents constructor, which also
207 // registers an observer for when the opener gets closed.
208 opener_ = opener;
209 AddDestructionObserver(opener_);
212 void TestWebContents::AddPendingContents(TestWebContents* contents) {
213 // This is normally only done in WebContentsImpl::CreateNewWindow.
214 pending_contents_[contents->GetRenderViewHost()->GetRoutingID()] = contents;
215 AddDestructionObserver(contents);
218 void TestWebContents::ExpectSetHistoryOffsetAndLength(int history_offset,
219 int history_length) {
220 expect_set_history_offset_and_length_ = true;
221 expect_set_history_offset_and_length_history_offset_ = history_offset;
222 expect_set_history_offset_and_length_history_length_ = history_length;
225 void TestWebContents::SetHistoryOffsetAndLength(int history_offset,
226 int history_length) {
227 EXPECT_TRUE(expect_set_history_offset_and_length_);
228 expect_set_history_offset_and_length_ = false;
229 EXPECT_EQ(expect_set_history_offset_and_length_history_offset_,
230 history_offset);
231 EXPECT_EQ(expect_set_history_offset_and_length_history_length_,
232 history_length);
235 void TestWebContents::TestDidFinishLoad(const GURL& url) {
236 FrameHostMsg_DidFinishLoad msg(0, url);
237 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
240 void TestWebContents::TestDidFailLoadWithError(
241 const GURL& url,
242 int error_code,
243 const base::string16& error_description) {
244 FrameHostMsg_DidFailLoadWithError msg(
245 0, url, error_code, error_description);
246 frame_tree_.root()->current_frame_host()->OnMessageReceived(msg);
249 void TestWebContents::CreateNewWindow(
250 int render_process_id,
251 int route_id,
252 int main_frame_route_id,
253 const ViewHostMsg_CreateWindow_Params& params,
254 SessionStorageNamespace* session_storage_namespace) {
257 void TestWebContents::CreateNewWidget(int render_process_id,
258 int route_id,
259 blink::WebPopupType popup_type) {
262 void TestWebContents::CreateNewFullscreenWidget(int render_process_id,
263 int route_id) {
266 void TestWebContents::ShowCreatedWindow(int route_id,
267 WindowOpenDisposition disposition,
268 const gfx::Rect& initial_pos,
269 bool user_gesture) {
272 void TestWebContents::ShowCreatedWidget(int route_id,
273 const gfx::Rect& initial_pos) {
276 void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
279 } // namespace content