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

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

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