| 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> |
| 6 #include <stdint.h> |
| 7 |
| 5 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 6 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 7 #include "base/location.h" | 10 #include "base/location.h" |
| 8 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 9 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/test/test_file_util.h" | 14 #include "base/test/test_file_util.h" |
| 12 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "build/build_config.h" |
| 13 #include "content/browser/browser_thread_impl.h" | 17 #include "content/browser/browser_thread_impl.h" |
| 14 #include "content/browser/byte_stream.h" | 18 #include "content/browser/byte_stream.h" |
| 15 #include "content/browser/download/download_create_info.h" | 19 #include "content/browser/download/download_create_info.h" |
| 16 #include "content/browser/download/download_file_impl.h" | 20 #include "content/browser/download/download_file_impl.h" |
| 17 #include "content/browser/download/download_request_handle.h" | 21 #include "content/browser/download/download_request_handle.h" |
| 18 #include "content/public/browser/download_destination_observer.h" | 22 #include "content/public/browser/download_destination_observer.h" |
| 19 #include "content/public/browser/download_interrupt_reasons.h" | 23 #include "content/public/browser/download_interrupt_reasons.h" |
| 20 #include "content/public/browser/download_manager.h" | 24 #include "content/public/browser/download_manager.h" |
| 21 #include "content/public/test/mock_download_manager.h" | 25 #include "content/public/test/mock_download_manager.h" |
| 22 #include "net/base/file_stream.h" | 26 #include "net/base/file_stream.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 43 | 47 |
| 44 // ByteStream functions | 48 // ByteStream functions |
| 45 MOCK_METHOD2(Read, ByteStreamReader::StreamState( | 49 MOCK_METHOD2(Read, ByteStreamReader::StreamState( |
| 46 scoped_refptr<net::IOBuffer>*, size_t*)); | 50 scoped_refptr<net::IOBuffer>*, size_t*)); |
| 47 MOCK_CONST_METHOD0(GetStatus, int()); | 51 MOCK_CONST_METHOD0(GetStatus, int()); |
| 48 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); | 52 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); |
| 49 }; | 53 }; |
| 50 | 54 |
| 51 class MockDownloadDestinationObserver : public DownloadDestinationObserver { | 55 class MockDownloadDestinationObserver : public DownloadDestinationObserver { |
| 52 public: | 56 public: |
| 53 MOCK_METHOD3(DestinationUpdate, void(int64, int64, const std::string&)); | 57 MOCK_METHOD3(DestinationUpdate, void(int64_t, int64_t, const std::string&)); |
| 54 MOCK_METHOD1(DestinationError, void(DownloadInterruptReason)); | 58 MOCK_METHOD1(DestinationError, void(DownloadInterruptReason)); |
| 55 MOCK_METHOD1(DestinationCompleted, void(const std::string&)); | 59 MOCK_METHOD1(DestinationCompleted, void(const std::string&)); |
| 56 | 60 |
| 57 // Doesn't override any methods in the base class. Used to make sure | 61 // Doesn't override any methods in the base class. Used to make sure |
| 58 // that the last DestinationUpdate before a Destination{Completed,Error} | 62 // that the last DestinationUpdate before a Destination{Completed,Error} |
| 59 // had the right values. | 63 // had the right values. |
| 60 MOCK_METHOD3(CurrentUpdateStatus, | 64 MOCK_METHOD3(CurrentUpdateStatus, void(int64_t, int64_t, const std::string&)); |
| 61 void(int64, int64, const std::string&)); | |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 MATCHER(IsNullCallback, "") { return (arg.is_null()); } | 67 MATCHER(IsNullCallback, "") { return (arg.is_null()); } |
| 65 | 68 |
| 66 typedef void (DownloadFile::*DownloadFileRenameMethodType)( | 69 typedef void (DownloadFile::*DownloadFileRenameMethodType)( |
| 67 const base::FilePath&, | 70 const base::FilePath&, |
| 68 const DownloadFile::RenameCompletionCallback&); | 71 const DownloadFile::RenameCompletionCallback&); |
| 69 | 72 |
| 70 // This is a test DownloadFileImpl that has no retry delay and, on Posix, | 73 // This is a test DownloadFileImpl that has no retry delay and, on Posix, |
| 71 // retries renames failed due to ACCESS_DENIED. | 74 // retries renames failed due to ACCESS_DENIED. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 108 |
| 106 } // namespace | 109 } // namespace |
| 107 | 110 |
| 108 class DownloadFileTest : public testing::Test { | 111 class DownloadFileTest : public testing::Test { |
| 109 public: | 112 public: |
| 110 | 113 |
| 111 static const char* kTestData1; | 114 static const char* kTestData1; |
| 112 static const char* kTestData2; | 115 static const char* kTestData2; |
| 113 static const char* kTestData3; | 116 static const char* kTestData3; |
| 114 static const char* kDataHash; | 117 static const char* kDataHash; |
| 115 static const uint32 kDummyDownloadId; | 118 static const uint32_t kDummyDownloadId; |
| 116 static const int kDummyChildId; | 119 static const int kDummyChildId; |
| 117 static const int kDummyRequestId; | 120 static const int kDummyRequestId; |
| 118 | 121 |
| 119 DownloadFileTest() : | 122 DownloadFileTest() : |
| 120 observer_(new StrictMock<MockDownloadDestinationObserver>), | 123 observer_(new StrictMock<MockDownloadDestinationObserver>), |
| 121 observer_factory_(observer_.get()), | 124 observer_factory_(observer_.get()), |
| 122 input_stream_(NULL), | 125 input_stream_(NULL), |
| 123 bytes_(-1), | 126 bytes_(-1), |
| 124 bytes_per_sec_(-1), | 127 bytes_per_sec_(-1), |
| 125 hash_state_("xyzzy"), | 128 hash_state_("xyzzy"), |
| 126 ui_thread_(BrowserThread::UI, &loop_), | 129 ui_thread_(BrowserThread::UI, &loop_), |
| 127 file_thread_(BrowserThread::FILE, &loop_) { | 130 file_thread_(BrowserThread::FILE, &loop_) { |
| 128 } | 131 } |
| 129 | 132 |
| 130 ~DownloadFileTest() override {} | 133 ~DownloadFileTest() override {} |
| 131 | 134 |
| 132 void SetUpdateDownloadInfo(int64 bytes, int64 bytes_per_sec, | 135 void SetUpdateDownloadInfo(int64_t bytes, |
| 136 int64_t bytes_per_sec, |
| 133 const std::string& hash_state) { | 137 const std::string& hash_state) { |
| 134 bytes_ = bytes; | 138 bytes_ = bytes; |
| 135 bytes_per_sec_ = bytes_per_sec; | 139 bytes_per_sec_ = bytes_per_sec; |
| 136 hash_state_ = hash_state; | 140 hash_state_ = hash_state; |
| 137 } | 141 } |
| 138 | 142 |
| 139 void ConfirmUpdateDownloadInfo() { | 143 void ConfirmUpdateDownloadInfo() { |
| 140 observer_->CurrentUpdateStatus(bytes_, bytes_per_sec_, hash_state_); | 144 observer_->CurrentUpdateStatus(bytes_, bytes_per_sec_, hash_state_); |
| 141 } | 145 } |
| 142 | 146 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 .WillOnce(DoAll(SetArgPointee<0>(data), | 231 .WillOnce(DoAll(SetArgPointee<0>(data), |
| 228 SetArgPointee<1>(length), | 232 SetArgPointee<1>(length), |
| 229 Return(ByteStreamReader::STREAM_HAS_DATA))) | 233 Return(ByteStreamReader::STREAM_HAS_DATA))) |
| 230 .RetiresOnSaturation(); | 234 .RetiresOnSaturation(); |
| 231 expected_data_ += source_data; | 235 expected_data_ += source_data; |
| 232 } | 236 } |
| 233 } | 237 } |
| 234 | 238 |
| 235 void VerifyStreamAndSize() { | 239 void VerifyStreamAndSize() { |
| 236 ::testing::Mock::VerifyAndClearExpectations(input_stream_); | 240 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
| 237 int64 size; | 241 int64_t size; |
| 238 EXPECT_TRUE(base::GetFileSize(download_file_->FullPath(), &size)); | 242 EXPECT_TRUE(base::GetFileSize(download_file_->FullPath(), &size)); |
| 239 EXPECT_EQ(expected_data_.size(), static_cast<size_t>(size)); | 243 EXPECT_EQ(expected_data_.size(), static_cast<size_t>(size)); |
| 240 } | 244 } |
| 241 | 245 |
| 242 // TODO(rdsmith): Manage full percentage issues properly. | 246 // TODO(rdsmith): Manage full percentage issues properly. |
| 243 void AppendDataToFile(const char **data_chunks, size_t num_chunks) { | 247 void AppendDataToFile(const char **data_chunks, size_t num_chunks) { |
| 244 ::testing::Sequence s1; | 248 ::testing::Sequence s1; |
| 245 SetupDataAppend(data_chunks, num_chunks, s1); | 249 SetupDataAppend(data_chunks, num_chunks, s1); |
| 246 EXPECT_CALL(*input_stream_, Read(_, _)) | 250 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 247 .InSequence(s1) | 251 .InSequence(s1) |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 scoped_ptr<DownloadFile> download_file_; | 332 scoped_ptr<DownloadFile> download_file_; |
| 329 | 333 |
| 330 // Stream for sending data into the download file. | 334 // Stream for sending data into the download file. |
| 331 // Owned by download_file_; will be alive for lifetime of download_file_. | 335 // Owned by download_file_; will be alive for lifetime of download_file_. |
| 332 StrictMock<MockByteStreamReader>* input_stream_; | 336 StrictMock<MockByteStreamReader>* input_stream_; |
| 333 | 337 |
| 334 // Sink callback data for stream. | 338 // Sink callback data for stream. |
| 335 base::Closure sink_callback_; | 339 base::Closure sink_callback_; |
| 336 | 340 |
| 337 // Latest update sent to the observer. | 341 // Latest update sent to the observer. |
| 338 int64 bytes_; | 342 int64_t bytes_; |
| 339 int64 bytes_per_sec_; | 343 int64_t bytes_per_sec_; |
| 340 std::string hash_state_; | 344 std::string hash_state_; |
| 341 | 345 |
| 342 base::MessageLoop loop_; | 346 base::MessageLoop loop_; |
| 343 | 347 |
| 344 private: | 348 private: |
| 345 void SetRenameResult(const base::Closure& closure, | 349 void SetRenameResult(const base::Closure& closure, |
| 346 DownloadInterruptReason* reason_p, | 350 DownloadInterruptReason* reason_p, |
| 347 base::FilePath* result_path_p, | 351 base::FilePath* result_path_p, |
| 348 DownloadInterruptReason reason, | 352 DownloadInterruptReason reason, |
| 349 const base::FilePath& result_path) { | 353 const base::FilePath& result_path) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 ::testing::Values(&DownloadFile::RenameAndAnnotate, | 394 ::testing::Values(&DownloadFile::RenameAndAnnotate, |
| 391 &DownloadFile::RenameAndUniquify)); | 395 &DownloadFile::RenameAndUniquify)); |
| 392 | 396 |
| 393 const char* DownloadFileTest::kTestData1 = | 397 const char* DownloadFileTest::kTestData1 = |
| 394 "Let's write some data to the file!\n"; | 398 "Let's write some data to the file!\n"; |
| 395 const char* DownloadFileTest::kTestData2 = "Writing more data.\n"; | 399 const char* DownloadFileTest::kTestData2 = "Writing more data.\n"; |
| 396 const char* DownloadFileTest::kTestData3 = "Final line."; | 400 const char* DownloadFileTest::kTestData3 = "Final line."; |
| 397 const char* DownloadFileTest::kDataHash = | 401 const char* DownloadFileTest::kDataHash = |
| 398 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8"; | 402 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8"; |
| 399 | 403 |
| 400 const uint32 DownloadFileTest::kDummyDownloadId = 23; | 404 const uint32_t DownloadFileTest::kDummyDownloadId = 23; |
| 401 const int DownloadFileTest::kDummyChildId = 3; | 405 const int DownloadFileTest::kDummyChildId = 3; |
| 402 const int DownloadFileTest::kDummyRequestId = 67; | 406 const int DownloadFileTest::kDummyRequestId = 67; |
| 403 | 407 |
| 404 // Rename the file before any data is downloaded, after some has, after it all | 408 // Rename the file before any data is downloaded, after some has, after it all |
| 405 // has, and after it's closed. | 409 // has, and after it's closed. |
| 406 TEST_P(DownloadFileTestWithRename, RenameFileFinal) { | 410 TEST_P(DownloadFileTestWithRename, RenameFileFinal) { |
| 407 ASSERT_TRUE(CreateDownloadFile(0, true)); | 411 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 408 base::FilePath initial_path(download_file_->FullPath()); | 412 base::FilePath initial_path(download_file_->FullPath()); |
| 409 EXPECT_TRUE(base::PathExists(initial_path)); | 413 EXPECT_TRUE(base::PathExists(initial_path)); |
| 410 base::FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | 414 base::FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 | 779 |
| 776 const char* chunks1[] = { kTestData1, kTestData2 }; | 780 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 777 AppendDataToFile(chunks1, 2); | 781 AppendDataToFile(chunks1, 2); |
| 778 | 782 |
| 779 // Run the message loops for 750ms and check for results. | 783 // Run the message loops for 750ms and check for results. |
| 780 loop_.task_runner()->PostDelayedTask(FROM_HERE, | 784 loop_.task_runner()->PostDelayedTask(FROM_HERE, |
| 781 base::MessageLoop::QuitWhenIdleClosure(), | 785 base::MessageLoop::QuitWhenIdleClosure(), |
| 782 base::TimeDelta::FromMilliseconds(750)); | 786 base::TimeDelta::FromMilliseconds(750)); |
| 783 loop_.Run(); | 787 loop_.Run(); |
| 784 | 788 |
| 785 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 789 EXPECT_EQ(static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)), |
| 786 bytes_); | 790 bytes_); |
| 787 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 791 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
| 788 | 792 |
| 789 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); | 793 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 790 DestroyDownloadFile(0); | 794 DestroyDownloadFile(0); |
| 791 } | 795 } |
| 792 | 796 |
| 793 } // namespace content | 797 } // namespace content |
| OLD | NEW |