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

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

Issue 10831302: Download resumption - Preliminary (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed content unit tests. Created 8 years, 2 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
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 "base/message_loop.h" 5 #include "base/message_loop.h"
6 #include "base/stl_util.h" 6 #include "base/stl_util.h"
7 #include "base/threading/thread.h" 7 #include "base/threading/thread.h"
8 #include "content/browser/download/byte_stream.h" 8 #include "content/browser/download/byte_stream.h"
9 #include "content/browser/download/download_create_info.h" 9 #include "content/browser/download/download_create_info.h"
10 #include "content/browser/download/download_file_manager.h" 10 #include "content/browser/download/download_file_manager.h"
11 #include "content/browser/download/download_item_impl.h" 11 #include "content/browser/download/download_item_impl.h"
12 #include "content/browser/download/download_item_impl_delegate.h" 12 #include "content/browser/download/download_item_impl_delegate.h"
13 #include "content/browser/download/download_request_handle.h" 13 #include "content/browser/download/download_request_handle.h"
14 #include "content/public/browser/download_id.h" 14 #include "content/public/browser/download_id.h"
15 #include "content/public/browser/download_interrupt_reasons.h" 15 #include "content/public/browser/download_interrupt_reasons.h"
16 #include "content/public/browser/download_url_parameters.h"
16 #include "content/public/test/mock_download_item.h" 17 #include "content/public/test/mock_download_item.h"
17 #include "content/public/test/test_browser_thread.h" 18 #include "content/public/test/test_browser_thread.h"
18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 using content::BrowserThread; 22 using content::BrowserThread;
22 using content::DownloadId; 23 using content::DownloadId;
23 using content::DownloadItem; 24 using content::DownloadItem;
24 using content::DownloadManager; 25 using content::DownloadManager;
25 using content::MockDownloadItem; 26 using content::MockDownloadItem;
26 using content::WebContents; 27 using content::WebContents;
27 using ::testing::_; 28 using ::testing::_;
28 using ::testing::AllOf; 29 using ::testing::AllOf;
29 using ::testing::Property; 30 using ::testing::Property;
30 using ::testing::Return; 31 using ::testing::Return;
31 32
32 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; 33 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain";
33 34
34 namespace { 35 namespace {
35 class MockDelegate : public DownloadItemImplDelegate { 36 class MockDelegate : public DownloadItemImplDelegate {
36 public: 37 public:
37 MockDelegate(DownloadFileManager* file_manager) 38 MockDelegate(DownloadFileManager* file_manager)
38 : file_manager_(file_manager) { 39 : file_manager_(file_manager) {
39 } 40 }
40 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); 41 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path));
41 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); 42 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download));
42 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); 43 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download));
43 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItemImpl* download)); 44 MOCK_METHOD1(MaybeCompleteDownload, void(int32 download_id));
45 MOCK_METHOD2(RestartInterruptedDownload,
46 void(DownloadItemImpl* download,
47 const content::DownloadUrlParameters::OnStartedCallback&));
44 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 48 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
45 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); 49 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download));
46 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); 50 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download));
47 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); 51 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download));
48 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); 52 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download));
49 MOCK_METHOD1(DownloadRenamedToIntermediateName, 53 MOCK_METHOD1(DownloadRenamedToIntermediateName,
50 void(DownloadItemImpl* download)); 54 void(DownloadItemImpl* download));
51 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); 55 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download));
52 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); 56 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download));
53 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE { 57 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE {
54 return file_manager_; 58 return file_manager_;
55 } 59 }
56 private: 60 private:
57 DownloadFileManager* file_manager_; 61 DownloadFileManager* file_manager_;
58 }; 62 };
59 63
60 class MockRequestHandle : public DownloadRequestHandleInterface { 64 class MockRequestHandle : public DownloadRequestHandleInterface {
61 public: 65 public:
62 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); 66 MOCK_CONST_METHOD0(GetWebContents, WebContents*());
63 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); 67 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*());
64 MOCK_CONST_METHOD0(PauseRequest, void()); 68 MOCK_CONST_METHOD0(PauseRequest, void());
65 MOCK_CONST_METHOD0(ResumeRequest, void()); 69 MOCK_CONST_METHOD0(ResumeRequest, void());
66 MOCK_CONST_METHOD0(CancelRequest, void()); 70 MOCK_CONST_METHOD0(CancelRequest, void());
71 MOCK_METHOD1(SetRequestId, void(int new_request_id));
72 MOCK_CONST_METHOD0(RequestId, int());
67 MOCK_CONST_METHOD0(DebugString, std::string()); 73 MOCK_CONST_METHOD0(DebugString, std::string());
68 }; 74 };
69 75
70 class MockDownloadFileFactory : public content::DownloadFileFactory { 76 class MockDownloadFileFactory : public content::DownloadFileFactory {
71 public: 77 public:
72 content::DownloadFile* CreateFile( 78 content::DownloadFile* CreateFile(
73 DownloadCreateInfo* info, 79 DownloadCreateInfo* info,
74 scoped_ptr<content::ByteStreamReader> stream_reader, 80 scoped_ptr<content::ByteStreamReader> stream_reader,
75 DownloadManager* mgr, 81 DownloadManager* mgr,
76 bool calculate_hash, 82 bool calculate_hash,
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 static int next_id; 218 static int next_id;
213 info_->download_id = 219 info_->download_id =
214 content::DownloadId(kValidDownloadItemIdDomain, ++next_id); 220 content::DownloadId(kValidDownloadItemIdDomain, ++next_id);
215 info_->prompt_user_for_save_location = false; 221 info_->prompt_user_for_save_location = false;
216 info_->url_chain.push_back(GURL()); 222 info_->url_chain.push_back(GURL());
217 info_->state = state; 223 info_->state = state;
218 224
219 scoped_ptr<DownloadRequestHandleInterface> request_handle( 225 scoped_ptr<DownloadRequestHandleInterface> request_handle(
220 new testing::NiceMock<MockRequestHandle>); 226 new testing::NiceMock<MockRequestHandle>);
221 DownloadItemImpl* download = 227 DownloadItemImpl* download =
222 new DownloadItemImpl(&delegate_, *(info_.get()), 228 new DownloadItemImpl(&delegate_, *(info_.get()), net::BoundNetLog());
223 request_handle.Pass(), net::BoundNetLog()); 229 download->SetRequest(request_handle.Pass());
224 allocated_downloads_.insert(download); 230 allocated_downloads_.insert(download);
225 return download; 231 return download;
226 } 232 }
227 233
228 // Destroy a previously created download item. 234 // Destroy a previously created download item.
229 void DestroyDownloadItem(DownloadItem* item) { 235 void DestroyDownloadItem(DownloadItem* item) {
230 allocated_downloads_.erase(item); 236 allocated_downloads_.erase(item);
231 delete item; 237 delete item;
232 } 238 }
233 239
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 MockObserver observer(item); 316 MockObserver observer(item);
311 317
312 item->OnDownloadedFileRemoved(); 318 item->OnDownloadedFileRemoved();
313 ASSERT_TRUE(observer.CheckUpdated()); 319 ASSERT_TRUE(observer.CheckUpdated());
314 } 320 }
315 321
316 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { 322 TEST_F(DownloadItemTest, NotificationAfterInterrupted) {
317 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 323 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
318 MockObserver observer(item); 324 MockObserver observer(item);
319 325
326 EXPECT_CALL(*mock_delegate(), RestartInterruptedDownload(_,_))
327 .Times(0);
328
320 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_NONE); 329 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_NONE);
321 ASSERT_TRUE(observer.CheckUpdated()); 330 ASSERT_TRUE(observer.CheckUpdated());
322 } 331 }
323 332
324 TEST_F(DownloadItemTest, NotificationAfterDelete) { 333 TEST_F(DownloadItemTest, NotificationAfterDelete) {
325 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 334 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
326 MockObserver observer(item); 335 MockObserver observer(item);
327 336
328 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); 337 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
329 ASSERT_TRUE(observer.CheckUpdated()); 338 ASSERT_TRUE(observer.CheckUpdated());
330 } 339 }
331 340
332 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { 341 TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
333 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 342 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
334 MockObserver observer(item); 343 MockObserver observer(item);
335 344
336 DestroyDownloadItem(item); 345 DestroyDownloadItem(item);
337 ASSERT_TRUE(observer.CheckDestroyed()); 346 ASSERT_TRUE(observer.CheckDestroyed());
338 } 347 }
339 348
349 TEST_F(DownloadItemTest, RestartAfterInterrupted) {
350 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
351 MockObserver observer(item);
352 // Pretend we've recorded it in the history DB, to allow restart.
353 item->SetDbHandle(DownloadItem::kUninitializedHandle);
354 item->SetIsPersisted();
355
356 EXPECT_CALL(*mock_delegate(), RestartInterruptedDownload(item,_))
357 .Times(1);
358
359 //EXPECT_CALL(item, IsInterrupted())
360 // .Times(1)
361 // .WillOnce(Return(true));
362
363 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
364 ASSERT_TRUE(observer.CheckUpdated());
365 }
366
367 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) {
368 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
369 MockObserver observer(item);
370 // Pretend we've recorded it in the history DB, to allow restart.
371 item->SetDbHandle(DownloadItem::kUninitializedHandle);
372 item->SetIsPersisted();
373
374 EXPECT_CALL(*mock_delegate(), RestartInterruptedDownload(item,_))
375 .Times(DownloadItemImpl::kMaxAutoResumeAttempts);
376
377 scoped_ptr<DownloadRequestHandleInterface> request_handle(
378 new testing::NiceMock<MockRequestHandle>);
379
380 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 2); ++i) {
381 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
382 // Should call delegate->RestartInterruptedDownload() here, up to
383 // kMaxAutoResumeAttempts times.
384 item->Resume(request_handle.Pass());
385 }
386 }
387
340 TEST_F(DownloadItemTest, NotificationAfterRemove) { 388 TEST_F(DownloadItemTest, NotificationAfterRemove) {
341 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 389 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
342 MockObserver observer(item); 390 MockObserver observer(item);
343 391
344 item->Remove(); 392 item->Remove();
345 ASSERT_TRUE(observer.CheckUpdated()); 393 ASSERT_TRUE(observer.CheckUpdated());
346 ASSERT_TRUE(observer.CheckRemoved()); 394 ASSERT_TRUE(observer.CheckRemoved());
347 } 395 }
348 396
349 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { 397 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) {
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 553 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
506 554
507 const content::DownloadInterruptReason reason( 555 const content::DownloadInterruptReason reason(
508 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 556 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
509 557
510 // Confirm interrupt sets state properly. 558 // Confirm interrupt sets state properly.
511 item->Interrupt(reason); 559 item->Interrupt(reason);
512 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 560 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
513 EXPECT_EQ(reason, item->GetLastReason()); 561 EXPECT_EQ(reason, item->GetLastReason());
514 562
515 // Cancel should result in no change. 563 // Cancel should kill it.
516 item->Cancel(true); 564 item->Cancel(true);
517 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 565 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
518 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, 566 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED,
519 item->GetLastReason()); 567 item->GetLastReason());
520 } 568 }
521 569
522 TEST_F(DownloadItemTest, Canceled) { 570 TEST_F(DownloadItemTest, Canceled) {
523 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 571 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
524 572
525 // Confirm cancel sets state properly. 573 // Confirm cancel sets state properly.
526 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); 574 EXPECT_CALL(*mock_delegate(), DownloadStopped(item));
527 item->Cancel(true); 575 item->Cancel(true);
528 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); 576 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
529 } 577 }
530 578
531 TEST_F(DownloadItemTest, FileRemoved) { 579 TEST_F(DownloadItemTest, FileRemoved) {
532 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 580 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
533 581
534 EXPECT_FALSE(item->GetFileExternallyRemoved()); 582 EXPECT_FALSE(item->GetFileExternallyRemoved());
535 item->OnDownloadedFileRemoved(); 583 item->OnDownloadedFileRemoved();
536 EXPECT_TRUE(item->GetFileExternallyRemoved()); 584 EXPECT_TRUE(item->GetFileExternallyRemoved());
537 } 585 }
538 586
539 TEST(MockDownloadItem, Compiles) { 587 TEST(MockDownloadItem, Compiles) {
540 MockDownloadItem mock_item; 588 MockDownloadItem mock_item;
541 } 589 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698