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

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

Issue 1811913003: PlzNavigate: support NavigationThrottle::WillProcessResponse (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase on top of 1832803002 Created 4 years, 9 months 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/frame_host/navigation_handle_impl.h" 5 #include "content/browser/frame_host/navigation_handle_impl.h"
6 #include "content/browser/web_contents/web_contents_impl.h" 6 #include "content/browser/web_contents/web_contents_impl.h"
7 #include "content/public/browser/web_contents.h" 7 #include "content/public/browser/web_contents.h"
8 #include "content/public/browser/web_contents_observer.h" 8 #include "content/public/browser/web_contents_observer.h"
9 #include "content/public/test/browser_test_utils.h" 9 #include "content/public/test/browser_test_utils.h"
10 #include "content/public/test/content_browser_test.h" 10 #include "content/public/test/content_browser_test.h"
11 #include "content/public/test/content_browser_test_utils.h" 11 #include "content/public/test/content_browser_test_utils.h"
12 #include "content/public/test/test_navigation_observer.h"
13 #include "content/public/test/test_utils.h"
12 #include "content/shell/browser/shell.h" 14 #include "content/shell/browser/shell.h"
13 #include "net/dns/mock_host_resolver.h" 15 #include "net/dns/mock_host_resolver.h"
14 #include "ui/base/page_transition_types.h" 16 #include "ui/base/page_transition_types.h"
15 #include "url/url_constants.h" 17 #include "url/url_constants.h"
16 18
17 namespace content { 19 namespace content {
18 20
19 namespace { 21 namespace {
20 22
21 class NavigationHandleObserver : public WebContentsObserver { 23 class NavigationHandleObserver : public WebContentsObserver {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 bool is_parent_main_frame_; 106 bool is_parent_main_frame_;
105 bool is_synchronous_; 107 bool is_synchronous_;
106 bool is_srcdoc_; 108 bool is_srcdoc_;
107 bool was_redirected_; 109 bool was_redirected_;
108 int frame_tree_node_id_; 110 int frame_tree_node_id_;
109 ui::PageTransition page_transition_; 111 ui::PageTransition page_transition_;
110 GURL expected_url_; 112 GURL expected_url_;
111 GURL last_committed_url_; 113 GURL last_committed_url_;
112 }; 114 };
113 115
116 // A test NavigationThrottle that will return pre-determined checks and run
117 // callbacks when the various NavigationThrottle methods are called.
118 class TestNavigationThrottle : public NavigationThrottle {
119 public:
120 TestNavigationThrottle(
121 NavigationHandle* handle,
122 NavigationThrottle::ThrottleCheckResult will_start_result,
123 NavigationThrottle::ThrottleCheckResult will_redirect_result,
124 NavigationThrottle::ThrottleCheckResult will_process_result,
125 base::Closure did_call_will_start,
126 base::Closure did_call_will_redirect,
127 base::Closure did_call_will_process)
128 : NavigationThrottle(handle),
129 will_start_result_(will_start_result),
130 will_redirect_result_(will_redirect_result),
131 will_process_result_(will_process_result),
132 did_call_will_start_(did_call_will_start),
133 did_call_will_redirect_(did_call_will_redirect),
134 did_call_will_process_(did_call_will_process) {}
135 ~TestNavigationThrottle() override {}
136
137 void Resume() { navigation_handle()->Resume(); }
138
139 private:
140 // NavigationThrottle implementation.
141 NavigationThrottle::ThrottleCheckResult WillStartRequest() override {
142 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, did_call_will_start_);
143 return will_start_result_;
144 }
145
146 NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override {
147 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
148 did_call_will_redirect_);
149 return will_redirect_result_;
150 }
151
152 NavigationThrottle::ThrottleCheckResult WillProcessResponse() override {
153 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
154 did_call_will_process_);
155 return will_process_result_;
156 }
157
158 NavigationThrottle::ThrottleCheckResult will_start_result_;
159 NavigationThrottle::ThrottleCheckResult will_redirect_result_;
160 NavigationThrottle::ThrottleCheckResult will_process_result_;
161 base::Closure did_call_will_start_;
162 base::Closure did_call_will_redirect_;
163 base::Closure did_call_will_process_;
164 };
165
166 // Install a TestNavigationThrottle on all requests and allows waiting for
167 // various NavigationThrottle related events.
168 class TestNavigationThrottleInstaller : public WebContentsObserver {
169 public:
170 TestNavigationThrottleInstaller(
171 WebContents* web_contents,
172 NavigationThrottle::ThrottleCheckResult will_start_result,
173 NavigationThrottle::ThrottleCheckResult will_redirect_result,
174 NavigationThrottle::ThrottleCheckResult will_process_result)
175 : WebContentsObserver(web_contents),
176 will_start_result_(will_start_result),
177 will_redirect_result_(will_redirect_result),
178 will_process_result_(will_process_result),
179 will_start_called_(0),
180 will_redirect_called_(0),
181 will_process_called_(0),
182 navigation_throttle_(nullptr) {}
183 ~TestNavigationThrottleInstaller() override{};
184
185 TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; }
186
187 void WaitForThrottleWillStart() {
188 if (will_start_called_)
189 return;
190 will_start_loop_runner_ = new MessageLoopRunner();
191 will_start_loop_runner_->Run();
192 will_start_loop_runner_ = nullptr;
193 }
194
195 void WaitForThrottleWillRedirect() {
196 if (will_redirect_called_)
197 return;
198 will_redirect_loop_runner_ = new MessageLoopRunner();
199 will_redirect_loop_runner_->Run();
200 will_redirect_loop_runner_ = nullptr;
201 }
202
203 void WaitForThrottleWillProcess() {
204 if (will_process_called_)
205 return;
206 will_process_loop_runner_ = new MessageLoopRunner();
207 will_process_loop_runner_->Run();
208 will_process_loop_runner_ = nullptr;
209 }
210
211 int will_start_called() { return will_start_called_; }
212 int will_redirect_called() { return will_redirect_called_; }
213 int will_process_called() { return will_process_called_; }
214
215 private:
216 void DidStartNavigation(NavigationHandle* handle) override {
217 scoped_ptr<NavigationThrottle> throttle(new TestNavigationThrottle(
218 handle, will_start_result_, will_redirect_result_, will_process_result_,
219 base::Bind(&TestNavigationThrottleInstaller::DidCallWillStartRequest,
220 base::Unretained(this)),
221 base::Bind(&TestNavigationThrottleInstaller::DidCallWillRedirectRequest,
222 base::Unretained(this)),
223 base::Bind(&TestNavigationThrottleInstaller::DidCallWillProcessResponse,
224 base::Unretained(this))));
225 navigation_throttle_ = static_cast<TestNavigationThrottle*>(throttle.get());
226 handle->RegisterThrottleForTesting(std::move(throttle));
227 }
228
229 void DidFinishNavigation(NavigationHandle* handle) override {
230 if (!navigation_throttle_)
231 return;
232
233 if (handle == navigation_throttle_->navigation_handle())
234 navigation_throttle_ = nullptr;
235 }
236
237 void DidCallWillStartRequest() {
238 will_start_called_++;
239 if (will_start_loop_runner_)
240 will_start_loop_runner_->Quit();
241 }
242
243 void DidCallWillRedirectRequest() {
244 will_redirect_called_++;
245 if (will_redirect_loop_runner_)
246 will_redirect_loop_runner_->Quit();
247 }
248
249 void DidCallWillProcessResponse() {
250 will_process_called_++;
251 if (will_process_loop_runner_)
252 will_process_loop_runner_->Quit();
253 }
254
255 NavigationThrottle::ThrottleCheckResult will_start_result_;
256 NavigationThrottle::ThrottleCheckResult will_redirect_result_;
257 NavigationThrottle::ThrottleCheckResult will_process_result_;
258 int will_start_called_;
259 int will_redirect_called_;
260 int will_process_called_;
261 TestNavigationThrottle* navigation_throttle_;
262 scoped_refptr<MessageLoopRunner> will_start_loop_runner_;
263 scoped_refptr<MessageLoopRunner> will_redirect_loop_runner_;
264 scoped_refptr<MessageLoopRunner> will_process_loop_runner_;
265 };
266
114 } // namespace 267 } // namespace
115 268
116 class NavigationHandleImplBrowserTest : public ContentBrowserTest { 269 class NavigationHandleImplBrowserTest : public ContentBrowserTest {
117 protected: 270 protected:
118 void SetUpOnMainThread() override { 271 void SetUpOnMainThread() override {
119 host_resolver()->AddRule("*", "127.0.0.1"); 272 host_resolver()->AddRule("*", "127.0.0.1");
120 ASSERT_TRUE(embedded_test_server()->Start()); 273 ASSERT_TRUE(embedded_test_server()->Start());
121 SetupCrossSiteRedirector(embedded_test_server()); 274 SetupCrossSiteRedirector(embedded_test_server());
122 } 275 }
123 }; 276 };
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 NavigationHandleObserver observer( 415 NavigationHandleObserver observer(
263 shell()->web_contents(), embedded_test_server()->GetURL("a.com", "/bar")); 416 shell()->web_contents(), embedded_test_server()->GetURL("a.com", "/bar"));
264 EXPECT_TRUE(ExecuteScript(root->child_at(0)->current_frame_host(), 417 EXPECT_TRUE(ExecuteScript(root->child_at(0)->current_frame_host(),
265 "window.history.pushState({}, '', 'bar');")); 418 "window.history.pushState({}, '', 'bar');"));
266 419
267 EXPECT_TRUE(observer.has_committed()); 420 EXPECT_TRUE(observer.has_committed());
268 EXPECT_FALSE(observer.is_error()); 421 EXPECT_FALSE(observer.is_error());
269 EXPECT_TRUE(observer.is_synchronous()); 422 EXPECT_TRUE(observer.is_synchronous());
270 } 423 }
271 424
425 // Ensure that a NavigationThrottle can cancel the navigation at navigation
426 // start.
427 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, ThrottleCancelStart) {
428 GURL start_url(embedded_test_server()->GetURL("/title1.html"));
429 EXPECT_TRUE(NavigateToURL(shell(), start_url));
430
431 GURL redirect_url(
432 embedded_test_server()->GetURL("/cross-site/bar.com/title2.html"));
433 NavigationHandleObserver observer(shell()->web_contents(), redirect_url);
434 TestNavigationThrottleInstaller installer(
435 shell()->web_contents(), NavigationThrottle::CANCEL,
436 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED);
437
438 EXPECT_FALSE(NavigateToURL(shell(), redirect_url));
439
440 EXPECT_FALSE(observer.has_committed());
441 EXPECT_TRUE(observer.is_error());
442
443 // The navigation should have been canceled before being redirected.
444 EXPECT_FALSE(observer.was_redirected());
445 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), start_url);
446 }
447
448 // Ensure that a NavigationThrottle can cancel the navigation when a navigation
449 // is redirected.
450 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest,
451 ThrottleCancelRedirect) {
452 GURL start_url(embedded_test_server()->GetURL("/title1.html"));
453 EXPECT_TRUE(NavigateToURL(shell(), start_url));
454
455 // A navigation with a redirect should be canceled.
456 {
457 GURL redirect_url(
458 embedded_test_server()->GetURL("/cross-site/bar.com/title2.html"));
459 NavigationHandleObserver observer(shell()->web_contents(), redirect_url);
460 TestNavigationThrottleInstaller installer(
461 shell()->web_contents(), NavigationThrottle::PROCEED,
462 NavigationThrottle::CANCEL, NavigationThrottle::PROCEED);
463
464 EXPECT_FALSE(NavigateToURL(shell(), redirect_url));
465
466 EXPECT_FALSE(observer.has_committed());
467 EXPECT_TRUE(observer.is_error());
468 EXPECT_TRUE(observer.was_redirected());
469 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), start_url);
470 }
471
472 // A navigation without redirects should be successful.
473 {
474 GURL no_redirect_url(embedded_test_server()->GetURL("/title2.html"));
475 NavigationHandleObserver observer(shell()->web_contents(), no_redirect_url);
476 TestNavigationThrottleInstaller installer(
477 shell()->web_contents(), NavigationThrottle::PROCEED,
478 NavigationThrottle::CANCEL, NavigationThrottle::PROCEED);
479
480 EXPECT_TRUE(NavigateToURL(shell(), no_redirect_url));
481
482 EXPECT_TRUE(observer.has_committed());
483 EXPECT_FALSE(observer.is_error());
484 EXPECT_FALSE(observer.was_redirected());
485 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), no_redirect_url);
486 }
487 }
488
489 // Ensure that a NavigationThrottle can cancel the navigation when the response
490 // is received.
491 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest,
492 ThrottleCancelResponse) {
493 GURL start_url(embedded_test_server()->GetURL("/title1.html"));
494 EXPECT_TRUE(NavigateToURL(shell(), start_url));
495
496 GURL redirect_url(
497 embedded_test_server()->GetURL("/cross-site/bar.com/title2.html"));
498 NavigationHandleObserver observer(shell()->web_contents(), redirect_url);
499 TestNavigationThrottleInstaller installer(
500 shell()->web_contents(), NavigationThrottle::PROCEED,
501 NavigationThrottle::PROCEED, NavigationThrottle::CANCEL);
502
503 EXPECT_FALSE(NavigateToURL(shell(), redirect_url));
504
505 EXPECT_FALSE(observer.has_committed());
506 EXPECT_TRUE(observer.is_error());
507 // The navigation should have been redirected first, and then canceled when
508 // the response arrived.
509 EXPECT_TRUE(observer.was_redirected());
510 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), start_url);
511 }
512
513 // Ensure that a NavigationThrottle can defer and resume the navigation at
514 // navigation start, navigation redirect and response received.
515 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, ThrottleDefer) {
516 GURL start_url(embedded_test_server()->GetURL("/title1.html"));
517 EXPECT_TRUE(NavigateToURL(shell(), start_url));
518
519 GURL redirect_url(
520 embedded_test_server()->GetURL("/cross-site/bar.com/title2.html"));
521 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
522 NavigationHandleObserver observer(shell()->web_contents(), redirect_url);
523 TestNavigationThrottleInstaller installer(
524 shell()->web_contents(), NavigationThrottle::DEFER,
525 NavigationThrottle::DEFER, NavigationThrottle::DEFER);
526
527 shell()->LoadURL(redirect_url);
528
529 // Wait for WillStartRequest.
530 installer.WaitForThrottleWillStart();
531 EXPECT_EQ(1, installer.will_start_called());
532 EXPECT_EQ(0, installer.will_redirect_called());
533 EXPECT_EQ(0, installer.will_process_called());
534 installer.navigation_throttle()->Resume();
535
536 // Wait for WillRedirectRequest.
537 installer.WaitForThrottleWillRedirect();
538 EXPECT_EQ(1, installer.will_start_called());
539 EXPECT_EQ(1, installer.will_redirect_called());
540 EXPECT_EQ(0, installer.will_process_called());
541 installer.navigation_throttle()->Resume();
542
543 // Wait for WillProcessResponse.
544 installer.WaitForThrottleWillProcess();
545 EXPECT_EQ(1, installer.will_start_called());
546 EXPECT_EQ(1, installer.will_redirect_called());
547 EXPECT_EQ(1, installer.will_process_called());
548 installer.navigation_throttle()->Resume();
549
550 // Wait for the end of the navigation.
551 navigation_observer.Wait();
552
553 EXPECT_TRUE(observer.has_committed());
554 EXPECT_TRUE(observer.was_redirected());
555 EXPECT_FALSE(observer.is_error());
556 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(),
557 GURL(embedded_test_server()->GetURL("bar.com", "/title2.html")));
558 }
559
272 } // namespace content 560 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/frame_host/navigation_handle_impl.cc ('k') | content/browser/frame_host/navigation_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698