| 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" | |
| 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 Loading... |
| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); | 153 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); |
| 147 MOCK_CONST_METHOD0(GetETag, const std::string&()); | 154 MOCK_CONST_METHOD0(GetETag, const std::string&()); |
| 148 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); | 155 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); |
| 149 MOCK_CONST_METHOD0(GetPersistentStoreInfo, | 156 MOCK_CONST_METHOD0(GetPersistentStoreInfo, |
| 150 content::DownloadPersistentStoreInfo()); | 157 content::DownloadPersistentStoreInfo()); |
| 151 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); | 158 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); |
| 152 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); | 159 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); |
| 153 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); | 160 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); |
| 154 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); | 161 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); |
| 155 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); | 162 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); |
| 156 MOCK_METHOD0(OffThreadCancel, void()); | |
| 157 MOCK_CONST_METHOD1(DebugString, std::string(bool)); | 163 MOCK_CONST_METHOD1(DebugString, std::string(bool)); |
| 158 MOCK_METHOD0(MockDownloadOpenForTesting, void()); | 164 MOCK_METHOD0(MockDownloadOpenForTesting, void()); |
| 159 }; | 165 }; |
| 160 | 166 |
| 161 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { | 167 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { |
| 162 public: | 168 public: |
| 163 MockDownloadManagerDelegate(); | 169 MockDownloadManagerDelegate(); |
| 164 virtual ~MockDownloadManagerDelegate(); | 170 virtual ~MockDownloadManagerDelegate(); |
| 165 | 171 |
| 166 MOCK_METHOD0(Shutdown, void()); | 172 MOCK_METHOD0(Shutdown, void()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 184 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*)); | 190 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*)); |
| 185 MOCK_METHOD5(ChooseSavePath, void( | 191 MOCK_METHOD5(ChooseSavePath, void( |
| 186 WebContents*, const FilePath&, const FilePath::StringType&, | 192 WebContents*, const FilePath&, const FilePath::StringType&, |
| 187 bool, const content::SavePackagePathPickedCallback&)); | 193 bool, const content::SavePackagePathPickedCallback&)); |
| 188 }; | 194 }; |
| 189 | 195 |
| 190 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} | 196 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} |
| 191 | 197 |
| 192 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} | 198 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} |
| 193 | 199 |
| 194 class MockDownloadFileManager : public DownloadFileManager { | 200 class NullDownloadItemImplDelegate : public DownloadItemImplDelegate { |
| 195 public: | 201 public: |
| 196 MockDownloadFileManager(); | 202 // Safe to null this out even if it doesn't do anything because none |
| 197 | 203 // of these functions will ever be called; this class just exists |
| 198 void CreateDownloadFile( | 204 // to have something to pass to the DownloadItemImpl base class |
| 199 scoped_ptr<DownloadCreateInfo> info, | 205 // of MockDownloadItemImpl. |
| 200 scoped_ptr<content::ByteStreamReader> stream, | 206 virtual void DelegateStart(DownloadItemImpl* download) OVERRIDE { |
| 201 scoped_refptr<content::DownloadManager> download_manager, | 207 NOTREACHED(); |
| 202 bool hash_needed, | |
| 203 const net::BoundNetLog& bound_net_log, | |
| 204 const CreateDownloadFileCallback& callback) OVERRIDE { | |
| 205 // Note that scoped_refptr<> on download manager is also stripped | |
| 206 // to make mock comparisons easier. Comparing the scoped_refptr<> | |
| 207 // works, but holds a reference to the DownloadManager until | |
| 208 // MockDownloadFileManager destruction, which messes up destruction | |
| 209 // testing. | |
| 210 MockCreateDownloadFile(info.get(), stream.get(), download_manager.get(), | |
| 211 hash_needed, bound_net_log, callback); | |
| 212 } | 208 } |
| 213 | |
| 214 MOCK_METHOD6(MockCreateDownloadFile, void( | |
| 215 DownloadCreateInfo* info, | |
| 216 content::ByteStreamReader* stream, | |
| 217 content::DownloadManager* download_manager, | |
| 218 bool hash_needed, | |
| 219 const net::BoundNetLog& bound_net_log, | |
| 220 const CreateDownloadFileCallback& callback)); | |
| 221 MOCK_METHOD0(Shutdown, void()); | |
| 222 MOCK_METHOD1(CancelDownload, void(content::DownloadId)); | |
| 223 MOCK_METHOD2(CompleteDownload, void(content::DownloadId, | |
| 224 const base::Closure&)); | |
| 225 MOCK_METHOD1(OnDownloadManagerShutdown, void(content::DownloadManager*)); | |
| 226 MOCK_METHOD4(RenameDownloadFile, void(content::DownloadId, | |
| 227 const FilePath&, | |
| 228 bool, | |
| 229 const RenameCompletionCallback&)); | |
| 230 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int()); | |
| 231 protected: | |
| 232 virtual ~MockDownloadFileManager(); | |
| 233 }; | 209 }; |
| 234 | 210 |
| 235 MockDownloadFileManager::MockDownloadFileManager() | |
| 236 : DownloadFileManager(NULL) {} | |
| 237 | |
| 238 MockDownloadFileManager::~MockDownloadFileManager() {} | |
| 239 | |
| 240 class MockDownloadItemFactory | 211 class MockDownloadItemFactory |
| 241 : public content::DownloadItemFactory, | 212 : public content::DownloadItemFactory, |
| 242 public base::SupportsWeakPtr<MockDownloadItemFactory> { | 213 public base::SupportsWeakPtr<MockDownloadItemFactory> { |
| 243 public: | 214 public: |
| 244 MockDownloadItemFactory(); | 215 MockDownloadItemFactory(); |
| 245 virtual ~MockDownloadItemFactory(); | 216 virtual ~MockDownloadItemFactory(); |
| 246 | 217 |
| 247 // Access to map of created items. | 218 // Access to map of created items. |
| 248 // TODO(rdsmith): Could add type (save page, persisted, etc.) | 219 // TODO(rdsmith): Could add type (save page, persisted, etc.) |
| 249 // functionality if it's ever needed by consumers. | 220 // functionality if it's ever needed by consumers. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 273 virtual DownloadItemImpl* CreateSavePageItem( | 244 virtual DownloadItemImpl* CreateSavePageItem( |
| 274 DownloadItemImplDelegate* delegate, | 245 DownloadItemImplDelegate* delegate, |
| 275 const FilePath& path, | 246 const FilePath& path, |
| 276 const GURL& url, | 247 const GURL& url, |
| 277 content::DownloadId download_id, | 248 content::DownloadId download_id, |
| 278 const std::string& mime_type, | 249 const std::string& mime_type, |
| 279 const net::BoundNetLog& bound_net_log) OVERRIDE; | 250 const net::BoundNetLog& bound_net_log) OVERRIDE; |
| 280 | 251 |
| 281 private: | 252 private: |
| 282 std::map<int32, MockDownloadItemImpl*> items_; | 253 std::map<int32, MockDownloadItemImpl*> items_; |
| 283 DownloadItemImplDelegate item_delegate_; | 254 NullDownloadItemImplDelegate item_delegate_; |
| 284 | 255 |
| 285 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); | 256 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); |
| 286 }; | 257 }; |
| 287 | 258 |
| 288 MockDownloadItemFactory::MockDownloadItemFactory() {} | 259 MockDownloadItemFactory::MockDownloadItemFactory() {} |
| 289 | 260 |
| 290 MockDownloadItemFactory::~MockDownloadItemFactory() {} | 261 MockDownloadItemFactory::~MockDownloadItemFactory() {} |
| 291 | 262 |
| 292 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) { | 263 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) { |
| 293 if (items_.find(id) == items_.end()) | 264 if (items_.find(id) == items_.end()) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 const DownloadCreateInfo& info, | 304 const DownloadCreateInfo& info, |
| 334 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 305 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
| 335 const net::BoundNetLog& bound_net_log) { | 306 const net::BoundNetLog& bound_net_log) { |
| 336 int local_id = info.download_id.local(); | 307 int local_id = info.download_id.local(); |
| 337 DCHECK(items_.find(local_id) == items_.end()); | 308 DCHECK(items_.find(local_id) == items_.end()); |
| 338 | 309 |
| 339 MockDownloadItemImpl* result = | 310 MockDownloadItemImpl* result = |
| 340 new StrictMock<MockDownloadItemImpl>(&item_delegate_); | 311 new StrictMock<MockDownloadItemImpl>(&item_delegate_); |
| 341 EXPECT_CALL(*result, GetId()) | 312 EXPECT_CALL(*result, GetId()) |
| 342 .WillRepeatedly(Return(local_id)); | 313 .WillRepeatedly(Return(local_id)); |
| 314 EXPECT_CALL(*result, GetGlobalId()) |
| 315 .WillRepeatedly(Return(content::DownloadId(delegate, local_id))); |
| 343 items_[local_id] = result; | 316 items_[local_id] = result; |
| 344 | 317 |
| 318 // Active items are created and then immediately are called to start |
| 319 // the download. |
| 320 EXPECT_CALL(*result, MockStart(_)); |
| 321 |
| 345 return result; | 322 return result; |
| 346 } | 323 } |
| 347 | 324 |
| 348 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( | 325 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( |
| 349 DownloadItemImplDelegate* delegate, | 326 DownloadItemImplDelegate* delegate, |
| 350 const FilePath& path, | 327 const FilePath& path, |
| 351 const GURL& url, | 328 const GURL& url, |
| 352 content::DownloadId download_id, | 329 content::DownloadId download_id, |
| 353 const std::string& mime_type, | 330 const std::string& mime_type, |
| 354 const net::BoundNetLog& bound_net_log) { | 331 const net::BoundNetLog& bound_net_log) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 | 383 |
| 407 DownloadManagerTest() | 384 DownloadManagerTest() |
| 408 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 385 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 409 file_thread_(content::BrowserThread::FILE, &message_loop_), | 386 file_thread_(content::BrowserThread::FILE, &message_loop_), |
| 410 next_download_id_(0) { | 387 next_download_id_(0) { |
| 411 } | 388 } |
| 412 | 389 |
| 413 // We tear down everything in TearDown(). | 390 // We tear down everything in TearDown(). |
| 414 ~DownloadManagerTest() {} | 391 ~DownloadManagerTest() {} |
| 415 | 392 |
| 416 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate, | 393 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate, |
| 417 // and MockDownloadFileManager, then create a DownloadManager that points | 394 // then create a DownloadManager that points |
| 418 // at all of those. | 395 // at all of those. |
| 419 virtual void SetUp() { | 396 virtual void SetUp() { |
| 420 DCHECK(!download_manager_.get()); | 397 DCHECK(!download_manager_.get()); |
| 421 | 398 |
| 422 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); | 399 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); |
| 423 mock_download_manager_delegate_.reset( | 400 mock_download_manager_delegate_.reset( |
| 424 new StrictMock<MockDownloadManagerDelegate>); | 401 new StrictMock<MockDownloadManagerDelegate>); |
| 425 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) | 402 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) |
| 426 .WillOnce(Return()); | 403 .WillOnce(Return()); |
| 427 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>; | |
| 428 EXPECT_CALL(*mock_download_file_manager_.get(), | |
| 429 OnDownloadManagerShutdown(_)); | |
| 430 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); | 404 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); |
| 431 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) | 405 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) |
| 432 .WillRepeatedly(Return(false)); | 406 .WillRepeatedly(Return(false)); |
| 433 | 407 |
| 434 download_manager_ = new DownloadManagerImpl( | 408 download_manager_ = new DownloadManagerImpl( |
| 435 mock_download_file_manager_.get(), | |
| 436 scoped_ptr<content::DownloadItemFactory>( | 409 scoped_ptr<content::DownloadItemFactory>( |
| 437 mock_download_item_factory_.get()).Pass(), NULL); | 410 mock_download_item_factory_.get()).Pass(), NULL); |
| 438 observer_.reset(new MockDownloadManagerObserver()); | 411 observer_.reset(new MockDownloadManagerObserver()); |
| 439 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) | 412 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) |
| 440 .WillOnce(Return()); | 413 .WillOnce(Return()); |
| 441 download_manager_->AddObserver(observer_.get()); | 414 download_manager_->AddObserver(observer_.get()); |
| 442 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); | 415 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); |
| 443 download_manager_->Init(mock_browser_context_.get()); | 416 download_manager_->Init(mock_browser_context_.get()); |
| 444 } | 417 } |
| 445 | 418 |
| 446 virtual void TearDown() { | 419 virtual void TearDown() { |
| 447 while (MockDownloadItemImpl* | 420 while (MockDownloadItemImpl* |
| 448 item = mock_download_item_factory_->PopItem()) { | 421 item = mock_download_item_factory_->PopItem()) { |
| 449 EXPECT_CALL(*item, GetSafetyState()) | 422 EXPECT_CALL(*item, GetSafetyState()) |
| 450 .WillOnce(Return(content::DownloadItem::SAFE)); | 423 .WillOnce(Return(content::DownloadItem::SAFE)); |
| 451 EXPECT_CALL(*item, IsPartialDownload()) | 424 EXPECT_CALL(*item, IsPartialDownload()) |
| 452 .WillOnce(Return(false)); | 425 .WillOnce(Return(false)); |
| 453 } | 426 } |
| 454 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) | 427 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) |
| 455 .WillOnce(Return()); | 428 .WillOnce(Return()); |
| 456 | 429 |
| 457 download_manager_->Shutdown(); | 430 download_manager_->Shutdown(); |
| 458 download_manager_ = NULL; | 431 download_manager_ = NULL; |
| 459 message_loop_.RunAllPending(); | 432 message_loop_.RunAllPending(); |
| 460 ASSERT_EQ(NULL, mock_download_item_factory_.get()); | 433 ASSERT_EQ(NULL, mock_download_item_factory_.get()); |
| 461 message_loop_.RunAllPending(); | 434 message_loop_.RunAllPending(); |
| 462 mock_download_manager_delegate_.reset(); | 435 mock_download_manager_delegate_.reset(); |
| 463 mock_download_file_manager_ = NULL; | |
| 464 mock_browser_context_.reset(); | 436 mock_browser_context_.reset(); |
| 465 } | 437 } |
| 466 | 438 |
| 467 // Returns download id. | 439 // Returns download id. |
| 468 MockDownloadItemImpl& AddItemToManager() { | 440 MockDownloadItemImpl& AddItemToManager() { |
| 469 DownloadCreateInfo info; | 441 DownloadCreateInfo info; |
| 470 | 442 |
| 471 static const char* kDownloadIdDomain = "Test download id domain"; | 443 static const char* kDownloadIdDomain = "Test download id domain"; |
| 472 | 444 |
| 473 // Args are ignored except for download id, so everything else can be | 445 // Args are ignored except for download id, so everything else can be |
| 474 // null. | 446 // null. |
| 475 int id = next_download_id_; | 447 int id = next_download_id_; |
| 476 ++next_download_id_; | 448 ++next_download_id_; |
| 477 info.download_id = content::DownloadId(kDownloadIdDomain, id); | 449 info.download_id = content::DownloadId(kDownloadIdDomain, id); |
| 478 info.request_handle = DownloadRequestHandle(); | 450 info.request_handle = DownloadRequestHandle(); |
| 479 download_manager_->CreateDownloadItem(&info); | 451 EXPECT_CALL(GetMockObserver(), |
| 452 OnDownloadCreated(download_manager_.get(), _)); |
| 453 download_manager_->CreateDownloadItem(&info, net::BoundNetLog()); |
| 480 | 454 |
| 481 DCHECK(mock_download_item_factory_->GetItem(id)); | 455 DCHECK(mock_download_item_factory_->GetItem(id)); |
| 482 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); | 456 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); |
| 483 ON_CALL(item, GetId()) | 457 // Satisfy expectation. If the item is created in StartDownload(), |
| 484 .WillByDefault(Return(id)); | 458 // we call Start on it immediately, so we need to set that expectation |
| 459 // in the factory. |
| 460 item.Start(scoped_ptr<content::DownloadFile>()); |
| 485 | 461 |
| 486 return item; | 462 return item; |
| 487 } | 463 } |
| 488 | 464 |
| 489 MockDownloadItemImpl& GetMockDownloadItem(int id) { | 465 MockDownloadItemImpl& GetMockDownloadItem(int id) { |
| 490 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); | 466 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); |
| 491 | 467 |
| 492 DCHECK(itemp); | 468 DCHECK(itemp); |
| 493 return *itemp; | 469 return *itemp; |
| 494 } | 470 } |
| 495 | 471 |
| 496 void RemoveMockDownloadItem(int id) { | 472 void RemoveMockDownloadItem(int id) { |
| 497 // Owned by DownloadManager; should be deleted there. | 473 // Owned by DownloadManager; should be deleted there. |
| 498 mock_download_item_factory_->RemoveItem(id); | 474 mock_download_item_factory_->RemoveItem(id); |
| 499 } | 475 } |
| 500 | 476 |
| 501 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { | 477 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { |
| 502 return *mock_download_manager_delegate_; | 478 return *mock_download_manager_delegate_; |
| 503 } | 479 } |
| 504 | 480 |
| 505 MockDownloadFileManager& GetMockDownloadFileManager() { | |
| 506 return *mock_download_file_manager_; | |
| 507 } | |
| 508 | |
| 509 MockDownloadManagerObserver& GetMockObserver() { | 481 MockDownloadManagerObserver& GetMockObserver() { |
| 510 return *observer_; | 482 return *observer_; |
| 511 } | 483 } |
| 512 | 484 |
| 513 // Probe at private internals. | 485 // Probe at private internals. |
| 514 void DownloadStopped(DownloadItemImpl* item) { | 486 void DownloadStopped(DownloadItemImpl* item) { |
| 515 download_manager_->DownloadStopped(item); | 487 download_manager_->DownloadStopped(item); |
| 516 } | 488 } |
| 517 | 489 |
| 490 void DelegateStart(DownloadItemImpl* item) { |
| 491 download_manager_->DelegateStart(item); |
| 492 } |
| 493 |
| 518 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { | 494 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { |
| 519 // For DCHECK in AddDownloadItemToHistory. Don't want to use | 495 // For DCHECK in AddDownloadItemToHistory. Don't want to use |
| 520 // WillRepeatedly as it may have to return true after this. | 496 // WillRepeatedly as it may have to return true after this. |
| 521 if (DCHECK_IS_ON()) | 497 if (DCHECK_IS_ON()) |
| 522 EXPECT_CALL(item, IsPersisted()) | 498 EXPECT_CALL(item, IsPersisted()) |
| 523 .WillRepeatedly(Return(false)); | 499 .WillRepeatedly(Return(false)); |
| 524 | 500 |
| 525 EXPECT_CALL(item, SetDbHandle(db_handle)); | 501 EXPECT_CALL(item, SetDbHandle(db_handle)); |
| 526 EXPECT_CALL(item, SetIsPersisted()); | 502 EXPECT_CALL(item, SetIsPersisted()); |
| 527 EXPECT_CALL(item, GetDbHandle()) | 503 EXPECT_CALL(item, GetDbHandle()) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 547 protected: | 523 protected: |
| 548 // Key test variable; we'll keep it available to sub-classes. | 524 // Key test variable; we'll keep it available to sub-classes. |
| 549 scoped_refptr<DownloadManagerImpl> download_manager_; | 525 scoped_refptr<DownloadManagerImpl> download_manager_; |
| 550 | 526 |
| 551 private: | 527 private: |
| 552 MessageLoopForUI message_loop_; | 528 MessageLoopForUI message_loop_; |
| 553 content::TestBrowserThread ui_thread_; | 529 content::TestBrowserThread ui_thread_; |
| 554 content::TestBrowserThread file_thread_; | 530 content::TestBrowserThread file_thread_; |
| 555 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; | 531 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; |
| 556 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; | 532 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; |
| 557 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_; | |
| 558 scoped_ptr<MockBrowserContext> mock_browser_context_; | 533 scoped_ptr<MockBrowserContext> mock_browser_context_; |
| 559 scoped_ptr<MockDownloadManagerObserver> observer_; | 534 scoped_ptr<MockDownloadManagerObserver> observer_; |
| 560 int next_download_id_; | 535 int next_download_id_; |
| 561 | 536 |
| 562 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 537 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 563 }; | 538 }; |
| 564 | 539 |
| 565 // Confirm the appropriate invocations occur when you start a download. | 540 // Confirm the appropriate invocations occur when you start a download. |
| 566 TEST_F(DownloadManagerTest, StartDownload) { | 541 TEST_F(DownloadManagerTest, StartDownload) { |
| 567 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 542 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 568 scoped_ptr<content::ByteStreamReader> stream; | 543 scoped_ptr<content::ByteStreamReader> stream; |
| 569 int32 local_id(5); // Random value | 544 int32 local_id(5); // Random value |
| 570 | 545 |
| 571 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id)); | 546 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id)); |
| 572 | 547 |
| 573 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | 548 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) |
| 574 .WillOnce(Return()); | 549 .WillOnce(Return()); |
| 575 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) | 550 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) |
| 576 .WillOnce(Return(content::DownloadId(this, local_id))); | 551 .WillOnce(Return(content::DownloadId(this, local_id))); |
| 577 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) | 552 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) |
| 578 .WillOnce(Return(true)); | 553 .WillOnce(Return(true)); |
| 579 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile( | |
| 580 info.get(), static_cast<content::ByteStreamReader*>(NULL), | |
| 581 download_manager_.get(), true, _, _)); | |
| 582 | 554 |
| 583 download_manager_->StartDownload(info.Pass(), stream.Pass()); | 555 download_manager_->StartDownload(info.Pass(), stream.Pass()); |
| 584 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); | 556 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); |
| 585 } | 557 } |
| 586 | 558 |
| 587 // Do the results of an OnDownloadInterrupted get passed through properly | 559 // Confirm that calling DelegateStart behaves properly if the delegate |
| 588 // to the DownloadItem? | 560 // blocks starting. |
| 589 TEST_F(DownloadManagerTest, OnDownloadInterrupted) { | 561 TEST_F(DownloadManagerTest, DelegateStart_True) { |
| 590 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | |
| 591 .WillOnce(Return()); | |
| 592 // Put a mock we have a handle to on the download manager. | 562 // Put a mock we have a handle to on the download manager. |
| 593 MockDownloadItemImpl& item(AddItemToManager()); | 563 MockDownloadItemImpl& item(AddItemToManager()); |
| 594 int download_id = item.GetId(); | |
| 595 | 564 |
| 596 content::DownloadInterruptReason reason( | 565 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 597 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 566 DetermineDownloadTarget(&item, _)) |
| 567 .WillOnce(Return(true)); |
| 568 DelegateStart(&item); |
| 569 } |
| 598 | 570 |
| 599 EXPECT_CALL(item, Interrupt(reason)); | 571 // Confirm that calling DelegateStart behaves properly if the delegate |
| 600 download_manager_->OnDownloadInterrupted(download_id, reason); | 572 // allows starting. This also tests OnDownloadTargetDetermined. |
| 601 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id)); | 573 TEST_F(DownloadManagerTest, DelegateStart_False) { |
| 574 // Put a mock we have a handle to on the download manager. |
| 575 MockDownloadItemImpl& item(AddItemToManager()); |
| 576 |
| 577 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 578 DetermineDownloadTarget(&item, _)) |
| 579 .WillOnce(Return(false)); |
| 580 EXPECT_CALL(item, OnDownloadTargetDetermined( |
| 581 _, content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 582 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, _)); |
| 583 FilePath null_path; |
| 584 EXPECT_CALL(item, GetForcedFilePath()) |
| 585 .WillOnce(ReturnRef(null_path)); |
| 586 DelegateStart(&item); |
| 602 } | 587 } |
| 603 | 588 |
| 604 // Does DownloadStopped remove Download from appropriate queues? | 589 // Does DownloadStopped remove Download from appropriate queues? |
| 605 // This test tests non-persisted downloads. | 590 // This test tests non-persisted downloads. |
| 606 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { | 591 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { |
| 607 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | |
| 608 .WillOnce(Return()); | |
| 609 // Put a mock we have a handle to on the download manager. | 592 // Put a mock we have a handle to on the download manager. |
| 610 MockDownloadItemImpl& item(AddItemToManager()); | 593 MockDownloadItemImpl& item(AddItemToManager()); |
| 611 | 594 |
| 612 EXPECT_CALL(item, IsPersisted()) | 595 EXPECT_CALL(item, IsPersisted()) |
| 613 .WillRepeatedly(Return(false)); | 596 .WillRepeatedly(Return(false)); |
| 614 EXPECT_CALL(item, GetState()) | 597 EXPECT_CALL(item, GetState()) |
| 615 .WillRepeatedly(Return(DownloadItem::CANCELLED)); | 598 .WillRepeatedly(Return(DownloadItem::CANCELLED)); |
| 616 EXPECT_CALL(item, GetDbHandle()) | 599 EXPECT_CALL(item, GetDbHandle()) |
| 617 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); | 600 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); |
| 618 | 601 |
| 619 EXPECT_CALL(item, OffThreadCancel()); | |
| 620 DownloadStopped(&item); | 602 DownloadStopped(&item); |
| 621 // TODO(rdsmith): Confirm that the download item is no longer on the | 603 // TODO(rdsmith): Confirm that the download item is no longer on the |
| 622 // active list by calling download_manager_->GetActiveDownloadItem(id). | 604 // active list by calling download_manager_->GetActiveDownloadItem(id). |
| 623 // Currently, the item is left on the active list for rendez-vous with | 605 // Currently, the item is left on the active list for rendez-vous with |
| 624 // the history system :-{. | 606 // the history system :-{. |
| 625 } | 607 } |
| 626 | 608 |
| 627 // Does DownloadStopped remove Download from appropriate queues? | 609 // Does DownloadStopped remove Download from appropriate queues? |
| 628 // This test tests persisted downloads. | 610 // This test tests persisted downloads. |
| 629 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { | 611 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { |
| 630 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | |
| 631 .WillOnce(Return()); | |
| 632 // Put a mock we have a handle to on the download manager. | 612 // Put a mock we have a handle to on the download manager. |
| 633 MockDownloadItemImpl& item(AddItemToManager()); | 613 MockDownloadItemImpl& item(AddItemToManager()); |
| 634 int download_id = item.GetId(); | 614 int download_id = item.GetId(); |
| 635 int64 db_handle = 0x7; | 615 int64 db_handle = 0x7; |
| 636 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) | 616 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) |
| 637 .WillOnce(Return()); | 617 .WillOnce(Return()); |
| 638 AddItemToHistory(item, db_handle); | 618 AddItemToHistory(item, db_handle); |
| 639 | 619 |
| 640 EXPECT_CALL(item, IsPersisted()) | 620 EXPECT_CALL(item, IsPersisted()) |
| 641 .WillRepeatedly(Return(true)); | 621 .WillRepeatedly(Return(true)); |
| 642 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 622 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 643 UpdateItemInPersistentStore(&item)); | 623 UpdateItemInPersistentStore(&item)); |
| 644 EXPECT_CALL(item, GetState()) | 624 EXPECT_CALL(item, GetState()) |
| 645 .WillRepeatedly(Return(DownloadItem::CANCELLED)); | 625 .WillRepeatedly(Return(DownloadItem::CANCELLED)); |
| 646 EXPECT_CALL(item, GetDbHandle()) | 626 EXPECT_CALL(item, GetDbHandle()) |
| 647 .WillRepeatedly(Return(db_handle)); | 627 .WillRepeatedly(Return(db_handle)); |
| 648 | 628 |
| 649 EXPECT_CALL(item, OffThreadCancel()); | |
| 650 DownloadStopped(&item); | 629 DownloadStopped(&item); |
| 651 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); | 630 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); |
| 652 } | 631 } |
| OLD | NEW |