Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: content/browser/frame_host/navigator_impl_unittest.cc

Issue 648813002: PlzNavigate: Add first version of NavigationURLLoader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@plz-navigate-prepare
Patch Set: rebase Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/guid.h" 6 #include "base/guid.h"
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/weak_ptr.h"
8 #include "base/test/histogram_tester.h" 9 #include "base/test/histogram_tester.h"
9 #include "base/time/time.h" 10 #include "base/time/time.h"
10 #include "content/browser/frame_host/navigation_controller_impl.h" 11 #include "content/browser/frame_host/navigation_controller_impl.h"
11 #include "content/browser/frame_host/navigation_entry_impl.h" 12 #include "content/browser/frame_host/navigation_entry_impl.h"
12 #include "content/browser/frame_host/navigation_request.h" 13 #include "content/browser/frame_host/navigation_request.h"
13 #include "content/browser/frame_host/navigation_request_info.h" 14 #include "content/browser/frame_host/navigation_request_info.h"
14 #include "content/browser/frame_host/navigator.h" 15 #include "content/browser/frame_host/navigator.h"
15 #include "content/browser/frame_host/navigator_impl.h" 16 #include "content/browser/frame_host/navigator_impl.h"
16 #include "content/browser/frame_host/render_frame_host_manager.h" 17 #include "content/browser/frame_host/render_frame_host_manager.h"
18 #include "content/browser/loader/navigation_url_loader.h"
19 #include "content/browser/loader/navigation_url_loader_delegate.h"
20 #include "content/browser/loader/navigation_url_loader_factory.h"
17 #include "content/browser/site_instance_impl.h" 21 #include "content/browser/site_instance_impl.h"
18 #include "content/browser/streams/stream.h" 22 #include "content/browser/streams/stream.h"
19 #include "content/browser/streams/stream_registry.h" 23 #include "content/browser/streams/stream_registry.h"
20 #include "content/common/navigation_params.h" 24 #include "content/common/navigation_params.h"
21 #include "content/public/browser/stream_handle.h" 25 #include "content/public/browser/stream_handle.h"
22 #include "content/public/common/content_switches.h" 26 #include "content/public/common/content_switches.h"
23 #include "content/public/common/url_constants.h" 27 #include "content/public/common/url_constants.h"
24 #include "content/public/common/url_utils.h" 28 #include "content/public/common/url_utils.h"
25 #include "content/test/test_render_frame_host.h" 29 #include "content/test/test_render_frame_host.h"
26 #include "content/test/test_web_contents.h" 30 #include "content/test/test_web_contents.h"
27 #include "net/base/load_flags.h" 31 #include "net/base/load_flags.h"
28 #include "net/http/http_response_headers.h" 32 #include "net/http/http_response_headers.h"
33 #include "net/url_request/redirect_info.h"
29 #include "ui/base/page_transition_types.h" 34 #include "ui/base/page_transition_types.h"
30 #include "url/url_constants.h" 35 #include "url/url_constants.h"
31 36
32 namespace content { 37 namespace content {
33 38
39 namespace {
40
41 class TestNavigationURLLoader
42 : public NavigationURLLoader,
43 public base::SupportsWeakPtr<TestNavigationURLLoader> {
44 public:
45 TestNavigationURLLoader(const CommonNavigationParams& common_params,
46 scoped_ptr<NavigationRequestInfo> request_info,
47 NavigationURLLoaderDelegate* delegate)
48 : common_params_(common_params),
49 request_info_(request_info.Pass()),
50 delegate_(delegate),
51 redirect_count_(0) {
52 }
53
54 // NavigationURLLoader implementation.
55 void FollowRedirect() override { redirect_count_++; }
56
57 const CommonNavigationParams& common_params() const { return common_params_; }
58 NavigationRequestInfo* request_info() const { return request_info_.get(); }
59
60 void CallOnRequestRedirected(
61 const net::RedirectInfo& redirect_info,
62 const scoped_refptr<ResourceResponse>& response) {
63 delegate_->OnRequestRedirected(redirect_info, response);
64 }
65
66 void CallOnResponseStarted(
67 const scoped_refptr<ResourceResponse>& response,
68 scoped_ptr<StreamHandle> body) {
69 delegate_->OnResponseStarted(response, body.Pass());
70 }
71
72 int redirect_count() { return redirect_count_; }
73
74 private:
75 CommonNavigationParams common_params_;
76 scoped_ptr<NavigationRequestInfo> request_info_;
77 NavigationURLLoaderDelegate* delegate_;
78 int redirect_count_;
79 };
80
81 class TestNavigationURLLoaderFactory : public NavigationURLLoaderFactory {
82 public:
83 // NavigationURLLoaderFactory implementation.
84 scoped_ptr<NavigationURLLoader> CreateLoader(
85 BrowserContext* browser_context,
86 int64 frame_tree_node_id,
87 const CommonNavigationParams& common_params,
88 scoped_ptr<NavigationRequestInfo> request_info,
89 ResourceRequestBody* request_body,
90 NavigationURLLoaderDelegate* delegate) override {
91 return scoped_ptr<NavigationURLLoader>(new TestNavigationURLLoader(
92 common_params, request_info.Pass(), delegate));
93 }
94 };
95
96 } // namespace
97
34 class NavigatorTest : public RenderViewHostImplTestHarness { 98 class NavigatorTest : public RenderViewHostImplTestHarness {
35 public: 99 public:
36 NavigatorTest() : stream_registry_(new StreamRegistry) {} 100 NavigatorTest() : stream_registry_(new StreamRegistry) {}
37 101
102 void SetUp() override {
103 RenderViewHostImplTestHarness::SetUp();
104 loader_factory_.reset(new TestNavigationURLLoaderFactory);
105 NavigationURLLoader::SetFactoryForTesting(loader_factory_.get());
106 }
107
108 void TearDown() override {
109 NavigationURLLoader::SetFactoryForTesting(nullptr);
110 loader_factory_.reset();
111 RenderViewHostImplTestHarness::TearDown();
112 }
113
38 NavigationRequest* GetNavigationRequestForFrameTreeNode( 114 NavigationRequest* GetNavigationRequestForFrameTreeNode(
39 FrameTreeNode* frame_tree_node) const { 115 FrameTreeNode* frame_tree_node) const {
40 NavigatorImpl* navigator = 116 NavigatorImpl* navigator =
41 static_cast<NavigatorImpl*>(frame_tree_node->navigator()); 117 static_cast<NavigatorImpl*>(frame_tree_node->navigator());
42 return navigator->navigation_request_map_.get( 118 return navigator->navigation_request_map_.get(
43 frame_tree_node->frame_tree_node_id()); 119 frame_tree_node->frame_tree_node_id());
44 } 120 }
45 121
122 TestNavigationURLLoader* GetLoaderForNavigationRequest(
123 NavigationRequest* request) const {
124 return static_cast<TestNavigationURLLoader*>(request->loader_for_testing());
125 }
126
46 void EnableBrowserSideNavigation() { 127 void EnableBrowserSideNavigation() {
47 CommandLine::ForCurrentProcess()->AppendSwitch( 128 CommandLine::ForCurrentProcess()->AppendSwitch(
48 switches::kEnableBrowserSideNavigation); 129 switches::kEnableBrowserSideNavigation);
49 } 130 }
50 131
51 void SendRequestNavigation(FrameTreeNode* node, 132 void SendRequestNavigation(FrameTreeNode* node,
52 const GURL& url) { 133 const GURL& url) {
53 SendRequestNavigationWithParameters( 134 SendRequestNavigationWithParameters(
54 node, url, Referrer(), ui::PAGE_TRANSITION_LINK, 135 node, url, Referrer(), ui::PAGE_TRANSITION_LINK,
55 NavigationController::NO_RELOAD); 136 NavigationController::NO_RELOAD);
(...skipping 20 matching lines...) Expand all
76 157
77 scoped_ptr<StreamHandle> MakeEmptyStream() { 158 scoped_ptr<StreamHandle> MakeEmptyStream() {
78 GURL url(std::string(url::kBlobScheme) + "://" + base::GenerateGUID()); 159 GURL url(std::string(url::kBlobScheme) + "://" + base::GenerateGUID());
79 scoped_refptr<Stream> stream(new Stream(stream_registry_.get(), NULL, url)); 160 scoped_refptr<Stream> stream(new Stream(stream_registry_.get(), NULL, url));
80 stream->Finalize(); 161 stream->Finalize();
81 return stream->CreateHandle(); 162 return stream->CreateHandle();
82 } 163 }
83 164
84 private: 165 private:
85 scoped_ptr<StreamRegistry> stream_registry_; 166 scoped_ptr<StreamRegistry> stream_registry_;
167 scoped_ptr<TestNavigationURLLoaderFactory> loader_factory_;
86 }; 168 };
87 169
88 // PlzNavigate: Test that a proper NavigationRequest is created by 170 // PlzNavigate: Test that a proper NavigationRequest is created by
89 // BeginNavigation. 171 // BeginNavigation.
90 // Note that all PlzNavigate methods on the browser side require the use of the 172 // Note that all PlzNavigate methods on the browser side require the use of the
91 // flag kEnableBrowserSideNavigation. 173 // flag kEnableBrowserSideNavigation.
92 TEST_F(NavigatorTest, BrowserSideNavigationBeginNavigation) { 174 TEST_F(NavigatorTest, BrowserSideNavigationBeginNavigation) {
93 const GURL kUrl1("http://www.google.com/"); 175 const GURL kUrl1("http://www.google.com/");
94 const GURL kUrl2("http://www.chromium.org/"); 176 const GURL kUrl2("http://www.chromium.org/");
95 const GURL kUrl3("http://www.gmail.com/"); 177 const GURL kUrl3("http://www.gmail.com/");
96 178
97 contents()->NavigateAndCommit(kUrl1); 179 contents()->NavigateAndCommit(kUrl1);
98 180
99 EnableBrowserSideNavigation(); 181 EnableBrowserSideNavigation();
100 182
101 // Add a subframe. 183 // Add a subframe.
102 FrameTreeNode* root = contents()->GetFrameTree()->root(); 184 FrameTreeNode* root = contents()->GetFrameTree()->root();
103 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>( 185 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>(
104 contents()->GetFrameTree()->AddFrame( 186 contents()->GetFrameTree()->AddFrame(
105 root, root->current_frame_host()->GetProcess()->GetID(), 14, 187 root, root->current_frame_host()->GetProcess()->GetID(), 14,
106 "Child")); 188 "Child"));
107 EXPECT_TRUE(subframe_rfh); 189 EXPECT_TRUE(subframe_rfh);
108 190
109 FrameTreeNode* subframe_node = subframe_rfh->frame_tree_node(); 191 FrameTreeNode* subframe_node = subframe_rfh->frame_tree_node();
110 SendRequestNavigation(subframe_rfh->frame_tree_node(), kUrl2); 192 SendRequestNavigation(subframe_rfh->frame_tree_node(), kUrl2);
111 // There is no previous renderer in the subframe, so BeginNavigation is 193 // There is no previous renderer in the subframe, so BeginNavigation is
112 // handled already. 194 // handled already.
113 NavigationRequest* subframe_request = 195 NavigationRequest* subframe_request =
114 GetNavigationRequestForFrameTreeNode(subframe_node); 196 GetNavigationRequestForFrameTreeNode(subframe_node);
197 TestNavigationURLLoader* subframe_loader =
198 GetLoaderForNavigationRequest(subframe_request);
115 ASSERT_TRUE(subframe_request); 199 ASSERT_TRUE(subframe_request);
116 EXPECT_EQ(kUrl2, subframe_request->common_params().url); 200 EXPECT_EQ(kUrl2, subframe_request->common_params().url);
201 EXPECT_EQ(kUrl2, subframe_loader->common_params().url);
117 // First party for cookies url should be that of the main frame. 202 // First party for cookies url should be that of the main frame.
118 EXPECT_EQ(kUrl1, subframe_request->info_for_test()->first_party_for_cookies); 203 EXPECT_EQ(kUrl1, subframe_loader->request_info()->first_party_for_cookies);
119 EXPECT_FALSE(subframe_request->info_for_test()->is_main_frame); 204 EXPECT_FALSE(subframe_loader->request_info()->is_main_frame);
120 EXPECT_TRUE(subframe_request->info_for_test()->parent_is_main_frame); 205 EXPECT_TRUE(subframe_loader->request_info()->parent_is_main_frame);
121 206
122 SendRequestNavigation(root, kUrl3); 207 SendRequestNavigation(root, kUrl3);
123 // Simulate a BeginNavigation IPC on the main frame. 208 // Simulate a BeginNavigation IPC on the main frame.
124 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl3); 209 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl3);
125 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(root); 210 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(root);
211 TestNavigationURLLoader* main_loader =
212 GetLoaderForNavigationRequest(main_request);
126 ASSERT_TRUE(main_request); 213 ASSERT_TRUE(main_request);
127 EXPECT_EQ(kUrl3, main_request->common_params().url); 214 EXPECT_EQ(kUrl3, main_request->common_params().url);
128 EXPECT_EQ(kUrl3, main_request->info_for_test()->first_party_for_cookies); 215 EXPECT_EQ(kUrl3, main_loader->common_params().url);
129 EXPECT_TRUE(main_request->info_for_test()->is_main_frame); 216 EXPECT_EQ(kUrl3, main_loader->request_info()->first_party_for_cookies);
130 EXPECT_FALSE(main_request->info_for_test()->parent_is_main_frame); 217 EXPECT_TRUE(main_loader->request_info()->is_main_frame);
218 EXPECT_FALSE(main_loader->request_info()->parent_is_main_frame);
131 } 219 }
132 220
133 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that 221 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that
134 // RenderFrameHost is not modified when the navigation commits. 222 // RenderFrameHost is not modified when the navigation commits.
135 TEST_F(NavigatorTest, BrowserSideNavigationRequestNavigationNoLiveRenderer) { 223 TEST_F(NavigatorTest, BrowserSideNavigationRequestNavigationNoLiveRenderer) {
136 const GURL kUrl("http://www.google.com/"); 224 const GURL kUrl("http://www.google.com/");
137 225
138 EnableBrowserSideNavigation(); 226 EnableBrowserSideNavigation();
139 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive()); 227 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive());
140 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 228 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
141 SendRequestNavigation(node, kUrl); 229 SendRequestNavigation(node, kUrl);
142 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); 230 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
143 // A NavigationRequest should have been generated. 231 // A NavigationRequest should have been generated.
144 EXPECT_TRUE(main_request != NULL); 232 EXPECT_TRUE(main_request != NULL);
145 RenderFrameHostImpl* rfh = main_test_rfh(); 233 RenderFrameHostImpl* rfh = main_test_rfh();
146 234
147 // Now commit the same url. 235 // Now return the response without any redirects. This will cause the
236 // navigation to commit at the same URL.
148 scoped_refptr<ResourceResponse> response(new ResourceResponse); 237 scoped_refptr<ResourceResponse> response(new ResourceResponse);
149 node->navigator()->CommitNavigation(node, response.get(), MakeEmptyStream()); 238 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
239 response, MakeEmptyStream());
150 main_request = GetNavigationRequestForFrameTreeNode(node); 240 main_request = GetNavigationRequestForFrameTreeNode(node);
151 241
152 // The main RFH should not have been changed, and the renderer should have 242 // The main RFH should not have been changed, and the renderer should have
153 // been initialized. 243 // been initialized.
154 EXPECT_EQ(rfh, main_test_rfh()); 244 EXPECT_EQ(rfh, main_test_rfh());
155 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive()); 245 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive());
156 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive()); 246 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive());
157 } 247 }
158 248
159 // PlzNavigate: Test that commiting an HTTP 204 or HTTP 205 response cancels the 249 // PlzNavigate: Test that commiting an HTTP 204 or HTTP 205 response cancels the
(...skipping 14 matching lines...) Expand all
174 SendRequestNavigation(node, kUrl2); 264 SendRequestNavigation(node, kUrl2);
175 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 265 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
176 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); 266 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
177 ASSERT_TRUE(main_request); 267 ASSERT_TRUE(main_request);
178 268
179 // Commit an HTTP 204 response. 269 // Commit an HTTP 204 response.
180 scoped_refptr<ResourceResponse> response(new ResourceResponse); 270 scoped_refptr<ResourceResponse> response(new ResourceResponse);
181 const char kNoContentHeaders[] = "HTTP/1.1 204 No Content\0\0"; 271 const char kNoContentHeaders[] = "HTTP/1.1 204 No Content\0\0";
182 response->head.headers = new net::HttpResponseHeaders( 272 response->head.headers = new net::HttpResponseHeaders(
183 std::string(kNoContentHeaders, arraysize(kNoContentHeaders))); 273 std::string(kNoContentHeaders, arraysize(kNoContentHeaders)));
184 node->navigator()->CommitNavigation(node, response.get(), MakeEmptyStream()); 274 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
275 response, MakeEmptyStream());
185 276
186 // There should be no pending RenderFrameHost; the navigation was aborted. 277 // There should be no pending RenderFrameHost; the navigation was aborted.
187 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); 278 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node));
188 EXPECT_FALSE(node->render_manager()->pending_frame_host()); 279 EXPECT_FALSE(node->render_manager()->pending_frame_host());
189 280
190 // Now, repeat the test with 205 Reset Content. 281 // Now, repeat the test with 205 Reset Content.
191 282
192 // Navigate to a different site again. 283 // Navigate to a different site again.
193 SendRequestNavigation(node, kUrl2); 284 SendRequestNavigation(node, kUrl2);
194 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 285 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
195 main_request = GetNavigationRequestForFrameTreeNode(node); 286 main_request = GetNavigationRequestForFrameTreeNode(node);
196 ASSERT_TRUE(main_request); 287 ASSERT_TRUE(main_request);
197 288
198 // Commit an HTTP 205 response. 289 // Commit an HTTP 205 response.
199 response = new ResourceResponse; 290 response = new ResourceResponse;
200 const char kResetContentHeaders[] = "HTTP/1.1 205 Reset Content\0\0"; 291 const char kResetContentHeaders[] = "HTTP/1.1 205 Reset Content\0\0";
201 response->head.headers = new net::HttpResponseHeaders( 292 response->head.headers = new net::HttpResponseHeaders(
202 std::string(kResetContentHeaders, arraysize(kResetContentHeaders))); 293 std::string(kResetContentHeaders, arraysize(kResetContentHeaders)));
203 node->navigator()->CommitNavigation(node, response.get(), MakeEmptyStream()); 294 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
295 response, MakeEmptyStream());
204 296
205 // There should be no pending RenderFrameHost; the navigation was aborted. 297 // There should be no pending RenderFrameHost; the navigation was aborted.
206 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); 298 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node));
207 EXPECT_FALSE(node->render_manager()->pending_frame_host()); 299 EXPECT_FALSE(node->render_manager()->pending_frame_host());
208 } 300 }
209 301
210 // PlzNavigate: Test that a new RenderFrameHost is created when doing a cross 302 // PlzNavigate: Test that a new RenderFrameHost is created when doing a cross
211 // site navigation. 303 // site navigation.
212 TEST_F(NavigatorTest, BrowserSideNavigationCrossSiteNavigation) { 304 TEST_F(NavigatorTest, BrowserSideNavigationCrossSiteNavigation) {
213 const GURL kUrl1("http://www.chromium.org/"); 305 const GURL kUrl1("http://www.chromium.org/");
214 const GURL kUrl2("http://www.google.com/"); 306 const GURL kUrl2("http://www.google.com/");
215 307
216 contents()->NavigateAndCommit(kUrl1); 308 contents()->NavigateAndCommit(kUrl1);
217 RenderFrameHostImpl* rfh = main_test_rfh(); 309 RenderFrameHostImpl* rfh = main_test_rfh();
218 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); 310 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
219 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 311 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
220 312
221 EnableBrowserSideNavigation(); 313 EnableBrowserSideNavigation();
222 314
223 // Navigate to a different site. 315 // Navigate to a different site.
224 SendRequestNavigation(node, kUrl2); 316 SendRequestNavigation(node, kUrl2);
225 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 317 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
226 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); 318 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
227 ASSERT_TRUE(main_request); 319 ASSERT_TRUE(main_request);
228 320
229 scoped_refptr<ResourceResponse> response(new ResourceResponse); 321 scoped_refptr<ResourceResponse> response(new ResourceResponse);
230 node->navigator()->CommitNavigation(node, response.get(), MakeEmptyStream()); 322 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
323 response, MakeEmptyStream());
231 RenderFrameHostImpl* pending_rfh = 324 RenderFrameHostImpl* pending_rfh =
232 node->render_manager()->pending_frame_host(); 325 node->render_manager()->pending_frame_host();
233 ASSERT_TRUE(pending_rfh); 326 ASSERT_TRUE(pending_rfh);
327 EXPECT_NE(pending_rfh, rfh);
328 EXPECT_TRUE(pending_rfh->IsRenderFrameLive());
329 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive());
330 }
331
332 // PlzNavigate: Test that redirects are followed.
333 TEST_F(NavigatorTest, BrowserSideNavigationRedirectCrossSite) {
334 const GURL kUrl1("http://www.chromium.org/");
335 const GURL kUrl2("http://www.google.com/");
336
337 contents()->NavigateAndCommit(kUrl1);
338 RenderFrameHostImpl* rfh = main_test_rfh();
339 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
340 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
341
342 EnableBrowserSideNavigation();
343
344 // Navigate to a URL on the same site.
345 SendRequestNavigation(node, kUrl1);
346 main_test_rfh()->SendBeginNavigationWithURL(kUrl1);
347 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
348 ASSERT_TRUE(main_request);
349
350 // It then redirects to another site.
351 net::RedirectInfo redirect_info;
352 redirect_info.status_code = 302;
353 redirect_info.new_method = "GET";
354 redirect_info.new_url = kUrl2;
355 redirect_info.new_first_party_for_cookies = kUrl2;
356 scoped_refptr<ResourceResponse> response(new ResourceResponse);
357 GetLoaderForNavigationRequest(main_request)->CallOnRequestRedirected(
358 redirect_info, response);
359
360 // The redirect should have been followed.
361 EXPECT_EQ(1, GetLoaderForNavigationRequest(main_request)->redirect_count());
362
363 // Then it commits.
364 response = new ResourceResponse;
365 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
366 response, MakeEmptyStream());
367 RenderFrameHostImpl* pending_rfh =
368 node->render_manager()->pending_frame_host();
369 ASSERT_TRUE(pending_rfh);
234 EXPECT_NE(pending_rfh, rfh); 370 EXPECT_NE(pending_rfh, rfh);
235 EXPECT_TRUE(pending_rfh->IsRenderFrameLive()); 371 EXPECT_TRUE(pending_rfh->IsRenderFrameLive());
236 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive()); 372 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive());
237 } 373 }
238 374
239 // PlzNavigate: Test that a navigation is cancelled if another request has been 375 // PlzNavigate: Test that a navigation is cancelled if another request has been
240 // issued in the meantime. 376 // issued in the meantime.
241 TEST_F(NavigatorTest, BrowserSideNavigationReplacePendingNavigation) { 377 TEST_F(NavigatorTest, BrowserSideNavigationReplacePendingNavigation) {
242 const GURL kUrl0("http://www.wikipedia.org/"); 378 const GURL kUrl0("http://www.wikipedia.org/");
243 const GURL kUrl0_site = SiteInstance::GetSiteForURL(browser_context(), kUrl0); 379 const GURL kUrl0_site = SiteInstance::GetSiteForURL(browser_context(), kUrl0);
244 const GURL kUrl1("http://www.chromium.org/"); 380 const GURL kUrl1("http://www.chromium.org/");
245 const GURL kUrl2("http://www.google.com/"); 381 const GURL kUrl2("http://www.google.com/");
246 const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2); 382 const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2);
247 383
248 // Initialization. 384 // Initialization.
249 contents()->NavigateAndCommit(kUrl0); 385 contents()->NavigateAndCommit(kUrl0);
250 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 386 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
251 EnableBrowserSideNavigation(); 387 EnableBrowserSideNavigation();
252 EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL()); 388 EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL());
253 389
254 // Request navigation to the 1st URL. 390 // Request navigation to the 1st URL.
255 SendRequestNavigation(node, kUrl1); 391 SendRequestNavigation(node, kUrl1);
256 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); 392 main_test_rfh()->SendBeginNavigationWithURL(kUrl1);
257 NavigationRequest* request1 = GetNavigationRequestForFrameTreeNode(node); 393 NavigationRequest* request1 = GetNavigationRequestForFrameTreeNode(node);
258 ASSERT_TRUE(request1); 394 ASSERT_TRUE(request1);
259 EXPECT_EQ(kUrl1, request1->common_params().url); 395 EXPECT_EQ(kUrl1, request1->common_params().url);
396 base::WeakPtr<TestNavigationURLLoader> loader1 =
397 GetLoaderForNavigationRequest(request1)->AsWeakPtr();
260 398
261 // Request navigation to the 2nd URL; the NavigationRequest must have been 399 // Request navigation to the 2nd URL; the NavigationRequest must have been
262 // replaced by a new one with a different URL. 400 // replaced by a new one with a different URL.
263 SendRequestNavigation(node, kUrl2); 401 SendRequestNavigation(node, kUrl2);
264 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 402 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
265 NavigationRequest* request2 = GetNavigationRequestForFrameTreeNode(node); 403 NavigationRequest* request2 = GetNavigationRequestForFrameTreeNode(node);
266 ASSERT_TRUE(request2); 404 ASSERT_TRUE(request2);
267 EXPECT_EQ(kUrl2, request2->common_params().url); 405 EXPECT_EQ(kUrl2, request2->common_params().url);
268 406
269 // Confirm that the commit corresonds to the new request. 407 // Confirm that the first loader got destroyed.
408 EXPECT_FALSE(loader1);
409
410 // Confirm that the commit corresponds to the new request.
270 scoped_refptr<ResourceResponse> response(new ResourceResponse); 411 scoped_refptr<ResourceResponse> response(new ResourceResponse);
271 node->navigator()->CommitNavigation(node, response.get(), MakeEmptyStream()); 412 GetLoaderForNavigationRequest(request2)->CallOnResponseStarted(
413 response, MakeEmptyStream());
272 RenderFrameHostImpl* pending_rfh = 414 RenderFrameHostImpl* pending_rfh =
273 node->render_manager()->pending_frame_host(); 415 node->render_manager()->pending_frame_host();
274 ASSERT_TRUE(pending_rfh); 416 ASSERT_TRUE(pending_rfh);
275 EXPECT_EQ(kUrl2_site, pending_rfh->GetSiteInstance()->GetSiteURL()); 417 EXPECT_EQ(kUrl2_site, pending_rfh->GetSiteInstance()->GetSiteURL());
276 } 418 }
277 419
278 // PlzNavigate: Tests that the navigation histograms are correctly tracked both 420 // PlzNavigate: Tests that the navigation histograms are correctly tracked both
279 // when PlzNavigate is enabled and disabled, and also ignores in-tab renderer 421 // when PlzNavigate is enabled and disabled, and also ignores in-tab renderer
280 // initiated navigation for the non-enabled case. 422 // initiated navigation for the non-enabled case.
281 // Note: the related histogram, Navigation.TimeToURLJobStart, cannot be tracked 423 // Note: the related histogram, Navigation.TimeToURLJobStart, cannot be tracked
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 NavigationController::RELOAD_IGNORING_CACHE); 471 NavigationController::RELOAD_IGNORING_CACHE);
330 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl); 472 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl);
331 // A NavigationRequest should have been generated. 473 // A NavigationRequest should have been generated.
332 main_request = GetNavigationRequestForFrameTreeNode(node); 474 main_request = GetNavigationRequestForFrameTreeNode(node);
333 ASSERT_TRUE(main_request != NULL); 475 ASSERT_TRUE(main_request != NULL);
334 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE, 476 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE,
335 main_request->common_params().navigation_type); 477 main_request->common_params().navigation_type);
336 } 478 }
337 479
338 } // namespace content 480 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/frame_host/navigation_request_info.h ('k') | content/browser/loader/navigation_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698