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