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/test/mock_download_item.h"
32 #include "content/public/test/test_browser_context.h"
33 #include "content/public/test/test_browser_thread.h"
34 #include "net/base/net_log.h"
35 #include "net/base/net_util.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gmock_mutant.h"
38 #include "testing/gtest/include/gtest/gtest.h"
40 using ::testing::AllOf
;
41 using ::testing::DoAll
;
44 using ::testing::Return
;
45 using ::testing::ReturnRef
;
46 using ::testing::SetArgPointee
;
47 using ::testing::StrictMock
;
50 ACTION_TEMPLATE(RunCallback
,
51 HAS_1_TEMPLATE_PARAMS(int, k
),
52 AND_1_VALUE_PARAMS(p0
)) {
53 return ::std::tr1::get
<k
>(args
).Run(p0
);
57 class ByteStreamReader
;
61 // Matches a DownloadCreateInfo* that points to the same object as |info| and
62 // has a |default_download_directory| that matches |download_directory|.
63 MATCHER_P2(DownloadCreateInfoWithDefaultPath
, info
, download_directory
, "") {
65 arg
->default_download_directory
== download_directory
;
68 class MockDownloadItemImpl
: public DownloadItemImpl
{
70 // Use history constructor for minimal base object.
71 explicit MockDownloadItemImpl(DownloadItemImplDelegate
* delegate
)
74 content::DownloadItem::kInvalidId
,
85 DownloadItem::COMPLETE
,
86 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
,
87 DOWNLOAD_INTERRUPT_REASON_NONE
,
89 net::BoundNetLog()) {}
90 virtual ~MockDownloadItemImpl() {}
92 MOCK_METHOD4(OnDownloadTargetDetermined
,
93 void(const base::FilePath
&, TargetDisposition
,
94 DownloadDangerType
, const base::FilePath
&));
95 MOCK_METHOD1(AddObserver
, void(DownloadItem::Observer
*));
96 MOCK_METHOD1(RemoveObserver
, void(DownloadItem::Observer
*));
97 MOCK_METHOD0(UpdateObservers
, void());
98 MOCK_METHOD0(CanShowInFolder
, bool());
99 MOCK_METHOD0(CanOpenDownload
, bool());
100 MOCK_METHOD0(ShouldOpenFileBasedOnExtension
, bool());
101 MOCK_METHOD0(OpenDownload
, void());
102 MOCK_METHOD0(ShowDownloadInShell
, void());
103 MOCK_METHOD0(ValidateDangerousDownload
, void());
104 MOCK_METHOD1(StealDangerousDownload
, void(const AcquireFileCallback
&));
105 MOCK_METHOD3(UpdateProgress
, void(int64
, int64
, const std::string
&));
106 MOCK_METHOD1(Cancel
, void(bool));
107 MOCK_METHOD0(MarkAsComplete
, void());
108 MOCK_METHOD1(OnAllDataSaved
, void(const std::string
&));
109 MOCK_METHOD0(OnDownloadedFileRemoved
, void());
111 scoped_ptr
<DownloadFile
> download_file
,
112 scoped_ptr
<DownloadRequestHandleInterface
> req_handle
) OVERRIDE
{
113 MockStart(download_file
.get(), req_handle
.get());
116 MOCK_METHOD2(MockStart
, void(DownloadFile
*, DownloadRequestHandleInterface
*));
118 MOCK_METHOD0(Remove
, void());
119 MOCK_CONST_METHOD1(TimeRemaining
, bool(base::TimeDelta
*));
120 MOCK_CONST_METHOD0(CurrentSpeed
, int64());
121 MOCK_CONST_METHOD0(PercentComplete
, int());
122 MOCK_CONST_METHOD0(AllDataSaved
, bool());
123 MOCK_CONST_METHOD1(MatchesQuery
, bool(const string16
& query
));
124 MOCK_CONST_METHOD0(IsDone
, bool());
125 MOCK_CONST_METHOD0(GetFullPath
, const base::FilePath
&());
126 MOCK_CONST_METHOD0(GetTargetFilePath
, const base::FilePath
&());
127 MOCK_CONST_METHOD0(GetTargetDisposition
, TargetDisposition());
128 MOCK_METHOD1(OnContentCheckCompleted
, void(DownloadDangerType
));
129 MOCK_CONST_METHOD0(GetState
, DownloadState());
130 MOCK_CONST_METHOD0(GetUrlChain
, const std::vector
<GURL
>&());
131 MOCK_METHOD1(SetTotalBytes
, void(int64
));
132 MOCK_CONST_METHOD0(GetURL
, const GURL
&());
133 MOCK_CONST_METHOD0(GetOriginalUrl
, const GURL
&());
134 MOCK_CONST_METHOD0(GetReferrerUrl
, const GURL
&());
135 MOCK_CONST_METHOD0(GetSuggestedFilename
, std::string());
136 MOCK_CONST_METHOD0(GetContentDisposition
, std::string());
137 MOCK_CONST_METHOD0(GetMimeType
, std::string());
138 MOCK_CONST_METHOD0(GetOriginalMimeType
, std::string());
139 MOCK_CONST_METHOD0(GetReferrerCharset
, std::string());
140 MOCK_CONST_METHOD0(GetRemoteAddress
, std::string());
141 MOCK_CONST_METHOD0(GetTotalBytes
, int64());
142 MOCK_CONST_METHOD0(GetReceivedBytes
, int64());
143 MOCK_CONST_METHOD0(GetHashState
, const std::string
&());
144 MOCK_CONST_METHOD0(GetHash
, const std::string
&());
145 MOCK_CONST_METHOD0(GetId
, uint32());
146 MOCK_CONST_METHOD0(GetStartTime
, base::Time());
147 MOCK_CONST_METHOD0(GetEndTime
, base::Time());
148 MOCK_METHOD0(GetDownloadManager
, DownloadManager
*());
149 MOCK_CONST_METHOD0(IsPaused
, bool());
150 MOCK_CONST_METHOD0(GetOpenWhenComplete
, bool());
151 MOCK_METHOD1(SetOpenWhenComplete
, void(bool));
152 MOCK_CONST_METHOD0(GetFileExternallyRemoved
, bool());
153 MOCK_CONST_METHOD0(GetDangerType
, DownloadDangerType());
154 MOCK_CONST_METHOD0(IsDangerous
, bool());
155 MOCK_METHOD0(GetAutoOpened
, bool());
156 MOCK_CONST_METHOD0(GetForcedFilePath
, const base::FilePath
&());
157 MOCK_CONST_METHOD0(HasUserGesture
, bool());
158 MOCK_CONST_METHOD0(GetTransitionType
, PageTransition());
159 MOCK_CONST_METHOD0(IsTemporary
, bool());
160 MOCK_METHOD1(SetIsTemporary
, void(bool));
161 MOCK_METHOD1(SetOpened
, void(bool));
162 MOCK_CONST_METHOD0(GetOpened
, bool());
163 MOCK_CONST_METHOD0(GetLastModifiedTime
, const std::string
&());
164 MOCK_CONST_METHOD0(GetETag
, const std::string
&());
165 MOCK_CONST_METHOD0(GetLastReason
, DownloadInterruptReason());
166 MOCK_CONST_METHOD0(GetBrowserContext
, BrowserContext
*());
167 MOCK_CONST_METHOD0(GetWebContents
, WebContents
*());
168 MOCK_CONST_METHOD0(GetFileNameToReportUser
, base::FilePath());
169 MOCK_METHOD1(SetDisplayName
, void(const base::FilePath
&));
170 MOCK_METHOD0(NotifyRemoved
, void());
171 // May be called when vlog is on.
172 virtual std::string
DebugString(bool verbose
) const OVERRIDE
{
173 return std::string();
177 class MockDownloadManagerDelegate
: public DownloadManagerDelegate
{
179 MockDownloadManagerDelegate();
180 virtual ~MockDownloadManagerDelegate();
182 MOCK_METHOD0(Shutdown
, void());
183 MOCK_METHOD1(GetNextId
, void(const DownloadIdCallback
&));
184 MOCK_METHOD2(DetermineDownloadTarget
,
185 bool(DownloadItem
* item
,
186 const DownloadTargetCallback
&));
187 MOCK_METHOD1(ShouldOpenFileBasedOnExtension
, bool(const base::FilePath
&));
188 MOCK_METHOD2(ShouldCompleteDownload
,
189 bool(DownloadItem
*, const base::Closure
&));
190 MOCK_METHOD2(ShouldOpenDownload
,
191 bool(DownloadItem
*, const DownloadOpenDelayedCallback
&));
192 MOCK_METHOD0(GenerateFileHash
, bool());
193 MOCK_METHOD4(GetSaveDir
, void(BrowserContext
*,
194 base::FilePath
*, base::FilePath
*, bool*));
195 MOCK_METHOD5(ChooseSavePath
, void(
196 WebContents
*, const base::FilePath
&, const base::FilePath::StringType
&,
197 bool, const SavePackagePathPickedCallback
&));
198 MOCK_CONST_METHOD0(ApplicationClientIdForFileScanning
, std::string());
201 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
203 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
205 class MockDownloadItemFactory
206 : public DownloadItemFactory
,
207 public base::SupportsWeakPtr
<MockDownloadItemFactory
> {
209 MockDownloadItemFactory();
210 virtual ~MockDownloadItemFactory();
212 // Access to map of created items.
213 // TODO(rdsmith): Could add type (save page, persisted, etc.)
214 // functionality if it's ever needed by consumers.
216 // Returns NULL if no item of that id is present.
217 MockDownloadItemImpl
* GetItem(int id
);
219 // Remove and return an item made by the factory.
220 // Generally used during teardown.
221 MockDownloadItemImpl
* PopItem();
223 // Should be called when the item of this id is removed so that
224 // we don't keep dangling pointers.
225 void RemoveItem(int id
);
227 // Overridden methods from DownloadItemFactory.
228 virtual DownloadItemImpl
* CreatePersistedItem(
229 DownloadItemImplDelegate
* delegate
,
231 const base::FilePath
& current_path
,
232 const base::FilePath
& target_path
,
233 const std::vector
<GURL
>& url_chain
,
234 const GURL
& referrer_url
,
235 const base::Time
& start_time
,
236 const base::Time
& end_time
,
237 const std::string
& etag
,
238 const std::string
& last_modofied
,
239 int64 received_bytes
,
241 DownloadItem::DownloadState state
,
242 DownloadDangerType danger_type
,
243 DownloadInterruptReason interrupt_reason
,
245 const net::BoundNetLog
& bound_net_log
) OVERRIDE
;
246 virtual DownloadItemImpl
* CreateActiveItem(
247 DownloadItemImplDelegate
* delegate
,
249 const DownloadCreateInfo
& info
,
250 const net::BoundNetLog
& bound_net_log
) OVERRIDE
;
251 virtual DownloadItemImpl
* CreateSavePageItem(
252 DownloadItemImplDelegate
* delegate
,
254 const base::FilePath
& path
,
256 const std::string
& mime_type
,
257 scoped_ptr
<DownloadRequestHandleInterface
> request_handle
,
258 const net::BoundNetLog
& bound_net_log
) OVERRIDE
;
261 std::map
<uint32
, MockDownloadItemImpl
*> items_
;
262 DownloadItemImplDelegate item_delegate_
;
264 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory
);
267 MockDownloadItemFactory::MockDownloadItemFactory() {}
269 MockDownloadItemFactory::~MockDownloadItemFactory() {}
271 MockDownloadItemImpl
* MockDownloadItemFactory::GetItem(int id
) {
272 if (items_
.find(id
) == items_
.end())
277 MockDownloadItemImpl
* MockDownloadItemFactory::PopItem() {
281 std::map
<uint32
, MockDownloadItemImpl
*>::iterator first_item
283 MockDownloadItemImpl
* result
= first_item
->second
;
284 items_
.erase(first_item
);
288 void MockDownloadItemFactory::RemoveItem(int id
) {
289 DCHECK(items_
.find(id
) != items_
.end());
293 DownloadItemImpl
* MockDownloadItemFactory::CreatePersistedItem(
294 DownloadItemImplDelegate
* delegate
,
296 const base::FilePath
& current_path
,
297 const base::FilePath
& target_path
,
298 const std::vector
<GURL
>& url_chain
,
299 const GURL
& referrer_url
,
300 const base::Time
& start_time
,
301 const base::Time
& end_time
,
302 const std::string
& etag
,
303 const std::string
& last_modified
,
304 int64 received_bytes
,
306 DownloadItem::DownloadState state
,
307 DownloadDangerType danger_type
,
308 DownloadInterruptReason interrupt_reason
,
310 const net::BoundNetLog
& bound_net_log
) {
311 DCHECK(items_
.find(download_id
) == items_
.end());
312 MockDownloadItemImpl
* result
=
313 new StrictMock
<MockDownloadItemImpl
>(&item_delegate_
);
314 EXPECT_CALL(*result
, GetId())
315 .WillRepeatedly(Return(download_id
));
316 items_
[download_id
] = result
;
320 DownloadItemImpl
* MockDownloadItemFactory::CreateActiveItem(
321 DownloadItemImplDelegate
* delegate
,
323 const DownloadCreateInfo
& info
,
324 const net::BoundNetLog
& bound_net_log
) {
325 DCHECK(items_
.find(download_id
) == items_
.end());
327 MockDownloadItemImpl
* result
=
328 new StrictMock
<MockDownloadItemImpl
>(&item_delegate_
);
329 EXPECT_CALL(*result
, GetId())
330 .WillRepeatedly(Return(download_id
));
331 items_
[download_id
] = result
;
333 // Active items are created and then immediately are called to start
335 EXPECT_CALL(*result
, MockStart(_
, _
));
340 DownloadItemImpl
* MockDownloadItemFactory::CreateSavePageItem(
341 DownloadItemImplDelegate
* delegate
,
343 const base::FilePath
& path
,
345 const std::string
& mime_type
,
346 scoped_ptr
<DownloadRequestHandleInterface
> request_handle
,
347 const net::BoundNetLog
& bound_net_log
) {
348 DCHECK(items_
.find(download_id
) == items_
.end());
350 MockDownloadItemImpl
* result
=
351 new StrictMock
<MockDownloadItemImpl
>(&item_delegate_
);
352 EXPECT_CALL(*result
, GetId())
353 .WillRepeatedly(Return(download_id
));
354 items_
[download_id
] = result
;
359 class MockDownloadFileFactory
360 : public DownloadFileFactory
,
361 public base::SupportsWeakPtr
<MockDownloadFileFactory
> {
363 MockDownloadFileFactory() {}
364 virtual ~MockDownloadFileFactory() {}
366 // Overridden method from DownloadFileFactory
367 MOCK_METHOD8(MockCreateFile
, MockDownloadFile
*(
368 const DownloadSaveInfo
&,
369 const base::FilePath
&,
370 const GURL
&, const GURL
&, bool,
372 const net::BoundNetLog
&,
373 base::WeakPtr
<DownloadDestinationObserver
>));
375 virtual DownloadFile
* CreateFile(
376 scoped_ptr
<DownloadSaveInfo
> save_info
,
377 const base::FilePath
& default_download_directory
,
379 const GURL
& referrer_url
,
381 scoped_ptr
<ByteStreamReader
> stream
,
382 const net::BoundNetLog
& bound_net_log
,
383 base::WeakPtr
<DownloadDestinationObserver
> observer
) {
384 return MockCreateFile(*save_info
.get(), default_download_directory
, url
,
385 referrer_url
, calculate_hash
,
386 stream
.get(), bound_net_log
, observer
);
390 class MockBrowserContext
: public BrowserContext
{
392 MockBrowserContext() {}
393 ~MockBrowserContext() {}
395 MOCK_CONST_METHOD0(GetPath
, base::FilePath());
396 MOCK_CONST_METHOD0(IsOffTheRecord
, bool());
397 MOCK_METHOD0(GetRequestContext
, net::URLRequestContextGetter
*());
398 MOCK_METHOD1(GetRequestContextForRenderProcess
,
399 net::URLRequestContextGetter
*(int renderer_child_id
));
400 MOCK_METHOD0(GetMediaRequestContext
,
401 net::URLRequestContextGetter
*());
402 MOCK_METHOD1(GetMediaRequestContextForRenderProcess
,
403 net::URLRequestContextGetter
*(int renderer_child_id
));
404 MOCK_METHOD2(GetMediaRequestContextForStoragePartition
,
405 net::URLRequestContextGetter
*(
406 const base::FilePath
& partition_path
, bool in_memory
));
407 MOCK_METHOD4(RequestMIDISysExPermission
,
408 void(int render_process_id
,
410 const GURL
& requesting_frame
,
411 const MIDISysExPermissionCallback
& callback
));
412 MOCK_METHOD0(GetResourceContext
, ResourceContext
*());
413 MOCK_METHOD0(GetDownloadManagerDelegate
, DownloadManagerDelegate
*());
414 MOCK_METHOD0(GetGeolocationPermissionContext
,
415 GeolocationPermissionContext
* ());
416 MOCK_METHOD0(GetSpecialStoragePolicy
, quota::SpecialStoragePolicy
*());
419 class MockDownloadManagerObserver
: public DownloadManager::Observer
{
421 MockDownloadManagerObserver() {}
422 ~MockDownloadManagerObserver() {}
423 MOCK_METHOD2(OnDownloadCreated
, void(
424 DownloadManager
*, DownloadItem
*));
425 MOCK_METHOD1(ManagerGoingDown
, void(DownloadManager
*));
426 MOCK_METHOD2(SelectFileDialogDisplayed
, void(
427 DownloadManager
*, int32
));
432 class DownloadManagerTest
: public testing::Test
{
434 static const char* kTestData
;
435 static const size_t kTestDataLen
;
437 DownloadManagerTest()
438 : callback_called_(false),
439 ui_thread_(BrowserThread::UI
, &message_loop_
),
440 file_thread_(BrowserThread::FILE, &message_loop_
),
441 next_download_id_(0) {
444 // We tear down everything in TearDown().
445 virtual ~DownloadManagerTest() {}
447 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
448 // then create a DownloadManager that points
450 virtual void SetUp() {
451 DCHECK(!download_manager_
);
453 mock_download_item_factory_
= (new MockDownloadItemFactory())->AsWeakPtr();
454 mock_download_file_factory_
= (new MockDownloadFileFactory())->AsWeakPtr();
455 mock_download_manager_delegate_
.reset(
456 new StrictMock
<MockDownloadManagerDelegate
>);
457 EXPECT_CALL(*mock_download_manager_delegate_
.get(), Shutdown())
459 mock_browser_context_
.reset(new StrictMock
<MockBrowserContext
>);
460 EXPECT_CALL(*mock_browser_context_
.get(), IsOffTheRecord())
461 .WillRepeatedly(Return(false));
463 download_manager_
.reset(new DownloadManagerImpl(
464 NULL
, mock_browser_context_
.get()));
465 download_manager_
->SetDownloadItemFactoryForTesting(
466 scoped_ptr
<DownloadItemFactory
>(
467 mock_download_item_factory_
.get()).Pass());
468 download_manager_
->SetDownloadFileFactoryForTesting(
469 scoped_ptr
<DownloadFileFactory
>(
470 mock_download_file_factory_
.get()).Pass());
471 observer_
.reset(new MockDownloadManagerObserver());
472 download_manager_
->AddObserver(observer_
.get());
473 download_manager_
->SetDelegate(mock_download_manager_delegate_
.get());
476 virtual void TearDown() {
477 while (MockDownloadItemImpl
*
478 item
= mock_download_item_factory_
->PopItem()) {
479 EXPECT_CALL(*item
, GetState())
480 .WillOnce(Return(DownloadItem::CANCELLED
));
482 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_
.get()))
485 download_manager_
->Shutdown();
486 download_manager_
.reset();
487 message_loop_
.RunUntilIdle();
488 ASSERT_EQ(NULL
, mock_download_item_factory_
.get());
489 ASSERT_EQ(NULL
, mock_download_file_factory_
.get());
490 message_loop_
.RunUntilIdle();
491 mock_download_manager_delegate_
.reset();
492 mock_browser_context_
.reset();
495 // Returns download id.
496 MockDownloadItemImpl
& AddItemToManager() {
497 DownloadCreateInfo info
;
499 // Args are ignored except for download id, so everything else can be
501 uint32 id
= next_download_id_
;
503 info
.request_handle
= DownloadRequestHandle();
504 download_manager_
->CreateActiveItem(id
, info
);
505 DCHECK(mock_download_item_factory_
->GetItem(id
));
506 MockDownloadItemImpl
& item(*mock_download_item_factory_
->GetItem(id
));
507 // Satisfy expectation. If the item is created in StartDownload(),
508 // we call Start on it immediately, so we need to set that expectation
510 scoped_ptr
<DownloadRequestHandleInterface
> req_handle
;
511 item
.Start(scoped_ptr
<DownloadFile
>(), req_handle
.Pass());
516 MockDownloadItemImpl
& GetMockDownloadItem(int id
) {
517 MockDownloadItemImpl
* itemp
= mock_download_item_factory_
->GetItem(id
);
523 void RemoveMockDownloadItem(int id
) {
524 // Owned by DownloadManager; should be deleted there.
525 mock_download_item_factory_
->RemoveItem(id
);
528 MockDownloadManagerDelegate
& GetMockDownloadManagerDelegate() {
529 return *mock_download_manager_delegate_
;
532 MockDownloadManagerObserver
& GetMockObserver() {
536 void DownloadTargetDeterminedCallback(
537 const base::FilePath
& target_path
,
538 DownloadItem::TargetDisposition disposition
,
539 DownloadDangerType danger_type
,
540 const base::FilePath
& intermediate_path
) {
541 callback_called_
= true;
542 target_path_
= target_path
;
543 target_disposition_
= disposition
;
544 danger_type_
= danger_type
;
545 intermediate_path_
= intermediate_path
;
548 void DetermineDownloadTarget(DownloadItemImpl
* item
) {
549 download_manager_
->DetermineDownloadTarget(
551 &DownloadManagerTest::DownloadTargetDeterminedCallback
,
552 base::Unretained(this)));
556 // Key test variable; we'll keep it available to sub-classes.
557 scoped_ptr
<DownloadManagerImpl
> download_manager_
;
558 base::WeakPtr
<MockDownloadFileFactory
> mock_download_file_factory_
;
560 // Target detetermined callback.
561 bool callback_called_
;
562 base::FilePath target_path_
;
563 DownloadItem::TargetDisposition target_disposition_
;
564 DownloadDangerType danger_type_
;
565 base::FilePath intermediate_path_
;
568 base::MessageLoopForUI message_loop_
;
569 TestBrowserThread ui_thread_
;
570 TestBrowserThread file_thread_
;
571 base::WeakPtr
<MockDownloadItemFactory
> mock_download_item_factory_
;
572 scoped_ptr
<MockDownloadManagerDelegate
> mock_download_manager_delegate_
;
573 scoped_ptr
<MockBrowserContext
> mock_browser_context_
;
574 scoped_ptr
<MockDownloadManagerObserver
> observer_
;
575 uint32 next_download_id_
;
577 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest
);
580 // Confirm the appropriate invocations occur when you start a download.
581 TEST_F(DownloadManagerTest
, StartDownload
) {
582 scoped_ptr
<DownloadCreateInfo
> info(new DownloadCreateInfo
);
583 scoped_ptr
<ByteStreamReader
> stream
;
584 uint32
local_id(5); // Random value
585 base::FilePath
download_path(FILE_PATH_LITERAL("download/path"));
587 EXPECT_FALSE(download_manager_
->GetDownload(local_id
));
589 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_
.get(), _
))
591 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_
))
592 .WillOnce(RunCallback
<0>(local_id
));
594 // Doing nothing will set the default download directory to null.
595 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_
, _
, _
, _
));
596 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
597 .WillOnce(Return(true));
598 EXPECT_CALL(GetMockDownloadManagerDelegate(),
599 ApplicationClientIdForFileScanning())
600 .WillRepeatedly(Return("client-id"));
601 MockDownloadFile
* mock_file
= new MockDownloadFile
;
602 EXPECT_CALL(*mock_file
, SetClientGuid("client-id"));
603 EXPECT_CALL(*mock_download_file_factory_
.get(),
604 MockCreateFile(Ref(*info
->save_info
.get()), _
, _
, _
, true,
606 .WillOnce(Return(mock_file
));
608 download_manager_
->StartDownload(
609 info
.Pass(), stream
.Pass(), DownloadUrlParameters::OnStartedCallback());
610 EXPECT_TRUE(download_manager_
->GetDownload(local_id
));
613 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
615 TEST_F(DownloadManagerTest
, DetermineDownloadTarget_True
) {
616 // Put a mock we have a handle to on the download manager.
617 MockDownloadItemImpl
& item(AddItemToManager());
618 EXPECT_CALL(item
, GetState())
619 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS
));
621 EXPECT_CALL(GetMockDownloadManagerDelegate(),
622 DetermineDownloadTarget(&item
, _
))
623 .WillOnce(Return(true));
624 DetermineDownloadTarget(&item
);
627 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
628 // allows starting. This also tests OnDownloadTargetDetermined.
629 TEST_F(DownloadManagerTest
, DetermineDownloadTarget_False
) {
630 // Put a mock we have a handle to on the download manager.
631 MockDownloadItemImpl
& item(AddItemToManager());
633 base::FilePath
path(FILE_PATH_LITERAL("random_filepath.txt"));
634 EXPECT_CALL(GetMockDownloadManagerDelegate(),
635 DetermineDownloadTarget(&item
, _
))
636 .WillOnce(Return(false));
637 EXPECT_CALL(item
, GetForcedFilePath())
638 .WillOnce(ReturnRef(path
));
640 // Confirm that the callback was called with the right values in this case.
641 callback_called_
= false;
642 DetermineDownloadTarget(&item
);
643 EXPECT_TRUE(callback_called_
);
644 EXPECT_EQ(path
, target_path_
);
645 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE
, target_disposition_
);
646 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
, danger_type_
);
647 EXPECT_EQ(path
, intermediate_path_
);
650 // Confirm the DownloadManagerImpl::RemoveAllDownloads() functionality
651 TEST_F(DownloadManagerTest
, RemoveAllDownloads
) {
652 base::Time
now(base::Time::Now());
653 for (uint32 i
= 0; i
< 4; ++i
) {
654 MockDownloadItemImpl
& item(AddItemToManager());
655 EXPECT_EQ(i
, item
.GetId());
656 EXPECT_CALL(item
, GetStartTime())
657 .WillRepeatedly(Return(now
));
660 // Specify states for each.
661 EXPECT_CALL(GetMockDownloadItem(0), GetState())
662 .WillRepeatedly(Return(DownloadItem::COMPLETE
));
663 EXPECT_CALL(GetMockDownloadItem(1), GetState())
664 .WillRepeatedly(Return(DownloadItem::CANCELLED
));
665 EXPECT_CALL(GetMockDownloadItem(2), GetState())
666 .WillRepeatedly(Return(DownloadItem::INTERRUPTED
));
667 EXPECT_CALL(GetMockDownloadItem(3), GetState())
668 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS
));
670 // Expectations for whether or not they'll actually be removed.
671 EXPECT_CALL(GetMockDownloadItem(0), Remove())
673 EXPECT_CALL(GetMockDownloadItem(1), Remove())
675 EXPECT_CALL(GetMockDownloadItem(2), Remove())
677 EXPECT_CALL(GetMockDownloadItem(3), Remove())
680 download_manager_
->RemoveAllDownloads();
681 // Because we're mocking the download item, the Remove call doesn't
682 // result in them being removed from the DownloadManager list.
685 } // namespace content