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 "content/browser/download/download_file_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 25 using content::BrowserThread; | 25 using content::BrowserThread; |
| 26 using content::BrowserThreadImpl; | 26 using content::BrowserThreadImpl; |
| 27 using content::DownloadId; | 27 using content::DownloadId; |
| 28 using content::MockDownloadManager; | 28 using content::MockDownloadManager; |
| 29 | 29 |
| 30 using ::testing::_; | 30 using ::testing::_; |
| 31 using ::testing::AtLeast; | 31 using ::testing::AtLeast; |
| 32 using ::testing::Mock; | 32 using ::testing::Mock; |
| 33 using ::testing::Return; | 33 using ::testing::Return; |
| 34 using ::testing::SaveArg; | |
| 34 using ::testing::StrictMock; | 35 using ::testing::StrictMock; |
| 35 using ::testing::StrEq; | 36 using ::testing::StrEq; |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 // MockDownloadManager with the addition of a mock callback used for testing. | 40 // MockDownloadManager with the addition of a mock callback used for testing. |
| 40 class TestDownloadManager : public MockDownloadManager { | 41 class TestDownloadManager : public MockDownloadManager { |
| 41 public: | 42 public: |
| 42 MOCK_METHOD2(OnDownloadRenamed, | 43 MOCK_METHOD3(OnDownloadRenamed, |
| 43 void(int download_id, const FilePath& full_path)); | 44 void(int download_id, |
| 45 content::DownloadInterruptReason reason, | |
| 46 const FilePath& full_path)); | |
| 44 private: | 47 private: |
| 45 ~TestDownloadManager() {} | 48 ~TestDownloadManager() {} |
| 46 }; | 49 }; |
| 47 | 50 |
| 48 class MockDownloadFileFactory : | 51 class MockDownloadFileFactory : |
| 49 public DownloadFileManager::DownloadFileFactory { | 52 public DownloadFileManager::DownloadFileFactory { |
| 50 | 53 |
| 51 public: | 54 public: |
| 52 MockDownloadFileFactory() {} | 55 MockDownloadFileFactory() {} |
| 53 virtual ~MockDownloadFileFactory() {} | 56 virtual ~MockDownloadFileFactory() {} |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 // renamed to. If there is an existing file at | 208 // renamed to. If there is an existing file at |
| 206 // |new_path| and |replace| is false, then |new_path| | 209 // |new_path| and |replace| is false, then |new_path| |
| 207 // will be uniquified. | 210 // will be uniquified. |
| 208 // |rename_error| is the error to inject. For no error, | 211 // |rename_error| is the error to inject. For no error, |
| 209 // use content::DOWNLOAD_INTERRUPT_REASON_NONE. | 212 // use content::DOWNLOAD_INTERRUPT_REASON_NONE. |
| 210 // |state| whether we are renaming an in-progress download or a | 213 // |state| whether we are renaming an in-progress download or a |
| 211 // completed download. | 214 // completed download. |
| 212 // |should_overwrite| indicates whether to replace or uniquify the file. | 215 // |should_overwrite| indicates whether to replace or uniquify the file. |
| 213 void RenameFile(const DownloadId& id, | 216 void RenameFile(const DownloadId& id, |
| 214 const FilePath& new_path, | 217 const FilePath& new_path, |
| 215 const FilePath& unique_path, | 218 const FilePath& unique_path, |
|
asanka
2012/07/05 18:47:21
|unique_path| should be unused.
Randy Smith (Not in Mondays)
2012/07/09 20:35:51
Yeah--I got lazy here since this file is about to
| |
| 216 content::DownloadInterruptReason rename_error, | 219 content::DownloadInterruptReason rename_error, |
|
asanka
2012/07/05 18:47:21
|rename_error| is unused.
Randy Smith (Not in Mondays)
2012/07/09 20:35:51
Done.
| |
| 217 RenameFileState state, | 220 RenameFileState state, |
|
asanka
2012/07/05 18:47:21
|state| is unused now.
Randy Smith (Not in Mondays)
2012/07/09 20:35:51
Done.
| |
| 218 RenameFileOverwrite should_overwrite) { | 221 RenameFileOverwrite should_overwrite) { |
| 219 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | 222 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); |
| 220 ASSERT_TRUE(file != NULL); | 223 ASSERT_TRUE(file != NULL); |
| 224 content::DownloadFile::RenameCompletionCallback rename_callback; | |
| 221 | 225 |
| 222 EXPECT_CALL(*file, Rename(unique_path)) | 226 EXPECT_CALL(*file, Rename(unique_path, (should_overwrite == OVERWRITE), _)) |
|
asanka
2012/07/05 18:47:21
Should be expecting Rename(new_path,...) since uni
Randy Smith (Not in Mondays)
2012/07/09 20:35:51
Done.
| |
| 223 .Times(1) | 227 .WillOnce(SaveArg<2>(&rename_callback)); |
| 224 .WillOnce(Return(rename_error)); | |
| 225 | 228 |
| 226 if (rename_error != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | 229 content::DownloadFile::RenameCompletionCallback passed_callback( |
| 227 EXPECT_CALL(*file, BytesSoFar()) | |
| 228 .Times(AtLeast(1)) | |
| 229 .WillRepeatedly(Return(byte_count_[id])); | |
| 230 EXPECT_CALL(*file, GetHashState()) | |
| 231 .Times(AtLeast(1)); | |
| 232 EXPECT_CALL(*file, GetDownloadManager()) | |
| 233 .Times(AtLeast(1)); | |
| 234 } | |
| 235 | |
| 236 download_file_manager_->RenameDownloadFile( | |
| 237 id, new_path, (should_overwrite == OVERWRITE), | |
| 238 base::Bind(&TestDownloadManager::OnDownloadRenamed, | 230 base::Bind(&TestDownloadManager::OnDownloadRenamed, |
| 239 download_manager_, id.local())); | 231 download_manager_, id.local())); |
| 240 | 232 |
| 241 if (rename_error != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | 233 download_file_manager_->RenameDownloadFile( |
| 242 EXPECT_CALL(*download_manager_, | 234 id, new_path, (should_overwrite == OVERWRITE), passed_callback); |
| 243 OnDownloadInterrupted( | 235 |
| 244 id.local(), | 236 EXPECT_TRUE(rename_callback.Equals(passed_callback)); |
| 245 byte_count_[id], | |
| 246 "", | |
| 247 rename_error)); | |
| 248 EXPECT_CALL(*download_manager_, | |
| 249 OnDownloadRenamed(id.local(), FilePath())); | |
| 250 ProcessAllPendingMessages(); | |
| 251 ++error_count_[id]; | |
| 252 } else { | |
| 253 EXPECT_CALL(*download_manager_, | |
| 254 OnDownloadRenamed(id.local(), unique_path)); | |
| 255 ProcessAllPendingMessages(); | |
| 256 } | |
| 257 } | 237 } |
| 258 | 238 |
| 259 void Complete(DownloadId id) { | 239 void Complete(DownloadId id) { |
| 260 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | 240 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); |
| 261 ASSERT_TRUE(file != NULL); | 241 ASSERT_TRUE(file != NULL); |
| 262 | 242 |
| 263 EXPECT_CALL(*file, AnnotateWithSourceInformation()) | 243 EXPECT_CALL(*file, AnnotateWithSourceInformation()) |
| 264 .WillOnce(Return()); | 244 .WillOnce(Return()); |
| 265 EXPECT_CALL(*file, Detach()) | 245 EXPECT_CALL(*file, Detach()) |
| 266 .WillOnce(Return()); | 246 .WillOnce(Return()); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 TEST_F(DownloadFileManagerTest, Complete) { | 318 TEST_F(DownloadFileManagerTest, Complete) { |
| 339 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 319 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 340 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 320 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 341 info->download_id = dummy_id; | 321 info->download_id = dummy_id; |
| 342 | 322 |
| 343 CreateDownloadFile(info.Pass()); | 323 CreateDownloadFile(info.Pass()); |
| 344 | 324 |
| 345 Complete(dummy_id); | 325 Complete(dummy_id); |
| 346 } | 326 } |
| 347 | 327 |
| 348 TEST_F(DownloadFileManagerTest, RenameInProgress) { | 328 TEST_F(DownloadFileManagerTest, RenameInProgress) { |
|
asanka
2012/07/05 18:47:21
The state of the download (whether it's IN_PROGRES
Randy Smith (Not in Mondays)
2012/07/09 20:35:51
Done.
| |
| 349 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 329 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 350 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 330 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 351 info->download_id = dummy_id; | 331 info->download_id = dummy_id; |
| 352 ScopedTempDir download_dir; | 332 ScopedTempDir download_dir; |
| 353 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 333 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 354 | 334 |
| 355 CreateDownloadFile(info.Pass()); | 335 CreateDownloadFile(info.Pass()); |
| 356 | 336 |
| 357 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 337 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
| 358 RenameFile(dummy_id, foo, foo, content::DOWNLOAD_INTERRUPT_REASON_NONE, | 338 RenameFile(dummy_id, foo, foo, content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 359 IN_PROGRESS, OVERWRITE); | 339 IN_PROGRESS, OVERWRITE); |
| 360 | 340 |
| 361 CleanUp(dummy_id); | 341 CleanUp(dummy_id); |
| 362 } | 342 } |
| 363 | 343 |
| 364 TEST_F(DownloadFileManagerTest, RenameInProgressWithUniquification) { | |
| 365 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | |
| 366 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | |
| 367 info->download_id = dummy_id; | |
| 368 ScopedTempDir download_dir; | |
| 369 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | |
| 370 | |
| 371 CreateDownloadFile(info.Pass()); | |
| 372 | |
| 373 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | |
| 374 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); | |
| 375 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); | |
| 376 RenameFile(dummy_id, foo, unique_foo, | |
| 377 content::DOWNLOAD_INTERRUPT_REASON_NONE, IN_PROGRESS, | |
| 378 DONT_OVERWRITE); | |
| 379 | |
| 380 CleanUp(dummy_id); | |
| 381 } | |
| 382 | |
| 383 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { | 344 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { |
|
asanka
2012/07/05 18:47:21
This can't be tested here anymore. See suggestion
Randy Smith (Not in Mondays)
2012/07/09 20:35:51
Done.
| |
| 384 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 345 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 385 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 346 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 386 info->download_id = dummy_id; | 347 info->download_id = dummy_id; |
| 387 ScopedTempDir download_dir; | 348 ScopedTempDir download_dir; |
| 388 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 349 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 389 | 350 |
| 390 CreateDownloadFile(info.Pass()); | 351 CreateDownloadFile(info.Pass()); |
| 391 | 352 |
| 392 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 353 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
| 393 RenameFile(dummy_id, foo, foo, | 354 RenameFile(dummy_id, foo, foo, |
| 394 content::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG, | 355 content::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG, |
| 395 IN_PROGRESS, OVERWRITE); | 356 IN_PROGRESS, OVERWRITE); |
| 396 | 357 |
| 397 CleanUp(dummy_id); | 358 CleanUp(dummy_id); |
| 398 } | 359 } |
| 399 | 360 |
| 400 TEST_F(DownloadFileManagerTest, RenameWithUniquification) { | |
| 401 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | |
| 402 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | |
| 403 info->download_id = dummy_id; | |
| 404 ScopedTempDir download_dir; | |
| 405 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | |
| 406 | |
| 407 CreateDownloadFile(info.Pass()); | |
| 408 | |
| 409 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | |
| 410 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); | |
| 411 // Create a file at |foo|. Since we are specifying DONT_OVERWRITE, | |
| 412 // RenameDownloadFile() should pick "foo (1).txt" instead of | |
| 413 // overwriting this file. | |
| 414 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); | |
| 415 RenameFile(dummy_id, foo, unique_foo, | |
| 416 content::DOWNLOAD_INTERRUPT_REASON_NONE, COMPLETE, DONT_OVERWRITE); | |
| 417 | |
| 418 CleanUp(dummy_id); | |
| 419 } | |
| 420 | |
| 421 TEST_F(DownloadFileManagerTest, RenameTwice) { | 361 TEST_F(DownloadFileManagerTest, RenameTwice) { |
| 422 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 362 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 423 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 363 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 424 info->download_id = dummy_id; | 364 info->download_id = dummy_id; |
| 425 ScopedTempDir download_dir; | 365 ScopedTempDir download_dir; |
| 426 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 366 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 427 | 367 |
| 428 CreateDownloadFile(info.Pass()); | 368 CreateDownloadFile(info.Pass()); |
| 429 | 369 |
| 430 FilePath crfoo(download_dir.path().Append( | 370 FilePath crfoo(download_dir.path().Append( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 476 | 416 |
| 477 CleanUp(dummy_id); | 417 CleanUp(dummy_id); |
| 478 } | 418 } |
| 479 | 419 |
| 480 // TODO(ahendrickson) -- A test for download manager shutdown. | 420 // TODO(ahendrickson) -- A test for download manager shutdown. |
| 481 // Expected call sequence: | 421 // Expected call sequence: |
| 482 // OnDownloadManagerShutdown | 422 // OnDownloadManagerShutdown |
| 483 // DownloadFile::GetDownloadManager | 423 // DownloadFile::GetDownloadManager |
| 484 // DownloadFile::CancelDownloadRequest | 424 // DownloadFile::CancelDownloadRequest |
| 485 // DownloadFile::~DownloadFile | 425 // DownloadFile::~DownloadFile |
| OLD | NEW |