| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/download_create_info.h" | 9 #include "content/browser/download/download_create_info.h" |
| 10 #include "content/browser/download/download_file_impl.h" | 10 #include "content/browser/download/download_file_impl.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 virtual void TearDown() { | 59 virtual void TearDown() { |
| 60 // When a DownloadManager's reference count drops to 0, it is not | 60 // When a DownloadManager's reference count drops to 0, it is not |
| 61 // deleted immediately. Instead, a task is posted to the UI thread's | 61 // deleted immediately. Instead, a task is posted to the UI thread's |
| 62 // message loop to delete it. | 62 // message loop to delete it. |
| 63 // So, drop the reference count to 0 and run the message loop once | 63 // So, drop the reference count to 0 and run the message loop once |
| 64 // to ensure that all resources are cleaned up before the test exits. | 64 // to ensure that all resources are cleaned up before the test exits. |
| 65 download_manager_ = NULL; | 65 download_manager_ = NULL; |
| 66 ui_thread_.message_loop()->RunAllPending(); | 66 ui_thread_.message_loop()->RunAllPending(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | 69 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, |
| 70 int offset, |
| 71 bool calculate_hash) { |
| 70 DownloadCreateInfo info; | 72 DownloadCreateInfo info; |
| 71 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); | 73 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); |
| 72 // info.request_handle default constructed to null. | 74 // info.request_handle default constructed to null. |
| 73 info.save_info.file_stream = file_stream_; | 75 info.save_info.file_stream = file_stream_; |
| 74 file->reset( | 76 file->reset( |
| 75 new DownloadFileImpl(&info, new DownloadRequestHandle(), | 77 new DownloadFileImpl(&info, new DownloadRequestHandle(), |
| 76 download_manager_)); | 78 download_manager_, calculate_hash)); |
| 77 } | 79 } |
| 78 | 80 |
| 79 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | 81 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { |
| 80 EXPECT_EQ(kDummyDownloadId + offset, (*file)->Id()); | 82 EXPECT_EQ(kDummyDownloadId + offset, (*file)->Id()); |
| 81 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); | 83 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); |
| 82 EXPECT_FALSE((*file)->InProgress()); | 84 EXPECT_FALSE((*file)->InProgress()); |
| 83 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 85 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
| 84 (*file)->BytesSoFar()); | 86 (*file)->BytesSoFar()); |
| 85 | 87 |
| 86 // Make sure the data has been properly written to disk. | 88 // Make sure the data has been properly written to disk. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 const char* DownloadFileTest::kDataHash = | 134 const char* DownloadFileTest::kDataHash = |
| 133 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8"; | 135 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8"; |
| 134 | 136 |
| 135 const int32 DownloadFileTest::kDummyDownloadId = 23; | 137 const int32 DownloadFileTest::kDummyDownloadId = 23; |
| 136 const int DownloadFileTest::kDummyChildId = 3; | 138 const int DownloadFileTest::kDummyChildId = 3; |
| 137 const int DownloadFileTest::kDummyRequestId = 67; | 139 const int DownloadFileTest::kDummyRequestId = 67; |
| 138 | 140 |
| 139 // Rename the file before any data is downloaded, after some has, after it all | 141 // Rename the file before any data is downloaded, after some has, after it all |
| 140 // has, and after it's closed. | 142 // has, and after it's closed. |
| 141 TEST_F(DownloadFileTest, RenameFileFinal) { | 143 TEST_F(DownloadFileTest, RenameFileFinal) { |
| 142 CreateDownloadFile(&download_file_, 0); | 144 CreateDownloadFile(&download_file_, 0, true); |
| 143 ASSERT_EQ(net::OK, download_file_->Initialize(true)); | 145 ASSERT_EQ(net::OK, download_file_->Initialize()); |
| 144 FilePath initial_path(download_file_->FullPath()); | 146 FilePath initial_path(download_file_->FullPath()); |
| 145 EXPECT_TRUE(file_util::PathExists(initial_path)); | 147 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 146 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | 148 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); |
| 147 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); | 149 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); |
| 148 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); | 150 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); |
| 149 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); | 151 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); |
| 150 | 152 |
| 151 // Rename the file before downloading any data. | 153 // Rename the file before downloading any data. |
| 152 EXPECT_EQ(net::OK, download_file_->Rename(path_1)); | 154 EXPECT_EQ(net::OK, download_file_->Rename(path_1)); |
| 153 FilePath renamed_path = download_file_->FullPath(); | 155 FilePath renamed_path = download_file_->FullPath(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 176 EXPECT_EQ(net::OK, download_file_->Rename(path_3)); | 178 EXPECT_EQ(net::OK, download_file_->Rename(path_3)); |
| 177 renamed_path = download_file_->FullPath(); | 179 renamed_path = download_file_->FullPath(); |
| 178 EXPECT_EQ(path_3, renamed_path); | 180 EXPECT_EQ(path_3, renamed_path); |
| 179 | 181 |
| 180 // Check the files. | 182 // Check the files. |
| 181 EXPECT_FALSE(file_util::PathExists(path_2)); | 183 EXPECT_FALSE(file_util::PathExists(path_2)); |
| 182 EXPECT_TRUE(file_util::PathExists(path_3)); | 184 EXPECT_TRUE(file_util::PathExists(path_3)); |
| 183 | 185 |
| 184 // Should not be able to get the hash until the file is closed. | 186 // Should not be able to get the hash until the file is closed. |
| 185 std::string hash; | 187 std::string hash; |
| 186 EXPECT_FALSE(download_file_->GetSha256Hash(&hash)); | 188 EXPECT_FALSE(download_file_->GetHash(&hash)); |
| 187 | 189 |
| 188 download_file_->Finish(); | 190 download_file_->Finish(); |
| 189 | 191 |
| 190 // Rename the file after downloading all the data and closing the file. | 192 // Rename the file after downloading all the data and closing the file. |
| 191 EXPECT_EQ(net::OK, download_file_->Rename(path_4)); | 193 EXPECT_EQ(net::OK, download_file_->Rename(path_4)); |
| 192 renamed_path = download_file_->FullPath(); | 194 renamed_path = download_file_->FullPath(); |
| 193 EXPECT_EQ(path_4, renamed_path); | 195 EXPECT_EQ(path_4, renamed_path); |
| 194 | 196 |
| 195 // Check the files. | 197 // Check the files. |
| 196 EXPECT_FALSE(file_util::PathExists(path_3)); | 198 EXPECT_FALSE(file_util::PathExists(path_3)); |
| 197 EXPECT_TRUE(file_util::PathExists(path_4)); | 199 EXPECT_TRUE(file_util::PathExists(path_4)); |
| 198 | 200 |
| 199 // Check the hash. | 201 // Check the hash. |
| 200 EXPECT_TRUE(download_file_->GetSha256Hash(&hash)); | 202 EXPECT_TRUE(download_file_->GetHash(&hash)); |
| 201 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 203 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
| 202 | 204 |
| 203 DestroyDownloadFile(&download_file_, 0); | 205 DestroyDownloadFile(&download_file_, 0); |
| 204 } | 206 } |
| OLD | NEW |