| 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/byte_stream.h" |
| 10 #include "content/browser/download/download_create_info.h" | 10 #include "content/browser/download/download_create_info.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 &info, | 125 &info, |
| 126 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), | 126 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), |
| 127 new DownloadRequestHandle(), | 127 new DownloadRequestHandle(), |
| 128 download_manager_, calculate_hash, | 128 download_manager_, calculate_hash, |
| 129 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), | 129 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), |
| 130 net::BoundNetLog())); | 130 net::BoundNetLog())); |
| 131 | 131 |
| 132 EXPECT_CALL(*input_stream_, Read(_, _)) | 132 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 133 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) | 133 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) |
| 134 .RetiresOnSaturation(); | 134 .RetiresOnSaturation(); |
| 135 net::Error result = download_file_->Initialize(); | 135 content::DownloadInterruptReason result = download_file_->Initialize(); |
| 136 ::testing::Mock::VerifyAndClearExpectations(input_stream_); | 136 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
| 137 return result == net::OK; | 137 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; |
| 138 } | 138 } |
| 139 | 139 |
| 140 virtual void DestroyDownloadFile(int offset) { | 140 virtual void DestroyDownloadFile(int offset) { |
| 141 EXPECT_EQ(kDummyDownloadId + offset, download_file_->Id()); | 141 EXPECT_EQ(kDummyDownloadId + offset, download_file_->Id()); |
| 142 EXPECT_EQ(download_manager_, download_file_->GetDownloadManager()); | 142 EXPECT_EQ(download_manager_, download_file_->GetDownloadManager()); |
| 143 EXPECT_FALSE(download_file_->InProgress()); | 143 EXPECT_FALSE(download_file_->InProgress()); |
| 144 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 144 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
| 145 download_file_->BytesSoFar()); | 145 download_file_->BytesSoFar()); |
| 146 | 146 |
| 147 // Make sure the data has been properly written to disk. | 147 // Make sure the data has been properly written to disk. |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 TEST_F(DownloadFileTest, RenameFileFinal) { | 276 TEST_F(DownloadFileTest, RenameFileFinal) { |
| 277 ASSERT_TRUE(CreateDownloadFile(0, true)); | 277 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 278 FilePath initial_path(download_file_->FullPath()); | 278 FilePath initial_path(download_file_->FullPath()); |
| 279 EXPECT_TRUE(file_util::PathExists(initial_path)); | 279 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 280 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | 280 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); |
| 281 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); | 281 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); |
| 282 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); | 282 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); |
| 283 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); | 283 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); |
| 284 | 284 |
| 285 // Rename the file before downloading any data. | 285 // Rename the file before downloading any data. |
| 286 EXPECT_EQ(net::OK, download_file_->Rename(path_1)); | 286 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 287 download_file_->Rename(path_1)); |
| 287 FilePath renamed_path = download_file_->FullPath(); | 288 FilePath renamed_path = download_file_->FullPath(); |
| 288 EXPECT_EQ(path_1, renamed_path); | 289 EXPECT_EQ(path_1, renamed_path); |
| 289 | 290 |
| 290 // Check the files. | 291 // Check the files. |
| 291 EXPECT_FALSE(file_util::PathExists(initial_path)); | 292 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 292 EXPECT_TRUE(file_util::PathExists(path_1)); | 293 EXPECT_TRUE(file_util::PathExists(path_1)); |
| 293 | 294 |
| 294 // Download the data. | 295 // Download the data. |
| 295 const char* chunks1[] = { kTestData1, kTestData2 }; | 296 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 296 AppendDataToFile(chunks1, 2); | 297 AppendDataToFile(chunks1, 2); |
| 297 | 298 |
| 298 // Rename the file after downloading some data. | 299 // Rename the file after downloading some data. |
| 299 EXPECT_EQ(net::OK, download_file_->Rename(path_2)); | 300 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 301 download_file_->Rename(path_2)); |
| 300 renamed_path = download_file_->FullPath(); | 302 renamed_path = download_file_->FullPath(); |
| 301 EXPECT_EQ(path_2, renamed_path); | 303 EXPECT_EQ(path_2, renamed_path); |
| 302 | 304 |
| 303 // Check the files. | 305 // Check the files. |
| 304 EXPECT_FALSE(file_util::PathExists(path_1)); | 306 EXPECT_FALSE(file_util::PathExists(path_1)); |
| 305 EXPECT_TRUE(file_util::PathExists(path_2)); | 307 EXPECT_TRUE(file_util::PathExists(path_2)); |
| 306 | 308 |
| 307 const char* chunks2[] = { kTestData3 }; | 309 const char* chunks2[] = { kTestData3 }; |
| 308 AppendDataToFile(chunks2, 1); | 310 AppendDataToFile(chunks2, 1); |
| 309 | 311 |
| 310 // Rename the file after downloading all the data. | 312 // Rename the file after downloading all the data. |
| 311 EXPECT_EQ(net::OK, download_file_->Rename(path_3)); | 313 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 314 download_file_->Rename(path_3)); |
| 312 renamed_path = download_file_->FullPath(); | 315 renamed_path = download_file_->FullPath(); |
| 313 EXPECT_EQ(path_3, renamed_path); | 316 EXPECT_EQ(path_3, renamed_path); |
| 314 | 317 |
| 315 // Check the files. | 318 // Check the files. |
| 316 EXPECT_FALSE(file_util::PathExists(path_2)); | 319 EXPECT_FALSE(file_util::PathExists(path_2)); |
| 317 EXPECT_TRUE(file_util::PathExists(path_3)); | 320 EXPECT_TRUE(file_util::PathExists(path_3)); |
| 318 | 321 |
| 319 // Should not be able to get the hash until the file is closed. | 322 // Should not be able to get the hash until the file is closed. |
| 320 std::string hash; | 323 std::string hash; |
| 321 EXPECT_FALSE(download_file_->GetHash(&hash)); | 324 EXPECT_FALSE(download_file_->GetHash(&hash)); |
| 322 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | 325 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 323 loop_.RunAllPending(); | 326 loop_.RunAllPending(); |
| 324 | 327 |
| 325 // Rename the file after downloading all the data and closing the file. | 328 // Rename the file after downloading all the data and closing the file. |
| 326 EXPECT_EQ(net::OK, download_file_->Rename(path_4)); | 329 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 330 download_file_->Rename(path_4)); |
| 327 renamed_path = download_file_->FullPath(); | 331 renamed_path = download_file_->FullPath(); |
| 328 EXPECT_EQ(path_4, renamed_path); | 332 EXPECT_EQ(path_4, renamed_path); |
| 329 | 333 |
| 330 // Check the files. | 334 // Check the files. |
| 331 EXPECT_FALSE(file_util::PathExists(path_3)); | 335 EXPECT_FALSE(file_util::PathExists(path_3)); |
| 332 EXPECT_TRUE(file_util::PathExists(path_4)); | 336 EXPECT_TRUE(file_util::PathExists(path_4)); |
| 333 | 337 |
| 334 // Check the hash. | 338 // Check the hash. |
| 335 EXPECT_TRUE(download_file_->GetHash(&hash)); | 339 EXPECT_TRUE(download_file_->GetHash(&hash)); |
| 336 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 340 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 base::TimeDelta::FromMilliseconds(750)); | 439 base::TimeDelta::FromMilliseconds(750)); |
| 436 loop_.Run(); | 440 loop_.Run(); |
| 437 | 441 |
| 438 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 442 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
| 439 bytes_); | 443 bytes_); |
| 440 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 444 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
| 441 | 445 |
| 442 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | 446 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 443 DestroyDownloadFile(0); | 447 DestroyDownloadFile(0); |
| 444 } | 448 } |
| OLD | NEW |