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