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