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

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

Issue 730553002: PlzNavigate: Refactor unit test helper functions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Nasko's comments 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"
6 #include "base/guid.h"
7 #include "base/macros.h" 5 #include "base/macros.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/test/histogram_tester.h"
10 #include "base/time/time.h" 6 #include "base/time/time.h"
11 #include "content/browser/frame_host/navigation_controller_impl.h" 7 #include "content/browser/frame_host/navigation_controller_impl.h"
12 #include "content/browser/frame_host/navigation_entry_impl.h" 8 #include "content/browser/frame_host/navigation_entry_impl.h"
13 #include "content/browser/frame_host/navigation_request.h" 9 #include "content/browser/frame_host/navigation_request.h"
14 #include "content/browser/frame_host/navigation_request_info.h" 10 #include "content/browser/frame_host/navigation_request_info.h"
15 #include "content/browser/frame_host/navigator.h" 11 #include "content/browser/frame_host/navigator.h"
16 #include "content/browser/frame_host/navigator_impl.h" 12 #include "content/browser/frame_host/navigator_impl.h"
17 #include "content/browser/frame_host/render_frame_host_manager.h" 13 #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"
21 #include "content/browser/site_instance_impl.h" 14 #include "content/browser/site_instance_impl.h"
22 #include "content/browser/streams/stream.h" 15 #include "content/browser/streams/stream.h"
23 #include "content/browser/streams/stream_registry.h"
24 #include "content/common/navigation_params.h" 16 #include "content/common/navigation_params.h"
25 #include "content/public/browser/stream_handle.h" 17 #include "content/public/browser/stream_handle.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/common/url_constants.h" 18 #include "content/public/common/url_constants.h"
28 #include "content/public/common/url_utils.h" 19 #include "content/public/common/url_utils.h"
20 #include "content/test/browser_side_navigation_test_utils.h"
21 #include "content/test/test_navigation_url_loader.h"
29 #include "content/test/test_render_frame_host.h" 22 #include "content/test/test_render_frame_host.h"
30 #include "content/test/test_web_contents.h" 23 #include "content/test/test_web_contents.h"
31 #include "net/base/load_flags.h" 24 #include "net/base/load_flags.h"
32 #include "net/http/http_response_headers.h" 25 #include "net/http/http_response_headers.h"
33 #include "net/url_request/redirect_info.h" 26 #include "net/url_request/redirect_info.h"
34 #include "ui/base/page_transition_types.h" 27 #include "ui/base/page_transition_types.h"
35 #include "url/url_constants.h" 28 #include "url/url_constants.h"
36 29
37 namespace content { 30 namespace content {
38 31
39 namespace { 32 class NavigatorTestWithBrowserSideNavigation
40 33 : public RenderViewHostImplTestHarness {
41 class TestNavigationURLLoader
42 : public NavigationURLLoader,
43 public base::SupportsWeakPtr<TestNavigationURLLoader> {
44 public: 34 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
98 class NavigatorTest : public RenderViewHostImplTestHarness {
99 public:
100 NavigatorTest() : stream_registry_(new StreamRegistry) {}
101
102 void SetUp() override { 35 void SetUp() override {
103 RenderViewHostImplTestHarness::SetUp(); 36 RenderViewHostImplTestHarness::SetUp();
104 loader_factory_.reset(new TestNavigationURLLoaderFactory); 37 BrowserSideNavigationTestUtils::SetUp();
105 NavigationURLLoader::SetFactoryForTesting(loader_factory_.get()); 38 BrowserSideNavigationTestUtils::EnableBrowserSideNavigation();
106 } 39 }
107 40
108 void TearDown() override { 41 void TearDown() override {
109 NavigationURLLoader::SetFactoryForTesting(nullptr); 42 BrowserSideNavigationTestUtils::TearDown();
110 loader_factory_.reset();
111 RenderViewHostImplTestHarness::TearDown(); 43 RenderViewHostImplTestHarness::TearDown();
112 } 44 }
113 45
114 NavigationRequest* GetNavigationRequestForFrameTreeNode(
115 FrameTreeNode* frame_tree_node) const {
116 NavigatorImpl* navigator =
117 static_cast<NavigatorImpl*>(frame_tree_node->navigator());
118 return navigator->navigation_request_map_.get(
119 frame_tree_node->frame_tree_node_id());
120 }
121
122 TestNavigationURLLoader* GetLoaderForNavigationRequest( 46 TestNavigationURLLoader* GetLoaderForNavigationRequest(
123 NavigationRequest* request) const { 47 NavigationRequest* request) const {
124 return static_cast<TestNavigationURLLoader*>(request->loader_for_testing()); 48 return static_cast<TestNavigationURLLoader*>(request->loader_for_testing());
125 } 49 }
126 50
127 void EnableBrowserSideNavigation() {
128 CommandLine::ForCurrentProcess()->AppendSwitch(
129 switches::kEnableBrowserSideNavigation);
130 }
131
132 void SendRequestNavigation(FrameTreeNode* node, 51 void SendRequestNavigation(FrameTreeNode* node,
133 const GURL& url) { 52 const GURL& url) {
134 SendRequestNavigationWithParameters( 53 SendRequestNavigationWithParameters(
135 node, url, Referrer(), ui::PAGE_TRANSITION_LINK, 54 node, url, Referrer(), ui::PAGE_TRANSITION_LINK,
136 NavigationController::NO_RELOAD); 55 NavigationController::NO_RELOAD);
137 } 56 }
138 57
139 void SendRequestNavigationWithParameters( 58 void SendRequestNavigationWithParameters(
140 FrameTreeNode* node, 59 FrameTreeNode* node,
141 const GURL& url, 60 const GURL& url,
142 const Referrer& referrer, 61 const Referrer& referrer,
143 ui::PageTransition transition_type, 62 ui::PageTransition transition_type,
144 NavigationController::ReloadType reload_type) { 63 NavigationController::ReloadType reload_type) {
145 scoped_ptr<NavigationEntryImpl> entry( 64 scoped_ptr<NavigationEntryImpl> entry(
146 NavigationEntryImpl::FromNavigationEntry( 65 NavigationEntryImpl::FromNavigationEntry(
147 NavigationController::CreateNavigationEntry( 66 NavigationController::CreateNavigationEntry(
148 url, 67 url,
149 referrer, 68 referrer,
150 transition_type, 69 transition_type,
151 false, 70 false,
152 std::string(), 71 std::string(),
153 controller().GetBrowserContext()))); 72 controller().GetBrowserContext())));
154 static_cast<NavigatorImpl*>(node->navigator())->RequestNavigation( 73 static_cast<NavigatorImpl*>(node->navigator())->RequestNavigation(
155 node, *entry, reload_type, base::TimeTicks::Now()); 74 node, *entry, reload_type, base::TimeTicks::Now());
156 } 75 }
157 76
158 scoped_ptr<StreamHandle> MakeEmptyStream() { 77 NavigationRequest* GetNavigationRequestForFrameTreeNode(
159 GURL url(std::string(url::kBlobScheme) + "://" + base::GenerateGUID()); 78 FrameTreeNode* frame_tree_node) {
160 scoped_refptr<Stream> stream(new Stream(stream_registry_.get(), NULL, url)); 79 return static_cast<NavigatorImpl*>(frame_tree_node->navigator())
161 stream->Finalize(); 80 ->GetNavigationRequestForNodeForTesting(frame_tree_node);
162 return stream->CreateHandle();
163 } 81 }
164
165 private:
166 scoped_ptr<StreamRegistry> stream_registry_;
167 scoped_ptr<TestNavigationURLLoaderFactory> loader_factory_;
168 }; 82 };
169 83
170 // PlzNavigate: Test that a proper NavigationRequest is created by 84 // PlzNavigate: Test that a proper NavigationRequest is created by
171 // BeginNavigation. 85 // BeginNavigation.
172 // Note that all PlzNavigate methods on the browser side require the use of the 86 // Note that all PlzNavigate methods on the browser side require the use of the
173 // flag kEnableBrowserSideNavigation. 87 // flag kEnableBrowserSideNavigation.
174 TEST_F(NavigatorTest, BrowserSideNavigationBeginNavigation) { 88 TEST_F(NavigatorTestWithBrowserSideNavigation, BeginNavigation) {
175 const GURL kUrl1("http://www.google.com/"); 89 const GURL kUrl1("http://www.google.com/");
176 const GURL kUrl2("http://www.chromium.org/"); 90 const GURL kUrl2("http://www.chromium.org/");
177 const GURL kUrl3("http://www.gmail.com/"); 91 const GURL kUrl3("http://www.gmail.com/");
178 92
179 contents()->NavigateAndCommit(kUrl1); 93 contents()->NavigateAndCommit(kUrl1);
180 94
181 EnableBrowserSideNavigation();
182
183 // Add a subframe. 95 // Add a subframe.
184 FrameTreeNode* root = contents()->GetFrameTree()->root(); 96 FrameTreeNode* root = contents()->GetFrameTree()->root();
185 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>( 97 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>(
186 contents()->GetFrameTree()->AddFrame( 98 contents()->GetFrameTree()->AddFrame(
187 root, root->current_frame_host()->GetProcess()->GetID(), 14, 99 root, root->current_frame_host()->GetProcess()->GetID(), 14,
188 "Child")); 100 "Child"));
189 EXPECT_TRUE(subframe_rfh); 101 EXPECT_TRUE(subframe_rfh);
190 102
191 FrameTreeNode* subframe_node = subframe_rfh->frame_tree_node(); 103 FrameTreeNode* subframe_node = subframe_rfh->frame_tree_node();
192 SendRequestNavigation(subframe_rfh->frame_tree_node(), kUrl2); 104 SendRequestNavigation(subframe_rfh->frame_tree_node(), kUrl2);
(...skipping 20 matching lines...) Expand all
213 ASSERT_TRUE(main_request); 125 ASSERT_TRUE(main_request);
214 EXPECT_EQ(kUrl3, main_request->common_params().url); 126 EXPECT_EQ(kUrl3, main_request->common_params().url);
215 EXPECT_EQ(kUrl3, main_loader->common_params().url); 127 EXPECT_EQ(kUrl3, main_loader->common_params().url);
216 EXPECT_EQ(kUrl3, main_loader->request_info()->first_party_for_cookies); 128 EXPECT_EQ(kUrl3, main_loader->request_info()->first_party_for_cookies);
217 EXPECT_TRUE(main_loader->request_info()->is_main_frame); 129 EXPECT_TRUE(main_loader->request_info()->is_main_frame);
218 EXPECT_FALSE(main_loader->request_info()->parent_is_main_frame); 130 EXPECT_FALSE(main_loader->request_info()->parent_is_main_frame);
219 } 131 }
220 132
221 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that 133 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that
222 // RenderFrameHost is not modified when the navigation commits. 134 // RenderFrameHost is not modified when the navigation commits.
223 TEST_F(NavigatorTest, BrowserSideNavigationRequestNavigationNoLiveRenderer) { 135 TEST_F(NavigatorTestWithBrowserSideNavigation, NoLiveRenderer) {
224 const GURL kUrl("http://www.google.com/"); 136 const GURL kUrl("http://www.google.com/");
225 137
226 EnableBrowserSideNavigation();
227 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive()); 138 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive());
228 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 139 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
229 SendRequestNavigation(node, kUrl); 140 SendRequestNavigation(node, kUrl);
230 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); 141 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
231 // A NavigationRequest should have been generated. 142 // A NavigationRequest should have been generated.
232 EXPECT_TRUE(main_request != NULL); 143 EXPECT_TRUE(main_request != NULL);
233 RenderFrameHostImpl* rfh = main_test_rfh(); 144 RenderFrameHostImpl* rfh = main_test_rfh();
234 145
235 // Now return the response without any redirects. This will cause the 146 // Now return the response without any redirects. This will cause the
236 // navigation to commit at the same URL. 147 // navigation to commit at the same URL.
237 scoped_refptr<ResourceResponse> response(new ResourceResponse); 148 scoped_refptr<ResourceResponse> response(new ResourceResponse);
238 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( 149 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
239 response, MakeEmptyStream()); 150 response, BrowserSideNavigationTestUtils::MakeEmptyStream());
240 main_request = GetNavigationRequestForFrameTreeNode(node); 151 main_request = GetNavigationRequestForFrameTreeNode(node);
241 152
242 // The main RFH should not have been changed, and the renderer should have 153 // The main RFH should not have been changed, and the renderer should have
243 // been initialized. 154 // been initialized.
244 EXPECT_EQ(rfh, main_test_rfh()); 155 EXPECT_EQ(rfh, main_test_rfh());
245 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive()); 156 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive());
246 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive()); 157 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive());
247 } 158 }
248 159
249 // PlzNavigate: Test that commiting an HTTP 204 or HTTP 205 response cancels the 160 // PlzNavigate: Test that commiting an HTTP 204 or HTTP 205 response cancels the
250 // navigation. 161 // navigation.
251 TEST_F(NavigatorTest, BrowserSideNavigationNoContent) { 162 TEST_F(NavigatorTestWithBrowserSideNavigation, NoContent) {
252 const GURL kUrl1("http://www.chromium.org/"); 163 const GURL kUrl1("http://www.chromium.org/");
253 const GURL kUrl2("http://www.google.com/"); 164 const GURL kUrl2("http://www.google.com/");
254 165
255 // Load a URL. 166 // Load a URL.
256 contents()->NavigateAndCommit(kUrl1); 167 contents()->NavigateAndCommit(kUrl1);
257 RenderFrameHostImpl* rfh = main_test_rfh(); 168 RenderFrameHostImpl* rfh = main_test_rfh();
258 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); 169 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
259 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 170 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
260 171
261 EnableBrowserSideNavigation();
262
263 // Navigate to a different site. 172 // Navigate to a different site.
264 SendRequestNavigation(node, kUrl2); 173 SendRequestNavigation(node, kUrl2);
265 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 174 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
266 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); 175 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
267 ASSERT_TRUE(main_request); 176 ASSERT_TRUE(main_request);
268 177
269 // Commit an HTTP 204 response. 178 // Commit an HTTP 204 response.
270 scoped_refptr<ResourceResponse> response(new ResourceResponse); 179 scoped_refptr<ResourceResponse> response(new ResourceResponse);
271 const char kNoContentHeaders[] = "HTTP/1.1 204 No Content\0\0"; 180 const char kNoContentHeaders[] = "HTTP/1.1 204 No Content\0\0";
272 response->head.headers = new net::HttpResponseHeaders( 181 response->head.headers = new net::HttpResponseHeaders(
273 std::string(kNoContentHeaders, arraysize(kNoContentHeaders))); 182 std::string(kNoContentHeaders, arraysize(kNoContentHeaders)));
274 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( 183 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
275 response, MakeEmptyStream()); 184 response, BrowserSideNavigationTestUtils::MakeEmptyStream());
276 185
277 // There should be no pending RenderFrameHost; the navigation was aborted. 186 // There should be no pending RenderFrameHost; the navigation was aborted.
278 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); 187 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node));
279 EXPECT_FALSE(node->render_manager()->pending_frame_host()); 188 EXPECT_FALSE(node->render_manager()->pending_frame_host());
280 189
281 // Now, repeat the test with 205 Reset Content. 190 // Now, repeat the test with 205 Reset Content.
282 191
283 // Navigate to a different site again. 192 // Navigate to a different site again.
284 SendRequestNavigation(node, kUrl2); 193 SendRequestNavigation(node, kUrl2);
285 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 194 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
286 main_request = GetNavigationRequestForFrameTreeNode(node); 195 main_request = GetNavigationRequestForFrameTreeNode(node);
287 ASSERT_TRUE(main_request); 196 ASSERT_TRUE(main_request);
288 197
289 // Commit an HTTP 205 response. 198 // Commit an HTTP 205 response.
290 response = new ResourceResponse; 199 response = new ResourceResponse;
291 const char kResetContentHeaders[] = "HTTP/1.1 205 Reset Content\0\0"; 200 const char kResetContentHeaders[] = "HTTP/1.1 205 Reset Content\0\0";
292 response->head.headers = new net::HttpResponseHeaders( 201 response->head.headers = new net::HttpResponseHeaders(
293 std::string(kResetContentHeaders, arraysize(kResetContentHeaders))); 202 std::string(kResetContentHeaders, arraysize(kResetContentHeaders)));
294 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( 203 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
295 response, MakeEmptyStream()); 204 response, BrowserSideNavigationTestUtils::MakeEmptyStream());
296 205
297 // There should be no pending RenderFrameHost; the navigation was aborted. 206 // There should be no pending RenderFrameHost; the navigation was aborted.
298 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); 207 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node));
299 EXPECT_FALSE(node->render_manager()->pending_frame_host()); 208 EXPECT_FALSE(node->render_manager()->pending_frame_host());
300 } 209 }
301 210
302 // PlzNavigate: Test that a new RenderFrameHost is created when doing a cross 211 // PlzNavigate: Test that a new RenderFrameHost is created when doing a cross
303 // site navigation. 212 // site navigation.
304 TEST_F(NavigatorTest, BrowserSideNavigationCrossSiteNavigation) { 213 TEST_F(NavigatorTestWithBrowserSideNavigation, CrossSiteNavigation) {
305 const GURL kUrl1("http://www.chromium.org/"); 214 const GURL kUrl1("http://www.chromium.org/");
306 const GURL kUrl2("http://www.google.com/"); 215 const GURL kUrl2("http://www.google.com/");
307 216
308 contents()->NavigateAndCommit(kUrl1); 217 contents()->NavigateAndCommit(kUrl1);
309 RenderFrameHostImpl* rfh = main_test_rfh(); 218 RenderFrameHostImpl* rfh = main_test_rfh();
310 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); 219 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
311 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 220 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
312 221
313 EnableBrowserSideNavigation();
314
315 // Navigate to a different site. 222 // Navigate to a different site.
316 SendRequestNavigation(node, kUrl2); 223 SendRequestNavigation(node, kUrl2);
317 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 224 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
318 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); 225 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
319 ASSERT_TRUE(main_request); 226 ASSERT_TRUE(main_request);
320 227
321 scoped_refptr<ResourceResponse> response(new ResourceResponse); 228 scoped_refptr<ResourceResponse> response(new ResourceResponse);
322 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( 229 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
323 response, MakeEmptyStream()); 230 response, BrowserSideNavigationTestUtils::MakeEmptyStream());
324 RenderFrameHostImpl* pending_rfh = 231 RenderFrameHostImpl* pending_rfh =
325 node->render_manager()->pending_frame_host(); 232 node->render_manager()->pending_frame_host();
326 ASSERT_TRUE(pending_rfh); 233 ASSERT_TRUE(pending_rfh);
327 EXPECT_NE(pending_rfh, rfh); 234 EXPECT_NE(pending_rfh, rfh);
328 EXPECT_TRUE(pending_rfh->IsRenderFrameLive()); 235 EXPECT_TRUE(pending_rfh->IsRenderFrameLive());
329 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive()); 236 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive());
330 } 237 }
331 238
332 // PlzNavigate: Test that redirects are followed. 239 // PlzNavigate: Test that redirects are followed.
333 TEST_F(NavigatorTest, BrowserSideNavigationRedirectCrossSite) { 240 TEST_F(NavigatorTestWithBrowserSideNavigation, RedirectCrossSite) {
334 const GURL kUrl1("http://www.chromium.org/"); 241 const GURL kUrl1("http://www.chromium.org/");
335 const GURL kUrl2("http://www.google.com/"); 242 const GURL kUrl2("http://www.google.com/");
336 243
337 contents()->NavigateAndCommit(kUrl1); 244 contents()->NavigateAndCommit(kUrl1);
338 RenderFrameHostImpl* rfh = main_test_rfh(); 245 RenderFrameHostImpl* rfh = main_test_rfh();
339 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); 246 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
340 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 247 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
341 248
342 EnableBrowserSideNavigation();
343
344 // Navigate to a URL on the same site. 249 // Navigate to a URL on the same site.
345 SendRequestNavigation(node, kUrl1); 250 SendRequestNavigation(node, kUrl1);
346 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); 251 main_test_rfh()->SendBeginNavigationWithURL(kUrl1);
347 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); 252 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
348 ASSERT_TRUE(main_request); 253 ASSERT_TRUE(main_request);
349 254
350 // It then redirects to another site. 255 // It then redirects to another site.
351 net::RedirectInfo redirect_info; 256 net::RedirectInfo redirect_info;
352 redirect_info.status_code = 302; 257 redirect_info.status_code = 302;
353 redirect_info.new_method = "GET"; 258 redirect_info.new_method = "GET";
354 redirect_info.new_url = kUrl2; 259 redirect_info.new_url = kUrl2;
355 redirect_info.new_first_party_for_cookies = kUrl2; 260 redirect_info.new_first_party_for_cookies = kUrl2;
356 scoped_refptr<ResourceResponse> response(new ResourceResponse); 261 scoped_refptr<ResourceResponse> response(new ResourceResponse);
357 GetLoaderForNavigationRequest(main_request)->CallOnRequestRedirected( 262 GetLoaderForNavigationRequest(main_request)->CallOnRequestRedirected(
358 redirect_info, response); 263 redirect_info, response);
359 264
360 // The redirect should have been followed. 265 // The redirect should have been followed.
361 EXPECT_EQ(1, GetLoaderForNavigationRequest(main_request)->redirect_count()); 266 EXPECT_EQ(1, GetLoaderForNavigationRequest(main_request)->redirect_count());
362 267
363 // Then it commits. 268 // Then it commits.
364 response = new ResourceResponse; 269 response = new ResourceResponse;
365 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( 270 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
366 response, MakeEmptyStream()); 271 response, BrowserSideNavigationTestUtils::MakeEmptyStream());
367 RenderFrameHostImpl* pending_rfh = 272 RenderFrameHostImpl* pending_rfh =
368 node->render_manager()->pending_frame_host(); 273 node->render_manager()->pending_frame_host();
369 ASSERT_TRUE(pending_rfh); 274 ASSERT_TRUE(pending_rfh);
370 EXPECT_NE(pending_rfh, rfh); 275 EXPECT_NE(pending_rfh, rfh);
371 EXPECT_TRUE(pending_rfh->IsRenderFrameLive()); 276 EXPECT_TRUE(pending_rfh->IsRenderFrameLive());
372 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive()); 277 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive());
373 } 278 }
374 279
375 // PlzNavigate: Test that a navigation is cancelled if another request has been 280 // PlzNavigate: Test that a navigation is cancelled if another request has been
376 // issued in the meantime. 281 // issued in the meantime.
377 TEST_F(NavigatorTest, BrowserSideNavigationReplacePendingNavigation) { 282 TEST_F(NavigatorTestWithBrowserSideNavigation, ReplacePendingNavigation) {
378 const GURL kUrl0("http://www.wikipedia.org/"); 283 const GURL kUrl0("http://www.wikipedia.org/");
379 const GURL kUrl0_site = SiteInstance::GetSiteForURL(browser_context(), kUrl0); 284 const GURL kUrl0_site = SiteInstance::GetSiteForURL(browser_context(), kUrl0);
380 const GURL kUrl1("http://www.chromium.org/"); 285 const GURL kUrl1("http://www.chromium.org/");
381 const GURL kUrl2("http://www.google.com/"); 286 const GURL kUrl2("http://www.google.com/");
382 const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2); 287 const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2);
383 288
384 // Initialization. 289 // Initialization.
385 contents()->NavigateAndCommit(kUrl0); 290 contents()->NavigateAndCommit(kUrl0);
386 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 291 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
387 EnableBrowserSideNavigation();
388 EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL()); 292 EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL());
389 293
390 // Request navigation to the 1st URL. 294 // Request navigation to the 1st URL.
391 SendRequestNavigation(node, kUrl1); 295 SendRequestNavigation(node, kUrl1);
392 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); 296 main_test_rfh()->SendBeginNavigationWithURL(kUrl1);
393 NavigationRequest* request1 = GetNavigationRequestForFrameTreeNode(node); 297 NavigationRequest* request1 = GetNavigationRequestForFrameTreeNode(node);
394 ASSERT_TRUE(request1); 298 ASSERT_TRUE(request1);
395 EXPECT_EQ(kUrl1, request1->common_params().url); 299 EXPECT_EQ(kUrl1, request1->common_params().url);
396 base::WeakPtr<TestNavigationURLLoader> loader1 = 300 base::WeakPtr<TestNavigationURLLoader> loader1 =
397 GetLoaderForNavigationRequest(request1)->AsWeakPtr(); 301 GetLoaderForNavigationRequest(request1)->AsWeakPtr();
398 302
399 // Request navigation to the 2nd URL; the NavigationRequest must have been 303 // Request navigation to the 2nd URL; the NavigationRequest must have been
400 // replaced by a new one with a different URL. 304 // replaced by a new one with a different URL.
401 SendRequestNavigation(node, kUrl2); 305 SendRequestNavigation(node, kUrl2);
402 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); 306 main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
403 NavigationRequest* request2 = GetNavigationRequestForFrameTreeNode(node); 307 NavigationRequest* request2 = GetNavigationRequestForFrameTreeNode(node);
404 ASSERT_TRUE(request2); 308 ASSERT_TRUE(request2);
405 EXPECT_EQ(kUrl2, request2->common_params().url); 309 EXPECT_EQ(kUrl2, request2->common_params().url);
406 310
407 // Confirm that the first loader got destroyed. 311 // Confirm that the first loader got destroyed.
408 EXPECT_FALSE(loader1); 312 EXPECT_FALSE(loader1);
409 313
410 // Confirm that the commit corresponds to the new request. 314 // Confirm that the commit corresponds to the new request.
411 scoped_refptr<ResourceResponse> response(new ResourceResponse); 315 scoped_refptr<ResourceResponse> response(new ResourceResponse);
412 GetLoaderForNavigationRequest(request2)->CallOnResponseStarted( 316 GetLoaderForNavigationRequest(request2)->CallOnResponseStarted(
413 response, MakeEmptyStream()); 317 response, BrowserSideNavigationTestUtils::MakeEmptyStream());
414 RenderFrameHostImpl* pending_rfh = 318 RenderFrameHostImpl* pending_rfh =
415 node->render_manager()->pending_frame_host(); 319 node->render_manager()->pending_frame_host();
416 ASSERT_TRUE(pending_rfh); 320 ASSERT_TRUE(pending_rfh);
417 EXPECT_EQ(kUrl2_site, pending_rfh->GetSiteInstance()->GetSiteURL()); 321 EXPECT_EQ(kUrl2_site, pending_rfh->GetSiteInstance()->GetSiteURL());
418 } 322 }
419 323
420 // PlzNavigate: Tests that the navigation histograms are correctly tracked both
421 // when PlzNavigate is enabled and disabled, and also ignores in-tab renderer
422 // initiated navigation for the non-enabled case.
423 // Note: the related histogram, Navigation.TimeToURLJobStart, cannot be tracked
424 // by this test as the IO thread is not running.
425 TEST_F(NavigatorTest, BrowserSideNavigationHistogramTest) {
426 const GURL kUrl0("http://www.google.com/");
427 const GURL kUrl1("http://www.chromium.org/");
428 base::HistogramTester histo_tester;
429
430 // Performs a "normal" non-PlzNavigate navigation
431 contents()->NavigateAndCommit(kUrl0);
432 histo_tester.ExpectTotalCount("Navigation.TimeToCommit", 1);
433
434 // Performs an in-tab renderer initiated navigation
435 int32 new_page_id = 1 + contents()->GetMaxPageIDForSiteInstance(
436 main_test_rfh()->GetSiteInstance());
437 main_test_rfh()->SendNavigate(new_page_id, kUrl0);
438 histo_tester.ExpectTotalCount("Navigation.TimeToCommit", 1);
439
440 // Performs a PlzNavigate navigation
441 EnableBrowserSideNavigation();
442 contents()->NavigateAndCommit(kUrl1);
443 histo_tester.ExpectTotalCount("Navigation.TimeToCommit", 2);
444 }
445
446 // PlzNavigate: Test that a reload navigation is properly signaled to the 324 // PlzNavigate: Test that a reload navigation is properly signaled to the
447 // renderer when the navigation can commit. 325 // renderer when the navigation can commit.
448 TEST_F(NavigatorTest, BrowserSideNavigationReload) { 326 TEST_F(NavigatorTestWithBrowserSideNavigation, Reload) {
449 const GURL kUrl("http://www.google.com/"); 327 const GURL kUrl("http://www.google.com/");
450 contents()->NavigateAndCommit(kUrl); 328 contents()->NavigateAndCommit(kUrl);
451 329
452 EnableBrowserSideNavigation();
453 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); 330 FrameTreeNode* node = main_test_rfh()->frame_tree_node();
454 SendRequestNavigationWithParameters( 331 SendRequestNavigationWithParameters(
455 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK, 332 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK,
456 NavigationController::RELOAD); 333 NavigationController::RELOAD);
457 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl); 334 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl);
458 // A NavigationRequest should have been generated. 335 // A NavigationRequest should have been generated.
459 NavigationRequest* main_request = 336 NavigationRequest* main_request =
460 GetNavigationRequestForFrameTreeNode(node); 337 GetNavigationRequestForFrameTreeNode(node);
461 ASSERT_TRUE(main_request != NULL); 338 ASSERT_TRUE(main_request != NULL);
462 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD, 339 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD,
463 main_request->common_params().navigation_type); 340 main_request->common_params().navigation_type);
464 int page_id = contents()->GetMaxPageIDForSiteInstance( 341 int page_id = contents()->GetMaxPageIDForSiteInstance(
465 main_test_rfh()->GetSiteInstance()) + 1; 342 main_test_rfh()->GetSiteInstance()) + 1;
466 main_test_rfh()->SendNavigate(page_id, kUrl); 343 main_test_rfh()->SendNavigate(page_id, kUrl);
467 344
468 // Now do a shift+reload. 345 // Now do a shift+reload.
469 SendRequestNavigationWithParameters( 346 SendRequestNavigationWithParameters(
470 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK, 347 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK,
471 NavigationController::RELOAD_IGNORING_CACHE); 348 NavigationController::RELOAD_IGNORING_CACHE);
472 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl); 349 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl);
473 // A NavigationRequest should have been generated. 350 // A NavigationRequest should have been generated.
474 main_request = GetNavigationRequestForFrameTreeNode(node); 351 main_request = GetNavigationRequestForFrameTreeNode(node);
475 ASSERT_TRUE(main_request != NULL); 352 ASSERT_TRUE(main_request != NULL);
476 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE, 353 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE,
477 main_request->common_params().navigation_type); 354 main_request->common_params().navigation_type);
478 } 355 }
479 356
480 } // namespace content 357 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698