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

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

Issue 10872102: Revert 153563 - Remove DownloadFileManager in favor of direct ownership of DownloadFiles. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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_factory.h" 21 #include "content/browser/download/download_file_manager.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"
36 #include "net/base/net_util.h" 35 #include "net/base/net_util.h"
37 #include "testing/gmock/include/gmock/gmock.h" 36 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gmock_mutant.h" 37 #include "testing/gmock_mutant.h"
39 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
40 39
41 using ::testing::DoAll; 40 using ::testing::DoAll;
42 using ::testing::Ref; 41 using ::testing::Ref;
43 using ::testing::Return; 42 using ::testing::Return;
44 using ::testing::ReturnRef; 43 using ::testing::ReturnRef;
45 using ::testing::SetArgPointee; 44 using ::testing::SetArgPointee;
(...skipping 28 matching lines...) Expand all
74 MOCK_METHOD0(CanShowInFolder, bool()); 73 MOCK_METHOD0(CanShowInFolder, bool());
75 MOCK_METHOD0(CanOpenDownload, bool()); 74 MOCK_METHOD0(CanOpenDownload, bool());
76 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); 75 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool());
77 MOCK_METHOD0(OpenDownload, void()); 76 MOCK_METHOD0(OpenDownload, void());
78 MOCK_METHOD0(ShowDownloadInShell, void()); 77 MOCK_METHOD0(ShowDownloadInShell, void());
79 MOCK_METHOD0(DangerousDownloadValidated, void()); 78 MOCK_METHOD0(DangerousDownloadValidated, void());
80 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); 79 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&));
81 MOCK_METHOD1(Cancel, void(bool)); 80 MOCK_METHOD1(Cancel, void(bool));
82 MOCK_METHOD0(MarkAsComplete, void()); 81 MOCK_METHOD0(MarkAsComplete, void());
83 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); 82 MOCK_METHOD1(DelayedDownloadOpened, void(bool));
84 MOCK_METHOD1(OnAllDataSaved, void(const std::string&)); 83 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&));
85 MOCK_METHOD0(OnDownloadedFileRemoved, void()); 84 MOCK_METHOD0(OnDownloadedFileRemoved, void());
86 MOCK_METHOD0(MaybeCompleteDownload, void()); 85 MOCK_METHOD0(MaybeCompleteDownload, void());
87 virtual void Start(
88 scoped_ptr<content::DownloadFile> download_file) OVERRIDE {
89 MockStart(download_file.get());
90 }
91
92 MOCK_METHOD1(MockStart, void(content::DownloadFile*));
93
94 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); 86 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason));
95 MOCK_METHOD1(Delete, void(DeleteReason)); 87 MOCK_METHOD1(Delete, void(DeleteReason));
96 MOCK_METHOD0(Remove, void()); 88 MOCK_METHOD0(Remove, void());
97 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); 89 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*));
98 MOCK_CONST_METHOD0(CurrentSpeed, int64()); 90 MOCK_CONST_METHOD0(CurrentSpeed, int64());
99 MOCK_CONST_METHOD0(PercentComplete, int()); 91 MOCK_CONST_METHOD0(PercentComplete, int());
100 MOCK_CONST_METHOD0(AllDataSaved, bool()); 92 MOCK_CONST_METHOD0(AllDataSaved, bool());
101 MOCK_METHOD0(TogglePause, void()); 93 MOCK_METHOD0(TogglePause, void());
102 MOCK_METHOD0(OnDownloadCompleting, void()); 94 MOCK_METHOD0(OnDownloadCompleting, void());
103 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); 95 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); 146 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
155 MOCK_CONST_METHOD0(GetETag, const std::string&()); 147 MOCK_CONST_METHOD0(GetETag, const std::string&());
156 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 148 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
157 MOCK_CONST_METHOD0(GetPersistentStoreInfo, 149 MOCK_CONST_METHOD0(GetPersistentStoreInfo,
158 content::DownloadPersistentStoreInfo()); 150 content::DownloadPersistentStoreInfo());
159 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 151 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
160 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); 152 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*());
161 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 153 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
162 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 154 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
163 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 155 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
156 MOCK_METHOD0(OffThreadCancel, void());
164 MOCK_CONST_METHOD1(DebugString, std::string(bool)); 157 MOCK_CONST_METHOD1(DebugString, std::string(bool));
165 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 158 MOCK_METHOD0(MockDownloadOpenForTesting, void());
166 }; 159 };
167 160
168 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { 161 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate {
169 public: 162 public:
170 MockDownloadManagerDelegate(); 163 MockDownloadManagerDelegate();
171 virtual ~MockDownloadManagerDelegate(); 164 virtual ~MockDownloadManagerDelegate();
172 165
173 MOCK_METHOD0(Shutdown, void()); 166 MOCK_METHOD0(Shutdown, void());
(...skipping 17 matching lines...) Expand all
191 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*)); 184 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*));
192 MOCK_METHOD5(ChooseSavePath, void( 185 MOCK_METHOD5(ChooseSavePath, void(
193 WebContents*, const FilePath&, const FilePath::StringType&, 186 WebContents*, const FilePath&, const FilePath::StringType&,
194 bool, const content::SavePackagePathPickedCallback&)); 187 bool, const content::SavePackagePathPickedCallback&));
195 }; 188 };
196 189
197 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} 190 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
198 191
199 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} 192 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
200 193
201 class NullDownloadItemImplDelegate : public DownloadItemImplDelegate { 194 class MockDownloadFileManager : public DownloadFileManager {
202 public: 195 public:
203 // Safe to null this out even if it doesn't do anything because none 196 MockDownloadFileManager();
204 // of these functions will ever be called; this class just exists 197
205 // to have something to pass to the DownloadItemImpl base class 198 void CreateDownloadFile(
206 // of MockDownloadItemImpl. 199 scoped_ptr<DownloadCreateInfo> info,
207 virtual void DelegateStart(DownloadItemImpl* download) OVERRIDE { 200 scoped_ptr<content::ByteStreamReader> stream,
208 NOTREACHED(); 201 scoped_refptr<content::DownloadManager> download_manager,
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);
209 } 212 }
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();
210 }; 233 };
211 234
235 MockDownloadFileManager::MockDownloadFileManager()
236 : DownloadFileManager(NULL) {}
237
238 MockDownloadFileManager::~MockDownloadFileManager() {}
239
212 class MockDownloadItemFactory 240 class MockDownloadItemFactory
213 : public content::DownloadItemFactory, 241 : public content::DownloadItemFactory,
214 public base::SupportsWeakPtr<MockDownloadItemFactory> { 242 public base::SupportsWeakPtr<MockDownloadItemFactory> {
215 public: 243 public:
216 MockDownloadItemFactory(); 244 MockDownloadItemFactory();
217 virtual ~MockDownloadItemFactory(); 245 virtual ~MockDownloadItemFactory();
218 246
219 // Access to map of created items. 247 // Access to map of created items.
220 // TODO(rdsmith): Could add type (save page, persisted, etc.) 248 // TODO(rdsmith): Could add type (save page, persisted, etc.)
221 // functionality if it's ever needed by consumers. 249 // functionality if it's ever needed by consumers.
(...skipping 23 matching lines...) Expand all
245 virtual DownloadItemImpl* CreateSavePageItem( 273 virtual DownloadItemImpl* CreateSavePageItem(
246 DownloadItemImplDelegate* delegate, 274 DownloadItemImplDelegate* delegate,
247 const FilePath& path, 275 const FilePath& path,
248 const GURL& url, 276 const GURL& url,
249 content::DownloadId download_id, 277 content::DownloadId download_id,
250 const std::string& mime_type, 278 const std::string& mime_type,
251 const net::BoundNetLog& bound_net_log) OVERRIDE; 279 const net::BoundNetLog& bound_net_log) OVERRIDE;
252 280
253 private: 281 private:
254 std::map<int32, MockDownloadItemImpl*> items_; 282 std::map<int32, MockDownloadItemImpl*> items_;
255 NullDownloadItemImplDelegate item_delegate_; 283 DownloadItemImplDelegate item_delegate_;
256 284
257 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); 285 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory);
258 }; 286 };
259 287
260 MockDownloadItemFactory::MockDownloadItemFactory() {} 288 MockDownloadItemFactory::MockDownloadItemFactory() {}
261 289
262 MockDownloadItemFactory::~MockDownloadItemFactory() {} 290 MockDownloadItemFactory::~MockDownloadItemFactory() {}
263 291
264 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) { 292 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) {
265 if (items_.find(id) == items_.end()) 293 if (items_.find(id) == items_.end())
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 const DownloadCreateInfo& info, 333 const DownloadCreateInfo& info,
306 scoped_ptr<DownloadRequestHandleInterface> request_handle, 334 scoped_ptr<DownloadRequestHandleInterface> request_handle,
307 const net::BoundNetLog& bound_net_log) { 335 const net::BoundNetLog& bound_net_log) {
308 int local_id = info.download_id.local(); 336 int local_id = info.download_id.local();
309 DCHECK(items_.find(local_id) == items_.end()); 337 DCHECK(items_.find(local_id) == items_.end());
310 338
311 MockDownloadItemImpl* result = 339 MockDownloadItemImpl* result =
312 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 340 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
313 EXPECT_CALL(*result, GetId()) 341 EXPECT_CALL(*result, GetId())
314 .WillRepeatedly(Return(local_id)); 342 .WillRepeatedly(Return(local_id));
315 EXPECT_CALL(*result, GetGlobalId())
316 .WillRepeatedly(Return(content::DownloadId(delegate, local_id)));
317 items_[local_id] = result; 343 items_[local_id] = result;
318 344
319 // Active items are created and then immediately are called to start
320 // the download.
321 EXPECT_CALL(*result, MockStart(_));
322
323 return result; 345 return result;
324 } 346 }
325 347
326 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( 348 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
327 DownloadItemImplDelegate* delegate, 349 DownloadItemImplDelegate* delegate,
328 const FilePath& path, 350 const FilePath& path,
329 const GURL& url, 351 const GURL& url,
330 content::DownloadId download_id, 352 content::DownloadId download_id,
331 const std::string& mime_type, 353 const std::string& mime_type,
332 const net::BoundNetLog& bound_net_log) { 354 const net::BoundNetLog& bound_net_log) {
333 int local_id = download_id.local(); 355 int local_id = download_id.local();
334 DCHECK(items_.find(local_id) == items_.end()); 356 DCHECK(items_.find(local_id) == items_.end());
335 357
336 MockDownloadItemImpl* result = 358 MockDownloadItemImpl* result =
337 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 359 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
338 EXPECT_CALL(*result, GetId()) 360 EXPECT_CALL(*result, GetId())
339 .WillRepeatedly(Return(local_id)); 361 .WillRepeatedly(Return(local_id));
340 items_[local_id] = result; 362 items_[local_id] = result;
341 363
342 return result; 364 return result;
343 } 365 }
344 366
345 class MockDownloadFileFactory
346 : public content::DownloadFileFactory,
347 public base::SupportsWeakPtr<MockDownloadFileFactory> {
348 public:
349 MockDownloadFileFactory() {}
350 virtual ~MockDownloadFileFactory() {}
351
352 // Overridden method from DownloadFileFactory
353 MOCK_METHOD8(MockCreateFile, content::DownloadFile*(
354 const content::DownloadSaveInfo&,
355 GURL, GURL, int64, bool,
356 content::ByteStreamReader*,
357 const net::BoundNetLog&,
358 base::WeakPtr<content::DownloadDestinationObserver>));
359
360 virtual content::DownloadFile* CreateFile(
361 const content::DownloadSaveInfo& save_info,
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, url, referrer_url, received_bytes,
370 calculate_hash, stream.get(), bound_net_log,
371 observer);
372 }
373 };
374
375 class MockBrowserContext : public content::BrowserContext { 367 class MockBrowserContext : public content::BrowserContext {
376 public: 368 public:
377 MockBrowserContext() {} 369 MockBrowserContext() {}
378 ~MockBrowserContext() {} 370 ~MockBrowserContext() {}
379 371
380 MOCK_METHOD0(GetPath, FilePath()); 372 MOCK_METHOD0(GetPath, FilePath());
381 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); 373 MOCK_CONST_METHOD0(IsOffTheRecord, bool());
382 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*()); 374 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*());
383 MOCK_METHOD1(GetRequestContextForRenderProcess, 375 MOCK_METHOD1(GetRequestContextForRenderProcess,
384 net::URLRequestContextGetter*(int renderer_child_id)); 376 net::URLRequestContextGetter*(int renderer_child_id));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 409
418 DownloadManagerTest() 410 DownloadManagerTest()
419 : ui_thread_(content::BrowserThread::UI, &message_loop_), 411 : ui_thread_(content::BrowserThread::UI, &message_loop_),
420 file_thread_(content::BrowserThread::FILE, &message_loop_), 412 file_thread_(content::BrowserThread::FILE, &message_loop_),
421 next_download_id_(0) { 413 next_download_id_(0) {
422 } 414 }
423 415
424 // We tear down everything in TearDown(). 416 // We tear down everything in TearDown().
425 ~DownloadManagerTest() {} 417 ~DownloadManagerTest() {}
426 418
427 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate, 419 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate,
428 // then create a DownloadManager that points 420 // and MockDownloadFileManager, then create a DownloadManager that points
429 // at all of those. 421 // at all of those.
430 virtual void SetUp() { 422 virtual void SetUp() {
431 DCHECK(!download_manager_.get()); 423 DCHECK(!download_manager_.get());
432 424
433 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); 425 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
434 mock_download_file_factory_ = (new MockDownloadFileFactory())->AsWeakPtr();
435 mock_download_manager_delegate_.reset( 426 mock_download_manager_delegate_.reset(
436 new StrictMock<MockDownloadManagerDelegate>); 427 new StrictMock<MockDownloadManagerDelegate>);
437 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) 428 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
438 .WillOnce(Return()); 429 .WillOnce(Return());
430 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>;
431 EXPECT_CALL(*mock_download_file_manager_.get(),
432 OnDownloadManagerShutdown(_));
439 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); 433 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
440 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) 434 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
441 .WillRepeatedly(Return(false)); 435 .WillRepeatedly(Return(false));
442 436
443 download_manager_ = new DownloadManagerImpl( 437 download_manager_ = new DownloadManagerImpl(
438 mock_download_file_manager_.get(),
444 scoped_ptr<content::DownloadItemFactory>( 439 scoped_ptr<content::DownloadItemFactory>(
445 mock_download_item_factory_.get()).Pass(), 440 mock_download_item_factory_.get()).Pass(), NULL);
446 scoped_ptr<content::DownloadFileFactory>(
447 mock_download_file_factory_.get()).Pass(), NULL);
448 observer_.reset(new MockDownloadManagerObserver()); 441 observer_.reset(new MockDownloadManagerObserver());
449 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 442 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
450 .WillOnce(Return()); 443 .WillOnce(Return());
451 download_manager_->AddObserver(observer_.get()); 444 download_manager_->AddObserver(observer_.get());
452 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); 445 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
453 download_manager_->Init(mock_browser_context_.get()); 446 download_manager_->Init(mock_browser_context_.get());
454 } 447 }
455 448
456 virtual void TearDown() { 449 virtual void TearDown() {
457 while (MockDownloadItemImpl* 450 while (MockDownloadItemImpl*
458 item = mock_download_item_factory_->PopItem()) { 451 item = mock_download_item_factory_->PopItem()) {
459 EXPECT_CALL(*item, GetSafetyState()) 452 EXPECT_CALL(*item, GetSafetyState())
460 .WillOnce(Return(content::DownloadItem::SAFE)); 453 .WillOnce(Return(content::DownloadItem::SAFE));
461 EXPECT_CALL(*item, IsPartialDownload()) 454 EXPECT_CALL(*item, IsPartialDownload())
462 .WillOnce(Return(false)); 455 .WillOnce(Return(false));
463 } 456 }
464 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) 457 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
465 .WillOnce(Return()); 458 .WillOnce(Return());
466 459
467 download_manager_->Shutdown(); 460 download_manager_->Shutdown();
468 download_manager_ = NULL; 461 download_manager_ = NULL;
469 message_loop_.RunAllPending(); 462 message_loop_.RunAllPending();
470 ASSERT_EQ(NULL, mock_download_item_factory_.get()); 463 ASSERT_EQ(NULL, mock_download_item_factory_.get());
471 ASSERT_EQ(NULL, mock_download_file_factory_.get());
472 message_loop_.RunAllPending(); 464 message_loop_.RunAllPending();
473 mock_download_manager_delegate_.reset(); 465 mock_download_manager_delegate_.reset();
466 mock_download_file_manager_ = NULL;
474 mock_browser_context_.reset(); 467 mock_browser_context_.reset();
475 } 468 }
476 469
477 // Returns download id. 470 // Returns download id.
478 MockDownloadItemImpl& AddItemToManager() { 471 MockDownloadItemImpl& AddItemToManager() {
479 DownloadCreateInfo info; 472 DownloadCreateInfo info;
480 473
481 static const char* kDownloadIdDomain = "Test download id domain"; 474 static const char* kDownloadIdDomain = "Test download id domain";
482 475
483 // Args are ignored except for download id, so everything else can be 476 // Args are ignored except for download id, so everything else can be
484 // null. 477 // null.
485 int id = next_download_id_; 478 int id = next_download_id_;
486 ++next_download_id_; 479 ++next_download_id_;
487 info.download_id = content::DownloadId(kDownloadIdDomain, id); 480 info.download_id = content::DownloadId(kDownloadIdDomain, id);
488 info.request_handle = DownloadRequestHandle(); 481 info.request_handle = DownloadRequestHandle();
489 EXPECT_CALL(GetMockObserver(), 482 download_manager_->CreateDownloadItem(&info);
490 OnDownloadCreated(download_manager_.get(), _));
491 download_manager_->CreateDownloadItem(&info, net::BoundNetLog());
492 483
493 DCHECK(mock_download_item_factory_->GetItem(id)); 484 DCHECK(mock_download_item_factory_->GetItem(id));
494 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); 485 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
495 // Satisfy expectation. If the item is created in StartDownload(), 486 ON_CALL(item, GetId())
496 // we call Start on it immediately, so we need to set that expectation 487 .WillByDefault(Return(id));
497 // in the factory.
498 item.Start(scoped_ptr<content::DownloadFile>());
499 488
500 return item; 489 return item;
501 } 490 }
502 491
503 MockDownloadItemImpl& GetMockDownloadItem(int id) { 492 MockDownloadItemImpl& GetMockDownloadItem(int id) {
504 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); 493 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
505 494
506 DCHECK(itemp); 495 DCHECK(itemp);
507 return *itemp; 496 return *itemp;
508 } 497 }
509 498
510 void RemoveMockDownloadItem(int id) { 499 void RemoveMockDownloadItem(int id) {
511 // Owned by DownloadManager; should be deleted there. 500 // Owned by DownloadManager; should be deleted there.
512 mock_download_item_factory_->RemoveItem(id); 501 mock_download_item_factory_->RemoveItem(id);
513 } 502 }
514 503
515 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { 504 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
516 return *mock_download_manager_delegate_; 505 return *mock_download_manager_delegate_;
517 } 506 }
518 507
508 MockDownloadFileManager& GetMockDownloadFileManager() {
509 return *mock_download_file_manager_;
510 }
511
519 MockDownloadManagerObserver& GetMockObserver() { 512 MockDownloadManagerObserver& GetMockObserver() {
520 return *observer_; 513 return *observer_;
521 } 514 }
522 515
523 // Probe at private internals. 516 // Probe at private internals.
524 void DownloadStopped(DownloadItemImpl* item) { 517 void DownloadStopped(DownloadItemImpl* item) {
525 download_manager_->DownloadStopped(item); 518 download_manager_->DownloadStopped(item);
526 } 519 }
527 520
528 void DelegateStart(DownloadItemImpl* item) {
529 download_manager_->DelegateStart(item);
530 }
531
532 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 521 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
533 // For DCHECK in AddDownloadItemToHistory. Don't want to use 522 // For DCHECK in AddDownloadItemToHistory. Don't want to use
534 // WillRepeatedly as it may have to return true after this. 523 // WillRepeatedly as it may have to return true after this.
535 if (DCHECK_IS_ON()) 524 if (DCHECK_IS_ON())
536 EXPECT_CALL(item, IsPersisted()) 525 EXPECT_CALL(item, IsPersisted())
537 .WillRepeatedly(Return(false)); 526 .WillRepeatedly(Return(false));
538 527
539 EXPECT_CALL(item, SetDbHandle(db_handle)); 528 EXPECT_CALL(item, SetDbHandle(db_handle));
540 EXPECT_CALL(item, SetIsPersisted()); 529 EXPECT_CALL(item, SetIsPersisted());
541 EXPECT_CALL(item, GetDbHandle()) 530 EXPECT_CALL(item, GetDbHandle())
(...skipping 12 matching lines...) Expand all
554 // Null out MaybeCompleteDownload 543 // Null out MaybeCompleteDownload
555 EXPECT_CALL(item, AllDataSaved()) 544 EXPECT_CALL(item, AllDataSaved())
556 .WillOnce(Return(false)); 545 .WillOnce(Return(false));
557 546
558 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle); 547 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle);
559 } 548 }
560 549
561 protected: 550 protected:
562 // Key test variable; we'll keep it available to sub-classes. 551 // Key test variable; we'll keep it available to sub-classes.
563 scoped_refptr<DownloadManagerImpl> download_manager_; 552 scoped_refptr<DownloadManagerImpl> download_manager_;
564 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
565 553
566 private: 554 private:
567 MessageLoopForUI message_loop_; 555 MessageLoopForUI message_loop_;
568 content::TestBrowserThread ui_thread_; 556 content::TestBrowserThread ui_thread_;
569 content::TestBrowserThread file_thread_; 557 content::TestBrowserThread file_thread_;
570 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; 558 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
571 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; 559 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
560 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_;
572 scoped_ptr<MockBrowserContext> mock_browser_context_; 561 scoped_ptr<MockBrowserContext> mock_browser_context_;
573 scoped_ptr<MockDownloadManagerObserver> observer_; 562 scoped_ptr<MockDownloadManagerObserver> observer_;
574 int next_download_id_; 563 int next_download_id_;
575 564
576 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 565 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
577 }; 566 };
578 567
579 // Confirm the appropriate invocations occur when you start a download. 568 // Confirm the appropriate invocations occur when you start a download.
580 TEST_F(DownloadManagerTest, StartDownload) { 569 TEST_F(DownloadManagerTest, StartDownload) {
581 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 570 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
582 scoped_ptr<content::ByteStreamReader> stream; 571 scoped_ptr<content::ByteStreamReader> stream;
583 int32 local_id(5); // Random value 572 int32 local_id(5); // Random value
584 573
585 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id)); 574 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id));
586 575
587 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 576 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
588 .WillOnce(Return()); 577 .WillOnce(Return());
589 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) 578 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId())
590 .WillOnce(Return(content::DownloadId(this, local_id))); 579 .WillOnce(Return(content::DownloadId(this, local_id)));
591 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) 580 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
592 .WillOnce(Return(true)); 581 .WillOnce(Return(true));
593 EXPECT_CALL(*mock_download_file_factory_.get(), 582 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile(
594 MockCreateFile(Ref(info->save_info), _, _, 0, true, stream.get(), 583 info.get(), static_cast<content::ByteStreamReader*>(NULL),
595 _, _)); 584 download_manager_.get(), true, _, _));
596 585
597 download_manager_->StartDownload(info.Pass(), stream.Pass()); 586 download_manager_->StartDownload(info.Pass(), stream.Pass());
598 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); 587 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id));
599 } 588 }
600 589
601 // Confirm that calling DelegateStart behaves properly if the delegate 590 // Do the results of an OnDownloadInterrupted get passed through properly
602 // blocks starting. 591 // to the DownloadItem?
603 TEST_F(DownloadManagerTest, DelegateStart_True) { 592 TEST_F(DownloadManagerTest, OnDownloadInterrupted) {
593 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
594 .WillOnce(Return());
604 // Put a mock we have a handle to on the download manager. 595 // Put a mock we have a handle to on the download manager.
605 MockDownloadItemImpl& item(AddItemToManager()); 596 MockDownloadItemImpl& item(AddItemToManager());
597 int download_id = item.GetId();
606 598
607 EXPECT_CALL(GetMockDownloadManagerDelegate(), 599 content::DownloadInterruptReason reason(
608 DetermineDownloadTarget(&item, _)) 600 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
609 .WillOnce(Return(true));
610 DelegateStart(&item);
611 }
612 601
613 // Confirm that calling DelegateStart behaves properly if the delegate 602 EXPECT_CALL(item, Interrupt(reason));
614 // allows starting. This also tests OnDownloadTargetDetermined. 603 download_manager_->OnDownloadInterrupted(download_id, reason);
615 TEST_F(DownloadManagerTest, DelegateStart_False) { 604 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id));
616 // Put a mock we have a handle to on the download manager.
617 MockDownloadItemImpl& item(AddItemToManager());
618
619 EXPECT_CALL(GetMockDownloadManagerDelegate(),
620 DetermineDownloadTarget(&item, _))
621 .WillOnce(Return(false));
622 EXPECT_CALL(item, OnDownloadTargetDetermined(
623 _, content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
624 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, _));
625 FilePath null_path;
626 EXPECT_CALL(item, GetForcedFilePath())
627 .WillOnce(ReturnRef(null_path));
628 DelegateStart(&item);
629 } 605 }
630 606
631 // Does DownloadStopped remove Download from appropriate queues? 607 // Does DownloadStopped remove Download from appropriate queues?
632 // This test tests non-persisted downloads. 608 // This test tests non-persisted downloads.
633 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { 609 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
610 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
611 .WillOnce(Return());
634 // 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.
635 MockDownloadItemImpl& item(AddItemToManager()); 613 MockDownloadItemImpl& item(AddItemToManager());
636 614
637 EXPECT_CALL(item, IsPersisted()) 615 EXPECT_CALL(item, IsPersisted())
638 .WillRepeatedly(Return(false)); 616 .WillRepeatedly(Return(false));
639 EXPECT_CALL(item, GetState()) 617 EXPECT_CALL(item, GetState())
640 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 618 .WillRepeatedly(Return(DownloadItem::CANCELLED));
641 EXPECT_CALL(item, GetDbHandle()) 619 EXPECT_CALL(item, GetDbHandle())
642 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); 620 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
643 621
622 EXPECT_CALL(item, OffThreadCancel());
644 DownloadStopped(&item); 623 DownloadStopped(&item);
645 // TODO(rdsmith): Confirm that the download item is no longer on the 624 // TODO(rdsmith): Confirm that the download item is no longer on the
646 // active list by calling download_manager_->GetActiveDownloadItem(id). 625 // active list by calling download_manager_->GetActiveDownloadItem(id).
647 // Currently, the item is left on the active list for rendez-vous with 626 // Currently, the item is left on the active list for rendez-vous with
648 // the history system :-{. 627 // the history system :-{.
649 } 628 }
650 629
651 // Does DownloadStopped remove Download from appropriate queues? 630 // Does DownloadStopped remove Download from appropriate queues?
652 // This test tests persisted downloads. 631 // This test tests persisted downloads.
653 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { 632 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
633 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
634 .WillOnce(Return());
654 // Put a mock we have a handle to on the download manager. 635 // Put a mock we have a handle to on the download manager.
655 MockDownloadItemImpl& item(AddItemToManager()); 636 MockDownloadItemImpl& item(AddItemToManager());
656 int download_id = item.GetId(); 637 int download_id = item.GetId();
657 int64 db_handle = 0x7; 638 int64 db_handle = 0x7;
658 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 639 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
659 .WillOnce(Return()); 640 .WillOnce(Return());
660 AddItemToHistory(item, db_handle); 641 AddItemToHistory(item, db_handle);
661 642
662 EXPECT_CALL(item, IsPersisted()) 643 EXPECT_CALL(item, IsPersisted())
663 .WillRepeatedly(Return(true)); 644 .WillRepeatedly(Return(true));
664 EXPECT_CALL(GetMockDownloadManagerDelegate(), 645 EXPECT_CALL(GetMockDownloadManagerDelegate(),
665 UpdateItemInPersistentStore(&item)); 646 UpdateItemInPersistentStore(&item));
666 EXPECT_CALL(item, GetState()) 647 EXPECT_CALL(item, GetState())
667 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 648 .WillRepeatedly(Return(DownloadItem::CANCELLED));
668 EXPECT_CALL(item, GetDbHandle()) 649 EXPECT_CALL(item, GetDbHandle())
669 .WillRepeatedly(Return(db_handle)); 650 .WillRepeatedly(Return(db_handle));
670 651
652 EXPECT_CALL(item, OffThreadCancel());
671 DownloadStopped(&item); 653 DownloadStopped(&item);
672 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); 654 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
673 } 655 }
OLDNEW
« no previous file with comments | « content/browser/download/download_manager_impl.cc ('k') | content/browser/download/download_resource_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698