| 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 "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/download/byte_stream.h" |
| 9 #include "content/browser/download/download_create_info.h" | 10 #include "content/browser/download/download_create_info.h" |
| 10 #include "content/browser/download/download_file_impl.h" | 11 #include "content/browser/download/download_file_impl.h" |
| 11 #include "content/browser/download/download_request_handle.h" | 12 #include "content/browser/download/download_request_handle.h" |
| 12 #include "content/browser/power_save_blocker.h" | 13 #include "content/browser/power_save_blocker.h" |
| 14 #include "content/public/browser/download_interrupt_reasons.h" |
| 13 #include "content/public/browser/download_manager.h" | 15 #include "content/public/browser/download_manager.h" |
| 14 #include "content/test/mock_download_manager.h" | 16 #include "content/test/mock_download_manager.h" |
| 15 #include "net/base/file_stream.h" | 17 #include "net/base/file_stream.h" |
| 16 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 21 |
| 19 using content::BrowserThread; | 22 using content::BrowserThread; |
| 20 using content::BrowserThreadImpl; | 23 using content::BrowserThreadImpl; |
| 21 using content::DownloadFile; | 24 using content::DownloadFile; |
| 22 using content::DownloadId; | 25 using content::DownloadId; |
| 23 using content::DownloadManager; | 26 using content::DownloadManager; |
| 24 using testing::_; | 27 using ::testing::_; |
| 25 using testing::AnyNumber; | 28 using ::testing::AnyNumber; |
| 26 using testing::StrictMock; | 29 using ::testing::DoAll; |
| 30 using ::testing::Return; |
| 31 using ::testing::SetArgPointee; |
| 32 using ::testing::StrictMock; |
| 33 |
| 34 namespace { |
| 35 |
| 36 class MockByteStreamReader : public content::ByteStreamReader { |
| 37 public: |
| 38 MockByteStreamReader() {} |
| 39 ~MockByteStreamReader() {} |
| 40 |
| 41 // ByteStream functions |
| 42 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState( |
| 43 scoped_refptr<net::IOBuffer>*, size_t*)); |
| 44 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason()); |
| 45 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); |
| 46 }; |
| 47 |
| 48 } // namespace |
| 27 | 49 |
| 28 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; | 50 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
| 29 | 51 |
| 30 class DownloadFileTest : public testing::Test { | 52 class DownloadFileTest : public testing::Test { |
| 31 public: | 53 public: |
| 32 | 54 |
| 33 static const char* kTestData1; | 55 static const char* kTestData1; |
| 34 static const char* kTestData2; | 56 static const char* kTestData2; |
| 35 static const char* kTestData3; | 57 static const char* kTestData3; |
| 36 static const char* kDataHash; | 58 static const char* kDataHash; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 virtual void TearDown() { | 92 virtual void TearDown() { |
| 71 // When a DownloadManager's reference count drops to 0, it is not | 93 // When a DownloadManager's reference count drops to 0, it is not |
| 72 // deleted immediately. Instead, a task is posted to the UI thread's | 94 // deleted immediately. Instead, a task is posted to the UI thread's |
| 73 // message loop to delete it. | 95 // message loop to delete it. |
| 74 // So, drop the reference count to 0 and run the message loop once | 96 // So, drop the reference count to 0 and run the message loop once |
| 75 // to ensure that all resources are cleaned up before the test exits. | 97 // to ensure that all resources are cleaned up before the test exits. |
| 76 download_manager_ = NULL; | 98 download_manager_ = NULL; |
| 77 ui_thread_.message_loop()->RunAllPending(); | 99 ui_thread_.message_loop()->RunAllPending(); |
| 78 } | 100 } |
| 79 | 101 |
| 80 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, | 102 // Mock calls to this function are forwarded here. |
| 81 int offset, | 103 void RegisterCallback(base::Closure sink_callback) { |
| 82 bool calculate_hash) { | 104 sink_callback_ = sink_callback; |
| 105 } |
| 106 |
| 107 virtual bool CreateDownloadFile(int offset, bool calculate_hash) { |
| 108 // There can be only one. |
| 109 DCHECK(!download_file_.get()); |
| 110 |
| 111 input_stream_ = new StrictMock<MockByteStreamReader>(); |
| 112 |
| 113 // TODO: Need to actually create a function that'll set the variables |
| 114 // based on the inputs from the callback. |
| 115 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
| 116 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) |
| 117 .RetiresOnSaturation(); |
| 118 |
| 83 DownloadCreateInfo info; | 119 DownloadCreateInfo info; |
| 84 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); | 120 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); |
| 85 // info.request_handle default constructed to null. | 121 // info.request_handle default constructed to null. |
| 86 info.save_info.file_stream = file_stream_; | 122 info.save_info.file_stream = file_stream_; |
| 87 file->reset( | 123 download_file_.reset( |
| 88 new DownloadFileImpl(&info, new DownloadRequestHandle(), | 124 new DownloadFileImpl( |
| 89 download_manager_, calculate_hash, | 125 &info, |
| 90 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), | 126 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), |
| 91 net::BoundNetLog())); | 127 new DownloadRequestHandle(), |
| 128 download_manager_, calculate_hash, |
| 129 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), |
| 130 net::BoundNetLog())); |
| 131 |
| 132 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 133 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) |
| 134 .RetiresOnSaturation(); |
| 135 net::Error result = download_file_->Initialize(); |
| 136 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
| 137 return result == net::OK; |
| 92 } | 138 } |
| 93 | 139 |
| 94 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | 140 virtual void DestroyDownloadFile(int offset) { |
| 95 EXPECT_EQ(kDummyDownloadId + offset, (*file)->Id()); | 141 EXPECT_EQ(kDummyDownloadId + offset, download_file_->Id()); |
| 96 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); | 142 EXPECT_EQ(download_manager_, download_file_->GetDownloadManager()); |
| 97 EXPECT_FALSE((*file)->InProgress()); | 143 EXPECT_FALSE(download_file_->InProgress()); |
| 98 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 144 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
| 99 (*file)->BytesSoFar()); | 145 download_file_->BytesSoFar()); |
| 100 | 146 |
| 101 // Make sure the data has been properly written to disk. | 147 // Make sure the data has been properly written to disk. |
| 102 std::string disk_data; | 148 std::string disk_data; |
| 103 EXPECT_TRUE(file_util::ReadFileToString((*file)->FullPath(), | 149 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), |
| 104 &disk_data)); | 150 &disk_data)); |
| 105 EXPECT_EQ(expected_data_, disk_data); | 151 EXPECT_EQ(expected_data_, disk_data); |
| 106 | 152 |
| 107 // Make sure the Browser and File threads outlive the DownloadFile | 153 // Make sure the Browser and File threads outlive the DownloadFile |
| 108 // to satisfy thread checks inside it. | 154 // to satisfy thread checks inside it. |
| 109 file->reset(); | 155 download_file_.reset(); |
| 110 } | 156 } |
| 111 | 157 |
| 112 void AppendDataToFile(scoped_ptr<DownloadFile>* file, | 158 // Setup the stream to do be a data append; don't actually trigger |
| 113 const std::string& data) { | 159 // the callback or do verifications. |
| 114 EXPECT_TRUE((*file)->InProgress()); | 160 void SetupDataAppend(const char **data_chunks, size_t num_chunks, |
| 115 (*file)->AppendDataToFile(data.data(), data.size()); | 161 ::testing::Sequence s) { |
| 116 expected_data_ += data; | 162 DCHECK(input_stream_); |
| 117 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 163 for (size_t i = 0; i < num_chunks; i++) { |
| 118 (*file)->BytesSoFar()); | 164 const char *source_data = data_chunks[i]; |
| 165 size_t length = strlen(source_data); |
| 166 scoped_refptr<net::IOBuffer> data = new net::IOBuffer(length); |
| 167 memcpy(data->data(), source_data, length); |
| 168 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 169 .InSequence(s) |
| 170 .WillOnce(DoAll(SetArgPointee<0>(data), |
| 171 SetArgPointee<1>(length), |
| 172 Return(content::ByteStreamReader::STREAM_HAS_DATA))) |
| 173 .RetiresOnSaturation(); |
| 174 expected_data_ += source_data; |
| 175 } |
| 176 } |
| 177 |
| 178 void VerifyStreamAndSize() { |
| 179 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
| 180 int64 size; |
| 181 EXPECT_TRUE(file_util::GetFileSize(download_file_->FullPath(), &size)); |
| 182 EXPECT_EQ(expected_data_.size(), static_cast<size_t>(size)); |
| 183 } |
| 184 |
| 185 // TODO(rdsmith): Manage full percentage issues properly. |
| 186 void AppendDataToFile(const char **data_chunks, size_t num_chunks) { |
| 187 ::testing::Sequence s1; |
| 188 SetupDataAppend(data_chunks, num_chunks, s1); |
| 189 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 190 .InSequence(s1) |
| 191 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) |
| 192 .RetiresOnSaturation(); |
| 193 sink_callback_.Run(); |
| 194 VerifyStreamAndSize(); |
| 195 } |
| 196 |
| 197 void SetupFinishStream(content::DownloadInterruptReason interrupt_reason, |
| 198 ::testing::Sequence s) { |
| 199 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 200 .InSequence(s) |
| 201 .WillOnce(Return(content::ByteStreamReader::STREAM_COMPLETE)) |
| 202 .RetiresOnSaturation(); |
| 203 EXPECT_CALL(*input_stream_, GetStatus()) |
| 204 .InSequence(s) |
| 205 .WillOnce(Return(interrupt_reason)) |
| 206 .RetiresOnSaturation(); |
| 207 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
| 208 .RetiresOnSaturation(); |
| 209 } |
| 210 |
| 211 void FinishStream(content::DownloadInterruptReason interrupt_reason, |
| 212 bool check_download_manager) { |
| 213 ::testing::Sequence s1; |
| 214 SetupFinishStream(interrupt_reason, s1); |
| 215 sink_callback_.Run(); |
| 216 VerifyStreamAndSize(); |
| 217 if (check_download_manager) { |
| 218 EXPECT_CALL(*download_manager_, OnResponseCompleted(_, _, _)); |
| 219 loop_.RunAllPending(); |
| 220 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); |
| 221 EXPECT_CALL(*(download_manager_.get()), |
| 222 UpdateDownload( |
| 223 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 224 _, _, _)) |
| 225 .Times(AnyNumber()) |
| 226 .WillRepeatedly(Invoke(this, |
| 227 &DownloadFileTest::SetUpdateDownloadInfo)); |
| 228 } |
| 119 } | 229 } |
| 120 | 230 |
| 121 protected: | 231 protected: |
| 122 scoped_refptr<StrictMock<content::MockDownloadManager> > download_manager_; | 232 scoped_refptr<StrictMock<content::MockDownloadManager> > download_manager_; |
| 123 | 233 |
| 124 linked_ptr<net::FileStream> file_stream_; | 234 linked_ptr<net::FileStream> file_stream_; |
| 125 | 235 |
| 126 // DownloadFile instance we are testing. | 236 // DownloadFile instance we are testing. |
| 127 scoped_ptr<DownloadFile> download_file_; | 237 scoped_ptr<DownloadFile> download_file_; |
| 128 | 238 |
| 239 // Stream for sending data into the download file. |
| 240 // Owned by download_file_; will be alive for lifetime of download_file_. |
| 241 StrictMock<MockByteStreamReader>* input_stream_; |
| 242 |
| 243 // Sink callback data for stream. |
| 244 base::Closure sink_callback_; |
| 245 |
| 129 // Latest update sent to the download manager. | 246 // Latest update sent to the download manager. |
| 130 int64 bytes_; | 247 int64 bytes_; |
| 131 int64 bytes_per_sec_; | 248 int64 bytes_per_sec_; |
| 132 std::string hash_state_; | 249 std::string hash_state_; |
| 133 | 250 |
| 134 MessageLoop loop_; | 251 MessageLoop loop_; |
| 135 | 252 |
| 136 private: | 253 private: |
| 137 // UI thread. | 254 // UI thread. |
| 138 BrowserThreadImpl ui_thread_; | 255 BrowserThreadImpl ui_thread_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 150 const char* DownloadFileTest::kDataHash = | 267 const char* DownloadFileTest::kDataHash = |
| 151 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8"; | 268 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8"; |
| 152 | 269 |
| 153 const int32 DownloadFileTest::kDummyDownloadId = 23; | 270 const int32 DownloadFileTest::kDummyDownloadId = 23; |
| 154 const int DownloadFileTest::kDummyChildId = 3; | 271 const int DownloadFileTest::kDummyChildId = 3; |
| 155 const int DownloadFileTest::kDummyRequestId = 67; | 272 const int DownloadFileTest::kDummyRequestId = 67; |
| 156 | 273 |
| 157 // Rename the file before any data is downloaded, after some has, after it all | 274 // Rename the file before any data is downloaded, after some has, after it all |
| 158 // has, and after it's closed. | 275 // has, and after it's closed. |
| 159 TEST_F(DownloadFileTest, RenameFileFinal) { | 276 TEST_F(DownloadFileTest, RenameFileFinal) { |
| 160 CreateDownloadFile(&download_file_, 0, true); | 277 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 161 ASSERT_EQ(net::OK, download_file_->Initialize()); | |
| 162 FilePath initial_path(download_file_->FullPath()); | 278 FilePath initial_path(download_file_->FullPath()); |
| 163 EXPECT_TRUE(file_util::PathExists(initial_path)); | 279 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 164 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | 280 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); |
| 165 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); | 281 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); |
| 166 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); | 282 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); |
| 167 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); | 283 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); |
| 168 | 284 |
| 169 // Rename the file before downloading any data. | 285 // Rename the file before downloading any data. |
| 170 EXPECT_EQ(net::OK, download_file_->Rename(path_1)); | 286 EXPECT_EQ(net::OK, download_file_->Rename(path_1)); |
| 171 FilePath renamed_path = download_file_->FullPath(); | 287 FilePath renamed_path = download_file_->FullPath(); |
| 172 EXPECT_EQ(path_1, renamed_path); | 288 EXPECT_EQ(path_1, renamed_path); |
| 173 | 289 |
| 174 // Check the files. | 290 // Check the files. |
| 175 EXPECT_FALSE(file_util::PathExists(initial_path)); | 291 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 176 EXPECT_TRUE(file_util::PathExists(path_1)); | 292 EXPECT_TRUE(file_util::PathExists(path_1)); |
| 177 | 293 |
| 178 // Download the data. | 294 // Download the data. |
| 179 AppendDataToFile(&download_file_, kTestData1); | 295 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 180 AppendDataToFile(&download_file_, kTestData2); | 296 AppendDataToFile(chunks1, 2); |
| 181 | 297 |
| 182 // Rename the file after downloading some data. | 298 // Rename the file after downloading some data. |
| 183 EXPECT_EQ(net::OK, download_file_->Rename(path_2)); | 299 EXPECT_EQ(net::OK, download_file_->Rename(path_2)); |
| 184 renamed_path = download_file_->FullPath(); | 300 renamed_path = download_file_->FullPath(); |
| 185 EXPECT_EQ(path_2, renamed_path); | 301 EXPECT_EQ(path_2, renamed_path); |
| 186 | 302 |
| 187 // Check the files. | 303 // Check the files. |
| 188 EXPECT_FALSE(file_util::PathExists(path_1)); | 304 EXPECT_FALSE(file_util::PathExists(path_1)); |
| 189 EXPECT_TRUE(file_util::PathExists(path_2)); | 305 EXPECT_TRUE(file_util::PathExists(path_2)); |
| 190 | 306 |
| 191 AppendDataToFile(&download_file_, kTestData3); | 307 const char* chunks2[] = { kTestData3 }; |
| 308 AppendDataToFile(chunks2, 1); |
| 192 | 309 |
| 193 // Rename the file after downloading all the data. | 310 // Rename the file after downloading all the data. |
| 194 EXPECT_EQ(net::OK, download_file_->Rename(path_3)); | 311 EXPECT_EQ(net::OK, download_file_->Rename(path_3)); |
| 195 renamed_path = download_file_->FullPath(); | 312 renamed_path = download_file_->FullPath(); |
| 196 EXPECT_EQ(path_3, renamed_path); | 313 EXPECT_EQ(path_3, renamed_path); |
| 197 | 314 |
| 198 // Check the files. | 315 // Check the files. |
| 199 EXPECT_FALSE(file_util::PathExists(path_2)); | 316 EXPECT_FALSE(file_util::PathExists(path_2)); |
| 200 EXPECT_TRUE(file_util::PathExists(path_3)); | 317 EXPECT_TRUE(file_util::PathExists(path_3)); |
| 201 | 318 |
| 202 // Should not be able to get the hash until the file is closed. | 319 // Should not be able to get the hash until the file is closed. |
| 203 std::string hash; | 320 std::string hash; |
| 204 EXPECT_FALSE(download_file_->GetHash(&hash)); | 321 EXPECT_FALSE(download_file_->GetHash(&hash)); |
| 205 | 322 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 206 download_file_->Finish(); | 323 loop_.RunAllPending(); |
| 207 | 324 |
| 208 // Rename the file after downloading all the data and closing the file. | 325 // Rename the file after downloading all the data and closing the file. |
| 209 EXPECT_EQ(net::OK, download_file_->Rename(path_4)); | 326 EXPECT_EQ(net::OK, download_file_->Rename(path_4)); |
| 210 renamed_path = download_file_->FullPath(); | 327 renamed_path = download_file_->FullPath(); |
| 211 EXPECT_EQ(path_4, renamed_path); | 328 EXPECT_EQ(path_4, renamed_path); |
| 212 | 329 |
| 213 // Check the files. | 330 // Check the files. |
| 214 EXPECT_FALSE(file_util::PathExists(path_3)); | 331 EXPECT_FALSE(file_util::PathExists(path_3)); |
| 215 EXPECT_TRUE(file_util::PathExists(path_4)); | 332 EXPECT_TRUE(file_util::PathExists(path_4)); |
| 216 | 333 |
| 217 // Check the hash. | 334 // Check the hash. |
| 218 EXPECT_TRUE(download_file_->GetHash(&hash)); | 335 EXPECT_TRUE(download_file_->GetHash(&hash)); |
| 219 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 336 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
| 220 | 337 |
| 221 DestroyDownloadFile(&download_file_, 0); | 338 DestroyDownloadFile(0); |
| 339 } |
| 340 |
| 341 // Various tests of the StreamActive method. |
| 342 TEST_F(DownloadFileTest, StreamEmptySuccess) { |
| 343 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 344 FilePath initial_path(download_file_->FullPath()); |
| 345 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 346 |
| 347 // Test that calling the sink_callback_ on an empty stream shouldn't |
| 348 // do anything. |
| 349 AppendDataToFile(NULL, 0); |
| 350 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); |
| 351 EXPECT_CALL(*(download_manager_.get()), |
| 352 UpdateDownload( |
| 353 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 354 _, _, _)) |
| 355 .Times(AnyNumber()) |
| 356 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); |
| 357 |
| 358 // Finish the download this way and make sure we see it on the |
| 359 // DownloadManager. |
| 360 EXPECT_CALL(*(download_manager_.get()), |
| 361 OnResponseCompleted(DownloadId(kValidIdDomain, |
| 362 kDummyDownloadId + 0).local(), |
| 363 0, _)); |
| 364 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false); |
| 365 |
| 366 DestroyDownloadFile(0); |
| 367 } |
| 368 |
| 369 TEST_F(DownloadFileTest, StreamEmptyError) { |
| 370 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 371 FilePath initial_path(download_file_->FullPath()); |
| 372 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 373 |
| 374 // Finish the download in error and make sure we see it on the |
| 375 // DownloadManager. |
| 376 EXPECT_CALL(*(download_manager_.get()), |
| 377 OnDownloadInterrupted( |
| 378 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 379 0, _, |
| 380 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)); |
| 381 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); |
| 382 |
| 383 DestroyDownloadFile(0); |
| 384 } |
| 385 |
| 386 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { |
| 387 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 388 FilePath initial_path(download_file_->FullPath()); |
| 389 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 390 |
| 391 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 392 ::testing::Sequence s1; |
| 393 SetupDataAppend(chunks1, 2, s1); |
| 394 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1); |
| 395 EXPECT_CALL(*(download_manager_.get()), |
| 396 OnResponseCompleted(DownloadId(kValidIdDomain, |
| 397 kDummyDownloadId + 0).local(), |
| 398 strlen(kTestData1) + strlen(kTestData2), |
| 399 _)); |
| 400 sink_callback_.Run(); |
| 401 VerifyStreamAndSize(); |
| 402 DestroyDownloadFile(0); |
| 403 } |
| 404 |
| 405 TEST_F(DownloadFileTest, StreamNonEmptyError) { |
| 406 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 407 FilePath initial_path(download_file_->FullPath()); |
| 408 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 409 |
| 410 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 411 ::testing::Sequence s1; |
| 412 SetupDataAppend(chunks1, 2, s1); |
| 413 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, |
| 414 s1); |
| 415 EXPECT_CALL(*(download_manager_.get()), |
| 416 OnDownloadInterrupted( |
| 417 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
| 418 strlen(kTestData1) + strlen(kTestData2), _, |
| 419 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)); |
| 420 sink_callback_.Run(); |
| 421 VerifyStreamAndSize(); |
| 422 DestroyDownloadFile(0); |
| 222 } | 423 } |
| 223 | 424 |
| 224 // Send some data, wait 3/4s of a second, run the message loop, and | 425 // Send some data, wait 3/4s of a second, run the message loop, and |
| 225 // confirm the values the DownloadManager received are correct. | 426 // confirm the values the DownloadManager received are correct. |
| 226 TEST_F(DownloadFileTest, ConfirmUpdate) { | 427 TEST_F(DownloadFileTest, ConfirmUpdate) { |
| 227 CreateDownloadFile(&download_file_, 0, true); | 428 CreateDownloadFile(0, true); |
| 228 ASSERT_EQ(net::OK, download_file_->Initialize()); | |
| 229 | 429 |
| 230 AppendDataToFile(&download_file_, kTestData1); | 430 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 231 AppendDataToFile(&download_file_, kTestData2); | 431 AppendDataToFile(chunks1, 2); |
| 232 | 432 |
| 233 // Run the message loops for 750ms and check for results. | 433 // Run the message loops for 750ms and check for results. |
| 234 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), | 434 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
| 235 base::TimeDelta::FromMilliseconds(750)); | 435 base::TimeDelta::FromMilliseconds(750)); |
| 236 loop_.Run(); | 436 loop_.Run(); |
| 237 | 437 |
| 238 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 438 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
| 239 bytes_); | 439 bytes_); |
| 240 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 440 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
| 241 | 441 |
| 242 download_file_->Finish(); | 442 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 243 DestroyDownloadFile(&download_file_, 0); | 443 DestroyDownloadFile(0); |
| 244 } | 444 } |
| OLD | NEW |