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

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

Issue 1538933002: Enable download resumption by default. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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 <sstream> 5 #include <sstream>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/feature_list.h"
10 #include "base/files/file.h" 11 #include "base/files/file.h"
11 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
12 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h" 14 #include "base/files/scoped_temp_dir.h"
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/path_service.h" 16 #include "base/path_service.h"
16 #include "base/prefs/pref_service.h" 17 #include "base/prefs/pref_service.h"
17 #include "base/stl_util.h" 18 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h" 20 #include "base/strings/string_split.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 #include "content/public/browser/download_item.h" 76 #include "content/public/browser/download_item.h"
76 #include "content/public/browser/download_manager.h" 77 #include "content/public/browser/download_manager.h"
77 #include "content/public/browser/download_save_info.h" 78 #include "content/public/browser/download_save_info.h"
78 #include "content/public/browser/download_url_parameters.h" 79 #include "content/public/browser/download_url_parameters.h"
79 #include "content/public/browser/notification_source.h" 80 #include "content/public/browser/notification_source.h"
80 #include "content/public/browser/render_frame_host.h" 81 #include "content/public/browser/render_frame_host.h"
81 #include "content/public/browser/render_view_host.h" 82 #include "content/public/browser/render_view_host.h"
82 #include "content/public/browser/render_widget_host.h" 83 #include "content/public/browser/render_widget_host.h"
83 #include "content/public/browser/resource_context.h" 84 #include "content/public/browser/resource_context.h"
84 #include "content/public/browser/web_contents.h" 85 #include "content/public/browser/web_contents.h"
86 #include "content/public/common/content_features.h"
85 #include "content/public/common/content_switches.h" 87 #include "content/public/common/content_switches.h"
86 #include "content/public/common/context_menu_params.h" 88 #include "content/public/common/context_menu_params.h"
87 #include "content/public/test/browser_test_utils.h" 89 #include "content/public/test/browser_test_utils.h"
88 #include "content/public/test/download_test_observer.h" 90 #include "content/public/test/download_test_observer.h"
89 #include "content/public/test/test_file_error_injector.h" 91 #include "content/public/test/test_file_error_injector.h"
90 #include "content/public/test/test_navigation_observer.h" 92 #include "content/public/test/test_navigation_observer.h"
91 #include "extensions/browser/extension_system.h" 93 #include "extensions/browser/extension_system.h"
92 #include "extensions/common/feature_switch.h" 94 #include "extensions/common/feature_switch.h"
93 #include "net/base/filename_util.h" 95 #include "net/base/filename_util.h"
94 #include "net/test/embedded_test_server/embedded_test_server.h" 96 #include "net/test/embedded_test_server/embedded_test_server.h"
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 }; 445 };
444 446
445 struct FileErrorInjectInfo { 447 struct FileErrorInjectInfo {
446 DownloadInfo download_info; 448 DownloadInfo download_info;
447 content::TestFileErrorInjector::FileErrorInfo error_info; 449 content::TestFileErrorInjector::FileErrorInfo error_info;
448 }; 450 };
449 451
450 DownloadTest() {} 452 DownloadTest() {}
451 453
452 void SetUpOnMainThread() override { 454 void SetUpOnMainThread() override {
455 base::FeatureList::ClearInstanceForTesting();
456 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList);
457 feature_list->InitializeFromCommandLine(
458 content::kDownloadResumptionFeature.name, std::string());
459 base::FeatureList::SetInstance(std::move(feature_list));
asanka 2015/12/18 19:25:35 +asvitkine: ^^^
453 BrowserThread::PostTask( 460 BrowserThread::PostTask(
454 BrowserThread::IO, FROM_HERE, 461 BrowserThread::IO, FROM_HERE,
455 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 462 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
456 ASSERT_TRUE(InitialSetup()); 463 ASSERT_TRUE(InitialSetup());
457 } 464 }
458 465
459 void TearDownOnMainThread() override { 466 void TearDownOnMainThread() override {
460 // Needs to be torn down on the main thread. file_activity_observer_ holds a 467 // Needs to be torn down on the main thread. file_activity_observer_ holds a
461 // reference to the ChromeDownloadManagerDelegate which should be destroyed 468 // reference to the ChromeDownloadManagerDelegate which should be destroyed
462 // on the UI thread. 469 // on the UI thread.
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 std::string server_path = "/downloads/"; 887 std::string server_path = "/downloads/";
881 server_path += download_info.url_name; 888 server_path += download_info.url_name;
882 GURL url = embedded_test_server()->GetURL(server_path); 889 GURL url = embedded_test_server()->GetURL(server_path);
883 ASSERT_TRUE(url.is_valid()); 890 ASSERT_TRUE(url.is_valid());
884 891
885 DownloadManager* download_manager = DownloadManagerForBrowser(browser()); 892 DownloadManager* download_manager = DownloadManagerForBrowser(browser());
886 WebContents* web_contents = 893 WebContents* web_contents =
887 browser()->tab_strip_model()->GetActiveWebContents(); 894 browser()->tab_strip_model()->GetActiveWebContents();
888 ASSERT_TRUE(web_contents); 895 ASSERT_TRUE(web_contents);
889 896
890 scoped_ptr<content::DownloadTestObserver> observer( 897 scoped_ptr<content::DownloadTestObserver> observer;
891 new content::DownloadTestObserverTerminal( 898 if (download_info.reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) {
892 download_manager, 899 observer.reset(new content::DownloadTestObserverTerminal(
893 1, 900 download_manager, 1,
894 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); 901 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
902 } else {
903 observer.reset(new content::DownloadTestObserverInterrupted(
904 download_manager, 1,
905 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
906 }
895 907
896 if (download_info.download_method == DOWNLOAD_DIRECT) { 908 if (download_info.download_method == DOWNLOAD_DIRECT) {
897 // Go directly to download. Don't wait for navigation. 909 // Go directly to download. Don't wait for navigation.
898 scoped_refptr<content::DownloadTestItemCreationObserver> 910 scoped_refptr<content::DownloadTestItemCreationObserver>
899 creation_observer(new content::DownloadTestItemCreationObserver); 911 creation_observer(new content::DownloadTestItemCreationObserver);
900 912
901 scoped_ptr<DownloadUrlParameters> params( 913 scoped_ptr<DownloadUrlParameters> params(
902 DownloadUrlParameters::FromWebContents(web_contents, url)); 914 DownloadUrlParameters::FromWebContents(web_contents, url));
903 params->set_callback(creation_observer->callback()); 915 params->set_callback(creation_observer->callback());
904 DownloadManagerForBrowser(browser())->DownloadUrl(params.Pass()); 916 DownloadManagerForBrowser(browser())->DownloadUrl(params.Pass());
(...skipping 830 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 EXPECT_EQ(net::URLRequestSlowDownloadJob::kFirstDownloadSize 1747 EXPECT_EQ(net::URLRequestSlowDownloadJob::kFirstDownloadSize
1736 + net::URLRequestSlowDownloadJob::kSecondDownloadSize, 1748 + net::URLRequestSlowDownloadJob::kSecondDownloadSize,
1737 row.total_bytes); 1749 row.total_bytes);
1738 EXPECT_EQ(history::DownloadState::IN_PROGRESS, row.state); 1750 EXPECT_EQ(history::DownloadState::IN_PROGRESS, row.state);
1739 EXPECT_FALSE(row.opened); 1751 EXPECT_FALSE(row.opened);
1740 1752
1741 // Finish the download. We're ok relying on the history to be flushed 1753 // Finish the download. We're ok relying on the history to be flushed
1742 // at this point as our queries will be behind the history updates 1754 // at this point as our queries will be behind the history updates
1743 // invoked by completion. 1755 // invoked by completion.
1744 scoped_ptr<content::DownloadTestObserver> download_observer( 1756 scoped_ptr<content::DownloadTestObserver> download_observer(
1745 CreateWaiter(browser(), 1)); 1757 new content::DownloadTestObserverInterrupted(
1746 ui_test_utils::NavigateToURL(browser(), 1758 DownloadManagerForBrowser(browser()), 1,
1747 GURL(net::URLRequestSlowDownloadJob::kErrorDownloadUrl)); 1759 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
1760 ui_test_utils::NavigateToURL(
1761 browser(), GURL(net::URLRequestSlowDownloadJob::kErrorDownloadUrl));
1748 download_observer->WaitForFinished(); 1762 download_observer->WaitForFinished();
1749 EXPECT_EQ(1u, download_observer->NumDownloadsSeenInState( 1763 EXPECT_EQ(1u, download_observer->NumDownloadsSeenInState(
1750 DownloadItem::INTERRUPTED)); 1764 DownloadItem::INTERRUPTED));
1751 base::Time end(base::Time::Now()); 1765 base::Time end(base::Time::Now());
1752 1766
1753 // Get what was stored in the history. 1767 // Get what was stored in the history.
1754 ASSERT_TRUE(DownloadsHistoryDataCollector( 1768 ASSERT_TRUE(DownloadsHistoryDataCollector(
1755 browser()->profile()).WaitForDownloadInfo(&downloads_in_database)); 1769 browser()->profile()).WaitForDownloadInfo(&downloads_in_database));
1756 ASSERT_EQ(1u, downloads_in_database->size()); 1770 ASSERT_EQ(1u, downloads_in_database->size());
1757 1771
(...skipping 1309 matching lines...) Expand 10 before | Expand all | Expand 10 after
3067 ASSERT_TRUE(base::GetFileSize( 3081 ASSERT_TRUE(base::GetFileSize(
3068 download_items[0]->GetTargetFilePath(), &downloaded_size)); 3082 download_items[0]->GetTargetFilePath(), &downloaded_size));
3069 ASSERT_EQ(size + 1, downloaded_size); 3083 ASSERT_EQ(size + 1, downloaded_size);
3070 ASSERT_TRUE(base::DieFileDie(file_path, false)); 3084 ASSERT_TRUE(base::DieFileDie(file_path, false));
3071 ASSERT_TRUE(base::DieFileDie(download_items[0]->GetTargetFilePath(), false)); 3085 ASSERT_TRUE(base::DieFileDie(download_items[0]->GetTargetFilePath(), false));
3072 } 3086 }
3073 3087
3074 // A download that is interrupted due to a file error should be able to be 3088 // A download that is interrupted due to a file error should be able to be
3075 // resumed. 3089 // resumed.
3076 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_NoPrompt) { 3090 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_NoPrompt) {
3077 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3078 switches::kEnableDownloadResumption);
3079 scoped_refptr<content::TestFileErrorInjector> error_injector( 3091 scoped_refptr<content::TestFileErrorInjector> error_injector(
3080 content::TestFileErrorInjector::Create( 3092 content::TestFileErrorInjector::Create(
3081 DownloadManagerForBrowser(browser()))); 3093 DownloadManagerForBrowser(browser())));
3082 scoped_ptr<content::DownloadTestObserver> completion_observer( 3094 scoped_ptr<content::DownloadTestObserver> completion_observer(
3083 CreateWaiter(browser(), 1)); 3095 CreateWaiter(browser(), 1));
3084 EnableFileChooser(true); 3096 EnableFileChooser(true);
3085 3097
3086 DownloadItem* download = StartMockDownloadAndInjectError( 3098 DownloadItem* download = StartMockDownloadAndInjectError(
3087 error_injector.get(), content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 3099 error_injector.get(), content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
3088 ASSERT_TRUE(download); 3100 ASSERT_TRUE(download);
3089 3101
3090 download->Resume(); 3102 download->Resume();
3091 completion_observer->WaitForFinished(); 3103 completion_observer->WaitForFinished();
3092 3104
3093 EXPECT_EQ( 3105 EXPECT_EQ(
3094 1u, completion_observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); 3106 1u, completion_observer->NumDownloadsSeenInState(DownloadItem::COMPLETE));
3095 EXPECT_FALSE(DidShowFileChooser()); 3107 EXPECT_FALSE(DidShowFileChooser());
3096 } 3108 }
3097 3109
3098 // A download that's interrupted due to a reason that indicates that the target 3110 // A download that's interrupted due to a reason that indicates that the target
3099 // path is invalid or unusable should cause a prompt to be displayed on 3111 // path is invalid or unusable should cause a prompt to be displayed on
3100 // resumption. 3112 // resumption.
3101 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_WithPrompt) { 3113 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_WithPrompt) {
3102 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3103 switches::kEnableDownloadResumption);
3104 scoped_refptr<content::TestFileErrorInjector> error_injector( 3114 scoped_refptr<content::TestFileErrorInjector> error_injector(
3105 content::TestFileErrorInjector::Create( 3115 content::TestFileErrorInjector::Create(
3106 DownloadManagerForBrowser(browser()))); 3116 DownloadManagerForBrowser(browser())));
3107 scoped_ptr<content::DownloadTestObserver> completion_observer( 3117 scoped_ptr<content::DownloadTestObserver> completion_observer(
3108 CreateWaiter(browser(), 1)); 3118 CreateWaiter(browser(), 1));
3109 EnableFileChooser(true); 3119 EnableFileChooser(true);
3110 3120
3111 DownloadItem* download = StartMockDownloadAndInjectError( 3121 DownloadItem* download = StartMockDownloadAndInjectError(
3112 error_injector.get(), content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE); 3122 error_injector.get(), content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE);
3113 ASSERT_TRUE(download); 3123 ASSERT_TRUE(download);
3114 3124
3115 download->Resume(); 3125 download->Resume();
3116 completion_observer->WaitForFinished(); 3126 completion_observer->WaitForFinished();
3117 3127
3118 EXPECT_EQ( 3128 EXPECT_EQ(
3119 1u, completion_observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); 3129 1u, completion_observer->NumDownloadsSeenInState(DownloadItem::COMPLETE));
3120 EXPECT_TRUE(DidShowFileChooser()); 3130 EXPECT_TRUE(DidShowFileChooser());
3121 } 3131 }
3122 3132
3123 // The user shouldn't be prompted on a resumed download unless a prompt is 3133 // The user shouldn't be prompted on a resumed download unless a prompt is
3124 // necessary due to the interrupt reason. 3134 // necessary due to the interrupt reason.
3125 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_WithPromptAlways) { 3135 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_WithPromptAlways) {
3126 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3127 switches::kEnableDownloadResumption);
3128 browser()->profile()->GetPrefs()->SetBoolean( 3136 browser()->profile()->GetPrefs()->SetBoolean(
3129 prefs::kPromptForDownload, true); 3137 prefs::kPromptForDownload, true);
3130 scoped_refptr<content::TestFileErrorInjector> error_injector( 3138 scoped_refptr<content::TestFileErrorInjector> error_injector(
3131 content::TestFileErrorInjector::Create( 3139 content::TestFileErrorInjector::Create(
3132 DownloadManagerForBrowser(browser()))); 3140 DownloadManagerForBrowser(browser())));
3133 scoped_ptr<content::DownloadTestObserver> completion_observer( 3141 scoped_ptr<content::DownloadTestObserver> completion_observer(
3134 CreateWaiter(browser(), 1)); 3142 CreateWaiter(browser(), 1));
3135 EnableFileChooser(true); 3143 EnableFileChooser(true);
3136 3144
3137 DownloadItem* download = StartMockDownloadAndInjectError( 3145 DownloadItem* download = StartMockDownloadAndInjectError(
3138 error_injector.get(), content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 3146 error_injector.get(), content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
3139 ASSERT_TRUE(download); 3147 ASSERT_TRUE(download);
3140 3148
3141 // Prompts the user initially because of the kPromptForDownload preference. 3149 // Prompts the user initially because of the kPromptForDownload preference.
3142 EXPECT_TRUE(DidShowFileChooser()); 3150 EXPECT_TRUE(DidShowFileChooser());
3143 3151
3144 download->Resume(); 3152 download->Resume();
3145 completion_observer->WaitForFinished(); 3153 completion_observer->WaitForFinished();
3146 3154
3147 EXPECT_EQ( 3155 EXPECT_EQ(
3148 1u, completion_observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); 3156 1u, completion_observer->NumDownloadsSeenInState(DownloadItem::COMPLETE));
3149 // Shouldn't prompt for resumption. 3157 // Shouldn't prompt for resumption.
3150 EXPECT_FALSE(DidShowFileChooser()); 3158 EXPECT_FALSE(DidShowFileChooser());
3151 } 3159 }
3152 3160
3153 // A download that is interrupted due to a transient error should be resumed 3161 // A download that is interrupted due to a transient error should be resumed
3154 // automatically. 3162 // automatically.
3155 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_Automatic) { 3163 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_Automatic) {
3156 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3157 switches::kEnableDownloadResumption);
3158 scoped_refptr<content::TestFileErrorInjector> error_injector( 3164 scoped_refptr<content::TestFileErrorInjector> error_injector(
3159 content::TestFileErrorInjector::Create( 3165 content::TestFileErrorInjector::Create(
3160 DownloadManagerForBrowser(browser()))); 3166 DownloadManagerForBrowser(browser())));
3161 3167
3162 DownloadItem* download = StartMockDownloadAndInjectError( 3168 DownloadItem* download = StartMockDownloadAndInjectError(
3163 error_injector.get(), 3169 error_injector.get(),
3164 content::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 3170 content::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
3165 ASSERT_TRUE(download); 3171 ASSERT_TRUE(download);
3166 3172
3167 // The number of times this the download is resumed automatically is defined 3173 // The number of times this the download is resumed automatically is defined
3168 // in DownloadItemImpl::kMaxAutoResumeAttempts. The number of DownloadFiles 3174 // in DownloadItemImpl::kMaxAutoResumeAttempts. The number of DownloadFiles
3169 // created should be that number + 1 (for the original download request). We 3175 // created should be that number + 1 (for the original download request). We
3170 // only care that it is greater than 1. 3176 // only care that it is greater than 1.
3171 EXPECT_GT(1u, error_injector->TotalFileCount()); 3177 EXPECT_GT(1u, error_injector->TotalFileCount());
3172 } 3178 }
3173 3179
3174 // An interrupting download should be resumable multiple times. 3180 // An interrupting download should be resumable multiple times.
3175 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_MultipleAttempts) { 3181 IN_PROC_BROWSER_TEST_F(DownloadTest, Resumption_MultipleAttempts) {
3176 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3177 switches::kEnableDownloadResumption);
3178 scoped_refptr<content::TestFileErrorInjector> error_injector( 3182 scoped_refptr<content::TestFileErrorInjector> error_injector(
3179 content::TestFileErrorInjector::Create( 3183 content::TestFileErrorInjector::Create(
3180 DownloadManagerForBrowser(browser()))); 3184 DownloadManagerForBrowser(browser())));
3181 scoped_ptr<DownloadTestObserverNotInProgress> completion_observer( 3185 scoped_ptr<DownloadTestObserverNotInProgress> completion_observer(
3182 new DownloadTestObserverNotInProgress( 3186 new DownloadTestObserverNotInProgress(
3183 DownloadManagerForBrowser(browser()), 1)); 3187 DownloadManagerForBrowser(browser()), 1));
3184 // Wait for two transitions to a resumable state 3188 // Wait for two transitions to a resumable state
3185 scoped_ptr<content::DownloadTestObserver> resumable_observer( 3189 scoped_ptr<content::DownloadTestObserver> resumable_observer(
3186 new DownloadTestObserverResumable( 3190 new DownloadTestObserverResumable(
3187 DownloadManagerForBrowser(browser()), 2)); 3191 DownloadManagerForBrowser(browser()), 2));
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after
3675 3679
3676 scoped_ptr<content::DownloadTestObserver> observer(DangerousDownloadWaiter( 3680 scoped_ptr<content::DownloadTestObserver> observer(DangerousDownloadWaiter(
3677 browser(), 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_DENY)); 3681 browser(), 1, content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_DENY));
3678 ui_test_utils::NavigateToURL(browser(), extension_url); 3682 ui_test_utils::NavigateToURL(browser(), extension_url);
3679 3683
3680 observer->WaitForFinished(); 3684 observer->WaitForFinished();
3681 3685
3682 // Download shelf should close. 3686 // Download shelf should close.
3683 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 3687 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
3684 } 3688 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698