| 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/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/test/test_file_util.h" | 8 #include "base/test/test_file_util.h" |
| 9 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
| 10 #include "content/browser/download/byte_stream.h" | 10 #include "content/browser/download/byte_stream.h" |
| 11 #include "content/browser/download/download_create_info.h" | 11 #include "content/browser/download/download_create_info.h" |
| 12 #include "content/browser/download/download_file_impl.h" | 12 #include "content/browser/download/download_file_impl.h" |
| 13 #include "content/browser/download/download_request_handle.h" | 13 #include "content/browser/download/download_request_handle.h" |
| 14 #include "content/browser/power_save_blocker.h" | 14 #include "content/browser/power_save_blocker.h" |
| 15 #include "content/public/browser/download_destination_observer.h" | |
| 16 #include "content/public/browser/download_interrupt_reasons.h" | 15 #include "content/public/browser/download_interrupt_reasons.h" |
| 17 #include "content/public/browser/download_manager.h" | 16 #include "content/public/browser/download_manager.h" |
| 18 #include "content/public/test/mock_download_manager.h" | 17 #include "content/public/test/mock_download_manager.h" |
| 19 #include "net/base/file_stream.h" | 18 #include "net/base/file_stream.h" |
| 20 #include "net/base/mock_file_stream.h" | 19 #include "net/base/mock_file_stream.h" |
| 21 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 23 |
| 25 using content::BrowserThread; | 24 using content::BrowserThread; |
| 26 using content::BrowserThreadImpl; | 25 using content::BrowserThreadImpl; |
| 27 using content::DownloadFile; | 26 using content::DownloadFile; |
| 28 using content::DownloadId; | 27 using content::DownloadId; |
| 28 using content::DownloadManager; |
| 29 using ::testing::_; | 29 using ::testing::_; |
| 30 using ::testing::AnyNumber; | 30 using ::testing::AnyNumber; |
| 31 using ::testing::DoAll; | 31 using ::testing::DoAll; |
| 32 using ::testing::InSequence; | 32 using ::testing::InSequence; |
| 33 using ::testing::Return; | 33 using ::testing::Return; |
| 34 using ::testing::SetArgPointee; | 34 using ::testing::SetArgPointee; |
| 35 using ::testing::StrictMock; | 35 using ::testing::StrictMock; |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 class MockByteStreamReader : public content::ByteStreamReader { | 39 class MockByteStreamReader : public content::ByteStreamReader { |
| 40 public: | 40 public: |
| 41 MockByteStreamReader() {} | 41 MockByteStreamReader() {} |
| 42 ~MockByteStreamReader() {} | 42 ~MockByteStreamReader() {} |
| 43 | 43 |
| 44 // ByteStream functions | 44 // ByteStream functions |
| 45 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState( | 45 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState( |
| 46 scoped_refptr<net::IOBuffer>*, size_t*)); | 46 scoped_refptr<net::IOBuffer>*, size_t*)); |
| 47 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason()); | 47 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason()); |
| 48 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); | 48 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 class MockDownloadDestinationObserver | 51 class LocalMockDownloadManager : public content::MockDownloadManager { |
| 52 : public content::DownloadDestinationObserver { | |
| 53 public: | 52 public: |
| 54 MOCK_METHOD3(DestinationUpdate, void(int64, int64, const std::string&)); | 53 MOCK_METHOD4(CurrentUpdateStatus, |
| 55 MOCK_METHOD1(DestinationError, void(content::DownloadInterruptReason)); | 54 void(int32, int64, int64, const std::string&)); |
| 56 MOCK_METHOD1(DestinationCompleted, void(const std::string&)); | 55 protected: |
| 57 | 56 virtual ~LocalMockDownloadManager() {} |
| 58 // Doesn't override any methods in the base class. Used to make sure | |
| 59 // that the last DestinationUpdate before a Destination{Completed,Error} | |
| 60 // had the right values. | |
| 61 MOCK_METHOD3(CurrentUpdateStatus, | |
| 62 void(int64, int64, const std::string&)); | |
| 63 }; | 57 }; |
| 64 | 58 |
| 65 | |
| 66 MATCHER(IsNullCallback, "") { return (arg.is_null()); } | 59 MATCHER(IsNullCallback, "") { return (arg.is_null()); } |
| 67 | 60 |
| 68 } // namespace | 61 } // namespace |
| 69 | 62 |
| 70 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; | 63 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
| 71 | 64 |
| 72 class DownloadFileTest : public testing::Test { | 65 class DownloadFileTest : public testing::Test { |
| 73 public: | 66 public: |
| 74 | 67 |
| 75 static const char* kTestData1; | 68 static const char* kTestData1; |
| 76 static const char* kTestData2; | 69 static const char* kTestData2; |
| 77 static const char* kTestData3; | 70 static const char* kTestData3; |
| 78 static const char* kDataHash; | 71 static const char* kDataHash; |
| 79 static const int32 kDummyDownloadId; | 72 static const int32 kDummyDownloadId; |
| 80 static const int kDummyChildId; | 73 static const int kDummyChildId; |
| 81 static const int kDummyRequestId; | 74 static const int kDummyRequestId; |
| 82 | 75 |
| 76 // We need a UI |BrowserThread| in order to destruct |download_manager_|, |
| 77 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, |
| 78 // calling Release() on |download_manager_| won't ever result in its |
| 79 // destructor being called and we get a leak. |
| 83 DownloadFileTest() : | 80 DownloadFileTest() : |
| 84 observer_(new StrictMock<MockDownloadDestinationObserver>), | 81 update_download_id_(-1), |
| 85 observer_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(observer_.get())), | |
| 86 bytes_(-1), | 82 bytes_(-1), |
| 87 bytes_per_sec_(-1), | 83 bytes_per_sec_(-1), |
| 88 hash_state_("xyzzy"), | 84 hash_state_("xyzzy"), |
| 89 ui_thread_(BrowserThread::UI, &loop_), | 85 ui_thread_(BrowserThread::UI, &loop_), |
| 90 file_thread_(BrowserThread::FILE, &loop_) { | 86 file_thread_(BrowserThread::FILE, &loop_) { |
| 91 } | 87 } |
| 92 | 88 |
| 93 ~DownloadFileTest() { | 89 ~DownloadFileTest() { |
| 94 } | 90 } |
| 95 | 91 |
| 96 void SetUpdateDownloadInfo(int64 bytes, int64 bytes_per_sec, | 92 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec, |
| 97 const std::string& hash_state) { | 93 const std::string& hash_state) { |
| 94 update_download_id_ = id; |
| 98 bytes_ = bytes; | 95 bytes_ = bytes; |
| 99 bytes_per_sec_ = bytes_per_sec; | 96 bytes_per_sec_ = bytes_per_sec; |
| 100 hash_state_ = hash_state; | 97 hash_state_ = hash_state; |
| 101 } | 98 } |
| 102 | 99 |
| 103 void ConfirmUpdateDownloadInfo() { | 100 void ConfirmUpdateDownloadInfo() { |
| 104 observer_->CurrentUpdateStatus(bytes_, bytes_per_sec_, hash_state_); | 101 download_manager_->CurrentUpdateStatus( |
| 102 update_download_id_, bytes_, bytes_per_sec_, hash_state_); |
| 105 } | 103 } |
| 106 | 104 |
| 107 virtual void SetUp() { | 105 virtual void SetUp() { |
| 108 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _)) | 106 download_manager_ = new StrictMock<LocalMockDownloadManager>; |
| 107 EXPECT_CALL(*(download_manager_.get()), |
| 108 UpdateDownload( |
| 109 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 110 _, _, _)) |
| 109 .Times(AnyNumber()) | 111 .Times(AnyNumber()) |
| 110 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); | 112 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); |
| 111 } | 113 } |
| 112 | 114 |
| 115 virtual void TearDown() { |
| 116 // When a DownloadManager's reference count drops to 0, it is not |
| 117 // deleted immediately. Instead, a task is posted to the UI thread's |
| 118 // message loop to delete it. |
| 119 // So, drop the reference count to 0 and run the message loop once |
| 120 // to ensure that all resources are cleaned up before the test exits. |
| 121 download_manager_ = NULL; |
| 122 ui_thread_.message_loop()->RunAllPending(); |
| 123 } |
| 124 |
| 113 // Mock calls to this function are forwarded here. | 125 // Mock calls to this function are forwarded here. |
| 114 void RegisterCallback(base::Closure sink_callback) { | 126 void RegisterCallback(base::Closure sink_callback) { |
| 115 sink_callback_ = sink_callback; | 127 sink_callback_ = sink_callback; |
| 116 } | 128 } |
| 117 | 129 |
| 118 void SetInterruptReasonCallback(bool* was_called, | |
| 119 content::DownloadInterruptReason* reason_p, | |
| 120 content::DownloadInterruptReason reason) { | |
| 121 *was_called = true; | |
| 122 *reason_p = reason; | |
| 123 } | |
| 124 | |
| 125 virtual bool CreateDownloadFile(int offset, bool calculate_hash) { | 130 virtual bool CreateDownloadFile(int offset, bool calculate_hash) { |
| 126 // There can be only one. | 131 // There can be only one. |
| 127 DCHECK(!download_file_.get()); | 132 DCHECK(!download_file_.get()); |
| 128 | 133 |
| 129 input_stream_ = new StrictMock<MockByteStreamReader>(); | 134 input_stream_ = new StrictMock<MockByteStreamReader>(); |
| 130 | 135 |
| 131 // TODO: Need to actually create a function that'll set the variables | 136 // TODO: Need to actually create a function that'll set the variables |
| 132 // based on the inputs from the callback. | 137 // based on the inputs from the callback. |
| 133 EXPECT_CALL(*input_stream_, RegisterCallback(_)) | 138 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
| 134 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) | 139 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) |
| 135 .RetiresOnSaturation(); | 140 .RetiresOnSaturation(); |
| 136 | 141 |
| 142 DownloadCreateInfo info; |
| 143 // info.request_handle default constructed to null. |
| 144 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); |
| 145 info.save_info.file_stream = file_stream_; |
| 137 download_file_.reset( | 146 download_file_.reset( |
| 138 new DownloadFileImpl( | 147 new DownloadFileImpl( |
| 139 content::DownloadSaveInfo(), | 148 &info, |
| 140 GURL(), // Source | 149 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), |
| 141 GURL(), // Referrer | 150 new DownloadRequestHandle(), |
| 142 0, // Received bytes | 151 download_manager_, calculate_hash, |
| 143 calculate_hash, | |
| 144 scoped_ptr<content::ByteStreamReader>(input_stream_), | |
| 145 net::BoundNetLog(), | |
| 146 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), | 152 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), |
| 147 observer_factory_.GetWeakPtr())); | 153 net::BoundNetLog())); |
| 148 | 154 |
| 149 EXPECT_CALL(*input_stream_, Read(_, _)) | 155 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 150 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) | 156 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) |
| 151 .RetiresOnSaturation(); | 157 .RetiresOnSaturation(); |
| 152 | 158 content::DownloadInterruptReason result = download_file_->Initialize(); |
| 153 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | |
| 154 bool called = false; | |
| 155 content::DownloadInterruptReason result; | |
| 156 download_file_->Initialize(base::Bind( | |
| 157 &DownloadFileTest::SetInterruptReasonCallback, | |
| 158 weak_ptr_factory.GetWeakPtr(), &called, &result)); | |
| 159 loop_.RunAllPending(); | |
| 160 EXPECT_TRUE(called); | |
| 161 | |
| 162 ::testing::Mock::VerifyAndClearExpectations(input_stream_); | 159 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
| 163 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; | 160 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; |
| 164 } | 161 } |
| 165 | 162 |
| 166 virtual void DestroyDownloadFile(int offset) { | 163 virtual void DestroyDownloadFile(int offset) { |
| 164 EXPECT_EQ(kDummyDownloadId + offset, download_file_->Id()); |
| 165 EXPECT_EQ(download_manager_, download_file_->GetDownloadManager()); |
| 167 EXPECT_FALSE(download_file_->InProgress()); | 166 EXPECT_FALSE(download_file_->InProgress()); |
| 168 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 167 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
| 169 download_file_->BytesSoFar()); | 168 download_file_->BytesSoFar()); |
| 170 | 169 |
| 171 // Make sure the data has been properly written to disk. | 170 // Make sure the data has been properly written to disk. |
| 172 std::string disk_data; | 171 std::string disk_data; |
| 173 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), | 172 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), |
| 174 &disk_data)); | 173 &disk_data)); |
| 175 EXPECT_EQ(expected_data_, disk_data); | 174 EXPECT_EQ(expected_data_, disk_data); |
| 176 | 175 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 .RetiresOnSaturation(); | 225 .RetiresOnSaturation(); |
| 227 EXPECT_CALL(*input_stream_, GetStatus()) | 226 EXPECT_CALL(*input_stream_, GetStatus()) |
| 228 .InSequence(s) | 227 .InSequence(s) |
| 229 .WillOnce(Return(interrupt_reason)) | 228 .WillOnce(Return(interrupt_reason)) |
| 230 .RetiresOnSaturation(); | 229 .RetiresOnSaturation(); |
| 231 EXPECT_CALL(*input_stream_, RegisterCallback(_)) | 230 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
| 232 .RetiresOnSaturation(); | 231 .RetiresOnSaturation(); |
| 233 } | 232 } |
| 234 | 233 |
| 235 void FinishStream(content::DownloadInterruptReason interrupt_reason, | 234 void FinishStream(content::DownloadInterruptReason interrupt_reason, |
| 236 bool check_observer) { | 235 bool check_download_manager) { |
| 237 ::testing::Sequence s1; | 236 ::testing::Sequence s1; |
| 238 SetupFinishStream(interrupt_reason, s1); | 237 SetupFinishStream(interrupt_reason, s1); |
| 239 sink_callback_.Run(); | 238 sink_callback_.Run(); |
| 240 VerifyStreamAndSize(); | 239 VerifyStreamAndSize(); |
| 241 if (check_observer) { | 240 if (check_download_manager) { |
| 242 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_)); | 241 EXPECT_CALL(*download_manager_, OnResponseCompleted(_, _, _)); |
| 243 loop_.RunAllPending(); | 242 loop_.RunAllPending(); |
| 244 ::testing::Mock::VerifyAndClearExpectations(observer_.get()); | 243 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); |
| 245 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _)) | 244 EXPECT_CALL(*(download_manager_.get()), |
| 245 UpdateDownload( |
| 246 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 247 _, _, _)) |
| 246 .Times(AnyNumber()) | 248 .Times(AnyNumber()) |
| 247 .WillRepeatedly(Invoke(this, | 249 .WillRepeatedly(Invoke(this, |
| 248 &DownloadFileTest::SetUpdateDownloadInfo)); | 250 &DownloadFileTest::SetUpdateDownloadInfo)); |
| 249 } | 251 } |
| 250 } | 252 } |
| 251 | 253 |
| 252 content::DownloadInterruptReason Rename( | 254 content::DownloadInterruptReason Rename( |
| 253 const FilePath& full_path, bool overwrite_existing_file, | 255 const FilePath& full_path, bool overwrite_existing_file, |
| 254 FilePath* result_path_p) { | 256 FilePath* result_path_p) { |
| 255 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | 257 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); |
| 256 content::DownloadInterruptReason result_reason( | 258 content::DownloadInterruptReason result_reason( |
| 257 content::DOWNLOAD_INTERRUPT_REASON_NONE); | 259 content::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 258 bool callback_was_called(false); | 260 bool callback_was_called(false); |
| 259 FilePath result_path; | 261 FilePath result_path; |
| 260 | 262 |
| 261 download_file_->Rename(full_path, overwrite_existing_file, | 263 download_file_->Rename(full_path, overwrite_existing_file, |
| 262 base::Bind(&DownloadFileTest::SetRenameResult, | 264 base::Bind(&DownloadFileTest::SetRenameResult, |
| 263 weak_ptr_factory.GetWeakPtr(), | 265 weak_ptr_factory.GetWeakPtr(), |
| 264 &callback_was_called, | 266 &callback_was_called, |
| 265 &result_reason, result_path_p)); | 267 &result_reason, result_path_p)); |
| 266 loop_.RunAllPending(); | 268 loop_.RunAllPending(); |
| 267 | 269 |
| 268 EXPECT_TRUE(callback_was_called); | 270 EXPECT_TRUE(callback_was_called); |
| 269 return result_reason; | 271 return result_reason; |
| 270 } | 272 } |
| 271 | 273 |
| 272 protected: | 274 protected: |
| 273 scoped_ptr<StrictMock<MockDownloadDestinationObserver> > observer_; | 275 scoped_refptr<StrictMock<LocalMockDownloadManager> > download_manager_; |
| 274 base::WeakPtrFactory<content::DownloadDestinationObserver> | |
| 275 observer_factory_; | |
| 276 | 276 |
| 277 linked_ptr<net::FileStream> file_stream_; | 277 linked_ptr<net::FileStream> file_stream_; |
| 278 | 278 |
| 279 // DownloadFile instance we are testing. | 279 // DownloadFile instance we are testing. |
| 280 scoped_ptr<DownloadFile> download_file_; | 280 scoped_ptr<DownloadFile> download_file_; |
| 281 | 281 |
| 282 // Stream for sending data into the download file. | 282 // Stream for sending data into the download file. |
| 283 // Owned by download_file_; will be alive for lifetime of download_file_. | 283 // Owned by download_file_; will be alive for lifetime of download_file_. |
| 284 StrictMock<MockByteStreamReader>* input_stream_; | 284 StrictMock<MockByteStreamReader>* input_stream_; |
| 285 | 285 |
| 286 // Sink callback data for stream. | 286 // Sink callback data for stream. |
| 287 base::Closure sink_callback_; | 287 base::Closure sink_callback_; |
| 288 | 288 |
| 289 // Latest update sent to the observer. | 289 // Latest update sent to the download manager. |
| 290 int32 update_download_id_; |
| 290 int64 bytes_; | 291 int64 bytes_; |
| 291 int64 bytes_per_sec_; | 292 int64 bytes_per_sec_; |
| 292 std::string hash_state_; | 293 std::string hash_state_; |
| 293 | 294 |
| 294 MessageLoop loop_; | 295 MessageLoop loop_; |
| 295 | 296 |
| 296 private: | 297 private: |
| 297 void SetRenameResult(bool* called_p, | 298 void SetRenameResult(bool* called_p, |
| 298 content::DownloadInterruptReason* reason_p, | 299 content::DownloadInterruptReason* reason_p, |
| 299 FilePath* result_path_p, | 300 FilePath* result_path_p, |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 | 481 |
| 481 // Various tests of the StreamActive method. | 482 // Various tests of the StreamActive method. |
| 482 TEST_F(DownloadFileTest, StreamEmptySuccess) { | 483 TEST_F(DownloadFileTest, StreamEmptySuccess) { |
| 483 ASSERT_TRUE(CreateDownloadFile(0, true)); | 484 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 484 FilePath initial_path(download_file_->FullPath()); | 485 FilePath initial_path(download_file_->FullPath()); |
| 485 EXPECT_TRUE(file_util::PathExists(initial_path)); | 486 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 486 | 487 |
| 487 // Test that calling the sink_callback_ on an empty stream shouldn't | 488 // Test that calling the sink_callback_ on an empty stream shouldn't |
| 488 // do anything. | 489 // do anything. |
| 489 AppendDataToFile(NULL, 0); | 490 AppendDataToFile(NULL, 0); |
| 491 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); |
| 492 EXPECT_CALL(*(download_manager_.get()), |
| 493 UpdateDownload( |
| 494 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 495 _, _, _)) |
| 496 .Times(AnyNumber()) |
| 497 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); |
| 490 | 498 |
| 491 // Finish the download this way and make sure we see it on the | 499 // Finish the download this way and make sure we see it on the |
| 492 // observer. | 500 // DownloadManager. |
| 493 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_)); | 501 EXPECT_CALL(*(download_manager_.get()), |
| 502 OnResponseCompleted(DownloadId(kValidIdDomain, |
| 503 kDummyDownloadId + 0).local(), |
| 504 0, _)); |
| 494 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false); | 505 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false); |
| 495 loop_.RunAllPending(); | |
| 496 | 506 |
| 497 DestroyDownloadFile(0); | 507 DestroyDownloadFile(0); |
| 498 } | 508 } |
| 499 | 509 |
| 500 TEST_F(DownloadFileTest, StreamEmptyError) { | 510 TEST_F(DownloadFileTest, StreamEmptyError) { |
| 501 ASSERT_TRUE(CreateDownloadFile(0, true)); | 511 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 502 FilePath initial_path(download_file_->FullPath()); | 512 FilePath initial_path(download_file_->FullPath()); |
| 503 EXPECT_TRUE(file_util::PathExists(initial_path)); | 513 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 504 | 514 |
| 505 // Finish the download in error and make sure we see it on the | 515 // Finish the download in error and make sure we see it on the |
| 506 // observer. | 516 // DownloadManager. |
| 507 EXPECT_CALL(*(observer_.get()), | 517 EXPECT_CALL(*(download_manager_.get()), |
| 508 DestinationError( | 518 OnDownloadInterrupted( |
| 519 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 509 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) | 520 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) |
| 510 .WillOnce(InvokeWithoutArgs( | 521 .WillOnce(InvokeWithoutArgs( |
| 511 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); | 522 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); |
| 512 | 523 |
| 513 // If this next EXPECT_CALL fails flakily, it's probably a real failure. | 524 // If this next EXPECT_CALL fails flakily, it's probably a real failure. |
| 514 // We'll be getting a stream of UpdateDownload calls from the timer, and | 525 // We'll be getting a stream of UpdateDownload calls from the timer, and |
| 515 // the last one may have the correct information even if the failure | 526 // the last one may have the correct information even if the failure |
| 516 // doesn't produce an update, as the timer update may have triggered at the | 527 // doesn't produce an update, as the timer update may have triggered at the |
| 517 // same time. | 528 // same time. |
| 518 EXPECT_CALL(*(observer_.get()), CurrentUpdateStatus(0, _, _)); | 529 EXPECT_CALL(*(download_manager_.get()), |
| 530 CurrentUpdateStatus(kDummyDownloadId + 0, 0, _, _)); |
| 519 | 531 |
| 520 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); | 532 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); |
| 521 | 533 |
| 522 loop_.RunAllPending(); | 534 loop_.RunAllPending(); |
| 523 | 535 |
| 524 DestroyDownloadFile(0); | 536 DestroyDownloadFile(0); |
| 525 } | 537 } |
| 526 | 538 |
| 527 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { | 539 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { |
| 528 ASSERT_TRUE(CreateDownloadFile(0, true)); | 540 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 529 FilePath initial_path(download_file_->FullPath()); | 541 FilePath initial_path(download_file_->FullPath()); |
| 530 EXPECT_TRUE(file_util::PathExists(initial_path)); | 542 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 531 | 543 |
| 532 const char* chunks1[] = { kTestData1, kTestData2 }; | 544 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 533 ::testing::Sequence s1; | 545 ::testing::Sequence s1; |
| 534 SetupDataAppend(chunks1, 2, s1); | 546 SetupDataAppend(chunks1, 2, s1); |
| 535 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1); | 547 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1); |
| 536 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_)); | 548 EXPECT_CALL(*(download_manager_.get()), |
| 549 OnResponseCompleted(DownloadId(kValidIdDomain, |
| 550 kDummyDownloadId + 0).local(), |
| 551 strlen(kTestData1) + strlen(kTestData2), |
| 552 _)); |
| 537 sink_callback_.Run(); | 553 sink_callback_.Run(); |
| 538 VerifyStreamAndSize(); | 554 VerifyStreamAndSize(); |
| 539 loop_.RunAllPending(); | |
| 540 DestroyDownloadFile(0); | 555 DestroyDownloadFile(0); |
| 541 } | 556 } |
| 542 | 557 |
| 543 TEST_F(DownloadFileTest, StreamNonEmptyError) { | 558 TEST_F(DownloadFileTest, StreamNonEmptyError) { |
| 544 ASSERT_TRUE(CreateDownloadFile(0, true)); | 559 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 545 FilePath initial_path(download_file_->FullPath()); | 560 FilePath initial_path(download_file_->FullPath()); |
| 546 EXPECT_TRUE(file_util::PathExists(initial_path)); | 561 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 547 | 562 |
| 548 const char* chunks1[] = { kTestData1, kTestData2 }; | 563 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 549 ::testing::Sequence s1; | 564 ::testing::Sequence s1; |
| 550 SetupDataAppend(chunks1, 2, s1); | 565 SetupDataAppend(chunks1, 2, s1); |
| 551 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, | 566 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, |
| 552 s1); | 567 s1); |
| 553 | 568 |
| 554 EXPECT_CALL(*(observer_.get()), | 569 EXPECT_CALL(*(download_manager_.get()), |
| 555 DestinationError( | 570 OnDownloadInterrupted( |
| 571 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 556 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) | 572 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) |
| 557 .WillOnce(InvokeWithoutArgs( | 573 .WillOnce(InvokeWithoutArgs( |
| 558 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); | 574 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); |
| 559 | 575 |
| 560 // If this next EXPECT_CALL fails flakily, it's probably a real failure. | 576 // If this next EXPECT_CALL fails flakily, it's probably a real failure. |
| 561 // We'll be getting a stream of UpdateDownload calls from the timer, and | 577 // We'll be getting a stream of UpdateDownload calls from the timer, and |
| 562 // the last one may have the correct information even if the failure | 578 // the last one may have the correct information even if the failure |
| 563 // doesn't produce an update, as the timer update may have triggered at the | 579 // doesn't produce an update, as the timer update may have triggered at the |
| 564 // same time. | 580 // same time. |
| 565 EXPECT_CALL(*(observer_.get()), | 581 EXPECT_CALL(*(download_manager_.get()), |
| 566 CurrentUpdateStatus(strlen(kTestData1) + strlen(kTestData2), | 582 CurrentUpdateStatus(kDummyDownloadId + 0, |
| 583 strlen(kTestData1) + strlen(kTestData2), |
| 567 _, _)); | 584 _, _)); |
| 568 | 585 |
| 569 sink_callback_.Run(); | 586 sink_callback_.Run(); |
| 570 loop_.RunAllPending(); | 587 loop_.RunAllPending(); |
| 571 VerifyStreamAndSize(); | 588 VerifyStreamAndSize(); |
| 572 DestroyDownloadFile(0); | 589 DestroyDownloadFile(0); |
| 573 } | 590 } |
| 574 | 591 |
| 575 // Send some data, wait 3/4s of a second, run the message loop, and | 592 // Send some data, wait 3/4s of a second, run the message loop, and |
| 576 // confirm the values the observer received are correct. | 593 // confirm the values the DownloadManager received are correct. |
| 577 TEST_F(DownloadFileTest, ConfirmUpdate) { | 594 TEST_F(DownloadFileTest, ConfirmUpdate) { |
| 578 CreateDownloadFile(0, true); | 595 CreateDownloadFile(0, true); |
| 579 | 596 |
| 580 const char* chunks1[] = { kTestData1, kTestData2 }; | 597 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 581 AppendDataToFile(chunks1, 2); | 598 AppendDataToFile(chunks1, 2); |
| 582 | 599 |
| 583 // Run the message loops for 750ms and check for results. | 600 // Run the message loops for 750ms and check for results. |
| 584 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), | 601 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
| 585 base::TimeDelta::FromMilliseconds(750)); | 602 base::TimeDelta::FromMilliseconds(750)); |
| 586 loop_.Run(); | 603 loop_.Run(); |
| 587 | 604 |
| 588 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 605 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
| 589 bytes_); | 606 bytes_); |
| 590 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 607 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
| 591 | 608 |
| 592 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | 609 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 593 DestroyDownloadFile(0); | 610 DestroyDownloadFile(0); |
| 594 } | 611 } |
| OLD | NEW |