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