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

Side by Side Diff: chrome/browser/download/download_request_limiter_unittest.cc

Issue 1964863002: Persist prompt/block download limiter state on renderer-initiated loads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implement DidFinishNavigation Created 4 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/download/download_request_limiter.h" 5 #include "chrome/browser/download/download_request_limiter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "chrome/test/base/testing_profile.h" 14 #include "chrome/test/base/testing_profile.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h" 15 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "content/public/browser/navigation_controller.h" 16 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_details.h" 17 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h" 19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/browser_side_navigation_policy.h"
21 #include "content/public/common/frame_navigate_params.h" 22 #include "content/public/common/frame_navigate_params.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 #if defined(OS_ANDROID) 25 #if defined(OS_ANDROID)
25 #include "chrome/browser/download/download_request_infobar_delegate_android.h" 26 #include "chrome/browser/download/download_request_infobar_delegate_android.h"
26 #include "chrome/browser/infobars/infobar_service.h" 27 #include "chrome/browser/infobars/infobar_service.h"
27 #else 28 #else
28 #include "chrome/browser/download/download_permission_request.h" 29 #include "chrome/browser/download/download_permission_request.h"
29 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" 30 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h"
30 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" 31 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 // Navigate to a new URL with the same host, which shouldn't reset the allow 338 // Navigate to a new URL with the same host, which shouldn't reset the allow
338 // all state. 339 // all state.
339 NavigateAndCommit(GURL("http://fooey.com/bar2")); 340 NavigateAndCommit(GURL("http://fooey.com/bar2"));
340 LoadCompleted(); 341 LoadCompleted();
341 CanDownload(); 342 CanDownload();
342 ExpectAndResetCounts(0, 1, 0, __LINE__); 343 ExpectAndResetCounts(0, 1, 0, __LINE__);
343 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, 344 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
344 download_request_limiter_->GetDownloadStatus(web_contents())); 345 download_request_limiter_->GetDownloadStatus(web_contents()));
345 } 346 }
346 347
348 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_RendererInitiated) {
349 // This test will only work when browser-side navigation enabled, as the
350 // renderer initiated navigation check will always return false otherwise.
351 if (content::IsBrowserSideNavigationEnabled()) {
352 NavigateAndCommit(GURL("http://foo.com/bar"));
353 LoadCompleted();
354
355 // Do one download so we end up in PROMPT.
356 CanDownload();
357 ExpectAndResetCounts(1, 0, 0, __LINE__);
358 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
359 download_request_limiter_->GetDownloadStatus(web_contents()));
360
361 // Set up a pending renderer-initiated download to the same host.
362 content::NavigationController::LoadURLParams load_params(
363 GURL("http://foo.com/bar2"));
364 load_params.is_renderer_initiated = true;
365 load_params.transition_type = ui::PAGE_TRANSITION_GENERATED;
366 load_params.referrer = content::Referrer();
367 controller().LoadURLWithParams(load_params);
368
369 // The state should not be reset.
370 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
371 download_request_limiter_->GetDownloadStatus(web_contents()));
372
373 // Pending renderer-initiated navigation to a different host should not
374 // reset the state.
375 load_params.url = GURL("http://fooey.com/bar");
376 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
377 download_request_limiter_->GetDownloadStatus(web_contents()));
378
379 // Set up a blocked state.
380 UpdateExpectations(CANCEL);
381 CanDownload();
382 ExpectAndResetCounts(0, 1, 1, __LINE__);
383 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
384 download_request_limiter_->GetDownloadStatus(web_contents()));
385
386 // The state should not be reset on a pending renderer-initiated load to
387 // either the same host or a different host.
388 load_params.url = GURL("http://fooeybar.com/bar");
389 controller().LoadURLWithParams(load_params);
390 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
391 download_request_limiter_->GetDownloadStatus(web_contents()));
392
393 load_params.url = GURL("http://foo.com/bar");
394 controller().LoadURLWithParams(load_params);
395 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
396 download_request_limiter_->GetDownloadStatus(web_contents()));
397
398 // Browser-initiated navigation to a different host, which should reset the
399 // state.
400 NavigateAndCommit(GURL("http://foobar.com"));
401 LoadCompleted();
402 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
403 download_request_limiter_->GetDownloadStatus(web_contents()));
404
405 // Set up an allow all state.
406 CanDownload();
407 ExpectAndResetCounts(1, 0, 0, __LINE__);
408 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
409 download_request_limiter_->GetDownloadStatus(web_contents()));
410
411 UpdateExpectations(ACCEPT);
412 CanDownload();
413 ExpectAndResetCounts(1, 0, 1, __LINE__);
414 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
415 download_request_limiter_->GetDownloadStatus(web_contents()));
416
417 // The state should not be reset on a pending renderer-initiated load to
418 // the same host.
419 load_params.url = GURL("http://foobar.com/bar");
420 controller().LoadURLWithParams(load_params);
421 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
422 download_request_limiter_->GetDownloadStatus(web_contents()));
423
424 // But a pending load to a different host should reset the state.
425 load_params.url = GURL("http://foo.com");
426 controller().LoadURLWithParams(load_params);
427 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
428 download_request_limiter_->GetDownloadStatus(web_contents()));
429 }
430 }
431
347 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { 432 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) {
348 NavigateAndCommit(GURL("http://foo.com/bar")); 433 NavigateAndCommit(GURL("http://foo.com/bar"));
349 LoadCompleted(); 434 LoadCompleted();
350 435
351 // Do one download, which should change to prompt before download. 436 // Do one download, which should change to prompt before download.
352 CanDownload(); 437 CanDownload();
353 ExpectAndResetCounts(1, 0, 0, __LINE__); 438 ExpectAndResetCounts(1, 0, 0, __LINE__);
354 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 439 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
355 download_request_limiter_->GetDownloadStatus(web_contents())); 440 download_request_limiter_->GetDownloadStatus(web_contents()));
356 441
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 // changing the content setting). 667 // changing the content setting).
583 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); 668 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK);
584 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 669 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
585 download_request_limiter_->GetDownloadStatus(web_contents())); 670 download_request_limiter_->GetDownloadStatus(web_contents()));
586 UpdateExpectations(WAIT); 671 UpdateExpectations(WAIT);
587 CanDownload(); 672 CanDownload();
588 ExpectAndResetCounts(0, 0, 1, __LINE__); 673 ExpectAndResetCounts(0, 0, 1, __LINE__);
589 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 674 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
590 download_request_limiter_->GetDownloadStatus(web_contents())); 675 download_request_limiter_->GetDownloadStatus(web_contents()));
591 } 676 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698