Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <string> | |
| 9 | |
| 8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ptr_util.h" | |
| 12 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 16 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 19 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 17 #include "chrome/browser/download/download_item_model.h" | 20 #include "chrome/browser/download/download_item_model.h" |
| 18 #include "chrome/browser/download/download_prefs.h" | 21 #include "chrome/browser/download/download_prefs.h" |
| 19 #include "chrome/browser/download/download_target_info.h" | 22 #include "chrome/browser/download/download_target_info.h" |
| 23 #include "chrome/common/features.h" | |
| 20 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 24 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 28 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 25 #include "content/public/browser/download_interrupt_reasons.h" | 29 #include "content/public/browser/download_interrupt_reasons.h" |
| 26 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/browser/web_contents_delegate.h" | 31 #include "content/public/browser/web_contents_delegate.h" |
| 28 #include "content/public/test/mock_download_item.h" | 32 #include "content/public/test/mock_download_item.h" |
| 29 #include "content/public/test/mock_download_manager.h" | 33 #include "content/public/test/mock_download_manager.h" |
| 30 #include "content/public/test/test_renderer_host.h" | 34 #include "content/public/test/test_renderer_host.h" |
| 31 #include "content/public/test/web_contents_tester.h" | 35 #include "content/public/test/web_contents_tester.h" |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 36 #include "testing/gmock/include/gmock/gmock.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 38 |
| 35 #if defined(FULL_SAFE_BROWSING) | 39 #if defined(FULL_SAFE_BROWSING) |
| 36 #include "chrome/browser/safe_browsing/download_protection_service.h" | 40 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 37 #endif | 41 #endif |
| 38 | 42 |
| 39 #if !defined(OS_ANDROID) | 43 #if defined(ENABLE_PLUGINS) |
| 40 #include "content/public/browser/plugin_service.h" | 44 #include "content/public/browser/plugin_service.h" |
| 41 #endif | 45 #endif |
| 42 | 46 |
| 47 #if BUILDFLAG(ANDROID_JAVA_UI) | |
| 48 #include "chrome/browser/infobars/infobar_service.h" | |
| 49 #include "components/infobars/core/infobar.h" | |
| 50 #include "components/infobars/core/infobar_delegate.h" | |
| 51 #include "components/infobars/core/infobar_manager.h" | |
| 52 #endif | |
| 53 | |
| 43 using ::testing::AtMost; | 54 using ::testing::AtMost; |
| 44 using ::testing::Invoke; | 55 using ::testing::Invoke; |
| 45 using ::testing::Ref; | 56 using ::testing::Ref; |
| 46 using ::testing::Return; | 57 using ::testing::Return; |
| 47 using ::testing::ReturnPointee; | 58 using ::testing::ReturnPointee; |
| 48 using ::testing::ReturnRef; | 59 using ::testing::ReturnRef; |
| 49 using ::testing::ReturnRefOfCopy; | 60 using ::testing::ReturnRefOfCopy; |
| 50 using ::testing::SetArgPointee; | 61 using ::testing::SetArgPointee; |
| 51 using ::testing::WithArg; | 62 using ::testing::WithArg; |
| 52 using ::testing::_; | 63 using ::testing::_; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 76 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 87 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 77 base::Bind(arg0, result)); | 88 base::Bind(arg0, result)); |
| 78 } | 89 } |
| 79 | 90 |
| 80 // Similar to ScheduleCallback, but binds 2 arguments. | 91 // Similar to ScheduleCallback, but binds 2 arguments. |
| 81 ACTION_P2(ScheduleCallback2, result0, result1) { | 92 ACTION_P2(ScheduleCallback2, result0, result1) { |
| 82 base::ThreadTaskRunnerHandle::Get()->PostTask( | 93 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 83 FROM_HERE, base::Bind(arg0, result0, result1)); | 94 FROM_HERE, base::Bind(arg0, result0, result1)); |
| 84 } | 95 } |
| 85 | 96 |
| 97 // Struct for holding the result of calling DetermineDownloadTarget. | |
| 98 struct DetermineDownloadTargetResult { | |
| 99 base::FilePath target_path; | |
| 100 content::DownloadItem::TargetDisposition disposition = | |
| 101 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE; | |
| 102 content::DownloadDangerType danger_type = | |
| 103 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; | |
| 104 base::FilePath intermediate_path; | |
| 105 content::DownloadInterruptReason interrupt_reason = | |
| 106 content::DOWNLOAD_INTERRUPT_REASON_NONE; | |
| 107 }; | |
| 108 | |
| 86 // Subclass of the ChromeDownloadManagerDelegate that uses a mock | 109 // Subclass of the ChromeDownloadManagerDelegate that uses a mock |
| 87 // DownloadProtectionService. | 110 // DownloadProtectionService. |
| 88 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { | 111 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
| 89 public: | 112 public: |
| 90 explicit TestChromeDownloadManagerDelegate(Profile* profile) | 113 explicit TestChromeDownloadManagerDelegate(Profile* profile) |
| 91 : ChromeDownloadManagerDelegate(profile) { | 114 : ChromeDownloadManagerDelegate(profile) { |
| 92 ON_CALL(*this, MockCheckDownloadUrl(_, _)) | 115 ON_CALL(*this, MockCheckDownloadUrl(_, _)) |
| 93 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); | 116 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
| 94 ON_CALL(*this, GetDownloadProtectionService()) | 117 ON_CALL(*this, GetDownloadProtectionService()) |
| 95 .WillByDefault(Return(nullptr)); | 118 .WillByDefault(Return(nullptr)); |
| 119 ON_CALL(*this, MockReserveVirtualPath(_, _, _, _, _)) | |
| 120 .WillByDefault(::testing::DoAll( | |
| 121 ::testing::SetArgPointee<4>(DownloadTargetResult::SUCCESS), | |
| 122 ::testing::ReturnArg<1>())); | |
| 96 } | 123 } |
| 97 | 124 |
| 98 ~TestChromeDownloadManagerDelegate() override {} | 125 ~TestChromeDownloadManagerDelegate() override {} |
| 99 | 126 |
| 100 void NotifyExtensions(content::DownloadItem* download, | 127 void NotifyExtensions(content::DownloadItem* download, |
| 101 const base::FilePath& suggested_virtual_path, | 128 const base::FilePath& suggested_virtual_path, |
| 102 const NotifyExtensionsCallback& callback) override { | 129 const NotifyExtensionsCallback& callback) override { |
| 103 callback.Run(base::FilePath(), | 130 callback.Run(base::FilePath(), |
| 104 DownloadPathReservationTracker::UNIQUIFY); | 131 DownloadPathReservationTracker::UNIQUIFY); |
| 105 } | 132 } |
| 106 | 133 |
| 107 void ReserveVirtualPath( | 134 void ReserveVirtualPath( |
| 108 content::DownloadItem* download, | 135 content::DownloadItem* download, |
| 109 const base::FilePath& virtual_path, | 136 const base::FilePath& virtual_path, |
| 110 bool create_directory, | 137 bool create_directory, |
| 111 DownloadPathReservationTracker::FilenameConflictAction conflict_action, | 138 DownloadPathReservationTracker::FilenameConflictAction conflict_action, |
| 112 const DownloadPathReservationTracker::ReservedPathCallback& callback) | 139 const DownloadPathReservationTracker::ReservedPathCallback& callback) |
| 113 override { | 140 override { |
| 114 // Pretend the path reservation succeeded without any change to | 141 DownloadTargetResult result = DownloadTargetResult::SUCCESS; |
| 115 // |target_path|. | 142 base::FilePath path_to_return = MockReserveVirtualPath( |
| 143 download, virtual_path, create_directory, conflict_action, &result); | |
| 116 base::ThreadTaskRunnerHandle::Get()->PostTask( | 144 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 117 FROM_HERE, base::Bind(callback, virtual_path, true)); | 145 FROM_HERE, base::Bind(callback, path_to_return, result)); |
| 118 } | 146 } |
| 119 | 147 |
| 120 void PromptUserForDownloadPath( | 148 MOCK_METHOD5( |
| 121 DownloadItem* download, | 149 MockReserveVirtualPath, |
| 122 const base::FilePath& suggested_path, | 150 base::FilePath(content::DownloadItem*, |
| 123 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback) | 151 const base::FilePath&, |
| 124 override { | 152 bool, |
| 125 base::FilePath return_path = MockPromptUserForDownloadPath(download, | 153 DownloadPathReservationTracker::FilenameConflictAction, |
| 126 suggested_path, | 154 DownloadTargetResult*)); |
| 127 callback); | |
| 128 callback.Run(return_path); | |
| 129 } | |
| 130 | 155 |
| 131 void CheckDownloadUrl(DownloadItem* download, | 156 void CheckDownloadUrl(DownloadItem* download, |
| 132 const base::FilePath& virtual_path, | 157 const base::FilePath& virtual_path, |
| 133 const CheckDownloadUrlCallback& callback) override { | 158 const CheckDownloadUrlCallback& callback) override { |
| 134 callback.Run(MockCheckDownloadUrl(download, virtual_path)); | 159 callback.Run(MockCheckDownloadUrl(download, virtual_path)); |
| 135 } | 160 } |
| 136 | 161 |
| 137 MOCK_METHOD0(GetDownloadProtectionService, | 162 MOCK_METHOD0(GetDownloadProtectionService, |
| 138 safe_browsing::DownloadProtectionService*()); | 163 safe_browsing::DownloadProtectionService*()); |
| 139 | 164 |
| 140 MOCK_METHOD3( | 165 MOCK_METHOD4( |
| 141 MockPromptUserForDownloadPath, | 166 RequestConfirmation, |
| 142 base::FilePath( | 167 void(DownloadItem*, |
| 143 DownloadItem*, | 168 const base::FilePath&, |
| 144 const base::FilePath&, | 169 DownloadConfirmationReason, |
| 145 const DownloadTargetDeterminerDelegate::FileSelectedCallback&)); | 170 const DownloadTargetDeterminerDelegate::FileSelectedCallback&)); |
| 146 | 171 |
| 147 MOCK_METHOD2(MockCheckDownloadUrl, | 172 MOCK_METHOD2(MockCheckDownloadUrl, |
| 148 content::DownloadDangerType(DownloadItem*, | 173 content::DownloadDangerType(DownloadItem*, |
| 149 const base::FilePath&)); | 174 const base::FilePath&)); |
| 175 | |
| 176 void RequestConfirmationConcrete( | |
| 177 DownloadItem* download_item, | |
| 178 const base::FilePath& path, | |
| 179 DownloadConfirmationReason reason, | |
| 180 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback) { | |
| 181 ChromeDownloadManagerDelegate::RequestConfirmation(download_item, path, | |
| 182 reason, callback); | |
| 183 } | |
| 150 }; | 184 }; |
| 151 | 185 |
| 152 class ChromeDownloadManagerDelegateTest | 186 class ChromeDownloadManagerDelegateTest |
| 153 : public ChromeRenderViewHostTestHarness { | 187 : public ChromeRenderViewHostTestHarness { |
| 154 public: | 188 public: |
| 189 // Result of calling DetermineDownloadTarget. | |
| 155 ChromeDownloadManagerDelegateTest(); | 190 ChromeDownloadManagerDelegateTest(); |
| 156 | 191 |
| 157 // ::testing::Test | 192 // ::testing::Test |
| 158 void SetUp() override; | 193 void SetUp() override; |
| 159 void TearDown() override; | 194 void TearDown() override; |
| 160 | 195 |
| 161 // Verifies and clears test expectations for |delegate_| and | 196 // Verifies and clears test expectations for |delegate_| and |
| 162 // |download_manager_|. | 197 // |download_manager_|. |
| 163 void VerifyAndClearExpectations(); | 198 void VerifyAndClearExpectations(); |
| 164 | 199 |
| 165 // Creates MockDownloadItem and sets up default expectations. | 200 // Creates MockDownloadItem and sets up default expectations. |
| 166 std::unique_ptr<content::MockDownloadItem> CreateActiveDownloadItem( | 201 std::unique_ptr<content::MockDownloadItem> CreateActiveDownloadItem( |
| 167 int32_t id); | 202 int32_t id); |
| 168 | 203 |
| 169 // Given the relative path |path|, returns the full path under the temporary | 204 // Given the relative path |path|, returns the full path under the temporary |
| 170 // downloads directory. | 205 // downloads directory. |
| 171 base::FilePath GetPathInDownloadDir(const char* path); | 206 base::FilePath GetPathInDownloadDir(const char* path); |
| 172 | 207 |
| 173 // Set the kDownloadDefaultDirectory user preference to |path|. | 208 // Set the kDownloadDefaultDirectory user preference to |path|. |
| 174 void SetDefaultDownloadPath(const base::FilePath& path); | 209 void SetDefaultDownloadPath(const base::FilePath& path); |
| 175 | 210 |
| 176 void DetermineDownloadTarget(DownloadItem* download, | 211 void DetermineDownloadTarget(DownloadItem* download, |
| 177 DownloadTargetInfo* result); | 212 DetermineDownloadTargetResult* result); |
| 178 | 213 |
| 179 // Invokes ChromeDownloadManagerDelegate::CheckForFileExistence and waits for | 214 // Invokes ChromeDownloadManagerDelegate::CheckForFileExistence and waits for |
| 180 // the asynchronous callback. The result passed into | 215 // the asynchronous callback. The result passed into |
| 181 // content::CheckForFileExistenceCallback is the return value from this | 216 // content::CheckForFileExistenceCallback is the return value from this |
| 182 // method. | 217 // method. |
| 183 bool CheckForFileExistence(DownloadItem* download); | 218 bool CheckForFileExistence(DownloadItem* download); |
| 184 | 219 |
| 185 const base::FilePath& default_download_path() const; | 220 const base::FilePath& default_download_path() const; |
| 186 TestChromeDownloadManagerDelegate* delegate(); | 221 TestChromeDownloadManagerDelegate* delegate(); |
| 187 content::MockDownloadManager* download_manager(); | 222 content::MockDownloadManager* download_manager(); |
| 188 DownloadPrefs* download_prefs(); | 223 DownloadPrefs* download_prefs(); |
| 189 | 224 |
| 190 private: | 225 private: |
| 191 syncable_prefs::TestingPrefServiceSyncable* pref_service_; | 226 syncable_prefs::TestingPrefServiceSyncable* pref_service_; |
| 192 base::ScopedTempDir test_download_dir_; | 227 base::ScopedTempDir test_download_dir_; |
| 193 std::unique_ptr<content::MockDownloadManager> download_manager_; | 228 std::unique_ptr<content::MockDownloadManager> download_manager_; |
| 194 std::unique_ptr<TestChromeDownloadManagerDelegate> delegate_; | 229 std::unique_ptr<TestChromeDownloadManagerDelegate> delegate_; |
| 195 MockWebContentsDelegate web_contents_delegate_; | 230 MockWebContentsDelegate web_contents_delegate_; |
| 196 }; | 231 }; |
| 197 | 232 |
| 198 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() | 233 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() |
| 199 : download_manager_(new ::testing::NiceMock<content::MockDownloadManager>) { | 234 : download_manager_(new ::testing::NiceMock<content::MockDownloadManager>) { |
| 200 } | 235 } |
| 201 | 236 |
| 202 void ChromeDownloadManagerDelegateTest::SetUp() { | 237 void ChromeDownloadManagerDelegateTest::SetUp() { |
| 203 ChromeRenderViewHostTestHarness::SetUp(); | 238 ChromeRenderViewHostTestHarness::SetUp(); |
| 204 | 239 |
| 205 CHECK(profile()); | 240 CHECK(profile()); |
| 206 delegate_.reset(new TestChromeDownloadManagerDelegate(profile())); | 241 delegate_ = |
| 242 base::MakeUnique<::testing::NiceMock<TestChromeDownloadManagerDelegate>>( | |
| 243 profile()); | |
| 207 delegate_->SetDownloadManager(download_manager_.get()); | 244 delegate_->SetDownloadManager(download_manager_.get()); |
| 208 pref_service_ = profile()->GetTestingPrefService(); | 245 pref_service_ = profile()->GetTestingPrefService(); |
| 209 web_contents()->SetDelegate(&web_contents_delegate_); | 246 web_contents()->SetDelegate(&web_contents_delegate_); |
| 210 | 247 |
| 211 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); | 248 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); |
| 212 SetDefaultDownloadPath(test_download_dir_.GetPath()); | 249 SetDefaultDownloadPath(test_download_dir_.GetPath()); |
| 213 } | 250 } |
| 214 | 251 |
| 215 void ChromeDownloadManagerDelegateTest::TearDown() { | 252 void ChromeDownloadManagerDelegateTest::TearDown() { |
| 216 base::RunLoop().RunUntilIdle(); | 253 base::RunLoop().RunUntilIdle(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 test_download_dir_.GetPath().AppendASCII(relative_path); | 304 test_download_dir_.GetPath().AppendASCII(relative_path); |
| 268 return full_path.NormalizePathSeparators(); | 305 return full_path.NormalizePathSeparators(); |
| 269 } | 306 } |
| 270 | 307 |
| 271 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath( | 308 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath( |
| 272 const base::FilePath& path) { | 309 const base::FilePath& path) { |
| 273 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path); | 310 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path); |
| 274 pref_service_->SetFilePath(prefs::kSaveFileDefaultDirectory, path); | 311 pref_service_->SetFilePath(prefs::kSaveFileDefaultDirectory, path); |
| 275 } | 312 } |
| 276 | 313 |
| 277 void StoreDownloadTargetInfo(const base::Closure& closure, | 314 void StoreDownloadTargetInfo( |
| 278 DownloadTargetInfo* target_info, | 315 const base::Closure& closure, |
| 279 const base::FilePath& target_path, | 316 DetermineDownloadTargetResult* result, |
| 280 DownloadItem::TargetDisposition target_disposition, | 317 const base::FilePath& target_path, |
| 281 content::DownloadDangerType danger_type, | 318 DownloadItem::TargetDisposition target_disposition, |
| 282 const base::FilePath& intermediate_path) { | 319 content::DownloadDangerType danger_type, |
| 283 target_info->target_path = target_path; | 320 const base::FilePath& intermediate_path, |
| 284 target_info->target_disposition = target_disposition; | 321 content::DownloadInterruptReason interrupt_reason) { |
| 285 target_info->danger_type = danger_type; | 322 result->target_path = target_path; |
| 286 target_info->intermediate_path = intermediate_path; | 323 result->disposition = target_disposition; |
| 324 result->danger_type = danger_type; | |
| 325 result->intermediate_path = intermediate_path; | |
| 326 result->interrupt_reason = interrupt_reason; | |
| 287 closure.Run(); | 327 closure.Run(); |
| 288 } | 328 } |
| 289 | 329 |
| 290 void ChromeDownloadManagerDelegateTest::DetermineDownloadTarget( | 330 void ChromeDownloadManagerDelegateTest::DetermineDownloadTarget( |
| 291 DownloadItem* download_item, | 331 DownloadItem* download_item, |
| 292 DownloadTargetInfo* result) { | 332 DetermineDownloadTargetResult* result) { |
| 293 base::RunLoop loop_runner; | 333 base::RunLoop loop_runner; |
| 294 delegate()->DetermineDownloadTarget( | 334 delegate()->DetermineDownloadTarget( |
| 295 download_item, | 335 download_item, |
| 296 base::Bind(&StoreDownloadTargetInfo, loop_runner.QuitClosure(), result)); | 336 base::Bind(&StoreDownloadTargetInfo, loop_runner.QuitClosure(), result)); |
| 297 loop_runner.Run(); | 337 loop_runner.Run(); |
| 298 } | 338 } |
| 299 | 339 |
| 300 void StoreBoolAndRunClosure(const base::Closure& closure, | 340 void StoreBoolAndRunClosure(const base::Closure& closure, |
| 301 bool* result_storage, | 341 bool* result_storage, |
| 302 bool result) { | 342 bool result) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 329 ChromeDownloadManagerDelegateTest::download_manager() { | 369 ChromeDownloadManagerDelegateTest::download_manager() { |
| 330 return download_manager_.get(); | 370 return download_manager_.get(); |
| 331 } | 371 } |
| 332 | 372 |
| 333 DownloadPrefs* ChromeDownloadManagerDelegateTest::download_prefs() { | 373 DownloadPrefs* ChromeDownloadManagerDelegateTest::download_prefs() { |
| 334 return delegate_->download_prefs(); | 374 return delegate_->download_prefs(); |
| 335 } | 375 } |
| 336 | 376 |
| 337 } // namespace | 377 } // namespace |
| 338 | 378 |
| 339 // There is no "save as" context menu option on Android. | 379 TEST_F(ChromeDownloadManagerDelegateTest, LastSavePath) { |
| 340 #if !BUILDFLAG(ANDROID_JAVA_UI) | |
| 341 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_LastSavePath) { | |
| 342 GURL download_url("http://example.com/foo.txt"); | 380 GURL download_url("http://example.com/foo.txt"); |
| 343 | 381 |
| 344 std::unique_ptr<content::MockDownloadItem> save_as_download = | 382 std::unique_ptr<content::MockDownloadItem> save_as_download = |
| 345 CreateActiveDownloadItem(0); | 383 CreateActiveDownloadItem(0); |
| 346 EXPECT_CALL(*save_as_download, GetURL()) | 384 EXPECT_CALL(*save_as_download, GetURL()) |
| 347 .Times(::testing::AnyNumber()) | 385 .Times(::testing::AnyNumber()) |
| 348 .WillRepeatedly(ReturnRef(download_url)); | 386 .WillRepeatedly(ReturnRef(download_url)); |
| 349 EXPECT_CALL(*save_as_download, GetTargetDisposition()) | 387 EXPECT_CALL(*save_as_download, GetTargetDisposition()) |
| 350 .Times(::testing::AnyNumber()) | 388 .Times(::testing::AnyNumber()) |
| 351 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_PROMPT)); | 389 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_PROMPT)); |
| 352 | 390 |
| 353 std::unique_ptr<content::MockDownloadItem> automatic_download = | 391 std::unique_ptr<content::MockDownloadItem> automatic_download = |
| 354 CreateActiveDownloadItem(1); | 392 CreateActiveDownloadItem(1); |
| 355 EXPECT_CALL(*automatic_download, GetURL()) | 393 EXPECT_CALL(*automatic_download, GetURL()) |
| 356 .Times(::testing::AnyNumber()) | 394 .Times(::testing::AnyNumber()) |
| 357 .WillRepeatedly(ReturnRef(download_url)); | 395 .WillRepeatedly(ReturnRef(download_url)); |
| 358 EXPECT_CALL(*automatic_download, GetTargetDisposition()) | 396 EXPECT_CALL(*automatic_download, GetTargetDisposition()) |
| 359 .Times(::testing::AnyNumber()) | 397 .Times(::testing::AnyNumber()) |
| 360 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | 398 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
| 361 | 399 |
| 362 { | 400 { |
| 363 // When the prompt is displayed for the first download, the user selects a | 401 // When the prompt is displayed for the first download, the user selects a |
| 364 // path in a different directory. | 402 // path in a different directory. |
| 365 DownloadTargetInfo result; | 403 DetermineDownloadTargetResult result; |
| 366 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); | 404 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); |
| 367 base::FilePath user_selected_path(GetPathInDownloadDir("bar/baz.txt")); | 405 base::FilePath user_selected_path(GetPathInDownloadDir("bar/baz.txt")); |
| 368 EXPECT_CALL(*delegate(), | 406 EXPECT_CALL(*delegate(), RequestConfirmation(save_as_download.get(), |
| 369 MockPromptUserForDownloadPath(save_as_download.get(), | 407 expected_prompt_path, _, _)) |
| 370 expected_prompt_path, _)) | 408 .WillOnce(WithArg<3>(ScheduleCallback2( |
| 371 .WillOnce(Return(user_selected_path)); | 409 DownloadConfirmationResult::CONFIRMED, user_selected_path))); |
| 372 DetermineDownloadTarget(save_as_download.get(), &result); | 410 DetermineDownloadTarget(save_as_download.get(), &result); |
| 373 EXPECT_EQ(user_selected_path, result.target_path); | 411 EXPECT_EQ(user_selected_path, result.target_path); |
| 374 VerifyAndClearExpectations(); | 412 VerifyAndClearExpectations(); |
| 375 } | 413 } |
| 376 | 414 |
| 377 { | 415 { |
| 378 // The prompt path for the second download is the user selected directroy | 416 // The prompt path for the second download is the user selected directory |
| 379 // from the previous download. | 417 // from the previous download. |
| 380 DownloadTargetInfo result; | 418 DetermineDownloadTargetResult result; |
| 381 base::FilePath expected_prompt_path(GetPathInDownloadDir("bar/foo.txt")); | 419 base::FilePath expected_prompt_path(GetPathInDownloadDir("bar/foo.txt")); |
| 382 EXPECT_CALL(*delegate(), | 420 EXPECT_CALL(*delegate(), RequestConfirmation(save_as_download.get(), |
| 383 MockPromptUserForDownloadPath(save_as_download.get(), | 421 expected_prompt_path, _, _)) |
| 384 expected_prompt_path, _)) | 422 .WillOnce(WithArg<3>(ScheduleCallback2( |
| 385 .WillOnce(Return(base::FilePath())); | 423 DownloadConfirmationResult::CANCELED, base::FilePath()))); |
| 386 DetermineDownloadTarget(save_as_download.get(), &result); | 424 DetermineDownloadTarget(save_as_download.get(), &result); |
| 387 VerifyAndClearExpectations(); | 425 VerifyAndClearExpectations(); |
| 388 } | 426 } |
| 389 | 427 |
| 390 { | 428 { |
| 391 // Start an automatic download. This one should get the default download | 429 // Start an automatic download. This one should get the default download |
| 392 // path since the last download path only affects Save As downloads. | 430 // path since the last download path only affects Save As downloads. |
| 393 DownloadTargetInfo result; | 431 DetermineDownloadTargetResult result; |
| 394 base::FilePath expected_path(GetPathInDownloadDir("foo.txt")); | 432 base::FilePath expected_path(GetPathInDownloadDir("foo.txt")); |
| 395 DetermineDownloadTarget(automatic_download.get(), &result); | 433 DetermineDownloadTarget(automatic_download.get(), &result); |
| 396 EXPECT_EQ(expected_path, result.target_path); | 434 EXPECT_EQ(expected_path, result.target_path); |
| 397 VerifyAndClearExpectations(); | 435 VerifyAndClearExpectations(); |
| 398 } | 436 } |
| 399 | 437 |
| 400 { | 438 { |
| 401 // The prompt path for the next download should be the default. | 439 // The prompt path for the next download should be the default. |
| 402 download_prefs()->SetSaveFilePath(download_prefs()->DownloadPath()); | 440 download_prefs()->SetSaveFilePath(download_prefs()->DownloadPath()); |
| 403 DownloadTargetInfo result; | 441 DetermineDownloadTargetResult result; |
| 404 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); | 442 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); |
| 405 EXPECT_CALL(*delegate(), | 443 EXPECT_CALL(*delegate(), RequestConfirmation(save_as_download.get(), |
| 406 MockPromptUserForDownloadPath(save_as_download.get(), | 444 expected_prompt_path, _, _)) |
| 407 expected_prompt_path, _)) | 445 .WillOnce(WithArg<3>(ScheduleCallback2( |
| 408 .WillOnce(Return(base::FilePath())); | 446 DownloadConfirmationResult::CANCELED, base::FilePath()))); |
| 409 DetermineDownloadTarget(save_as_download.get(), &result); | 447 DetermineDownloadTarget(save_as_download.get(), &result); |
| 410 VerifyAndClearExpectations(); | 448 VerifyAndClearExpectations(); |
| 411 } | 449 } |
| 412 } | 450 } |
| 413 #endif // !BUILDFLAG(ANDROID_JAVA_UI) | 451 |
| 452 TEST_F(ChromeDownloadManagerDelegateTest, ConflictAction) { | |
| 453 const GURL kUrl("http://example.com/foo"); | |
| 454 const std::string kTargetDisposition("attachment; filename=\"foo.txt\""); | |
| 455 | |
| 456 std::unique_ptr<content::MockDownloadItem> download_item = | |
| 457 CreateActiveDownloadItem(0); | |
| 458 EXPECT_CALL(*download_item, GetURL()).WillRepeatedly(ReturnRef(kUrl)); | |
| 459 EXPECT_CALL(*download_item, GetContentDisposition()) | |
| 460 .WillRepeatedly(Return(kTargetDisposition)); | |
| 461 | |
| 462 base::FilePath kExpectedPath = GetPathInDownloadDir("bar.txt"); | |
| 463 | |
| 464 DetermineDownloadTargetResult result; | |
| 465 | |
| 466 EXPECT_CALL(*delegate(), MockReserveVirtualPath(_, _, _, _, _)) | |
| 467 .WillOnce(::testing::DoAll( | |
| 468 ::testing::SetArgPointee<4>(DownloadTargetResult::CONFLICT), | |
| 469 ::testing::ReturnArg<1>())); | |
| 470 EXPECT_CALL( | |
| 471 *delegate(), | |
| 472 RequestConfirmation(_, _, DownloadConfirmationReason::TARGET_CONFLICT, _)) | |
| 473 .WillOnce(WithArg<3>(ScheduleCallback2( | |
| 474 DownloadConfirmationResult::CONFIRMED, kExpectedPath))); | |
| 475 DetermineDownloadTarget(download_item.get(), &result); | |
| 476 EXPECT_EQ(content::DownloadItem::TARGET_DISPOSITION_PROMPT, | |
| 477 result.disposition); | |
| 478 EXPECT_EQ(kExpectedPath, result.target_path); | |
| 479 | |
| 480 VerifyAndClearExpectations(); | |
| 481 } | |
| 414 | 482 |
| 415 TEST_F(ChromeDownloadManagerDelegateTest, MaybeDangerousContent) { | 483 TEST_F(ChromeDownloadManagerDelegateTest, MaybeDangerousContent) { |
| 416 #if !defined(OS_ANDROID) | 484 #if defined(ENABLE_PLUGINS) |
| 417 content::PluginService::GetInstance()->Init(); | 485 content::PluginService::GetInstance()->Init(); |
| 418 #endif | 486 #endif |
| 419 | 487 |
| 420 GURL url("http://example.com/foo"); | 488 GURL url("http://example.com/foo"); |
| 421 | 489 |
| 422 std::unique_ptr<content::MockDownloadItem> download_item = | 490 std::unique_ptr<content::MockDownloadItem> download_item = |
| 423 CreateActiveDownloadItem(0); | 491 CreateActiveDownloadItem(0); |
| 424 EXPECT_CALL(*download_item, GetURL()).WillRepeatedly(ReturnRef(url)); | 492 EXPECT_CALL(*download_item, GetURL()).WillRepeatedly(ReturnRef(url)); |
| 425 EXPECT_CALL(*download_item, GetTargetDisposition()) | 493 EXPECT_CALL(*download_item, GetTargetDisposition()) |
| 426 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | 494 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
| 427 EXPECT_CALL(*delegate(), MockCheckDownloadUrl(_, _)) | 495 EXPECT_CALL(*delegate(), MockCheckDownloadUrl(_, _)) |
| 428 .WillRepeatedly( | 496 .WillRepeatedly( |
| 429 Return(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT)); | 497 Return(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT)); |
| 430 | 498 |
| 431 { | 499 { |
| 432 const std::string kDangerousContentDisposition( | 500 const std::string kDangerousContentDisposition( |
| 433 "attachment; filename=\"foo.swf\""); | 501 "attachment; filename=\"foo.swf\""); |
| 434 EXPECT_CALL(*download_item, GetContentDisposition()) | 502 EXPECT_CALL(*download_item, GetContentDisposition()) |
| 435 .WillRepeatedly(Return(kDangerousContentDisposition)); | 503 .WillRepeatedly(Return(kDangerousContentDisposition)); |
| 436 DownloadTargetInfo target_info; | 504 DetermineDownloadTargetResult result; |
| 437 DetermineDownloadTarget(download_item.get(), &target_info); | 505 DetermineDownloadTarget(download_item.get(), &result); |
| 438 | 506 |
| 439 EXPECT_EQ(DownloadFileType::DANGEROUS, | 507 EXPECT_EQ(DownloadFileType::DANGEROUS, |
| 440 DownloadItemModel(download_item.get()).GetDangerLevel()); | 508 DownloadItemModel(download_item.get()).GetDangerLevel()); |
| 441 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | 509 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 442 target_info.danger_type); | 510 result.danger_type); |
| 443 } | 511 } |
| 444 | 512 |
| 445 { | 513 { |
| 446 const std::string kSafeContentDisposition( | 514 const std::string kSafeContentDisposition( |
| 447 "attachment; filename=\"foo.txt\""); | 515 "attachment; filename=\"foo.txt\""); |
| 448 EXPECT_CALL(*download_item, GetContentDisposition()) | 516 EXPECT_CALL(*download_item, GetContentDisposition()) |
| 449 .WillRepeatedly(Return(kSafeContentDisposition)); | 517 .WillRepeatedly(Return(kSafeContentDisposition)); |
| 450 DownloadTargetInfo target_info; | 518 DetermineDownloadTargetResult result; |
| 451 DetermineDownloadTarget(download_item.get(), &target_info); | 519 DetermineDownloadTarget(download_item.get(), &result); |
| 452 EXPECT_EQ(DownloadFileType::NOT_DANGEROUS, | 520 EXPECT_EQ(DownloadFileType::NOT_DANGEROUS, |
| 453 DownloadItemModel(download_item.get()).GetDangerLevel()); | 521 DownloadItemModel(download_item.get()).GetDangerLevel()); |
| 454 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | 522 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 455 target_info.danger_type); | 523 result.danger_type); |
| 456 } | 524 } |
| 457 | 525 |
| 458 { | 526 { |
| 459 const std::string kModerateContentDisposition( | 527 const std::string kModerateContentDisposition( |
| 460 "attachment; filename=\"foo.crx\""); | 528 "attachment; filename=\"foo.crx\""); |
| 461 EXPECT_CALL(*download_item, GetContentDisposition()) | 529 EXPECT_CALL(*download_item, GetContentDisposition()) |
| 462 .WillRepeatedly(Return(kModerateContentDisposition)); | 530 .WillRepeatedly(Return(kModerateContentDisposition)); |
| 463 DownloadTargetInfo target_info; | 531 DetermineDownloadTargetResult result; |
| 464 DetermineDownloadTarget(download_item.get(), &target_info); | 532 DetermineDownloadTarget(download_item.get(), &result); |
| 465 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, | 533 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, |
| 466 DownloadItemModel(download_item.get()).GetDangerLevel()); | 534 DownloadItemModel(download_item.get()).GetDangerLevel()); |
| 467 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, | 535 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, |
| 468 target_info.danger_type); | 536 result.danger_type); |
| 469 } | 537 } |
| 470 } | 538 } |
| 471 | 539 |
| 472 TEST_F(ChromeDownloadManagerDelegateTest, CheckForFileExistence) { | 540 TEST_F(ChromeDownloadManagerDelegateTest, CheckForFileExistence) { |
| 473 const char kData[] = "helloworld"; | 541 const char kData[] = "helloworld"; |
| 474 const size_t kDataLength = sizeof(kData) - 1; | 542 const size_t kDataLength = sizeof(kData) - 1; |
| 475 base::FilePath existing_path = default_download_path().AppendASCII("foo"); | 543 base::FilePath existing_path = default_download_path().AppendASCII("foo"); |
| 476 base::FilePath non_existent_path = | 544 base::FilePath non_existent_path = |
| 477 default_download_path().AppendASCII("bar"); | 545 default_download_path().AppendASCII("bar"); |
| 478 base::WriteFile(existing_path, kData, kDataLength); | 546 base::WriteFile(existing_path, kData, kDataLength); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 EXPECT_CALL(*download_item, OnContentCheckCompleted(_)).Times(0); | 746 EXPECT_CALL(*download_item, OnContentCheckCompleted(_)).Times(0); |
| 679 } | 747 } |
| 680 | 748 |
| 681 base::RunLoop run_loop; | 749 base::RunLoop run_loop; |
| 682 ASSERT_FALSE(delegate()->ShouldCompleteDownload(download_item.get(), | 750 ASSERT_FALSE(delegate()->ShouldCompleteDownload(download_item.get(), |
| 683 run_loop.QuitClosure())); | 751 run_loop.QuitClosure())); |
| 684 run_loop.Run(); | 752 run_loop.Run(); |
| 685 } | 753 } |
| 686 | 754 |
| 687 #endif // FULL_SAFE_BROWSING | 755 #endif // FULL_SAFE_BROWSING |
| 756 | |
| 757 #if BUILDFLAG(ANDROID_JAVA_UI) | |
| 758 | |
| 759 namespace { | |
| 760 | |
| 761 class AndroidDownloadInfobarCounter | |
| 762 : public infobars::InfoBarManager::Observer { | |
| 763 public: | |
| 764 explicit AndroidDownloadInfobarCounter(content::WebContents* web_contents) { | |
| 765 infobar_service_ = InfoBarService::FromWebContents(web_contents); | |
| 766 infobar_service_->AddObserver(this); | |
| 767 } | |
| 768 | |
| 769 ~AndroidDownloadInfobarCounter() { infobar_service_->RemoveObserver(this); } | |
| 770 | |
| 771 int infobar_count() const { return infobar_count_; } | |
| 772 | |
| 773 private: | |
| 774 void OnInfoBarAdded(infobars::InfoBar* infobar) override { | |
| 775 if (infobar->delegate()->GetIdentifier() == | |
| 776 infobars::InfoBarDelegate:: | |
| 777 CHROME_DOWNLOAD_MANAGER_OVERWRITE_INFOBAR_DELEGATE) { | |
| 778 ++infobar_count_; | |
| 779 } | |
| 780 infobar->delegate()->InfoBarDismissed(); | |
| 781 infobar->RemoveSelf(); | |
| 782 } | |
| 783 | |
| 784 InfoBarService* infobar_service_ = nullptr; | |
| 785 int infobar_count_ = 0; | |
| 786 }; | |
| 787 | |
| 788 } // namespace | |
| 789 | |
| 790 TEST_F(ChromeDownloadManagerDelegateTest, RequestConfirmation_Android) { | |
| 791 EXPECT_CALL(*delegate(), RequestConfirmation(_, _, _, _)) | |
| 792 .WillRepeatedly(Invoke( | |
| 793 delegate(), | |
| 794 &TestChromeDownloadManagerDelegate::RequestConfirmationConcrete)); | |
| 795 InfoBarService::CreateForWebContents(web_contents()); | |
| 796 base::FilePath fake_path = GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt")); | |
| 797 GURL url("http://example.com"); | |
| 798 AndroidDownloadInfobarCounter infobar_counter(web_contents()); | |
| 799 | |
| 800 struct { | |
| 801 DownloadConfirmationReason confirmation_reason; | |
| 802 DownloadConfirmationResult expected_result; | |
| 803 bool use_web_contents; | |
| 804 bool expect_full_path; | |
| 805 } kTestCases[] = { | |
| 806 {DownloadConfirmationReason::TARGET_NOT_WRITEABLE, | |
| 807 DownloadConfirmationResult::CANCELED, true, false}, | |
| 808 | |
| 809 {DownloadConfirmationReason::NAME_TOO_LONG, | |
| 810 DownloadConfirmationResult::CONTINUE_WITHOUT_CONFIRMATION, true, true}, | |
| 811 | |
| 812 {DownloadConfirmationReason::TARGET_NO_SPACE, | |
| 813 DownloadConfirmationResult::CONTINUE_WITHOUT_CONFIRMATION, true, true}, | |
| 814 | |
| 815 {DownloadConfirmationReason::SAVE_AS, | |
| 816 DownloadConfirmationResult::CONTINUE_WITHOUT_CONFIRMATION, true, true}, | |
| 817 | |
| 818 {DownloadConfirmationReason::PREFERENCE, | |
| 819 DownloadConfirmationResult::CONTINUE_WITHOUT_CONFIRMATION, true, true}, | |
| 820 | |
| 821 // This case results in an infobar. The logic above dismisses the infobar | |
| 822 // and counts it for testing. The functionality of the infobar is not | |
| 823 // tested here other than that dimssing the infobar is treated as a user | |
| 824 // initiated cancellation. | |
| 825 {DownloadConfirmationReason::TARGET_CONFLICT, | |
| 826 DownloadConfirmationResult::CANCELED, true, false}, | |
| 827 | |
| 828 {DownloadConfirmationReason::TARGET_CONFLICT, | |
| 829 DownloadConfirmationResult::CANCELED, false, false}, | |
| 830 | |
| 831 {DownloadConfirmationReason::UNEXPECTED, | |
| 832 DownloadConfirmationResult::CANCELED, true, false}, | |
| 833 }; | |
| 834 | |
| 835 for (const auto& test_case : kTestCases) { | |
| 836 std::unique_ptr<content::MockDownloadItem> download_item = | |
| 837 CreateActiveDownloadItem(1); | |
| 838 EXPECT_CALL(*download_item, GetWebContents()) | |
| 839 .WillRepeatedly( | |
| 840 Return(test_case.use_web_contents ? web_contents() : nullptr)); | |
| 841 EXPECT_CALL(*download_item, GetURL()).WillRepeatedly(ReturnRef(url)); | |
| 842 | |
| 843 base::RunLoop loop; | |
| 844 const auto callback = base::Bind( | |
| 845 [](const base::Closure& closure, | |
| 846 DownloadConfirmationResult expected_result, | |
| 847 const base::FilePath& expected_path, | |
| 848 DownloadConfirmationResult actual_result, | |
| 849 const base::FilePath& actual_path) { | |
| 850 EXPECT_EQ(expected_result, actual_result); | |
| 851 EXPECT_EQ(expected_path, actual_path); | |
| 852 closure.Run(); | |
| 853 }, | |
| 854 loop.QuitClosure(), test_case.expected_result, | |
| 855 test_case.expect_full_path ? fake_path : base::FilePath()); | |
| 856 delegate()->RequestConfirmation(download_item.get(), fake_path, | |
| 857 test_case.confirmation_reason, callback); | |
| 858 loop.Run(); | |
| 859 } | |
| 860 | |
| 861 EXPECT_EQ(1, infobar_counter.infobar_count()); | |
|
svaldez
2016/10/28 17:29:35
Possible check this within the loop, so that you a
asanka
2016/11/07 19:50:15
Good point. Done.
| |
| 862 } | |
| 863 #endif | |
| OLD | NEW |