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
3 // found in the LICENSE file.
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "build/build_config.h"
18 #include "content/browser/byte_stream.h"
19 #include "content/browser/download/download_create_info.h"
20 #include "content/browser/download/download_file_factory.h"
21 #include "content/browser/download/download_item_factory.h"
22 #include "content/browser/download/download_item_impl.h"
23 #include "content/browser/download/download_item_impl_delegate.h"
24 #include "content/browser/download/download_manager_impl.h"
25 #include "content/browser/download/download_request_handle.h"
26 #include "content/browser/download/mock_download_file.h"
27 #include "content/public/browser/browser_context.h"
28 #include "content/public/browser/download_interrupt_reasons.h"
29 #include "content/public/browser/download_item.h"
30 #include "content/public/browser/download_manager_delegate.h"
31 #include "content/public/browser/zoom_level_delegate.h"
32 #include "content/public/test/mock_download_item.h"
33 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread.h"
35 #include "net/base/net_util.h"
36 #include "net/log/net_log.h"
37 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gmock_mutant.h"
39 #include "testing/gtest/include/gtest/gtest.h"
41 using ::testing::AllOf
;
42 using ::testing::DoAll
;
45 using ::testing::Return
;
46 using ::testing::ReturnRef
;
47 using ::testing::SetArgPointee
;
48 using ::testing::StrictMock
;
51 ACTION_TEMPLATE(RunCallback
,
52 HAS_1_TEMPLATE_PARAMS(int, k
),
53 AND_1_VALUE_PARAMS(p0
)) {
54 return ::std::tr1::get
<k
>(args
).Run(p0
);
58 class ByteStreamReader
;
62 // Matches a DownloadCreateInfo* that points to the same object as |info| and
63 // has a |default_download_directory| that matches |download_directory|.
64 MATCHER_P2(DownloadCreateInfoWithDefaultPath
, info
, download_directory
, "") {
66 arg
->default_download_directory
== download_directory
;
69 class MockDownloadItemImpl
: public DownloadItemImpl
{
71 // Use history constructor for minimal base object.
72 explicit MockDownloadItemImpl(DownloadItemImplDelegate
* delegate
)
75 content::DownloadItem::kInvalidId
,
80 "application/octet-stream",
81 "application/octet-stream",
88 DownloadItem::COMPLETE
,
89 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
,
90 DOWNLOAD_INTERRUPT_REASON_NONE
,
92 net::BoundNetLog()) {}
93 virtual ~MockDownloadItemImpl() {}
95 MOCK_METHOD4(OnDownloadTargetDetermined
,
96 void(const base::FilePath
&, TargetDisposition
,
97 DownloadDangerType
, const base::FilePath
&));
98 MOCK_METHOD1(AddObserver
, void(DownloadItem::Observer
*));
99 MOCK_METHOD1(RemoveObserver
, void(DownloadItem::Observer
*));
100 MOCK_METHOD0(UpdateObservers
, void());
101 MOCK_METHOD0(CanShowInFolder
, bool());
102 MOCK_METHOD0(CanOpenDownload
, bool());
103 MOCK_METHOD0(ShouldOpenFileBasedOnExtension
, bool());
104 MOCK_METHOD0(OpenDownload
, void());
105 MOCK_METHOD0(ShowDownloadInShell
, void());
106 MOCK_METHOD0(ValidateDangerousDownload
, void());
107 MOCK_METHOD1(StealDangerousDownload
, void(const AcquireFileCallback
&));
108 MOCK_METHOD3(UpdateProgress
, void(int64
, int64
, const std::string
&));
109 MOCK_METHOD1(Cancel
, void(bool));
110 MOCK_METHOD0(MarkAsComplete
, void());
111 MOCK_METHOD1(OnAllDataSaved
, void(const std::string
&));
112 MOCK_METHOD0(OnDownloadedFileRemoved
, void());
113 void Start(scoped_ptr
<DownloadFile
> download_file
,
114 scoped_ptr
<DownloadRequestHandleInterface
> req_handle
) override
{
115 MockStart(download_file
.get(), req_handle
.get());
118 MOCK_METHOD2(MockStart
, void(DownloadFile
*, DownloadRequestHandleInterface
*));
120 MOCK_METHOD0(Remove
, void());
121 MOCK_CONST_METHOD1(TimeRemaining
, bool(base::TimeDelta
*));
122 MOCK_CONST_METHOD0(CurrentSpeed
, int64());
123 MOCK_CONST_METHOD0(PercentComplete
, int());
124 MOCK_CONST_METHOD0(AllDataSaved
, bool());
125 MOCK_CONST_METHOD1(MatchesQuery
, bool(const base::string16
& query
));
126 MOCK_CONST_METHOD0(IsDone
, bool());
127 MOCK_CONST_METHOD0(GetFullPath
, const base::FilePath
&());
128 MOCK_CONST_METHOD0(GetTargetFilePath
, const base::FilePath
&());
129 MOCK_CONST_METHOD0(GetTargetDisposition
, TargetDisposition());
130 MOCK_METHOD1(OnContentCheckCompleted
, void(DownloadDangerType
));
131 MOCK_CONST_METHOD0(GetState
, DownloadState());
132 MOCK_CONST_METHOD0(GetUrlChain
, const std::vector
<GURL
>&());
133 MOCK_METHOD1(SetTotalBytes
, void(int64
));
134 MOCK_CONST_METHOD0(GetURL
, const GURL
&());
135 MOCK_CONST_METHOD0(GetOriginalUrl
, const GURL
&());
136 MOCK_CONST_METHOD0(GetReferrerUrl
, const GURL
&());
137 MOCK_CONST_METHOD0(GetTabUrl
, const GURL
&());
138 MOCK_CONST_METHOD0(GetTabReferrerUrl
, const GURL
&());
139 MOCK_CONST_METHOD0(GetSuggestedFilename
, std::string());
140 MOCK_CONST_METHOD0(GetContentDisposition
, std::string());
141 MOCK_CONST_METHOD0(GetMimeType
, std::string());
142 MOCK_CONST_METHOD0(GetOriginalMimeType
, std::string());
143 MOCK_CONST_METHOD0(GetReferrerCharset
, std::string());
144 MOCK_CONST_METHOD0(GetRemoteAddress
, std::string());
145 MOCK_CONST_METHOD0(GetTotalBytes
, int64());
146 MOCK_CONST_METHOD0(GetReceivedBytes
, int64());
147 MOCK_CONST_METHOD0(GetHashState
, const std::string
&());
148 MOCK_CONST_METHOD0(GetHash
, const std::string
&());
149 MOCK_CONST_METHOD0(GetId
, uint32());
150 MOCK_CONST_METHOD0(GetStartTime
, base::Time());
151 MOCK_CONST_METHOD0(GetEndTime
, base::Time());
152 MOCK_METHOD0(GetDownloadManager
, DownloadManager
*());
153 MOCK_CONST_METHOD0(IsPaused
, bool());
154 MOCK_CONST_METHOD0(GetOpenWhenComplete
, bool());
155 MOCK_METHOD1(SetOpenWhenComplete
, void(bool));
156 MOCK_CONST_METHOD0(GetFileExternallyRemoved
, bool());
157 MOCK_CONST_METHOD0(GetDangerType
, DownloadDangerType());
158 MOCK_CONST_METHOD0(IsDangerous
, bool());
159 MOCK_METHOD0(GetAutoOpened
, bool());
160 MOCK_CONST_METHOD0(GetForcedFilePath
, const base::FilePath
&());
161 MOCK_CONST_METHOD0(HasUserGesture
, bool());
162 MOCK_CONST_METHOD0(GetTransitionType
, ui::PageTransition());
163 MOCK_CONST_METHOD0(IsTemporary
, bool());
164 MOCK_METHOD1(SetIsTemporary
, void(bool));
165 MOCK_METHOD1(SetOpened
, void(bool));
166 MOCK_CONST_METHOD0(GetOpened
, bool());
167 MOCK_CONST_METHOD0(GetLastModifiedTime
, const std::string
&());
168 MOCK_CONST_METHOD0(GetETag
, const std::string
&());
169 MOCK_CONST_METHOD0(GetLastReason
, DownloadInterruptReason());
170 MOCK_CONST_METHOD0(GetBrowserContext
, BrowserContext
*());
171 MOCK_CONST_METHOD0(GetWebContents
, WebContents
*());
172 MOCK_CONST_METHOD0(GetFileNameToReportUser
, base::FilePath());
173 MOCK_METHOD1(SetDisplayName
, void(const base::FilePath
&));
174 MOCK_METHOD0(NotifyRemoved
, void());
175 // May be called when vlog is on.
176 std::string
DebugString(bool verbose
) const override
{
177 return std::string();
181 class MockDownloadManagerDelegate
: public DownloadManagerDelegate
{
183 MockDownloadManagerDelegate();
184 virtual ~MockDownloadManagerDelegate();
186 MOCK_METHOD0(Shutdown
, void());
187 MOCK_METHOD1(GetNextId
, void(const DownloadIdCallback
&));
188 MOCK_METHOD2(DetermineDownloadTarget
,
189 bool(DownloadItem
* item
,
190 const DownloadTargetCallback
&));
191 MOCK_METHOD1(ShouldOpenFileBasedOnExtension
, bool(const base::FilePath
&));
192 MOCK_METHOD2(ShouldCompleteDownload
,
193 bool(DownloadItem
*, const base::Closure
&));
194 MOCK_METHOD2(ShouldOpenDownload
,
195 bool(DownloadItem
*, const DownloadOpenDelayedCallback
&));
196 MOCK_METHOD0(GenerateFileHash
, bool());
197 MOCK_METHOD4(GetSaveDir
, void(BrowserContext
*,
198 base::FilePath
*, base::FilePath
*, bool*));
199 MOCK_METHOD5(ChooseSavePath
, void(
200 WebContents
*, const base::FilePath
&, const base::FilePath::StringType
&,
201 bool, const SavePackagePathPickedCallback
&));
202 MOCK_CONST_METHOD0(ApplicationClientIdForFileScanning
, std::string());
205 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
207 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
209 class MockDownloadItemFactory
210 : public DownloadItemFactory
,
211 public base::SupportsWeakPtr
<MockDownloadItemFactory
> {
213 MockDownloadItemFactory();
214 ~MockDownloadItemFactory() override
;
216 // Access to map of created items.
217 // TODO(rdsmith): Could add type (save page, persisted, etc.)
218 // functionality if it's ever needed by consumers.
220 // Returns NULL if no item of that id is present.
221 MockDownloadItemImpl
* GetItem(int id
);
223 // Remove and return an item made by the factory.
224 // Generally used during teardown.
225 MockDownloadItemImpl
* PopItem();
227 // Should be called when the item of this id is removed so that
228 // we don't keep dangling pointers.
229 void RemoveItem(int id
);
231 // Overridden methods from DownloadItemFactory.
232 DownloadItemImpl
* CreatePersistedItem(
233 DownloadItemImplDelegate
* delegate
,
235 const base::FilePath
& current_path
,
236 const base::FilePath
& target_path
,
237 const std::vector
<GURL
>& url_chain
,
238 const GURL
& referrer_url
,
239 const std::string
& mime_type
,
240 const std::string
& original_mime_type
,
241 const base::Time
& start_time
,
242 const base::Time
& end_time
,
243 const std::string
& etag
,
244 const std::string
& last_modofied
,
245 int64 received_bytes
,
247 DownloadItem::DownloadState state
,
248 DownloadDangerType danger_type
,
249 DownloadInterruptReason interrupt_reason
,
251 const net::BoundNetLog
& bound_net_log
) override
;
252 DownloadItemImpl
* CreateActiveItem(
253 DownloadItemImplDelegate
* delegate
,
255 const DownloadCreateInfo
& info
,
256 const net::BoundNetLog
& bound_net_log
) override
;
257 DownloadItemImpl
* CreateSavePageItem(
258 DownloadItemImplDelegate
* delegate
,
260 const base::FilePath
& path
,
262 const std::string
& mime_type
,
263 scoped_ptr
<DownloadRequestHandleInterface
> request_handle
,
264 const net::BoundNetLog
& bound_net_log
) override
;
267 std::map
<uint32
, MockDownloadItemImpl
*> items_
;
268 DownloadItemImplDelegate item_delegate_
;
270 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory
);
273 MockDownloadItemFactory::MockDownloadItemFactory() {}
275 MockDownloadItemFactory::~MockDownloadItemFactory() {}
277 MockDownloadItemImpl
* MockDownloadItemFactory::GetItem(int id
) {
278 if (items_
.find(id
) == items_
.end())
283 MockDownloadItemImpl
* MockDownloadItemFactory::PopItem() {
287 std::map
<uint32
, MockDownloadItemImpl
*>::iterator first_item
289 MockDownloadItemImpl
* result
= first_item
->second
;
290 items_
.erase(first_item
);
294 void MockDownloadItemFactory::RemoveItem(int id
) {
295 DCHECK(items_
.find(id
) != items_
.end());
299 DownloadItemImpl
* MockDownloadItemFactory::CreatePersistedItem(
300 DownloadItemImplDelegate
* delegate
,
302 const base::FilePath
& current_path
,
303 const base::FilePath
& target_path
,
304 const std::vector
<GURL
>& url_chain
,
305 const GURL
& referrer_url
,
306 const std::string
& mime_type
,
307 const std::string
& original_mime_type
,
308 const base::Time
& start_time
,
309 const base::Time
& end_time
,
310 const std::string
& etag
,
311 const std::string
& last_modified
,
312 int64 received_bytes
,
314 DownloadItem::DownloadState state
,
315 DownloadDangerType danger_type
,
316 DownloadInterruptReason interrupt_reason
,
318 const net::BoundNetLog
& bound_net_log
) {
319 DCHECK(items_
.find(download_id
) == items_
.end());
320 MockDownloadItemImpl
* result
=
321 new StrictMock
<MockDownloadItemImpl
>(&item_delegate_
);
322 EXPECT_CALL(*result
, GetId())
323 .WillRepeatedly(Return(download_id
));
324 items_
[download_id
] = result
;
328 DownloadItemImpl
* MockDownloadItemFactory::CreateActiveItem(
329 DownloadItemImplDelegate
* delegate
,
331 const DownloadCreateInfo
& info
,
332 const net::BoundNetLog
& bound_net_log
) {
333 DCHECK(items_
.find(download_id
) == items_
.end());
335 MockDownloadItemImpl
* result
=
336 new StrictMock
<MockDownloadItemImpl
>(&item_delegate_
);
337 EXPECT_CALL(*result
, GetId())
338 .WillRepeatedly(Return(download_id
));
339 items_
[download_id
] = result
;
341 // Active items are created and then immediately are called to start
343 EXPECT_CALL(*result
, MockStart(_
, _
));
348 DownloadItemImpl
* MockDownloadItemFactory::CreateSavePageItem(
349 DownloadItemImplDelegate
* delegate
,
351 const base::FilePath
& path
,
353 const std::string
& mime_type
,
354 scoped_ptr
<DownloadRequestHandleInterface
> request_handle
,
355 const net::BoundNetLog
& bound_net_log
) {
356 DCHECK(items_
.find(download_id
) == items_
.end());
358 MockDownloadItemImpl
* result
=
359 new StrictMock
<MockDownloadItemImpl
>(&item_delegate_
);
360 EXPECT_CALL(*result
, GetId())
361 .WillRepeatedly(Return(download_id
));
362 items_
[download_id
] = result
;
367 class MockDownloadFileFactory
368 : public DownloadFileFactory
,
369 public base::SupportsWeakPtr
<MockDownloadFileFactory
> {
371 MockDownloadFileFactory() {}
372 virtual ~MockDownloadFileFactory() {}
374 // Overridden method from DownloadFileFactory
375 MOCK_METHOD8(MockCreateFile
, MockDownloadFile
*(
376 const DownloadSaveInfo
&,
377 const base::FilePath
&,
378 const GURL
&, const GURL
&, bool,
380 const net::BoundNetLog
&,
381 base::WeakPtr
<DownloadDestinationObserver
>));
383 virtual DownloadFile
* CreateFile(
384 scoped_ptr
<DownloadSaveInfo
> save_info
,
385 const base::FilePath
& default_download_directory
,
387 const GURL
& referrer_url
,
389 scoped_ptr
<ByteStreamReader
> stream
,
390 const net::BoundNetLog
& bound_net_log
,
391 base::WeakPtr
<DownloadDestinationObserver
> observer
) {
392 return MockCreateFile(*save_info
.get(), default_download_directory
, url
,
393 referrer_url
, calculate_hash
,
394 stream
.get(), bound_net_log
, observer
);
398 class MockBrowserContext
: public BrowserContext
{
400 MockBrowserContext() {}
401 ~MockBrowserContext() {}
403 MOCK_CONST_METHOD0(GetPath
, base::FilePath());
404 MOCK_METHOD1(CreateZoomLevelDelegateMock
,
405 ZoomLevelDelegate
*(const base::FilePath
&));
406 MOCK_CONST_METHOD0(IsOffTheRecord
, bool());
407 MOCK_METHOD0(GetRequestContext
, net::URLRequestContextGetter
*());
408 MOCK_METHOD1(GetRequestContextForRenderProcess
,
409 net::URLRequestContextGetter
*(int renderer_child_id
));
410 MOCK_METHOD0(GetMediaRequestContext
,
411 net::URLRequestContextGetter
*());
412 MOCK_METHOD1(GetMediaRequestContextForRenderProcess
,
413 net::URLRequestContextGetter
*(int renderer_child_id
));
414 MOCK_METHOD2(GetMediaRequestContextForStoragePartition
,
415 net::URLRequestContextGetter
*(
416 const base::FilePath
& partition_path
, bool in_memory
));
417 MOCK_METHOD0(GetResourceContext
, ResourceContext
*());
418 MOCK_METHOD0(GetDownloadManagerDelegate
, DownloadManagerDelegate
*());
419 MOCK_METHOD0(GetGuestManager
, BrowserPluginGuestManager
* ());
420 MOCK_METHOD0(GetSpecialStoragePolicy
, storage::SpecialStoragePolicy
*());
421 MOCK_METHOD0(GetPushMessagingService
, PushMessagingService
*());
422 MOCK_METHOD0(GetSSLHostStateDelegate
, SSLHostStateDelegate
*());
423 MOCK_METHOD0(GetPermissionManager
, PermissionManager
*());
425 scoped_ptr
<ZoomLevelDelegate
> CreateZoomLevelDelegate(
426 const base::FilePath
& path
) override
{
427 return scoped_ptr
<ZoomLevelDelegate
>(CreateZoomLevelDelegateMock(path
));
431 class MockDownloadManagerObserver
: public DownloadManager::Observer
{
433 MockDownloadManagerObserver() {}
434 ~MockDownloadManagerObserver() {}
435 MOCK_METHOD2(OnDownloadCreated
, void(
436 DownloadManager
*, DownloadItem
*));
437 MOCK_METHOD1(ManagerGoingDown
, void(DownloadManager
*));
438 MOCK_METHOD2(SelectFileDialogDisplayed
, void(
439 DownloadManager
*, int32
));
444 class DownloadManagerTest
: public testing::Test
{
446 static const char* kTestData
;
447 static const size_t kTestDataLen
;
449 DownloadManagerTest()
450 : callback_called_(false),
451 ui_thread_(BrowserThread::UI
, &message_loop_
),
452 file_thread_(BrowserThread::FILE, &message_loop_
),
453 next_download_id_(0) {
456 // We tear down everything in TearDown().
457 ~DownloadManagerTest() override
{}
459 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
460 // then create a DownloadManager that points
462 void SetUp() override
{
463 DCHECK(!download_manager_
);
465 mock_download_item_factory_
= (new MockDownloadItemFactory())->AsWeakPtr();
466 mock_download_file_factory_
= (new MockDownloadFileFactory())->AsWeakPtr();
467 mock_download_manager_delegate_
.reset(
468 new StrictMock
<MockDownloadManagerDelegate
>);
469 EXPECT_CALL(*mock_download_manager_delegate_
.get(), Shutdown())
471 mock_browser_context_
.reset(new StrictMock
<MockBrowserContext
>);
472 EXPECT_CALL(*mock_browser_context_
.get(), IsOffTheRecord())
473 .WillRepeatedly(Return(false));
475 download_manager_
.reset(new DownloadManagerImpl(
476 NULL
, mock_browser_context_
.get()));
477 download_manager_
->SetDownloadItemFactoryForTesting(
478 scoped_ptr
<DownloadItemFactory
>(
479 mock_download_item_factory_
.get()).Pass());
480 download_manager_
->SetDownloadFileFactoryForTesting(
481 scoped_ptr
<DownloadFileFactory
>(
482 mock_download_file_factory_
.get()).Pass());
483 observer_
.reset(new MockDownloadManagerObserver());
484 download_manager_
->AddObserver(observer_
.get());
485 download_manager_
->SetDelegate(mock_download_manager_delegate_
.get());
488 void TearDown() override
{
489 while (MockDownloadItemImpl
*
490 item
= mock_download_item_factory_
->PopItem()) {
491 EXPECT_CALL(*item
, GetState())
492 .WillOnce(Return(DownloadItem::CANCELLED
));
494 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_
.get()))
497 download_manager_
->Shutdown();
498 download_manager_
.reset();
499 message_loop_
.RunUntilIdle();
500 ASSERT_EQ(NULL
, mock_download_item_factory_
.get());
501 ASSERT_EQ(NULL
, mock_download_file_factory_
.get());
502 message_loop_
.RunUntilIdle();
503 mock_download_manager_delegate_
.reset();
504 mock_browser_context_
.reset();
507 // Returns download id.
508 MockDownloadItemImpl
& AddItemToManager() {
509 DownloadCreateInfo info
;
511 // Args are ignored except for download id, so everything else can be
513 uint32 id
= next_download_id_
;
515 info
.request_handle
= DownloadRequestHandle();
516 download_manager_
->CreateActiveItem(id
, info
);
517 DCHECK(mock_download_item_factory_
->GetItem(id
));
518 MockDownloadItemImpl
& item(*mock_download_item_factory_
->GetItem(id
));
519 // Satisfy expectation. If the item is created in StartDownload(),
520 // we call Start on it immediately, so we need to set that expectation
522 scoped_ptr
<DownloadRequestHandleInterface
> req_handle
;
523 item
.Start(scoped_ptr
<DownloadFile
>(), req_handle
.Pass());
528 MockDownloadItemImpl
& GetMockDownloadItem(int id
) {
529 MockDownloadItemImpl
* itemp
= mock_download_item_factory_
->GetItem(id
);
535 void RemoveMockDownloadItem(int id
) {
536 // Owned by DownloadManager; should be deleted there.
537 mock_download_item_factory_
->RemoveItem(id
);
540 MockDownloadManagerDelegate
& GetMockDownloadManagerDelegate() {
541 return *mock_download_manager_delegate_
;
544 MockDownloadManagerObserver
& GetMockObserver() {
548 void DownloadTargetDeterminedCallback(
549 const base::FilePath
& target_path
,
550 DownloadItem::TargetDisposition disposition
,
551 DownloadDangerType danger_type
,
552 const base::FilePath
& intermediate_path
) {
553 callback_called_
= true;
554 target_path_
= target_path
;
555 target_disposition_
= disposition
;
556 danger_type_
= danger_type
;
557 intermediate_path_
= intermediate_path
;
560 void DetermineDownloadTarget(DownloadItemImpl
* item
) {
561 download_manager_
->DetermineDownloadTarget(
563 &DownloadManagerTest::DownloadTargetDeterminedCallback
,
564 base::Unretained(this)));
568 // Key test variable; we'll keep it available to sub-classes.
569 scoped_ptr
<DownloadManagerImpl
> download_manager_
;
570 base::WeakPtr
<MockDownloadFileFactory
> mock_download_file_factory_
;
572 // Target detetermined callback.
573 bool callback_called_
;
574 base::FilePath target_path_
;
575 DownloadItem::TargetDisposition target_disposition_
;
576 DownloadDangerType danger_type_
;
577 base::FilePath intermediate_path_
;
580 base::MessageLoopForUI message_loop_
;
581 TestBrowserThread ui_thread_
;
582 TestBrowserThread file_thread_
;
583 base::WeakPtr
<MockDownloadItemFactory
> mock_download_item_factory_
;
584 scoped_ptr
<MockDownloadManagerDelegate
> mock_download_manager_delegate_
;
585 scoped_ptr
<MockBrowserContext
> mock_browser_context_
;
586 scoped_ptr
<MockDownloadManagerObserver
> observer_
;
587 uint32 next_download_id_
;
589 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest
);
592 // Confirm the appropriate invocations occur when you start a download.
593 TEST_F(DownloadManagerTest
, StartDownload
) {
594 scoped_ptr
<DownloadCreateInfo
> info(new DownloadCreateInfo
);
595 scoped_ptr
<ByteStreamReader
> stream
;
596 uint32
local_id(5); // Random value
597 base::FilePath
download_path(FILE_PATH_LITERAL("download/path"));
599 EXPECT_FALSE(download_manager_
->GetDownload(local_id
));
601 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_
.get(), _
))
603 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_
))
604 .WillOnce(RunCallback
<0>(local_id
));
606 // Doing nothing will set the default download directory to null.
607 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_
, _
, _
, _
));
608 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
609 .WillOnce(Return(true));
610 EXPECT_CALL(GetMockDownloadManagerDelegate(),
611 ApplicationClientIdForFileScanning())
612 .WillRepeatedly(Return("client-id"));
613 MockDownloadFile
* mock_file
= new MockDownloadFile
;
614 EXPECT_CALL(*mock_file
, SetClientGuid("client-id"));
615 EXPECT_CALL(*mock_download_file_factory_
.get(),
616 MockCreateFile(Ref(*info
->save_info
.get()), _
, _
, _
, true,
618 .WillOnce(Return(mock_file
));
620 download_manager_
->StartDownload(
621 info
.Pass(), stream
.Pass(), DownloadUrlParameters::OnStartedCallback());
622 EXPECT_TRUE(download_manager_
->GetDownload(local_id
));
625 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
627 TEST_F(DownloadManagerTest
, DetermineDownloadTarget_True
) {
628 // Put a mock we have a handle to on the download manager.
629 MockDownloadItemImpl
& item(AddItemToManager());
630 EXPECT_CALL(item
, GetState())
631 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS
));
633 EXPECT_CALL(GetMockDownloadManagerDelegate(),
634 DetermineDownloadTarget(&item
, _
))
635 .WillOnce(Return(true));
636 DetermineDownloadTarget(&item
);
639 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
640 // allows starting. This also tests OnDownloadTargetDetermined.
641 TEST_F(DownloadManagerTest
, DetermineDownloadTarget_False
) {
642 // Put a mock we have a handle to on the download manager.
643 MockDownloadItemImpl
& item(AddItemToManager());
645 base::FilePath
path(FILE_PATH_LITERAL("random_filepath.txt"));
646 EXPECT_CALL(GetMockDownloadManagerDelegate(),
647 DetermineDownloadTarget(&item
, _
))
648 .WillOnce(Return(false));
649 EXPECT_CALL(item
, GetForcedFilePath())
650 .WillOnce(ReturnRef(path
));
652 // Confirm that the callback was called with the right values in this case.
653 callback_called_
= false;
654 DetermineDownloadTarget(&item
);
655 EXPECT_TRUE(callback_called_
);
656 EXPECT_EQ(path
, target_path_
);
657 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE
, target_disposition_
);
658 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
, danger_type_
);
659 EXPECT_EQ(path
, intermediate_path_
);
662 // Confirm the DownloadManagerImpl::RemoveAllDownloads() functionality
663 TEST_F(DownloadManagerTest
, RemoveAllDownloads
) {
664 base::Time
now(base::Time::Now());
665 for (uint32 i
= 0; i
< 4; ++i
) {
666 MockDownloadItemImpl
& item(AddItemToManager());
667 EXPECT_EQ(i
, item
.GetId());
668 EXPECT_CALL(item
, GetStartTime())
669 .WillRepeatedly(Return(now
));
672 // Specify states for each.
673 EXPECT_CALL(GetMockDownloadItem(0), GetState())
674 .WillRepeatedly(Return(DownloadItem::COMPLETE
));
675 EXPECT_CALL(GetMockDownloadItem(1), GetState())
676 .WillRepeatedly(Return(DownloadItem::CANCELLED
));
677 EXPECT_CALL(GetMockDownloadItem(2), GetState())
678 .WillRepeatedly(Return(DownloadItem::INTERRUPTED
));
679 EXPECT_CALL(GetMockDownloadItem(3), GetState())
680 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS
));
682 // Expectations for whether or not they'll actually be removed.
683 EXPECT_CALL(GetMockDownloadItem(0), Remove())
685 EXPECT_CALL(GetMockDownloadItem(1), Remove())
687 EXPECT_CALL(GetMockDownloadItem(2), Remove())
689 EXPECT_CALL(GetMockDownloadItem(3), Remove())
692 download_manager_
->RemoveAllDownloads();
693 // Because we're mocking the download item, the Remove call doesn't
694 // result in them being removed from the DownloadManager list.
697 } // namespace content