Chromium Code Reviews
[email protected] (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

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

Issue 14947007: [Downloads] Allow acquiring dangerous download file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix UMA and support stealing interrupted downloads Created 7 years, 7 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/callback.h"
5 #include "base/command_line.h" 6 #include "base/command_line.h"
6 #include "base/message_loop.h" 7 #include "base/message_loop.h"
7 #include "base/stl_util.h" 8 #include "base/stl_util.h"
8 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
9 #include "content/browser/byte_stream.h" 10 #include "content/browser/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_factory.h" 12 #include "content/browser/download/download_file_factory.h"
12 #include "content/browser/download/download_item_impl.h" 13 #include "content/browser/download/download_item_impl.h"
13 #include "content/browser/download/download_item_impl_delegate.h" 14 #include "content/browser/download/download_item_impl_delegate.h"
14 #include "content/browser/download/download_request_handle.h" 15 #include "content/browser/download/download_request_handle.h"
15 #include "content/browser/download/mock_download_file.h" 16 #include "content/browser/download/mock_download_file.h"
17 #include "content/public/browser/download_destination_observer.h"
16 #include "content/public/browser/download_id.h" 18 #include "content/public/browser/download_id.h"
17 #include "content/public/browser/download_destination_observer.h"
18 #include "content/public/browser/download_interrupt_reasons.h" 19 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_url_parameters.h" 20 #include "content/public/browser/download_url_parameters.h"
20 #include "content/public/common/content_switches.h" 21 #include "content/public/common/content_switches.h"
21 #include "content/public/test/mock_download_item.h" 22 #include "content/public/test/mock_download_item.h"
22 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 using ::testing::_; 27 using ::testing::_;
27 using ::testing::NiceMock; 28 using ::testing::NiceMock;
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 .WillRepeatedly(Return(false)); 272 .WillRepeatedly(Return(false));
272 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) 273 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _))
273 .WillRepeatedly(Return(true)); 274 .WillRepeatedly(Return(true));
274 275
275 return mock_download_file; 276 return mock_download_file;
276 } 277 }
277 278
278 // Perform the intermediate rename for |item|. The target path for the 279 // Perform the intermediate rename for |item|. The target path for the
279 // download will be set to kDummyPath. Returns the MockDownloadFile* that was 280 // download will be set to kDummyPath. Returns the MockDownloadFile* that was
280 // added to the DownloadItem. 281 // added to the DownloadItem.
281 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item) { 282 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item,
283 DownloadDangerType danger_type) {
282 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 284 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
283 EXPECT_TRUE(item->GetTargetFilePath().empty()); 285 EXPECT_TRUE(item->GetTargetFilePath().empty());
284 DownloadItemImplDelegate::DownloadTargetCallback callback; 286 DownloadItemImplDelegate::DownloadTargetCallback callback;
285 MockDownloadFile* download_file = 287 MockDownloadFile* download_file =
286 AddDownloadFileToDownloadItem(item, &callback); 288 AddDownloadFileToDownloadItem(item, &callback);
287 base::FilePath target_path(kDummyPath); 289 base::FilePath target_path(kDummyPath);
288 base::FilePath intermediate_path( 290 base::FilePath intermediate_path(
289 target_path.InsertBeforeExtensionASCII("x")); 291 target_path.InsertBeforeExtensionASCII("x"));
290 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 292 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
291 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 293 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
292 intermediate_path)); 294 intermediate_path));
293 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 295 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
294 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 296 danger_type, intermediate_path);
295 RunAllPendingInMessageLoops(); 297 RunAllPendingInMessageLoops();
296 return download_file; 298 return download_file;
297 } 299 }
298 300
299 // Cleanup a download item (specifically get rid of the DownloadFile on it). 301 // Cleanup a download item (specifically get rid of the DownloadFile on it).
300 // The item must be in the expected state. 302 // The item must be in the expected state.
301 void CleanupItem(DownloadItemImpl* item, 303 void CleanupItem(DownloadItemImpl* item,
302 MockDownloadFile* download_file, 304 MockDownloadFile* download_file,
303 DownloadItem::DownloadState expected_state) { 305 DownloadItem::DownloadState expected_state) {
304 EXPECT_EQ(expected_state, item->GetState()); 306 EXPECT_EQ(expected_state, item->GetState());
(...skipping 12 matching lines...) Expand all
317 } 319 }
318 320
319 void RunAllPendingInMessageLoops() { 321 void RunAllPendingInMessageLoops() {
320 loop_.RunUntilIdle(); 322 loop_.RunUntilIdle();
321 } 323 }
322 324
323 MockDelegate* mock_delegate() { 325 MockDelegate* mock_delegate() {
324 return &delegate_; 326 return &delegate_;
325 } 327 }
326 328
329 void OnDownloadFileAcquired(base::FilePath* return_path,
330 const base::FilePath& path) {
331 *return_path = path;
332 }
333
327 private: 334 private:
328 base::MessageLoopForUI loop_; 335 base::MessageLoopForUI loop_;
329 TestBrowserThread ui_thread_; // UI thread 336 TestBrowserThread ui_thread_; // UI thread
330 TestBrowserThread file_thread_; // FILE thread 337 TestBrowserThread file_thread_; // FILE thread
331 StrictMock<MockDelegate> delegate_; 338 StrictMock<MockDelegate> delegate_;
332 std::set<DownloadItem*> allocated_downloads_; 339 std::set<DownloadItem*> allocated_downloads_;
333 }; 340 };
334 341
335 // Tests to ensure calls that change a DownloadItem generate an update to 342 // Tests to ensure calls that change a DownloadItem generate an update to
336 // observers. 343 // observers.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) { 389 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) {
383 DownloadItemImpl* item = CreateDownloadItem(); 390 DownloadItemImpl* item = CreateDownloadItem();
384 MockObserver observer(item); 391 MockObserver observer(item);
385 392
386 item->OnDownloadedFileRemoved(); 393 item->OnDownloadedFileRemoved();
387 ASSERT_TRUE(observer.CheckUpdated()); 394 ASSERT_TRUE(observer.CheckUpdated());
388 } 395 }
389 396
390 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { 397 TEST_F(DownloadItemTest, NotificationAfterInterrupted) {
391 DownloadItemImpl* item = CreateDownloadItem(); 398 DownloadItemImpl* item = CreateDownloadItem();
392 MockDownloadFile* download_file = DoIntermediateRename(item); 399 MockDownloadFile* download_file =
400 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
393 EXPECT_CALL(*download_file, Cancel()); 401 EXPECT_CALL(*download_file, Cancel());
394 MockObserver observer(item); 402 MockObserver observer(item);
395 403
396 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) 404 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_))
397 .Times(0); 405 .Times(0);
398 406
399 item->DestinationObserverAsWeakPtr()->DestinationError( 407 item->DestinationObserverAsWeakPtr()->DestinationError(
400 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 408 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
401 ASSERT_TRUE(observer.CheckUpdated()); 409 ASSERT_TRUE(observer.CheckUpdated());
402 } 410 }
403 411
404 TEST_F(DownloadItemTest, NotificationAfterDelete) {
405 DownloadItemImpl* item = CreateDownloadItem();
406 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
407 EXPECT_CALL(*download_file, Cancel());
408 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
409 MockObserver observer(item);
410
411 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
412 ASSERT_TRUE(observer.CheckUpdated());
413 }
414
415 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { 412 TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
416 DownloadItemImpl* item = CreateDownloadItem(); 413 DownloadItemImpl* item = CreateDownloadItem();
417 MockObserver observer(item); 414 MockObserver observer(item);
418 415
419 DestroyDownloadItem(item); 416 DestroyDownloadItem(item);
420 ASSERT_TRUE(observer.CheckDestroyed()); 417 ASSERT_TRUE(observer.CheckDestroyed());
421 } 418 }
422 419
423 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { 420 TEST_F(DownloadItemTest, ContinueAfterInterrupted) {
424 CommandLine::ForCurrentProcess()->AppendSwitch( 421 CommandLine::ForCurrentProcess()->AppendSwitch(
425 switches::kEnableDownloadResumption); 422 switches::kEnableDownloadResumption);
426 423
427 DownloadItemImpl* item = CreateDownloadItem(); 424 DownloadItemImpl* item = CreateDownloadItem();
428 MockObserver observer(item); 425 MockObserver observer(item);
429 DownloadItemImplDelegate::DownloadTargetCallback callback; 426 DownloadItemImplDelegate::DownloadTargetCallback callback;
430 MockDownloadFile* download_file = DoIntermediateRename(item); 427 MockDownloadFile* download_file =
428 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
431 429
432 // Interrupt the download, using a continuable interrupt. 430 // Interrupt the download, using a continuable interrupt.
431 EXPECT_CALL(*download_file, FullPath())
432 .WillOnce(Return(base::FilePath()));
433 EXPECT_CALL(*download_file, Detach()); 433 EXPECT_CALL(*download_file, Detach());
434 item->DestinationObserverAsWeakPtr()->DestinationError( 434 item->DestinationObserverAsWeakPtr()->DestinationError(
435 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 435 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
436 ASSERT_TRUE(observer.CheckUpdated()); 436 ASSERT_TRUE(observer.CheckUpdated());
437 // Should attempt to auto-resume. Because we don't have a mock WebContents, 437 // Should attempt to auto-resume. Because we don't have a mock WebContents,
438 // ResumeInterruptedDownload() will abort early, with another interrupt, 438 // ResumeInterruptedDownload() will abort early, with another interrupt,
439 // which will be ignored. 439 // which will be ignored.
440 ASSERT_EQ(1, observer.GetInterruptCount()); 440 ASSERT_EQ(1, observer.GetInterruptCount());
441 ASSERT_EQ(0, observer.GetResumeCount()); 441 ASSERT_EQ(0, observer.GetResumeCount());
442 RunAllPendingInMessageLoops(); 442 RunAllPendingInMessageLoops();
443 443
444 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); 444 CleanupItem(item, download_file, DownloadItem::INTERRUPTED);
445 } 445 }
446 446
447 // Same as above, but with a non-continuable interrupt. 447 // Same as above, but with a non-continuable interrupt.
448 TEST_F(DownloadItemTest, RestartAfterInterrupted) { 448 TEST_F(DownloadItemTest, RestartAfterInterrupted) {
449 CommandLine::ForCurrentProcess()->AppendSwitch( 449 CommandLine::ForCurrentProcess()->AppendSwitch(
450 switches::kEnableDownloadResumption); 450 switches::kEnableDownloadResumption);
451 451
452 DownloadItemImpl* item = CreateDownloadItem(); 452 DownloadItemImpl* item = CreateDownloadItem();
453 MockObserver observer(item); 453 MockObserver observer(item);
454 DownloadItemImplDelegate::DownloadTargetCallback callback; 454 DownloadItemImplDelegate::DownloadTargetCallback callback;
455 MockDownloadFile* download_file = DoIntermediateRename(item); 455 MockDownloadFile* download_file =
456 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
456 457
457 // Interrupt the download, using a restartable interrupt. 458 // Interrupt the download, using a restartable interrupt.
458 EXPECT_CALL(*download_file, Cancel()); 459 EXPECT_CALL(*download_file, Cancel());
459 item->DestinationObserverAsWeakPtr()->DestinationError( 460 item->DestinationObserverAsWeakPtr()->DestinationError(
460 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 461 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
461 ASSERT_TRUE(observer.CheckUpdated()); 462 ASSERT_TRUE(observer.CheckUpdated());
462 // Should not try to auto-resume. 463 // Should not try to auto-resume.
463 ASSERT_EQ(1, observer.GetInterruptCount()); 464 ASSERT_EQ(1, observer.GetInterruptCount());
464 ASSERT_EQ(0, observer.GetResumeCount()); 465 ASSERT_EQ(0, observer.GetResumeCount());
465 RunAllPendingInMessageLoops(); 466 RunAllPendingInMessageLoops();
(...skipping 18 matching lines...) Expand all
484 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 485 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
485 .WillRepeatedly(SaveArg<1>(&callback)); 486 .WillRepeatedly(SaveArg<1>(&callback));
486 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { 487 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) {
487 DVLOG(20) << "Loop iteration " << i; 488 DVLOG(20) << "Loop iteration " << i;
488 489
489 mock_download_file = new NiceMock<MockDownloadFile>; 490 mock_download_file = new NiceMock<MockDownloadFile>;
490 download_file.reset(mock_download_file); 491 download_file.reset(mock_download_file);
491 mock_request_handle = new NiceMock<MockRequestHandle>; 492 mock_request_handle = new NiceMock<MockRequestHandle>;
492 request_handle.reset(mock_request_handle); 493 request_handle.reset(mock_request_handle);
493 494
495 ON_CALL(*mock_download_file, FullPath())
496 .WillByDefault(Return(base::FilePath()));
497
494 // It's too complicated to set up a WebContents instance that would cause 498 // It's too complicated to set up a WebContents instance that would cause
495 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function 499 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function
496 // to be callled, so we simply verify that GetWebContents() is called. 500 // to be callled, so we simply verify that GetWebContents() is called.
497 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { 501 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) {
498 EXPECT_CALL(*mock_request_handle, GetWebContents()) 502 EXPECT_CALL(*mock_request_handle, GetWebContents())
499 .WillOnce(Return(static_cast<WebContents*>(NULL))); 503 .WillOnce(Return(static_cast<WebContents*>(NULL)));
500 } 504 }
501 505
502 // Copied key parts of DoIntermediateRename & AddDownloadFileToDownloadItem 506 // Copied key parts of DoIntermediateRename & AddDownloadFileToDownloadItem
503 // to allow for holding onto the request handle. 507 // to allow for holding onto the request handle.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 // Setting to unsafe url or unsafe file should trigger a notification. 557 // Setting to unsafe url or unsafe file should trigger a notification.
554 DownloadItemImpl* unsafeurl_item = 558 DownloadItemImpl* unsafeurl_item =
555 CreateDownloadItem(); 559 CreateDownloadItem();
556 MockObserver unsafeurl_observer(unsafeurl_item); 560 MockObserver unsafeurl_observer(unsafeurl_item);
557 561
558 unsafeurl_item->OnAllDataSaved(std::string()); 562 unsafeurl_item->OnAllDataSaved(std::string());
559 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 563 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
560 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); 564 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL);
561 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 565 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
562 566
563 unsafeurl_item->DangerousDownloadValidated(); 567 unsafeurl_item->ValidateDangerousDownload();
564 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 568 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
565 569
566 DownloadItemImpl* unsafefile_item = 570 DownloadItemImpl* unsafefile_item =
567 CreateDownloadItem(); 571 CreateDownloadItem();
568 MockObserver unsafefile_observer(unsafefile_item); 572 MockObserver unsafefile_observer(unsafefile_item);
569 573
570 unsafefile_item->OnAllDataSaved(std::string()); 574 unsafefile_item->OnAllDataSaved(std::string());
571 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 575 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
572 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 576 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
573 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 577 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
574 578
575 unsafefile_item->DangerousDownloadValidated(); 579 unsafefile_item->ValidateDangerousDownload();
576 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 580 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
577 } 581 }
578 582
579 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run 583 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run
580 // DownloadFile::Rename(). Once the rename 584 // DownloadFile::Rename(). Once the rename
581 // completes, DownloadItemImpl receives a notification with the new file 585 // completes, DownloadItemImpl receives a notification with the new file
582 // name. Check that observers are updated when the new filename is available and 586 // name. Check that observers are updated when the new filename is available and
583 // not before. 587 // not before.
584 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { 588 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) {
585 DownloadItemImpl* item = CreateDownloadItem(); 589 DownloadItemImpl* item = CreateDownloadItem();
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 RunAllPendingInMessageLoops(); 693 RunAllPendingInMessageLoops();
690 // All the callbacks should have happened by now. 694 // All the callbacks should have happened by now.
691 ::testing::Mock::VerifyAndClearExpectations(download_file); 695 ::testing::Mock::VerifyAndClearExpectations(download_file);
692 mock_delegate()->VerifyAndClearExpectations(); 696 mock_delegate()->VerifyAndClearExpectations();
693 697
694 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 698 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
695 .WillOnce(Return(true)); 699 .WillOnce(Return(true));
696 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) 700 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _))
697 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 701 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
698 final_path)); 702 final_path));
703 EXPECT_CALL(*download_file, FullPath())
704 .WillOnce(Return(base::FilePath()));
699 EXPECT_CALL(*download_file, Detach()); 705 EXPECT_CALL(*download_file, Detach());
700 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 706 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
701 RunAllPendingInMessageLoops(); 707 RunAllPendingInMessageLoops();
702 ::testing::Mock::VerifyAndClearExpectations(download_file); 708 ::testing::Mock::VerifyAndClearExpectations(download_file);
703 mock_delegate()->VerifyAndClearExpectations(); 709 mock_delegate()->VerifyAndClearExpectations();
704 } 710 }
705 711
706 // Test that the delegate is invoked after the download file is renamed and the 712 // Test that the delegate is invoked after the download file is renamed and the
707 // download item is in an interrupted state. 713 // download item is in an interrupted state.
708 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { 714 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) {
(...skipping 14 matching lines...) Expand all
723 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 729 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
724 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 730 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
725 RunAllPendingInMessageLoops(); 731 RunAllPendingInMessageLoops();
726 // All the callbacks should have happened by now. 732 // All the callbacks should have happened by now.
727 ::testing::Mock::VerifyAndClearExpectations(download_file); 733 ::testing::Mock::VerifyAndClearExpectations(download_file);
728 mock_delegate()->VerifyAndClearExpectations(); 734 mock_delegate()->VerifyAndClearExpectations();
729 } 735 }
730 736
731 TEST_F(DownloadItemTest, Interrupted) { 737 TEST_F(DownloadItemTest, Interrupted) {
732 DownloadItemImpl* item = CreateDownloadItem(); 738 DownloadItemImpl* item = CreateDownloadItem();
733 MockDownloadFile* download_file = DoIntermediateRename(item); 739 MockDownloadFile* download_file =
740 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
734 741
735 const DownloadInterruptReason reason( 742 const DownloadInterruptReason reason(
736 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 743 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
737 744
738 // Confirm interrupt sets state properly. 745 // Confirm interrupt sets state properly.
739 EXPECT_CALL(*download_file, Cancel()); 746 EXPECT_CALL(*download_file, Cancel());
740 item->DestinationObserverAsWeakPtr()->DestinationError(reason); 747 item->DestinationObserverAsWeakPtr()->DestinationError(reason);
741 RunAllPendingInMessageLoops(); 748 RunAllPendingInMessageLoops();
742 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 749 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
743 EXPECT_EQ(reason, item->GetLastReason()); 750 EXPECT_EQ(reason, item->GetLastReason());
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 as_observer->DestinationUpdate(200, 20, "livebeef"); 829 as_observer->DestinationUpdate(200, 20, "livebeef");
823 EXPECT_EQ(20l, item->CurrentSpeed()); 830 EXPECT_EQ(20l, item->CurrentSpeed());
824 EXPECT_EQ("livebeef", item->GetHashState()); 831 EXPECT_EQ("livebeef", item->GetHashState());
825 EXPECT_EQ(200l, item->GetReceivedBytes()); 832 EXPECT_EQ(200l, item->GetReceivedBytes());
826 EXPECT_EQ(0l, item->GetTotalBytes()); 833 EXPECT_EQ(0l, item->GetTotalBytes());
827 EXPECT_TRUE(observer.CheckUpdated()); 834 EXPECT_TRUE(observer.CheckUpdated());
828 } 835 }
829 836
830 TEST_F(DownloadItemTest, DestinationError) { 837 TEST_F(DownloadItemTest, DestinationError) {
831 DownloadItemImpl* item = CreateDownloadItem(); 838 DownloadItemImpl* item = CreateDownloadItem();
832 MockDownloadFile* download_file = DoIntermediateRename(item); 839 MockDownloadFile* download_file =
840 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
833 base::WeakPtr<DownloadDestinationObserver> as_observer( 841 base::WeakPtr<DownloadDestinationObserver> as_observer(
834 item->DestinationObserverAsWeakPtr()); 842 item->DestinationObserverAsWeakPtr());
835 MockObserver observer(item); 843 MockObserver observer(item);
836 844
837 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 845 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
838 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); 846 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason());
839 EXPECT_FALSE(observer.CheckUpdated()); 847 EXPECT_FALSE(observer.CheckUpdated());
840 848
841 EXPECT_CALL(*download_file, Cancel()); 849 EXPECT_CALL(*download_file, Cancel());
842 as_observer->DestinationError( 850 as_observer->DestinationError(
(...skipping 29 matching lines...) Expand all
872 mock_delegate()->VerifyAndClearExpectations(); 880 mock_delegate()->VerifyAndClearExpectations();
873 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 881 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
874 EXPECT_TRUE(observer.CheckUpdated()); 882 EXPECT_TRUE(observer.CheckUpdated());
875 EXPECT_EQ("livebeef", item->GetHash()); 883 EXPECT_EQ("livebeef", item->GetHash());
876 EXPECT_EQ("", item->GetHashState()); 884 EXPECT_EQ("", item->GetHashState());
877 EXPECT_TRUE(item->AllDataSaved()); 885 EXPECT_TRUE(item->AllDataSaved());
878 } 886 }
879 887
880 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { 888 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) {
881 DownloadItemImpl* item = CreateDownloadItem(); 889 DownloadItemImpl* item = CreateDownloadItem();
882 MockDownloadFile* download_file = DoIntermediateRename(item); 890 MockDownloadFile* download_file =
891 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
883 892
884 // InProgress 893 // InProgress
885 ASSERT_TRUE(item->IsInProgress()); 894 ASSERT_TRUE(item->IsInProgress());
886 ASSERT_FALSE(item->GetTargetFilePath().empty()); 895 ASSERT_FALSE(item->GetTargetFilePath().empty());
887 EXPECT_TRUE(item->CanShowInFolder()); 896 EXPECT_TRUE(item->CanShowInFolder());
888 EXPECT_TRUE(item->CanOpenDownload()); 897 EXPECT_TRUE(item->CanOpenDownload());
889 898
890 // Complete 899 // Complete
891 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) 900 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _))
892 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 901 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
893 base::FilePath(kDummyPath))); 902 base::FilePath(kDummyPath)));
894 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 903 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
895 .WillOnce(Return(true)); 904 .WillOnce(Return(true));
905 EXPECT_CALL(*download_file, FullPath())
906 .WillOnce(Return(base::FilePath()));
896 EXPECT_CALL(*download_file, Detach()); 907 EXPECT_CALL(*download_file, Detach());
897 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 908 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
898 RunAllPendingInMessageLoops(); 909 RunAllPendingInMessageLoops();
899 910
900 ASSERT_TRUE(item->IsComplete()); 911 ASSERT_TRUE(item->IsComplete());
901 EXPECT_TRUE(item->CanShowInFolder()); 912 EXPECT_TRUE(item->CanShowInFolder());
902 EXPECT_TRUE(item->CanOpenDownload()); 913 EXPECT_TRUE(item->CanOpenDownload());
903 } 914 }
904 915
905 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { 916 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) {
906 DownloadItemImpl* item = CreateDownloadItem(); 917 DownloadItemImpl* item = CreateDownloadItem();
907 MockDownloadFile* download_file = DoIntermediateRename(item); 918 MockDownloadFile* download_file =
919 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
908 item->SetIsTemporary(true); 920 item->SetIsTemporary(true);
909 921
910 // InProgress Temporary 922 // InProgress Temporary
911 ASSERT_TRUE(item->IsInProgress()); 923 ASSERT_TRUE(item->IsInProgress());
912 ASSERT_FALSE(item->GetTargetFilePath().empty()); 924 ASSERT_FALSE(item->GetTargetFilePath().empty());
913 ASSERT_TRUE(item->IsTemporary()); 925 ASSERT_TRUE(item->IsTemporary());
914 EXPECT_FALSE(item->CanShowInFolder()); 926 EXPECT_FALSE(item->CanShowInFolder());
915 EXPECT_FALSE(item->CanOpenDownload()); 927 EXPECT_FALSE(item->CanOpenDownload());
916 928
917 // Complete Temporary 929 // Complete Temporary
918 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 930 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
919 .WillOnce(Return(true)); 931 .WillOnce(Return(true));
920 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) 932 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _))
921 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 933 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
922 base::FilePath(kDummyPath))); 934 base::FilePath(kDummyPath)));
935 EXPECT_CALL(*download_file, FullPath())
936 .WillOnce(Return(base::FilePath()));
923 EXPECT_CALL(*download_file, Detach()); 937 EXPECT_CALL(*download_file, Detach());
924 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 938 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
925 RunAllPendingInMessageLoops(); 939 RunAllPendingInMessageLoops();
926 940
927 ASSERT_TRUE(item->IsComplete()); 941 ASSERT_TRUE(item->IsComplete());
928 EXPECT_FALSE(item->CanShowInFolder()); 942 EXPECT_FALSE(item->CanShowInFolder());
929 EXPECT_FALSE(item->CanOpenDownload()); 943 EXPECT_FALSE(item->CanOpenDownload());
930 } 944 }
931 945
932 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { 946 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) {
933 DownloadItemImpl* item = CreateDownloadItem(); 947 DownloadItemImpl* item = CreateDownloadItem();
934 MockDownloadFile* download_file = DoIntermediateRename(item); 948 MockDownloadFile* download_file =
949 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
935 950
936 EXPECT_CALL(*download_file, Cancel()); 951 EXPECT_CALL(*download_file, Cancel());
937 item->DestinationObserverAsWeakPtr()->DestinationError( 952 item->DestinationObserverAsWeakPtr()->DestinationError(
938 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 953 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
939 RunAllPendingInMessageLoops(); 954 RunAllPendingInMessageLoops();
940 955
941 ASSERT_TRUE(item->IsInterrupted()); 956 ASSERT_TRUE(item->IsInterrupted());
942 ASSERT_FALSE(item->GetTargetFilePath().empty()); 957 ASSERT_FALSE(item->GetTargetFilePath().empty());
943 EXPECT_FALSE(item->CanShowInFolder()); 958 EXPECT_FALSE(item->CanShowInFolder());
944 EXPECT_FALSE(item->CanOpenDownload()); 959 EXPECT_FALSE(item->CanOpenDownload());
945 } 960 }
946 961
947 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { 962 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) {
948 DownloadItemImpl* item = CreateDownloadItem(); 963 DownloadItemImpl* item = CreateDownloadItem();
949 MockDownloadFile* download_file = DoIntermediateRename(item); 964 MockDownloadFile* download_file =
965 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
950 966
951 EXPECT_CALL(*download_file, Cancel()); 967 EXPECT_CALL(*download_file, Cancel());
952 item->Cancel(true); 968 item->Cancel(true);
953 RunAllPendingInMessageLoops(); 969 RunAllPendingInMessageLoops();
954 970
955 ASSERT_TRUE(item->IsCancelled()); 971 ASSERT_TRUE(item->IsCancelled());
956 EXPECT_FALSE(item->CanShowInFolder()); 972 EXPECT_FALSE(item->CanShowInFolder());
957 EXPECT_FALSE(item->CanOpenDownload()); 973 EXPECT_FALSE(item->CanOpenDownload());
958 } 974 }
959 975
960 // Test various aspects of the delegate completion blocker. 976 // Test various aspects of the delegate completion blocker.
961 977
962 // Just allowing completion. 978 // Just allowing completion.
963 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { 979 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) {
964 // Test to confirm that if we have a callback that returns true, 980 // Test to confirm that if we have a callback that returns true,
965 // we complete immediately. 981 // we complete immediately.
966 DownloadItemImpl* item = CreateDownloadItem(); 982 DownloadItemImpl* item = CreateDownloadItem();
967 MockDownloadFile* download_file = DoIntermediateRename(item); 983 MockDownloadFile* download_file =
984 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
968 985
969 // Drive the delegate interaction. 986 // Drive the delegate interaction.
970 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 987 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
971 .WillOnce(Return(true)); 988 .WillOnce(Return(true));
972 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 989 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
973 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 990 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
974 EXPECT_FALSE(item->IsDangerous()); 991 EXPECT_FALSE(item->IsDangerous());
975 992
976 // Make sure the download can complete. 993 // Make sure the download can complete.
977 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) 994 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _))
978 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 995 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
979 base::FilePath(kDummyPath))); 996 base::FilePath(kDummyPath)));
980 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 997 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
981 .WillOnce(Return(true)); 998 .WillOnce(Return(true));
999 EXPECT_CALL(*download_file, FullPath())
1000 .WillOnce(Return(base::FilePath()));
982 EXPECT_CALL(*download_file, Detach()); 1001 EXPECT_CALL(*download_file, Detach());
983 RunAllPendingInMessageLoops(); 1002 RunAllPendingInMessageLoops();
984 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1003 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
985 } 1004 }
986 1005
987 // Just delaying completion. 1006 // Just delaying completion.
988 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { 1007 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) {
989 // Test to confirm that if we have a callback that returns true, 1008 // Test to confirm that if we have a callback that returns true,
990 // we complete immediately. 1009 // we complete immediately.
991 DownloadItemImpl* item = CreateDownloadItem(); 1010 DownloadItemImpl* item = CreateDownloadItem();
992 MockDownloadFile* download_file = DoIntermediateRename(item); 1011 MockDownloadFile* download_file =
1012 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
993 1013
994 // Drive the delegate interaction. 1014 // Drive the delegate interaction.
995 base::Closure delegate_callback; 1015 base::Closure delegate_callback;
996 base::Closure copy_delegate_callback; 1016 base::Closure copy_delegate_callback;
997 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1017 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
998 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1018 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
999 Return(false))) 1019 Return(false)))
1000 .WillOnce(Return(true)); 1020 .WillOnce(Return(true));
1001 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1021 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
1002 ASSERT_FALSE(delegate_callback.is_null()); 1022 ASSERT_FALSE(delegate_callback.is_null());
1003 copy_delegate_callback = delegate_callback; 1023 copy_delegate_callback = delegate_callback;
1004 delegate_callback.Reset(); 1024 delegate_callback.Reset();
1005 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1025 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1006 copy_delegate_callback.Run(); 1026 copy_delegate_callback.Run();
1007 ASSERT_TRUE(delegate_callback.is_null()); 1027 ASSERT_TRUE(delegate_callback.is_null());
1008 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1028 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1009 EXPECT_FALSE(item->IsDangerous()); 1029 EXPECT_FALSE(item->IsDangerous());
1010 1030
1011 // Make sure the download can complete. 1031 // Make sure the download can complete.
1012 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) 1032 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _))
1013 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1033 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
1014 base::FilePath(kDummyPath))); 1034 base::FilePath(kDummyPath)));
1015 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1035 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1016 .WillOnce(Return(true)); 1036 .WillOnce(Return(true));
1037 EXPECT_CALL(*download_file, FullPath())
1038 .WillOnce(Return(base::FilePath()));
1017 EXPECT_CALL(*download_file, Detach()); 1039 EXPECT_CALL(*download_file, Detach());
1018 RunAllPendingInMessageLoops(); 1040 RunAllPendingInMessageLoops();
1019 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1041 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1020 } 1042 }
1021 1043
1022 // Delay and set danger. 1044 // Delay and set danger.
1023 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { 1045 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) {
1024 // Test to confirm that if we have a callback that returns true, 1046 // Test to confirm that if we have a callback that returns true,
1025 // we complete immediately. 1047 // we complete immediately.
1026 DownloadItemImpl* item = CreateDownloadItem(); 1048 DownloadItemImpl* item = CreateDownloadItem();
1027 MockDownloadFile* download_file = DoIntermediateRename(item); 1049 MockDownloadFile* download_file =
1050 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1028 1051
1029 // Drive the delegate interaction. 1052 // Drive the delegate interaction.
1030 base::Closure delegate_callback; 1053 base::Closure delegate_callback;
1031 base::Closure copy_delegate_callback; 1054 base::Closure copy_delegate_callback;
1032 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1055 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1033 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1056 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1034 Return(false))) 1057 Return(false)))
1035 .WillOnce(Return(true)); 1058 .WillOnce(Return(true));
1036 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1059 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
1037 ASSERT_FALSE(delegate_callback.is_null()); 1060 ASSERT_FALSE(delegate_callback.is_null());
1038 copy_delegate_callback = delegate_callback; 1061 copy_delegate_callback = delegate_callback;
1039 delegate_callback.Reset(); 1062 delegate_callback.Reset();
1040 EXPECT_FALSE(item->IsDangerous()); 1063 EXPECT_FALSE(item->IsDangerous());
1041 item->OnContentCheckCompleted( 1064 item->OnContentCheckCompleted(
1042 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1065 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1043 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1066 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1044 copy_delegate_callback.Run(); 1067 copy_delegate_callback.Run();
1045 ASSERT_TRUE(delegate_callback.is_null()); 1068 ASSERT_TRUE(delegate_callback.is_null());
1046 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1069 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1047 EXPECT_TRUE(item->IsDangerous()); 1070 EXPECT_TRUE(item->IsDangerous());
1048 1071
1049 // Make sure the download doesn't complete until we've validated it. 1072 // Make sure the download doesn't complete until we've validated it.
1050 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) 1073 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _))
1051 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1074 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
1052 base::FilePath(kDummyPath))); 1075 base::FilePath(kDummyPath)));
1053 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1076 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1054 .WillOnce(Return(true)); 1077 .WillOnce(Return(true));
1078 EXPECT_CALL(*download_file, FullPath())
1079 .WillOnce(Return(base::FilePath()));
1055 EXPECT_CALL(*download_file, Detach()); 1080 EXPECT_CALL(*download_file, Detach());
1056 RunAllPendingInMessageLoops(); 1081 RunAllPendingInMessageLoops();
1057 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1082 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1058 EXPECT_TRUE(item->IsDangerous()); 1083 EXPECT_TRUE(item->IsDangerous());
1059 1084
1060 item->DangerousDownloadValidated(); 1085 item->ValidateDangerousDownload();
1061 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); 1086 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType());
1062 RunAllPendingInMessageLoops(); 1087 RunAllPendingInMessageLoops();
1063 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1088 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1064 } 1089 }
1065 1090
1066 // Just delaying completion twice. 1091 // Just delaying completion twice.
1067 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { 1092 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) {
1068 // Test to confirm that if we have a callback that returns true, 1093 // Test to confirm that if we have a callback that returns true,
1069 // we complete immediately. 1094 // we complete immediately.
1070 DownloadItemImpl* item = CreateDownloadItem(); 1095 DownloadItemImpl* item = CreateDownloadItem();
1071 MockDownloadFile* download_file = DoIntermediateRename(item); 1096 MockDownloadFile* download_file =
1097 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1072 1098
1073 // Drive the delegate interaction. 1099 // Drive the delegate interaction.
1074 base::Closure delegate_callback; 1100 base::Closure delegate_callback;
1075 base::Closure copy_delegate_callback; 1101 base::Closure copy_delegate_callback;
1076 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1102 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1077 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1103 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1078 Return(false))) 1104 Return(false)))
1079 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1105 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1080 Return(false))) 1106 Return(false)))
1081 .WillOnce(Return(true)); 1107 .WillOnce(Return(true));
(...skipping 11 matching lines...) Expand all
1093 ASSERT_TRUE(delegate_callback.is_null()); 1119 ASSERT_TRUE(delegate_callback.is_null());
1094 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1120 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1095 EXPECT_FALSE(item->IsDangerous()); 1121 EXPECT_FALSE(item->IsDangerous());
1096 1122
1097 // Make sure the download can complete. 1123 // Make sure the download can complete.
1098 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) 1124 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _))
1099 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1125 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
1100 base::FilePath(kDummyPath))); 1126 base::FilePath(kDummyPath)));
1101 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1127 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1102 .WillOnce(Return(true)); 1128 .WillOnce(Return(true));
1129 EXPECT_CALL(*download_file, FullPath())
1130 .WillOnce(Return(base::FilePath()));
1103 EXPECT_CALL(*download_file, Detach()); 1131 EXPECT_CALL(*download_file, Detach());
1104 RunAllPendingInMessageLoops(); 1132 RunAllPendingInMessageLoops();
1105 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1133 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1106 } 1134 }
1107 1135
1136 TEST_F(DownloadItemTest, StealDangerousDownload) {
1137 DownloadItemImpl* item = CreateDownloadItem();
1138 MockDownloadFile* download_file =
1139 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1140 ASSERT_TRUE(item->IsDangerous());
1141 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt"));
1142 base::FilePath returned_path;
1143
1144 EXPECT_CALL(*download_file, FullPath())
1145 .WillOnce(Return(full_path));
1146 EXPECT_CALL(*download_file, Detach());
1147 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1148 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1149 item->StealDangerousDownload(
1150 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1151 weak_ptr_factory.GetWeakPtr(),
1152 base::Unretained(&returned_path)));
1153 RunAllPendingInMessageLoops();
1154 EXPECT_EQ(full_path, returned_path);
1155 }
1156
1157 TEST_F(DownloadItemTest, StealInterruptedDangerousDownload) {
Randy Smith (Not in Mondays) 2013/05/28 17:29:40 Do you want a version of this that steals an inter
asanka 2013/05/28 22:01:39 Yup. Thanks and done.
1158 CommandLine::ForCurrentProcess()->AppendSwitch(
1159 switches::kEnableDownloadResumption);
1160 base::FilePath returned_path;
1161 DownloadItemImpl* item = CreateDownloadItem();
1162 MockDownloadFile* download_file =
1163 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1164 base::FilePath full_path = item->GetFullPath();
1165 EXPECT_FALSE(full_path.empty());
1166 EXPECT_CALL(*download_file, FullPath())
1167 .WillOnce(Return(full_path));
1168 EXPECT_CALL(*download_file, Detach());
1169 item->DestinationObserverAsWeakPtr()->DestinationError(
1170 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
1171 ASSERT_TRUE(item->IsDangerous());
1172
1173 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1174 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1175 item->StealDangerousDownload(
1176 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1177 weak_ptr_factory.GetWeakPtr(),
1178 base::Unretained(&returned_path)));
1179 RunAllPendingInMessageLoops();
1180 EXPECT_EQ(full_path, returned_path);
1181 }
1182
1108 TEST(MockDownloadItem, Compiles) { 1183 TEST(MockDownloadItem, Compiles) {
1109 MockDownloadItem mock_item; 1184 MockDownloadItem mock_item;
1110 } 1185 }
1111 1186
1112 } // namespace content 1187 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698