Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(423)

Side by Side Diff: content/browser/download/download_file_unittest.cc

Issue 10689093: Move Rename functionality from DownloadFileManager to DownloadFileImple. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Upload fater merging past revert to figure out if I still have a patch. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
8 #include "content/browser/browser_thread_impl.h" 9 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/download/byte_stream.h" 10 #include "content/browser/download/byte_stream.h"
10 #include "content/browser/download/download_create_info.h" 11 #include "content/browser/download/download_create_info.h"
11 #include "content/browser/download/download_file_impl.h" 12 #include "content/browser/download/download_file_impl.h"
12 #include "content/browser/download/download_request_handle.h" 13 #include "content/browser/download/download_request_handle.h"
13 #include "content/browser/power_save_blocker.h" 14 #include "content/browser/power_save_blocker.h"
14 #include "content/public/browser/download_interrupt_reasons.h" 15 #include "content/public/browser/download_interrupt_reasons.h"
15 #include "content/public/browser/download_manager.h" 16 #include "content/public/browser/download_manager.h"
16 #include "content/public/test/mock_download_manager.h" 17 #include "content/public/test/mock_download_manager.h"
17 #include "net/base/file_stream.h" 18 #include "net/base/file_stream.h"
19 #include "net/base/mock_file_stream.h"
18 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
19 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 23
22 using content::BrowserThread; 24 using content::BrowserThread;
23 using content::BrowserThreadImpl; 25 using content::BrowserThreadImpl;
24 using content::DownloadFile; 26 using content::DownloadFile;
25 using content::DownloadId; 27 using content::DownloadId;
26 using content::DownloadManager; 28 using content::DownloadManager;
27 using ::testing::_; 29 using ::testing::_;
28 using ::testing::AnyNumber; 30 using ::testing::AnyNumber;
29 using ::testing::DoAll; 31 using ::testing::DoAll;
32 using ::testing::InSequence;
30 using ::testing::Return; 33 using ::testing::Return;
31 using ::testing::SetArgPointee; 34 using ::testing::SetArgPointee;
32 using ::testing::StrictMock; 35 using ::testing::StrictMock;
33 36
34 namespace { 37 namespace {
35 38
36 class MockByteStreamReader : public content::ByteStreamReader { 39 class MockByteStreamReader : public content::ByteStreamReader {
37 public: 40 public:
38 MockByteStreamReader() {} 41 MockByteStreamReader() {}
39 ~MockByteStreamReader() {} 42 ~MockByteStreamReader() {}
40 43
41 // ByteStream functions 44 // ByteStream functions
42 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState( 45 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState(
43 scoped_refptr<net::IOBuffer>*, size_t*)); 46 scoped_refptr<net::IOBuffer>*, size_t*));
44 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason()); 47 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason());
45 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); 48 MOCK_METHOD1(RegisterCallback, void(const base::Closure&));
46 }; 49 };
47 50
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
48 } // namespace 59 } // namespace
49 60
50 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; 61 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain";
51 62
52 class DownloadFileTest : public testing::Test { 63 class DownloadFileTest : public testing::Test {
53 public: 64 public:
54 65
55 static const char* kTestData1; 66 static const char* kTestData1;
56 static const char* kTestData2; 67 static const char* kTestData2;
57 static const char* kTestData3; 68 static const char* kTestData3;
58 static const char* kDataHash; 69 static const char* kDataHash;
59 static const int32 kDummyDownloadId; 70 static const int32 kDummyDownloadId;
60 static const int kDummyChildId; 71 static const int kDummyChildId;
61 static const int kDummyRequestId; 72 static const int kDummyRequestId;
62 73
63 // We need a UI |BrowserThread| in order to destruct |download_manager_|, 74 // We need a UI |BrowserThread| in order to destruct |download_manager_|,
64 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, 75 // which has trait |BrowserThread::DeleteOnUIThread|. Without this,
65 // calling Release() on |download_manager_| won't ever result in its 76 // calling Release() on |download_manager_| won't ever result in its
66 // destructor being called and we get a leak. 77 // destructor being called and we get a leak.
67 DownloadFileTest() : 78 DownloadFileTest() :
79 update_download_id_(-1),
80 bytes_(-1),
81 bytes_per_sec_(-1),
82 hash_state_("xyzzy"),
68 ui_thread_(BrowserThread::UI, &loop_), 83 ui_thread_(BrowserThread::UI, &loop_),
69 file_thread_(BrowserThread::FILE, &loop_) { 84 file_thread_(BrowserThread::FILE, &loop_) {
70 } 85 }
71 86
72 ~DownloadFileTest() { 87 ~DownloadFileTest() {
73 } 88 }
74 89
75 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec, 90 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec,
76 const std::string& hash_state) { 91 const std::string& hash_state) {
92 update_download_id_ = id;
77 bytes_ = bytes; 93 bytes_ = bytes;
78 bytes_per_sec_ = bytes_per_sec; 94 bytes_per_sec_ = bytes_per_sec;
79 hash_state_ = hash_state; 95 hash_state_ = hash_state;
80 } 96 }
81 97
98 void ConfirmUpdateDownloadInfo() {
99 download_manager_->CurrentUpdateStatus(
100 update_download_id_, bytes_, bytes_per_sec_, hash_state_);
101 }
102
82 virtual void SetUp() { 103 virtual void SetUp() {
83 download_manager_ = new StrictMock<content::MockDownloadManager>; 104 download_manager_ = new StrictMock<LocalMockDownloadManager>;
84 EXPECT_CALL(*(download_manager_.get()), 105 EXPECT_CALL(*(download_manager_.get()),
85 UpdateDownload( 106 UpdateDownload(
86 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), 107 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
87 _, _, _)) 108 _, _, _))
88 .Times(AnyNumber()) 109 .Times(AnyNumber())
89 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); 110 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo));
90 } 111 }
91 112
92 virtual void TearDown() { 113 virtual void TearDown() {
93 // When a DownloadManager's reference count drops to 0, it is not 114 // When a DownloadManager's reference count drops to 0, it is not
(...skipping 16 matching lines...) Expand all
110 131
111 input_stream_ = new StrictMock<MockByteStreamReader>(); 132 input_stream_ = new StrictMock<MockByteStreamReader>();
112 133
113 // TODO: Need to actually create a function that'll set the variables 134 // TODO: Need to actually create a function that'll set the variables
114 // based on the inputs from the callback. 135 // based on the inputs from the callback.
115 EXPECT_CALL(*input_stream_, RegisterCallback(_)) 136 EXPECT_CALL(*input_stream_, RegisterCallback(_))
116 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) 137 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback))
117 .RetiresOnSaturation(); 138 .RetiresOnSaturation();
118 139
119 DownloadCreateInfo info; 140 DownloadCreateInfo info;
141 // info.request_handle default constructed to null.
120 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); 142 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset);
121 // info.request_handle default constructed to null.
122 info.save_info.file_stream = file_stream_; 143 info.save_info.file_stream = file_stream_;
123 download_file_.reset( 144 download_file_.reset(
124 new DownloadFileImpl( 145 new DownloadFileImpl(
125 &info, 146 &info,
126 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), 147 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(),
127 new DownloadRequestHandle(), 148 new DownloadRequestHandle(),
128 download_manager_, calculate_hash, 149 download_manager_, calculate_hash,
129 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), 150 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(),
130 net::BoundNetLog())); 151 net::BoundNetLog()));
131 152
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 EXPECT_CALL(*(download_manager_.get()), 242 EXPECT_CALL(*(download_manager_.get()),
222 UpdateDownload( 243 UpdateDownload(
223 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), 244 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
224 _, _, _)) 245 _, _, _))
225 .Times(AnyNumber()) 246 .Times(AnyNumber())
226 .WillRepeatedly(Invoke(this, 247 .WillRepeatedly(Invoke(this,
227 &DownloadFileTest::SetUpdateDownloadInfo)); 248 &DownloadFileTest::SetUpdateDownloadInfo));
228 } 249 }
229 } 250 }
230 251
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
231 protected: 272 protected:
232 scoped_refptr<StrictMock<content::MockDownloadManager> > download_manager_; 273 scoped_refptr<StrictMock<LocalMockDownloadManager> > download_manager_;
233 274
234 linked_ptr<net::FileStream> file_stream_; 275 linked_ptr<net::FileStream> file_stream_;
235 276
236 // DownloadFile instance we are testing. 277 // DownloadFile instance we are testing.
237 scoped_ptr<DownloadFile> download_file_; 278 scoped_ptr<DownloadFile> download_file_;
238 279
239 // Stream for sending data into the download file. 280 // Stream for sending data into the download file.
240 // Owned by download_file_; will be alive for lifetime of download_file_. 281 // Owned by download_file_; will be alive for lifetime of download_file_.
241 StrictMock<MockByteStreamReader>* input_stream_; 282 StrictMock<MockByteStreamReader>* input_stream_;
242 283
243 // Sink callback data for stream. 284 // Sink callback data for stream.
244 base::Closure sink_callback_; 285 base::Closure sink_callback_;
245 286
246 // Latest update sent to the download manager. 287 // Latest update sent to the download manager.
288 int32 update_download_id_;
247 int64 bytes_; 289 int64 bytes_;
248 int64 bytes_per_sec_; 290 int64 bytes_per_sec_;
249 std::string hash_state_; 291 std::string hash_state_;
250 292
251 MessageLoop loop_; 293 MessageLoop loop_;
252 294
253 private: 295 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
254 // UI thread. 309 // UI thread.
255 BrowserThreadImpl ui_thread_; 310 BrowserThreadImpl ui_thread_;
256 // File thread to satisfy debug checks in DownloadFile. 311 // File thread to satisfy debug checks in DownloadFile.
257 BrowserThreadImpl file_thread_; 312 BrowserThreadImpl file_thread_;
258 313
259 // Keep track of what data should be saved to the disk file. 314 // Keep track of what data should be saved to the disk file.
260 std::string expected_data_; 315 std::string expected_data_;
261 }; 316 };
262 317
263 const char* DownloadFileTest::kTestData1 = 318 const char* DownloadFileTest::kTestData1 =
(...skipping 10 matching lines...) Expand all
274 // Rename the file before any data is downloaded, after some has, after it all 329 // Rename the file before any data is downloaded, after some has, after it all
275 // has, and after it's closed. 330 // has, and after it's closed.
276 TEST_F(DownloadFileTest, RenameFileFinal) { 331 TEST_F(DownloadFileTest, RenameFileFinal) {
277 ASSERT_TRUE(CreateDownloadFile(0, true)); 332 ASSERT_TRUE(CreateDownloadFile(0, true));
278 FilePath initial_path(download_file_->FullPath()); 333 FilePath initial_path(download_file_->FullPath());
279 EXPECT_TRUE(file_util::PathExists(initial_path)); 334 EXPECT_TRUE(file_util::PathExists(initial_path));
280 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); 335 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1"));
281 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); 336 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2"));
282 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); 337 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3"));
283 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); 338 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4"));
339 FilePath path_5(initial_path.InsertBeforeExtensionASCII("_5"));
340 FilePath output_path;
284 341
285 // Rename the file before downloading any data. 342 // Rename the file before downloading any data.
286 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, 343 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE,
287 download_file_->Rename(path_1)); 344 Rename(path_1, false, &output_path));
288 FilePath renamed_path = download_file_->FullPath(); 345 FilePath renamed_path = download_file_->FullPath();
289 EXPECT_EQ(path_1, renamed_path); 346 EXPECT_EQ(path_1, renamed_path);
347 EXPECT_EQ(path_1, output_path);
290 348
291 // Check the files. 349 // Check the files.
292 EXPECT_FALSE(file_util::PathExists(initial_path)); 350 EXPECT_FALSE(file_util::PathExists(initial_path));
293 EXPECT_TRUE(file_util::PathExists(path_1)); 351 EXPECT_TRUE(file_util::PathExists(path_1));
294 352
295 // Download the data. 353 // Download the data.
296 const char* chunks1[] = { kTestData1, kTestData2 }; 354 const char* chunks1[] = { kTestData1, kTestData2 };
297 AppendDataToFile(chunks1, 2); 355 AppendDataToFile(chunks1, 2);
298 356
299 // Rename the file after downloading some data. 357 // Rename the file after downloading some data.
300 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, 358 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE,
301 download_file_->Rename(path_2)); 359 Rename(path_2, false, &output_path));
302 renamed_path = download_file_->FullPath(); 360 renamed_path = download_file_->FullPath();
303 EXPECT_EQ(path_2, renamed_path); 361 EXPECT_EQ(path_2, renamed_path);
362 EXPECT_EQ(path_2, output_path);
304 363
305 // Check the files. 364 // Check the files.
306 EXPECT_FALSE(file_util::PathExists(path_1)); 365 EXPECT_FALSE(file_util::PathExists(path_1));
307 EXPECT_TRUE(file_util::PathExists(path_2)); 366 EXPECT_TRUE(file_util::PathExists(path_2));
308 367
309 const char* chunks2[] = { kTestData3 }; 368 const char* chunks2[] = { kTestData3 };
310 AppendDataToFile(chunks2, 1); 369 AppendDataToFile(chunks2, 1);
311 370
312 // Rename the file after downloading all the data. 371 // Rename the file after downloading all the data.
313 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, 372 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE,
314 download_file_->Rename(path_3)); 373 Rename(path_3, false, &output_path));
315 renamed_path = download_file_->FullPath(); 374 renamed_path = download_file_->FullPath();
316 EXPECT_EQ(path_3, renamed_path); 375 EXPECT_EQ(path_3, renamed_path);
376 EXPECT_EQ(path_3, output_path);
317 377
318 // Check the files. 378 // Check the files.
319 EXPECT_FALSE(file_util::PathExists(path_2)); 379 EXPECT_FALSE(file_util::PathExists(path_2));
320 EXPECT_TRUE(file_util::PathExists(path_3)); 380 EXPECT_TRUE(file_util::PathExists(path_3));
321 381
322 // Should not be able to get the hash until the file is closed. 382 // Should not be able to get the hash until the file is closed.
323 std::string hash; 383 std::string hash;
324 EXPECT_FALSE(download_file_->GetHash(&hash)); 384 EXPECT_FALSE(download_file_->GetHash(&hash));
325 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); 385 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true);
326 loop_.RunAllPending(); 386 loop_.RunAllPending();
327 387
328 // Rename the file after downloading all the data and closing the file. 388 // Rename the file after downloading all the data and closing the file.
329 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, 389 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE,
330 download_file_->Rename(path_4)); 390 Rename(path_4, false, &output_path));
331 renamed_path = download_file_->FullPath(); 391 renamed_path = download_file_->FullPath();
332 EXPECT_EQ(path_4, renamed_path); 392 EXPECT_EQ(path_4, renamed_path);
393 EXPECT_EQ(path_4, output_path);
333 394
334 // Check the files. 395 // Check the files.
335 EXPECT_FALSE(file_util::PathExists(path_3)); 396 EXPECT_FALSE(file_util::PathExists(path_3));
336 EXPECT_TRUE(file_util::PathExists(path_4)); 397 EXPECT_TRUE(file_util::PathExists(path_4));
337 398
338 // Check the hash. 399 // Check the hash.
339 EXPECT_TRUE(download_file_->GetHash(&hash)); 400 EXPECT_TRUE(download_file_->GetHash(&hash));
340 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); 401 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size()));
341 402
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
342 DestroyDownloadFile(0); 421 DestroyDownloadFile(0);
343 } 422 }
344 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);
479 }
480
345 // Various tests of the StreamActive method. 481 // Various tests of the StreamActive method.
346 TEST_F(DownloadFileTest, StreamEmptySuccess) { 482 TEST_F(DownloadFileTest, StreamEmptySuccess) {
347 ASSERT_TRUE(CreateDownloadFile(0, true)); 483 ASSERT_TRUE(CreateDownloadFile(0, true));
348 FilePath initial_path(download_file_->FullPath()); 484 FilePath initial_path(download_file_->FullPath());
349 EXPECT_TRUE(file_util::PathExists(initial_path)); 485 EXPECT_TRUE(file_util::PathExists(initial_path));
350 486
351 // Test that calling the sink_callback_ on an empty stream shouldn't 487 // Test that calling the sink_callback_ on an empty stream shouldn't
352 // do anything. 488 // do anything.
353 AppendDataToFile(NULL, 0); 489 AppendDataToFile(NULL, 0);
354 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); 490 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get());
(...skipping 18 matching lines...) Expand all
373 TEST_F(DownloadFileTest, StreamEmptyError) { 509 TEST_F(DownloadFileTest, StreamEmptyError) {
374 ASSERT_TRUE(CreateDownloadFile(0, true)); 510 ASSERT_TRUE(CreateDownloadFile(0, true));
375 FilePath initial_path(download_file_->FullPath()); 511 FilePath initial_path(download_file_->FullPath());
376 EXPECT_TRUE(file_util::PathExists(initial_path)); 512 EXPECT_TRUE(file_util::PathExists(initial_path));
377 513
378 // Finish the download in error and make sure we see it on the 514 // Finish the download in error and make sure we see it on the
379 // DownloadManager. 515 // DownloadManager.
380 EXPECT_CALL(*(download_manager_.get()), 516 EXPECT_CALL(*(download_manager_.get()),
381 OnDownloadInterrupted( 517 OnDownloadInterrupted(
382 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), 518 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
383 0, _, 519 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))
384 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)); 520 .WillOnce(InvokeWithoutArgs(
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
385 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); 531 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false);
386 532
533 loop_.RunAllPending();
534
387 DestroyDownloadFile(0); 535 DestroyDownloadFile(0);
388 } 536 }
389 537
390 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { 538 TEST_F(DownloadFileTest, StreamNonEmptySuccess) {
391 ASSERT_TRUE(CreateDownloadFile(0, true)); 539 ASSERT_TRUE(CreateDownloadFile(0, true));
392 FilePath initial_path(download_file_->FullPath()); 540 FilePath initial_path(download_file_->FullPath());
393 EXPECT_TRUE(file_util::PathExists(initial_path)); 541 EXPECT_TRUE(file_util::PathExists(initial_path));
394 542
395 const char* chunks1[] = { kTestData1, kTestData2 }; 543 const char* chunks1[] = { kTestData1, kTestData2 };
396 ::testing::Sequence s1; 544 ::testing::Sequence s1;
(...skipping 12 matching lines...) Expand all
409 TEST_F(DownloadFileTest, StreamNonEmptyError) { 557 TEST_F(DownloadFileTest, StreamNonEmptyError) {
410 ASSERT_TRUE(CreateDownloadFile(0, true)); 558 ASSERT_TRUE(CreateDownloadFile(0, true));
411 FilePath initial_path(download_file_->FullPath()); 559 FilePath initial_path(download_file_->FullPath());
412 EXPECT_TRUE(file_util::PathExists(initial_path)); 560 EXPECT_TRUE(file_util::PathExists(initial_path));
413 561
414 const char* chunks1[] = { kTestData1, kTestData2 }; 562 const char* chunks1[] = { kTestData1, kTestData2 };
415 ::testing::Sequence s1; 563 ::testing::Sequence s1;
416 SetupDataAppend(chunks1, 2, s1); 564 SetupDataAppend(chunks1, 2, s1);
417 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, 565 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED,
418 s1); 566 s1);
567
419 EXPECT_CALL(*(download_manager_.get()), 568 EXPECT_CALL(*(download_manager_.get()),
420 OnDownloadInterrupted( 569 OnDownloadInterrupted(
421 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), 570 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
422 strlen(kTestData1) + strlen(kTestData2), _, 571 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))
423 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)); 572 .WillOnce(InvokeWithoutArgs(
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
424 sink_callback_.Run(); 585 sink_callback_.Run();
586 loop_.RunAllPending();
425 VerifyStreamAndSize(); 587 VerifyStreamAndSize();
426 DestroyDownloadFile(0); 588 DestroyDownloadFile(0);
427 } 589 }
428 590
429 // Send some data, wait 3/4s of a second, run the message loop, and 591 // Send some data, wait 3/4s of a second, run the message loop, and
430 // confirm the values the DownloadManager received are correct. 592 // confirm the values the DownloadManager received are correct.
431 TEST_F(DownloadFileTest, ConfirmUpdate) { 593 TEST_F(DownloadFileTest, ConfirmUpdate) {
432 CreateDownloadFile(0, true); 594 CreateDownloadFile(0, true);
433 595
434 const char* chunks1[] = { kTestData1, kTestData2 }; 596 const char* chunks1[] = { kTestData1, kTestData2 };
435 AppendDataToFile(chunks1, 2); 597 AppendDataToFile(chunks1, 2);
436 598
437 // Run the message loops for 750ms and check for results. 599 // Run the message loops for 750ms and check for results.
438 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 600 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(),
439 base::TimeDelta::FromMilliseconds(750)); 601 base::TimeDelta::FromMilliseconds(750));
440 loop_.Run(); 602 loop_.Run();
441 603
442 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), 604 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)),
443 bytes_); 605 bytes_);
444 EXPECT_EQ(download_file_->GetHashState(), hash_state_); 606 EXPECT_EQ(download_file_->GetHashState(), hash_state_);
445 607
446 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); 608 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true);
447 DestroyDownloadFile(0); 609 DestroyDownloadFile(0);
448 } 610 }
OLDNEW
« no previous file with comments | « content/browser/download/download_file_manager_unittest.cc ('k') | content/browser/download/download_item_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698