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

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

Issue 1414723008: Add a way to cancel deferred navigations in NavigationHandle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Nasko's comments Created 5 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/macros.h" 5 #include "base/macros.h"
6 #include "content/browser/frame_host/navigation_handle_impl.h" 6 #include "content/browser/frame_host/navigation_handle_impl.h"
7 #include "content/public/browser/navigation_throttle.h" 7 #include "content/public/browser/navigation_throttle.h"
8 #include "content/test/test_render_frame_host.h" 8 #include "content/test/test_render_frame_host.h"
9 9
10 namespace content { 10 namespace content {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 } 65 }
66 66
67 bool IsDeferringStart() { 67 bool IsDeferringStart() {
68 return test_handle_->state() == NavigationHandleImpl::DEFERRING_START; 68 return test_handle_->state() == NavigationHandleImpl::DEFERRING_START;
69 } 69 }
70 70
71 bool IsDeferringRedirect() { 71 bool IsDeferringRedirect() {
72 return test_handle_->state() == NavigationHandleImpl::DEFERRING_REDIRECT; 72 return test_handle_->state() == NavigationHandleImpl::DEFERRING_REDIRECT;
73 } 73 }
74 74
75 bool IsCanceling() {
76 return test_handle_->state() == NavigationHandleImpl::CANCELING;
77 }
78
75 // Helper function to call WillStartRequest on |handle|. If this function 79 // Helper function to call WillStartRequest on |handle|. If this function
76 // returns DEFER, |callback_result_| will be set to the actual result of 80 // returns DEFER, |callback_result_| will be set to the actual result of
77 // the throttle checks when they are finished. 81 // the throttle checks when they are finished.
78 void SimulateWillStartRequest() { 82 void SimulateWillStartRequest() {
79 was_callback_called_ = false; 83 was_callback_called_ = false;
80 callback_result_ = NavigationThrottle::DEFER; 84 callback_result_ = NavigationThrottle::DEFER;
81 85
82 // It's safe to use base::Unretained since the NavigationHandle is owned by 86 // It's safe to use base::Unretained since the NavigationHandle is owned by
83 // the NavigationHandleImplTest. 87 // the NavigationHandleImplTest.
84 test_handle_->WillStartRequest( 88 test_handle_->WillStartRequest(
85 false, Referrer(), false, ui::PAGE_TRANSITION_LINK, false, 89 false, Referrer(), false, ui::PAGE_TRANSITION_LINK, false,
86 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult, 90 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult,
87 base::Unretained(this))); 91 base::Unretained(this)));
88 } 92 }
89 93
90 // Helper function to call WillRedirectRequest on |handle|. If this function 94 // Helper function to call WillRedirectRequest on |handle|. If this function
91 // returns DEFER, |callback_result_| will be set to the actual result of the 95 // returns DEFER, |callback_result_| will be set to the actual result of the
92 // throttle checks when they are finished. 96 // throttle checks when they are finished.
97 // TODO(clamy): this should also simulate that WillStartRequest was called if
98 // it has not been called before.
93 void SimulateWillRedirectRequest() { 99 void SimulateWillRedirectRequest() {
94 was_callback_called_ = false; 100 was_callback_called_ = false;
95 callback_result_ = NavigationThrottle::DEFER; 101 callback_result_ = NavigationThrottle::DEFER;
96 102
97 // It's safe to use base::Unretained since the NavigationHandle is owned by 103 // It's safe to use base::Unretained since the NavigationHandle is owned by
98 // the NavigationHandleImplTest. 104 // the NavigationHandleImplTest.
99 test_handle_->WillRedirectRequest( 105 test_handle_->WillRedirectRequest(
100 GURL(), false, GURL(), false, 106 GURL(), false, GURL(), false,
101 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult, 107 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult,
102 base::Unretained(this))); 108 base::Unretained(this)));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 // should have been called. 185 // should have been called.
180 test_handle()->Resume(); 186 test_handle()->Resume();
181 EXPECT_FALSE(IsDeferringStart()); 187 EXPECT_FALSE(IsDeferringStart());
182 EXPECT_FALSE(IsDeferringRedirect()); 188 EXPECT_FALSE(IsDeferringRedirect());
183 EXPECT_TRUE(was_callback_called()); 189 EXPECT_TRUE(was_callback_called());
184 EXPECT_EQ(NavigationThrottle::PROCEED, callback_result()); 190 EXPECT_EQ(NavigationThrottle::PROCEED, callback_result());
185 EXPECT_EQ(1, test_throttle->will_start_calls()); 191 EXPECT_EQ(1, test_throttle->will_start_calls());
186 EXPECT_EQ(1, test_throttle->will_redirect_calls()); 192 EXPECT_EQ(1, test_throttle->will_redirect_calls());
187 } 193 }
188 194
195 // Checks that a navigation deferred during WillStartRequest can be properly
196 // cancelled.
197 TEST_F(NavigationHandleImplTest, CancelDeferredWillStart) {
198 TestNavigationThrottle* test_throttle =
199 CreateTestNavigationThrottle(NavigationThrottle::DEFER);
200 EXPECT_FALSE(IsDeferringStart());
201 EXPECT_FALSE(IsDeferringRedirect());
202 EXPECT_EQ(0, test_throttle->will_start_calls());
203 EXPECT_EQ(0, test_throttle->will_redirect_calls());
204
205 // Simulate WillStartRequest. The request should be deferred. The callback
206 // should not have been called.
207 SimulateWillStartRequest();
208 EXPECT_TRUE(IsDeferringStart());
209 EXPECT_FALSE(IsDeferringRedirect());
210 EXPECT_FALSE(was_callback_called());
211 EXPECT_EQ(1, test_throttle->will_start_calls());
212 EXPECT_EQ(0, test_throttle->will_redirect_calls());
213
214 // Cancel the request. The callback should have been called.
215 test_handle()->CancelDeferredNavigation(
216 NavigationThrottle::CANCEL_AND_IGNORE);
217 EXPECT_FALSE(IsDeferringStart());
218 EXPECT_FALSE(IsDeferringRedirect());
219 EXPECT_TRUE(IsCanceling());
220 EXPECT_TRUE(was_callback_called());
221 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result());
222 EXPECT_EQ(1, test_throttle->will_start_calls());
223 EXPECT_EQ(0, test_throttle->will_redirect_calls());
224 }
225
226 // Checks that a navigation deferred during WillRedirectRequest can be properly
227 // cancelled.
228 TEST_F(NavigationHandleImplTest, CancelDeferredWillRedirect) {
229 TestNavigationThrottle* test_throttle =
230 CreateTestNavigationThrottle(NavigationThrottle::DEFER);
231 EXPECT_FALSE(IsDeferringStart());
232 EXPECT_FALSE(IsDeferringRedirect());
233 EXPECT_EQ(0, test_throttle->will_start_calls());
234 EXPECT_EQ(0, test_throttle->will_redirect_calls());
235
236 // Simulate WillRedirectRequest. The request should be deferred. The callback
237 // should not have been called.
238 SimulateWillRedirectRequest();
239 EXPECT_FALSE(IsDeferringStart());
240 EXPECT_TRUE(IsDeferringRedirect());
241 EXPECT_FALSE(was_callback_called());
242 EXPECT_EQ(0, test_throttle->will_start_calls());
243 EXPECT_EQ(1, test_throttle->will_redirect_calls());
244
245 // Cancel the request. The callback should have been called.
246 test_handle()->CancelDeferredNavigation(
247 NavigationThrottle::CANCEL_AND_IGNORE);
248 EXPECT_FALSE(IsDeferringStart());
249 EXPECT_FALSE(IsDeferringRedirect());
250 EXPECT_TRUE(IsCanceling());
251 EXPECT_TRUE(was_callback_called());
252 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result());
253 EXPECT_EQ(0, test_throttle->will_start_calls());
254 EXPECT_EQ(1, test_throttle->will_redirect_calls());
255 }
256
257 // Checks that a navigation deferred can be canceled and not ignored.
258 TEST_F(NavigationHandleImplTest, CancelDeferredNoIgnore) {
259 TestNavigationThrottle* test_throttle =
260 CreateTestNavigationThrottle(NavigationThrottle::DEFER);
261 EXPECT_FALSE(IsDeferringStart());
262 EXPECT_FALSE(IsDeferringRedirect());
263 EXPECT_EQ(0, test_throttle->will_start_calls());
264 EXPECT_EQ(0, test_throttle->will_redirect_calls());
265
266 // Simulate WillRedirectRequest. The request should be deferred. The callback
267 // should not have been called.
268 SimulateWillStartRequest();
269 EXPECT_TRUE(IsDeferringStart());
270 EXPECT_FALSE(IsDeferringRedirect());
271 EXPECT_FALSE(was_callback_called());
272 EXPECT_EQ(1, test_throttle->will_start_calls());
273 EXPECT_EQ(0, test_throttle->will_redirect_calls());
274
275 // Cancel the request. The callback should have been called with CANCEL, and
276 // not CANCEL_AND_IGNORE.
277 test_handle()->CancelDeferredNavigation(NavigationThrottle::CANCEL);
278 EXPECT_FALSE(IsDeferringStart());
279 EXPECT_FALSE(IsDeferringRedirect());
280 EXPECT_TRUE(IsCanceling());
281 EXPECT_TRUE(was_callback_called());
282 EXPECT_EQ(NavigationThrottle::CANCEL, callback_result());
283 EXPECT_EQ(1, test_throttle->will_start_calls());
284 EXPECT_EQ(0, test_throttle->will_redirect_calls());
285 }
286
189 // Checks that a NavigationThrottle asking to defer followed by a 287 // Checks that a NavigationThrottle asking to defer followed by a
190 // NavigationThrottle asking to proceed behave correctly. 288 // NavigationThrottle asking to proceed behave correctly.
191 TEST_F(NavigationHandleImplTest, DeferThenProceed) { 289 TEST_F(NavigationHandleImplTest, DeferThenProceed) {
192 TestNavigationThrottle* defer_throttle = 290 TestNavigationThrottle* defer_throttle =
193 CreateTestNavigationThrottle(NavigationThrottle::DEFER); 291 CreateTestNavigationThrottle(NavigationThrottle::DEFER);
194 TestNavigationThrottle* proceed_throttle = 292 TestNavigationThrottle* proceed_throttle =
195 CreateTestNavigationThrottle(NavigationThrottle::PROCEED); 293 CreateTestNavigationThrottle(NavigationThrottle::PROCEED);
196 EXPECT_FALSE(IsDeferringStart()); 294 EXPECT_FALSE(IsDeferringStart());
197 EXPECT_FALSE(IsDeferringRedirect()); 295 EXPECT_FALSE(IsDeferringRedirect());
198 EXPECT_EQ(0, defer_throttle->will_start_calls()); 296 EXPECT_EQ(0, defer_throttle->will_start_calls());
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 EXPECT_FALSE(IsDeferringRedirect()); 369 EXPECT_FALSE(IsDeferringRedirect());
272 EXPECT_FALSE(was_callback_called()); 370 EXPECT_FALSE(was_callback_called());
273 EXPECT_EQ(1, defer_throttle->will_start_calls()); 371 EXPECT_EQ(1, defer_throttle->will_start_calls());
274 EXPECT_EQ(0, defer_throttle->will_redirect_calls()); 372 EXPECT_EQ(0, defer_throttle->will_redirect_calls());
275 EXPECT_EQ(0, cancel_throttle->will_start_calls()); 373 EXPECT_EQ(0, cancel_throttle->will_start_calls());
276 EXPECT_EQ(0, cancel_throttle->will_redirect_calls()); 374 EXPECT_EQ(0, cancel_throttle->will_redirect_calls());
277 375
278 // Resume the request. The callback should have been called. The second 376 // Resume the request. The callback should have been called. The second
279 // throttle should have been notified. 377 // throttle should have been notified.
280 test_handle()->Resume(); 378 test_handle()->Resume();
281 EXPECT_TRUE(IsDeferringStart()); 379 EXPECT_FALSE(IsDeferringStart());
282 EXPECT_FALSE(IsDeferringRedirect()); 380 EXPECT_FALSE(IsDeferringRedirect());
381 EXPECT_TRUE(IsCanceling());
283 EXPECT_TRUE(was_callback_called()); 382 EXPECT_TRUE(was_callback_called());
284 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); 383 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result());
285 EXPECT_EQ(1, defer_throttle->will_start_calls()); 384 EXPECT_EQ(1, defer_throttle->will_start_calls());
286 EXPECT_EQ(0, defer_throttle->will_redirect_calls()); 385 EXPECT_EQ(0, defer_throttle->will_redirect_calls());
287 EXPECT_EQ(1, cancel_throttle->will_start_calls()); 386 EXPECT_EQ(1, cancel_throttle->will_start_calls());
288 EXPECT_EQ(0, cancel_throttle->will_redirect_calls()); 387 EXPECT_EQ(0, cancel_throttle->will_redirect_calls());
289 } 388 }
290 389
291 // Checks that a NavigationThrottle asking to defer followed by a 390 // Checks that a NavigationThrottle asking to defer followed by a
292 // NavigationThrottle asking to cancel behave correctly in WillRedirectRequest. 391 // NavigationThrottle asking to cancel behave correctly in WillRedirectRequest.
(...skipping 18 matching lines...) Expand all
311 EXPECT_FALSE(was_callback_called()); 410 EXPECT_FALSE(was_callback_called());
312 EXPECT_EQ(0, defer_throttle->will_start_calls()); 411 EXPECT_EQ(0, defer_throttle->will_start_calls());
313 EXPECT_EQ(1, defer_throttle->will_redirect_calls()); 412 EXPECT_EQ(1, defer_throttle->will_redirect_calls());
314 EXPECT_EQ(0, cancel_throttle->will_start_calls()); 413 EXPECT_EQ(0, cancel_throttle->will_start_calls());
315 EXPECT_EQ(0, cancel_throttle->will_redirect_calls()); 414 EXPECT_EQ(0, cancel_throttle->will_redirect_calls());
316 415
317 // Resume the request. The callback should have been called. The second 416 // Resume the request. The callback should have been called. The second
318 // throttle should have been notified. 417 // throttle should have been notified.
319 test_handle()->Resume(); 418 test_handle()->Resume();
320 EXPECT_FALSE(IsDeferringStart()); 419 EXPECT_FALSE(IsDeferringStart());
321 EXPECT_TRUE(IsDeferringRedirect()); 420 EXPECT_FALSE(IsDeferringRedirect());
421 EXPECT_TRUE(IsCanceling());
322 EXPECT_TRUE(was_callback_called()); 422 EXPECT_TRUE(was_callback_called());
323 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); 423 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result());
324 EXPECT_EQ(0, defer_throttle->will_start_calls()); 424 EXPECT_EQ(0, defer_throttle->will_start_calls());
325 EXPECT_EQ(1, defer_throttle->will_redirect_calls()); 425 EXPECT_EQ(1, defer_throttle->will_redirect_calls());
326 EXPECT_EQ(0, cancel_throttle->will_start_calls()); 426 EXPECT_EQ(0, cancel_throttle->will_start_calls());
327 EXPECT_EQ(1, cancel_throttle->will_redirect_calls()); 427 EXPECT_EQ(1, cancel_throttle->will_redirect_calls());
328 } 428 }
329 429
330 // Checks that a NavigationThrottle asking to cancel followed by a 430 // Checks that a NavigationThrottle asking to cancel followed by a
331 // NavigationThrottle asking to proceed behave correctly in WillStartRequest. 431 // NavigationThrottle asking to proceed behave correctly in WillStartRequest.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 EXPECT_FALSE(IsDeferringRedirect()); 481 EXPECT_FALSE(IsDeferringRedirect());
382 EXPECT_TRUE(was_callback_called()); 482 EXPECT_TRUE(was_callback_called());
383 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); 483 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result());
384 EXPECT_EQ(0, cancel_throttle->will_start_calls()); 484 EXPECT_EQ(0, cancel_throttle->will_start_calls());
385 EXPECT_EQ(1, cancel_throttle->will_redirect_calls()); 485 EXPECT_EQ(1, cancel_throttle->will_redirect_calls());
386 EXPECT_EQ(0, proceed_throttle->will_start_calls()); 486 EXPECT_EQ(0, proceed_throttle->will_start_calls());
387 EXPECT_EQ(0, proceed_throttle->will_redirect_calls()); 487 EXPECT_EQ(0, proceed_throttle->will_redirect_calls());
388 } 488 }
389 489
390 } // namespace content 490 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698