| 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 "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 10 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 11 #include "chrome/browser/download/download_prefs.h" | 11 #include "chrome/browser/download/download_prefs.h" |
| 12 #include "chrome/browser/download/download_target_info.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 14 #include "chrome/test/base/testing_pref_service_syncable.h" | 15 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 16 #include "content/public/browser/download_interrupt_reasons.h" | 17 #include "content/public/browser/download_interrupt_reasons.h" |
| 17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_contents_delegate.h" | 19 #include "content/public/browser/web_contents_delegate.h" |
| 19 #include "content/public/test/mock_download_item.h" | 20 #include "content/public/test/mock_download_item.h" |
| 20 #include "content/public/test/mock_download_manager.h" | 21 #include "content/public/test/mock_download_manager.h" |
| 21 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 ACTION_P(ScheduleCallback, result) { | 59 ACTION_P(ScheduleCallback, result) { |
| 59 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result)); | 60 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 // Similar to ScheduleCallback, but binds 2 arguments. | 63 // Similar to ScheduleCallback, but binds 2 arguments. |
| 63 ACTION_P2(ScheduleCallback2, result0, result1) { | 64 ACTION_P2(ScheduleCallback2, result0, result1) { |
| 64 base::MessageLoop::current()->PostTask( | 65 base::MessageLoop::current()->PostTask( |
| 65 FROM_HERE, base::Bind(arg0, result0, result1)); | 66 FROM_HERE, base::Bind(arg0, result0, result1)); |
| 66 } | 67 } |
| 67 | 68 |
| 68 struct DownloadTarget { | |
| 69 base::FilePath target_path; | |
| 70 base::FilePath intermediate_path; | |
| 71 DownloadItem::TargetDisposition target_disposition; | |
| 72 content::DownloadDangerType danger_type; | |
| 73 }; | |
| 74 | |
| 75 // Subclass of the ChromeDownloadManagerDelegate that uses a mock | 69 // Subclass of the ChromeDownloadManagerDelegate that uses a mock |
| 76 // DownloadProtectionService. | 70 // DownloadProtectionService. |
| 77 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { | 71 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
| 78 public: | 72 public: |
| 79 explicit TestChromeDownloadManagerDelegate(Profile* profile) | 73 explicit TestChromeDownloadManagerDelegate(Profile* profile) |
| 80 : ChromeDownloadManagerDelegate(profile) { | 74 : ChromeDownloadManagerDelegate(profile) { |
| 81 } | 75 } |
| 82 | 76 |
| 83 virtual safe_browsing::DownloadProtectionService* | 77 virtual safe_browsing::DownloadProtectionService* |
| 84 GetDownloadProtectionService() OVERRIDE { | 78 GetDownloadProtectionService() OVERRIDE { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 content::MockDownloadItem* CreateActiveDownloadItem(int32 id); | 139 content::MockDownloadItem* CreateActiveDownloadItem(int32 id); |
| 146 | 140 |
| 147 // Given the relative path |path|, returns the full path under the temporary | 141 // Given the relative path |path|, returns the full path under the temporary |
| 148 // downloads directory. | 142 // downloads directory. |
| 149 base::FilePath GetPathInDownloadDir(const char* path); | 143 base::FilePath GetPathInDownloadDir(const char* path); |
| 150 | 144 |
| 151 // Set the kDownloadDefaultDirectory user preference to |path|. | 145 // Set the kDownloadDefaultDirectory user preference to |path|. |
| 152 void SetDefaultDownloadPath(const base::FilePath& path); | 146 void SetDefaultDownloadPath(const base::FilePath& path); |
| 153 | 147 |
| 154 void DetermineDownloadTarget(DownloadItem* download, | 148 void DetermineDownloadTarget(DownloadItem* download, |
| 155 DownloadTarget* result); | 149 DownloadTargetInfo* result); |
| 156 | 150 |
| 157 const base::FilePath& default_download_path() const; | 151 const base::FilePath& default_download_path() const; |
| 158 TestChromeDownloadManagerDelegate* delegate(); | 152 TestChromeDownloadManagerDelegate* delegate(); |
| 159 content::MockDownloadManager* download_manager(); | 153 content::MockDownloadManager* download_manager(); |
| 160 DownloadPrefs* download_prefs(); | 154 DownloadPrefs* download_prefs(); |
| 161 | 155 |
| 162 private: | 156 private: |
| 163 void OnDownloadTargetDone(DownloadTarget* result, | |
| 164 const base::FilePath& target_path, | |
| 165 DownloadItem::TargetDisposition disposition, | |
| 166 content::DownloadDangerType danger_type, | |
| 167 const base::FilePath& intermediate_path); | |
| 168 | |
| 169 TestingPrefServiceSyncable* pref_service_; | 157 TestingPrefServiceSyncable* pref_service_; |
| 170 base::ScopedTempDir test_download_dir_; | 158 base::ScopedTempDir test_download_dir_; |
| 171 scoped_ptr<content::MockDownloadManager> download_manager_; | 159 scoped_ptr<content::MockDownloadManager> download_manager_; |
| 172 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_; | 160 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_; |
| 173 MockWebContentsDelegate web_contents_delegate_; | 161 MockWebContentsDelegate web_contents_delegate_; |
| 174 }; | 162 }; |
| 175 | 163 |
| 176 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() | 164 ChromeDownloadManagerDelegateTest::ChromeDownloadManagerDelegateTest() |
| 177 : download_manager_(new ::testing::NiceMock<content::MockDownloadManager>) { | 165 : download_manager_(new ::testing::NiceMock<content::MockDownloadManager>) { |
| 178 } | 166 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 test_download_dir_.path().AppendASCII(relative_path); | 233 test_download_dir_.path().AppendASCII(relative_path); |
| 246 return full_path.NormalizePathSeparators(); | 234 return full_path.NormalizePathSeparators(); |
| 247 } | 235 } |
| 248 | 236 |
| 249 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath( | 237 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath( |
| 250 const base::FilePath& path) { | 238 const base::FilePath& path) { |
| 251 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path); | 239 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path); |
| 252 pref_service_->SetFilePath(prefs::kSaveFileDefaultDirectory, path); | 240 pref_service_->SetFilePath(prefs::kSaveFileDefaultDirectory, path); |
| 253 } | 241 } |
| 254 | 242 |
| 243 void StoreDownloadTargetInfo(const base::Closure& closure, |
| 244 DownloadTargetInfo* target_info, |
| 245 const base::FilePath& target_path, |
| 246 DownloadItem::TargetDisposition target_disposition, |
| 247 content::DownloadDangerType danger_type, |
| 248 const base::FilePath& intermediate_path) { |
| 249 target_info->target_path = target_path; |
| 250 target_info->target_disposition = target_disposition; |
| 251 target_info->danger_type = danger_type; |
| 252 target_info->intermediate_path = intermediate_path; |
| 253 closure.Run(); |
| 254 } |
| 255 |
| 255 void ChromeDownloadManagerDelegateTest::DetermineDownloadTarget( | 256 void ChromeDownloadManagerDelegateTest::DetermineDownloadTarget( |
| 256 DownloadItem* download_item, | 257 DownloadItem* download_item, |
| 257 DownloadTarget* result) { | 258 DownloadTargetInfo* result) { |
| 258 base::WeakPtrFactory<ChromeDownloadManagerDelegateTest> factory(this); | 259 base::RunLoop loop_runner; |
| 259 delegate()->DetermineDownloadTarget( | 260 delegate()->DetermineDownloadTarget( |
| 260 download_item, | 261 download_item, |
| 261 base::Bind(&ChromeDownloadManagerDelegateTest::OnDownloadTargetDone, | 262 base::Bind(&StoreDownloadTargetInfo, loop_runner.QuitClosure(), result)); |
| 262 factory.GetWeakPtr(), base::Unretained(result))); | 263 loop_runner.Run(); |
| 263 base::RunLoop loop_runner; | |
| 264 loop_runner.RunUntilIdle(); | |
| 265 } | |
| 266 | |
| 267 void ChromeDownloadManagerDelegateTest::OnDownloadTargetDone( | |
| 268 DownloadTarget* result, | |
| 269 const base::FilePath& target_path, | |
| 270 DownloadItem::TargetDisposition target_disposition, | |
| 271 content::DownloadDangerType danger_type, | |
| 272 const base::FilePath& intermediate_path) { | |
| 273 result->target_path = target_path; | |
| 274 result->intermediate_path = intermediate_path; | |
| 275 result->target_disposition = target_disposition; | |
| 276 result->danger_type = danger_type; | |
| 277 } | 264 } |
| 278 | 265 |
| 279 const base::FilePath& ChromeDownloadManagerDelegateTest::default_download_path() | 266 const base::FilePath& ChromeDownloadManagerDelegateTest::default_download_path() |
| 280 const { | 267 const { |
| 281 return test_download_dir_.path(); | 268 return test_download_dir_.path(); |
| 282 } | 269 } |
| 283 | 270 |
| 284 TestChromeDownloadManagerDelegate* | 271 TestChromeDownloadManagerDelegate* |
| 285 ChromeDownloadManagerDelegateTest::delegate() { | 272 ChromeDownloadManagerDelegateTest::delegate() { |
| 286 return delegate_.get(); | 273 return delegate_.get(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 314 EXPECT_CALL(*automatic_download, GetURL()) | 301 EXPECT_CALL(*automatic_download, GetURL()) |
| 315 .Times(::testing::AnyNumber()) | 302 .Times(::testing::AnyNumber()) |
| 316 .WillRepeatedly(ReturnRef(download_url)); | 303 .WillRepeatedly(ReturnRef(download_url)); |
| 317 EXPECT_CALL(*automatic_download, GetTargetDisposition()) | 304 EXPECT_CALL(*automatic_download, GetTargetDisposition()) |
| 318 .Times(::testing::AnyNumber()) | 305 .Times(::testing::AnyNumber()) |
| 319 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | 306 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
| 320 | 307 |
| 321 { | 308 { |
| 322 // When the prompt is displayed for the first download, the user selects a | 309 // When the prompt is displayed for the first download, the user selects a |
| 323 // path in a different directory. | 310 // path in a different directory. |
| 324 DownloadTarget result; | 311 DownloadTargetInfo result; |
| 325 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); | 312 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); |
| 326 base::FilePath user_selected_path(GetPathInDownloadDir("bar/baz.txt")); | 313 base::FilePath user_selected_path(GetPathInDownloadDir("bar/baz.txt")); |
| 327 EXPECT_CALL(*delegate(), | 314 EXPECT_CALL(*delegate(), |
| 328 MockPromptUserForDownloadPath(save_as_download.get(), | 315 MockPromptUserForDownloadPath(save_as_download.get(), |
| 329 expected_prompt_path, _)) | 316 expected_prompt_path, _)) |
| 330 .WillOnce(Return(user_selected_path)); | 317 .WillOnce(Return(user_selected_path)); |
| 331 DetermineDownloadTarget(save_as_download.get(), &result); | 318 DetermineDownloadTarget(save_as_download.get(), &result); |
| 332 EXPECT_EQ(user_selected_path, result.target_path); | 319 EXPECT_EQ(user_selected_path, result.target_path); |
| 333 VerifyAndClearExpectations(); | 320 VerifyAndClearExpectations(); |
| 334 } | 321 } |
| 335 | 322 |
| 336 { | 323 { |
| 337 // The prompt path for the second download is the user selected directroy | 324 // The prompt path for the second download is the user selected directroy |
| 338 // from the previous download. | 325 // from the previous download. |
| 339 DownloadTarget result; | 326 DownloadTargetInfo result; |
| 340 base::FilePath expected_prompt_path(GetPathInDownloadDir("bar/foo.txt")); | 327 base::FilePath expected_prompt_path(GetPathInDownloadDir("bar/foo.txt")); |
| 341 EXPECT_CALL(*delegate(), | 328 EXPECT_CALL(*delegate(), |
| 342 MockPromptUserForDownloadPath(save_as_download.get(), | 329 MockPromptUserForDownloadPath(save_as_download.get(), |
| 343 expected_prompt_path, _)) | 330 expected_prompt_path, _)) |
| 344 .WillOnce(Return(base::FilePath())); | 331 .WillOnce(Return(base::FilePath())); |
| 345 DetermineDownloadTarget(save_as_download.get(), &result); | 332 DetermineDownloadTarget(save_as_download.get(), &result); |
| 346 VerifyAndClearExpectations(); | 333 VerifyAndClearExpectations(); |
| 347 } | 334 } |
| 348 | 335 |
| 349 { | 336 { |
| 350 // Start an automatic download. This one should get the default download | 337 // Start an automatic download. This one should get the default download |
| 351 // path since the last download path only affects Save As downloads. | 338 // path since the last download path only affects Save As downloads. |
| 352 DownloadTarget result; | 339 DownloadTargetInfo result; |
| 353 base::FilePath expected_path(GetPathInDownloadDir("foo.txt")); | 340 base::FilePath expected_path(GetPathInDownloadDir("foo.txt")); |
| 354 DetermineDownloadTarget(automatic_download.get(), &result); | 341 DetermineDownloadTarget(automatic_download.get(), &result); |
| 355 EXPECT_EQ(expected_path, result.target_path); | 342 EXPECT_EQ(expected_path, result.target_path); |
| 356 VerifyAndClearExpectations(); | 343 VerifyAndClearExpectations(); |
| 357 } | 344 } |
| 358 | 345 |
| 359 { | 346 { |
| 360 // The prompt path for the next download should be the default. | 347 // The prompt path for the next download should be the default. |
| 361 download_prefs()->SetSaveFilePath(download_prefs()->DownloadPath()); | 348 download_prefs()->SetSaveFilePath(download_prefs()->DownloadPath()); |
| 362 DownloadTarget result; | 349 DownloadTargetInfo result; |
| 363 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); | 350 base::FilePath expected_prompt_path(GetPathInDownloadDir("foo.txt")); |
| 364 EXPECT_CALL(*delegate(), | 351 EXPECT_CALL(*delegate(), |
| 365 MockPromptUserForDownloadPath(save_as_download.get(), | 352 MockPromptUserForDownloadPath(save_as_download.get(), |
| 366 expected_prompt_path, _)) | 353 expected_prompt_path, _)) |
| 367 .WillOnce(Return(base::FilePath())); | 354 .WillOnce(Return(base::FilePath())); |
| 368 DetermineDownloadTarget(save_as_download.get(), &result); | 355 DetermineDownloadTarget(save_as_download.get(), &result); |
| 369 VerifyAndClearExpectations(); | 356 VerifyAndClearExpectations(); |
| 370 } | 357 } |
| 371 } | 358 } |
| OLD | NEW |