| OLD | NEW |
| 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" | 21 #include "content/browser/download/download_file_factory.h" |
| 22 #include "content/browser/download/download_item_factory.h" | 22 #include "content/browser/download/download_item_factory.h" |
| 23 #include "content/browser/download/download_item_impl.h" | 23 #include "content/browser/download/download_item_impl.h" |
| 24 #include "content/browser/download/download_item_impl_delegate.h" | 24 #include "content/browser/download/download_item_impl_delegate.h" |
| 25 #include "content/browser/download/download_manager_impl.h" | 25 #include "content/browser/download/download_manager_impl.h" |
| 26 #include "content/browser/download/download_request_handle.h" | 26 #include "content/browser/download/download_request_handle.h" |
| 27 #include "content/browser/download/mock_download_file.h" | 27 #include "content/browser/download/mock_download_file.h" |
| 28 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/download_interrupt_reasons.h" | 29 #include "content/public/browser/download_interrupt_reasons.h" |
| 30 #include "content/public/browser/download_item.h" | 30 #include "content/public/browser/download_item.h" |
| 31 #include "content/public/browser/download_manager_delegate.h" | 31 #include "content/public/browser/download_manager_delegate.h" |
| 32 #include "content/public/test/mock_download_item.h" | 32 #include "content/public/test/mock_download_item.h" |
| 33 #include "content/public/test/test_browser_context.h" | 33 #include "content/public/test/test_browser_context.h" |
| 34 #include "content/public/test/test_browser_thread.h" | 34 #include "content/public/test/test_browser_thread.h" |
| 35 #include "net/base/net_log.h" |
| 35 #include "net/base/net_util.h" | 36 #include "net/base/net_util.h" |
| 36 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
| 37 #include "testing/gmock_mutant.h" | 38 #include "testing/gmock_mutant.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 39 | 40 |
| 40 using ::testing::AllOf; | 41 using ::testing::AllOf; |
| 41 using ::testing::DoAll; | 42 using ::testing::DoAll; |
| 42 using ::testing::Eq; | 43 using ::testing::Eq; |
| 43 using ::testing::Ref; | 44 using ::testing::Ref; |
| 44 using ::testing::Return; | 45 using ::testing::Return; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 MOCK_METHOD0(CanShowInFolder, bool()); | 83 MOCK_METHOD0(CanShowInFolder, bool()); |
| 83 MOCK_METHOD0(CanOpenDownload, bool()); | 84 MOCK_METHOD0(CanOpenDownload, bool()); |
| 84 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); | 85 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); |
| 85 MOCK_METHOD0(OpenDownload, void()); | 86 MOCK_METHOD0(OpenDownload, void()); |
| 86 MOCK_METHOD0(ShowDownloadInShell, void()); | 87 MOCK_METHOD0(ShowDownloadInShell, void()); |
| 87 MOCK_METHOD0(DangerousDownloadValidated, void()); | 88 MOCK_METHOD0(DangerousDownloadValidated, void()); |
| 88 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); | 89 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); |
| 89 MOCK_METHOD1(Cancel, void(bool)); | 90 MOCK_METHOD1(Cancel, void(bool)); |
| 90 MOCK_METHOD0(MarkAsComplete, void()); | 91 MOCK_METHOD0(MarkAsComplete, void()); |
| 91 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); | 92 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); |
| 92 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&)); | 93 MOCK_METHOD1(OnAllDataSaved, void(const std::string&)); |
| 93 MOCK_METHOD0(OnDownloadedFileRemoved, void()); | 94 MOCK_METHOD0(OnDownloadedFileRemoved, void()); |
| 94 MOCK_METHOD0(MaybeCompleteDownload, void()); | 95 MOCK_METHOD0(MaybeCompleteDownload, void()); |
| 96 virtual void Start( |
| 97 scoped_ptr<content::DownloadFile> download_file) OVERRIDE { |
| 98 MockStart(download_file.get()); |
| 99 } |
| 100 |
| 101 MOCK_METHOD1(MockStart, void(content::DownloadFile*)); |
| 102 |
| 95 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); | 103 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); |
| 96 MOCK_METHOD1(Delete, void(DeleteReason)); | 104 MOCK_METHOD1(Delete, void(DeleteReason)); |
| 97 MOCK_METHOD0(Remove, void()); | 105 MOCK_METHOD0(Remove, void()); |
| 98 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); | 106 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); |
| 99 MOCK_CONST_METHOD0(CurrentSpeed, int64()); | 107 MOCK_CONST_METHOD0(CurrentSpeed, int64()); |
| 100 MOCK_CONST_METHOD0(PercentComplete, int()); | 108 MOCK_CONST_METHOD0(PercentComplete, int()); |
| 101 MOCK_CONST_METHOD0(AllDataSaved, bool()); | 109 MOCK_CONST_METHOD0(AllDataSaved, bool()); |
| 102 MOCK_METHOD0(TogglePause, void()); | 110 MOCK_METHOD0(TogglePause, void()); |
| 103 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); | 111 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); |
| 104 MOCK_CONST_METHOD0(IsPartialDownload, bool()); | 112 MOCK_CONST_METHOD0(IsPartialDownload, bool()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); | 161 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); |
| 154 MOCK_CONST_METHOD0(GetETag, const std::string&()); | 162 MOCK_CONST_METHOD0(GetETag, const std::string&()); |
| 155 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); | 163 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); |
| 156 MOCK_CONST_METHOD0(GetPersistentStoreInfo, | 164 MOCK_CONST_METHOD0(GetPersistentStoreInfo, |
| 157 content::DownloadPersistentStoreInfo()); | 165 content::DownloadPersistentStoreInfo()); |
| 158 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); | 166 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); |
| 159 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); | 167 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); |
| 160 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); | 168 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); |
| 161 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); | 169 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); |
| 162 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); | 170 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); |
| 163 MOCK_METHOD0(OffThreadCancel, void()); | |
| 164 MOCK_CONST_METHOD1(DebugString, std::string(bool)); | 171 MOCK_CONST_METHOD1(DebugString, std::string(bool)); |
| 165 MOCK_METHOD0(MockDownloadOpenForTesting, void()); | 172 MOCK_METHOD0(MockDownloadOpenForTesting, void()); |
| 166 }; | 173 }; |
| 167 | 174 |
| 168 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { | 175 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { |
| 169 public: | 176 public: |
| 170 MockDownloadManagerDelegate(); | 177 MockDownloadManagerDelegate(); |
| 171 virtual ~MockDownloadManagerDelegate(); | 178 virtual ~MockDownloadManagerDelegate(); |
| 172 | 179 |
| 173 MOCK_METHOD0(Shutdown, void()); | 180 MOCK_METHOD0(Shutdown, void()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 192 FilePath*, FilePath*, bool*)); | 199 FilePath*, FilePath*, bool*)); |
| 193 MOCK_METHOD5(ChooseSavePath, void( | 200 MOCK_METHOD5(ChooseSavePath, void( |
| 194 WebContents*, const FilePath&, const FilePath::StringType&, | 201 WebContents*, const FilePath&, const FilePath::StringType&, |
| 195 bool, const content::SavePackagePathPickedCallback&)); | 202 bool, const content::SavePackagePathPickedCallback&)); |
| 196 }; | 203 }; |
| 197 | 204 |
| 198 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} | 205 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} |
| 199 | 206 |
| 200 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} | 207 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} |
| 201 | 208 |
| 202 class MockDownloadFileManager : public DownloadFileManager { | |
| 203 public: | |
| 204 MockDownloadFileManager(); | |
| 205 | |
| 206 void CreateDownloadFile( | |
| 207 scoped_ptr<DownloadCreateInfo> info, | |
| 208 scoped_ptr<content::ByteStreamReader> stream, | |
| 209 scoped_refptr<content::DownloadManager> download_manager, | |
| 210 bool hash_needed, | |
| 211 const net::BoundNetLog& bound_net_log, | |
| 212 const CreateDownloadFileCallback& callback) OVERRIDE { | |
| 213 // Note that scoped_refptr<> on download manager is also stripped | |
| 214 // to make mock comparisons easier. Comparing the scoped_refptr<> | |
| 215 // works, but holds a reference to the DownloadManager until | |
| 216 // MockDownloadFileManager destruction, which messes up destruction | |
| 217 // testing. | |
| 218 MockCreateDownloadFile(info.get(), stream.get(), download_manager.get(), | |
| 219 hash_needed, bound_net_log, callback); | |
| 220 } | |
| 221 | |
| 222 MOCK_METHOD6(MockCreateDownloadFile, void( | |
| 223 DownloadCreateInfo* info, | |
| 224 content::ByteStreamReader* stream, | |
| 225 content::DownloadManager* download_manager, | |
| 226 bool hash_needed, | |
| 227 const net::BoundNetLog& bound_net_log, | |
| 228 const CreateDownloadFileCallback& callback)); | |
| 229 MOCK_METHOD0(Shutdown, void()); | |
| 230 MOCK_METHOD1(CancelDownload, void(content::DownloadId)); | |
| 231 MOCK_METHOD2(CompleteDownload, void(content::DownloadId, | |
| 232 const base::Closure&)); | |
| 233 MOCK_METHOD1(OnDownloadManagerShutdown, void(content::DownloadManager*)); | |
| 234 MOCK_METHOD4(RenameDownloadFile, void(content::DownloadId, | |
| 235 const FilePath&, | |
| 236 bool, | |
| 237 const RenameCompletionCallback&)); | |
| 238 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int()); | |
| 239 protected: | |
| 240 virtual ~MockDownloadFileManager(); | |
| 241 }; | |
| 242 | |
| 243 MockDownloadFileManager::MockDownloadFileManager() | |
| 244 : DownloadFileManager(NULL) {} | |
| 245 | |
| 246 MockDownloadFileManager::~MockDownloadFileManager() {} | |
| 247 | |
| 248 class MockDownloadItemFactory | 209 class MockDownloadItemFactory |
| 249 : public content::DownloadItemFactory, | 210 : public content::DownloadItemFactory, |
| 250 public base::SupportsWeakPtr<MockDownloadItemFactory> { | 211 public base::SupportsWeakPtr<MockDownloadItemFactory> { |
| 251 public: | 212 public: |
| 252 MockDownloadItemFactory(); | 213 MockDownloadItemFactory(); |
| 253 virtual ~MockDownloadItemFactory(); | 214 virtual ~MockDownloadItemFactory(); |
| 254 | 215 |
| 255 // Access to map of created items. | 216 // Access to map of created items. |
| 256 // TODO(rdsmith): Could add type (save page, persisted, etc.) | 217 // TODO(rdsmith): Could add type (save page, persisted, etc.) |
| 257 // functionality if it's ever needed by consumers. | 218 // functionality if it's ever needed by consumers. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 const DownloadCreateInfo& info, | 302 const DownloadCreateInfo& info, |
| 342 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 303 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
| 343 const net::BoundNetLog& bound_net_log) { | 304 const net::BoundNetLog& bound_net_log) { |
| 344 int local_id = info.download_id.local(); | 305 int local_id = info.download_id.local(); |
| 345 DCHECK(items_.find(local_id) == items_.end()); | 306 DCHECK(items_.find(local_id) == items_.end()); |
| 346 | 307 |
| 347 MockDownloadItemImpl* result = | 308 MockDownloadItemImpl* result = |
| 348 new StrictMock<MockDownloadItemImpl>(&item_delegate_); | 309 new StrictMock<MockDownloadItemImpl>(&item_delegate_); |
| 349 EXPECT_CALL(*result, GetId()) | 310 EXPECT_CALL(*result, GetId()) |
| 350 .WillRepeatedly(Return(local_id)); | 311 .WillRepeatedly(Return(local_id)); |
| 312 EXPECT_CALL(*result, GetGlobalId()) |
| 313 .WillRepeatedly(Return(content::DownloadId(delegate, local_id))); |
| 351 items_[local_id] = result; | 314 items_[local_id] = result; |
| 352 | 315 |
| 316 // Active items are created and then immediately are called to start |
| 317 // the download. |
| 318 EXPECT_CALL(*result, MockStart(_)); |
| 319 |
| 353 return result; | 320 return result; |
| 354 } | 321 } |
| 355 | 322 |
| 356 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( | 323 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( |
| 357 DownloadItemImplDelegate* delegate, | 324 DownloadItemImplDelegate* delegate, |
| 358 const FilePath& path, | 325 const FilePath& path, |
| 359 const GURL& url, | 326 const GURL& url, |
| 360 content::DownloadId download_id, | 327 content::DownloadId download_id, |
| 361 const std::string& mime_type, | 328 const std::string& mime_type, |
| 362 const net::BoundNetLog& bound_net_log) { | 329 const net::BoundNetLog& bound_net_log) { |
| 363 int local_id = download_id.local(); | 330 int local_id = download_id.local(); |
| 364 DCHECK(items_.find(local_id) == items_.end()); | 331 DCHECK(items_.find(local_id) == items_.end()); |
| 365 | 332 |
| 366 MockDownloadItemImpl* result = | 333 MockDownloadItemImpl* result = |
| 367 new StrictMock<MockDownloadItemImpl>(&item_delegate_); | 334 new StrictMock<MockDownloadItemImpl>(&item_delegate_); |
| 368 EXPECT_CALL(*result, GetId()) | 335 EXPECT_CALL(*result, GetId()) |
| 369 .WillRepeatedly(Return(local_id)); | 336 .WillRepeatedly(Return(local_id)); |
| 370 items_[local_id] = result; | 337 items_[local_id] = result; |
| 371 | 338 |
| 372 return result; | 339 return result; |
| 373 } | 340 } |
| 374 | 341 |
| 342 class MockDownloadFileFactory |
| 343 : public content::DownloadFileFactory, |
| 344 public base::SupportsWeakPtr<MockDownloadFileFactory> { |
| 345 public: |
| 346 MockDownloadFileFactory() {} |
| 347 virtual ~MockDownloadFileFactory() {} |
| 348 |
| 349 // Overridden method from DownloadFileFactory |
| 350 MOCK_METHOD9(MockCreateFile, content::DownloadFile*( |
| 351 const content::DownloadSaveInfo&, |
| 352 const FilePath&, |
| 353 const GURL&, const GURL&, int64, bool, |
| 354 content::ByteStreamReader*, |
| 355 const net::BoundNetLog&, |
| 356 base::WeakPtr<content::DownloadDestinationObserver>)); |
| 357 |
| 358 virtual content::DownloadFile* CreateFile( |
| 359 scoped_ptr<content::DownloadSaveInfo> save_info, |
| 360 const FilePath& default_download_directory, |
| 361 const GURL& url, |
| 362 const GURL& referrer_url, |
| 363 int64 received_bytes, |
| 364 bool calculate_hash, |
| 365 scoped_ptr<content::ByteStreamReader> stream, |
| 366 const net::BoundNetLog& bound_net_log, |
| 367 base::WeakPtr<content::DownloadDestinationObserver> observer) { |
| 368 return MockCreateFile(*save_info.get(), default_download_directory, url, |
| 369 referrer_url, received_bytes, calculate_hash, |
| 370 stream.get(), bound_net_log, observer); |
| 371 } |
| 372 }; |
| 373 |
| 375 class MockBrowserContext : public content::BrowserContext { | 374 class MockBrowserContext : public content::BrowserContext { |
| 376 public: | 375 public: |
| 377 MockBrowserContext() {} | 376 MockBrowserContext() {} |
| 378 ~MockBrowserContext() {} | 377 ~MockBrowserContext() {} |
| 379 | 378 |
| 380 MOCK_METHOD0(GetPath, FilePath()); | 379 MOCK_METHOD0(GetPath, FilePath()); |
| 381 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); | 380 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); |
| 382 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*()); | 381 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*()); |
| 383 MOCK_METHOD1(GetRequestContextForRenderProcess, | 382 MOCK_METHOD1(GetRequestContextForRenderProcess, |
| 384 net::URLRequestContextGetter*(int renderer_child_id)); | 383 net::URLRequestContextGetter*(int renderer_child_id)); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 419 |
| 421 DownloadManagerTest() | 420 DownloadManagerTest() |
| 422 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 421 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 423 file_thread_(content::BrowserThread::FILE, &message_loop_), | 422 file_thread_(content::BrowserThread::FILE, &message_loop_), |
| 424 next_download_id_(0) { | 423 next_download_id_(0) { |
| 425 } | 424 } |
| 426 | 425 |
| 427 // We tear down everything in TearDown(). | 426 // We tear down everything in TearDown(). |
| 428 ~DownloadManagerTest() {} | 427 ~DownloadManagerTest() {} |
| 429 | 428 |
| 430 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate, | 429 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate, |
| 431 // and MockDownloadFileManager, then create a DownloadManager that points | 430 // then create a DownloadManager that points |
| 432 // at all of those. | 431 // at all of those. |
| 433 virtual void SetUp() { | 432 virtual void SetUp() { |
| 434 DCHECK(!download_manager_.get()); | 433 DCHECK(!download_manager_.get()); |
| 435 | 434 |
| 436 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); | 435 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); |
| 436 mock_download_file_factory_ = (new MockDownloadFileFactory())->AsWeakPtr(); |
| 437 mock_download_manager_delegate_.reset( | 437 mock_download_manager_delegate_.reset( |
| 438 new StrictMock<MockDownloadManagerDelegate>); | 438 new StrictMock<MockDownloadManagerDelegate>); |
| 439 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) | 439 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) |
| 440 .WillOnce(Return()); | 440 .WillOnce(Return()); |
| 441 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>; | |
| 442 EXPECT_CALL(*mock_download_file_manager_.get(), | |
| 443 OnDownloadManagerShutdown(_)); | |
| 444 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); | 441 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); |
| 445 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) | 442 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) |
| 446 .WillRepeatedly(Return(false)); | 443 .WillRepeatedly(Return(false)); |
| 447 | 444 |
| 448 download_manager_ = new DownloadManagerImpl( | 445 download_manager_ = new DownloadManagerImpl(NULL); |
| 449 mock_download_file_manager_.get(), | 446 download_manager_->SetDownloadItemFactoryForTesting( |
| 450 scoped_ptr<content::DownloadItemFactory>( | 447 scoped_ptr<content::DownloadItemFactory>( |
| 451 mock_download_item_factory_.get()).Pass(), NULL); | 448 mock_download_item_factory_.get()).Pass()); |
| 449 download_manager_->SetDownloadFileFactoryForTesting( |
| 450 scoped_ptr<content::DownloadFileFactory>( |
| 451 mock_download_file_factory_.get()).Pass()); |
| 452 observer_.reset(new MockDownloadManagerObserver()); | 452 observer_.reset(new MockDownloadManagerObserver()); |
| 453 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) | 453 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) |
| 454 .WillOnce(Return()); | 454 .WillOnce(Return()); |
| 455 download_manager_->AddObserver(observer_.get()); | 455 download_manager_->AddObserver(observer_.get()); |
| 456 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); | 456 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); |
| 457 download_manager_->Init(mock_browser_context_.get()); | 457 download_manager_->Init(mock_browser_context_.get()); |
| 458 } | 458 } |
| 459 | 459 |
| 460 virtual void TearDown() { | 460 virtual void TearDown() { |
| 461 while (MockDownloadItemImpl* | 461 while (MockDownloadItemImpl* |
| 462 item = mock_download_item_factory_->PopItem()) { | 462 item = mock_download_item_factory_->PopItem()) { |
| 463 EXPECT_CALL(*item, GetSafetyState()) | 463 EXPECT_CALL(*item, GetSafetyState()) |
| 464 .WillOnce(Return(content::DownloadItem::SAFE)); | 464 .WillOnce(Return(content::DownloadItem::SAFE)); |
| 465 EXPECT_CALL(*item, IsPartialDownload()) | 465 EXPECT_CALL(*item, IsPartialDownload()) |
| 466 .WillOnce(Return(false)); | 466 .WillOnce(Return(false)); |
| 467 } | 467 } |
| 468 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) | 468 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) |
| 469 .WillOnce(Return()); | 469 .WillOnce(Return()); |
| 470 | 470 |
| 471 download_manager_->Shutdown(); | 471 download_manager_->Shutdown(); |
| 472 download_manager_ = NULL; | 472 download_manager_ = NULL; |
| 473 message_loop_.RunAllPending(); | 473 message_loop_.RunAllPending(); |
| 474 ASSERT_EQ(NULL, mock_download_item_factory_.get()); | 474 ASSERT_EQ(NULL, mock_download_item_factory_.get()); |
| 475 ASSERT_EQ(NULL, mock_download_file_factory_.get()); |
| 475 message_loop_.RunAllPending(); | 476 message_loop_.RunAllPending(); |
| 476 mock_download_manager_delegate_.reset(); | 477 mock_download_manager_delegate_.reset(); |
| 477 mock_download_file_manager_ = NULL; | |
| 478 mock_browser_context_.reset(); | 478 mock_browser_context_.reset(); |
| 479 } | 479 } |
| 480 | 480 |
| 481 // Returns download id. | 481 // Returns download id. |
| 482 MockDownloadItemImpl& AddItemToManager() { | 482 MockDownloadItemImpl& AddItemToManager() { |
| 483 DownloadCreateInfo info; | 483 DownloadCreateInfo info; |
| 484 | 484 |
| 485 static const char* kDownloadIdDomain = "Test download id domain"; | 485 static const char* kDownloadIdDomain = "Test download id domain"; |
| 486 | 486 |
| 487 // Args are ignored except for download id, so everything else can be | 487 // Args are ignored except for download id, so everything else can be |
| 488 // null. | 488 // null. |
| 489 int id = next_download_id_; | 489 int id = next_download_id_; |
| 490 ++next_download_id_; | 490 ++next_download_id_; |
| 491 info.download_id = content::DownloadId(kDownloadIdDomain, id); | 491 info.download_id = content::DownloadId(kDownloadIdDomain, id); |
| 492 info.request_handle = DownloadRequestHandle(); | 492 info.request_handle = DownloadRequestHandle(); |
| 493 download_manager_->CreateDownloadItem(&info); | 493 download_manager_->CreateDownloadItem(&info, net::BoundNetLog()); |
| 494 | 494 |
| 495 DCHECK(mock_download_item_factory_->GetItem(id)); | 495 DCHECK(mock_download_item_factory_->GetItem(id)); |
| 496 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); | 496 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); |
| 497 ON_CALL(item, GetId()) | 497 // Satisfy expectation. If the item is created in StartDownload(), |
| 498 .WillByDefault(Return(id)); | 498 // we call Start on it immediately, so we need to set that expectation |
| 499 // in the factory. |
| 500 item.Start(scoped_ptr<content::DownloadFile>()); |
| 499 | 501 |
| 500 return item; | 502 return item; |
| 501 } | 503 } |
| 502 | 504 |
| 503 MockDownloadItemImpl& GetMockDownloadItem(int id) { | 505 MockDownloadItemImpl& GetMockDownloadItem(int id) { |
| 504 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); | 506 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); |
| 505 | 507 |
| 506 DCHECK(itemp); | 508 DCHECK(itemp); |
| 507 return *itemp; | 509 return *itemp; |
| 508 } | 510 } |
| 509 | 511 |
| 510 void RemoveMockDownloadItem(int id) { | 512 void RemoveMockDownloadItem(int id) { |
| 511 // Owned by DownloadManager; should be deleted there. | 513 // Owned by DownloadManager; should be deleted there. |
| 512 mock_download_item_factory_->RemoveItem(id); | 514 mock_download_item_factory_->RemoveItem(id); |
| 513 } | 515 } |
| 514 | 516 |
| 515 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { | 517 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { |
| 516 return *mock_download_manager_delegate_; | 518 return *mock_download_manager_delegate_; |
| 517 } | 519 } |
| 518 | 520 |
| 519 MockDownloadFileManager& GetMockDownloadFileManager() { | |
| 520 return *mock_download_file_manager_; | |
| 521 } | |
| 522 | |
| 523 MockDownloadManagerObserver& GetMockObserver() { | 521 MockDownloadManagerObserver& GetMockObserver() { |
| 524 return *observer_; | 522 return *observer_; |
| 525 } | 523 } |
| 526 | 524 |
| 527 // Probe at private internals. | 525 // Probe at private internals. |
| 528 void DownloadStopped(DownloadItemImpl* item) { | 526 void DownloadStopped(DownloadItemImpl* item) { |
| 529 download_manager_->DownloadStopped(item); | 527 download_manager_->DownloadStopped(item); |
| 530 } | 528 } |
| 531 | 529 |
| 530 void DownloadTargetDeterminedCallback( |
| 531 const FilePath& target_path, |
| 532 content::DownloadItem::TargetDisposition disposition, |
| 533 content::DownloadDangerType danger_type, |
| 534 const FilePath& intermediate_path) { |
| 535 callback_called_ = true; |
| 536 target_path_ = target_path; |
| 537 target_disposition_ = disposition; |
| 538 danger_type_ = danger_type; |
| 539 intermediate_path_ = intermediate_path; |
| 540 } |
| 541 |
| 542 void DetermineDownloadTarget(DownloadItemImpl* item) { |
| 543 download_manager_->DetermineDownloadTarget( |
| 544 item, base::Bind( |
| 545 &DownloadManagerTest::DownloadTargetDeterminedCallback, |
| 546 base::Unretained(this))); |
| 547 } |
| 548 |
| 532 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { | 549 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { |
| 533 // For DCHECK in AddDownloadItemToHistory. Don't want to use | 550 // For DCHECK in AddDownloadItemToHistory. Don't want to use |
| 534 // WillRepeatedly as it may have to return true after this. | 551 // WillRepeatedly as it may have to return true after this. |
| 535 if (DCHECK_IS_ON()) | 552 if (DCHECK_IS_ON()) |
| 536 EXPECT_CALL(item, IsPersisted()) | 553 EXPECT_CALL(item, IsPersisted()) |
| 537 .WillRepeatedly(Return(false)); | 554 .WillRepeatedly(Return(false)); |
| 538 | 555 |
| 539 EXPECT_CALL(item, SetDbHandle(db_handle)); | 556 EXPECT_CALL(item, SetDbHandle(db_handle)); |
| 540 EXPECT_CALL(item, SetIsPersisted()); | 557 EXPECT_CALL(item, SetIsPersisted()); |
| 541 EXPECT_CALL(item, GetDbHandle()) | 558 EXPECT_CALL(item, GetDbHandle()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 554 // History addition should result in a call into MaybeCompleteDownload(). | 571 // History addition should result in a call into MaybeCompleteDownload(). |
| 555 EXPECT_CALL(item, MaybeCompleteDownload()) | 572 EXPECT_CALL(item, MaybeCompleteDownload()) |
| 556 .WillOnce(Return()); | 573 .WillOnce(Return()); |
| 557 | 574 |
| 558 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle); | 575 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle); |
| 559 } | 576 } |
| 560 | 577 |
| 561 protected: | 578 protected: |
| 562 // Key test variable; we'll keep it available to sub-classes. | 579 // Key test variable; we'll keep it available to sub-classes. |
| 563 scoped_refptr<DownloadManagerImpl> download_manager_; | 580 scoped_refptr<DownloadManagerImpl> download_manager_; |
| 581 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_; |
| 582 |
| 583 // Target detetermined callback. |
| 584 bool callback_called_; |
| 585 FilePath target_path_; |
| 586 content::DownloadItem::TargetDisposition target_disposition_; |
| 587 content::DownloadDangerType danger_type_; |
| 588 FilePath intermediate_path_; |
| 564 | 589 |
| 565 private: | 590 private: |
| 566 MessageLoopForUI message_loop_; | 591 MessageLoopForUI message_loop_; |
| 567 content::TestBrowserThread ui_thread_; | 592 content::TestBrowserThread ui_thread_; |
| 568 content::TestBrowserThread file_thread_; | 593 content::TestBrowserThread file_thread_; |
| 569 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; | 594 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; |
| 570 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; | 595 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; |
| 571 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_; | |
| 572 scoped_ptr<MockBrowserContext> mock_browser_context_; | 596 scoped_ptr<MockBrowserContext> mock_browser_context_; |
| 573 scoped_ptr<MockDownloadManagerObserver> observer_; | 597 scoped_ptr<MockDownloadManagerObserver> observer_; |
| 574 int next_download_id_; | 598 int next_download_id_; |
| 575 | 599 |
| 576 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 600 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 577 }; | 601 }; |
| 578 | 602 |
| 579 // Confirm the appropriate invocations occur when you start a download. | 603 // Confirm the appropriate invocations occur when you start a download. |
| 580 TEST_F(DownloadManagerTest, StartDownload) { | 604 TEST_F(DownloadManagerTest, StartDownload) { |
| 581 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 605 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 582 scoped_ptr<content::ByteStreamReader> stream; | 606 scoped_ptr<content::ByteStreamReader> stream; |
| 583 int32 local_id(5); // Random value | 607 int32 local_id(5); // Random value |
| 584 FilePath download_path(FILE_PATH_LITERAL("download/path")); | 608 FilePath download_path(FILE_PATH_LITERAL("download/path")); |
| 585 | 609 |
| 586 EXPECT_FALSE(download_manager_->GetDownload(local_id)); | 610 EXPECT_FALSE(download_manager_->GetDownload(local_id)); |
| 587 | 611 |
| 588 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | 612 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) |
| 589 .WillOnce(Return()); | 613 .WillOnce(Return()); |
| 590 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) | 614 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) |
| 591 .WillOnce(Return(content::DownloadId(this, local_id))); | 615 .WillOnce(Return(content::DownloadId(this, local_id))); |
| 616 |
| 617 // Doing nothing will set the default download directory to null. |
| 618 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _)); |
| 592 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) | 619 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) |
| 593 .WillOnce(Return(true)); | 620 .WillOnce(Return(true)); |
| 594 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_,_,_,_)) | 621 EXPECT_CALL(*mock_download_file_factory_.get(), |
| 595 .WillOnce(SetArgPointee<2>(download_path)); | 622 MockCreateFile(Ref(*info->save_info.get()), _, _, _, 0, true, |
| 596 | 623 stream.get(), _, _)); |
| 597 // The CreateDownloadFile call should specify a DownloadCreateInfo that | |
| 598 // includes the result of the GetSaveDir() call. | |
| 599 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile( | |
| 600 DownloadCreateInfoWithDefaultPath(info.get(), download_path), | |
| 601 static_cast<content::ByteStreamReader*>(NULL), | |
| 602 download_manager_.get(), true, _, _)); | |
| 603 | 624 |
| 604 download_manager_->StartDownload(info.Pass(), stream.Pass()); | 625 download_manager_->StartDownload(info.Pass(), stream.Pass()); |
| 605 EXPECT_TRUE(download_manager_->GetDownload(local_id)); | 626 EXPECT_TRUE(download_manager_->GetDownload(local_id)); |
| 606 } | 627 } |
| 607 | 628 |
| 608 // Do the results of an OnDownloadInterrupted get passed through properly | 629 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate |
| 609 // to the DownloadItem? | 630 // blocks starting. |
| 610 TEST_F(DownloadManagerTest, OnDownloadInterrupted) { | 631 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { |
| 611 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | |
| 612 .WillOnce(Return()); | |
| 613 // Put a mock we have a handle to on the download manager. | 632 // Put a mock we have a handle to on the download manager. |
| 614 MockDownloadItemImpl& item(AddItemToManager()); | 633 MockDownloadItemImpl& item(AddItemToManager()); |
| 615 int download_id = item.GetId(); | |
| 616 | 634 |
| 617 content::DownloadInterruptReason reason( | 635 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 618 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 636 DetermineDownloadTarget(&item, _)) |
| 637 .WillOnce(Return(true)); |
| 638 DetermineDownloadTarget(&item); |
| 639 } |
| 619 | 640 |
| 620 EXPECT_CALL(item, Interrupt(reason)); | 641 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate |
| 621 download_manager_->OnDownloadInterrupted(download_id, reason); | 642 // allows starting. This also tests OnDownloadTargetDetermined. |
| 622 EXPECT_EQ(&item, download_manager_->GetDownload(download_id)); | 643 TEST_F(DownloadManagerTest, DetermineDownloadTarget_False) { |
| 644 // Put a mock we have a handle to on the download manager. |
| 645 MockDownloadItemImpl& item(AddItemToManager()); |
| 646 |
| 647 FilePath path(FILE_PATH_LITERAL("random_filepath.txt")); |
| 648 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 649 DetermineDownloadTarget(&item, _)) |
| 650 .WillOnce(Return(false)); |
| 651 EXPECT_CALL(item, GetForcedFilePath()) |
| 652 .WillOnce(ReturnRef(path)); |
| 653 |
| 654 // Confirm that the callback was called with the right values in this case. |
| 655 callback_called_ = false; |
| 656 DetermineDownloadTarget(&item); |
| 657 EXPECT_TRUE(callback_called_); |
| 658 EXPECT_EQ(path, target_path_); |
| 659 EXPECT_EQ(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 660 target_disposition_); |
| 661 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_); |
| 662 EXPECT_EQ(path, intermediate_path_); |
| 623 } | 663 } |
| 624 | 664 |
| 625 // Does DownloadStopped remove Download from appropriate queues? | 665 // Does DownloadStopped remove Download from appropriate queues? |
| 626 // This test tests non-persisted downloads. | 666 // This test tests non-persisted downloads. |
| 627 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { | 667 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { |
| 628 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | |
| 629 .WillOnce(Return()); | |
| 630 // Put a mock we have a handle to on the download manager. | 668 // Put a mock we have a handle to on the download manager. |
| 631 MockDownloadItemImpl& item(AddItemToManager()); | 669 MockDownloadItemImpl& item(AddItemToManager()); |
| 632 | 670 |
| 633 EXPECT_CALL(item, IsPersisted()) | 671 EXPECT_CALL(item, IsPersisted()) |
| 634 .WillRepeatedly(Return(false)); | 672 .WillRepeatedly(Return(false)); |
| 635 EXPECT_CALL(item, GetState()) | 673 EXPECT_CALL(item, GetState()) |
| 636 .WillRepeatedly(Return(DownloadItem::CANCELLED)); | 674 .WillRepeatedly(Return(DownloadItem::CANCELLED)); |
| 637 EXPECT_CALL(item, GetDbHandle()) | 675 EXPECT_CALL(item, GetDbHandle()) |
| 638 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); | 676 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); |
| 639 | 677 |
| 640 EXPECT_CALL(item, OffThreadCancel()); | |
| 641 DownloadStopped(&item); | 678 DownloadStopped(&item); |
| 642 } | 679 } |
| 643 | 680 |
| 644 // Does DownloadStopped remove Download from appropriate queues? | 681 // Does DownloadStopped remove Download from appropriate queues? |
| 645 // This test tests persisted downloads. | 682 // This test tests persisted downloads. |
| 646 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { | 683 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { |
| 647 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | |
| 648 .WillOnce(Return()); | |
| 649 // Put a mock we have a handle to on the download manager. | 684 // Put a mock we have a handle to on the download manager. |
| 650 MockDownloadItemImpl& item(AddItemToManager()); | 685 MockDownloadItemImpl& item(AddItemToManager()); |
| 651 int64 db_handle = 0x7; | 686 int64 db_handle = 0x7; |
| 652 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) | 687 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) |
| 653 .WillOnce(Return()); | 688 .WillOnce(Return()); |
| 654 AddItemToHistory(item, db_handle); | 689 AddItemToHistory(item, db_handle); |
| 655 | 690 |
| 656 EXPECT_CALL(item, IsPersisted()) | 691 EXPECT_CALL(item, IsPersisted()) |
| 657 .WillRepeatedly(Return(true)); | 692 .WillRepeatedly(Return(true)); |
| 658 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 693 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 659 UpdateItemInPersistentStore(&item)); | 694 UpdateItemInPersistentStore(&item)); |
| 660 EXPECT_CALL(item, GetState()) | 695 EXPECT_CALL(item, GetState()) |
| 661 .WillRepeatedly(Return(DownloadItem::CANCELLED)); | 696 .WillRepeatedly(Return(DownloadItem::CANCELLED)); |
| 662 EXPECT_CALL(item, GetDbHandle()) | 697 EXPECT_CALL(item, GetDbHandle()) |
| 663 .WillRepeatedly(Return(db_handle)); | 698 .WillRepeatedly(Return(db_handle)); |
| 664 | 699 |
| 665 EXPECT_CALL(item, OffThreadCancel()); | |
| 666 DownloadStopped(&item); | 700 DownloadStopped(&item); |
| 667 } | 701 } |
| OLD | NEW |