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

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

Issue 10912173: Replace the DownloadFileManager with direct ownership of DownloadFileImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync'd to LKGR. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "content/browser/download/byte_stream.h" 19 #include "content/browser/download/byte_stream.h"
20 #include "content/browser/download/download_create_info.h" 20 #include "content/browser/download/download_create_info.h"
21 #include "content/browser/download/download_file_manager.h" 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
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
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
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 { 210 class NullDownloadItemImplDelegate : public DownloadItemImplDelegate {
204 public: 211 public:
205 MockDownloadFileManager(); 212 // Safe to null this out even if it doesn't do anything because none
206 213 // of these functions will ever be called; this class just exists
207 void CreateDownloadFile( 214 // to have something to pass to the DownloadItemImpl base class
208 scoped_ptr<DownloadCreateInfo> info, 215 // of MockDownloadItemImpl.
209 scoped_ptr<content::ByteStreamReader> stream, 216 virtual void DelegateStart(DownloadItemImpl* download) OVERRIDE {
210 scoped_refptr<content::DownloadManager> download_manager, 217 NOTREACHED();
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 } 218 }
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 }; 219 };
243 220
244 MockDownloadFileManager::MockDownloadFileManager()
245 : DownloadFileManager(NULL) {}
246
247 MockDownloadFileManager::~MockDownloadFileManager() {}
248
249 class MockDownloadItemFactory 221 class MockDownloadItemFactory
250 : public content::DownloadItemFactory, 222 : public content::DownloadItemFactory,
251 public base::SupportsWeakPtr<MockDownloadItemFactory> { 223 public base::SupportsWeakPtr<MockDownloadItemFactory> {
252 public: 224 public:
253 MockDownloadItemFactory(); 225 MockDownloadItemFactory();
254 virtual ~MockDownloadItemFactory(); 226 virtual ~MockDownloadItemFactory();
255 227
256 // Access to map of created items. 228 // Access to map of created items.
257 // TODO(rdsmith): Could add type (save page, persisted, etc.) 229 // TODO(rdsmith): Could add type (save page, persisted, etc.)
258 // functionality if it's ever needed by consumers. 230 // functionality if it's ever needed by consumers.
(...skipping 23 matching lines...) Expand all
282 virtual DownloadItemImpl* CreateSavePageItem( 254 virtual DownloadItemImpl* CreateSavePageItem(
283 DownloadItemImplDelegate* delegate, 255 DownloadItemImplDelegate* delegate,
284 const FilePath& path, 256 const FilePath& path,
285 const GURL& url, 257 const GURL& url,
286 content::DownloadId download_id, 258 content::DownloadId download_id,
287 const std::string& mime_type, 259 const std::string& mime_type,
288 const net::BoundNetLog& bound_net_log) OVERRIDE; 260 const net::BoundNetLog& bound_net_log) OVERRIDE;
289 261
290 private: 262 private:
291 std::map<int32, MockDownloadItemImpl*> items_; 263 std::map<int32, MockDownloadItemImpl*> items_;
292 DownloadItemImplDelegate item_delegate_; 264 NullDownloadItemImplDelegate item_delegate_;
293 265
294 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); 266 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory);
295 }; 267 };
296 268
297 MockDownloadItemFactory::MockDownloadItemFactory() {} 269 MockDownloadItemFactory::MockDownloadItemFactory() {}
298 270
299 MockDownloadItemFactory::~MockDownloadItemFactory() {} 271 MockDownloadItemFactory::~MockDownloadItemFactory() {}
300 272
301 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) { 273 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) {
302 if (items_.find(id) == items_.end()) 274 if (items_.find(id) == items_.end())
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 const DownloadCreateInfo& info, 314 const DownloadCreateInfo& info,
343 scoped_ptr<DownloadRequestHandleInterface> request_handle, 315 scoped_ptr<DownloadRequestHandleInterface> request_handle,
344 const net::BoundNetLog& bound_net_log) { 316 const net::BoundNetLog& bound_net_log) {
345 int local_id = info.download_id.local(); 317 int local_id = info.download_id.local();
346 DCHECK(items_.find(local_id) == items_.end()); 318 DCHECK(items_.find(local_id) == items_.end());
347 319
348 MockDownloadItemImpl* result = 320 MockDownloadItemImpl* result =
349 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 321 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
350 EXPECT_CALL(*result, GetId()) 322 EXPECT_CALL(*result, GetId())
351 .WillRepeatedly(Return(local_id)); 323 .WillRepeatedly(Return(local_id));
324 EXPECT_CALL(*result, GetGlobalId())
325 .WillRepeatedly(Return(content::DownloadId(delegate, local_id)));
352 items_[local_id] = result; 326 items_[local_id] = result;
353 327
328 // Active items are created and then immediately are called to start
329 // the download.
330 EXPECT_CALL(*result, MockStart(_));
331
354 return result; 332 return result;
355 } 333 }
356 334
357 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( 335 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
358 DownloadItemImplDelegate* delegate, 336 DownloadItemImplDelegate* delegate,
359 const FilePath& path, 337 const FilePath& path,
360 const GURL& url, 338 const GURL& url,
361 content::DownloadId download_id, 339 content::DownloadId download_id,
362 const std::string& mime_type, 340 const std::string& mime_type,
363 const net::BoundNetLog& bound_net_log) { 341 const net::BoundNetLog& bound_net_log) {
364 int local_id = download_id.local(); 342 int local_id = download_id.local();
365 DCHECK(items_.find(local_id) == items_.end()); 343 DCHECK(items_.find(local_id) == items_.end());
366 344
367 MockDownloadItemImpl* result = 345 MockDownloadItemImpl* result =
368 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 346 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
369 EXPECT_CALL(*result, GetId()) 347 EXPECT_CALL(*result, GetId())
370 .WillRepeatedly(Return(local_id)); 348 .WillRepeatedly(Return(local_id));
371 items_[local_id] = result; 349 items_[local_id] = result;
372 350
373 return result; 351 return result;
374 } 352 }
375 353
354 class MockDownloadFileFactory
355 : public content::DownloadFileFactory,
356 public base::SupportsWeakPtr<MockDownloadFileFactory> {
357 public:
358 MockDownloadFileFactory() {}
359 virtual ~MockDownloadFileFactory() {}
360
361 // Overridden method from DownloadFileFactory
362 MOCK_METHOD9(MockCreateFile, content::DownloadFile*(
363 const content::DownloadSaveInfo&,
364 const FilePath&,
365 GURL, GURL, int64, bool,
366 content::ByteStreamReader*,
367 const net::BoundNetLog&,
368 base::WeakPtr<content::DownloadDestinationObserver>));
369
370 virtual content::DownloadFile* CreateFile(
371 const content::DownloadSaveInfo& save_info,
372 const FilePath& default_download_directory,
373 GURL url,
374 GURL referrer_url,
375 int64 received_bytes,
376 bool calculate_hash,
377 scoped_ptr<content::ByteStreamReader> stream,
378 const net::BoundNetLog& bound_net_log,
379 base::WeakPtr<content::DownloadDestinationObserver> observer) {
380 return MockCreateFile(save_info, default_download_directory, url,
381 referrer_url, received_bytes, calculate_hash,
382 stream.get(), bound_net_log, observer);
383 }
384 };
385
376 class MockBrowserContext : public content::BrowserContext { 386 class MockBrowserContext : public content::BrowserContext {
377 public: 387 public:
378 MockBrowserContext() {} 388 MockBrowserContext() {}
379 ~MockBrowserContext() {} 389 ~MockBrowserContext() {}
380 390
381 MOCK_METHOD0(GetPath, FilePath()); 391 MOCK_METHOD0(GetPath, FilePath());
382 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); 392 MOCK_CONST_METHOD0(IsOffTheRecord, bool());
383 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*()); 393 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*());
384 MOCK_METHOD1(GetRequestContextForRenderProcess, 394 MOCK_METHOD1(GetRequestContextForRenderProcess,
385 net::URLRequestContextGetter*(int renderer_child_id)); 395 net::URLRequestContextGetter*(int renderer_child_id));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 432
423 DownloadManagerTest() 433 DownloadManagerTest()
424 : ui_thread_(content::BrowserThread::UI, &message_loop_), 434 : ui_thread_(content::BrowserThread::UI, &message_loop_),
425 file_thread_(content::BrowserThread::FILE, &message_loop_), 435 file_thread_(content::BrowserThread::FILE, &message_loop_),
426 next_download_id_(0) { 436 next_download_id_(0) {
427 } 437 }
428 438
429 // We tear down everything in TearDown(). 439 // We tear down everything in TearDown().
430 ~DownloadManagerTest() {} 440 ~DownloadManagerTest() {}
431 441
432 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate, 442 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
433 // and MockDownloadFileManager, then create a DownloadManager that points 443 // then create a DownloadManager that points
434 // at all of those. 444 // at all of those.
435 virtual void SetUp() { 445 virtual void SetUp() {
436 DCHECK(!download_manager_.get()); 446 DCHECK(!download_manager_.get());
437 447
438 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); 448 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
449 mock_download_file_factory_ = (new MockDownloadFileFactory())->AsWeakPtr();
439 mock_download_manager_delegate_.reset( 450 mock_download_manager_delegate_.reset(
440 new StrictMock<MockDownloadManagerDelegate>); 451 new StrictMock<MockDownloadManagerDelegate>);
441 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) 452 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
442 .WillOnce(Return()); 453 .WillOnce(Return());
443 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>;
444 EXPECT_CALL(*mock_download_file_manager_.get(),
445 OnDownloadManagerShutdown(_));
446 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); 454 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
447 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) 455 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
448 .WillRepeatedly(Return(false)); 456 .WillRepeatedly(Return(false));
449 457
450 download_manager_ = new DownloadManagerImpl( 458 download_manager_ = new DownloadManagerImpl(
451 mock_download_file_manager_.get(),
452 scoped_ptr<content::DownloadItemFactory>( 459 scoped_ptr<content::DownloadItemFactory>(
453 mock_download_item_factory_.get()).Pass(), NULL); 460 mock_download_item_factory_.get()).Pass(),
461 scoped_ptr<content::DownloadFileFactory>(
462 mock_download_file_factory_.get()).Pass(), NULL);
454 observer_.reset(new MockDownloadManagerObserver()); 463 observer_.reset(new MockDownloadManagerObserver());
455 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 464 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
456 .WillOnce(Return()); 465 .WillOnce(Return());
457 download_manager_->AddObserver(observer_.get()); 466 download_manager_->AddObserver(observer_.get());
458 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); 467 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
459 download_manager_->Init(mock_browser_context_.get()); 468 download_manager_->Init(mock_browser_context_.get());
460 } 469 }
461 470
462 virtual void TearDown() { 471 virtual void TearDown() {
463 while (MockDownloadItemImpl* 472 while (MockDownloadItemImpl*
464 item = mock_download_item_factory_->PopItem()) { 473 item = mock_download_item_factory_->PopItem()) {
465 EXPECT_CALL(*item, GetSafetyState()) 474 EXPECT_CALL(*item, GetSafetyState())
466 .WillOnce(Return(content::DownloadItem::SAFE)); 475 .WillOnce(Return(content::DownloadItem::SAFE));
467 EXPECT_CALL(*item, IsPartialDownload()) 476 EXPECT_CALL(*item, IsPartialDownload())
468 .WillOnce(Return(false)); 477 .WillOnce(Return(false));
469 } 478 }
470 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) 479 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
471 .WillOnce(Return()); 480 .WillOnce(Return());
472 481
473 download_manager_->Shutdown(); 482 download_manager_->Shutdown();
474 download_manager_ = NULL; 483 download_manager_ = NULL;
475 message_loop_.RunAllPending(); 484 message_loop_.RunAllPending();
476 ASSERT_EQ(NULL, mock_download_item_factory_.get()); 485 ASSERT_EQ(NULL, mock_download_item_factory_.get());
486 ASSERT_EQ(NULL, mock_download_file_factory_.get());
477 message_loop_.RunAllPending(); 487 message_loop_.RunAllPending();
478 mock_download_manager_delegate_.reset(); 488 mock_download_manager_delegate_.reset();
479 mock_download_file_manager_ = NULL;
480 mock_browser_context_.reset(); 489 mock_browser_context_.reset();
481 } 490 }
482 491
483 // Returns download id. 492 // Returns download id.
484 MockDownloadItemImpl& AddItemToManager() { 493 MockDownloadItemImpl& AddItemToManager() {
485 DownloadCreateInfo info; 494 DownloadCreateInfo info;
486 495
487 static const char* kDownloadIdDomain = "Test download id domain"; 496 static const char* kDownloadIdDomain = "Test download id domain";
488 497
489 // Args are ignored except for download id, so everything else can be 498 // Args are ignored except for download id, so everything else can be
490 // null. 499 // null.
491 int id = next_download_id_; 500 int id = next_download_id_;
492 ++next_download_id_; 501 ++next_download_id_;
493 info.download_id = content::DownloadId(kDownloadIdDomain, id); 502 info.download_id = content::DownloadId(kDownloadIdDomain, id);
494 info.request_handle = DownloadRequestHandle(); 503 info.request_handle = DownloadRequestHandle();
495 download_manager_->CreateDownloadItem(&info); 504 download_manager_->CreateDownloadItem(&info, net::BoundNetLog());
496 505
497 DCHECK(mock_download_item_factory_->GetItem(id)); 506 DCHECK(mock_download_item_factory_->GetItem(id));
498 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); 507 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
499 ON_CALL(item, GetId()) 508 // Satisfy expectation. If the item is created in StartDownload(),
500 .WillByDefault(Return(id)); 509 // we call Start on it immediately, so we need to set that expectation
510 // in the factory.
511 item.Start(scoped_ptr<content::DownloadFile>());
501 512
502 return item; 513 return item;
503 } 514 }
504 515
505 MockDownloadItemImpl& GetMockDownloadItem(int id) { 516 MockDownloadItemImpl& GetMockDownloadItem(int id) {
506 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); 517 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
507 518
508 DCHECK(itemp); 519 DCHECK(itemp);
509 return *itemp; 520 return *itemp;
510 } 521 }
511 522
512 void RemoveMockDownloadItem(int id) { 523 void RemoveMockDownloadItem(int id) {
513 // Owned by DownloadManager; should be deleted there. 524 // Owned by DownloadManager; should be deleted there.
514 mock_download_item_factory_->RemoveItem(id); 525 mock_download_item_factory_->RemoveItem(id);
515 } 526 }
516 527
517 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { 528 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
518 return *mock_download_manager_delegate_; 529 return *mock_download_manager_delegate_;
519 } 530 }
520 531
521 MockDownloadFileManager& GetMockDownloadFileManager() {
522 return *mock_download_file_manager_;
523 }
524
525 MockDownloadManagerObserver& GetMockObserver() { 532 MockDownloadManagerObserver& GetMockObserver() {
526 return *observer_; 533 return *observer_;
527 } 534 }
528 535
529 // Probe at private internals. 536 // Probe at private internals.
530 void DownloadStopped(DownloadItemImpl* item) { 537 void DownloadStopped(DownloadItemImpl* item) {
531 download_manager_->DownloadStopped(item); 538 download_manager_->DownloadStopped(item);
532 } 539 }
533 540
541 void DelegateStart(DownloadItemImpl* item) {
542 download_manager_->DelegateStart(item);
543 }
544
534 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 545 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
535 // For DCHECK in AddDownloadItemToHistory. Don't want to use 546 // For DCHECK in AddDownloadItemToHistory. Don't want to use
536 // WillRepeatedly as it may have to return true after this. 547 // WillRepeatedly as it may have to return true after this.
537 if (DCHECK_IS_ON()) 548 if (DCHECK_IS_ON())
538 EXPECT_CALL(item, IsPersisted()) 549 EXPECT_CALL(item, IsPersisted())
539 .WillRepeatedly(Return(false)); 550 .WillRepeatedly(Return(false));
540 551
541 EXPECT_CALL(item, SetDbHandle(db_handle)); 552 EXPECT_CALL(item, SetDbHandle(db_handle));
542 EXPECT_CALL(item, SetIsPersisted()); 553 EXPECT_CALL(item, SetIsPersisted());
543 EXPECT_CALL(item, GetDbHandle()) 554 EXPECT_CALL(item, GetDbHandle())
(...skipping 12 matching lines...) Expand all
556 // Null out MaybeCompleteDownload 567 // Null out MaybeCompleteDownload
557 EXPECT_CALL(item, AllDataSaved()) 568 EXPECT_CALL(item, AllDataSaved())
558 .WillOnce(Return(false)); 569 .WillOnce(Return(false));
559 570
560 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle); 571 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle);
561 } 572 }
562 573
563 protected: 574 protected:
564 // Key test variable; we'll keep it available to sub-classes. 575 // Key test variable; we'll keep it available to sub-classes.
565 scoped_refptr<DownloadManagerImpl> download_manager_; 576 scoped_refptr<DownloadManagerImpl> download_manager_;
577 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
566 578
567 private: 579 private:
568 MessageLoopForUI message_loop_; 580 MessageLoopForUI message_loop_;
569 content::TestBrowserThread ui_thread_; 581 content::TestBrowserThread ui_thread_;
570 content::TestBrowserThread file_thread_; 582 content::TestBrowserThread file_thread_;
571 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; 583 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
572 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; 584 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
573 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_;
574 scoped_ptr<MockBrowserContext> mock_browser_context_; 585 scoped_ptr<MockBrowserContext> mock_browser_context_;
575 scoped_ptr<MockDownloadManagerObserver> observer_; 586 scoped_ptr<MockDownloadManagerObserver> observer_;
576 int next_download_id_; 587 int next_download_id_;
577 588
578 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 589 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
579 }; 590 };
580 591
581 // Confirm the appropriate invocations occur when you start a download. 592 // Confirm the appropriate invocations occur when you start a download.
582 TEST_F(DownloadManagerTest, StartDownload) { 593 TEST_F(DownloadManagerTest, StartDownload) {
583 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 594 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
584 scoped_ptr<content::ByteStreamReader> stream; 595 scoped_ptr<content::ByteStreamReader> stream;
585 int32 local_id(5); // Random value 596 int32 local_id(5); // Random value
586 FilePath download_path(FILE_PATH_LITERAL("download/path")); 597 FilePath download_path(FILE_PATH_LITERAL("download/path"));
587 598
588 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id)); 599 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id));
589 600
590 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 601 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
591 .WillOnce(Return()); 602 .WillOnce(Return());
592 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) 603 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId())
593 .WillOnce(Return(content::DownloadId(this, local_id))); 604 .WillOnce(Return(content::DownloadId(this, local_id)));
605
606 // Doing nothing will set the default download directory to null.
607 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _));
594 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) 608 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
595 .WillOnce(Return(true)); 609 .WillOnce(Return(true));
596 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_,_,_,_)) 610 EXPECT_CALL(*mock_download_file_factory_.get(),
597 .WillOnce(SetArgPointee<2>(download_path)); 611 MockCreateFile(Ref(info->save_info), _, _, _, 0, true,
598 612 stream.get(), _, _));
599 // The CreateDownloadFile call should specify a DownloadCreateInfo that
600 // includes the result of the GetSaveDir() call.
601 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile(
602 DownloadCreateInfoWithDefaultPath(info.get(), download_path),
603 static_cast<content::ByteStreamReader*>(NULL),
604 download_manager_.get(), true, _, _));
605 613
606 download_manager_->StartDownload(info.Pass(), stream.Pass()); 614 download_manager_->StartDownload(info.Pass(), stream.Pass());
607 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); 615 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id));
608 } 616 }
609 617
610 // Do the results of an OnDownloadInterrupted get passed through properly 618 // Confirm that calling DelegateStart behaves properly if the delegate
611 // to the DownloadItem? 619 // blocks starting.
612 TEST_F(DownloadManagerTest, OnDownloadInterrupted) { 620 TEST_F(DownloadManagerTest, DelegateStart_True) {
613 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
614 .WillOnce(Return());
615 // Put a mock we have a handle to on the download manager. 621 // Put a mock we have a handle to on the download manager.
616 MockDownloadItemImpl& item(AddItemToManager()); 622 MockDownloadItemImpl& item(AddItemToManager());
617 int download_id = item.GetId();
618 623
619 content::DownloadInterruptReason reason( 624 EXPECT_CALL(GetMockDownloadManagerDelegate(),
620 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 625 DetermineDownloadTarget(&item, _))
626 .WillOnce(Return(true));
627 DelegateStart(&item);
628 }
621 629
622 EXPECT_CALL(item, Interrupt(reason)); 630 // Confirm that calling DelegateStart behaves properly if the delegate
623 download_manager_->OnDownloadInterrupted(download_id, reason); 631 // allows starting. This also tests OnDownloadTargetDetermined.
624 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id)); 632 TEST_F(DownloadManagerTest, DelegateStart_False) {
633 // Put a mock we have a handle to on the download manager.
634 MockDownloadItemImpl& item(AddItemToManager());
635
636 EXPECT_CALL(GetMockDownloadManagerDelegate(),
637 DetermineDownloadTarget(&item, _))
638 .WillOnce(Return(false));
639 EXPECT_CALL(item, OnDownloadTargetDetermined(
640 _, content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
641 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, _));
642 FilePath null_path;
643 EXPECT_CALL(item, GetForcedFilePath())
644 .WillOnce(ReturnRef(null_path));
645 DelegateStart(&item);
625 } 646 }
626 647
627 // Does DownloadStopped remove Download from appropriate queues? 648 // Does DownloadStopped remove Download from appropriate queues?
628 // This test tests non-persisted downloads. 649 // This test tests non-persisted downloads.
629 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { 650 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
630 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
631 .WillOnce(Return());
632 // Put a mock we have a handle to on the download manager. 651 // Put a mock we have a handle to on the download manager.
633 MockDownloadItemImpl& item(AddItemToManager()); 652 MockDownloadItemImpl& item(AddItemToManager());
634 653
635 EXPECT_CALL(item, IsPersisted()) 654 EXPECT_CALL(item, IsPersisted())
636 .WillRepeatedly(Return(false)); 655 .WillRepeatedly(Return(false));
637 EXPECT_CALL(item, GetState()) 656 EXPECT_CALL(item, GetState())
638 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 657 .WillRepeatedly(Return(DownloadItem::CANCELLED));
639 EXPECT_CALL(item, GetDbHandle()) 658 EXPECT_CALL(item, GetDbHandle())
640 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); 659 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
641 660
642 EXPECT_CALL(item, OffThreadCancel());
643 DownloadStopped(&item); 661 DownloadStopped(&item);
644 // TODO(rdsmith): Confirm that the download item is no longer on the 662 // TODO(rdsmith): Confirm that the download item is no longer on the
645 // active list by calling download_manager_->GetActiveDownloadItem(id). 663 // active list by calling download_manager_->GetActiveDownloadItem(id).
646 // Currently, the item is left on the active list for rendez-vous with 664 // Currently, the item is left on the active list for rendez-vous with
647 // the history system :-{. 665 // the history system :-{.
648 } 666 }
649 667
650 // Does DownloadStopped remove Download from appropriate queues? 668 // Does DownloadStopped remove Download from appropriate queues?
651 // This test tests persisted downloads. 669 // This test tests persisted downloads.
652 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { 670 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
653 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
654 .WillOnce(Return());
655 // Put a mock we have a handle to on the download manager. 671 // Put a mock we have a handle to on the download manager.
656 MockDownloadItemImpl& item(AddItemToManager()); 672 MockDownloadItemImpl& item(AddItemToManager());
657 int download_id = item.GetId(); 673 int download_id = item.GetId();
658 int64 db_handle = 0x7; 674 int64 db_handle = 0x7;
659 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 675 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
660 .WillOnce(Return()); 676 .WillOnce(Return());
661 AddItemToHistory(item, db_handle); 677 AddItemToHistory(item, db_handle);
662 678
663 EXPECT_CALL(item, IsPersisted()) 679 EXPECT_CALL(item, IsPersisted())
664 .WillRepeatedly(Return(true)); 680 .WillRepeatedly(Return(true));
665 EXPECT_CALL(GetMockDownloadManagerDelegate(), 681 EXPECT_CALL(GetMockDownloadManagerDelegate(),
666 UpdateItemInPersistentStore(&item)); 682 UpdateItemInPersistentStore(&item));
667 EXPECT_CALL(item, GetState()) 683 EXPECT_CALL(item, GetState())
668 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 684 .WillRepeatedly(Return(DownloadItem::CANCELLED));
669 EXPECT_CALL(item, GetDbHandle()) 685 EXPECT_CALL(item, GetDbHandle())
670 .WillRepeatedly(Return(db_handle)); 686 .WillRepeatedly(Return(db_handle));
671 687
672 EXPECT_CALL(item, OffThreadCancel());
673 DownloadStopped(&item); 688 DownloadStopped(&item);
674 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); 689 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
675 } 690 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698