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/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 Loading... |
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 Loading... |
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 Loading... |
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) { | 412 TEST_F(DownloadItemTest, NotificationAfterDiscardDangerousDownload) { |
405 DownloadItemImpl* item = CreateDownloadItem(); | 413 DownloadItemImpl* item = CreateDownloadItem(); |
406 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 414 MockDownloadFile* download_file = |
| 415 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 416 ASSERT_TRUE(item->IsDangerous()); |
| 417 |
| 418 MockObserver observer(item); |
407 EXPECT_CALL(*download_file, Cancel()); | 419 EXPECT_CALL(*download_file, Cancel()); |
408 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | 420 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
409 MockObserver observer(item); | 421 item->DiscardDangerousDownload(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN, |
410 | 422 DownloadItem::AcquireFileCallback()); |
411 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | |
412 ASSERT_TRUE(observer.CheckUpdated()); | 423 ASSERT_TRUE(observer.CheckUpdated()); |
413 } | 424 } |
414 | 425 |
415 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 426 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
416 DownloadItemImpl* item = CreateDownloadItem(); | 427 DownloadItemImpl* item = CreateDownloadItem(); |
417 MockObserver observer(item); | 428 MockObserver observer(item); |
418 | 429 |
419 DestroyDownloadItem(item); | 430 DestroyDownloadItem(item); |
420 ASSERT_TRUE(observer.CheckDestroyed()); | 431 ASSERT_TRUE(observer.CheckDestroyed()); |
421 } | 432 } |
422 | 433 |
423 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { | 434 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { |
424 CommandLine::ForCurrentProcess()->AppendSwitch( | 435 CommandLine::ForCurrentProcess()->AppendSwitch( |
425 switches::kEnableDownloadResumption); | 436 switches::kEnableDownloadResumption); |
426 | 437 |
427 DownloadItemImpl* item = CreateDownloadItem(); | 438 DownloadItemImpl* item = CreateDownloadItem(); |
428 MockObserver observer(item); | 439 MockObserver observer(item); |
429 DownloadItemImplDelegate::DownloadTargetCallback callback; | 440 DownloadItemImplDelegate::DownloadTargetCallback callback; |
430 MockDownloadFile* download_file = DoIntermediateRename(item); | 441 MockDownloadFile* download_file = |
| 442 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
431 | 443 |
432 // Interrupt the download, using a continuable interrupt. | 444 // Interrupt the download, using a continuable interrupt. |
| 445 EXPECT_CALL(*download_file, FullPath()) |
| 446 .WillOnce(Return(base::FilePath())); |
433 EXPECT_CALL(*download_file, Detach()); | 447 EXPECT_CALL(*download_file, Detach()); |
434 item->DestinationObserverAsWeakPtr()->DestinationError( | 448 item->DestinationObserverAsWeakPtr()->DestinationError( |
435 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); | 449 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
436 ASSERT_TRUE(observer.CheckUpdated()); | 450 ASSERT_TRUE(observer.CheckUpdated()); |
437 // Should attempt to auto-resume. Because we don't have a mock WebContents, | 451 // Should attempt to auto-resume. Because we don't have a mock WebContents, |
438 // ResumeInterruptedDownload() will abort early, with another interrupt, | 452 // ResumeInterruptedDownload() will abort early, with another interrupt, |
439 // which will be ignored. | 453 // which will be ignored. |
440 ASSERT_EQ(1, observer.GetInterruptCount()); | 454 ASSERT_EQ(1, observer.GetInterruptCount()); |
441 ASSERT_EQ(0, observer.GetResumeCount()); | 455 ASSERT_EQ(0, observer.GetResumeCount()); |
442 RunAllPendingInMessageLoops(); | 456 RunAllPendingInMessageLoops(); |
443 | 457 |
444 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); | 458 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); |
445 } | 459 } |
446 | 460 |
447 // Same as above, but with a non-continuable interrupt. | 461 // Same as above, but with a non-continuable interrupt. |
448 TEST_F(DownloadItemTest, RestartAfterInterrupted) { | 462 TEST_F(DownloadItemTest, RestartAfterInterrupted) { |
449 CommandLine::ForCurrentProcess()->AppendSwitch( | 463 CommandLine::ForCurrentProcess()->AppendSwitch( |
450 switches::kEnableDownloadResumption); | 464 switches::kEnableDownloadResumption); |
451 | 465 |
452 DownloadItemImpl* item = CreateDownloadItem(); | 466 DownloadItemImpl* item = CreateDownloadItem(); |
453 MockObserver observer(item); | 467 MockObserver observer(item); |
454 DownloadItemImplDelegate::DownloadTargetCallback callback; | 468 DownloadItemImplDelegate::DownloadTargetCallback callback; |
455 MockDownloadFile* download_file = DoIntermediateRename(item); | 469 MockDownloadFile* download_file = |
| 470 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
456 | 471 |
457 // Interrupt the download, using a restartable interrupt. | 472 // Interrupt the download, using a restartable interrupt. |
458 EXPECT_CALL(*download_file, Cancel()); | 473 EXPECT_CALL(*download_file, Cancel()); |
459 item->DestinationObserverAsWeakPtr()->DestinationError( | 474 item->DestinationObserverAsWeakPtr()->DestinationError( |
460 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 475 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
461 ASSERT_TRUE(observer.CheckUpdated()); | 476 ASSERT_TRUE(observer.CheckUpdated()); |
462 // Should not try to auto-resume. | 477 // Should not try to auto-resume. |
463 ASSERT_EQ(1, observer.GetInterruptCount()); | 478 ASSERT_EQ(1, observer.GetInterruptCount()); |
464 ASSERT_EQ(0, observer.GetResumeCount()); | 479 ASSERT_EQ(0, observer.GetResumeCount()); |
465 RunAllPendingInMessageLoops(); | 480 RunAllPendingInMessageLoops(); |
(...skipping 18 matching lines...) Expand all Loading... |
484 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 499 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
485 .WillRepeatedly(SaveArg<1>(&callback)); | 500 .WillRepeatedly(SaveArg<1>(&callback)); |
486 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { | 501 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { |
487 DVLOG(20) << "Loop iteration " << i; | 502 DVLOG(20) << "Loop iteration " << i; |
488 | 503 |
489 mock_download_file = new NiceMock<MockDownloadFile>; | 504 mock_download_file = new NiceMock<MockDownloadFile>; |
490 download_file.reset(mock_download_file); | 505 download_file.reset(mock_download_file); |
491 mock_request_handle = new NiceMock<MockRequestHandle>; | 506 mock_request_handle = new NiceMock<MockRequestHandle>; |
492 request_handle.reset(mock_request_handle); | 507 request_handle.reset(mock_request_handle); |
493 | 508 |
| 509 ON_CALL(*mock_download_file, FullPath()) |
| 510 .WillByDefault(Return(base::FilePath())); |
| 511 |
494 // It's too complicated to set up a WebContents instance that would cause | 512 // It's too complicated to set up a WebContents instance that would cause |
495 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function | 513 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function |
496 // to be callled, so we simply verify that GetWebContents() is called. | 514 // to be callled, so we simply verify that GetWebContents() is called. |
497 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { | 515 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { |
498 EXPECT_CALL(*mock_request_handle, GetWebContents()) | 516 EXPECT_CALL(*mock_request_handle, GetWebContents()) |
499 .WillOnce(Return(static_cast<WebContents*>(NULL))); | 517 .WillOnce(Return(static_cast<WebContents*>(NULL))); |
500 } | 518 } |
501 | 519 |
502 // Copied key parts of DoIntermediateRename & AddDownloadFileToDownloadItem | 520 // Copied key parts of DoIntermediateRename & AddDownloadFileToDownloadItem |
503 // to allow for holding onto the request handle. | 521 // to allow for holding onto the request handle. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 // Setting to unsafe url or unsafe file should trigger a notification. | 571 // Setting to unsafe url or unsafe file should trigger a notification. |
554 DownloadItemImpl* unsafeurl_item = | 572 DownloadItemImpl* unsafeurl_item = |
555 CreateDownloadItem(); | 573 CreateDownloadItem(); |
556 MockObserver unsafeurl_observer(unsafeurl_item); | 574 MockObserver unsafeurl_observer(unsafeurl_item); |
557 | 575 |
558 unsafeurl_item->OnAllDataSaved(std::string()); | 576 unsafeurl_item->OnAllDataSaved(std::string()); |
559 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 577 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
560 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); | 578 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); |
561 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 579 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
562 | 580 |
563 unsafeurl_item->DangerousDownloadValidated(); | 581 unsafeurl_item->ValidateDangerousDownload(); |
564 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 582 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
565 | 583 |
566 DownloadItemImpl* unsafefile_item = | 584 DownloadItemImpl* unsafefile_item = |
567 CreateDownloadItem(); | 585 CreateDownloadItem(); |
568 MockObserver unsafefile_observer(unsafefile_item); | 586 MockObserver unsafefile_observer(unsafefile_item); |
569 | 587 |
570 unsafefile_item->OnAllDataSaved(std::string()); | 588 unsafefile_item->OnAllDataSaved(std::string()); |
571 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 589 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
572 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 590 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
573 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 591 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
574 | 592 |
575 unsafefile_item->DangerousDownloadValidated(); | 593 unsafefile_item->ValidateDangerousDownload(); |
576 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 594 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
577 } | 595 } |
578 | 596 |
579 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run | 597 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run |
580 // DownloadFile::Rename(). Once the rename | 598 // DownloadFile::Rename(). Once the rename |
581 // completes, DownloadItemImpl receives a notification with the new file | 599 // completes, DownloadItemImpl receives a notification with the new file |
582 // name. Check that observers are updated when the new filename is available and | 600 // name. Check that observers are updated when the new filename is available and |
583 // not before. | 601 // not before. |
584 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { | 602 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { |
585 DownloadItemImpl* item = CreateDownloadItem(); | 603 DownloadItemImpl* item = CreateDownloadItem(); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 RunAllPendingInMessageLoops(); | 707 RunAllPendingInMessageLoops(); |
690 // All the callbacks should have happened by now. | 708 // All the callbacks should have happened by now. |
691 ::testing::Mock::VerifyAndClearExpectations(download_file); | 709 ::testing::Mock::VerifyAndClearExpectations(download_file); |
692 mock_delegate()->VerifyAndClearExpectations(); | 710 mock_delegate()->VerifyAndClearExpectations(); |
693 | 711 |
694 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 712 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
695 .WillOnce(Return(true)); | 713 .WillOnce(Return(true)); |
696 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) | 714 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) |
697 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 715 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
698 final_path)); | 716 final_path)); |
| 717 EXPECT_CALL(*download_file, FullPath()) |
| 718 .WillOnce(Return(base::FilePath())); |
699 EXPECT_CALL(*download_file, Detach()); | 719 EXPECT_CALL(*download_file, Detach()); |
700 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); | 720 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); |
701 RunAllPendingInMessageLoops(); | 721 RunAllPendingInMessageLoops(); |
702 ::testing::Mock::VerifyAndClearExpectations(download_file); | 722 ::testing::Mock::VerifyAndClearExpectations(download_file); |
703 mock_delegate()->VerifyAndClearExpectations(); | 723 mock_delegate()->VerifyAndClearExpectations(); |
704 } | 724 } |
705 | 725 |
706 // Test that the delegate is invoked after the download file is renamed and the | 726 // Test that the delegate is invoked after the download file is renamed and the |
707 // download item is in an interrupted state. | 727 // download item is in an interrupted state. |
708 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { | 728 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { |
(...skipping 14 matching lines...) Expand all Loading... |
723 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 743 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
724 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 744 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
725 RunAllPendingInMessageLoops(); | 745 RunAllPendingInMessageLoops(); |
726 // All the callbacks should have happened by now. | 746 // All the callbacks should have happened by now. |
727 ::testing::Mock::VerifyAndClearExpectations(download_file); | 747 ::testing::Mock::VerifyAndClearExpectations(download_file); |
728 mock_delegate()->VerifyAndClearExpectations(); | 748 mock_delegate()->VerifyAndClearExpectations(); |
729 } | 749 } |
730 | 750 |
731 TEST_F(DownloadItemTest, Interrupted) { | 751 TEST_F(DownloadItemTest, Interrupted) { |
732 DownloadItemImpl* item = CreateDownloadItem(); | 752 DownloadItemImpl* item = CreateDownloadItem(); |
733 MockDownloadFile* download_file = DoIntermediateRename(item); | 753 MockDownloadFile* download_file = |
| 754 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
734 | 755 |
735 const DownloadInterruptReason reason( | 756 const DownloadInterruptReason reason( |
736 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 757 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
737 | 758 |
738 // Confirm interrupt sets state properly. | 759 // Confirm interrupt sets state properly. |
739 EXPECT_CALL(*download_file, Cancel()); | 760 EXPECT_CALL(*download_file, Cancel()); |
740 item->DestinationObserverAsWeakPtr()->DestinationError(reason); | 761 item->DestinationObserverAsWeakPtr()->DestinationError(reason); |
741 RunAllPendingInMessageLoops(); | 762 RunAllPendingInMessageLoops(); |
742 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 763 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
743 EXPECT_EQ(reason, item->GetLastReason()); | 764 EXPECT_EQ(reason, item->GetLastReason()); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 as_observer->DestinationUpdate(200, 20, "livebeef"); | 843 as_observer->DestinationUpdate(200, 20, "livebeef"); |
823 EXPECT_EQ(20l, item->CurrentSpeed()); | 844 EXPECT_EQ(20l, item->CurrentSpeed()); |
824 EXPECT_EQ("livebeef", item->GetHashState()); | 845 EXPECT_EQ("livebeef", item->GetHashState()); |
825 EXPECT_EQ(200l, item->GetReceivedBytes()); | 846 EXPECT_EQ(200l, item->GetReceivedBytes()); |
826 EXPECT_EQ(0l, item->GetTotalBytes()); | 847 EXPECT_EQ(0l, item->GetTotalBytes()); |
827 EXPECT_TRUE(observer.CheckUpdated()); | 848 EXPECT_TRUE(observer.CheckUpdated()); |
828 } | 849 } |
829 | 850 |
830 TEST_F(DownloadItemTest, DestinationError) { | 851 TEST_F(DownloadItemTest, DestinationError) { |
831 DownloadItemImpl* item = CreateDownloadItem(); | 852 DownloadItemImpl* item = CreateDownloadItem(); |
832 MockDownloadFile* download_file = DoIntermediateRename(item); | 853 MockDownloadFile* download_file = |
| 854 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
833 base::WeakPtr<DownloadDestinationObserver> as_observer( | 855 base::WeakPtr<DownloadDestinationObserver> as_observer( |
834 item->DestinationObserverAsWeakPtr()); | 856 item->DestinationObserverAsWeakPtr()); |
835 MockObserver observer(item); | 857 MockObserver observer(item); |
836 | 858 |
837 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 859 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
838 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); | 860 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); |
839 EXPECT_FALSE(observer.CheckUpdated()); | 861 EXPECT_FALSE(observer.CheckUpdated()); |
840 | 862 |
841 EXPECT_CALL(*download_file, Cancel()); | 863 EXPECT_CALL(*download_file, Cancel()); |
842 as_observer->DestinationError( | 864 as_observer->DestinationError( |
(...skipping 29 matching lines...) Expand all Loading... |
872 mock_delegate()->VerifyAndClearExpectations(); | 894 mock_delegate()->VerifyAndClearExpectations(); |
873 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 895 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
874 EXPECT_TRUE(observer.CheckUpdated()); | 896 EXPECT_TRUE(observer.CheckUpdated()); |
875 EXPECT_EQ("livebeef", item->GetHash()); | 897 EXPECT_EQ("livebeef", item->GetHash()); |
876 EXPECT_EQ("", item->GetHashState()); | 898 EXPECT_EQ("", item->GetHashState()); |
877 EXPECT_TRUE(item->AllDataSaved()); | 899 EXPECT_TRUE(item->AllDataSaved()); |
878 } | 900 } |
879 | 901 |
880 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { | 902 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { |
881 DownloadItemImpl* item = CreateDownloadItem(); | 903 DownloadItemImpl* item = CreateDownloadItem(); |
882 MockDownloadFile* download_file = DoIntermediateRename(item); | 904 MockDownloadFile* download_file = |
| 905 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
883 | 906 |
884 // InProgress | 907 // InProgress |
885 ASSERT_TRUE(item->IsInProgress()); | 908 ASSERT_TRUE(item->IsInProgress()); |
886 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 909 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
887 EXPECT_TRUE(item->CanShowInFolder()); | 910 EXPECT_TRUE(item->CanShowInFolder()); |
888 EXPECT_TRUE(item->CanOpenDownload()); | 911 EXPECT_TRUE(item->CanOpenDownload()); |
889 | 912 |
890 // Complete | 913 // Complete |
891 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) | 914 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) |
892 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 915 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
893 base::FilePath(kDummyPath))); | 916 base::FilePath(kDummyPath))); |
894 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 917 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
895 .WillOnce(Return(true)); | 918 .WillOnce(Return(true)); |
| 919 EXPECT_CALL(*download_file, FullPath()) |
| 920 .WillOnce(Return(base::FilePath())); |
896 EXPECT_CALL(*download_file, Detach()); | 921 EXPECT_CALL(*download_file, Detach()); |
897 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); | 922 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); |
898 RunAllPendingInMessageLoops(); | 923 RunAllPendingInMessageLoops(); |
899 | 924 |
900 ASSERT_TRUE(item->IsComplete()); | 925 ASSERT_TRUE(item->IsComplete()); |
901 EXPECT_TRUE(item->CanShowInFolder()); | 926 EXPECT_TRUE(item->CanShowInFolder()); |
902 EXPECT_TRUE(item->CanOpenDownload()); | 927 EXPECT_TRUE(item->CanOpenDownload()); |
903 } | 928 } |
904 | 929 |
905 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { | 930 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { |
906 DownloadItemImpl* item = CreateDownloadItem(); | 931 DownloadItemImpl* item = CreateDownloadItem(); |
907 MockDownloadFile* download_file = DoIntermediateRename(item); | 932 MockDownloadFile* download_file = |
| 933 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
908 item->SetIsTemporary(true); | 934 item->SetIsTemporary(true); |
909 | 935 |
910 // InProgress Temporary | 936 // InProgress Temporary |
911 ASSERT_TRUE(item->IsInProgress()); | 937 ASSERT_TRUE(item->IsInProgress()); |
912 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 938 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
913 ASSERT_TRUE(item->IsTemporary()); | 939 ASSERT_TRUE(item->IsTemporary()); |
914 EXPECT_FALSE(item->CanShowInFolder()); | 940 EXPECT_FALSE(item->CanShowInFolder()); |
915 EXPECT_FALSE(item->CanOpenDownload()); | 941 EXPECT_FALSE(item->CanOpenDownload()); |
916 | 942 |
917 // Complete Temporary | 943 // Complete Temporary |
918 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 944 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
919 .WillOnce(Return(true)); | 945 .WillOnce(Return(true)); |
920 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) | 946 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) |
921 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 947 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
922 base::FilePath(kDummyPath))); | 948 base::FilePath(kDummyPath))); |
| 949 EXPECT_CALL(*download_file, FullPath()) |
| 950 .WillOnce(Return(base::FilePath())); |
923 EXPECT_CALL(*download_file, Detach()); | 951 EXPECT_CALL(*download_file, Detach()); |
924 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); | 952 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); |
925 RunAllPendingInMessageLoops(); | 953 RunAllPendingInMessageLoops(); |
926 | 954 |
927 ASSERT_TRUE(item->IsComplete()); | 955 ASSERT_TRUE(item->IsComplete()); |
928 EXPECT_FALSE(item->CanShowInFolder()); | 956 EXPECT_FALSE(item->CanShowInFolder()); |
929 EXPECT_FALSE(item->CanOpenDownload()); | 957 EXPECT_FALSE(item->CanOpenDownload()); |
930 } | 958 } |
931 | 959 |
932 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { | 960 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { |
933 DownloadItemImpl* item = CreateDownloadItem(); | 961 DownloadItemImpl* item = CreateDownloadItem(); |
934 MockDownloadFile* download_file = DoIntermediateRename(item); | 962 MockDownloadFile* download_file = |
| 963 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
935 | 964 |
936 EXPECT_CALL(*download_file, Cancel()); | 965 EXPECT_CALL(*download_file, Cancel()); |
937 item->DestinationObserverAsWeakPtr()->DestinationError( | 966 item->DestinationObserverAsWeakPtr()->DestinationError( |
938 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 967 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
939 RunAllPendingInMessageLoops(); | 968 RunAllPendingInMessageLoops(); |
940 | 969 |
941 ASSERT_TRUE(item->IsInterrupted()); | 970 ASSERT_TRUE(item->IsInterrupted()); |
942 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 971 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
943 EXPECT_FALSE(item->CanShowInFolder()); | 972 EXPECT_FALSE(item->CanShowInFolder()); |
944 EXPECT_FALSE(item->CanOpenDownload()); | 973 EXPECT_FALSE(item->CanOpenDownload()); |
945 } | 974 } |
946 | 975 |
947 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { | 976 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { |
948 DownloadItemImpl* item = CreateDownloadItem(); | 977 DownloadItemImpl* item = CreateDownloadItem(); |
949 MockDownloadFile* download_file = DoIntermediateRename(item); | 978 MockDownloadFile* download_file = |
| 979 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
950 | 980 |
951 EXPECT_CALL(*download_file, Cancel()); | 981 EXPECT_CALL(*download_file, Cancel()); |
952 item->Cancel(true); | 982 item->Cancel(true); |
953 RunAllPendingInMessageLoops(); | 983 RunAllPendingInMessageLoops(); |
954 | 984 |
955 ASSERT_TRUE(item->IsCancelled()); | 985 ASSERT_TRUE(item->IsCancelled()); |
956 EXPECT_FALSE(item->CanShowInFolder()); | 986 EXPECT_FALSE(item->CanShowInFolder()); |
957 EXPECT_FALSE(item->CanOpenDownload()); | 987 EXPECT_FALSE(item->CanOpenDownload()); |
958 } | 988 } |
959 | 989 |
960 // Test various aspects of the delegate completion blocker. | 990 // Test various aspects of the delegate completion blocker. |
961 | 991 |
962 // Just allowing completion. | 992 // Just allowing completion. |
963 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { | 993 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { |
964 // Test to confirm that if we have a callback that returns true, | 994 // Test to confirm that if we have a callback that returns true, |
965 // we complete immediately. | 995 // we complete immediately. |
966 DownloadItemImpl* item = CreateDownloadItem(); | 996 DownloadItemImpl* item = CreateDownloadItem(); |
967 MockDownloadFile* download_file = DoIntermediateRename(item); | 997 MockDownloadFile* download_file = |
| 998 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
968 | 999 |
969 // Drive the delegate interaction. | 1000 // Drive the delegate interaction. |
970 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1001 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
971 .WillOnce(Return(true)); | 1002 .WillOnce(Return(true)); |
972 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); | 1003 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); |
973 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1004 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
974 EXPECT_FALSE(item->IsDangerous()); | 1005 EXPECT_FALSE(item->IsDangerous()); |
975 | 1006 |
976 // Make sure the download can complete. | 1007 // Make sure the download can complete. |
977 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) | 1008 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) |
978 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 1009 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
979 base::FilePath(kDummyPath))); | 1010 base::FilePath(kDummyPath))); |
980 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1011 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
981 .WillOnce(Return(true)); | 1012 .WillOnce(Return(true)); |
| 1013 EXPECT_CALL(*download_file, FullPath()) |
| 1014 .WillOnce(Return(base::FilePath())); |
982 EXPECT_CALL(*download_file, Detach()); | 1015 EXPECT_CALL(*download_file, Detach()); |
983 RunAllPendingInMessageLoops(); | 1016 RunAllPendingInMessageLoops(); |
984 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1017 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
985 } | 1018 } |
986 | 1019 |
987 // Just delaying completion. | 1020 // Just delaying completion. |
988 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { | 1021 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { |
989 // Test to confirm that if we have a callback that returns true, | 1022 // Test to confirm that if we have a callback that returns true, |
990 // we complete immediately. | 1023 // we complete immediately. |
991 DownloadItemImpl* item = CreateDownloadItem(); | 1024 DownloadItemImpl* item = CreateDownloadItem(); |
992 MockDownloadFile* download_file = DoIntermediateRename(item); | 1025 MockDownloadFile* download_file = |
| 1026 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
993 | 1027 |
994 // Drive the delegate interaction. | 1028 // Drive the delegate interaction. |
995 base::Closure delegate_callback; | 1029 base::Closure delegate_callback; |
996 base::Closure copy_delegate_callback; | 1030 base::Closure copy_delegate_callback; |
997 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1031 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
998 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), | 1032 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), |
999 Return(false))) | 1033 Return(false))) |
1000 .WillOnce(Return(true)); | 1034 .WillOnce(Return(true)); |
1001 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); | 1035 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); |
1002 ASSERT_FALSE(delegate_callback.is_null()); | 1036 ASSERT_FALSE(delegate_callback.is_null()); |
1003 copy_delegate_callback = delegate_callback; | 1037 copy_delegate_callback = delegate_callback; |
1004 delegate_callback.Reset(); | 1038 delegate_callback.Reset(); |
1005 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1039 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1006 copy_delegate_callback.Run(); | 1040 copy_delegate_callback.Run(); |
1007 ASSERT_TRUE(delegate_callback.is_null()); | 1041 ASSERT_TRUE(delegate_callback.is_null()); |
1008 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1042 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1009 EXPECT_FALSE(item->IsDangerous()); | 1043 EXPECT_FALSE(item->IsDangerous()); |
1010 | 1044 |
1011 // Make sure the download can complete. | 1045 // Make sure the download can complete. |
1012 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) | 1046 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) |
1013 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 1047 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
1014 base::FilePath(kDummyPath))); | 1048 base::FilePath(kDummyPath))); |
1015 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1049 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
1016 .WillOnce(Return(true)); | 1050 .WillOnce(Return(true)); |
| 1051 EXPECT_CALL(*download_file, FullPath()) |
| 1052 .WillOnce(Return(base::FilePath())); |
1017 EXPECT_CALL(*download_file, Detach()); | 1053 EXPECT_CALL(*download_file, Detach()); |
1018 RunAllPendingInMessageLoops(); | 1054 RunAllPendingInMessageLoops(); |
1019 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1055 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
1020 } | 1056 } |
1021 | 1057 |
1022 // Delay and set danger. | 1058 // Delay and set danger. |
1023 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { | 1059 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { |
1024 // Test to confirm that if we have a callback that returns true, | 1060 // Test to confirm that if we have a callback that returns true, |
1025 // we complete immediately. | 1061 // we complete immediately. |
1026 DownloadItemImpl* item = CreateDownloadItem(); | 1062 DownloadItemImpl* item = CreateDownloadItem(); |
1027 MockDownloadFile* download_file = DoIntermediateRename(item); | 1063 MockDownloadFile* download_file = |
| 1064 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
1028 | 1065 |
1029 // Drive the delegate interaction. | 1066 // Drive the delegate interaction. |
1030 base::Closure delegate_callback; | 1067 base::Closure delegate_callback; |
1031 base::Closure copy_delegate_callback; | 1068 base::Closure copy_delegate_callback; |
1032 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1069 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
1033 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), | 1070 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), |
1034 Return(false))) | 1071 Return(false))) |
1035 .WillOnce(Return(true)); | 1072 .WillOnce(Return(true)); |
1036 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); | 1073 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); |
1037 ASSERT_FALSE(delegate_callback.is_null()); | 1074 ASSERT_FALSE(delegate_callback.is_null()); |
1038 copy_delegate_callback = delegate_callback; | 1075 copy_delegate_callback = delegate_callback; |
1039 delegate_callback.Reset(); | 1076 delegate_callback.Reset(); |
1040 EXPECT_FALSE(item->IsDangerous()); | 1077 EXPECT_FALSE(item->IsDangerous()); |
1041 item->OnContentCheckCompleted( | 1078 item->OnContentCheckCompleted( |
1042 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1079 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
1043 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1080 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1044 copy_delegate_callback.Run(); | 1081 copy_delegate_callback.Run(); |
1045 ASSERT_TRUE(delegate_callback.is_null()); | 1082 ASSERT_TRUE(delegate_callback.is_null()); |
1046 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1083 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1047 EXPECT_TRUE(item->IsDangerous()); | 1084 EXPECT_TRUE(item->IsDangerous()); |
1048 | 1085 |
1049 // Make sure the download doesn't complete until we've validated it. | 1086 // Make sure the download doesn't complete until we've validated it. |
1050 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) | 1087 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) |
1051 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 1088 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
1052 base::FilePath(kDummyPath))); | 1089 base::FilePath(kDummyPath))); |
1053 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1090 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
1054 .WillOnce(Return(true)); | 1091 .WillOnce(Return(true)); |
| 1092 EXPECT_CALL(*download_file, FullPath()) |
| 1093 .WillOnce(Return(base::FilePath())); |
1055 EXPECT_CALL(*download_file, Detach()); | 1094 EXPECT_CALL(*download_file, Detach()); |
1056 RunAllPendingInMessageLoops(); | 1095 RunAllPendingInMessageLoops(); |
1057 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1096 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1058 EXPECT_TRUE(item->IsDangerous()); | 1097 EXPECT_TRUE(item->IsDangerous()); |
1059 | 1098 |
1060 item->DangerousDownloadValidated(); | 1099 item->ValidateDangerousDownload(); |
1061 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); | 1100 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); |
1062 RunAllPendingInMessageLoops(); | 1101 RunAllPendingInMessageLoops(); |
1063 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1102 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
1064 } | 1103 } |
1065 | 1104 |
1066 // Just delaying completion twice. | 1105 // Just delaying completion twice. |
1067 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { | 1106 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { |
1068 // Test to confirm that if we have a callback that returns true, | 1107 // Test to confirm that if we have a callback that returns true, |
1069 // we complete immediately. | 1108 // we complete immediately. |
1070 DownloadItemImpl* item = CreateDownloadItem(); | 1109 DownloadItemImpl* item = CreateDownloadItem(); |
1071 MockDownloadFile* download_file = DoIntermediateRename(item); | 1110 MockDownloadFile* download_file = |
| 1111 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
1072 | 1112 |
1073 // Drive the delegate interaction. | 1113 // Drive the delegate interaction. |
1074 base::Closure delegate_callback; | 1114 base::Closure delegate_callback; |
1075 base::Closure copy_delegate_callback; | 1115 base::Closure copy_delegate_callback; |
1076 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1116 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
1077 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), | 1117 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), |
1078 Return(false))) | 1118 Return(false))) |
1079 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), | 1119 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), |
1080 Return(false))) | 1120 Return(false))) |
1081 .WillOnce(Return(true)); | 1121 .WillOnce(Return(true)); |
(...skipping 11 matching lines...) Expand all Loading... |
1093 ASSERT_TRUE(delegate_callback.is_null()); | 1133 ASSERT_TRUE(delegate_callback.is_null()); |
1094 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1134 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1095 EXPECT_FALSE(item->IsDangerous()); | 1135 EXPECT_FALSE(item->IsDangerous()); |
1096 | 1136 |
1097 // Make sure the download can complete. | 1137 // Make sure the download can complete. |
1098 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) | 1138 EXPECT_CALL(*download_file, RenameAndAnnotate(base::FilePath(kDummyPath), _)) |
1099 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 1139 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
1100 base::FilePath(kDummyPath))); | 1140 base::FilePath(kDummyPath))); |
1101 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1141 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
1102 .WillOnce(Return(true)); | 1142 .WillOnce(Return(true)); |
| 1143 EXPECT_CALL(*download_file, FullPath()) |
| 1144 .WillOnce(Return(base::FilePath())); |
1103 EXPECT_CALL(*download_file, Detach()); | 1145 EXPECT_CALL(*download_file, Detach()); |
1104 RunAllPendingInMessageLoops(); | 1146 RunAllPendingInMessageLoops(); |
1105 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1147 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
1106 } | 1148 } |
1107 | 1149 |
| 1150 TEST_F(DownloadItemTest, AcquireFileAndDiscardDownload) { |
| 1151 DownloadItemImpl* item = CreateDownloadItem(); |
| 1152 MockDownloadFile* download_file = |
| 1153 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1154 ASSERT_TRUE(item->IsDangerous()); |
| 1155 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); |
| 1156 base::FilePath returned_path; |
| 1157 |
| 1158 // DiscardDangerousDownload with a non-null AcquireFileCallback should cause |
| 1159 // the DownloadFile to be detached and its filename to be passed to the |
| 1160 // callback. |
| 1161 EXPECT_CALL(*download_file, FullPath()) |
| 1162 .WillOnce(Return(full_path)); |
| 1163 EXPECT_CALL(*download_file, Detach()); |
| 1164 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
| 1165 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1166 item->DiscardDangerousDownload( |
| 1167 DownloadItem::DELETE_DUE_TO_USER_DISCARD, |
| 1168 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1169 weak_ptr_factory.GetWeakPtr(), |
| 1170 base::Unretained(&returned_path))); |
| 1171 RunAllPendingInMessageLoops(); |
| 1172 EXPECT_EQ(full_path, returned_path); |
| 1173 } |
| 1174 |
1108 TEST(MockDownloadItem, Compiles) { | 1175 TEST(MockDownloadItem, Compiles) { |
1109 MockDownloadItem mock_item; | 1176 MockDownloadItem mock_item; |
1110 } | 1177 } |
1111 | 1178 |
1112 } // namespace content | 1179 } // namespace content |
OLD | NEW |