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

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

Issue 10799005: Replace the DownloadFileManager with direct ownership (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed Windows compile error. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "content/browser/download/byte_stream.h" 19 #include "content/browser/download/byte_stream.h"
20 #include "content/browser/download/download_create_info.h" 20 #include "content/browser/download/download_create_info.h"
21 #include "content/browser/download/download_file_manager.h"
22 #include "content/browser/download/download_item_factory.h" 21 #include "content/browser/download/download_item_factory.h"
23 #include "content/browser/download/download_item_impl.h" 22 #include "content/browser/download/download_item_impl.h"
24 #include "content/browser/download/download_item_impl_delegate.h" 23 #include "content/browser/download/download_item_impl_delegate.h"
25 #include "content/browser/download/download_manager_impl.h" 24 #include "content/browser/download/download_manager_impl.h"
26 #include "content/browser/download/download_request_handle.h" 25 #include "content/browser/download/download_request_handle.h"
27 #include "content/browser/download/mock_download_file.h" 26 #include "content/browser/download/mock_download_file.h"
28 #include "content/public/browser/browser_context.h" 27 #include "content/public/browser/browser_context.h"
29 #include "content/public/browser/download_interrupt_reasons.h" 28 #include "content/public/browser/download_interrupt_reasons.h"
30 #include "content/public/browser/download_item.h" 29 #include "content/public/browser/download_item.h"
31 #include "content/public/browser/download_manager_delegate.h" 30 #include "content/public/browser/download_manager_delegate.h"
32 #include "content/public/test/mock_download_item.h" 31 #include "content/public/test/mock_download_item.h"
33 #include "content/public/test/test_browser_context.h" 32 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread.h" 33 #include "content/public/test/test_browser_thread.h"
34 #include "net/base/net_log.h"
35 #include "net/base/net_util.h" 35 #include "net/base/net_util.h"
36 #include "testing/gmock/include/gmock/gmock.h" 36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gmock_mutant.h" 37 #include "testing/gmock_mutant.h"
38 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
39 39
40 using ::testing::DoAll; 40 using ::testing::DoAll;
41 using ::testing::Ref; 41 using ::testing::Ref;
42 using ::testing::Return; 42 using ::testing::Return;
43 using ::testing::ReturnRef; 43 using ::testing::ReturnRef;
44 using ::testing::SetArgPointee; 44 using ::testing::SetArgPointee;
(...skipping 28 matching lines...) Expand all
73 MOCK_METHOD0(CanShowInFolder, bool()); 73 MOCK_METHOD0(CanShowInFolder, bool());
74 MOCK_METHOD0(CanOpenDownload, bool()); 74 MOCK_METHOD0(CanOpenDownload, bool());
75 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); 75 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool());
76 MOCK_METHOD0(OpenDownload, void()); 76 MOCK_METHOD0(OpenDownload, void());
77 MOCK_METHOD0(ShowDownloadInShell, void()); 77 MOCK_METHOD0(ShowDownloadInShell, void());
78 MOCK_METHOD0(DangerousDownloadValidated, void()); 78 MOCK_METHOD0(DangerousDownloadValidated, void());
79 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); 79 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&));
80 MOCK_METHOD1(Cancel, void(bool)); 80 MOCK_METHOD1(Cancel, void(bool));
81 MOCK_METHOD0(MarkAsComplete, void()); 81 MOCK_METHOD0(MarkAsComplete, void());
82 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); 82 MOCK_METHOD1(DelayedDownloadOpened, void(bool));
83 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&)); 83 MOCK_METHOD1(OnAllDataSaved, void(const std::string&));
84 MOCK_METHOD0(OnDownloadedFileRemoved, void()); 84 MOCK_METHOD0(OnDownloadedFileRemoved, void());
85 MOCK_METHOD0(MaybeCompleteDownload, void()); 85 MOCK_METHOD0(MaybeCompleteDownload, void());
86 virtual void Start(
87 scoped_ptr<content::DownloadFile> download_file) OVERRIDE {
88 MockStart(download_file.get());
89 }
90
91 MOCK_METHOD1(MockStart, void(content::DownloadFile*));
92
86 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); 93 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason));
87 MOCK_METHOD1(Delete, void(DeleteReason)); 94 MOCK_METHOD1(Delete, void(DeleteReason));
88 MOCK_METHOD0(Remove, void()); 95 MOCK_METHOD0(Remove, void());
89 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); 96 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*));
90 MOCK_CONST_METHOD0(CurrentSpeed, int64()); 97 MOCK_CONST_METHOD0(CurrentSpeed, int64());
91 MOCK_CONST_METHOD0(PercentComplete, int()); 98 MOCK_CONST_METHOD0(PercentComplete, int());
92 MOCK_CONST_METHOD0(AllDataSaved, bool()); 99 MOCK_CONST_METHOD0(AllDataSaved, bool());
93 MOCK_METHOD0(TogglePause, void()); 100 MOCK_METHOD0(TogglePause, void());
94 MOCK_METHOD0(OnDownloadCompleting, void()); 101 MOCK_METHOD0(OnDownloadCompleting, void());
95 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); 102 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); 154 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
148 MOCK_CONST_METHOD0(GetETag, const std::string&()); 155 MOCK_CONST_METHOD0(GetETag, const std::string&());
149 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 156 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
150 MOCK_CONST_METHOD0(GetPersistentStoreInfo, 157 MOCK_CONST_METHOD0(GetPersistentStoreInfo,
151 content::DownloadPersistentStoreInfo()); 158 content::DownloadPersistentStoreInfo());
152 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 159 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
153 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); 160 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*());
154 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 161 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
155 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 162 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
156 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 163 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
157 MOCK_METHOD0(OffThreadCancel, void());
158 MOCK_CONST_METHOD1(DebugString, std::string(bool)); 164 MOCK_CONST_METHOD1(DebugString, std::string(bool));
159 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 165 MOCK_METHOD0(MockDownloadOpenForTesting, void());
160 MOCK_METHOD1(GetExternalData, ExternalData*(const void*)); 166 MOCK_METHOD1(GetExternalData, ExternalData*(const void*));
161 MOCK_CONST_METHOD1(GetExternalData, const ExternalData*(const void*)); 167 MOCK_CONST_METHOD1(GetExternalData, const ExternalData*(const void*));
162 MOCK_METHOD2(SetExternalData, void(const void*, ExternalData*)); 168 MOCK_METHOD2(SetExternalData, void(const void*, ExternalData*));
163 }; 169 };
164 170
165 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { 171 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate {
166 public: 172 public:
167 MockDownloadManagerDelegate(); 173 MockDownloadManagerDelegate();
(...skipping 20 matching lines...) Expand all
188 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*)); 194 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*));
189 MOCK_METHOD5(ChooseSavePath, void( 195 MOCK_METHOD5(ChooseSavePath, void(
190 WebContents*, const FilePath&, const FilePath::StringType&, 196 WebContents*, const FilePath&, const FilePath::StringType&,
191 bool, const content::SavePackagePathPickedCallback&)); 197 bool, const content::SavePackagePathPickedCallback&));
192 }; 198 };
193 199
194 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} 200 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
195 201
196 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} 202 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
197 203
198 class MockDownloadFileManager : public DownloadFileManager { 204 class NullDownloadItemImplDelegate : public DownloadItemImplDelegate {
199 public: 205 public:
200 MockDownloadFileManager(); 206 // Safe to null this out even if it doesn't do anything because none
201 207 // of these functions will ever be called; this class just exists
202 void CreateDownloadFile( 208 // to have something to pass to the DownloadItemImpl base class
203 scoped_ptr<DownloadCreateInfo> info, 209 // of MockDownloadItemImpl.
204 scoped_ptr<content::ByteStreamReader> stream, 210 virtual void DelegateStart(DownloadItemImpl* download) OVERRIDE {
205 scoped_refptr<content::DownloadManager> download_manager, 211 NOTREACHED();
206 bool hash_needed,
207 const net::BoundNetLog& bound_net_log,
208 const CreateDownloadFileCallback& callback) OVERRIDE {
209 // Note that scoped_refptr<> on download manager is also stripped
210 // to make mock comparisons easier. Comparing the scoped_refptr<>
211 // works, but holds a reference to the DownloadManager until
212 // MockDownloadFileManager destruction, which messes up destruction
213 // testing.
214 MockCreateDownloadFile(info.get(), stream.get(), download_manager.get(),
215 hash_needed, bound_net_log, callback);
216 } 212 }
217
218 MOCK_METHOD6(MockCreateDownloadFile, void(
219 DownloadCreateInfo* info,
220 content::ByteStreamReader* stream,
221 content::DownloadManager* download_manager,
222 bool hash_needed,
223 const net::BoundNetLog& bound_net_log,
224 const CreateDownloadFileCallback& callback));
225 MOCK_METHOD0(Shutdown, void());
226 MOCK_METHOD1(CancelDownload, void(content::DownloadId));
227 MOCK_METHOD2(CompleteDownload, void(content::DownloadId,
228 const base::Closure&));
229 MOCK_METHOD1(OnDownloadManagerShutdown, void(content::DownloadManager*));
230 MOCK_METHOD4(RenameDownloadFile, void(content::DownloadId,
231 const FilePath&,
232 bool,
233 const RenameCompletionCallback&));
234 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int());
235 protected:
236 virtual ~MockDownloadFileManager();
237 }; 213 };
238 214
239 MockDownloadFileManager::MockDownloadFileManager()
240 : DownloadFileManager(NULL) {}
241
242 MockDownloadFileManager::~MockDownloadFileManager() {}
243
244 class MockDownloadItemFactory 215 class MockDownloadItemFactory
245 : public content::DownloadItemFactory, 216 : public content::DownloadItemFactory,
246 public base::SupportsWeakPtr<MockDownloadItemFactory> { 217 public base::SupportsWeakPtr<MockDownloadItemFactory> {
247 public: 218 public:
248 MockDownloadItemFactory(); 219 MockDownloadItemFactory();
249 virtual ~MockDownloadItemFactory(); 220 virtual ~MockDownloadItemFactory();
250 221
251 // Access to map of created items. 222 // Access to map of created items.
252 // TODO(rdsmith): Could add type (save page, persisted, etc.) 223 // TODO(rdsmith): Could add type (save page, persisted, etc.)
253 // functionality if it's ever needed by consumers. 224 // functionality if it's ever needed by consumers.
(...skipping 25 matching lines...) Expand all
279 DownloadItemImplDelegate* delegate, 250 DownloadItemImplDelegate* delegate,
280 const FilePath& path, 251 const FilePath& path,
281 const GURL& url, 252 const GURL& url,
282 bool is_otr, 253 bool is_otr,
283 content::DownloadId download_id, 254 content::DownloadId download_id,
284 const std::string& mime_type, 255 const std::string& mime_type,
285 const net::BoundNetLog& bound_net_log) OVERRIDE; 256 const net::BoundNetLog& bound_net_log) OVERRIDE;
286 257
287 private: 258 private:
288 std::map<int32, MockDownloadItemImpl*> items_; 259 std::map<int32, MockDownloadItemImpl*> items_;
289 DownloadItemImplDelegate item_delegate_; 260 NullDownloadItemImplDelegate item_delegate_;
290 261
291 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); 262 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory);
292 }; 263 };
293 264
294 MockDownloadItemFactory::MockDownloadItemFactory() {} 265 MockDownloadItemFactory::MockDownloadItemFactory() {}
295 266
296 MockDownloadItemFactory::~MockDownloadItemFactory() {} 267 MockDownloadItemFactory::~MockDownloadItemFactory() {}
297 268
298 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) { 269 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) {
299 if (items_.find(id) == items_.end()) 270 if (items_.find(id) == items_.end())
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 scoped_ptr<DownloadRequestHandleInterface> request_handle, 311 scoped_ptr<DownloadRequestHandleInterface> request_handle,
341 bool is_otr, 312 bool is_otr,
342 const net::BoundNetLog& bound_net_log) { 313 const net::BoundNetLog& bound_net_log) {
343 int local_id = info.download_id.local(); 314 int local_id = info.download_id.local();
344 DCHECK(items_.find(local_id) == items_.end()); 315 DCHECK(items_.find(local_id) == items_.end());
345 316
346 MockDownloadItemImpl* result = 317 MockDownloadItemImpl* result =
347 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 318 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
348 EXPECT_CALL(*result, GetId()) 319 EXPECT_CALL(*result, GetId())
349 .WillRepeatedly(Return(local_id)); 320 .WillRepeatedly(Return(local_id));
321 EXPECT_CALL(*result, GetGlobalId())
322 .WillRepeatedly(Return(content::DownloadId(delegate, local_id)));
350 items_[local_id] = result; 323 items_[local_id] = result;
351 324
325 // Active items are created and then immediately are called to start
326 // the download.
327 EXPECT_CALL(*result, MockStart(_));
328
352 return result; 329 return result;
353 } 330 }
354 331
355 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( 332 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
356 DownloadItemImplDelegate* delegate, 333 DownloadItemImplDelegate* delegate,
357 const FilePath& path, 334 const FilePath& path,
358 const GURL& url, 335 const GURL& url,
359 bool is_otr, 336 bool is_otr,
360 content::DownloadId download_id, 337 content::DownloadId download_id,
361 const std::string& mime_type, 338 const std::string& mime_type,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 391
415 DownloadManagerTest() 392 DownloadManagerTest()
416 : ui_thread_(content::BrowserThread::UI, &message_loop_), 393 : ui_thread_(content::BrowserThread::UI, &message_loop_),
417 file_thread_(content::BrowserThread::FILE, &message_loop_), 394 file_thread_(content::BrowserThread::FILE, &message_loop_),
418 next_download_id_(0) { 395 next_download_id_(0) {
419 } 396 }
420 397
421 // We tear down everything in TearDown(). 398 // We tear down everything in TearDown().
422 ~DownloadManagerTest() {} 399 ~DownloadManagerTest() {}
423 400
424 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate, 401 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
425 // and MockDownloadFileManager, then create a DownloadManager that points 402 // then create a DownloadManager that points
426 // at all of those. 403 // at all of those.
427 virtual void SetUp() { 404 virtual void SetUp() {
428 DCHECK(!download_manager_.get()); 405 DCHECK(!download_manager_.get());
429 406
430 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); 407 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
431 mock_download_manager_delegate_.reset( 408 mock_download_manager_delegate_.reset(
432 new StrictMock<MockDownloadManagerDelegate>); 409 new StrictMock<MockDownloadManagerDelegate>);
433 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) 410 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
434 .WillOnce(Return()); 411 .WillOnce(Return());
435 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>;
436 EXPECT_CALL(*mock_download_file_manager_.get(),
437 OnDownloadManagerShutdown(_));
438 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); 412 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
439 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) 413 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
440 .WillRepeatedly(Return(false)); 414 .WillRepeatedly(Return(false));
441 415
442 download_manager_ = new DownloadManagerImpl( 416 download_manager_ = new DownloadManagerImpl(
443 mock_download_file_manager_.get(),
444 scoped_ptr<content::DownloadItemFactory>( 417 scoped_ptr<content::DownloadItemFactory>(
445 mock_download_item_factory_.get()).Pass(), NULL); 418 mock_download_item_factory_.get()).Pass(), NULL);
446 observer_.reset(new MockDownloadManagerObserver()); 419 observer_.reset(new MockDownloadManagerObserver());
447 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 420 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
448 .WillOnce(Return()); 421 .WillOnce(Return());
449 download_manager_->AddObserver(observer_.get()); 422 download_manager_->AddObserver(observer_.get());
450 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); 423 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
451 download_manager_->Init(mock_browser_context_.get()); 424 download_manager_->Init(mock_browser_context_.get());
452 } 425 }
453 426
454 virtual void TearDown() { 427 virtual void TearDown() {
455 while (MockDownloadItemImpl* 428 while (MockDownloadItemImpl*
456 item = mock_download_item_factory_->PopItem()) { 429 item = mock_download_item_factory_->PopItem()) {
457 EXPECT_CALL(*item, GetSafetyState()) 430 EXPECT_CALL(*item, GetSafetyState())
458 .WillOnce(Return(content::DownloadItem::SAFE)); 431 .WillOnce(Return(content::DownloadItem::SAFE));
459 EXPECT_CALL(*item, IsPartialDownload()) 432 EXPECT_CALL(*item, IsPartialDownload())
460 .WillOnce(Return(false)); 433 .WillOnce(Return(false));
461 } 434 }
462 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) 435 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
463 .WillOnce(Return()); 436 .WillOnce(Return());
464 437
465 download_manager_->Shutdown(); 438 download_manager_->Shutdown();
466 download_manager_ = NULL; 439 download_manager_ = NULL;
467 message_loop_.RunAllPending(); 440 message_loop_.RunAllPending();
468 ASSERT_EQ(NULL, mock_download_item_factory_.get()); 441 ASSERT_EQ(NULL, mock_download_item_factory_.get());
469 message_loop_.RunAllPending(); 442 message_loop_.RunAllPending();
470 mock_download_manager_delegate_.reset(); 443 mock_download_manager_delegate_.reset();
471 mock_download_file_manager_ = NULL;
472 mock_browser_context_.reset(); 444 mock_browser_context_.reset();
473 } 445 }
474 446
475 // Returns download id. 447 // Returns download id.
476 MockDownloadItemImpl& AddItemToManager() { 448 MockDownloadItemImpl& AddItemToManager() {
477 DownloadCreateInfo info; 449 DownloadCreateInfo info;
478 450
479 static const char* kDownloadIdDomain = "Test download id domain"; 451 static const char* kDownloadIdDomain = "Test download id domain";
480 452
481 // Args are ignored except for download id, so everything else can be 453 // Args are ignored except for download id, so everything else can be
482 // null. 454 // null.
483 int id = next_download_id_; 455 int id = next_download_id_;
484 ++next_download_id_; 456 ++next_download_id_;
485 info.download_id = content::DownloadId(kDownloadIdDomain, id); 457 info.download_id = content::DownloadId(kDownloadIdDomain, id);
486 info.request_handle = DownloadRequestHandle(); 458 info.request_handle = DownloadRequestHandle();
487 download_manager_->CreateDownloadItem(&info); 459 EXPECT_CALL(GetMockObserver(),
460 OnDownloadCreated(download_manager_.get(), _));
461 download_manager_->CreateDownloadItem(&info, net::BoundNetLog());
488 462
489 DCHECK(mock_download_item_factory_->GetItem(id)); 463 DCHECK(mock_download_item_factory_->GetItem(id));
490 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); 464 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
491 ON_CALL(item, GetId()) 465 // Satisfy expectation. If the item is created in StartDownload(),
492 .WillByDefault(Return(id)); 466 // we call Start on it immediately, so we need to set that expectation
467 // in the factory.
468 item.Start(scoped_ptr<content::DownloadFile>());
493 469
494 return item; 470 return item;
495 } 471 }
496 472
497 MockDownloadItemImpl& GetMockDownloadItem(int id) { 473 MockDownloadItemImpl& GetMockDownloadItem(int id) {
498 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); 474 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
499 475
500 DCHECK(itemp); 476 DCHECK(itemp);
501 return *itemp; 477 return *itemp;
502 } 478 }
503 479
504 void RemoveMockDownloadItem(int id) { 480 void RemoveMockDownloadItem(int id) {
505 // Owned by DownloadManager; should be deleted there. 481 // Owned by DownloadManager; should be deleted there.
506 mock_download_item_factory_->RemoveItem(id); 482 mock_download_item_factory_->RemoveItem(id);
507 } 483 }
508 484
509 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { 485 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
510 return *mock_download_manager_delegate_; 486 return *mock_download_manager_delegate_;
511 } 487 }
512 488
513 MockDownloadFileManager& GetMockDownloadFileManager() {
514 return *mock_download_file_manager_;
515 }
516
517 MockDownloadManagerObserver& GetMockObserver() { 489 MockDownloadManagerObserver& GetMockObserver() {
518 return *observer_; 490 return *observer_;
519 } 491 }
520 492
521 // Probe at private internals. 493 // Probe at private internals.
522 void DownloadStopped(DownloadItemImpl* item) { 494 void DownloadStopped(DownloadItemImpl* item) {
523 download_manager_->DownloadStopped(item); 495 download_manager_->DownloadStopped(item);
524 } 496 }
525 497
498 void DelegateStart(DownloadItemImpl* item) {
499 download_manager_->DelegateStart(item);
500 }
501
526 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 502 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
527 // For DCHECK in AddDownloadItemToHistory. Don't want to use 503 // For DCHECK in AddDownloadItemToHistory. Don't want to use
528 // WillRepeatedly as it may have to return true after this. 504 // WillRepeatedly as it may have to return true after this.
529 if (DCHECK_IS_ON()) 505 if (DCHECK_IS_ON())
530 EXPECT_CALL(item, IsPersisted()) 506 EXPECT_CALL(item, IsPersisted())
531 .WillRepeatedly(Return(false)); 507 .WillRepeatedly(Return(false));
532 508
533 EXPECT_CALL(item, SetDbHandle(db_handle)); 509 EXPECT_CALL(item, SetDbHandle(db_handle));
534 EXPECT_CALL(item, SetIsPersisted()); 510 EXPECT_CALL(item, SetIsPersisted());
535 EXPECT_CALL(item, GetDbHandle()) 511 EXPECT_CALL(item, GetDbHandle())
(...skipping 19 matching lines...) Expand all
555 protected: 531 protected:
556 // Key test variable; we'll keep it available to sub-classes. 532 // Key test variable; we'll keep it available to sub-classes.
557 scoped_refptr<DownloadManagerImpl> download_manager_; 533 scoped_refptr<DownloadManagerImpl> download_manager_;
558 534
559 private: 535 private:
560 MessageLoopForUI message_loop_; 536 MessageLoopForUI message_loop_;
561 content::TestBrowserThread ui_thread_; 537 content::TestBrowserThread ui_thread_;
562 content::TestBrowserThread file_thread_; 538 content::TestBrowserThread file_thread_;
563 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; 539 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
564 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; 540 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
565 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_;
566 scoped_ptr<MockBrowserContext> mock_browser_context_; 541 scoped_ptr<MockBrowserContext> mock_browser_context_;
567 scoped_ptr<MockDownloadManagerObserver> observer_; 542 scoped_ptr<MockDownloadManagerObserver> observer_;
568 int next_download_id_; 543 int next_download_id_;
569 544
570 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 545 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
571 }; 546 };
572 547
573 // Confirm the appropriate invocations occur when you start a download. 548 // Confirm the appropriate invocations occur when you start a download.
574 TEST_F(DownloadManagerTest, StartDownload) { 549 TEST_F(DownloadManagerTest, StartDownload) {
575 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 550 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
576 scoped_ptr<content::ByteStreamReader> stream; 551 scoped_ptr<content::ByteStreamReader> stream;
577 int32 local_id(5); // Random value 552 int32 local_id(5); // Random value
578 553
579 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id)); 554 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id));
580 555
581 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 556 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
582 .WillOnce(Return()); 557 .WillOnce(Return());
583 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) 558 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId())
584 .WillOnce(Return(content::DownloadId(this, local_id))); 559 .WillOnce(Return(content::DownloadId(this, local_id)));
585 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) 560 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
586 .WillOnce(Return(true)); 561 .WillOnce(Return(true));
587 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile(
588 info.get(), static_cast<content::ByteStreamReader*>(NULL),
589 download_manager_.get(), true, _, _));
590 562
591 download_manager_->StartDownload(info.Pass(), stream.Pass()); 563 download_manager_->StartDownload(info.Pass(), stream.Pass());
592 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); 564 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id));
593 } 565 }
594 566
595 // Do the results of an OnDownloadInterrupted get passed through properly 567 // Confirm that calling DelegateStart behaves properly if the delegate
596 // to the DownloadItem? 568 // blocks starting.
597 TEST_F(DownloadManagerTest, OnDownloadInterrupted) { 569 TEST_F(DownloadManagerTest, DelegateStart_True) {
598 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
599 .WillOnce(Return());
600 // Put a mock we have a handle to on the download manager. 570 // Put a mock we have a handle to on the download manager.
601 MockDownloadItemImpl& item(AddItemToManager()); 571 MockDownloadItemImpl& item(AddItemToManager());
602 int download_id = item.GetId();
603 572
604 content::DownloadInterruptReason reason( 573 EXPECT_CALL(GetMockDownloadManagerDelegate(),
605 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 574 DetermineDownloadTarget(&item, _))
575 .WillOnce(Return(true));
576 DelegateStart(&item);
577 }
606 578
607 EXPECT_CALL(item, Interrupt(reason)); 579 // Confirm that calling DelegateStart behaves properly if the delegate
608 download_manager_->OnDownloadInterrupted(download_id, reason); 580 // allows starting. This also tests OnDownloadTargetDetermined.
609 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id)); 581 TEST_F(DownloadManagerTest, DelegateStart_False) {
582 // Put a mock we have a handle to on the download manager.
583 MockDownloadItemImpl& item(AddItemToManager());
584
585 EXPECT_CALL(GetMockDownloadManagerDelegate(),
586 DetermineDownloadTarget(&item, _))
587 .WillOnce(Return(false));
588 EXPECT_CALL(item, OnDownloadTargetDetermined(
589 _, content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
590 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, _));
591 FilePath null_path;
592 EXPECT_CALL(item, GetForcedFilePath())
593 .WillOnce(ReturnRef(null_path));
594 DelegateStart(&item);
610 } 595 }
611 596
612 // Does DownloadStopped remove Download from appropriate queues? 597 // Does DownloadStopped remove Download from appropriate queues?
613 // This test tests non-persisted downloads. 598 // This test tests non-persisted downloads.
614 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { 599 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
615 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
616 .WillOnce(Return());
617 // Put a mock we have a handle to on the download manager. 600 // Put a mock we have a handle to on the download manager.
618 MockDownloadItemImpl& item(AddItemToManager()); 601 MockDownloadItemImpl& item(AddItemToManager());
619 602
620 EXPECT_CALL(item, IsPersisted()) 603 EXPECT_CALL(item, IsPersisted())
621 .WillRepeatedly(Return(false)); 604 .WillRepeatedly(Return(false));
622 EXPECT_CALL(item, GetState()) 605 EXPECT_CALL(item, GetState())
623 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 606 .WillRepeatedly(Return(DownloadItem::CANCELLED));
624 EXPECT_CALL(item, GetDbHandle()) 607 EXPECT_CALL(item, GetDbHandle())
625 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); 608 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
626 609
627 EXPECT_CALL(item, OffThreadCancel());
628 DownloadStopped(&item); 610 DownloadStopped(&item);
629 // TODO(rdsmith): Confirm that the download item is no longer on the 611 // TODO(rdsmith): Confirm that the download item is no longer on the
630 // active list by calling download_manager_->GetActiveDownloadItem(id). 612 // active list by calling download_manager_->GetActiveDownloadItem(id).
631 // Currently, the item is left on the active list for rendez-vous with 613 // Currently, the item is left on the active list for rendez-vous with
632 // the history system :-{. 614 // the history system :-{.
633 } 615 }
634 616
635 // Does DownloadStopped remove Download from appropriate queues? 617 // Does DownloadStopped remove Download from appropriate queues?
636 // This test tests persisted downloads. 618 // This test tests persisted downloads.
637 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { 619 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
638 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
639 .WillOnce(Return());
640 // Put a mock we have a handle to on the download manager. 620 // Put a mock we have a handle to on the download manager.
641 MockDownloadItemImpl& item(AddItemToManager()); 621 MockDownloadItemImpl& item(AddItemToManager());
642 int download_id = item.GetId(); 622 int download_id = item.GetId();
643 int64 db_handle = 0x7; 623 int64 db_handle = 0x7;
644 EXPECT_CALL(item, GetExternalData(_)) 624 EXPECT_CALL(item, GetExternalData(_))
645 .WillOnce(Return(static_cast<DownloadItem::ExternalData*>(NULL))); 625 .WillOnce(Return(static_cast<DownloadItem::ExternalData*>(NULL)));
646 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 626 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
647 .WillOnce(Return()); 627 .WillOnce(Return());
648 AddItemToHistory(item, db_handle); 628 AddItemToHistory(item, db_handle);
649 629
650 EXPECT_CALL(item, IsPersisted()) 630 EXPECT_CALL(item, IsPersisted())
651 .WillRepeatedly(Return(true)); 631 .WillRepeatedly(Return(true));
652 EXPECT_CALL(GetMockDownloadManagerDelegate(), 632 EXPECT_CALL(GetMockDownloadManagerDelegate(),
653 UpdateItemInPersistentStore(&item)); 633 UpdateItemInPersistentStore(&item));
654 EXPECT_CALL(item, GetState()) 634 EXPECT_CALL(item, GetState())
655 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 635 .WillRepeatedly(Return(DownloadItem::CANCELLED));
656 EXPECT_CALL(item, GetDbHandle()) 636 EXPECT_CALL(item, GetDbHandle())
657 .WillRepeatedly(Return(db_handle)); 637 .WillRepeatedly(Return(db_handle));
658 638
659 EXPECT_CALL(item, OffThreadCancel());
660 DownloadStopped(&item); 639 DownloadStopped(&item);
661 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); 640 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
662 } 641 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698