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

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

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix typos Created 4 years, 10 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 "content/browser/download/download_item_impl.h" 5 #include "content/browser/download/download_item_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <iterator> 9 #include <iterator>
10 #include <queue> 10 #include <queue>
(...skipping 10 matching lines...) Expand all
21 #include "content/browser/download/download_file_factory.h" 21 #include "content/browser/download/download_file_factory.h"
22 #include "content/browser/download/download_item_impl_delegate.h" 22 #include "content/browser/download/download_item_impl_delegate.h"
23 #include "content/browser/download/download_request_handle.h" 23 #include "content/browser/download/download_request_handle.h"
24 #include "content/browser/download/mock_download_file.h" 24 #include "content/browser/download/mock_download_file.h"
25 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/download_destination_observer.h" 26 #include "content/public/browser/download_destination_observer.h"
27 #include "content/public/browser/download_interrupt_reasons.h" 27 #include "content/public/browser/download_interrupt_reasons.h"
28 #include "content/public/browser/download_url_parameters.h" 28 #include "content/public/browser/download_url_parameters.h"
29 #include "content/public/common/content_features.h" 29 #include "content/public/common/content_features.h"
30 #include "content/public/test/mock_download_item.h" 30 #include "content/public/test/mock_download_item.h"
31 #include "content/public/test/mock_download_item.h"
32 #include "content/public/test/test_browser_context.h"
31 #include "content/public/test/test_browser_context.h" 33 #include "content/public/test/test_browser_context.h"
32 #include "content/public/test/test_browser_thread_bundle.h" 34 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "content/public/test/web_contents_tester.h"
33 #include "testing/gmock/include/gmock/gmock.h" 36 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
35 38
36 using ::testing::DoAll; 39 using ::testing::DoAll;
37 using ::testing::NiceMock; 40 using ::testing::NiceMock;
38 using ::testing::Property; 41 using ::testing::Property;
39 using ::testing::Return; 42 using ::testing::Return;
40 using ::testing::SaveArg; 43 using ::testing::SaveArg;
41 using ::testing::StrictMock; 44 using ::testing::StrictMock;
42 using ::testing::WithArg; 45 using ::testing::WithArg;
(...skipping 26 matching lines...) Expand all
69 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); 72 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*));
70 73
71 void ResumeInterruptedDownload(scoped_ptr<DownloadUrlParameters> params, 74 void ResumeInterruptedDownload(scoped_ptr<DownloadUrlParameters> params,
72 uint32_t id) override { 75 uint32_t id) override {
73 MockResumeInterruptedDownload(params.get(), id); 76 MockResumeInterruptedDownload(params.get(), id);
74 } 77 }
75 MOCK_METHOD2(MockResumeInterruptedDownload, 78 MOCK_METHOD2(MockResumeInterruptedDownload,
76 void(DownloadUrlParameters* params, uint32_t id)); 79 void(DownloadUrlParameters* params, uint32_t id));
77 80
78 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); 81 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*());
79 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*));
80 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); 82 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*));
81 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); 83 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*));
82 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); 84 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*));
83 85
84 void VerifyAndClearExpectations() { 86 void VerifyAndClearExpectations() {
85 ::testing::Mock::VerifyAndClearExpectations(this); 87 ::testing::Mock::VerifyAndClearExpectations(this);
86 SetDefaultExpectations(); 88 SetDefaultExpectations();
87 } 89 }
88 90
89 private: 91 private:
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 int resume_count() const { return resume_count_; } 133 int resume_count() const { return resume_count_; }
132 134
133 bool CheckAndResetDownloadUpdated() { 135 bool CheckAndResetDownloadUpdated() {
134 bool was_updated = updated_; 136 bool was_updated = updated_;
135 updated_ = false; 137 updated_ = false;
136 return was_updated; 138 return was_updated;
137 } 139 }
138 140
139 private: 141 private:
140 void OnDownloadRemoved(DownloadItem* download) override { 142 void OnDownloadRemoved(DownloadItem* download) override {
141 DVLOG(20) << " " << __FUNCTION__ 143 SCOPED_TRACE(::testing::Message() << " " << __FUNCTION__ << " download = "
142 << " download = " << download->DebugString(false); 144 << download->DebugString(false));
143 removed_ = true; 145 removed_ = true;
144 } 146 }
145 147
146 void OnDownloadUpdated(DownloadItem* download) override { 148 void OnDownloadUpdated(DownloadItem* download) override {
147 DVLOG(20) << " " << __FUNCTION__ 149 DVLOG(20) << " " << __FUNCTION__
148 << " download = " << download->DebugString(false); 150 << " download = " << download->DebugString(false);
149 updated_ = true; 151 updated_ = true;
150 DownloadItem::DownloadState new_state = download->GetState(); 152 DownloadItem::DownloadState new_state = download->GetState();
151 if (last_state_ == DownloadItem::IN_PROGRESS && 153 if (last_state_ == DownloadItem::IN_PROGRESS &&
152 new_state == DownloadItem::INTERRUPTED) { 154 new_state == DownloadItem::INTERRUPTED) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 } 208 }
207 209
208 ACTION_P(ScheduleClosure, closure) { 210 ACTION_P(ScheduleClosure, closure) {
209 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); 211 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure);
210 } 212 }
211 213
212 } // namespace 214 } // namespace
213 215
214 class DownloadItemTest : public testing::Test { 216 class DownloadItemTest : public testing::Test {
215 public: 217 public:
216 DownloadItemTest() { 218 DownloadItemTest()
219 : delegate_(), next_download_id_(DownloadItem::kInvalidId + 1) {
217 base::FeatureList::ClearInstanceForTesting(); 220 base::FeatureList::ClearInstanceForTesting();
218 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); 221 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList);
219 feature_list->InitializeFromCommandLine(features::kDownloadResumption.name, 222 feature_list->InitializeFromCommandLine(features::kDownloadResumption.name,
220 std::string()); 223 std::string());
221 base::FeatureList::SetInstance(std::move(feature_list)); 224 base::FeatureList::SetInstance(std::move(feature_list));
225
226 create_info_.reset(new DownloadCreateInfo());
227 create_info_->save_info =
228 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo());
229 create_info_->save_info->prompt_for_save_location = false;
230 create_info_->url_chain.push_back(GURL("http://example.com/download"));
231 create_info_->etag = "SomethingToSatisfyResumption";
222 } 232 }
223 233
224 ~DownloadItemTest() { 234 ~DownloadItemTest() {
225 RunAllPendingInMessageLoops(); 235 RunAllPendingInMessageLoops();
226 STLDeleteElements(&allocated_downloads_); 236 STLDeleteElements(&allocated_downloads_);
227 } 237 }
228 238
229 DownloadItemImpl* CreateDownloadItemWithCreateInfo( 239 DownloadItemImpl* CreateDownloadItemWithCreateInfo(
230 scoped_ptr<DownloadCreateInfo> info) { 240 scoped_ptr<DownloadCreateInfo> info) {
231 DownloadItemImpl* download = new DownloadItemImpl( 241 DownloadItemImpl* download = new DownloadItemImpl(
232 &delegate_, next_download_id_++, *(info.get()), net::BoundNetLog()); 242 &delegate_, next_download_id_++, *(info.get()), net::BoundNetLog());
233 allocated_downloads_.insert(download); 243 allocated_downloads_.insert(download);
234 return download; 244 return download;
235 } 245 }
236 246
237 // This class keeps ownership of the created download item; it will 247 // This class keeps ownership of the created download item; it will
238 // be torn down at the end of the test unless DestroyDownloadItem is 248 // be torn down at the end of the test unless DestroyDownloadItem is
239 // called. 249 // called.
240 DownloadItemImpl* CreateDownloadItem() { 250 DownloadItemImpl* CreateDownloadItem() {
241 scoped_ptr<DownloadCreateInfo> info; 251 create_info_->download_id = ++next_download_id_;
242 252 DownloadItemImpl* download =
243 info.reset(new DownloadCreateInfo()); 253 new DownloadItemImpl(&delegate_, create_info_->download_id,
244 info->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); 254 *create_info_, net::BoundNetLog());
245 info->save_info->prompt_for_save_location = false; 255 allocated_downloads_.insert(download);
246 info->url_chain.push_back(GURL("http://example.com/download")); 256 return download;
247 info->etag = "SomethingToSatisfyResumption";
248
249 return CreateDownloadItemWithCreateInfo(std::move(info));
250 } 257 }
251 258
252 // Add DownloadFile to DownloadItem 259 // Add DownloadFile to DownloadItem. Set |callback| to nullptr if a download
260 // target determination is not expected.
253 MockDownloadFile* CallDownloadItemStart( 261 MockDownloadFile* CallDownloadItemStart(
254 DownloadItemImpl* item, 262 DownloadItemImpl* item,
255 DownloadItemImplDelegate::DownloadTargetCallback* callback) { 263 DownloadItemImplDelegate::DownloadTargetCallback* callback) {
256 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); 264 MockDownloadFile* mock_download_file = nullptr;
257 scoped_ptr<DownloadFile> download_file(mock_download_file); 265 scoped_ptr<DownloadFile> download_file;
258 EXPECT_CALL(*mock_download_file, Initialize(_));
259 if (callback) { 266 if (callback) {
260 // Save the callback.
261 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 267 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
262 .WillOnce(SaveArg<1>(callback)); 268 .WillOnce(SaveArg<1>(callback));
263 } else { 269 } else {
264 // Drop it on the floor. 270 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)).Times(0);
265 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
266 } 271 }
267 272
268 scoped_ptr<DownloadRequestHandleInterface> request_handle( 273 // Only create a DownloadFile if the request was successful.
274 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
275 mock_download_file = new StrictMock<MockDownloadFile>;
276 download_file.reset(mock_download_file);
277 EXPECT_CALL(*mock_download_file, Initialize(_))
278 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE));
279 EXPECT_CALL(*mock_download_file, FullPath())
280 .WillRepeatedly(Return(base::FilePath()));
281 }
282
283 scoped_ptr<MockRequestHandle> request_handle(
269 new NiceMock<MockRequestHandle>); 284 new NiceMock<MockRequestHandle>);
270 item->Start(std::move(download_file), std::move(request_handle)); 285 item->Start(std::move(download_file), std::move(request_handle),
286 *create_info_);
271 RunAllPendingInMessageLoops(); 287 RunAllPendingInMessageLoops();
272 288
273 // So that we don't have a function writing to a stack variable 289 // So that we don't have a function writing to a stack variable
274 // lying around if the above failed. 290 // lying around if the above failed.
275 mock_delegate()->VerifyAndClearExpectations(); 291 mock_delegate()->VerifyAndClearExpectations();
276 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) 292 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_))
277 .WillRepeatedly(Return()); 293 .WillRepeatedly(Return());
278 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) 294 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_))
279 .WillRepeatedly(Return(false)); 295 .WillRepeatedly(Return(false));
280 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) 296 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _))
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 360
345 MockDelegate* mock_delegate() { 361 MockDelegate* mock_delegate() {
346 return &delegate_; 362 return &delegate_;
347 } 363 }
348 364
349 void OnDownloadFileAcquired(base::FilePath* return_path, 365 void OnDownloadFileAcquired(base::FilePath* return_path,
350 const base::FilePath& path) { 366 const base::FilePath& path) {
351 *return_path = path; 367 *return_path = path;
352 } 368 }
353 369
370 DownloadCreateInfo* create_info() { return create_info_.get(); }
371
372 BrowserContext* browser_context() { return &browser_context_; }
373
354 private: 374 private:
355 int next_download_id_ = DownloadItem::kInvalidId + 1;
356 StrictMock<MockDelegate> delegate_; 375 StrictMock<MockDelegate> delegate_;
357 std::set<DownloadItem*> allocated_downloads_; 376 std::set<DownloadItem*> allocated_downloads_;
377 scoped_ptr<DownloadCreateInfo> create_info_;
378 uint32_t next_download_id_ = DownloadItem::kInvalidId + 1;
379 TestBrowserContext browser_context_;
358 TestBrowserThreadBundle thread_bundle_; 380 TestBrowserThreadBundle thread_bundle_;
359 }; 381 };
360 382
361 // Tests to ensure calls that change a DownloadItem generate an update to 383 // Tests to ensure calls that change a DownloadItem generate an update to
362 // observers. 384 // observers.
363 // State changing functions not tested: 385 // State changing functions not tested:
364 // void OpenDownload(); 386 // void OpenDownload();
365 // void ShowDownloadInShell(); 387 // void ShowDownloadInShell();
366 // void CompleteDelayedDownload(); 388 // void CompleteDelayedDownload();
367 // set_* mutators 389 // set_* mutators
368 390
369 TEST_F(DownloadItemTest, NotificationAfterUpdate) { 391 TEST_F(DownloadItemTest, NotificationAfterUpdate) {
370 DownloadItemImpl* item = CreateDownloadItem(); 392 DownloadItemImpl* item = CreateDownloadItem();
371 MockDownloadFile* file = 393 MockDownloadFile* file =
372 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 394 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
373 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 395 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
374 TestDownloadItemObserver observer(item); 396 TestDownloadItemObserver observer(item);
375 397
376 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed, std::string()); 398 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed, std::string());
377 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 399 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
378 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); 400 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed());
379 CleanupItem(item, file, DownloadItem::IN_PROGRESS); 401 CleanupItem(item, file, DownloadItem::IN_PROGRESS);
380 } 402 }
381 403
382 TEST_F(DownloadItemTest, NotificationAfterCancel) { 404 TEST_F(DownloadItemTest, NotificationAfterCancel) {
383 DownloadItemImpl* user_cancel = CreateDownloadItem(); 405 DownloadItemImpl* user_cancel = CreateDownloadItem();
384 MockDownloadFile* download_file = CallDownloadItemStart(user_cancel, nullptr); 406 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
407 MockDownloadFile* download_file =
408 CallDownloadItemStart(user_cancel, &target_callback);
385 EXPECT_CALL(*download_file, Cancel()); 409 EXPECT_CALL(*download_file, Cancel());
410
386 TestDownloadItemObserver observer1(user_cancel); 411 TestDownloadItemObserver observer1(user_cancel);
387
388 user_cancel->Cancel(true); 412 user_cancel->Cancel(true);
389 ASSERT_TRUE(observer1.CheckAndResetDownloadUpdated()); 413 ASSERT_TRUE(observer1.CheckAndResetDownloadUpdated());
390 414
391 DownloadItemImpl* system_cancel = CreateDownloadItem(); 415 DownloadItemImpl* system_cancel = CreateDownloadItem();
392 download_file = CallDownloadItemStart(system_cancel, nullptr); 416 download_file = CallDownloadItemStart(system_cancel, &target_callback);
393 EXPECT_CALL(*download_file, Cancel()); 417 EXPECT_CALL(*download_file, Cancel());
418
394 TestDownloadItemObserver observer2(system_cancel); 419 TestDownloadItemObserver observer2(system_cancel);
395
396 system_cancel->Cancel(false); 420 system_cancel->Cancel(false);
397 ASSERT_TRUE(observer2.CheckAndResetDownloadUpdated()); 421 ASSERT_TRUE(observer2.CheckAndResetDownloadUpdated());
398 } 422 }
399 423
400 TEST_F(DownloadItemTest, NotificationAfterComplete) { 424 TEST_F(DownloadItemTest, NotificationAfterComplete) {
401 DownloadItemImpl* item = CreateDownloadItem(); 425 DownloadItemImpl* item = CreateDownloadItem();
402 TestDownloadItemObserver observer(item); 426 TestDownloadItemObserver observer(item);
403 MockDownloadFile* download_file = 427 MockDownloadFile* download_file =
404 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 428 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
405 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 429 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
(...skipping 25 matching lines...) Expand all
431 } 455 }
432 456
433 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { 457 TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
434 DownloadItemImpl* item = CreateDownloadItem(); 458 DownloadItemImpl* item = CreateDownloadItem();
435 TestDownloadItemObserver observer(item); 459 TestDownloadItemObserver observer(item);
436 460
437 DestroyDownloadItem(item); 461 DestroyDownloadItem(item);
438 ASSERT_TRUE(observer.download_destroyed()); 462 ASSERT_TRUE(observer.download_destroyed());
439 } 463 }
440 464
465 // Test that a download is resumed automatcially after a continuable interrupt.
441 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { 466 TEST_F(DownloadItemTest, ContinueAfterInterrupted) {
442 TestBrowserContext test_browser_context;
443 DownloadItemImpl* item = CreateDownloadItem(); 467 DownloadItemImpl* item = CreateDownloadItem();
444 TestDownloadItemObserver observer(item); 468 TestDownloadItemObserver observer(item);
445 MockDownloadFile* download_file = 469 MockDownloadFile* download_file =
446 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 470 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
447 471
448 // Interrupt the download, using a continuable interrupt. 472 // Interrupt the download, using a continuable interrupt.
449 EXPECT_CALL(*download_file, FullPath()) 473 EXPECT_CALL(*download_file, FullPath())
450 .WillOnce(Return(base::FilePath())); 474 .WillOnce(Return(base::FilePath()));
451 EXPECT_CALL(*download_file, Detach()); 475 EXPECT_CALL(*download_file, Detach());
452 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) 476 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
453 .WillRepeatedly(Return(&test_browser_context)); 477 .WillRepeatedly(Return(browser_context()));
454 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1); 478 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1);
455 item->DestinationObserverAsWeakPtr()->DestinationError( 479 item->DestinationObserverAsWeakPtr()->DestinationError(
456 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 480 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
457 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 481 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
458 // Since the download is resumed automatically, the interrupt count doesn't 482 // Since the download is resumed automatically, the interrupt count doesn't
459 // increase. 483 // increase.
460 ASSERT_EQ(0, observer.interrupt_count()); 484 ASSERT_EQ(0, observer.interrupt_count());
461 485
462 // Test expectations verify that ResumeInterruptedDownload() is called (by way 486 // Test expectations verify that ResumeInterruptedDownload() is called (by way
463 // of MockResumeInterruptedDownload) after the download is interrupted. But 487 // of MockResumeInterruptedDownload) after the download is interrupted. But
464 // the mock doesn't follow through with the resumption. 488 // the mock doesn't follow through with the resumption.
465 // ResumeInterruptedDownload() being called is sufficient for verifying that 489 // ResumeInterruptedDownload() being called is sufficient for verifying that
466 // the automatic resumption was triggered. 490 // the automatic resumption was triggered.
467 RunAllPendingInMessageLoops(); 491 RunAllPendingInMessageLoops();
468 492
469 // The download item is currently in RESUMING_INTERNAL state, which maps to 493 // The download item is currently in RESUMING_INTERNAL state, which maps to
470 // IN_PROGRESS. 494 // IN_PROGRESS.
471 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 495 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
472 } 496 }
473 497
474 // Same as above, but with a non-continuable interrupt. 498 // Test that automatic resumption doesn't happen after a non-continuable
499 // interrupt.
475 TEST_F(DownloadItemTest, RestartAfterInterrupted) { 500 TEST_F(DownloadItemTest, RestartAfterInterrupted) {
476 DownloadItemImpl* item = CreateDownloadItem(); 501 DownloadItemImpl* item = CreateDownloadItem();
477 TestDownloadItemObserver observer(item); 502 TestDownloadItemObserver observer(item);
478 MockDownloadFile* download_file = 503 MockDownloadFile* download_file =
479 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 504 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
480 505
481 // Interrupt the download, using a restartable interrupt. 506 // Interrupt the download, using a restartable interrupt.
482 EXPECT_CALL(*download_file, Cancel()); 507 EXPECT_CALL(*download_file, Cancel());
483 item->DestinationObserverAsWeakPtr()->DestinationError( 508 item->DestinationObserverAsWeakPtr()->DestinationError(
484 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 509 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
(...skipping 28 matching lines...) Expand all
513 538
514 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 539 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
515 // Should not try to auto-resume. 540 // Should not try to auto-resume.
516 ASSERT_EQ(1, observer.interrupt_count()); 541 ASSERT_EQ(1, observer.interrupt_count());
517 ASSERT_EQ(0, observer.resume_count()); 542 ASSERT_EQ(0, observer.resume_count());
518 543
519 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 544 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
520 } 545 }
521 546
522 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { 547 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) {
523 TestBrowserContext test_browser_context;
524 DownloadItemImpl* item = CreateDownloadItem(); 548 DownloadItemImpl* item = CreateDownloadItem();
525 base::WeakPtr<DownloadDestinationObserver> as_observer( 549 base::WeakPtr<DownloadDestinationObserver> as_observer(
526 item->DestinationObserverAsWeakPtr()); 550 item->DestinationObserverAsWeakPtr());
527 TestDownloadItemObserver observer(item); 551 TestDownloadItemObserver observer(item);
528 MockDownloadFile* mock_download_file(nullptr); 552 MockDownloadFile* mock_download_file(nullptr);
529 scoped_ptr<DownloadFile> download_file; 553 scoped_ptr<DownloadFile> download_file;
530 MockRequestHandle* mock_request_handle(nullptr); 554 MockRequestHandle* mock_request_handle(nullptr);
531 scoped_ptr<DownloadRequestHandleInterface> request_handle; 555 scoped_ptr<DownloadRequestHandleInterface> request_handle;
532 DownloadItemImplDelegate::DownloadTargetCallback callback; 556 DownloadItemImplDelegate::DownloadTargetCallback callback;
533 557
534 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 558 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
535 .WillRepeatedly(SaveArg<1>(&callback)); 559 .WillRepeatedly(SaveArg<1>(&callback));
536 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) 560 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
537 .WillRepeatedly(Return(&test_browser_context)); 561 .WillRepeatedly(Return(browser_context()));
538 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)) 562 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _))
539 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); 563 .Times(DownloadItemImpl::kMaxAutoResumeAttempts);
540 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { 564 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) {
541 DVLOG(20) << "Loop iteration " << i; 565 SCOPED_TRACE(::testing::Message() << "Iteration " << i);
542 566
543 mock_download_file = new NiceMock<MockDownloadFile>; 567 mock_download_file = new NiceMock<MockDownloadFile>;
544 download_file.reset(mock_download_file); 568 download_file.reset(mock_download_file);
545 mock_request_handle = new NiceMock<MockRequestHandle>; 569 mock_request_handle = new NiceMock<MockRequestHandle>;
546 request_handle.reset(mock_request_handle); 570 request_handle.reset(mock_request_handle);
547 571
548 ON_CALL(*mock_download_file, FullPath()) 572 ON_CALL(*mock_download_file, FullPath())
549 .WillByDefault(Return(base::FilePath())); 573 .WillByDefault(Return(base::FilePath()));
550 574
551 // Copied key parts of DoIntermediateRename & CallDownloadItemStart 575 // Copied key parts of DoIntermediateRename & CallDownloadItemStart
552 // to allow for holding onto the request handle. 576 // to allow for holding onto the request handle.
553 item->Start(std::move(download_file), std::move(request_handle)); 577 item->Start(std::move(download_file), std::move(request_handle),
578 *create_info());
554 RunAllPendingInMessageLoops(); 579 RunAllPendingInMessageLoops();
555 580
556 base::FilePath target_path(kDummyTargetPath); 581 base::FilePath target_path(kDummyTargetPath);
557 base::FilePath intermediate_path(kDummyIntermediatePath); 582 base::FilePath intermediate_path(kDummyIntermediatePath);
558 if (i == 0) { 583 if (i == 0) {
559 // RenameAndUniquify is only called the first time. In all the subsequent 584 // RenameAndUniquify is only called the first time. In all the subsequent
560 // iterations, the intermediate file already has the correct name, hence 585 // iterations, the intermediate file already has the correct name, hence
561 // no rename is necessary. 586 // no rename is necessary.
562 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _)) 587 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _))
563 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 588 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
564 intermediate_path)); 589 intermediate_path));
565 } 590 }
566 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 591 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
567 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 592 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
568 RunAllPendingInMessageLoops(); 593 RunAllPendingInMessageLoops();
569 594
570 // Use a continuable interrupt. 595 // Use a continuable interrupt.
571 item->DestinationObserverAsWeakPtr()->DestinationError( 596 item->DestinationObserverAsWeakPtr()->DestinationError(
572 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 597 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
573 598
599 RunAllPendingInMessageLoops();
574 ::testing::Mock::VerifyAndClearExpectations(mock_download_file); 600 ::testing::Mock::VerifyAndClearExpectations(mock_download_file);
575 } 601 }
576 602
577 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 603 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
578 EXPECT_EQ(1, observer.interrupt_count()); 604 EXPECT_EQ(1, observer.interrupt_count());
579 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 605 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
580 } 606 }
581 607
608 // If the download attempts to resume and the resumption request fails, the
609 // subsequent Start() call shouldn't update the origin state (URL redirect
610 // chains, Content-Disposition, download URL, etc..)
611 TEST_F(DownloadItemTest, FailedResumptionDoesntUpdateOriginState) {
612 const char kContentDisposition[] = "attachment; filename=foo";
613 const char kFirstETag[] = "ABC";
614 const char kFirstLastModified[] = "Yesterday";
615 const char kFirstURL[] = "http://www.example.com/download";
616 create_info()->content_disposition = kContentDisposition;
617 create_info()->etag = kFirstETag;
618 create_info()->last_modified = kFirstLastModified;
619 create_info()->url_chain.push_back(GURL(kFirstURL));
620
621 DownloadItemImpl* item = CreateDownloadItem();
622 MockDownloadFile* download_file =
623 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
624 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
625 EXPECT_EQ(kFirstETag, item->GetETag());
626 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
627 EXPECT_EQ(kFirstURL, item->GetURL().spec());
628
629 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _));
630 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
631 .WillRepeatedly(Return(browser_context()));
632 EXPECT_CALL(*download_file, Detach());
633 item->DestinationObserverAsWeakPtr()->DestinationError(
634 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
635 RunAllPendingInMessageLoops();
636 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
637
638 // Now change the create info. The changes should not cause the DownloadItem
639 // to be updated.
640 const char kSecondContentDisposition[] = "attachment; filename=bar";
641 const char kSecondETag[] = "123";
642 const char kSecondLastModified[] = "Today";
643 const char kSecondURL[] = "http://example.com/another-download";
644 create_info()->content_disposition = kSecondContentDisposition;
645 create_info()->etag = kSecondETag;
646 create_info()->last_modified = kSecondLastModified;
647 create_info()->url_chain.clear();
648 create_info()->url_chain.push_back(GURL(kSecondURL));
649 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
650
651 // The following ends up calling DownloadItem::Start(), but shouldn't result
652 // in an origin update.
653 download_file = CallDownloadItemStart(item, nullptr);
654
655 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
656 EXPECT_EQ(kFirstETag, item->GetETag());
657 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
658 EXPECT_EQ(kFirstURL, item->GetURL().spec());
659 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
660 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
661 }
662
582 // Test that resumption uses the final URL in a URL chain when resuming. 663 // Test that resumption uses the final URL in a URL chain when resuming.
583 TEST_F(DownloadItemTest, ResumeUsingFinalURL) { 664 TEST_F(DownloadItemTest, ResumeUsingFinalURL) {
584 TestBrowserContext test_browser_context; 665 create_info()->save_info->prompt_for_save_location = false;
585 scoped_ptr<DownloadCreateInfo> create_info(new DownloadCreateInfo); 666 create_info()->url_chain.clear();
586 create_info->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); 667 create_info()->url_chain.push_back(GURL("http://example.com/a"));
587 create_info->save_info->prompt_for_save_location = false; 668 create_info()->url_chain.push_back(GURL("http://example.com/b"));
588 create_info->etag = "SomethingToSatisfyResumption"; 669 create_info()->url_chain.push_back(GURL("http://example.com/c"));
589 create_info->url_chain.push_back(GURL("http://example.com/a"));
590 create_info->url_chain.push_back(GURL("http://example.com/b"));
591 create_info->url_chain.push_back(GURL("http://example.com/c"));
592 670
593 DownloadItemImpl* item = 671 DownloadItemImpl* item = CreateDownloadItem();
594 CreateDownloadItemWithCreateInfo(std::move(create_info));
595 TestDownloadItemObserver observer(item); 672 TestDownloadItemObserver observer(item);
596 MockDownloadFile* download_file = 673 MockDownloadFile* download_file =
597 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 674 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
598 675
599 // Interrupt the download, using a continuable interrupt. 676 // Interrupt the download, using a continuable interrupt.
600 EXPECT_CALL(*download_file, FullPath()).WillOnce(Return(base::FilePath())); 677 EXPECT_CALL(*download_file, FullPath()).WillOnce(Return(base::FilePath()));
601 EXPECT_CALL(*download_file, Detach()); 678 EXPECT_CALL(*download_file, Detach());
602 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) 679 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
603 .WillRepeatedly(Return(&test_browser_context)); 680 .WillRepeatedly(Return(browser_context()));
604 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload( 681 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(
605 Property(&DownloadUrlParameters::url, 682 Property(&DownloadUrlParameters::url,
606 GURL("http://example.com/c")), 683 GURL("http://example.com/c")),
607 _)) 684 _))
608 .Times(1); 685 .Times(1);
609 item->DestinationObserverAsWeakPtr()->DestinationError( 686 item->DestinationObserverAsWeakPtr()->DestinationError(
610 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 687 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
611 688
612 // Test expectations verify that ResumeInterruptedDownload() is called (by way 689 // Test expectations verify that ResumeInterruptedDownload() is called (by way
613 // of MockResumeInterruptedDownload) after the download is interrupted. But 690 // of MockResumeInterruptedDownload) after the download is interrupted. But
614 // the mock doesn't follow through with the resumption. 691 // the mock doesn't follow through with the resumption.
615 // ResumeInterruptedDownload() being called is sufficient for verifying that 692 // ResumeInterruptedDownload() being called is sufficient for verifying that
616 // the resumption was triggered. 693 // the resumption was triggered.
617 RunAllPendingInMessageLoops(); 694 RunAllPendingInMessageLoops();
618 695
619 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. 696 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS.
620 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 697 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
621 } 698 }
622 699
623 TEST_F(DownloadItemTest, NotificationAfterRemove) { 700 TEST_F(DownloadItemTest, NotificationAfterRemove) {
624 DownloadItemImpl* item = CreateDownloadItem(); 701 DownloadItemImpl* item = CreateDownloadItem();
625 MockDownloadFile* download_file = CallDownloadItemStart(item, nullptr); 702 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
703 MockDownloadFile* download_file =
704 CallDownloadItemStart(item, &target_callback);
626 EXPECT_CALL(*download_file, Cancel()); 705 EXPECT_CALL(*download_file, Cancel());
627 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 706 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
628 TestDownloadItemObserver observer(item); 707 TestDownloadItemObserver observer(item);
629 708
630 item->Remove(); 709 item->Remove();
631 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 710 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
632 ASSERT_TRUE(observer.download_removed()); 711 ASSERT_TRUE(observer.download_removed());
633 } 712 }
634 713
635 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { 714 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { 795 TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
717 DownloadItemImpl* item = CreateDownloadItem(); 796 DownloadItemImpl* item = CreateDownloadItem();
718 TestDownloadItemObserver observer(item); 797 TestDownloadItemObserver observer(item);
719 MockDownloadFile* mock_download_file(new MockDownloadFile); 798 MockDownloadFile* mock_download_file(new MockDownloadFile);
720 scoped_ptr<DownloadFile> download_file(mock_download_file); 799 scoped_ptr<DownloadFile> download_file(mock_download_file);
721 scoped_ptr<DownloadRequestHandleInterface> request_handle( 800 scoped_ptr<DownloadRequestHandleInterface> request_handle(
722 new NiceMock<MockRequestHandle>); 801 new NiceMock<MockRequestHandle>);
723 802
724 EXPECT_CALL(*mock_download_file, Initialize(_)); 803 EXPECT_CALL(*mock_download_file, Initialize(_));
725 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); 804 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _));
726 item->Start(std::move(download_file), std::move(request_handle)); 805 item->Start(std::move(download_file), std::move(request_handle),
806 *create_info());
727 807
728 item->Pause(); 808 item->Pause();
729 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 809 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
730 810
731 ASSERT_TRUE(item->IsPaused()); 811 ASSERT_TRUE(item->IsPaused());
732 812
733 item->Resume(); 813 item->Resume();
734 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 814 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
735 815
736 RunAllPendingInMessageLoops(); 816 RunAllPendingInMessageLoops();
(...skipping 26 matching lines...) Expand all
763 843
764 // Test to make sure that Start method calls DF initialize properly. 844 // Test to make sure that Start method calls DF initialize properly.
765 TEST_F(DownloadItemTest, Start) { 845 TEST_F(DownloadItemTest, Start) {
766 MockDownloadFile* mock_download_file(new MockDownloadFile); 846 MockDownloadFile* mock_download_file(new MockDownloadFile);
767 scoped_ptr<DownloadFile> download_file(mock_download_file); 847 scoped_ptr<DownloadFile> download_file(mock_download_file);
768 DownloadItemImpl* item = CreateDownloadItem(); 848 DownloadItemImpl* item = CreateDownloadItem();
769 EXPECT_CALL(*mock_download_file, Initialize(_)); 849 EXPECT_CALL(*mock_download_file, Initialize(_));
770 scoped_ptr<DownloadRequestHandleInterface> request_handle( 850 scoped_ptr<DownloadRequestHandleInterface> request_handle(
771 new NiceMock<MockRequestHandle>); 851 new NiceMock<MockRequestHandle>);
772 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); 852 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
773 item->Start(std::move(download_file), std::move(request_handle)); 853 item->Start(std::move(download_file), std::move(request_handle),
854 *create_info());
774 RunAllPendingInMessageLoops(); 855 RunAllPendingInMessageLoops();
775 856
776 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); 857 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
777 } 858 }
778 859
779 // Download file and the request should be cancelled as a result of download 860 // Download file and the request should be cancelled as a result of download
780 // file initialization failing. 861 // file initialization failing.
781 TEST_F(DownloadItemTest, InitDownloadFileFails) { 862 TEST_F(DownloadItemTest, InitDownloadFileFails) {
863 DownloadItemImpl* item = CreateDownloadItem();
782 scoped_ptr<MockDownloadFile> file(new MockDownloadFile()); 864 scoped_ptr<MockDownloadFile> file(new MockDownloadFile());
783 scoped_ptr<MockRequestHandle> request_handle(new MockRequestHandle()); 865 scoped_ptr<MockRequestHandle> request_handle(new MockRequestHandle());
784 EXPECT_CALL(*file, Cancel()); 866 EXPECT_CALL(*file, Cancel());
785 EXPECT_CALL(*request_handle, CancelRequest()); 867 EXPECT_CALL(*request_handle, CancelRequest());
786 EXPECT_CALL(*file, Initialize(_)) 868 EXPECT_CALL(*file, Initialize(_))
787 .WillOnce(ScheduleCallbackWithParam( 869 .WillOnce(ScheduleCallbackWithParam(
788 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED)); 870 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED));
789 871
790 DownloadItemImpl* item = CreateDownloadItem(); 872 base::RunLoop start_download_loop;
791 item->Start(std::move(file), std::move(request_handle)); 873 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback;
874 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
875 .WillOnce(DoAll(SaveArg<1>(&download_target_callback),
876 ScheduleClosure(start_download_loop.QuitClosure())));
877
878 item->Start(std::move(file), std::move(request_handle), *create_info());
879 start_download_loop.Run();
880
881 download_target_callback.Run(base::FilePath(kDummyTargetPath),
882 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
883 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
884 base::FilePath(kDummyIntermediatePath));
792 RunAllPendingInMessageLoops(); 885 RunAllPendingInMessageLoops();
793 886
794 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 887 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
795 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, 888 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED,
796 item->GetLastReason()); 889 item->GetLastReason());
797 } 890 }
798 891
892 // Handling of downloads initiated via a failed request. In this case, Start()
893 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason.
894 TEST_F(DownloadItemTest, StartFailedDownload) {
895 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
896 DownloadItemImpl* item = CreateDownloadItem();
897
898 // DownloadFile and DownloadRequestHandleInterface objects aren't created for
899 // failed downloads.
900 scoped_ptr<DownloadFile> null_download_file;
901 scoped_ptr<DownloadRequestHandleInterface> null_request_handle;
902 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback;
903 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
904 .WillOnce(SaveArg<1>(&download_target_callback));
905 item->Start(std::move(null_download_file), std::move(null_request_handle),
906 *create_info());
907 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
908 RunAllPendingInMessageLoops();
909
910 // The DownloadItemImpl should attempt to determine a target path even if the
911 // download was interrupted.
912 ASSERT_FALSE(download_target_callback.is_null());
913 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
914 base::FilePath target_path(FILE_PATH_LITERAL("foo"));
915 download_target_callback.Run(target_path,
916 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
917 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path);
918 RunAllPendingInMessageLoops();
919
920 EXPECT_EQ(target_path, item->GetTargetFilePath());
921 CleanupItem(item, NULL, DownloadItem::INTERRUPTED);
922 }
923
799 // Test that the delegate is invoked after the download file is renamed. 924 // Test that the delegate is invoked after the download file is renamed.
800 TEST_F(DownloadItemTest, CallbackAfterRename) { 925 TEST_F(DownloadItemTest, CallbackAfterRename) {
801 DownloadItemImpl* item = CreateDownloadItem(); 926 DownloadItemImpl* item = CreateDownloadItem();
802 DownloadItemImplDelegate::DownloadTargetCallback callback; 927 DownloadItemImplDelegate::DownloadTargetCallback callback;
803 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 928 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
804 base::FilePath final_path( 929 base::FilePath final_path(
805 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 930 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
806 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 931 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
807 base::FilePath new_intermediate_path( 932 base::FilePath new_intermediate_path(
808 final_path.InsertBeforeExtensionASCII("y")); 933 final_path.InsertBeforeExtensionASCII("y"));
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 ::testing::Mock::VerifyAndClearExpectations(download_file); 1096 ::testing::Mock::VerifyAndClearExpectations(download_file);
972 mock_delegate()->VerifyAndClearExpectations(); 1097 mock_delegate()->VerifyAndClearExpectations();
973 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1098 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
974 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); 1099 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
975 EXPECT_TRUE(item->GetFullPath().empty()); 1100 EXPECT_TRUE(item->GetFullPath().empty());
976 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1101 EXPECT_EQ(final_path, item->GetTargetFilePath());
977 } 1102 }
978 1103
979 TEST_F(DownloadItemTest, Canceled) { 1104 TEST_F(DownloadItemTest, Canceled) {
980 DownloadItemImpl* item = CreateDownloadItem(); 1105 DownloadItemImpl* item = CreateDownloadItem();
981 MockDownloadFile* download_file = CallDownloadItemStart(item, nullptr); 1106 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
1107 MockDownloadFile* download_file =
1108 CallDownloadItemStart(item, &target_callback);
982 1109
983 // Confirm cancel sets state properly. 1110 // Confirm cancel sets state properly.
984 EXPECT_CALL(*download_file, Cancel()); 1111 EXPECT_CALL(*download_file, Cancel());
985 item->Cancel(true); 1112 item->Cancel(true);
986 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); 1113 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
987 } 1114 }
988 1115
989 TEST_F(DownloadItemTest, FileRemoved) { 1116 TEST_F(DownloadItemTest, FileRemoved) {
990 DownloadItemImpl* item = CreateDownloadItem(); 1117 DownloadItemImpl* item = CreateDownloadItem();
991 1118
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1044 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 1171 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
1045 mock_delegate()->VerifyAndClearExpectations(); 1172 mock_delegate()->VerifyAndClearExpectations();
1046 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1173 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1047 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1174 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1048 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, 1175 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED,
1049 item->GetLastReason()); 1176 item->GetLastReason());
1050 } 1177 }
1051 1178
1052 TEST_F(DownloadItemTest, DestinationCompleted) { 1179 TEST_F(DownloadItemTest, DestinationCompleted) {
1053 DownloadItemImpl* item = CreateDownloadItem(); 1180 DownloadItemImpl* item = CreateDownloadItem();
1054 MockDownloadFile* download_file = CallDownloadItemStart(item, nullptr); 1181 MockDownloadFile* download_file =
1182 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1055 base::WeakPtr<DownloadDestinationObserver> as_observer( 1183 base::WeakPtr<DownloadDestinationObserver> as_observer(
1056 item->DestinationObserverAsWeakPtr()); 1184 item->DestinationObserverAsWeakPtr());
1057 TestDownloadItemObserver observer(item); 1185 TestDownloadItemObserver observer(item);
1058 1186
1059 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1187 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1060 EXPECT_EQ("", item->GetHash()); 1188 EXPECT_EQ("", item->GetHash());
1061 EXPECT_EQ("", item->GetHashState()); 1189 EXPECT_EQ("", item->GetHashState());
1062 EXPECT_FALSE(item->AllDataSaved()); 1190 EXPECT_FALSE(item->AllDataSaved());
1063 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 1191 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
1064 1192
1065 as_observer->DestinationUpdate(10, 20, "deadbeef"); 1193 as_observer->DestinationUpdate(10, 20, "deadbeef");
1066 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1194 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1067 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset. 1195 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset.
1068 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1196 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1069 EXPECT_EQ("", item->GetHash()); 1197 EXPECT_EQ("", item->GetHash());
1070 EXPECT_EQ("deadbeef", item->GetHashState()); 1198 EXPECT_EQ("deadbeef", item->GetHashState());
1071 EXPECT_FALSE(item->AllDataSaved()); 1199 EXPECT_FALSE(item->AllDataSaved());
1072 1200
1201 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _));
1073 as_observer->DestinationCompleted("livebeef"); 1202 as_observer->DestinationCompleted("livebeef");
1074 mock_delegate()->VerifyAndClearExpectations(); 1203 mock_delegate()->VerifyAndClearExpectations();
1075 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1204 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1076 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1205 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1077 EXPECT_EQ("livebeef", item->GetHash()); 1206 EXPECT_EQ("livebeef", item->GetHash());
1078 EXPECT_EQ("", item->GetHashState()); 1207 EXPECT_EQ("", item->GetHashState());
1079 EXPECT_TRUE(item->AllDataSaved()); 1208 EXPECT_TRUE(item->AllDataSaved());
1080 1209
1081 // Even though the DownloadItem receives a DestinationCompleted() event, 1210 // Even though the DownloadItem receives a DestinationCompleted() event,
1082 // target determination hasn't completed, hence the download item is stuck in 1211 // target determination hasn't completed, hence the download item is stuck in
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after
1640 // Expect that the download file and the request will be cancelled as a 1769 // Expect that the download file and the request will be cancelled as a
1641 // result. 1770 // result.
1642 EXPECT_CALL(*file_, Cancel()); 1771 EXPECT_CALL(*file_, Cancel());
1643 EXPECT_CALL(*request_handle_, CancelRequest()); 1772 EXPECT_CALL(*request_handle_, CancelRequest());
1644 1773
1645 base::RunLoop download_start_loop; 1774 base::RunLoop download_start_loop;
1646 DownloadFile::InitializeCallback initialize_callback; 1775 DownloadFile::InitializeCallback initialize_callback;
1647 EXPECT_CALL(*file_, Initialize(_)) 1776 EXPECT_CALL(*file_, Initialize(_))
1648 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 1777 .WillOnce(DoAll(SaveArg<0>(&initialize_callback),
1649 ScheduleClosure(download_start_loop.QuitClosure()))); 1778 ScheduleClosure(download_start_loop.QuitClosure())));
1650 item_->Start(std::move(file_), std::move(request_handle_)); 1779 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
1651 download_start_loop.Run(); 1780 download_start_loop.Run();
1652 1781
1653 base::WeakPtr<DownloadDestinationObserver> destination_observer = 1782 base::WeakPtr<DownloadDestinationObserver> destination_observer =
1654 item_->DestinationObserverAsWeakPtr(); 1783 item_->DestinationObserverAsWeakPtr();
1655 1784
1656 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 1785 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
1657 RunAllPendingInMessageLoops(); 1786 RunAllPendingInMessageLoops();
1658 1787
1659 base::RunLoop initialize_completion_loop; 1788 base::RunLoop initialize_completion_loop;
1660 DownloadItemImplDelegate::DownloadTargetCallback target_callback; 1789 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1693 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) 1822 EXPECT_CALL(*file_, RenameAndUniquify(_, _))
1694 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), 1823 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback),
1695 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); 1824 ScheduleClosure(intermediate_rename_loop.QuitClosure())));
1696 1825
1697 base::RunLoop download_start_loop; 1826 base::RunLoop download_start_loop;
1698 DownloadFile::InitializeCallback initialize_callback; 1827 DownloadFile::InitializeCallback initialize_callback;
1699 EXPECT_CALL(*file_, Initialize(_)) 1828 EXPECT_CALL(*file_, Initialize(_))
1700 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 1829 .WillOnce(DoAll(SaveArg<0>(&initialize_callback),
1701 ScheduleClosure(download_start_loop.QuitClosure()))); 1830 ScheduleClosure(download_start_loop.QuitClosure())));
1702 1831
1703 item_->Start(std::move(file_), std::move(request_handle_)); 1832 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
1704 download_start_loop.Run(); 1833 download_start_loop.Run();
1705 base::WeakPtr<DownloadDestinationObserver> destination_observer = 1834 base::WeakPtr<DownloadDestinationObserver> destination_observer =
1706 item_->DestinationObserverAsWeakPtr(); 1835 item_->DestinationObserverAsWeakPtr();
1707 1836
1708 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 1837 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
1709 RunAllPendingInMessageLoops(); 1838 RunAllPendingInMessageLoops();
1710 1839
1711 base::RunLoop initialize_completion_loop; 1840 base::RunLoop initialize_completion_loop;
1712 DownloadItemImplDelegate::DownloadTargetCallback target_callback; 1841 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
1713 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) 1842 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _))
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1762 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) 1891 EXPECT_CALL(*file_, RenameAndUniquify(_, _))
1763 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), 1892 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback),
1764 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); 1893 ScheduleClosure(intermediate_rename_loop.QuitClosure())));
1765 1894
1766 base::RunLoop download_start_loop; 1895 base::RunLoop download_start_loop;
1767 DownloadFile::InitializeCallback initialize_callback; 1896 DownloadFile::InitializeCallback initialize_callback;
1768 EXPECT_CALL(*file_, Initialize(_)) 1897 EXPECT_CALL(*file_, Initialize(_))
1769 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 1898 .WillOnce(DoAll(SaveArg<0>(&initialize_callback),
1770 ScheduleClosure(download_start_loop.QuitClosure()))); 1899 ScheduleClosure(download_start_loop.QuitClosure())));
1771 1900
1772 item_->Start(std::move(file_), std::move(request_handle_)); 1901 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
1773 download_start_loop.Run(); 1902 download_start_loop.Run();
1774 base::WeakPtr<DownloadDestinationObserver> destination_observer = 1903 base::WeakPtr<DownloadDestinationObserver> destination_observer =
1775 item_->DestinationObserverAsWeakPtr(); 1904 item_->DestinationObserverAsWeakPtr();
1776 1905
1777 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 1906 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
1778 RunAllPendingInMessageLoops(); 1907 RunAllPendingInMessageLoops();
1779 1908
1780 base::RunLoop initialize_completion_loop; 1909 base::RunLoop initialize_completion_loop;
1781 DownloadItemImplDelegate::DownloadTargetCallback target_callback; 1910 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
1782 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) 1911 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _))
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1823 1952
1824 item_->Cancel(true); 1953 item_->Cancel(true);
1825 RunAllPendingInMessageLoops(); 1954 RunAllPendingInMessageLoops();
1826 } 1955 }
1827 1956
1828 TEST(MockDownloadItem, Compiles) { 1957 TEST(MockDownloadItem, Compiles) {
1829 MockDownloadItem mock_item; 1958 MockDownloadItem mock_item;
1830 } 1959 }
1831 1960
1832 } // namespace content 1961 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl.cc ('k') | content/browser/download/download_manager_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698