Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / download / download_manager_impl_unittest.cc
blob7817d3e915445a8bd1e4e10e524df017965c03fa
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.
5 #include <set>
6 #include <string>
8 #include "base/bind.h"
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.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;
42 using ::testing::Eq;
43 using ::testing::Ref;
44 using ::testing::Return;
45 using ::testing::ReturnRef;
46 using ::testing::SetArgPointee;
47 using ::testing::StrictMock;
48 using ::testing::_;
51 namespace content {
52 class ByteStreamReader;
54 namespace {
56 // Matches a DownloadCreateInfo* that points to the same object as |info| and
57 // has a |default_download_directory| that matches |download_directory|.
58 MATCHER_P2(DownloadCreateInfoWithDefaultPath, info, download_directory, "") {
59 return arg == info &&
60 arg->default_download_directory == download_directory;
63 class MockDownloadItemImpl : public DownloadItemImpl {
64 public:
65 // Use history constructor for minimal base object.
66 explicit MockDownloadItemImpl(DownloadItemImplDelegate* delegate)
67 : DownloadItemImpl(
68 delegate,
69 content::DownloadId(),
70 base::FilePath(),
71 base::FilePath(),
72 std::vector<GURL>(),
73 GURL(),
74 base::Time(),
75 base::Time(),
78 DownloadItem::COMPLETE,
79 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
80 DOWNLOAD_INTERRUPT_REASON_NONE,
81 false,
82 net::BoundNetLog()) {}
83 virtual ~MockDownloadItemImpl() {}
85 MOCK_METHOD4(OnDownloadTargetDetermined,
86 void(const base::FilePath&, TargetDisposition,
87 DownloadDangerType, const base::FilePath&));
88 MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*));
89 MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*));
90 MOCK_METHOD0(UpdateObservers, void());
91 MOCK_METHOD0(CanShowInFolder, bool());
92 MOCK_METHOD0(CanOpenDownload, bool());
93 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool());
94 MOCK_METHOD0(OpenDownload, void());
95 MOCK_METHOD0(ShowDownloadInShell, void());
96 MOCK_METHOD0(ValidateDangerousDownload, void());
97 MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&));
98 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&));
99 MOCK_METHOD1(Cancel, void(bool));
100 MOCK_METHOD0(MarkAsComplete, void());
101 MOCK_METHOD1(OnAllDataSaved, void(const std::string&));
102 MOCK_METHOD0(OnDownloadedFileRemoved, void());
103 virtual void Start(
104 scoped_ptr<DownloadFile> download_file,
105 scoped_ptr<DownloadRequestHandleInterface> req_handle) OVERRIDE {
106 MockStart(download_file.get(), req_handle.get());
109 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*));
111 MOCK_METHOD0(Remove, void());
112 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*));
113 MOCK_CONST_METHOD0(CurrentSpeed, int64());
114 MOCK_CONST_METHOD0(PercentComplete, int());
115 MOCK_CONST_METHOD0(AllDataSaved, bool());
116 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query));
117 MOCK_CONST_METHOD0(IsDone, bool());
118 MOCK_CONST_METHOD0(GetFullPath, const base::FilePath&());
119 MOCK_CONST_METHOD0(GetTargetFilePath, const base::FilePath&());
120 MOCK_CONST_METHOD0(GetTargetDisposition, TargetDisposition());
121 MOCK_METHOD1(OnContentCheckCompleted, void(DownloadDangerType));
122 MOCK_CONST_METHOD0(GetState, DownloadState());
123 MOCK_CONST_METHOD0(GetUrlChain, const std::vector<GURL>&());
124 MOCK_METHOD1(SetTotalBytes, void(int64));
125 MOCK_CONST_METHOD0(GetURL, const GURL&());
126 MOCK_CONST_METHOD0(GetOriginalUrl, const GURL&());
127 MOCK_CONST_METHOD0(GetReferrerUrl, const GURL&());
128 MOCK_CONST_METHOD0(GetSuggestedFilename, std::string());
129 MOCK_CONST_METHOD0(GetContentDisposition, std::string());
130 MOCK_CONST_METHOD0(GetMimeType, std::string());
131 MOCK_CONST_METHOD0(GetOriginalMimeType, std::string());
132 MOCK_CONST_METHOD0(GetReferrerCharset, std::string());
133 MOCK_CONST_METHOD0(GetRemoteAddress, std::string());
134 MOCK_CONST_METHOD0(GetTotalBytes, int64());
135 MOCK_CONST_METHOD0(GetReceivedBytes, int64());
136 MOCK_CONST_METHOD0(GetHashState, const std::string&());
137 MOCK_CONST_METHOD0(GetHash, const std::string&());
138 MOCK_CONST_METHOD0(GetId, int32());
139 MOCK_CONST_METHOD0(GetGlobalId, DownloadId());
140 MOCK_CONST_METHOD0(GetStartTime, base::Time());
141 MOCK_CONST_METHOD0(GetEndTime, base::Time());
142 MOCK_METHOD0(GetDownloadManager, DownloadManager*());
143 MOCK_CONST_METHOD0(IsPaused, bool());
144 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool());
145 MOCK_METHOD1(SetOpenWhenComplete, void(bool));
146 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool());
147 MOCK_CONST_METHOD0(GetDangerType, DownloadDangerType());
148 MOCK_CONST_METHOD0(IsDangerous, bool());
149 MOCK_METHOD0(GetAutoOpened, bool());
150 MOCK_CONST_METHOD0(GetForcedFilePath, const base::FilePath&());
151 MOCK_CONST_METHOD0(HasUserGesture, bool());
152 MOCK_CONST_METHOD0(GetTransitionType, PageTransition());
153 MOCK_CONST_METHOD0(IsTemporary, bool());
154 MOCK_METHOD1(SetIsTemporary, void(bool));
155 MOCK_METHOD1(SetOpened, void(bool));
156 MOCK_CONST_METHOD0(GetOpened, bool());
157 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
158 MOCK_CONST_METHOD0(GetETag, const std::string&());
159 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
160 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*());
161 MOCK_CONST_METHOD0(GetWebContents, WebContents*());
162 MOCK_CONST_METHOD0(GetFileNameToReportUser, base::FilePath());
163 MOCK_METHOD1(SetDisplayName, void(const base::FilePath&));
164 MOCK_METHOD0(NotifyRemoved, void());
165 // May be called when vlog is on.
166 virtual std::string DebugString(bool verbose) const OVERRIDE {
167 return std::string();
171 class MockDownloadManagerDelegate : public DownloadManagerDelegate {
172 public:
173 MockDownloadManagerDelegate();
174 virtual ~MockDownloadManagerDelegate();
176 MOCK_METHOD0(Shutdown, void());
177 MOCK_METHOD0(GetNextId, DownloadId());
178 MOCK_METHOD2(DetermineDownloadTarget,
179 bool(DownloadItem* item,
180 const DownloadTargetCallback&));
181 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const base::FilePath&));
182 MOCK_METHOD2(ShouldCompleteDownload,
183 bool(DownloadItem*, const base::Closure&));
184 MOCK_METHOD2(ShouldOpenDownload,
185 bool(DownloadItem*, const DownloadOpenDelayedCallback&));
186 MOCK_METHOD0(GenerateFileHash, bool());
187 MOCK_METHOD4(GetSaveDir, void(BrowserContext*,
188 base::FilePath*, base::FilePath*, bool*));
189 MOCK_METHOD5(ChooseSavePath, void(
190 WebContents*, const base::FilePath&, const base::FilePath::StringType&,
191 bool, const SavePackagePathPickedCallback&));
194 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
196 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
198 class MockDownloadItemFactory
199 : public DownloadItemFactory,
200 public base::SupportsWeakPtr<MockDownloadItemFactory> {
201 public:
202 MockDownloadItemFactory();
203 virtual ~MockDownloadItemFactory();
205 // Access to map of created items.
206 // TODO(rdsmith): Could add type (save page, persisted, etc.)
207 // functionality if it's ever needed by consumers.
209 // Returns NULL if no item of that id is present.
210 MockDownloadItemImpl* GetItem(int id);
212 // Remove and return an item made by the factory.
213 // Generally used during teardown.
214 MockDownloadItemImpl* PopItem();
216 // Should be called when the item of this id is removed so that
217 // we don't keep dangling pointers.
218 void RemoveItem(int id);
220 // Overridden methods from DownloadItemFactory.
221 virtual DownloadItemImpl* CreatePersistedItem(
222 DownloadItemImplDelegate* delegate,
223 DownloadId download_id,
224 const base::FilePath& current_path,
225 const base::FilePath& target_path,
226 const std::vector<GURL>& url_chain,
227 const GURL& referrer_url,
228 const base::Time& start_time,
229 const base::Time& end_time,
230 int64 received_bytes,
231 int64 total_bytes,
232 DownloadItem::DownloadState state,
233 DownloadDangerType danger_type,
234 DownloadInterruptReason interrupt_reason,
235 bool opened,
236 const net::BoundNetLog& bound_net_log) OVERRIDE;
237 virtual DownloadItemImpl* CreateActiveItem(
238 DownloadItemImplDelegate* delegate,
239 DownloadId download_id,
240 const DownloadCreateInfo& info,
241 const net::BoundNetLog& bound_net_log) OVERRIDE;
242 virtual DownloadItemImpl* CreateSavePageItem(
243 DownloadItemImplDelegate* delegate,
244 DownloadId download_id,
245 const base::FilePath& path,
246 const GURL& url,
247 const std::string& mime_type,
248 scoped_ptr<DownloadRequestHandleInterface> request_handle,
249 const net::BoundNetLog& bound_net_log) OVERRIDE;
251 private:
252 std::map<int32, MockDownloadItemImpl*> items_;
253 DownloadItemImplDelegate item_delegate_;
255 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory);
258 MockDownloadItemFactory::MockDownloadItemFactory() {}
260 MockDownloadItemFactory::~MockDownloadItemFactory() {}
262 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) {
263 if (items_.find(id) == items_.end())
264 return NULL;
265 return items_[id];
268 MockDownloadItemImpl* MockDownloadItemFactory::PopItem() {
269 if (items_.empty())
270 return NULL;
272 std::map<int32, MockDownloadItemImpl*>::iterator first_item
273 = items_.begin();
274 MockDownloadItemImpl* result = first_item->second;
275 items_.erase(first_item);
276 return result;
279 void MockDownloadItemFactory::RemoveItem(int id) {
280 DCHECK(items_.find(id) != items_.end());
281 items_.erase(id);
284 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem(
285 DownloadItemImplDelegate* delegate,
286 DownloadId download_id,
287 const base::FilePath& current_path,
288 const base::FilePath& target_path,
289 const std::vector<GURL>& url_chain,
290 const GURL& referrer_url,
291 const base::Time& start_time,
292 const base::Time& end_time,
293 int64 received_bytes,
294 int64 total_bytes,
295 DownloadItem::DownloadState state,
296 DownloadDangerType danger_type,
297 DownloadInterruptReason interrupt_reason,
298 bool opened,
299 const net::BoundNetLog& bound_net_log) {
300 int local_id = download_id.local();
301 DCHECK(items_.find(local_id) == items_.end());
303 MockDownloadItemImpl* result =
304 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
305 EXPECT_CALL(*result, GetId())
306 .WillRepeatedly(Return(local_id));
307 items_[local_id] = result;
309 return result;
312 DownloadItemImpl* MockDownloadItemFactory::CreateActiveItem(
313 DownloadItemImplDelegate* delegate,
314 DownloadId download_id,
315 const DownloadCreateInfo& info,
316 const net::BoundNetLog& bound_net_log) {
317 int local_id = download_id.local();
318 DCHECK(items_.find(local_id) == items_.end());
320 MockDownloadItemImpl* result =
321 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
322 EXPECT_CALL(*result, GetId())
323 .WillRepeatedly(Return(local_id));
324 EXPECT_CALL(*result, GetGlobalId())
325 .WillRepeatedly(Return(download_id));
326 items_[local_id] = result;
328 // Active items are created and then immediately are called to start
329 // the download.
330 EXPECT_CALL(*result, MockStart(_, _));
332 return result;
335 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
336 DownloadItemImplDelegate* delegate,
337 DownloadId download_id,
338 const base::FilePath& path,
339 const GURL& url,
340 const std::string& mime_type,
341 scoped_ptr<DownloadRequestHandleInterface> request_handle,
342 const net::BoundNetLog& bound_net_log) {
343 int local_id = download_id.local();
344 DCHECK(items_.find(local_id) == items_.end());
346 MockDownloadItemImpl* result =
347 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
348 EXPECT_CALL(*result, GetId())
349 .WillRepeatedly(Return(local_id));
350 items_[local_id] = result;
352 return result;
355 class MockDownloadFileFactory
356 : public DownloadFileFactory,
357 public base::SupportsWeakPtr<MockDownloadFileFactory> {
358 public:
359 MockDownloadFileFactory() {}
360 virtual ~MockDownloadFileFactory() {}
362 // Overridden method from DownloadFileFactory
363 MOCK_METHOD8(MockCreateFile, DownloadFile*(
364 const DownloadSaveInfo&,
365 const base::FilePath&,
366 const GURL&, const GURL&, bool,
367 ByteStreamReader*,
368 const net::BoundNetLog&,
369 base::WeakPtr<DownloadDestinationObserver>));
371 virtual DownloadFile* CreateFile(
372 scoped_ptr<DownloadSaveInfo> save_info,
373 const base::FilePath& default_download_directory,
374 const GURL& url,
375 const GURL& referrer_url,
376 bool calculate_hash,
377 scoped_ptr<ByteStreamReader> stream,
378 const net::BoundNetLog& bound_net_log,
379 base::WeakPtr<DownloadDestinationObserver> observer) {
380 return MockCreateFile(*save_info.get(), default_download_directory, url,
381 referrer_url, calculate_hash,
382 stream.get(), bound_net_log, observer);
386 class MockBrowserContext : public BrowserContext {
387 public:
388 MockBrowserContext() {}
389 ~MockBrowserContext() {}
391 MOCK_METHOD0(GetPath, base::FilePath());
392 MOCK_CONST_METHOD0(IsOffTheRecord, bool());
393 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*());
394 MOCK_METHOD1(GetRequestContextForRenderProcess,
395 net::URLRequestContextGetter*(int renderer_child_id));
396 MOCK_METHOD0(GetMediaRequestContext,
397 net::URLRequestContextGetter*());
398 MOCK_METHOD1(GetMediaRequestContextForRenderProcess,
399 net::URLRequestContextGetter*(int renderer_child_id));
400 MOCK_METHOD2(GetMediaRequestContextForStoragePartition,
401 net::URLRequestContextGetter*(
402 const base::FilePath& partition_path, bool in_memory));
403 MOCK_METHOD0(GetResourceContext, ResourceContext*());
404 MOCK_METHOD0(GetDownloadManagerDelegate, DownloadManagerDelegate*());
405 MOCK_METHOD0(GetGeolocationPermissionContext,
406 GeolocationPermissionContext* ());
407 MOCK_METHOD0(GetSpeechRecognitionPreferences,
408 SpeechRecognitionPreferences* ());
409 MOCK_METHOD0(GetSpecialStoragePolicy, quota::SpecialStoragePolicy*());
412 class MockDownloadManagerObserver : public DownloadManager::Observer {
413 public:
414 MockDownloadManagerObserver() {}
415 ~MockDownloadManagerObserver() {}
416 MOCK_METHOD2(OnDownloadCreated, void(
417 DownloadManager*, DownloadItem*));
418 MOCK_METHOD1(ManagerGoingDown, void(DownloadManager*));
419 MOCK_METHOD2(SelectFileDialogDisplayed, void(
420 DownloadManager*, int32));
423 } // namespace
425 class DownloadManagerTest : public testing::Test {
426 public:
427 static const char* kTestData;
428 static const size_t kTestDataLen;
430 DownloadManagerTest()
431 : ui_thread_(BrowserThread::UI, &message_loop_),
432 file_thread_(BrowserThread::FILE, &message_loop_),
433 next_download_id_(0) {
436 // We tear down everything in TearDown().
437 virtual ~DownloadManagerTest() {}
439 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
440 // then create a DownloadManager that points
441 // at all of those.
442 virtual void SetUp() {
443 DCHECK(!download_manager_);
445 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
446 mock_download_file_factory_ = (new MockDownloadFileFactory())->AsWeakPtr();
447 mock_download_manager_delegate_.reset(
448 new StrictMock<MockDownloadManagerDelegate>);
449 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
450 .WillOnce(Return());
451 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
452 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
453 .WillRepeatedly(Return(false));
455 download_manager_.reset(new DownloadManagerImpl(
456 NULL, mock_browser_context_.get()));
457 download_manager_->SetDownloadItemFactoryForTesting(
458 scoped_ptr<DownloadItemFactory>(
459 mock_download_item_factory_.get()).Pass());
460 download_manager_->SetDownloadFileFactoryForTesting(
461 scoped_ptr<DownloadFileFactory>(
462 mock_download_file_factory_.get()).Pass());
463 observer_.reset(new MockDownloadManagerObserver());
464 download_manager_->AddObserver(observer_.get());
465 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
468 virtual void TearDown() {
469 while (MockDownloadItemImpl*
470 item = mock_download_item_factory_->PopItem()) {
471 EXPECT_CALL(*item, GetState())
472 .WillOnce(Return(DownloadItem::CANCELLED));
474 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
475 .WillOnce(Return());
477 download_manager_->Shutdown();
478 download_manager_.reset();
479 message_loop_.RunUntilIdle();
480 ASSERT_EQ(NULL, mock_download_item_factory_.get());
481 ASSERT_EQ(NULL, mock_download_file_factory_.get());
482 message_loop_.RunUntilIdle();
483 mock_download_manager_delegate_.reset();
484 mock_browser_context_.reset();
487 // Returns download id.
488 MockDownloadItemImpl& AddItemToManager() {
489 DownloadCreateInfo info;
491 static const char* kDownloadIdDomain = "Test download id domain";
493 // Args are ignored except for download id, so everything else can be
494 // null.
495 int id = next_download_id_;
496 ++next_download_id_;
497 info.request_handle = DownloadRequestHandle();
498 download_manager_->CreateActiveItem(DownloadId(kDownloadIdDomain, id),
499 info);
500 DCHECK(mock_download_item_factory_->GetItem(id));
501 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
502 // Satisfy expectation. If the item is created in StartDownload(),
503 // we call Start on it immediately, so we need to set that expectation
504 // in the factory.
505 scoped_ptr<DownloadRequestHandleInterface> req_handle;
506 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass());
508 return item;
511 MockDownloadItemImpl& GetMockDownloadItem(int id) {
512 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
514 DCHECK(itemp);
515 return *itemp;
518 void RemoveMockDownloadItem(int id) {
519 // Owned by DownloadManager; should be deleted there.
520 mock_download_item_factory_->RemoveItem(id);
523 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
524 return *mock_download_manager_delegate_;
527 MockDownloadManagerObserver& GetMockObserver() {
528 return *observer_;
531 void DownloadTargetDeterminedCallback(
532 const base::FilePath& target_path,
533 DownloadItem::TargetDisposition disposition,
534 DownloadDangerType danger_type,
535 const base::FilePath& intermediate_path) {
536 callback_called_ = true;
537 target_path_ = target_path;
538 target_disposition_ = disposition;
539 danger_type_ = danger_type;
540 intermediate_path_ = intermediate_path;
543 void DetermineDownloadTarget(DownloadItemImpl* item) {
544 download_manager_->DetermineDownloadTarget(
545 item, base::Bind(
546 &DownloadManagerTest::DownloadTargetDeterminedCallback,
547 base::Unretained(this)));
550 protected:
551 // Key test variable; we'll keep it available to sub-classes.
552 scoped_ptr<DownloadManagerImpl> download_manager_;
553 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
555 // Target detetermined callback.
556 bool callback_called_;
557 base::FilePath target_path_;
558 DownloadItem::TargetDisposition target_disposition_;
559 DownloadDangerType danger_type_;
560 base::FilePath intermediate_path_;
562 private:
563 base::MessageLoopForUI message_loop_;
564 TestBrowserThread ui_thread_;
565 TestBrowserThread file_thread_;
566 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
567 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
568 scoped_ptr<MockBrowserContext> mock_browser_context_;
569 scoped_ptr<MockDownloadManagerObserver> observer_;
570 int next_download_id_;
572 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
575 // Confirm the appropriate invocations occur when you start a download.
576 TEST_F(DownloadManagerTest, StartDownload) {
577 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
578 scoped_ptr<ByteStreamReader> stream;
579 int32 local_id(5); // Random value
580 base::FilePath download_path(FILE_PATH_LITERAL("download/path"));
582 EXPECT_FALSE(download_manager_->GetDownload(local_id));
584 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
585 .WillOnce(Return());
586 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId())
587 .WillOnce(Return(DownloadId(this, local_id)));
589 // Doing nothing will set the default download directory to null.
590 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _));
591 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
592 .WillOnce(Return(true));
593 EXPECT_CALL(*mock_download_file_factory_.get(),
594 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true,
595 stream.get(), _, _));
597 download_manager_->StartDownload(info.Pass(), stream.Pass());
598 EXPECT_TRUE(download_manager_->GetDownload(local_id));
601 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
602 // blocks starting.
603 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) {
604 // Put a mock we have a handle to on the download manager.
605 MockDownloadItemImpl& item(AddItemToManager());
606 EXPECT_CALL(item, GetState())
607 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS));
609 EXPECT_CALL(GetMockDownloadManagerDelegate(),
610 DetermineDownloadTarget(&item, _))
611 .WillOnce(Return(true));
612 DetermineDownloadTarget(&item);
615 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
616 // allows starting. This also tests OnDownloadTargetDetermined.
617 TEST_F(DownloadManagerTest, DetermineDownloadTarget_False) {
618 // Put a mock we have a handle to on the download manager.
619 MockDownloadItemImpl& item(AddItemToManager());
621 base::FilePath path(FILE_PATH_LITERAL("random_filepath.txt"));
622 EXPECT_CALL(GetMockDownloadManagerDelegate(),
623 DetermineDownloadTarget(&item, _))
624 .WillOnce(Return(false));
625 EXPECT_CALL(item, GetForcedFilePath())
626 .WillOnce(ReturnRef(path));
628 // Confirm that the callback was called with the right values in this case.
629 callback_called_ = false;
630 DetermineDownloadTarget(&item);
631 EXPECT_TRUE(callback_called_);
632 EXPECT_EQ(path, target_path_);
633 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_);
634 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_);
635 EXPECT_EQ(path, intermediate_path_);
638 // Confirm the DownloadManagerImpl::RemoveAllDownloads() functionality
639 TEST_F(DownloadManagerTest, RemoveAllDownloads) {
640 base::Time now(base::Time::Now());
641 for (int i = 0; i < 4; ++i) {
642 MockDownloadItemImpl& item(AddItemToManager());
643 EXPECT_EQ(i, item.GetId());
644 EXPECT_CALL(item, GetStartTime())
645 .WillRepeatedly(Return(now));
648 // Specify states for each.
649 EXPECT_CALL(GetMockDownloadItem(0), GetState())
650 .WillRepeatedly(Return(DownloadItem::COMPLETE));
651 EXPECT_CALL(GetMockDownloadItem(1), GetState())
652 .WillRepeatedly(Return(DownloadItem::CANCELLED));
653 EXPECT_CALL(GetMockDownloadItem(2), GetState())
654 .WillRepeatedly(Return(DownloadItem::INTERRUPTED));
655 EXPECT_CALL(GetMockDownloadItem(3), GetState())
656 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS));
658 // Expectations for whether or not they'll actually be removed.
659 EXPECT_CALL(GetMockDownloadItem(0), Remove())
660 .WillOnce(Return());
661 EXPECT_CALL(GetMockDownloadItem(1), Remove())
662 .WillOnce(Return());
663 EXPECT_CALL(GetMockDownloadItem(2), Remove())
664 .WillOnce(Return());
665 EXPECT_CALL(GetMockDownloadItem(3), Remove())
666 .Times(0);
668 download_manager_->RemoveAllDownloads();
669 // Because we're mocking the download item, the Remove call doesn't
670 // result in them being removed from the DownloadManager list.
673 } // namespace content