Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / download / download_manager_impl_unittest.cc
blob6b7850571cd761cb39f00474c9826beef3050dac
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/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_log.h"
36 #include "net/base/net_util.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;
43 using ::testing::Eq;
44 using ::testing::Ref;
45 using ::testing::Return;
46 using ::testing::ReturnRef;
47 using ::testing::SetArgPointee;
48 using ::testing::StrictMock;
49 using ::testing::_;
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);
57 namespace content {
58 class ByteStreamReader;
60 namespace {
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, "") {
65 return arg == info &&
66 arg->default_download_directory == download_directory;
69 class MockDownloadItemImpl : public DownloadItemImpl {
70 public:
71 // Use history constructor for minimal base object.
72 explicit MockDownloadItemImpl(DownloadItemImplDelegate* delegate)
73 : DownloadItemImpl(
74 delegate,
75 content::DownloadItem::kInvalidId,
76 base::FilePath(),
77 base::FilePath(),
78 std::vector<GURL>(),
79 GURL(),
80 "application/octet-stream",
81 "application/octet-stream",
82 base::Time(),
83 base::Time(),
84 std::string(),
85 std::string(),
88 DownloadItem::COMPLETE,
89 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
90 DOWNLOAD_INTERRUPT_REASON_NONE,
91 false,
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 {
182 public:
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> {
212 public:
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,
234 uint32 download_id,
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,
246 int64 total_bytes,
247 DownloadItem::DownloadState state,
248 DownloadDangerType danger_type,
249 DownloadInterruptReason interrupt_reason,
250 bool opened,
251 const net::BoundNetLog& bound_net_log) override;
252 DownloadItemImpl* CreateActiveItem(
253 DownloadItemImplDelegate* delegate,
254 uint32 download_id,
255 const DownloadCreateInfo& info,
256 const net::BoundNetLog& bound_net_log) override;
257 DownloadItemImpl* CreateSavePageItem(
258 DownloadItemImplDelegate* delegate,
259 uint32 download_id,
260 const base::FilePath& path,
261 const GURL& url,
262 const std::string& mime_type,
263 scoped_ptr<DownloadRequestHandleInterface> request_handle,
264 const net::BoundNetLog& bound_net_log) override;
266 private:
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())
279 return NULL;
280 return items_[id];
283 MockDownloadItemImpl* MockDownloadItemFactory::PopItem() {
284 if (items_.empty())
285 return NULL;
287 std::map<uint32, MockDownloadItemImpl*>::iterator first_item
288 = items_.begin();
289 MockDownloadItemImpl* result = first_item->second;
290 items_.erase(first_item);
291 return result;
294 void MockDownloadItemFactory::RemoveItem(int id) {
295 DCHECK(items_.find(id) != items_.end());
296 items_.erase(id);
299 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem(
300 DownloadItemImplDelegate* delegate,
301 uint32 download_id,
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,
313 int64 total_bytes,
314 DownloadItem::DownloadState state,
315 DownloadDangerType danger_type,
316 DownloadInterruptReason interrupt_reason,
317 bool opened,
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;
325 return result;
328 DownloadItemImpl* MockDownloadItemFactory::CreateActiveItem(
329 DownloadItemImplDelegate* delegate,
330 uint32 download_id,
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
342 // the download.
343 EXPECT_CALL(*result, MockStart(_, _));
345 return result;
348 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
349 DownloadItemImplDelegate* delegate,
350 uint32 download_id,
351 const base::FilePath& path,
352 const GURL& url,
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;
364 return result;
367 class MockDownloadFileFactory
368 : public DownloadFileFactory,
369 public base::SupportsWeakPtr<MockDownloadFileFactory> {
370 public:
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,
379 ByteStreamReader*,
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,
386 const GURL& url,
387 const GURL& referrer_url,
388 bool calculate_hash,
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 {
399 public:
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*());
424 scoped_ptr<ZoomLevelDelegate> CreateZoomLevelDelegate(
425 const base::FilePath& path) override {
426 return scoped_ptr<ZoomLevelDelegate>(CreateZoomLevelDelegateMock(path));
430 class MockDownloadManagerObserver : public DownloadManager::Observer {
431 public:
432 MockDownloadManagerObserver() {}
433 ~MockDownloadManagerObserver() {}
434 MOCK_METHOD2(OnDownloadCreated, void(
435 DownloadManager*, DownloadItem*));
436 MOCK_METHOD1(ManagerGoingDown, void(DownloadManager*));
437 MOCK_METHOD2(SelectFileDialogDisplayed, void(
438 DownloadManager*, int32));
441 } // namespace
443 class DownloadManagerTest : public testing::Test {
444 public:
445 static const char* kTestData;
446 static const size_t kTestDataLen;
448 DownloadManagerTest()
449 : callback_called_(false),
450 ui_thread_(BrowserThread::UI, &message_loop_),
451 file_thread_(BrowserThread::FILE, &message_loop_),
452 next_download_id_(0) {
455 // We tear down everything in TearDown().
456 ~DownloadManagerTest() override {}
458 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
459 // then create a DownloadManager that points
460 // at all of those.
461 void SetUp() override {
462 DCHECK(!download_manager_);
464 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
465 mock_download_file_factory_ = (new MockDownloadFileFactory())->AsWeakPtr();
466 mock_download_manager_delegate_.reset(
467 new StrictMock<MockDownloadManagerDelegate>);
468 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
469 .WillOnce(Return());
470 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
471 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
472 .WillRepeatedly(Return(false));
474 download_manager_.reset(new DownloadManagerImpl(
475 NULL, mock_browser_context_.get()));
476 download_manager_->SetDownloadItemFactoryForTesting(
477 scoped_ptr<DownloadItemFactory>(
478 mock_download_item_factory_.get()).Pass());
479 download_manager_->SetDownloadFileFactoryForTesting(
480 scoped_ptr<DownloadFileFactory>(
481 mock_download_file_factory_.get()).Pass());
482 observer_.reset(new MockDownloadManagerObserver());
483 download_manager_->AddObserver(observer_.get());
484 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
487 void TearDown() override {
488 while (MockDownloadItemImpl*
489 item = mock_download_item_factory_->PopItem()) {
490 EXPECT_CALL(*item, GetState())
491 .WillOnce(Return(DownloadItem::CANCELLED));
493 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
494 .WillOnce(Return());
496 download_manager_->Shutdown();
497 download_manager_.reset();
498 message_loop_.RunUntilIdle();
499 ASSERT_EQ(NULL, mock_download_item_factory_.get());
500 ASSERT_EQ(NULL, mock_download_file_factory_.get());
501 message_loop_.RunUntilIdle();
502 mock_download_manager_delegate_.reset();
503 mock_browser_context_.reset();
506 // Returns download id.
507 MockDownloadItemImpl& AddItemToManager() {
508 DownloadCreateInfo info;
510 // Args are ignored except for download id, so everything else can be
511 // null.
512 uint32 id = next_download_id_;
513 ++next_download_id_;
514 info.request_handle = DownloadRequestHandle();
515 download_manager_->CreateActiveItem(id, info);
516 DCHECK(mock_download_item_factory_->GetItem(id));
517 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
518 // Satisfy expectation. If the item is created in StartDownload(),
519 // we call Start on it immediately, so we need to set that expectation
520 // in the factory.
521 scoped_ptr<DownloadRequestHandleInterface> req_handle;
522 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass());
524 return item;
527 MockDownloadItemImpl& GetMockDownloadItem(int id) {
528 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
530 DCHECK(itemp);
531 return *itemp;
534 void RemoveMockDownloadItem(int id) {
535 // Owned by DownloadManager; should be deleted there.
536 mock_download_item_factory_->RemoveItem(id);
539 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
540 return *mock_download_manager_delegate_;
543 MockDownloadManagerObserver& GetMockObserver() {
544 return *observer_;
547 void DownloadTargetDeterminedCallback(
548 const base::FilePath& target_path,
549 DownloadItem::TargetDisposition disposition,
550 DownloadDangerType danger_type,
551 const base::FilePath& intermediate_path) {
552 callback_called_ = true;
553 target_path_ = target_path;
554 target_disposition_ = disposition;
555 danger_type_ = danger_type;
556 intermediate_path_ = intermediate_path;
559 void DetermineDownloadTarget(DownloadItemImpl* item) {
560 download_manager_->DetermineDownloadTarget(
561 item, base::Bind(
562 &DownloadManagerTest::DownloadTargetDeterminedCallback,
563 base::Unretained(this)));
566 protected:
567 // Key test variable; we'll keep it available to sub-classes.
568 scoped_ptr<DownloadManagerImpl> download_manager_;
569 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
571 // Target detetermined callback.
572 bool callback_called_;
573 base::FilePath target_path_;
574 DownloadItem::TargetDisposition target_disposition_;
575 DownloadDangerType danger_type_;
576 base::FilePath intermediate_path_;
578 private:
579 base::MessageLoopForUI message_loop_;
580 TestBrowserThread ui_thread_;
581 TestBrowserThread file_thread_;
582 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
583 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
584 scoped_ptr<MockBrowserContext> mock_browser_context_;
585 scoped_ptr<MockDownloadManagerObserver> observer_;
586 uint32 next_download_id_;
588 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
591 // Confirm the appropriate invocations occur when you start a download.
592 TEST_F(DownloadManagerTest, StartDownload) {
593 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
594 scoped_ptr<ByteStreamReader> stream;
595 uint32 local_id(5); // Random value
596 base::FilePath download_path(FILE_PATH_LITERAL("download/path"));
598 EXPECT_FALSE(download_manager_->GetDownload(local_id));
600 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
601 .WillOnce(Return());
602 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_))
603 .WillOnce(RunCallback<0>(local_id));
605 // Doing nothing will set the default download directory to null.
606 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _));
607 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
608 .WillOnce(Return(true));
609 EXPECT_CALL(GetMockDownloadManagerDelegate(),
610 ApplicationClientIdForFileScanning())
611 .WillRepeatedly(Return("client-id"));
612 MockDownloadFile* mock_file = new MockDownloadFile;
613 EXPECT_CALL(*mock_file, SetClientGuid("client-id"));
614 EXPECT_CALL(*mock_download_file_factory_.get(),
615 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true,
616 stream.get(), _, _))
617 .WillOnce(Return(mock_file));
619 download_manager_->StartDownload(
620 info.Pass(), stream.Pass(), DownloadUrlParameters::OnStartedCallback());
621 EXPECT_TRUE(download_manager_->GetDownload(local_id));
624 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
625 // blocks starting.
626 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) {
627 // Put a mock we have a handle to on the download manager.
628 MockDownloadItemImpl& item(AddItemToManager());
629 EXPECT_CALL(item, GetState())
630 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS));
632 EXPECT_CALL(GetMockDownloadManagerDelegate(),
633 DetermineDownloadTarget(&item, _))
634 .WillOnce(Return(true));
635 DetermineDownloadTarget(&item);
638 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
639 // allows starting. This also tests OnDownloadTargetDetermined.
640 TEST_F(DownloadManagerTest, DetermineDownloadTarget_False) {
641 // Put a mock we have a handle to on the download manager.
642 MockDownloadItemImpl& item(AddItemToManager());
644 base::FilePath path(FILE_PATH_LITERAL("random_filepath.txt"));
645 EXPECT_CALL(GetMockDownloadManagerDelegate(),
646 DetermineDownloadTarget(&item, _))
647 .WillOnce(Return(false));
648 EXPECT_CALL(item, GetForcedFilePath())
649 .WillOnce(ReturnRef(path));
651 // Confirm that the callback was called with the right values in this case.
652 callback_called_ = false;
653 DetermineDownloadTarget(&item);
654 EXPECT_TRUE(callback_called_);
655 EXPECT_EQ(path, target_path_);
656 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_);
657 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_);
658 EXPECT_EQ(path, intermediate_path_);
661 // Confirm the DownloadManagerImpl::RemoveAllDownloads() functionality
662 TEST_F(DownloadManagerTest, RemoveAllDownloads) {
663 base::Time now(base::Time::Now());
664 for (uint32 i = 0; i < 4; ++i) {
665 MockDownloadItemImpl& item(AddItemToManager());
666 EXPECT_EQ(i, item.GetId());
667 EXPECT_CALL(item, GetStartTime())
668 .WillRepeatedly(Return(now));
671 // Specify states for each.
672 EXPECT_CALL(GetMockDownloadItem(0), GetState())
673 .WillRepeatedly(Return(DownloadItem::COMPLETE));
674 EXPECT_CALL(GetMockDownloadItem(1), GetState())
675 .WillRepeatedly(Return(DownloadItem::CANCELLED));
676 EXPECT_CALL(GetMockDownloadItem(2), GetState())
677 .WillRepeatedly(Return(DownloadItem::INTERRUPTED));
678 EXPECT_CALL(GetMockDownloadItem(3), GetState())
679 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS));
681 // Expectations for whether or not they'll actually be removed.
682 EXPECT_CALL(GetMockDownloadItem(0), Remove())
683 .WillOnce(Return());
684 EXPECT_CALL(GetMockDownloadItem(1), Remove())
685 .WillOnce(Return());
686 EXPECT_CALL(GetMockDownloadItem(2), Remove())
687 .WillOnce(Return());
688 EXPECT_CALL(GetMockDownloadItem(3), Remove())
689 .Times(0);
691 download_manager_->RemoveAllDownloads();
692 // Because we're mocking the download item, the Remove call doesn't
693 // result in them being removed from the DownloadManager list.
696 } // namespace content