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

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

Issue 2728783003: Fix NavigationHandleImplBrowserTest flakiness. (Closed)
Patch Set: Created 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/memory/weak_ptr.h" 5 #include "base/memory/weak_ptr.h"
6 #include "content/browser/frame_host/navigation_handle_impl.h" 6 #include "content/browser/frame_host/navigation_handle_impl.h"
7 #include "content/browser/web_contents/web_contents_impl.h" 7 #include "content/browser/web_contents/web_contents_impl.h"
8 #include "content/public/browser/web_contents.h" 8 #include "content/public/browser/web_contents.h"
9 #include "content/public/browser/web_contents_observer.h" 9 #include "content/public/browser/web_contents_observer.h"
10 #include "content/public/common/request_context_type.h" 10 #include "content/public/common/request_context_type.h"
(...skipping 12 matching lines...) Expand all
23 23
24 namespace { 24 namespace {
25 25
26 // Gathers data from the NavigationHandle assigned to navigations that start 26 // Gathers data from the NavigationHandle assigned to navigations that start
27 // with the expected URL. 27 // with the expected URL.
28 class NavigationHandleObserver : public WebContentsObserver { 28 class NavigationHandleObserver : public WebContentsObserver {
29 public: 29 public:
30 NavigationHandleObserver(WebContents* web_contents, 30 NavigationHandleObserver(WebContents* web_contents,
31 const GURL& expected_start_url) 31 const GURL& expected_start_url)
32 : WebContentsObserver(web_contents), 32 : WebContentsObserver(web_contents),
33 handle_(nullptr),
34 has_committed_(false),
35 is_error_(false),
36 is_main_frame_(false),
37 is_parent_main_frame_(false),
38 is_renderer_initiated_(true),
39 is_same_page_(false),
40 was_redirected_(false),
41 frame_tree_node_id_(-1),
42 page_transition_(ui::PAGE_TRANSITION_LINK), 33 page_transition_(ui::PAGE_TRANSITION_LINK),
43 expected_start_url_(expected_start_url), 34 expected_start_url_(expected_start_url) {}
44 net_error_code_(net::OK) {}
45 35
46 void DidStartNavigation(NavigationHandle* navigation_handle) override { 36 void DidStartNavigation(NavigationHandle* navigation_handle) override {
47 if (handle_ || navigation_handle->GetURL() != expected_start_url_) 37 if (handle_ || navigation_handle->GetURL() != expected_start_url_)
48 return; 38 return;
49 39
50 handle_ = navigation_handle; 40 handle_ = navigation_handle;
51 has_committed_ = false; 41 has_committed_ = false;
52 is_error_ = false; 42 is_error_ = false;
53 page_transition_ = ui::PAGE_TRANSITION_LINK; 43 page_transition_ = ui::PAGE_TRANSITION_LINK;
54 last_committed_url_ = GURL(); 44 last_committed_url_ = GURL();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 const GURL& last_committed_url() { return last_committed_url_; } 92 const GURL& last_committed_url() { return last_committed_url_; }
103 93
104 ui::PageTransition page_transition() { return page_transition_; } 94 ui::PageTransition page_transition() { return page_transition_; }
105 95
106 net::Error net_error_code() { return net_error_code_; } 96 net::Error net_error_code() { return net_error_code_; }
107 97
108 private: 98 private:
109 // A reference to the NavigationHandle so this class will track only 99 // A reference to the NavigationHandle so this class will track only
110 // one navigation at a time. It is set at DidStartNavigation and cleared 100 // one navigation at a time. It is set at DidStartNavigation and cleared
111 // at DidFinishNavigation before the NavigationHandle is destroyed. 101 // at DidFinishNavigation before the NavigationHandle is destroyed.
112 NavigationHandle* handle_; 102 NavigationHandle* handle_ = nullptr;
113 bool has_committed_; 103 bool has_committed_ = false;
114 bool is_error_; 104 bool is_error_ = false;
115 bool is_main_frame_; 105 bool is_main_frame_ = false;
116 bool is_parent_main_frame_; 106 bool is_parent_main_frame_ = false;
117 bool is_renderer_initiated_; 107 bool is_renderer_initiated_ = true;
118 bool is_same_page_; 108 bool is_same_page_ = false;
119 bool was_redirected_; 109 bool was_redirected_ = false;
120 int frame_tree_node_id_; 110 int frame_tree_node_id_ = -1;
121 ui::PageTransition page_transition_; 111 ui::PageTransition page_transition_ = ui::PAGE_TRANSITION_LINK;
122 GURL expected_start_url_; 112 GURL expected_start_url_;
123 GURL last_committed_url_; 113 GURL last_committed_url_;
124 net::Error net_error_code_; 114 net::Error net_error_code_ = net::OK;
125 }; 115 };
126 116
127 // A test NavigationThrottle that will return pre-determined checks and run 117 // A test NavigationThrottle that will return pre-determined checks and run
128 // callbacks when the various NavigationThrottle methods are called. It is 118 // callbacks when the various NavigationThrottle methods are called. It is
129 // not instantiated directly but through a TestNavigationThrottleInstaller. 119 // not instantiated directly but through a TestNavigationThrottleInstaller.
130 class TestNavigationThrottle : public NavigationThrottle { 120 class TestNavigationThrottle : public NavigationThrottle {
131 public: 121 public:
132 TestNavigationThrottle( 122 TestNavigationThrottle(
133 NavigationHandle* handle, 123 NavigationHandle* handle,
134 NavigationThrottle::ThrottleCheckResult will_start_result, 124 NavigationThrottle::ThrottleCheckResult will_start_result,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 did_call_will_process_); 174 did_call_will_process_);
185 return will_process_result_; 175 return will_process_result_;
186 } 176 }
187 177
188 NavigationThrottle::ThrottleCheckResult will_start_result_; 178 NavigationThrottle::ThrottleCheckResult will_start_result_;
189 NavigationThrottle::ThrottleCheckResult will_redirect_result_; 179 NavigationThrottle::ThrottleCheckResult will_redirect_result_;
190 NavigationThrottle::ThrottleCheckResult will_process_result_; 180 NavigationThrottle::ThrottleCheckResult will_process_result_;
191 base::Closure did_call_will_start_; 181 base::Closure did_call_will_start_;
192 base::Closure did_call_will_redirect_; 182 base::Closure did_call_will_redirect_;
193 base::Closure did_call_will_process_; 183 base::Closure did_call_will_process_;
194 RequestContextType request_context_type_; 184 RequestContextType request_context_type_ = REQUEST_CONTEXT_TYPE_UNSPECIFIED;
195 }; 185 };
196 186
197 // Install a TestNavigationThrottle on all following requests and allows waiting 187 // Install a TestNavigationThrottle on all following requests and allows waiting
198 // for various NavigationThrottle related events. Waiting works only for the 188 // for various NavigationThrottle related events. Waiting works only for the
199 // immediately next navigation. New instances are needed to wait for further 189 // immediately next navigation. New instances are needed to wait for further
200 // navigations. 190 // navigations.
201 class TestNavigationThrottleInstaller : public WebContentsObserver { 191 class TestNavigationThrottleInstaller : public WebContentsObserver {
202 public: 192 public:
203 TestNavigationThrottleInstaller( 193 TestNavigationThrottleInstaller(
204 WebContents* web_contents, 194 WebContents* web_contents,
205 NavigationThrottle::ThrottleCheckResult will_start_result, 195 NavigationThrottle::ThrottleCheckResult will_start_result,
206 NavigationThrottle::ThrottleCheckResult will_redirect_result, 196 NavigationThrottle::ThrottleCheckResult will_redirect_result,
207 NavigationThrottle::ThrottleCheckResult will_process_result) 197 NavigationThrottle::ThrottleCheckResult will_process_result)
208 : WebContentsObserver(web_contents), 198 : WebContentsObserver(web_contents),
209 will_start_result_(will_start_result), 199 will_start_result_(will_start_result),
210 will_redirect_result_(will_redirect_result), 200 will_redirect_result_(will_redirect_result),
211 will_process_result_(will_process_result), 201 will_process_result_(will_process_result),
212 will_start_called_(0),
213 will_redirect_called_(0),
214 will_process_called_(0),
215 navigation_throttle_(nullptr),
216 weak_factory_(this) {} 202 weak_factory_(this) {}
217 ~TestNavigationThrottleInstaller() override {} 203 ~TestNavigationThrottleInstaller() override {}
218 204
219 TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; } 205 TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; }
220 206
221 void WaitForThrottleWillStart() { 207 void WaitForThrottleWillStart() {
222 if (will_start_called_) 208 if (will_start_called_)
223 return; 209 return;
224 will_start_loop_runner_ = new MessageLoopRunner(); 210 will_start_loop_runner_ = new MessageLoopRunner();
225 will_start_loop_runner_->Run(); 211 will_start_loop_runner_->Run();
(...skipping 13 matching lines...) Expand all
239 return; 225 return;
240 will_process_loop_runner_ = new MessageLoopRunner(); 226 will_process_loop_runner_ = new MessageLoopRunner();
241 will_process_loop_runner_->Run(); 227 will_process_loop_runner_->Run();
242 will_process_loop_runner_ = nullptr; 228 will_process_loop_runner_ = nullptr;
243 } 229 }
244 230
245 int will_start_called() { return will_start_called_; } 231 int will_start_called() { return will_start_called_; }
246 int will_redirect_called() { return will_redirect_called_; } 232 int will_redirect_called() { return will_redirect_called_; }
247 int will_process_called() { return will_process_called_; } 233 int will_process_called() { return will_process_called_; }
248 234
235 int install_count() { return install_count_; }
236
249 private: 237 private:
250 void DidStartNavigation(NavigationHandle* handle) override { 238 void DidStartNavigation(NavigationHandle* handle) override {
251 std::unique_ptr<NavigationThrottle> throttle(new TestNavigationThrottle( 239 std::unique_ptr<NavigationThrottle> throttle(new TestNavigationThrottle(
252 handle, will_start_result_, will_redirect_result_, will_process_result_, 240 handle, will_start_result_, will_redirect_result_, will_process_result_,
253 base::Bind(&TestNavigationThrottleInstaller::DidCallWillStartRequest, 241 base::Bind(&TestNavigationThrottleInstaller::DidCallWillStartRequest,
254 weak_factory_.GetWeakPtr()), 242 weak_factory_.GetWeakPtr()),
255 base::Bind(&TestNavigationThrottleInstaller::DidCallWillRedirectRequest, 243 base::Bind(&TestNavigationThrottleInstaller::DidCallWillRedirectRequest,
256 weak_factory_.GetWeakPtr()), 244 weak_factory_.GetWeakPtr()),
257 base::Bind(&TestNavigationThrottleInstaller::DidCallWillProcessResponse, 245 base::Bind(&TestNavigationThrottleInstaller::DidCallWillProcessResponse,
258 weak_factory_.GetWeakPtr()))); 246 weak_factory_.GetWeakPtr())));
259 navigation_throttle_ = static_cast<TestNavigationThrottle*>(throttle.get()); 247 navigation_throttle_ = static_cast<TestNavigationThrottle*>(throttle.get());
260 handle->RegisterThrottleForTesting(std::move(throttle)); 248 handle->RegisterThrottleForTesting(std::move(throttle));
249 ++install_count_;
261 } 250 }
262 251
263 void DidFinishNavigation(NavigationHandle* handle) override { 252 void DidFinishNavigation(NavigationHandle* handle) override {
264 if (!navigation_throttle_) 253 if (!navigation_throttle_)
265 return; 254 return;
266 255
267 if (handle == navigation_throttle_->navigation_handle()) 256 if (handle == navigation_throttle_->navigation_handle())
268 navigation_throttle_ = nullptr; 257 navigation_throttle_ = nullptr;
269 } 258 }
270 259
(...skipping 11 matching lines...) Expand all
282 271
283 void DidCallWillProcessResponse() { 272 void DidCallWillProcessResponse() {
284 will_process_called_++; 273 will_process_called_++;
285 if (will_process_loop_runner_) 274 if (will_process_loop_runner_)
286 will_process_loop_runner_->Quit(); 275 will_process_loop_runner_->Quit();
287 } 276 }
288 277
289 NavigationThrottle::ThrottleCheckResult will_start_result_; 278 NavigationThrottle::ThrottleCheckResult will_start_result_;
290 NavigationThrottle::ThrottleCheckResult will_redirect_result_; 279 NavigationThrottle::ThrottleCheckResult will_redirect_result_;
291 NavigationThrottle::ThrottleCheckResult will_process_result_; 280 NavigationThrottle::ThrottleCheckResult will_process_result_;
292 int will_start_called_; 281 int will_start_called_ = 0;
293 int will_redirect_called_; 282 int will_redirect_called_ = 0;
294 int will_process_called_; 283 int will_process_called_ = 0;
295 TestNavigationThrottle* navigation_throttle_; 284 TestNavigationThrottle* navigation_throttle_ = nullptr;
285 int install_count_ = 0;
296 scoped_refptr<MessageLoopRunner> will_start_loop_runner_; 286 scoped_refptr<MessageLoopRunner> will_start_loop_runner_;
297 scoped_refptr<MessageLoopRunner> will_redirect_loop_runner_; 287 scoped_refptr<MessageLoopRunner> will_redirect_loop_runner_;
298 scoped_refptr<MessageLoopRunner> will_process_loop_runner_; 288 scoped_refptr<MessageLoopRunner> will_process_loop_runner_;
299 289
300 // The throttle installer can be deleted before all tasks posted by its 290 // The throttle installer can be deleted before all tasks posted by its
301 // throttles are run, so it must be referenced via weak pointers. 291 // throttles are run, so it must be referenced via weak pointers.
302 base::WeakPtrFactory<TestNavigationThrottleInstaller> weak_factory_; 292 base::WeakPtrFactory<TestNavigationThrottleInstaller> weak_factory_;
303 }; 293 };
304 294
305 // Records all navigation start URLs from the WebContents. 295 // Records all navigation start URLs from the WebContents.
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 // Wait for the end of the navigation. 708 // Wait for the end of the navigation.
719 navigation_observer.Wait(); 709 navigation_observer.Wait();
720 710
721 EXPECT_TRUE(observer.has_committed()); 711 EXPECT_TRUE(observer.has_committed());
722 EXPECT_TRUE(observer.was_redirected()); 712 EXPECT_TRUE(observer.was_redirected());
723 EXPECT_FALSE(observer.is_error()); 713 EXPECT_FALSE(observer.is_error());
724 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), 714 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(),
725 GURL(embedded_test_server()->GetURL("bar.com", "/title2.html"))); 715 GURL(embedded_test_server()->GetURL("bar.com", "/title2.html")));
726 } 716 }
727 717
728 #if defined(OS_WIN)
729 #define MAYBE_VerifyRequestContextTypeForFrameTree \
730 DISABLED_VerifyRequestContextTypeForFrameTree
731 #else
732 #define MAYBE_VerifyRequestContextTypeForFrameTree \
733 VerifyRequestContextTypeForFrameTree
734 #endif
735
736 // Checks that the RequestContextType value is properly set. 718 // Checks that the RequestContextType value is properly set.
737 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, 719 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest,
738 MAYBE_VerifyRequestContextTypeForFrameTree) { 720 VerifyRequestContextTypeForFrameTree) {
739 GURL main_url(embedded_test_server()->GetURL( 721 GURL main_url(embedded_test_server()->GetURL(
740 "a.com", "/cross_site_iframe_factory.html?a(b(c))")); 722 "a.com", "/cross_site_iframe_factory.html?a(b(c))"));
741 GURL b_url(embedded_test_server()->GetURL( 723 GURL b_url(embedded_test_server()->GetURL(
742 "b.com", "/cross_site_iframe_factory.html?b(c())")); 724 "b.com", "/cross_site_iframe_factory.html?b(c())"));
743 GURL c_url(embedded_test_server()->GetURL( 725 GURL c_url(embedded_test_server()->GetURL(
744 "c.com", "/cross_site_iframe_factory.html?c()")); 726 "c.com", "/cross_site_iframe_factory.html?c()"));
745 727
746 TestNavigationThrottleInstaller installer( 728 TestNavigationThrottleInstaller installer(
747 shell()->web_contents(), NavigationThrottle::PROCEED, 729 shell()->web_contents(), NavigationThrottle::PROCEED,
748 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); 730 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED);
749 TestNavigationManager main_manager(shell()->web_contents(), main_url); 731 TestNavigationManager main_manager(shell()->web_contents(), main_url);
750 TestNavigationManager b_manager(shell()->web_contents(), b_url); 732 TestNavigationManager b_manager(shell()->web_contents(), b_url);
751 TestNavigationManager c_manager(shell()->web_contents(), c_url); 733 TestNavigationManager c_manager(shell()->web_contents(), c_url);
752 NavigationStartUrlRecorder url_recorder(shell()->web_contents()); 734 NavigationStartUrlRecorder url_recorder(shell()->web_contents());
753 TestNavigationThrottle* previous_throttle = nullptr;
754 735
755 // Starts and verifies the main frame navigation. 736 // Starts and verifies the main frame navigation.
756 shell()->LoadURL(main_url); 737 shell()->LoadURL(main_url);
757 EXPECT_TRUE(main_manager.WaitForRequestStart()); 738 EXPECT_TRUE(main_manager.WaitForRequestStart());
758 // The throttle should not be null. 739 // For each navigation a new throttle should have been installed.
759 EXPECT_NE(previous_throttle, installer.navigation_throttle()); 740 EXPECT_EQ(1, installer.install_count());
760 // Checks the only URL recorded so far is the one expected for the main frame. 741 // Checks the only URL recorded so far is the one expected for the main frame.
761 EXPECT_EQ(main_url, url_recorder.urls().back()); 742 EXPECT_EQ(main_url, url_recorder.urls().back());
762 EXPECT_EQ(1ul, url_recorder.urls().size()); 743 EXPECT_EQ(1ul, url_recorder.urls().size());
763 // Checks the main frame RequestContextType. 744 // Checks the main frame RequestContextType.
764 EXPECT_EQ(REQUEST_CONTEXT_TYPE_LOCATION, 745 EXPECT_EQ(REQUEST_CONTEXT_TYPE_LOCATION,
765 installer.navigation_throttle()->request_context_type()); 746 installer.navigation_throttle()->request_context_type());
766 // For each navigations the throttle should be a different instance.
767 previous_throttle = installer.navigation_throttle();
768 747
769 // Ditto for frame b navigation. 748 // Ditto for frame b navigation.
770 main_manager.WaitForNavigationFinished(); 749 main_manager.WaitForNavigationFinished();
771 EXPECT_TRUE(b_manager.WaitForRequestStart()); 750 EXPECT_TRUE(b_manager.WaitForRequestStart());
772 EXPECT_NE(previous_throttle, installer.navigation_throttle()); 751 EXPECT_EQ(2, installer.install_count());
773 EXPECT_EQ(b_url, url_recorder.urls().back()); 752 EXPECT_EQ(b_url, url_recorder.urls().back());
774 EXPECT_EQ(2ul, url_recorder.urls().size()); 753 EXPECT_EQ(2ul, url_recorder.urls().size());
775 EXPECT_EQ(REQUEST_CONTEXT_TYPE_LOCATION, 754 EXPECT_EQ(REQUEST_CONTEXT_TYPE_LOCATION,
776 installer.navigation_throttle()->request_context_type()); 755 installer.navigation_throttle()->request_context_type());
777 previous_throttle = installer.navigation_throttle();
778 756
779 // Ditto for frame c navigation. 757 // Ditto for frame c navigation.
780 b_manager.WaitForNavigationFinished(); 758 b_manager.WaitForNavigationFinished();
781 EXPECT_TRUE(c_manager.WaitForRequestStart()); 759 EXPECT_TRUE(c_manager.WaitForRequestStart());
782 EXPECT_NE(previous_throttle, installer.navigation_throttle()); 760 EXPECT_EQ(3, installer.install_count());
783 EXPECT_EQ(c_url, url_recorder.urls().back()); 761 EXPECT_EQ(c_url, url_recorder.urls().back());
784 EXPECT_EQ(3ul, url_recorder.urls().size()); 762 EXPECT_EQ(3ul, url_recorder.urls().size());
785 EXPECT_EQ(REQUEST_CONTEXT_TYPE_LOCATION, 763 EXPECT_EQ(REQUEST_CONTEXT_TYPE_LOCATION,
786 installer.navigation_throttle()->request_context_type()); 764 installer.navigation_throttle()->request_context_type());
787 765
788 // Lets the final navigation finish so that we conclude running the 766 // Lets the final navigation finish so that we conclude running the
789 // RequestContextType checks that happen in TestNavigationThrottle. 767 // RequestContextType checks that happen in TestNavigationThrottle.
790 c_manager.WaitForNavigationFinished(); 768 c_manager.WaitForNavigationFinished();
791 // Confirms the last navigation did finish. 769 // Confirms the last navigation did finish.
792 EXPECT_FALSE(installer.navigation_throttle()); 770 EXPECT_FALSE(installer.navigation_throttle());
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
1051 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); 1029 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED);
1052 NavigationHandleObserver observer(shell()->web_contents(), url); 1030 NavigationHandleObserver observer(shell()->web_contents(), url);
1053 EXPECT_TRUE(NavigateToURL(shell(), url)); 1031 EXPECT_TRUE(NavigateToURL(shell(), url));
1054 EXPECT_EQ(1, installer.will_start_called()); 1032 EXPECT_EQ(1, installer.will_start_called());
1055 EXPECT_EQ(1, installer.will_process_called()); 1033 EXPECT_EQ(1, installer.will_process_called());
1056 EXPECT_FALSE(observer.is_same_page()); 1034 EXPECT_FALSE(observer.is_same_page());
1057 } 1035 }
1058 } 1036 }
1059 1037
1060 } // namespace content 1038 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698