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 "base/test/test_file_util.h" | |
9 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
10 #include "content/browser/download/byte_stream.h" | 9 #include "content/browser/download/byte_stream.h" |
11 #include "content/browser/download/download_create_info.h" | 10 #include "content/browser/download/download_create_info.h" |
12 #include "content/browser/download/download_file_impl.h" | 11 #include "content/browser/download/download_file_impl.h" |
13 #include "content/browser/download/download_request_handle.h" | 12 #include "content/browser/download/download_request_handle.h" |
14 #include "content/browser/power_save_blocker.h" | 13 #include "content/browser/power_save_blocker.h" |
15 #include "content/public/browser/download_interrupt_reasons.h" | 14 #include "content/public/browser/download_interrupt_reasons.h" |
16 #include "content/public/browser/download_manager.h" | 15 #include "content/public/browser/download_manager.h" |
17 #include "content/public/test/mock_download_manager.h" | 16 #include "content/public/test/mock_download_manager.h" |
18 #include "net/base/file_stream.h" | 17 #include "net/base/file_stream.h" |
19 #include "net/base/mock_file_stream.h" | |
20 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
23 | 21 |
24 using content::BrowserThread; | 22 using content::BrowserThread; |
25 using content::BrowserThreadImpl; | 23 using content::BrowserThreadImpl; |
26 using content::DownloadFile; | 24 using content::DownloadFile; |
27 using content::DownloadId; | 25 using content::DownloadId; |
28 using content::DownloadManager; | 26 using content::DownloadManager; |
29 using ::testing::_; | 27 using ::testing::_; |
30 using ::testing::AnyNumber; | 28 using ::testing::AnyNumber; |
31 using ::testing::DoAll; | 29 using ::testing::DoAll; |
32 using ::testing::InSequence; | |
33 using ::testing::Return; | 30 using ::testing::Return; |
34 using ::testing::SetArgPointee; | 31 using ::testing::SetArgPointee; |
35 using ::testing::StrictMock; | 32 using ::testing::StrictMock; |
36 | 33 |
37 namespace { | 34 namespace { |
38 | 35 |
39 class MockByteStreamReader : public content::ByteStreamReader { | 36 class MockByteStreamReader : public content::ByteStreamReader { |
40 public: | 37 public: |
41 MockByteStreamReader() {} | 38 MockByteStreamReader() {} |
42 ~MockByteStreamReader() {} | 39 ~MockByteStreamReader() {} |
43 | 40 |
44 // ByteStream functions | 41 // ByteStream functions |
45 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState( | 42 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState( |
46 scoped_refptr<net::IOBuffer>*, size_t*)); | 43 scoped_refptr<net::IOBuffer>*, size_t*)); |
47 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason()); | 44 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason()); |
48 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); | 45 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); |
49 }; | 46 }; |
50 | 47 |
51 class LocalMockDownloadManager : public content::MockDownloadManager { | |
52 public: | |
53 MOCK_METHOD4(CurrentUpdateStatus, | |
54 void(int32, int64, int64, const std::string&)); | |
55 protected: | |
56 virtual ~LocalMockDownloadManager() {} | |
57 }; | |
58 | |
59 } // namespace | 48 } // namespace |
60 | 49 |
61 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; | 50 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
62 | 51 |
63 class DownloadFileTest : public testing::Test { | 52 class DownloadFileTest : public testing::Test { |
64 public: | 53 public: |
65 | 54 |
66 static const char* kTestData1; | 55 static const char* kTestData1; |
67 static const char* kTestData2; | 56 static const char* kTestData2; |
68 static const char* kTestData3; | 57 static const char* kTestData3; |
69 static const char* kDataHash; | 58 static const char* kDataHash; |
70 static const int32 kDummyDownloadId; | 59 static const int32 kDummyDownloadId; |
71 static const int kDummyChildId; | 60 static const int kDummyChildId; |
72 static const int kDummyRequestId; | 61 static const int kDummyRequestId; |
73 | 62 |
74 // We need a UI |BrowserThread| in order to destruct |download_manager_|, | 63 // We need a UI |BrowserThread| in order to destruct |download_manager_|, |
75 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, | 64 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, |
76 // calling Release() on |download_manager_| won't ever result in its | 65 // calling Release() on |download_manager_| won't ever result in its |
77 // destructor being called and we get a leak. | 66 // destructor being called and we get a leak. |
78 DownloadFileTest() : | 67 DownloadFileTest() : |
79 update_download_id_(-1), | |
80 bytes_(-1), | |
81 bytes_per_sec_(-1), | |
82 hash_state_("xyzzy"), | |
83 ui_thread_(BrowserThread::UI, &loop_), | 68 ui_thread_(BrowserThread::UI, &loop_), |
84 file_thread_(BrowserThread::FILE, &loop_) { | 69 file_thread_(BrowserThread::FILE, &loop_) { |
85 } | 70 } |
86 | 71 |
87 ~DownloadFileTest() { | 72 ~DownloadFileTest() { |
88 } | 73 } |
89 | 74 |
90 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec, | 75 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec, |
91 const std::string& hash_state) { | 76 const std::string& hash_state) { |
92 update_download_id_ = id; | |
93 bytes_ = bytes; | 77 bytes_ = bytes; |
94 bytes_per_sec_ = bytes_per_sec; | 78 bytes_per_sec_ = bytes_per_sec; |
95 hash_state_ = hash_state; | 79 hash_state_ = hash_state; |
96 } | 80 } |
97 | 81 |
98 void ConfirmUpdateDownloadInfo() { | |
99 download_manager_->CurrentUpdateStatus( | |
100 update_download_id_, bytes_, bytes_per_sec_, hash_state_); | |
101 } | |
102 | |
103 virtual void SetUp() { | 82 virtual void SetUp() { |
104 download_manager_ = new StrictMock<LocalMockDownloadManager>; | 83 download_manager_ = new StrictMock<content::MockDownloadManager>; |
105 EXPECT_CALL(*(download_manager_.get()), | 84 EXPECT_CALL(*(download_manager_.get()), |
106 UpdateDownload( | 85 UpdateDownload( |
107 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | 86 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
108 _, _, _)) | 87 _, _, _)) |
109 .Times(AnyNumber()) | 88 .Times(AnyNumber()) |
110 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); | 89 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); |
111 } | 90 } |
112 | 91 |
113 virtual void TearDown() { | 92 virtual void TearDown() { |
114 // When a DownloadManager's reference count drops to 0, it is not | 93 // When a DownloadManager's reference count drops to 0, it is not |
(...skipping 16 matching lines...) Expand all Loading... |
131 | 110 |
132 input_stream_ = new StrictMock<MockByteStreamReader>(); | 111 input_stream_ = new StrictMock<MockByteStreamReader>(); |
133 | 112 |
134 // TODO: Need to actually create a function that'll set the variables | 113 // TODO: Need to actually create a function that'll set the variables |
135 // based on the inputs from the callback. | 114 // based on the inputs from the callback. |
136 EXPECT_CALL(*input_stream_, RegisterCallback(_)) | 115 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
137 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) | 116 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) |
138 .RetiresOnSaturation(); | 117 .RetiresOnSaturation(); |
139 | 118 |
140 DownloadCreateInfo info; | 119 DownloadCreateInfo info; |
| 120 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); |
141 // info.request_handle default constructed to null. | 121 // info.request_handle default constructed to null. |
142 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); | |
143 info.save_info.file_stream = file_stream_; | 122 info.save_info.file_stream = file_stream_; |
144 download_file_.reset( | 123 download_file_.reset( |
145 new DownloadFileImpl( | 124 new DownloadFileImpl( |
146 &info, | 125 &info, |
147 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), | 126 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), |
148 new DownloadRequestHandle(), | 127 new DownloadRequestHandle(), |
149 download_manager_, calculate_hash, | 128 download_manager_, calculate_hash, |
150 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), | 129 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), |
151 net::BoundNetLog())); | 130 net::BoundNetLog())); |
152 | 131 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 EXPECT_CALL(*(download_manager_.get()), | 221 EXPECT_CALL(*(download_manager_.get()), |
243 UpdateDownload( | 222 UpdateDownload( |
244 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | 223 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
245 _, _, _)) | 224 _, _, _)) |
246 .Times(AnyNumber()) | 225 .Times(AnyNumber()) |
247 .WillRepeatedly(Invoke(this, | 226 .WillRepeatedly(Invoke(this, |
248 &DownloadFileTest::SetUpdateDownloadInfo)); | 227 &DownloadFileTest::SetUpdateDownloadInfo)); |
249 } | 228 } |
250 } | 229 } |
251 | 230 |
252 content::DownloadInterruptReason Rename( | |
253 const FilePath& full_path, bool overwrite_existing_file, | |
254 FilePath* result_path_p) { | |
255 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | |
256 content::DownloadInterruptReason result_reason( | |
257 content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
258 bool callback_was_called(false); | |
259 FilePath result_path; | |
260 | |
261 download_file_->Rename(full_path, overwrite_existing_file, | |
262 base::Bind(&DownloadFileTest::SetRenameResult, | |
263 weak_ptr_factory.GetWeakPtr(), | |
264 &callback_was_called, | |
265 &result_reason, result_path_p)); | |
266 loop_.RunAllPending(); | |
267 | |
268 EXPECT_TRUE(callback_was_called); | |
269 return result_reason; | |
270 } | |
271 | |
272 protected: | 231 protected: |
273 scoped_refptr<StrictMock<LocalMockDownloadManager> > download_manager_; | 232 scoped_refptr<StrictMock<content::MockDownloadManager> > download_manager_; |
274 | 233 |
275 linked_ptr<net::FileStream> file_stream_; | 234 linked_ptr<net::FileStream> file_stream_; |
276 | 235 |
277 // DownloadFile instance we are testing. | 236 // DownloadFile instance we are testing. |
278 scoped_ptr<DownloadFile> download_file_; | 237 scoped_ptr<DownloadFile> download_file_; |
279 | 238 |
280 // Stream for sending data into the download file. | 239 // Stream for sending data into the download file. |
281 // Owned by download_file_; will be alive for lifetime of download_file_. | 240 // Owned by download_file_; will be alive for lifetime of download_file_. |
282 StrictMock<MockByteStreamReader>* input_stream_; | 241 StrictMock<MockByteStreamReader>* input_stream_; |
283 | 242 |
284 // Sink callback data for stream. | 243 // Sink callback data for stream. |
285 base::Closure sink_callback_; | 244 base::Closure sink_callback_; |
286 | 245 |
287 // Latest update sent to the download manager. | 246 // Latest update sent to the download manager. |
288 int32 update_download_id_; | |
289 int64 bytes_; | 247 int64 bytes_; |
290 int64 bytes_per_sec_; | 248 int64 bytes_per_sec_; |
291 std::string hash_state_; | 249 std::string hash_state_; |
292 | 250 |
293 MessageLoop loop_; | 251 MessageLoop loop_; |
294 | 252 |
295 private: | 253 private: |
296 void SetRenameResult(bool* called_p, | |
297 content::DownloadInterruptReason* reason_p, | |
298 FilePath* result_path_p, | |
299 content::DownloadInterruptReason reason, | |
300 const FilePath& result_path) { | |
301 if (called_p) | |
302 *called_p = true; | |
303 if (reason_p) | |
304 *reason_p = reason; | |
305 if (result_path_p) | |
306 *result_path_p = result_path; | |
307 } | |
308 | |
309 // UI thread. | 254 // UI thread. |
310 BrowserThreadImpl ui_thread_; | 255 BrowserThreadImpl ui_thread_; |
311 // File thread to satisfy debug checks in DownloadFile. | 256 // File thread to satisfy debug checks in DownloadFile. |
312 BrowserThreadImpl file_thread_; | 257 BrowserThreadImpl file_thread_; |
313 | 258 |
314 // Keep track of what data should be saved to the disk file. | 259 // Keep track of what data should be saved to the disk file. |
315 std::string expected_data_; | 260 std::string expected_data_; |
316 }; | 261 }; |
317 | 262 |
318 const char* DownloadFileTest::kTestData1 = | 263 const char* DownloadFileTest::kTestData1 = |
(...skipping 10 matching lines...) Expand all Loading... |
329 // Rename the file before any data is downloaded, after some has, after it all | 274 // Rename the file before any data is downloaded, after some has, after it all |
330 // has, and after it's closed. | 275 // has, and after it's closed. |
331 TEST_F(DownloadFileTest, RenameFileFinal) { | 276 TEST_F(DownloadFileTest, RenameFileFinal) { |
332 ASSERT_TRUE(CreateDownloadFile(0, true)); | 277 ASSERT_TRUE(CreateDownloadFile(0, true)); |
333 FilePath initial_path(download_file_->FullPath()); | 278 FilePath initial_path(download_file_->FullPath()); |
334 EXPECT_TRUE(file_util::PathExists(initial_path)); | 279 EXPECT_TRUE(file_util::PathExists(initial_path)); |
335 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | 280 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); |
336 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); | 281 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); |
337 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); | 282 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); |
338 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); | 283 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); |
339 FilePath path_5(initial_path.InsertBeforeExtensionASCII("_5")); | |
340 FilePath output_path; | |
341 | 284 |
342 // Rename the file before downloading any data. | 285 // Rename the file before downloading any data. |
343 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | 286 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
344 Rename(path_1, false, &output_path)); | 287 download_file_->Rename(path_1)); |
345 FilePath renamed_path = download_file_->FullPath(); | 288 FilePath renamed_path = download_file_->FullPath(); |
346 EXPECT_EQ(path_1, renamed_path); | 289 EXPECT_EQ(path_1, renamed_path); |
347 EXPECT_EQ(path_1, output_path); | |
348 | 290 |
349 // Check the files. | 291 // Check the files. |
350 EXPECT_FALSE(file_util::PathExists(initial_path)); | 292 EXPECT_FALSE(file_util::PathExists(initial_path)); |
351 EXPECT_TRUE(file_util::PathExists(path_1)); | 293 EXPECT_TRUE(file_util::PathExists(path_1)); |
352 | 294 |
353 // Download the data. | 295 // Download the data. |
354 const char* chunks1[] = { kTestData1, kTestData2 }; | 296 const char* chunks1[] = { kTestData1, kTestData2 }; |
355 AppendDataToFile(chunks1, 2); | 297 AppendDataToFile(chunks1, 2); |
356 | 298 |
357 // Rename the file after downloading some data. | 299 // Rename the file after downloading some data. |
358 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | 300 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
359 Rename(path_2, false, &output_path)); | 301 download_file_->Rename(path_2)); |
360 renamed_path = download_file_->FullPath(); | 302 renamed_path = download_file_->FullPath(); |
361 EXPECT_EQ(path_2, renamed_path); | 303 EXPECT_EQ(path_2, renamed_path); |
362 EXPECT_EQ(path_2, output_path); | |
363 | 304 |
364 // Check the files. | 305 // Check the files. |
365 EXPECT_FALSE(file_util::PathExists(path_1)); | 306 EXPECT_FALSE(file_util::PathExists(path_1)); |
366 EXPECT_TRUE(file_util::PathExists(path_2)); | 307 EXPECT_TRUE(file_util::PathExists(path_2)); |
367 | 308 |
368 const char* chunks2[] = { kTestData3 }; | 309 const char* chunks2[] = { kTestData3 }; |
369 AppendDataToFile(chunks2, 1); | 310 AppendDataToFile(chunks2, 1); |
370 | 311 |
371 // Rename the file after downloading all the data. | 312 // Rename the file after downloading all the data. |
372 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | 313 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
373 Rename(path_3, false, &output_path)); | 314 download_file_->Rename(path_3)); |
374 renamed_path = download_file_->FullPath(); | 315 renamed_path = download_file_->FullPath(); |
375 EXPECT_EQ(path_3, renamed_path); | 316 EXPECT_EQ(path_3, renamed_path); |
376 EXPECT_EQ(path_3, output_path); | |
377 | 317 |
378 // Check the files. | 318 // Check the files. |
379 EXPECT_FALSE(file_util::PathExists(path_2)); | 319 EXPECT_FALSE(file_util::PathExists(path_2)); |
380 EXPECT_TRUE(file_util::PathExists(path_3)); | 320 EXPECT_TRUE(file_util::PathExists(path_3)); |
381 | 321 |
382 // 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. |
383 std::string hash; | 323 std::string hash; |
384 EXPECT_FALSE(download_file_->GetHash(&hash)); | 324 EXPECT_FALSE(download_file_->GetHash(&hash)); |
385 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | 325 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
386 loop_.RunAllPending(); | 326 loop_.RunAllPending(); |
387 | 327 |
388 // 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. |
389 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | 329 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
390 Rename(path_4, false, &output_path)); | 330 download_file_->Rename(path_4)); |
391 renamed_path = download_file_->FullPath(); | 331 renamed_path = download_file_->FullPath(); |
392 EXPECT_EQ(path_4, renamed_path); | 332 EXPECT_EQ(path_4, renamed_path); |
393 EXPECT_EQ(path_4, output_path); | |
394 | 333 |
395 // Check the files. | 334 // Check the files. |
396 EXPECT_FALSE(file_util::PathExists(path_3)); | 335 EXPECT_FALSE(file_util::PathExists(path_3)); |
397 EXPECT_TRUE(file_util::PathExists(path_4)); | 336 EXPECT_TRUE(file_util::PathExists(path_4)); |
398 | 337 |
399 // Check the hash. | 338 // Check the hash. |
400 EXPECT_TRUE(download_file_->GetHash(&hash)); | 339 EXPECT_TRUE(download_file_->GetHash(&hash)); |
401 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 340 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
402 | 341 |
403 // Check that a rename with overwrite to an existing file succeeds. | |
404 std::string file_contents; | |
405 ASSERT_FALSE(file_util::PathExists(path_5)); | |
406 static const char file_data[] = "xyzzy"; | |
407 ASSERT_EQ(static_cast<int>(sizeof(file_data) - 1), | |
408 file_util::WriteFile(path_5, file_data, sizeof(file_data) - 1)); | |
409 ASSERT_TRUE(file_util::PathExists(path_5)); | |
410 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); | |
411 EXPECT_EQ(std::string(file_data), file_contents); | |
412 | |
413 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
414 Rename(path_5, true, &output_path)); | |
415 EXPECT_EQ(path_5, output_path); | |
416 | |
417 file_contents = ""; | |
418 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); | |
419 EXPECT_NE(std::string(file_data), file_contents); | |
420 | |
421 DestroyDownloadFile(0); | |
422 } | |
423 | |
424 // Test to make sure the rename uniquifies if we aren't overwriting | |
425 // and there's a file where we're aiming. | |
426 TEST_F(DownloadFileTest, RenameUniquifies) { | |
427 ASSERT_TRUE(CreateDownloadFile(0, true)); | |
428 FilePath initial_path(download_file_->FullPath()); | |
429 EXPECT_TRUE(file_util::PathExists(initial_path)); | |
430 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | |
431 FilePath path_1_suffixed(path_1.InsertBeforeExtensionASCII(" (1)")); | |
432 | |
433 ASSERT_FALSE(file_util::PathExists(path_1)); | |
434 static const char file_data[] = "xyzzy"; | |
435 ASSERT_EQ(static_cast<int>(sizeof(file_data)), | |
436 file_util::WriteFile(path_1, file_data, sizeof(file_data))); | |
437 ASSERT_TRUE(file_util::PathExists(path_1)); | |
438 | |
439 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
440 Rename(path_1, false, NULL)); | |
441 EXPECT_TRUE(file_util::PathExists(path_1_suffixed)); | |
442 | |
443 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | |
444 loop_.RunAllPending(); | |
445 DestroyDownloadFile(0); | |
446 } | |
447 | |
448 // Test to make sure we get the proper error on failure. | |
449 TEST_F(DownloadFileTest, RenameError) { | |
450 ASSERT_TRUE(CreateDownloadFile(0, true)); | |
451 FilePath initial_path(download_file_->FullPath()); | |
452 | |
453 // Create a subdirectory and rename into it. | |
454 FilePath tempdir(initial_path.DirName().Append(FILE_PATH_LITERAL("tempdir"))); | |
455 ASSERT_TRUE(file_util::CreateDirectory(tempdir)); | |
456 FilePath new_path(tempdir.Append(initial_path.BaseName())); | |
457 ASSERT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
458 Rename(new_path, true, NULL)); | |
459 | |
460 // Targets | |
461 FilePath path_1(new_path.InsertBeforeExtensionASCII("_1")); | |
462 FilePath path_1_suffixed(path_1.InsertBeforeExtensionASCII(" (1)")); | |
463 ASSERT_FALSE(file_util::PathExists(path_1)); | |
464 ASSERT_FALSE(file_util::PathExists(path_1_suffixed)); | |
465 | |
466 // Make the directory unwritable and try to rename within it. | |
467 { | |
468 file_util::PermissionRestorer restorer(tempdir); | |
469 ASSERT_TRUE(file_util::MakeFileUnwritable(tempdir)); | |
470 | |
471 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | |
472 Rename(path_1, true, NULL)); | |
473 EXPECT_FALSE(file_util::PathExists(path_1_suffixed)); | |
474 } | |
475 | |
476 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | |
477 loop_.RunAllPending(); | |
478 DestroyDownloadFile(0); | 342 DestroyDownloadFile(0); |
479 } | 343 } |
480 | 344 |
481 // Various tests of the StreamActive method. | 345 // Various tests of the StreamActive method. |
482 TEST_F(DownloadFileTest, StreamEmptySuccess) { | 346 TEST_F(DownloadFileTest, StreamEmptySuccess) { |
483 ASSERT_TRUE(CreateDownloadFile(0, true)); | 347 ASSERT_TRUE(CreateDownloadFile(0, true)); |
484 FilePath initial_path(download_file_->FullPath()); | 348 FilePath initial_path(download_file_->FullPath()); |
485 EXPECT_TRUE(file_util::PathExists(initial_path)); | 349 EXPECT_TRUE(file_util::PathExists(initial_path)); |
486 | 350 |
487 // Test that calling the sink_callback_ on an empty stream shouldn't | 351 // Test that calling the sink_callback_ on an empty stream shouldn't |
(...skipping 21 matching lines...) Expand all Loading... |
509 TEST_F(DownloadFileTest, StreamEmptyError) { | 373 TEST_F(DownloadFileTest, StreamEmptyError) { |
510 ASSERT_TRUE(CreateDownloadFile(0, true)); | 374 ASSERT_TRUE(CreateDownloadFile(0, true)); |
511 FilePath initial_path(download_file_->FullPath()); | 375 FilePath initial_path(download_file_->FullPath()); |
512 EXPECT_TRUE(file_util::PathExists(initial_path)); | 376 EXPECT_TRUE(file_util::PathExists(initial_path)); |
513 | 377 |
514 // Finish the download in error and make sure we see it on the | 378 // Finish the download in error and make sure we see it on the |
515 // DownloadManager. | 379 // DownloadManager. |
516 EXPECT_CALL(*(download_manager_.get()), | 380 EXPECT_CALL(*(download_manager_.get()), |
517 OnDownloadInterrupted( | 381 OnDownloadInterrupted( |
518 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | 382 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
519 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) | 383 0, _, |
520 .WillOnce(InvokeWithoutArgs( | 384 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)); |
521 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); | |
522 | |
523 // If this next EXPECT_CALL fails flakily, it's probably a real failure. | |
524 // We'll be getting a stream of UpdateDownload calls from the timer, and | |
525 // the last one may have the correct information even if the failure | |
526 // doesn't produce an update, as the timer update may have triggered at the | |
527 // same time. | |
528 EXPECT_CALL(*(download_manager_.get()), | |
529 CurrentUpdateStatus(kDummyDownloadId + 0, 0, _, _)); | |
530 | |
531 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); | 385 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); |
532 | 386 |
533 loop_.RunAllPending(); | |
534 | |
535 DestroyDownloadFile(0); | 387 DestroyDownloadFile(0); |
536 } | 388 } |
537 | 389 |
538 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { | 390 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { |
539 ASSERT_TRUE(CreateDownloadFile(0, true)); | 391 ASSERT_TRUE(CreateDownloadFile(0, true)); |
540 FilePath initial_path(download_file_->FullPath()); | 392 FilePath initial_path(download_file_->FullPath()); |
541 EXPECT_TRUE(file_util::PathExists(initial_path)); | 393 EXPECT_TRUE(file_util::PathExists(initial_path)); |
542 | 394 |
543 const char* chunks1[] = { kTestData1, kTestData2 }; | 395 const char* chunks1[] = { kTestData1, kTestData2 }; |
544 ::testing::Sequence s1; | 396 ::testing::Sequence s1; |
(...skipping 12 matching lines...) Expand all Loading... |
557 TEST_F(DownloadFileTest, StreamNonEmptyError) { | 409 TEST_F(DownloadFileTest, StreamNonEmptyError) { |
558 ASSERT_TRUE(CreateDownloadFile(0, true)); | 410 ASSERT_TRUE(CreateDownloadFile(0, true)); |
559 FilePath initial_path(download_file_->FullPath()); | 411 FilePath initial_path(download_file_->FullPath()); |
560 EXPECT_TRUE(file_util::PathExists(initial_path)); | 412 EXPECT_TRUE(file_util::PathExists(initial_path)); |
561 | 413 |
562 const char* chunks1[] = { kTestData1, kTestData2 }; | 414 const char* chunks1[] = { kTestData1, kTestData2 }; |
563 ::testing::Sequence s1; | 415 ::testing::Sequence s1; |
564 SetupDataAppend(chunks1, 2, s1); | 416 SetupDataAppend(chunks1, 2, s1); |
565 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, | 417 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, |
566 s1); | 418 s1); |
567 | |
568 EXPECT_CALL(*(download_manager_.get()), | 419 EXPECT_CALL(*(download_manager_.get()), |
569 OnDownloadInterrupted( | 420 OnDownloadInterrupted( |
570 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | 421 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), |
571 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) | 422 strlen(kTestData1) + strlen(kTestData2), _, |
572 .WillOnce(InvokeWithoutArgs( | 423 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)); |
573 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); | |
574 | |
575 // If this next EXPECT_CALL fails flakily, it's probably a real failure. | |
576 // We'll be getting a stream of UpdateDownload calls from the timer, and | |
577 // the last one may have the correct information even if the failure | |
578 // doesn't produce an update, as the timer update may have triggered at the | |
579 // same time. | |
580 EXPECT_CALL(*(download_manager_.get()), | |
581 CurrentUpdateStatus(kDummyDownloadId + 0, | |
582 strlen(kTestData1) + strlen(kTestData2), | |
583 _, _)); | |
584 | |
585 sink_callback_.Run(); | 424 sink_callback_.Run(); |
586 loop_.RunAllPending(); | |
587 VerifyStreamAndSize(); | 425 VerifyStreamAndSize(); |
588 DestroyDownloadFile(0); | 426 DestroyDownloadFile(0); |
589 } | 427 } |
590 | 428 |
591 // Send some data, wait 3/4s of a second, run the message loop, and | 429 // Send some data, wait 3/4s of a second, run the message loop, and |
592 // confirm the values the DownloadManager received are correct. | 430 // confirm the values the DownloadManager received are correct. |
593 TEST_F(DownloadFileTest, ConfirmUpdate) { | 431 TEST_F(DownloadFileTest, ConfirmUpdate) { |
594 CreateDownloadFile(0, true); | 432 CreateDownloadFile(0, true); |
595 | 433 |
596 const char* chunks1[] = { kTestData1, kTestData2 }; | 434 const char* chunks1[] = { kTestData1, kTestData2 }; |
597 AppendDataToFile(chunks1, 2); | 435 AppendDataToFile(chunks1, 2); |
598 | 436 |
599 // Run the message loops for 750ms and check for results. | 437 // Run the message loops for 750ms and check for results. |
600 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), | 438 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
601 base::TimeDelta::FromMilliseconds(750)); | 439 base::TimeDelta::FromMilliseconds(750)); |
602 loop_.Run(); | 440 loop_.Run(); |
603 | 441 |
604 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 442 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
605 bytes_); | 443 bytes_); |
606 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 444 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
607 | 445 |
608 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | 446 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
609 DestroyDownloadFile(0); | 447 DestroyDownloadFile(0); |
610 } | 448 } |
OLD | NEW |