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

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

Issue 1517283002: Use a test BrowserContext in DownloadItemImpl unittests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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
« no previous file with comments | « content/browser/download/download_item_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/callback.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
10 #include "content/browser/byte_stream.h" 10 #include "content/browser/byte_stream.h"
11 #include "content/browser/download/download_create_info.h" 11 #include "content/browser/download/download_create_info.h"
12 #include "content/browser/download/download_file_factory.h" 12 #include "content/browser/download/download_file_factory.h"
13 #include "content/browser/download/download_item_impl.h" 13 #include "content/browser/download/download_item_impl.h"
14 #include "content/browser/download/download_item_impl_delegate.h" 14 #include "content/browser/download/download_item_impl_delegate.h"
15 #include "content/browser/download/download_request_handle.h" 15 #include "content/browser/download/download_request_handle.h"
16 #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" 17 #include "content/public/browser/download_destination_observer.h"
18 #include "content/public/browser/download_interrupt_reasons.h" 18 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_url_parameters.h" 19 #include "content/public/browser/download_url_parameters.h"
20 #include "content/public/common/content_switches.h" 20 #include "content/public/common/content_switches.h"
21 #include "content/public/test/mock_download_item.h" 21 #include "content/public/test/mock_download_item.h"
22 #include "content/public/test/test_browser_context.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;
28 using ::testing::Property; 29 using ::testing::Property;
29 using ::testing::Return; 30 using ::testing::Return;
30 using ::testing::SaveArg; 31 using ::testing::SaveArg;
31 using ::testing::StrictMock; 32 using ::testing::StrictMock;
(...skipping 21 matching lines...) Expand all
53 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const base::FilePath&)); 54 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const base::FilePath&));
54 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); 55 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*));
55 56
56 void ResumeInterruptedDownload( 57 void ResumeInterruptedDownload(
57 scoped_ptr<DownloadUrlParameters> params, uint32 id) override { 58 scoped_ptr<DownloadUrlParameters> params, uint32 id) override {
58 MockResumeInterruptedDownload(params.get(), id); 59 MockResumeInterruptedDownload(params.get(), id);
59 } 60 }
60 MOCK_METHOD2(MockResumeInterruptedDownload, 61 MOCK_METHOD2(MockResumeInterruptedDownload,
61 void(DownloadUrlParameters* params, uint32 id)); 62 void(DownloadUrlParameters* params, uint32 id));
62 63
63 BrowserContext* GetBrowserContext() const override { return nullptr; } 64 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*());
64 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); 65 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*));
65 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); 66 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*));
66 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); 67 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*));
67 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); 68 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*));
68 69
69 void VerifyAndClearExpectations() { 70 void VerifyAndClearExpectations() {
70 ::testing::Mock::VerifyAndClearExpectations(this); 71 ::testing::Mock::VerifyAndClearExpectations(this);
71 SetDefaultExpectations(); 72 SetDefaultExpectations();
72 } 73 }
73 74
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 MockObserver observer(item); 405 MockObserver observer(item);
405 406
406 DestroyDownloadItem(item); 407 DestroyDownloadItem(item);
407 ASSERT_TRUE(observer.CheckDestroyed()); 408 ASSERT_TRUE(observer.CheckDestroyed());
408 } 409 }
409 410
410 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { 411 TEST_F(DownloadItemTest, ContinueAfterInterrupted) {
411 base::CommandLine::ForCurrentProcess()->AppendSwitch( 412 base::CommandLine::ForCurrentProcess()->AppendSwitch(
412 switches::kEnableDownloadResumption); 413 switches::kEnableDownloadResumption);
413 414
415 TestBrowserContext test_browser_context;
414 DownloadItemImpl* item = CreateDownloadItem(); 416 DownloadItemImpl* item = CreateDownloadItem();
415 MockObserver observer(item); 417 MockObserver observer(item);
416 DownloadItemImplDelegate::DownloadTargetCallback callback; 418 DownloadItemImplDelegate::DownloadTargetCallback callback;
417 MockDownloadFile* download_file = 419 MockDownloadFile* download_file =
418 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 420 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
419 421
420 // Interrupt the download, using a continuable interrupt. 422 // Interrupt the download, using a continuable interrupt.
421 EXPECT_CALL(*download_file, FullPath()) 423 EXPECT_CALL(*download_file, FullPath())
422 .WillOnce(Return(base::FilePath())); 424 .WillOnce(Return(base::FilePath()));
423 EXPECT_CALL(*download_file, Detach()); 425 EXPECT_CALL(*download_file, Detach());
426 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
427 .WillRepeatedly(Return(&test_browser_context));
428 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1);
424 item->DestinationObserverAsWeakPtr()->DestinationError( 429 item->DestinationObserverAsWeakPtr()->DestinationError(
425 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 430 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
426 ASSERT_TRUE(observer.CheckUpdated()); 431 ASSERT_TRUE(observer.CheckUpdated());
427 // Should attempt to auto-resume. Because we don't have a mock WebContents,
428 // ResumeInterruptedDownload() will abort early, with another interrupt,
429 // which will be ignored.
430 ASSERT_EQ(1, observer.GetInterruptCount()); 432 ASSERT_EQ(1, observer.GetInterruptCount());
433
434 // Test expectations verify that ResumeInterruptedDownload() is called when
435 // the download is interrupted. But the mock doesn't actually invoke
svaldez 2015/12/11 20:53:04 nit: Maybe clarify where we cancel the resume?
asanka 2015/12/11 21:23:40 Done.
431 ASSERT_EQ(0, observer.GetResumeCount()); 436 ASSERT_EQ(0, observer.GetResumeCount());
432 RunAllPendingInMessageLoops(); 437 RunAllPendingInMessageLoops();
433 438
434 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); 439 CleanupItem(item, download_file, DownloadItem::INTERRUPTED);
435 } 440 }
436 441
437 // Same as above, but with a non-continuable interrupt. 442 // Same as above, but with a non-continuable interrupt.
438 TEST_F(DownloadItemTest, RestartAfterInterrupted) { 443 TEST_F(DownloadItemTest, RestartAfterInterrupted) {
439 base::CommandLine::ForCurrentProcess()->AppendSwitch( 444 base::CommandLine::ForCurrentProcess()->AppendSwitch(
440 switches::kEnableDownloadResumption); 445 switches::kEnableDownloadResumption);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 ASSERT_EQ(1, observer.GetInterruptCount()); 491 ASSERT_EQ(1, observer.GetInterruptCount());
487 ASSERT_EQ(0, observer.GetResumeCount()); 492 ASSERT_EQ(0, observer.GetResumeCount());
488 493
489 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); 494 CleanupItem(item, download_file, DownloadItem::INTERRUPTED);
490 } 495 }
491 496
492 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { 497 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) {
493 base::CommandLine::ForCurrentProcess()->AppendSwitch( 498 base::CommandLine::ForCurrentProcess()->AppendSwitch(
494 switches::kEnableDownloadResumption); 499 switches::kEnableDownloadResumption);
495 500
501 TestBrowserContext test_browser_context;
496 DownloadItemImpl* item = CreateDownloadItem(); 502 DownloadItemImpl* item = CreateDownloadItem();
497 base::WeakPtr<DownloadDestinationObserver> as_observer( 503 base::WeakPtr<DownloadDestinationObserver> as_observer(
498 item->DestinationObserverAsWeakPtr()); 504 item->DestinationObserverAsWeakPtr());
499 MockObserver observer(item); 505 MockObserver observer(item);
500 MockDownloadFile* mock_download_file(NULL); 506 MockDownloadFile* mock_download_file(NULL);
501 scoped_ptr<DownloadFile> download_file; 507 scoped_ptr<DownloadFile> download_file;
502 MockRequestHandle* mock_request_handle(NULL); 508 MockRequestHandle* mock_request_handle(NULL);
503 scoped_ptr<DownloadRequestHandleInterface> request_handle; 509 scoped_ptr<DownloadRequestHandleInterface> request_handle;
504 DownloadItemImplDelegate::DownloadTargetCallback callback; 510 DownloadItemImplDelegate::DownloadTargetCallback callback;
505 511
506 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 512 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
507 .WillRepeatedly(SaveArg<1>(&callback)); 513 .WillRepeatedly(SaveArg<1>(&callback));
514 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
515 .WillRepeatedly(Return(&test_browser_context));
516 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _))
517 .Times(DownloadItemImpl::kMaxAutoResumeAttempts);
508 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { 518 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) {
509 DVLOG(20) << "Loop iteration " << i; 519 DVLOG(20) << "Loop iteration " << i;
510 520
511 mock_download_file = new NiceMock<MockDownloadFile>; 521 mock_download_file = new NiceMock<MockDownloadFile>;
512 download_file.reset(mock_download_file); 522 download_file.reset(mock_download_file);
513 mock_request_handle = new NiceMock<MockRequestHandle>; 523 mock_request_handle = new NiceMock<MockRequestHandle>;
514 request_handle.reset(mock_request_handle); 524 request_handle.reset(mock_request_handle);
515 525
516 ON_CALL(*mock_download_file, FullPath()) 526 ON_CALL(*mock_download_file, FullPath())
517 .WillByDefault(Return(base::FilePath())); 527 .WillByDefault(Return(base::FilePath()));
518 528
519 // It's too complicated to set up a WebContents instance that would cause
520 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function
521 // to be callled, so we simply verify that GetWebContents() is called.
522 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) {
523 EXPECT_CALL(*mock_request_handle, GetWebContents())
524 .WillRepeatedly(Return(static_cast<WebContents*>(NULL)));
525 }
526
527 // Copied key parts of DoIntermediateRename & AddDownloadFileToDownloadItem 529 // Copied key parts of DoIntermediateRename & AddDownloadFileToDownloadItem
528 // to allow for holding onto the request handle. 530 // to allow for holding onto the request handle.
529 item->Start(download_file.Pass(), request_handle.Pass()); 531 item->Start(download_file.Pass(), request_handle.Pass());
530 RunAllPendingInMessageLoops(); 532 RunAllPendingInMessageLoops();
531 if (i == 0) { 533 if (i == 0) {
532 // Target determination is only done the first time through. 534 // Target determination is only done the first time through.
533 base::FilePath target_path(kDummyPath); 535 base::FilePath target_path(kDummyPath);
534 base::FilePath intermediate_path( 536 base::FilePath intermediate_path(
535 target_path.InsertBeforeExtensionASCII("x")); 537 target_path.InsertBeforeExtensionASCII("x"));
536 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _)) 538 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _))
(...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after
1292 base::Unretained(&returned_path))); 1294 base::Unretained(&returned_path)));
1293 RunAllPendingInMessageLoops(); 1295 RunAllPendingInMessageLoops();
1294 EXPECT_TRUE(returned_path.empty()); 1296 EXPECT_TRUE(returned_path.empty());
1295 } 1297 }
1296 1298
1297 TEST(MockDownloadItem, Compiles) { 1299 TEST(MockDownloadItem, Compiles) {
1298 MockDownloadItem mock_item; 1300 MockDownloadItem mock_item;
1299 } 1301 }
1300 1302
1301 } // namespace content 1303 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698