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

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

Issue 2890853002: Downloads: replace BrowserThread::FILE with task scheduler. (Closed)
Patch Set: Revert ScopedCOMInitializer invocations. Created 3 years, 5 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 "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 <deque> 9 #include <deque>
10 #include <iterator> 10 #include <iterator>
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <queue> 13 #include <queue>
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/callback.h" 17 #include "base/callback.h"
18 #include "base/callback_helpers.h" 18 #include "base/callback_helpers.h"
19 #include "base/feature_list.h" 19 #include "base/feature_list.h"
20 #include "base/files/file_util.h" 20 #include "base/files/file_util.h"
21 #include "base/memory/ptr_util.h" 21 #include "base/memory/ptr_util.h"
22 #include "base/message_loop/message_loop.h" 22 #include "base/test/scoped_task_environment.h"
23 #include "base/run_loop.h"
24 #include "base/threading/thread.h" 23 #include "base/threading/thread.h"
25 #include "content/browser/byte_stream.h" 24 #include "content/browser/byte_stream.h"
26 #include "content/browser/download/download_create_info.h" 25 #include "content/browser/download/download_create_info.h"
27 #include "content/browser/download/download_destination_observer.h" 26 #include "content/browser/download/download_destination_observer.h"
28 #include "content/browser/download/download_file_factory.h" 27 #include "content/browser/download/download_file_factory.h"
29 #include "content/browser/download/download_item_impl_delegate.h" 28 #include "content/browser/download/download_item_impl_delegate.h"
30 #include "content/browser/download/download_request_handle.h" 29 #include "content/browser/download/download_request_handle.h"
31 #include "content/browser/download/mock_download_file.h" 30 #include "content/browser/download/mock_download_file.h"
32 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/download_interrupt_reasons.h" 32 #include "content/public/browser/download_interrupt_reasons.h"
34 #include "content/public/browser/download_url_parameters.h" 33 #include "content/public/browser/download_url_parameters.h"
35 #include "content/public/common/content_features.h" 34 #include "content/public/common/content_features.h"
36 #include "content/public/test/mock_download_item.h" 35 #include "content/public/test/mock_download_item.h"
37 #include "content/public/test/test_browser_context.h" 36 #include "content/public/test/test_browser_context.h"
38 #include "content/public/test/test_browser_thread_bundle.h" 37 #include "content/public/test/test_browser_thread_bundle.h"
38 #include "content/public/test/test_utils.h"
39 #include "content/public/test/web_contents_tester.h" 39 #include "content/public/test/web_contents_tester.h"
40 #include "crypto/secure_hash.h" 40 #include "crypto/secure_hash.h"
41 #include "net/http/http_response_headers.h" 41 #include "net/http/http_response_headers.h"
42 #include "testing/gmock/include/gmock/gmock.h" 42 #include "testing/gmock/include/gmock/gmock.h"
43 #include "testing/gtest/include/gtest/gtest.h" 43 #include "testing/gtest/include/gtest/gtest.h"
44 44
45 using ::testing::DoAll; 45 using ::testing::DoAll;
46 using ::testing::Invoke; 46 using ::testing::Invoke;
47 using ::testing::InvokeWithoutArgs; 47 using ::testing::InvokeWithoutArgs;
48 using ::testing::NiceMock; 48 using ::testing::NiceMock;
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 const uint8_t kHashOfTestData1[] = { 251 const uint8_t kHashOfTestData1[] = {
252 0xd2, 0xfc, 0x16, 0xa1, 0xf5, 0x1a, 0x65, 0x3a, 0xa0, 0x19, 0x64, 252 0xd2, 0xfc, 0x16, 0xa1, 0xf5, 0x1a, 0x65, 0x3a, 0xa0, 0x19, 0x64,
253 0xef, 0x9c, 0x92, 0x33, 0x36, 0xe1, 0x06, 0x53, 0xfe, 0xc1, 0x95, 253 0xef, 0x9c, 0x92, 0x33, 0x36, 0xe1, 0x06, 0x53, 0xfe, 0xc1, 0x95,
254 0xf4, 0x93, 0x45, 0x8b, 0x3b, 0x21, 0x89, 0x0e, 0x1b, 0x97}; 254 0xf4, 0x93, 0x45, 0x8b, 0x3b, 0x21, 0x89, 0x0e, 0x1b, 0x97};
255 255
256 } // namespace 256 } // namespace
257 257
258 class DownloadItemTest : public testing::Test { 258 class DownloadItemTest : public testing::Test {
259 public: 259 public:
260 DownloadItemTest() 260 DownloadItemTest()
261 : delegate_(), next_download_id_(DownloadItem::kInvalidId + 1) { 261 : task_environment_(
262 base::test::ScopedTaskEnvironment::MainThreadType::UI,
263 base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED),
264 next_download_id_(DownloadItem::kInvalidId + 1) {
262 create_info_.reset(new DownloadCreateInfo()); 265 create_info_.reset(new DownloadCreateInfo());
263 create_info_->save_info = 266 create_info_->save_info =
264 std::unique_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); 267 std::unique_ptr<DownloadSaveInfo>(new DownloadSaveInfo());
265 create_info_->save_info->prompt_for_save_location = false; 268 create_info_->save_info->prompt_for_save_location = false;
266 create_info_->url_chain.push_back(GURL("http://example.com/download")); 269 create_info_->url_chain.push_back(GURL("http://example.com/download"));
267 create_info_->etag = "SomethingToSatisfyResumption"; 270 create_info_->etag = "SomethingToSatisfyResumption";
268 } 271 }
269 272
270 ~DownloadItemTest() {
271 RunAllPendingInMessageLoops();
272 }
273
274 DownloadItemImpl* CreateDownloadItemWithCreateInfo( 273 DownloadItemImpl* CreateDownloadItemWithCreateInfo(
275 std::unique_ptr<DownloadCreateInfo> info) { 274 std::unique_ptr<DownloadCreateInfo> info) {
276 DownloadItemImpl* download = 275 DownloadItemImpl* download =
277 new DownloadItemImpl(&delegate_, next_download_id_++, *(info.get()), 276 new DownloadItemImpl(mock_delegate(), next_download_id_++,
278 net::NetLogWithSource()); 277 *(info.get()), net::NetLogWithSource());
279 allocated_downloads_[download] = base::WrapUnique(download); 278 allocated_downloads_[download] = base::WrapUnique(download);
280 return download; 279 return download;
281 } 280 }
282 281
283 // Creates a new net::HttpResponseHeaders object for the |response_code|. 282 // Creates a new net::HttpResponseHeaders object for the |response_code|.
284 scoped_refptr<const net::HttpResponseHeaders> CreateResponseHeaders( 283 scoped_refptr<const net::HttpResponseHeaders> CreateResponseHeaders(
285 int response_code) { 284 int response_code) {
286 return make_scoped_refptr(new net::HttpResponseHeaders( 285 return make_scoped_refptr(new net::HttpResponseHeaders(
287 "HTTP/1.1 " + std::to_string(response_code))); 286 "HTTP/1.1 " + std::to_string(response_code)));
288 } 287 }
289 288
290 // This class keeps ownership of the created download item; it will 289 // This class keeps ownership of the created download item; it will
291 // be torn down at the end of the test unless DestroyDownloadItem is 290 // be torn down at the end of the test unless DestroyDownloadItem is
292 // called. 291 // called.
293 DownloadItemImpl* CreateDownloadItem() { 292 DownloadItemImpl* CreateDownloadItem() {
294 create_info_->download_id = ++next_download_id_; 293 create_info_->download_id = ++next_download_id_;
295 DownloadItemImpl* download = 294 DownloadItemImpl* download =
296 new DownloadItemImpl(&delegate_, create_info_->download_id, 295 new DownloadItemImpl(mock_delegate(), create_info_->download_id,
297 *create_info_, net::NetLogWithSource()); 296 *create_info_, net::NetLogWithSource());
298 allocated_downloads_[download] = base::WrapUnique(download); 297 allocated_downloads_[download] = base::WrapUnique(download);
299 return download; 298 return download;
300 } 299 }
301 300
302 // Add DownloadFile to DownloadItem. 301 // Add DownloadFile to DownloadItem.
303 MockDownloadFile* CallDownloadItemStart(DownloadItemImpl* item, 302 MockDownloadFile* CallDownloadItemStart(DownloadItemImpl* item,
304 DownloadTargetCallback* callback) { 303 DownloadTargetCallback* callback) {
305 MockDownloadFile* mock_download_file = nullptr; 304 MockDownloadFile* mock_download_file = nullptr;
306 std::unique_ptr<DownloadFile> download_file; 305 std::unique_ptr<DownloadFile> download_file;
307 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 306 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
308 .WillOnce(SaveArg<1>(callback)); 307 .WillOnce(SaveArg<1>(callback));
309 308
310 // Only create a DownloadFile if the request was successful. 309 // Only create a DownloadFile if the request was successful.
311 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) { 310 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
312 mock_download_file = new StrictMock<MockDownloadFile>; 311 mock_download_file = new StrictMock<MockDownloadFile>;
313 download_file.reset(mock_download_file); 312 download_file.reset(mock_download_file);
314 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)) 313 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _))
315 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); 314 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE));
316 EXPECT_CALL(*mock_download_file, FullPath()) 315 EXPECT_CALL(*mock_download_file, FullPath())
317 .WillRepeatedly(ReturnRefOfCopy(base::FilePath())); 316 .WillRepeatedly(ReturnRefOfCopy(base::FilePath()));
318 } 317 }
319 318
320 std::unique_ptr<MockRequestHandle> request_handle = 319 std::unique_ptr<MockRequestHandle> request_handle =
321 base::MakeUnique<NiceMock<MockRequestHandle>>(); 320 base::MakeUnique<NiceMock<MockRequestHandle>>();
322 item->Start(std::move(download_file), std::move(request_handle), 321 item->Start(std::move(download_file), std::move(request_handle),
323 *create_info_); 322 *create_info_);
324 RunAllPendingInMessageLoops(); 323 task_environment_.RunUntilIdle();
325 324
326 // So that we don't have a function writing to a stack variable 325 // So that we don't have a function writing to a stack variable
327 // lying around if the above failed. 326 // lying around if the above failed.
328 mock_delegate()->VerifyAndClearExpectations(); 327 mock_delegate()->VerifyAndClearExpectations();
329 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) 328 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_))
330 .WillRepeatedly(Return()); 329 .WillRepeatedly(Return());
331 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) 330 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_))
332 .WillRepeatedly(Return(false)); 331 .WillRepeatedly(Return(false));
333 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) 332 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _))
334 .WillRepeatedly(Return(true)); 333 .WillRepeatedly(Return(true));
(...skipping 11 matching lines...) Expand all
346 DownloadTargetCallback callback; 345 DownloadTargetCallback callback;
347 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 346 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
348 base::FilePath target_path(kDummyTargetPath); 347 base::FilePath target_path(kDummyTargetPath);
349 base::FilePath intermediate_path(kDummyIntermediatePath); 348 base::FilePath intermediate_path(kDummyIntermediatePath);
350 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 349 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
351 .WillOnce(ScheduleRenameAndUniquifyCallback( 350 .WillOnce(ScheduleRenameAndUniquifyCallback(
352 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); 351 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
353 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 352 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
354 danger_type, intermediate_path, 353 danger_type, intermediate_path,
355 DOWNLOAD_INTERRUPT_REASON_NONE); 354 DOWNLOAD_INTERRUPT_REASON_NONE);
356 RunAllPendingInMessageLoops(); 355 task_environment_.RunUntilIdle();
357 return download_file; 356 return download_file;
358 } 357 }
359 358
360 void DoDestinationComplete(DownloadItemImpl* item, 359 void DoDestinationComplete(DownloadItemImpl* item,
361 MockDownloadFile* download_file) { 360 MockDownloadFile* download_file) {
362 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) 361 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _))
363 .WillOnce(Return(true)); 362 .WillOnce(Return(true));
364 base::FilePath final_path(kDummyTargetPath); 363 base::FilePath final_path(kDummyTargetPath);
365 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) 364 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _))
366 .WillOnce(ScheduleRenameAndAnnotateCallback( 365 .WillOnce(ScheduleRenameAndAnnotateCallback(
367 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); 366 DOWNLOAD_INTERRUPT_REASON_NONE, final_path));
368 EXPECT_CALL(*download_file, FullPath()) 367 EXPECT_CALL(*download_file, FullPath())
369 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyTargetPath))); 368 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyTargetPath)));
370 EXPECT_CALL(*download_file, Detach()); 369 EXPECT_CALL(*download_file, Detach());
371 370
372 item->DestinationObserverAsWeakPtr()->DestinationCompleted( 371 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
373 0, std::unique_ptr<crypto::SecureHash>()); 372 0, std::unique_ptr<crypto::SecureHash>());
374 RunAllPendingInMessageLoops(); 373 task_environment_.RunUntilIdle();
375 } 374 }
376 375
377 // Cleanup a download item (specifically get rid of the DownloadFile on it). 376 // Cleanup a download item (specifically get rid of the DownloadFile on it).
378 // The item must be in the expected state. 377 // The item must be in the expected state.
379 void CleanupItem(DownloadItemImpl* item, 378 void CleanupItem(DownloadItemImpl* item,
380 MockDownloadFile* download_file, 379 MockDownloadFile* download_file,
381 DownloadItem::DownloadState expected_state) { 380 DownloadItem::DownloadState expected_state) {
382 EXPECT_EQ(expected_state, item->GetState()); 381 EXPECT_EQ(expected_state, item->GetState());
383 382
384 if (expected_state == DownloadItem::IN_PROGRESS) { 383 if (expected_state == DownloadItem::IN_PROGRESS) {
385 if (download_file) 384 if (download_file)
386 EXPECT_CALL(*download_file, Cancel()); 385 EXPECT_CALL(*download_file, Cancel());
387 item->Cancel(true); 386 item->Cancel(true);
388 RunAllPendingInMessageLoops(); 387 task_environment_.RunUntilIdle();
389 } 388 }
390 } 389 }
391 390
392 // Destroy a previously created download item. 391 // Destroy a previously created download item.
393 void DestroyDownloadItem(DownloadItem* item) { 392 void DestroyDownloadItem(DownloadItem* item) {
394 allocated_downloads_.erase(item); 393 allocated_downloads_.erase(item);
395 } 394 }
396 395
397 void RunAllPendingInMessageLoops() { base::RunLoop().RunUntilIdle(); } 396 MockDelegate* mock_delegate() { return &mock_delegate_; }
398
399 MockDelegate* mock_delegate() {
400 return &delegate_;
401 }
402 397
403 void OnDownloadFileAcquired(base::FilePath* return_path, 398 void OnDownloadFileAcquired(base::FilePath* return_path,
404 const base::FilePath& path) { 399 const base::FilePath& path) {
405 *return_path = path; 400 *return_path = path;
406 } 401 }
407 402
408 DownloadCreateInfo* create_info() { return create_info_.get(); } 403 DownloadCreateInfo* create_info() { return create_info_.get(); }
409 404
410 BrowserContext* browser_context() { return &browser_context_; } 405 BrowserContext* browser_context() { return &browser_context_; }
411 406
407 base::test::ScopedTaskEnvironment task_environment_;
408
412 private: 409 private:
413 TestBrowserThreadBundle thread_bundle_; 410 TestBrowserThreadBundle thread_bundle_;
414 StrictMock<MockDelegate> delegate_; 411 StrictMock<MockDelegate> mock_delegate_;
415 std::map<DownloadItem*, std::unique_ptr<DownloadItem>> allocated_downloads_; 412 std::map<DownloadItem*, std::unique_ptr<DownloadItem>> allocated_downloads_;
416 std::unique_ptr<DownloadCreateInfo> create_info_; 413 std::unique_ptr<DownloadCreateInfo> create_info_;
417 uint32_t next_download_id_ = DownloadItem::kInvalidId + 1; 414 uint32_t next_download_id_ = DownloadItem::kInvalidId + 1;
418 TestBrowserContext browser_context_; 415 TestBrowserContext browser_context_;
419 }; 416 };
420 417
421 // Tests to ensure calls that change a DownloadItem generate an update to 418 // Tests to ensure calls that change a DownloadItem generate an update to
422 // observers. 419 // observers.
423 // State changing functions not tested: 420 // State changing functions not tested:
424 // void OpenDownload(); 421 // void OpenDownload();
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 581 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
585 .WillOnce(ScheduleRenameAndUniquifyCallback( 582 .WillOnce(ScheduleRenameAndUniquifyCallback(
586 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 583 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
587 584
588 // Currently, a notification would be generated if the danger type is anything 585 // Currently, a notification would be generated if the danger type is anything
589 // other than NOT_DANGEROUS. 586 // other than NOT_DANGEROUS.
590 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 587 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
591 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 588 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
592 DOWNLOAD_INTERRUPT_REASON_NONE); 589 DOWNLOAD_INTERRUPT_REASON_NONE);
593 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 590 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
594 RunAllPendingInMessageLoops(); 591 task_environment_.RunUntilIdle();
595 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 592 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
596 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 593 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
597 594
598 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 595 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
599 } 596 }
600 597
601 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { 598 TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
602 DownloadItemImpl* item = CreateDownloadItem(); 599 DownloadItemImpl* item = CreateDownloadItem();
603 TestDownloadItemObserver observer(item); 600 TestDownloadItemObserver observer(item);
604 MockDownloadFile* mock_download_file(new MockDownloadFile); 601 MockDownloadFile* mock_download_file(new MockDownloadFile);
605 std::unique_ptr<DownloadFile> download_file(mock_download_file); 602 std::unique_ptr<DownloadFile> download_file(mock_download_file);
606 std::unique_ptr<DownloadRequestHandleInterface> request_handle( 603 std::unique_ptr<DownloadRequestHandleInterface> request_handle(
607 new NiceMock<MockRequestHandle>); 604 new NiceMock<MockRequestHandle>);
608 605
609 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); 606 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _));
610 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); 607 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _));
611 item->Start(std::move(download_file), std::move(request_handle), 608 item->Start(std::move(download_file), std::move(request_handle),
612 *create_info()); 609 *create_info());
613 610
614 item->Pause(); 611 item->Pause();
615 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 612 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
616 613
617 ASSERT_TRUE(item->IsPaused()); 614 ASSERT_TRUE(item->IsPaused());
618 615
619 item->Resume(); 616 item->Resume();
620 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 617 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
621 618
622 RunAllPendingInMessageLoops(); 619 task_environment_.RunUntilIdle();
623 620
624 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); 621 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
625 } 622 }
626 623
627 // Test that a download is resumed automatically after a continuable interrupt. 624 // Test that a download is resumed automatically after a continuable interrupt.
628 TEST_F(DownloadItemTest, AutomaticResumption_Continue) { 625 TEST_F(DownloadItemTest, AutomaticResumption_Continue) {
629 DownloadItemImpl* item = CreateDownloadItem(); 626 DownloadItemImpl* item = CreateDownloadItem();
630 TestDownloadItemObserver observer(item); 627 TestDownloadItemObserver observer(item);
631 MockDownloadFile* download_file = 628 MockDownloadFile* download_file =
632 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 629 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
(...skipping 22 matching lines...) Expand all
655 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 652 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
656 // Since the download is resumed automatically, the interrupt count doesn't 653 // Since the download is resumed automatically, the interrupt count doesn't
657 // increase. 654 // increase.
658 ASSERT_EQ(0, observer.interrupt_count()); 655 ASSERT_EQ(0, observer.interrupt_count());
659 656
660 // Test expectations verify that ResumeInterruptedDownload() is called (by way 657 // Test expectations verify that ResumeInterruptedDownload() is called (by way
661 // of MockResumeInterruptedDownload) after the download is interrupted. But 658 // of MockResumeInterruptedDownload) after the download is interrupted. But
662 // the mock doesn't follow through with the resumption. 659 // the mock doesn't follow through with the resumption.
663 // ResumeInterruptedDownload() being called is sufficient for verifying that 660 // ResumeInterruptedDownload() being called is sufficient for verifying that
664 // the automatic resumption was triggered. 661 // the automatic resumption was triggered.
665 RunAllPendingInMessageLoops(); 662 task_environment_.RunUntilIdle();
666 663
667 // The download item is currently in RESUMING_INTERNAL state, which maps to 664 // The download item is currently in RESUMING_INTERNAL state, which maps to
668 // IN_PROGRESS. 665 // IN_PROGRESS.
669 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 666 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
670 } 667 }
671 668
672 // Automatic resumption should restart and discard the intermediate file if the 669 // Automatic resumption should restart and discard the intermediate file if the
673 // interrupt reason requires it. 670 // interrupt reason requires it.
674 TEST_F(DownloadItemTest, AutomaticResumption_Restart) { 671 TEST_F(DownloadItemTest, AutomaticResumption_Restart) {
675 DownloadItemImpl* item = CreateDownloadItem(); 672 DownloadItemImpl* item = CreateDownloadItem();
(...skipping 13 matching lines...) Expand all
689 _)); 686 _));
690 687
691 item->DestinationObserverAsWeakPtr()->DestinationError( 688 item->DestinationObserverAsWeakPtr()->DestinationError(
692 DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE, 1, 689 DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE, 1,
693 std::unique_ptr<crypto::SecureHash>()); 690 std::unique_ptr<crypto::SecureHash>());
694 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 691 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
695 692
696 // Since the download is resumed automatically, the interrupt count doesn't 693 // Since the download is resumed automatically, the interrupt count doesn't
697 // increase. 694 // increase.
698 ASSERT_EQ(0, observer.interrupt_count()); 695 ASSERT_EQ(0, observer.interrupt_count());
699 RunAllPendingInMessageLoops(); 696 task_environment_.RunUntilIdle();
700 697
701 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 698 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
702 } 699 }
703 700
704 // Test that automatic resumption doesn't happen after an interrupt that 701 // Test that automatic resumption doesn't happen after an interrupt that
705 // requires user action to resolve. 702 // requires user action to resolve.
706 TEST_F(DownloadItemTest, AutomaticResumption_NeedsUserAction) { 703 TEST_F(DownloadItemTest, AutomaticResumption_NeedsUserAction) {
707 DownloadItemImpl* item = CreateDownloadItem(); 704 DownloadItemImpl* item = CreateDownloadItem();
708 TestDownloadItemObserver observer(item); 705 TestDownloadItemObserver observer(item);
709 MockDownloadFile* download_file = 706 MockDownloadFile* download_file =
710 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 707 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
711 708
712 // Interrupt the download, using a restartable interrupt. 709 // Interrupt the download, using a restartable interrupt.
713 EXPECT_CALL(*download_file, Cancel()); 710 EXPECT_CALL(*download_file, Cancel());
714 item->DestinationObserverAsWeakPtr()->DestinationError( 711 item->DestinationObserverAsWeakPtr()->DestinationError(
715 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, 712 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1,
716 std::unique_ptr<crypto::SecureHash>()); 713 std::unique_ptr<crypto::SecureHash>());
717 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 714 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
718 // Should not try to auto-resume. 715 // Should not try to auto-resume.
719 ASSERT_EQ(1, observer.interrupt_count()); 716 ASSERT_EQ(1, observer.interrupt_count());
720 ASSERT_EQ(0, observer.resume_count()); 717 ASSERT_EQ(0, observer.resume_count());
721 RunAllPendingInMessageLoops(); 718 task_environment_.RunUntilIdle();
722 719
723 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 720 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
724 } 721 }
725 722
726 // Test that a download is resumed automatically after a content length mismatch 723 // Test that a download is resumed automatically after a content length mismatch
727 // error. 724 // error.
728 TEST_F(DownloadItemTest, AutomaticResumption_ContentLengthMismatch) { 725 TEST_F(DownloadItemTest, AutomaticResumption_ContentLengthMismatch) {
729 DownloadItemImpl* item = CreateDownloadItem(); 726 DownloadItemImpl* item = CreateDownloadItem();
730 TestDownloadItemObserver observer(item); 727 TestDownloadItemObserver observer(item);
731 MockDownloadFile* download_file = 728 MockDownloadFile* download_file =
(...skipping 18 matching lines...) Expand all
750 747
751 item->DestinationObserverAsWeakPtr()->DestinationError( 748 item->DestinationObserverAsWeakPtr()->DestinationError(
752 DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH, 1, 749 DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH, 1,
753 std::unique_ptr<crypto::SecureHash>()); 750 std::unique_ptr<crypto::SecureHash>());
754 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 751 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
755 // Since the download is resumed automatically, the observer shouldn't notice 752 // Since the download is resumed automatically, the observer shouldn't notice
756 // the interruption. 753 // the interruption.
757 ASSERT_EQ(0, observer.interrupt_count()); 754 ASSERT_EQ(0, observer.interrupt_count());
758 ASSERT_EQ(0, observer.resume_count()); 755 ASSERT_EQ(0, observer.resume_count());
759 756
760 RunAllPendingInMessageLoops(); 757 task_environment_.RunUntilIdle();
761 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 758 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
762 } 759 }
763 760
764 // Check we do correct cleanup for RESUME_MODE_INVALID interrupts. 761 // Check we do correct cleanup for RESUME_MODE_INVALID interrupts.
765 TEST_F(DownloadItemTest, UnresumableInterrupt) { 762 TEST_F(DownloadItemTest, UnresumableInterrupt) {
766 DownloadItemImpl* item = CreateDownloadItem(); 763 DownloadItemImpl* item = CreateDownloadItem();
767 TestDownloadItemObserver observer(item); 764 TestDownloadItemObserver observer(item);
768 MockDownloadFile* download_file = 765 MockDownloadFile* download_file =
769 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 766 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
770 767
771 // Fail final rename with unresumable reason. 768 // Fail final rename with unresumable reason.
772 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 769 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
773 .WillOnce(Return(true)); 770 .WillOnce(Return(true));
774 EXPECT_CALL(*download_file, 771 EXPECT_CALL(*download_file,
775 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) 772 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
776 .WillOnce(ScheduleRenameAndAnnotateCallback( 773 .WillOnce(ScheduleRenameAndAnnotateCallback(
777 DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED, base::FilePath())); 774 DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED, base::FilePath()));
778 EXPECT_CALL(*download_file, Cancel()); 775 EXPECT_CALL(*download_file, Cancel());
779 776
780 // Complete download to trigger final rename. 777 // Complete download to trigger final rename.
781 item->DestinationObserverAsWeakPtr()->DestinationCompleted( 778 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
782 0, std::unique_ptr<crypto::SecureHash>()); 779 0, std::unique_ptr<crypto::SecureHash>());
783 RunAllPendingInMessageLoops(); 780 task_environment_.RunUntilIdle();
784 781
785 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 782 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
786 // Should not try to auto-resume. 783 // Should not try to auto-resume.
787 ASSERT_EQ(1, observer.interrupt_count()); 784 ASSERT_EQ(1, observer.interrupt_count());
788 ASSERT_EQ(0, observer.resume_count()); 785 ASSERT_EQ(0, observer.resume_count());
789 786
790 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 787 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
791 } 788 }
792 789
793 TEST_F(DownloadItemTest, AutomaticResumption_AttemptLimit) { 790 TEST_F(DownloadItemTest, AutomaticResumption_AttemptLimit) {
(...skipping 25 matching lines...) Expand all
819 mock_download_file_ref = mock_download_file.get(); 816 mock_download_file_ref = mock_download_file.get();
820 mock_request_handle = base::MakeUnique<NiceMock<MockRequestHandle>>(); 817 mock_request_handle = base::MakeUnique<NiceMock<MockRequestHandle>>();
821 818
822 ON_CALL(*mock_download_file_ref, FullPath()) 819 ON_CALL(*mock_download_file_ref, FullPath())
823 .WillByDefault(ReturnRefOfCopy(base::FilePath())); 820 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
824 821
825 // Copied key parts of DoIntermediateRename & CallDownloadItemStart 822 // Copied key parts of DoIntermediateRename & CallDownloadItemStart
826 // to allow for holding onto the request handle. 823 // to allow for holding onto the request handle.
827 item->Start(std::move(mock_download_file), std::move(mock_request_handle), 824 item->Start(std::move(mock_download_file), std::move(mock_request_handle),
828 *create_info()); 825 *create_info());
829 RunAllPendingInMessageLoops(); 826 task_environment_.RunUntilIdle();
830 827
831 base::FilePath target_path(kDummyTargetPath); 828 base::FilePath target_path(kDummyTargetPath);
832 base::FilePath intermediate_path(kDummyIntermediatePath); 829 base::FilePath intermediate_path(kDummyIntermediatePath);
833 830
834 // Target of RenameAndUniquify is always the intermediate path. 831 // Target of RenameAndUniquify is always the intermediate path.
835 ON_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)) 832 ON_CALL(*mock_download_file_ref, RenameAndUniquify(_, _))
836 .WillByDefault(ScheduleRenameAndUniquifyCallback( 833 .WillByDefault(ScheduleRenameAndUniquifyCallback(
837 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); 834 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
838 835
839 // RenameAndUniquify is only called the first time. In all the subsequent 836 // RenameAndUniquify is only called the first time. In all the subsequent
840 // iterations, the intermediate file already has the correct name, hence no 837 // iterations, the intermediate file already has the correct name, hence no
841 // rename is necessary. 838 // rename is necessary.
842 EXPECT_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)).Times(i == 0); 839 EXPECT_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)).Times(i == 0);
843 840
844 ASSERT_FALSE(callback.is_null()); 841 ASSERT_FALSE(callback.is_null());
845 base::ResetAndReturn(&callback).Run( 842 base::ResetAndReturn(&callback).Run(
846 target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 843 target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
847 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 844 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
848 DOWNLOAD_INTERRUPT_REASON_NONE); 845 DOWNLOAD_INTERRUPT_REASON_NONE);
849 RunAllPendingInMessageLoops(); 846 task_environment_.RunUntilIdle();
850 847
851 // Use a continuable interrupt. 848 // Use a continuable interrupt.
852 EXPECT_CALL(*mock_download_file_ref, Cancel()).Times(0); 849 EXPECT_CALL(*mock_download_file_ref, Cancel()).Times(0);
853 item->DestinationObserverAsWeakPtr()->DestinationError( 850 item->DestinationObserverAsWeakPtr()->DestinationError(
854 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, 851 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1,
855 std::unique_ptr<crypto::SecureHash>()); 852 std::unique_ptr<crypto::SecureHash>());
856 853
857 RunAllPendingInMessageLoops(); 854 task_environment_.RunUntilIdle();
858 ::testing::Mock::VerifyAndClearExpectations(mock_download_file_ref); 855 ::testing::Mock::VerifyAndClearExpectations(mock_download_file_ref);
859 } 856 }
860 857
861 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 858 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
862 EXPECT_EQ(1, observer.interrupt_count()); 859 EXPECT_EQ(1, observer.interrupt_count());
863 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 860 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
864 } 861 }
865 862
866 // If the download attempts to resume and the resumption request fails, the 863 // If the download attempts to resume and the resumption request fails, the
867 // subsequent Start() call shouldn't update the origin state (URL redirect 864 // subsequent Start() call shouldn't update the origin state (URL redirect
(...skipping 27 matching lines...) Expand all
895 MockResumeInterruptedDownload( 892 MockResumeInterruptedDownload(
896 AllOf(Property(&DownloadUrlParameters::file_path, 893 AllOf(Property(&DownloadUrlParameters::file_path,
897 Property(&base::FilePath::value, 894 Property(&base::FilePath::value,
898 kDummyIntermediatePath)), 895 kDummyIntermediatePath)),
899 Property(&DownloadUrlParameters::offset, 1)), 896 Property(&DownloadUrlParameters::offset, 1)),
900 _)); 897 _));
901 EXPECT_CALL(*download_file, Detach()); 898 EXPECT_CALL(*download_file, Detach());
902 item->DestinationObserverAsWeakPtr()->DestinationError( 899 item->DestinationObserverAsWeakPtr()->DestinationError(
903 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, 900 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1,
904 std::unique_ptr<crypto::SecureHash>()); 901 std::unique_ptr<crypto::SecureHash>());
905 RunAllPendingInMessageLoops(); 902 task_environment_.RunUntilIdle();
906 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 903 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
907 904
908 // Now change the create info. The changes should not cause the DownloadItem 905 // Now change the create info. The changes should not cause the DownloadItem
909 // to be updated. 906 // to be updated.
910 constexpr int kSecondResponseCode = 418; 907 constexpr int kSecondResponseCode = 418;
911 const char kSecondContentDisposition[] = "attachment; filename=bar"; 908 const char kSecondContentDisposition[] = "attachment; filename=bar";
912 const char kSecondETag[] = "123"; 909 const char kSecondETag[] = "123";
913 const char kSecondLastModified[] = "Today"; 910 const char kSecondLastModified[] = "Today";
914 const char kSecondURL[] = "http://example.com/another-download"; 911 const char kSecondURL[] = "http://example.com/another-download";
915 const char kSecondMimeType[] = "text/html"; 912 const char kSecondMimeType[] = "text/html";
(...skipping 11 matching lines...) Expand all
927 // Calling Start() with a response indicating failure shouldn't cause a target 924 // Calling Start() with a response indicating failure shouldn't cause a target
928 // update, nor should it result in discarding the intermediate file. 925 // update, nor should it result in discarding the intermediate file.
929 DownloadTargetCallback target_callback; 926 DownloadTargetCallback target_callback;
930 download_file = CallDownloadItemStart(item, &target_callback); 927 download_file = CallDownloadItemStart(item, &target_callback);
931 ASSERT_FALSE(target_callback.is_null()); 928 ASSERT_FALSE(target_callback.is_null());
932 target_callback.Run(base::FilePath(kDummyTargetPath), 929 target_callback.Run(base::FilePath(kDummyTargetPath),
933 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 930 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
934 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 931 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
935 base::FilePath(kDummyIntermediatePath), 932 base::FilePath(kDummyIntermediatePath),
936 DOWNLOAD_INTERRUPT_REASON_NONE); 933 DOWNLOAD_INTERRUPT_REASON_NONE);
937 RunAllPendingInMessageLoops(); 934 task_environment_.RunUntilIdle();
938 935
939 ASSERT_TRUE(item->GetResponseHeaders()); 936 ASSERT_TRUE(item->GetResponseHeaders());
940 EXPECT_EQ(kFirstResponseCode, item->GetResponseHeaders()->response_code()); 937 EXPECT_EQ(kFirstResponseCode, item->GetResponseHeaders()->response_code());
941 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); 938 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
942 EXPECT_EQ(kFirstETag, item->GetETag()); 939 EXPECT_EQ(kFirstETag, item->GetETag());
943 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); 940 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
944 EXPECT_EQ(kFirstURL, item->GetURL().spec()); 941 EXPECT_EQ(kFirstURL, item->GetURL().spec());
945 EXPECT_EQ(kMimeType, item->GetMimeType()); 942 EXPECT_EQ(kMimeType, item->GetMimeType());
946 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 943 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
947 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); 944 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
(...skipping 18 matching lines...) Expand all
966 create_info()->mime_type = kMimeType; 963 create_info()->mime_type = kMimeType;
967 964
968 DownloadItemImpl* item = CreateDownloadItem(); 965 DownloadItemImpl* item = CreateDownloadItem();
969 MockDownloadFile* download_file = 966 MockDownloadFile* download_file =
970 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 967 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
971 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); 968 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _));
972 EXPECT_CALL(*download_file, Detach()); 969 EXPECT_CALL(*download_file, Detach());
973 item->DestinationObserverAsWeakPtr()->DestinationError( 970 item->DestinationObserverAsWeakPtr()->DestinationError(
974 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, 971 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0,
975 std::unique_ptr<crypto::SecureHash>()); 972 std::unique_ptr<crypto::SecureHash>());
976 RunAllPendingInMessageLoops(); 973 task_environment_.RunUntilIdle();
977 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 974 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
978 975
979 // Now change the create info. The changes should not cause the DownloadItem 976 // Now change the create info. The changes should not cause the DownloadItem
980 // to be updated. 977 // to be updated.
981 constexpr int kSecondResponseCode = 201; 978 constexpr int kSecondResponseCode = 201;
982 const char kSecondContentDisposition[] = "attachment; filename=bar"; 979 const char kSecondContentDisposition[] = "attachment; filename=bar";
983 const char kSecondETag[] = "123"; 980 const char kSecondETag[] = "123";
984 const char kSecondLastModified[] = "Today"; 981 const char kSecondLastModified[] = "Today";
985 const char kSecondURL[] = "http://example.com/another-download"; 982 const char kSecondURL[] = "http://example.com/another-download";
986 const char kSecondMimeType[] = "text/html"; 983 const char kSecondMimeType[] = "text/html";
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 item->DestinationObserverAsWeakPtr()->DestinationUpdate(10, 100, 1022 item->DestinationObserverAsWeakPtr()->DestinationUpdate(10, 100,
1026 kReceivedSlice); 1023 kReceivedSlice);
1027 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); 1024 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices());
1028 EXPECT_EQ(10, item->GetReceivedBytes()); 1025 EXPECT_EQ(10, item->GetReceivedBytes());
1029 1026
1030 item->DestinationObserverAsWeakPtr()->DestinationError( 1027 item->DestinationObserverAsWeakPtr()->DestinationError(
1031 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, 1028 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0,
1032 std::unique_ptr<crypto::SecureHash>()); 1029 std::unique_ptr<crypto::SecureHash>());
1033 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); 1030 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices());
1034 1031
1035 RunAllPendingInMessageLoops(); 1032 task_environment_.RunUntilIdle();
1036 1033
1037 // Change the strong validator and resume the download, the received slices 1034 // Change the strong validator and resume the download, the received slices
1038 // should be cleared. 1035 // should be cleared.
1039 create_info()->etag = kSecondETag; 1036 create_info()->etag = kSecondETag;
1040 DownloadTargetCallback target_callback; 1037 DownloadTargetCallback target_callback;
1041 download_file = CallDownloadItemStart(item, &target_callback); 1038 download_file = CallDownloadItemStart(item, &target_callback);
1042 EXPECT_TRUE(item->GetReceivedSlices().empty()); 1039 EXPECT_TRUE(item->GetReceivedSlices().empty());
1043 EXPECT_EQ(0, item->GetReceivedBytes()); 1040 EXPECT_EQ(0, item->GetReceivedBytes());
1044 1041
1045 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 1042 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
(...skipping 23 matching lines...) Expand all
1069 .Times(1); 1066 .Times(1);
1070 item->DestinationObserverAsWeakPtr()->DestinationError( 1067 item->DestinationObserverAsWeakPtr()->DestinationError(
1071 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, 1068 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1,
1072 std::unique_ptr<crypto::SecureHash>()); 1069 std::unique_ptr<crypto::SecureHash>());
1073 1070
1074 // Test expectations verify that ResumeInterruptedDownload() is called (by way 1071 // Test expectations verify that ResumeInterruptedDownload() is called (by way
1075 // of MockResumeInterruptedDownload) after the download is interrupted. But 1072 // of MockResumeInterruptedDownload) after the download is interrupted. But
1076 // the mock doesn't follow through with the resumption. 1073 // the mock doesn't follow through with the resumption.
1077 // ResumeInterruptedDownload() being called is sufficient for verifying that 1074 // ResumeInterruptedDownload() being called is sufficient for verifying that
1078 // the resumption was triggered. 1075 // the resumption was triggered.
1079 RunAllPendingInMessageLoops(); 1076 task_environment_.RunUntilIdle();
1080 1077
1081 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. 1078 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS.
1082 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 1079 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
1083 } 1080 }
1084 1081
1085 TEST_F(DownloadItemTest, DisplayName) { 1082 TEST_F(DownloadItemTest, DisplayName) {
1086 DownloadItemImpl* item = CreateDownloadItem(); 1083 DownloadItemImpl* item = CreateDownloadItem();
1087 DownloadTargetCallback callback; 1084 DownloadTargetCallback callback;
1088 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1085 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1089 base::FilePath target_path( 1086 base::FilePath target_path(
1090 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1087 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1091 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); 1088 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x"));
1092 EXPECT_EQ(FILE_PATH_LITERAL(""), 1089 EXPECT_EQ(FILE_PATH_LITERAL(""),
1093 item->GetFileNameToReportUser().value()); 1090 item->GetFileNameToReportUser().value());
1094 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) 1091 EXPECT_CALL(*download_file, RenameAndUniquify(_, _))
1095 .WillOnce(ScheduleRenameAndUniquifyCallback( 1092 .WillOnce(ScheduleRenameAndUniquifyCallback(
1096 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); 1093 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
1097 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1094 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1098 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 1095 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1099 DOWNLOAD_INTERRUPT_REASON_NONE); 1096 DOWNLOAD_INTERRUPT_REASON_NONE);
1100 RunAllPendingInMessageLoops(); 1097 task_environment_.RunUntilIdle();
1101 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), 1098 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"),
1102 item->GetFileNameToReportUser().value()); 1099 item->GetFileNameToReportUser().value());
1103 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name"))); 1100 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name")));
1104 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), 1101 EXPECT_EQ(FILE_PATH_LITERAL("new.name"),
1105 item->GetFileNameToReportUser().value()); 1102 item->GetFileNameToReportUser().value());
1106 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 1103 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
1107 } 1104 }
1108 1105
1109 // Test to make sure that Start method calls DF initialize properly. 1106 // Test to make sure that Start method calls DF initialize properly.
1110 TEST_F(DownloadItemTest, Start) { 1107 TEST_F(DownloadItemTest, Start) {
1111 MockDownloadFile* mock_download_file(new MockDownloadFile); 1108 MockDownloadFile* mock_download_file(new MockDownloadFile);
1112 std::unique_ptr<DownloadFile> download_file(mock_download_file); 1109 std::unique_ptr<DownloadFile> download_file(mock_download_file);
1113 DownloadItemImpl* item = CreateDownloadItem(); 1110 DownloadItemImpl* item = CreateDownloadItem();
1114 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); 1111 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _));
1115 std::unique_ptr<DownloadRequestHandleInterface> request_handle( 1112 std::unique_ptr<DownloadRequestHandleInterface> request_handle(
1116 new NiceMock<MockRequestHandle>); 1113 new NiceMock<MockRequestHandle>);
1117 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); 1114 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
1118 item->Start(std::move(download_file), std::move(request_handle), 1115 item->Start(std::move(download_file), std::move(request_handle),
1119 *create_info()); 1116 *create_info());
1120 RunAllPendingInMessageLoops(); 1117 task_environment_.RunUntilIdle();
1121 1118
1122 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); 1119 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
1123 } 1120 }
1124 1121
1125 // Download file and the request should be cancelled as a result of download 1122 // Download file and the request should be cancelled as a result of download
1126 // file initialization failing. 1123 // file initialization failing.
1127 TEST_F(DownloadItemTest, InitDownloadFileFails) { 1124 TEST_F(DownloadItemTest, InitDownloadFileFails) {
1128 DownloadItemImpl* item = CreateDownloadItem(); 1125 DownloadItemImpl* item = CreateDownloadItem();
1129 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>(); 1126 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>();
1130 std::unique_ptr<MockRequestHandle> request_handle = 1127 std::unique_ptr<MockRequestHandle> request_handle =
1131 base::MakeUnique<MockRequestHandle>(); 1128 base::MakeUnique<MockRequestHandle>();
1132 1129
1133 EXPECT_CALL(*file, Cancel()); 1130 EXPECT_CALL(*file, Cancel());
1134 EXPECT_CALL(*request_handle, CancelRequest(_)); 1131 EXPECT_CALL(*request_handle, CancelRequest(_));
1135 EXPECT_CALL(*file, Initialize(_, _, _, _)) 1132 EXPECT_CALL(*file, Initialize(_, _, _, _))
1136 .WillOnce(ScheduleCallbackWithParam( 1133 .WillOnce(ScheduleCallbackWithParam(
1137 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED)); 1134 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED));
1138 1135
1139 base::RunLoop start_download_loop;
1140 DownloadTargetCallback download_target_callback; 1136 DownloadTargetCallback download_target_callback;
1141 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 1137 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
1142 .WillOnce(DoAll(SaveArg<1>(&download_target_callback), 1138 .WillOnce(SaveArg<1>(&download_target_callback));
1143 ScheduleClosure(start_download_loop.QuitClosure())));
1144 1139
1145 item->Start(std::move(file), std::move(request_handle), *create_info()); 1140 item->Start(std::move(file), std::move(request_handle), *create_info());
1146 start_download_loop.Run(); 1141 task_environment_.RunUntilIdle();
1147 1142
1148 download_target_callback.Run(base::FilePath(kDummyTargetPath), 1143 download_target_callback.Run(base::FilePath(kDummyTargetPath),
1149 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1144 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1150 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 1145 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1151 base::FilePath(kDummyIntermediatePath), 1146 base::FilePath(kDummyIntermediatePath),
1152 DOWNLOAD_INTERRUPT_REASON_NONE); 1147 DOWNLOAD_INTERRUPT_REASON_NONE);
1153 RunAllPendingInMessageLoops(); 1148 task_environment_.RunUntilIdle();
1154 1149
1155 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1150 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1156 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, 1151 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED,
1157 item->GetLastReason()); 1152 item->GetLastReason());
1158 EXPECT_FALSE(item->GetTargetFilePath().empty()); 1153 EXPECT_FALSE(item->GetTargetFilePath().empty());
1159 EXPECT_TRUE(item->GetFullPath().empty()); 1154 EXPECT_TRUE(item->GetFullPath().empty());
1160 } 1155 }
1161 1156
1162 // Handling of downloads initiated via a failed request. In this case, Start() 1157 // Handling of downloads initiated via a failed request. In this case, Start()
1163 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason. 1158 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason.
1164 TEST_F(DownloadItemTest, StartFailedDownload) { 1159 TEST_F(DownloadItemTest, StartFailedDownload) {
1165 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; 1160 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
1166 DownloadItemImpl* item = CreateDownloadItem(); 1161 DownloadItemImpl* item = CreateDownloadItem();
1167 1162
1168 // DownloadFile and DownloadRequestHandleInterface objects aren't created for 1163 // DownloadFile and DownloadRequestHandleInterface objects aren't created for
1169 // failed downloads. 1164 // failed downloads.
1170 std::unique_ptr<DownloadFile> null_download_file; 1165 std::unique_ptr<DownloadFile> null_download_file;
1171 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle; 1166 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle;
1172 DownloadTargetCallback download_target_callback; 1167 DownloadTargetCallback download_target_callback;
1173 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 1168 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
1174 .WillOnce(SaveArg<1>(&download_target_callback)); 1169 .WillOnce(SaveArg<1>(&download_target_callback));
1175 item->Start(std::move(null_download_file), std::move(null_request_handle), 1170 item->Start(std::move(null_download_file), std::move(null_request_handle),
1176 *create_info()); 1171 *create_info());
1177 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1172 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1178 RunAllPendingInMessageLoops(); 1173 task_environment_.RunUntilIdle();
1179 1174
1180 // The DownloadItemImpl should attempt to determine a target path even if the 1175 // The DownloadItemImpl should attempt to determine a target path even if the
1181 // download was interrupted. 1176 // download was interrupted.
1182 ASSERT_FALSE(download_target_callback.is_null()); 1177 ASSERT_FALSE(download_target_callback.is_null());
1183 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1178 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1184 base::FilePath target_path(FILE_PATH_LITERAL("foo")); 1179 base::FilePath target_path(FILE_PATH_LITERAL("foo"));
1185 download_target_callback.Run(target_path, 1180 download_target_callback.Run(target_path,
1186 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1181 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1187 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path, 1182 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
1188 DOWNLOAD_INTERRUPT_REASON_NONE); 1183 DOWNLOAD_INTERRUPT_REASON_NONE);
1189 RunAllPendingInMessageLoops(); 1184 task_environment_.RunUntilIdle();
1190 1185
1191 EXPECT_EQ(target_path, item->GetTargetFilePath()); 1186 EXPECT_EQ(target_path, item->GetTargetFilePath());
1192 CleanupItem(item, NULL, DownloadItem::INTERRUPTED); 1187 CleanupItem(item, NULL, DownloadItem::INTERRUPTED);
1193 } 1188 }
1194 1189
1195 // Test that the delegate is invoked after the download file is renamed. 1190 // Test that the delegate is invoked after the download file is renamed.
1196 TEST_F(DownloadItemTest, CallbackAfterRename) { 1191 TEST_F(DownloadItemTest, CallbackAfterRename) {
1197 DownloadItemImpl* item = CreateDownloadItem(); 1192 DownloadItemImpl* item = CreateDownloadItem();
1198 DownloadTargetCallback callback; 1193 DownloadTargetCallback callback;
1199 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1194 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1200 base::FilePath final_path( 1195 base::FilePath final_path(
1201 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1196 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1202 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1197 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1203 base::FilePath new_intermediate_path( 1198 base::FilePath new_intermediate_path(
1204 final_path.InsertBeforeExtensionASCII("y")); 1199 final_path.InsertBeforeExtensionASCII("y"));
1205 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1200 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1206 .WillOnce(ScheduleRenameAndUniquifyCallback( 1201 .WillOnce(ScheduleRenameAndUniquifyCallback(
1207 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 1202 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1208 1203
1209 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1204 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1210 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 1205 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1211 DOWNLOAD_INTERRUPT_REASON_NONE); 1206 DOWNLOAD_INTERRUPT_REASON_NONE);
1212 RunAllPendingInMessageLoops(); 1207 task_environment_.RunUntilIdle();
1213 // All the callbacks should have happened by now. 1208 // All the callbacks should have happened by now.
1214 ::testing::Mock::VerifyAndClearExpectations(download_file); 1209 ::testing::Mock::VerifyAndClearExpectations(download_file);
1215 mock_delegate()->VerifyAndClearExpectations(); 1210 mock_delegate()->VerifyAndClearExpectations();
1216 1211
1217 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1212 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1218 .WillOnce(Return(true)); 1213 .WillOnce(Return(true));
1219 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _)) 1214 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _))
1220 .WillOnce(ScheduleRenameAndAnnotateCallback( 1215 .WillOnce(ScheduleRenameAndAnnotateCallback(
1221 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); 1216 DOWNLOAD_INTERRUPT_REASON_NONE, final_path));
1222 EXPECT_CALL(*download_file, FullPath()) 1217 EXPECT_CALL(*download_file, FullPath())
1223 .WillOnce(ReturnRefOfCopy(base::FilePath())); 1218 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1224 EXPECT_CALL(*download_file, Detach()); 1219 EXPECT_CALL(*download_file, Detach());
1225 item->DestinationObserverAsWeakPtr()->DestinationCompleted( 1220 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1226 0, std::unique_ptr<crypto::SecureHash>()); 1221 0, std::unique_ptr<crypto::SecureHash>());
1227 RunAllPendingInMessageLoops(); 1222 task_environment_.RunUntilIdle();
1228 ::testing::Mock::VerifyAndClearExpectations(download_file); 1223 ::testing::Mock::VerifyAndClearExpectations(download_file);
1229 mock_delegate()->VerifyAndClearExpectations(); 1224 mock_delegate()->VerifyAndClearExpectations();
1230 } 1225 }
1231 1226
1232 // Test that the delegate is invoked after the download file is renamed and the 1227 // Test that the delegate is invoked after the download file is renamed and the
1233 // download item is in an interrupted state. 1228 // download item is in an interrupted state.
1234 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { 1229 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) {
1235 DownloadItemImpl* item = CreateDownloadItem(); 1230 DownloadItemImpl* item = CreateDownloadItem();
1236 DownloadTargetCallback callback; 1231 DownloadTargetCallback callback;
1237 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1232 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1238 base::FilePath final_path( 1233 base::FilePath final_path(
1239 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1234 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1240 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1235 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1241 base::FilePath new_intermediate_path( 1236 base::FilePath new_intermediate_path(
1242 final_path.InsertBeforeExtensionASCII("y")); 1237 final_path.InsertBeforeExtensionASCII("y"));
1243 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1238 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1244 .WillOnce(ScheduleRenameAndUniquifyCallback( 1239 .WillOnce(ScheduleRenameAndUniquifyCallback(
1245 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); 1240 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path));
1246 EXPECT_CALL(*download_file, Cancel()) 1241 EXPECT_CALL(*download_file, Cancel())
1247 .Times(1); 1242 .Times(1);
1248 1243
1249 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1244 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1250 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 1245 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1251 DOWNLOAD_INTERRUPT_REASON_NONE); 1246 DOWNLOAD_INTERRUPT_REASON_NONE);
1252 RunAllPendingInMessageLoops(); 1247 task_environment_.RunUntilIdle();
1253 // All the callbacks should have happened by now. 1248 // All the callbacks should have happened by now.
1254 ::testing::Mock::VerifyAndClearExpectations(download_file); 1249 ::testing::Mock::VerifyAndClearExpectations(download_file);
1255 mock_delegate()->VerifyAndClearExpectations(); 1250 mock_delegate()->VerifyAndClearExpectations();
1256 } 1251 }
1257 1252
1258 TEST_F(DownloadItemTest, Interrupted) { 1253 TEST_F(DownloadItemTest, Interrupted) {
1259 DownloadItemImpl* item = CreateDownloadItem(); 1254 DownloadItemImpl* item = CreateDownloadItem();
1260 MockDownloadFile* download_file = 1255 MockDownloadFile* download_file =
1261 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1256 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1262 1257
1263 const DownloadInterruptReason reason( 1258 const DownloadInterruptReason reason(
1264 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 1259 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
1265 1260
1266 // Confirm interrupt sets state properly. 1261 // Confirm interrupt sets state properly.
1267 EXPECT_CALL(*download_file, Cancel()); 1262 EXPECT_CALL(*download_file, Cancel());
1268 item->DestinationObserverAsWeakPtr()->DestinationError( 1263 item->DestinationObserverAsWeakPtr()->DestinationError(
1269 reason, 0, std::unique_ptr<crypto::SecureHash>()); 1264 reason, 0, std::unique_ptr<crypto::SecureHash>());
1270 RunAllPendingInMessageLoops(); 1265 task_environment_.RunUntilIdle();
1271 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1266 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1272 EXPECT_EQ(reason, item->GetLastReason()); 1267 EXPECT_EQ(reason, item->GetLastReason());
1273 1268
1274 // Cancel should kill it. 1269 // Cancel should kill it.
1275 item->Cancel(true); 1270 item->Cancel(true);
1276 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); 1271 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
1277 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); 1272 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason());
1278 } 1273 }
1279 1274
1280 // Destination errors that occur before the intermediate rename shouldn't cause 1275 // Destination errors that occur before the intermediate rename shouldn't cause
(...skipping 14 matching lines...) Expand all
1295 final_path.InsertBeforeExtensionASCII("y")); 1290 final_path.InsertBeforeExtensionASCII("y"));
1296 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1291 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1297 .WillOnce(ScheduleRenameAndUniquifyCallback( 1292 .WillOnce(ScheduleRenameAndUniquifyCallback(
1298 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 1293 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1299 EXPECT_CALL(*download_file, Cancel()) 1294 EXPECT_CALL(*download_file, Cancel())
1300 .Times(1); 1295 .Times(1);
1301 1296
1302 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1297 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1303 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 1298 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1304 DOWNLOAD_INTERRUPT_REASON_NONE); 1299 DOWNLOAD_INTERRUPT_REASON_NONE);
1305 RunAllPendingInMessageLoops(); 1300 task_environment_.RunUntilIdle();
1306 // All the callbacks should have happened by now. 1301 // All the callbacks should have happened by now.
1307 ::testing::Mock::VerifyAndClearExpectations(download_file); 1302 ::testing::Mock::VerifyAndClearExpectations(download_file);
1308 mock_delegate()->VerifyAndClearExpectations(); 1303 mock_delegate()->VerifyAndClearExpectations();
1309 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1304 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1310 EXPECT_TRUE(item->GetFullPath().empty()); 1305 EXPECT_TRUE(item->GetFullPath().empty());
1311 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1306 EXPECT_EQ(final_path, item->GetTargetFilePath());
1312 } 1307 }
1313 1308
1314 // As above. But if the download can be resumed by continuing, then the 1309 // As above. But if the download can be resumed by continuing, then the
1315 // intermediate path should be retained when the download is interrupted after 1310 // intermediate path should be retained when the download is interrupted after
(...skipping 18 matching lines...) Expand all
1334 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1329 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1335 .WillOnce(ScheduleRenameAndUniquifyCallback( 1330 .WillOnce(ScheduleRenameAndUniquifyCallback(
1336 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 1331 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1337 EXPECT_CALL(*download_file, FullPath()) 1332 EXPECT_CALL(*download_file, FullPath())
1338 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path))); 1333 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path)));
1339 EXPECT_CALL(*download_file, Detach()); 1334 EXPECT_CALL(*download_file, Detach());
1340 1335
1341 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1336 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1342 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 1337 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1343 DOWNLOAD_INTERRUPT_REASON_NONE); 1338 DOWNLOAD_INTERRUPT_REASON_NONE);
1344 RunAllPendingInMessageLoops(); 1339 task_environment_.RunUntilIdle();
1345 // All the callbacks should have happened by now. 1340 // All the callbacks should have happened by now.
1346 ::testing::Mock::VerifyAndClearExpectations(download_file); 1341 ::testing::Mock::VerifyAndClearExpectations(download_file);
1347 mock_delegate()->VerifyAndClearExpectations(); 1342 mock_delegate()->VerifyAndClearExpectations();
1348 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1343 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1349 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 1344 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
1350 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1345 EXPECT_EQ(final_path, item->GetTargetFilePath());
1351 } 1346 }
1352 1347
1353 // As above. If the intermediate rename fails, then the interrupt reason should 1348 // As above. If the intermediate rename fails, then the interrupt reason should
1354 // be set to the file error and the intermediate path should be empty. 1349 // be set to the file error and the intermediate path should be empty.
(...skipping 13 matching lines...) Expand all
1368 final_path.InsertBeforeExtensionASCII("y")); 1363 final_path.InsertBeforeExtensionASCII("y"));
1369 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1364 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1370 .WillOnce(ScheduleRenameAndUniquifyCallback( 1365 .WillOnce(ScheduleRenameAndUniquifyCallback(
1371 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); 1366 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path));
1372 EXPECT_CALL(*download_file, Cancel()) 1367 EXPECT_CALL(*download_file, Cancel())
1373 .Times(1); 1368 .Times(1);
1374 1369
1375 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1370 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1376 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, 1371 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1377 DOWNLOAD_INTERRUPT_REASON_NONE); 1372 DOWNLOAD_INTERRUPT_REASON_NONE);
1378 RunAllPendingInMessageLoops(); 1373 task_environment_.RunUntilIdle();
1379 // All the callbacks should have happened by now. 1374 // All the callbacks should have happened by now.
1380 ::testing::Mock::VerifyAndClearExpectations(download_file); 1375 ::testing::Mock::VerifyAndClearExpectations(download_file);
1381 mock_delegate()->VerifyAndClearExpectations(); 1376 mock_delegate()->VerifyAndClearExpectations();
1382 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1377 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1383 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason()); 1378 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason());
1384 EXPECT_TRUE(item->GetFullPath().empty()); 1379 EXPECT_TRUE(item->GetFullPath().empty());
1385 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1380 EXPECT_EQ(final_path, item->GetTargetFilePath());
1386 } 1381 }
1387 1382
1388 TEST_F(DownloadItemTest, Canceled) { 1383 TEST_F(DownloadItemTest, Canceled) {
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
1591 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) 1586 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _))
1592 .WillOnce(ScheduleRenameAndAnnotateCallback( 1587 .WillOnce(ScheduleRenameAndAnnotateCallback(
1593 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); 1588 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1594 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1589 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1595 .WillOnce(Return(true)); 1590 .WillOnce(Return(true));
1596 EXPECT_CALL(*download_file, FullPath()) 1591 EXPECT_CALL(*download_file, FullPath())
1597 .WillOnce(ReturnRefOfCopy(base::FilePath())); 1592 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1598 EXPECT_CALL(*download_file, Detach()); 1593 EXPECT_CALL(*download_file, Detach());
1599 item->DestinationObserverAsWeakPtr()->DestinationCompleted( 1594 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1600 0, std::unique_ptr<crypto::SecureHash>()); 1595 0, std::unique_ptr<crypto::SecureHash>());
1601 RunAllPendingInMessageLoops(); 1596 task_environment_.RunUntilIdle();
1602 1597
1603 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); 1598 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState());
1604 EXPECT_TRUE(item->CanShowInFolder()); 1599 EXPECT_TRUE(item->CanShowInFolder());
1605 EXPECT_TRUE(item->CanOpenDownload()); 1600 EXPECT_TRUE(item->CanOpenDownload());
1606 } 1601 }
1607 1602
1608 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { 1603 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) {
1609 // A download created with a non-empty FilePath is considered a temporary 1604 // A download created with a non-empty FilePath is considered a temporary
1610 // download. 1605 // download.
1611 create_info()->save_info->file_path = base::FilePath(kDummyTargetPath); 1606 create_info()->save_info->file_path = base::FilePath(kDummyTargetPath);
(...skipping 12 matching lines...) Expand all
1624 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1619 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1625 .WillOnce(Return(true)); 1620 .WillOnce(Return(true));
1626 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) 1621 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _))
1627 .WillOnce(ScheduleRenameAndAnnotateCallback( 1622 .WillOnce(ScheduleRenameAndAnnotateCallback(
1628 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); 1623 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1629 EXPECT_CALL(*download_file, FullPath()) 1624 EXPECT_CALL(*download_file, FullPath())
1630 .WillOnce(ReturnRefOfCopy(base::FilePath())); 1625 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1631 EXPECT_CALL(*download_file, Detach()); 1626 EXPECT_CALL(*download_file, Detach());
1632 item->DestinationObserverAsWeakPtr()->DestinationCompleted( 1627 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1633 0, std::unique_ptr<crypto::SecureHash>()); 1628 0, std::unique_ptr<crypto::SecureHash>());
1634 RunAllPendingInMessageLoops(); 1629 task_environment_.RunUntilIdle();
1635 1630
1636 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); 1631 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState());
1637 EXPECT_FALSE(item->CanShowInFolder()); 1632 EXPECT_FALSE(item->CanShowInFolder());
1638 EXPECT_FALSE(item->CanOpenDownload()); 1633 EXPECT_FALSE(item->CanOpenDownload());
1639 } 1634 }
1640 1635
1641 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { 1636 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) {
1642 DownloadItemImpl* item = CreateDownloadItem(); 1637 DownloadItemImpl* item = CreateDownloadItem();
1643 MockDownloadFile* download_file = 1638 MockDownloadFile* download_file =
1644 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1639 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1645 1640
1646 EXPECT_CALL(*download_file, Cancel()); 1641 EXPECT_CALL(*download_file, Cancel());
1647 item->DestinationObserverAsWeakPtr()->DestinationError( 1642 item->DestinationObserverAsWeakPtr()->DestinationError(
1648 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, 1643 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0,
1649 std::unique_ptr<crypto::SecureHash>()); 1644 std::unique_ptr<crypto::SecureHash>());
1650 RunAllPendingInMessageLoops(); 1645 task_environment_.RunUntilIdle();
1651 1646
1652 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1647 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1653 ASSERT_FALSE(item->GetTargetFilePath().empty()); 1648 ASSERT_FALSE(item->GetTargetFilePath().empty());
1654 EXPECT_FALSE(item->CanShowInFolder()); 1649 EXPECT_FALSE(item->CanShowInFolder());
1655 EXPECT_TRUE(item->CanOpenDownload()); 1650 EXPECT_TRUE(item->CanOpenDownload());
1656 } 1651 }
1657 1652
1658 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { 1653 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) {
1659 DownloadItemImpl* item = CreateDownloadItem(); 1654 DownloadItemImpl* item = CreateDownloadItem();
1660 MockDownloadFile* download_file = 1655 MockDownloadFile* download_file =
1661 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1656 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1662 1657
1663 EXPECT_CALL(*download_file, Cancel()); 1658 EXPECT_CALL(*download_file, Cancel());
1664 item->Cancel(true); 1659 item->Cancel(true);
1665 RunAllPendingInMessageLoops(); 1660 task_environment_.RunUntilIdle();
1666 1661
1667 ASSERT_EQ(DownloadItem::CANCELLED, item->GetState()); 1662 ASSERT_EQ(DownloadItem::CANCELLED, item->GetState());
1668 EXPECT_FALSE(item->CanShowInFolder()); 1663 EXPECT_FALSE(item->CanShowInFolder());
1669 EXPECT_FALSE(item->CanOpenDownload()); 1664 EXPECT_FALSE(item->CanOpenDownload());
1670 } 1665 }
1671 1666
1672 // Test various aspects of the delegate completion blocker. 1667 // Test various aspects of the delegate completion blocker.
1673 1668
1674 // Just allowing completion. 1669 // Just allowing completion.
1675 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { 1670 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) {
(...skipping 14 matching lines...) Expand all
1690 // Make sure the download can complete. 1685 // Make sure the download can complete.
1691 EXPECT_CALL(*download_file, 1686 EXPECT_CALL(*download_file,
1692 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) 1687 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1693 .WillOnce(ScheduleRenameAndAnnotateCallback( 1688 .WillOnce(ScheduleRenameAndAnnotateCallback(
1694 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); 1689 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1695 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1690 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1696 .WillOnce(Return(true)); 1691 .WillOnce(Return(true));
1697 EXPECT_CALL(*download_file, FullPath()) 1692 EXPECT_CALL(*download_file, FullPath())
1698 .WillOnce(ReturnRefOfCopy(base::FilePath())); 1693 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1699 EXPECT_CALL(*download_file, Detach()); 1694 EXPECT_CALL(*download_file, Detach());
1700 RunAllPendingInMessageLoops(); 1695 task_environment_.RunUntilIdle();
1701 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1696 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1702 } 1697 }
1703 1698
1704 // Just delaying completion. 1699 // Just delaying completion.
1705 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { 1700 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) {
1706 // Test to confirm that if we have a callback that returns true, 1701 // Test to confirm that if we have a callback that returns true,
1707 // we complete immediately. 1702 // we complete immediately.
1703
1708 DownloadItemImpl* item = CreateDownloadItem(); 1704 DownloadItemImpl* item = CreateDownloadItem();
1709 MockDownloadFile* download_file = 1705 MockDownloadFile* download_file =
1710 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1706 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1711 1707
1712 // Drive the delegate interaction.
1713 base::Closure delegate_callback; 1708 base::Closure delegate_callback;
1714 base::Closure copy_delegate_callback; 1709 base::Closure copy_delegate_callback;
1715 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1710 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1716 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1711 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1717 Return(false))) 1712 Return(false)))
1718 .WillOnce(Return(true)); 1713 .WillOnce(Return(true));
1719 item->DestinationObserverAsWeakPtr()->DestinationCompleted( 1714 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1720 0, std::unique_ptr<crypto::SecureHash>()); 1715 0, std::unique_ptr<crypto::SecureHash>());
1721 ASSERT_FALSE(delegate_callback.is_null()); 1716 ASSERT_FALSE(delegate_callback.is_null());
1722 copy_delegate_callback = delegate_callback; 1717 copy_delegate_callback = delegate_callback;
1723 delegate_callback.Reset(); 1718 delegate_callback.Reset();
1724 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1719 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1725 copy_delegate_callback.Run(); 1720 copy_delegate_callback.Run();
1726 ASSERT_TRUE(delegate_callback.is_null()); 1721 ASSERT_TRUE(delegate_callback.is_null());
1727 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1722 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1728 EXPECT_FALSE(item->IsDangerous()); 1723 EXPECT_FALSE(item->IsDangerous());
1729 1724
1730 // Make sure the download can complete. 1725 // Make sure the download can complete.
1731 EXPECT_CALL(*download_file, 1726 EXPECT_CALL(*download_file,
1732 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) 1727 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1733 .WillOnce(ScheduleRenameAndAnnotateCallback( 1728 .WillOnce(ScheduleRenameAndAnnotateCallback(
1734 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); 1729 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1735 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1730 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1736 .WillOnce(Return(true)); 1731 .WillOnce(Return(true));
1737 EXPECT_CALL(*download_file, FullPath()) 1732 EXPECT_CALL(*download_file, FullPath())
1738 .WillOnce(ReturnRefOfCopy(base::FilePath())); 1733 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1739 EXPECT_CALL(*download_file, Detach()); 1734 EXPECT_CALL(*download_file, Detach());
1740 RunAllPendingInMessageLoops(); 1735 task_environment_.RunUntilIdle();
1741 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1736 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1742 } 1737 }
1743 1738
1744 // Delay and set danger. 1739 // Delay and set danger.
1745 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { 1740 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) {
1746 // Test to confirm that if we have a callback that returns true, 1741 // Test to confirm that if we have a callback that returns true,
1747 // we complete immediately. 1742 // we complete immediately.
1748 DownloadItemImpl* item = CreateDownloadItem(); 1743 DownloadItemImpl* item = CreateDownloadItem();
1749 MockDownloadFile* download_file = 1744 MockDownloadFile* download_file =
1750 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1745 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
(...skipping 22 matching lines...) Expand all
1773 // Make sure the download doesn't complete until we've validated it. 1768 // Make sure the download doesn't complete until we've validated it.
1774 EXPECT_CALL(*download_file, 1769 EXPECT_CALL(*download_file,
1775 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) 1770 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1776 .WillOnce(ScheduleRenameAndAnnotateCallback( 1771 .WillOnce(ScheduleRenameAndAnnotateCallback(
1777 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); 1772 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1778 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1773 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1779 .WillOnce(Return(true)); 1774 .WillOnce(Return(true));
1780 EXPECT_CALL(*download_file, FullPath()) 1775 EXPECT_CALL(*download_file, FullPath())
1781 .WillOnce(ReturnRefOfCopy(base::FilePath())); 1776 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1782 EXPECT_CALL(*download_file, Detach()); 1777 EXPECT_CALL(*download_file, Detach());
1783 RunAllPendingInMessageLoops(); 1778 task_environment_.RunUntilIdle();
1784 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1779 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1785 EXPECT_TRUE(item->IsDangerous()); 1780 EXPECT_TRUE(item->IsDangerous());
1786 1781
1787 item->ValidateDangerousDownload(); 1782 item->ValidateDangerousDownload();
1788 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); 1783 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType());
1789 RunAllPendingInMessageLoops(); 1784 task_environment_.RunUntilIdle();
1790 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1785 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1791 } 1786 }
1792 1787
1793 // Just delaying completion twice. 1788 // Just delaying completion twice.
1794 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { 1789 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) {
1795 // Test to confirm that if we have a callback that returns true, 1790 // Test to confirm that if we have a callback that returns true,
1796 // we complete immediately. 1791 // we complete immediately.
1797 DownloadItemImpl* item = CreateDownloadItem(); 1792 DownloadItemImpl* item = CreateDownloadItem();
1798 MockDownloadFile* download_file = 1793 MockDownloadFile* download_file =
1799 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1794 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
(...skipping 26 matching lines...) Expand all
1826 // Make sure the download can complete. 1821 // Make sure the download can complete.
1827 EXPECT_CALL(*download_file, 1822 EXPECT_CALL(*download_file,
1828 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) 1823 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1829 .WillOnce(ScheduleRenameAndAnnotateCallback( 1824 .WillOnce(ScheduleRenameAndAnnotateCallback(
1830 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); 1825 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1831 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1826 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1832 .WillOnce(Return(true)); 1827 .WillOnce(Return(true));
1833 EXPECT_CALL(*download_file, FullPath()) 1828 EXPECT_CALL(*download_file, FullPath())
1834 .WillOnce(ReturnRefOfCopy(base::FilePath())); 1829 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1835 EXPECT_CALL(*download_file, Detach()); 1830 EXPECT_CALL(*download_file, Detach());
1836 RunAllPendingInMessageLoops(); 1831 task_environment_.RunUntilIdle();
1837 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1832 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1838 } 1833 }
1839 1834
1840 TEST_F(DownloadItemTest, StealDangerousDownloadAndDiscard) { 1835 TEST_F(DownloadItemTest, StealDangerousDownloadAndDiscard) {
1841 DownloadItemImpl* item = CreateDownloadItem(); 1836 DownloadItemImpl* item = CreateDownloadItem();
1842 MockDownloadFile* download_file = 1837 MockDownloadFile* download_file =
1843 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1838 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1844 ASSERT_TRUE(item->IsDangerous()); 1839 ASSERT_TRUE(item->IsDangerous());
1845 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); 1840 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt"));
1846 base::FilePath returned_path; 1841 base::FilePath returned_path;
1847 1842
1848 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); 1843 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path));
1849 EXPECT_CALL(*download_file, Detach()); 1844 EXPECT_CALL(*download_file, Detach());
1850 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1845 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1851 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1846 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1852 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); 1847 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>());
1853 item->StealDangerousDownload( 1848 item->StealDangerousDownload(
1854 true, // delete_file_after_feedback 1849 true, // delete_file_after_feedback
1855 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1850 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1856 weak_ptr_factory.GetWeakPtr(), 1851 weak_ptr_factory.GetWeakPtr(),
1857 base::Unretained(&returned_path))); 1852 base::Unretained(&returned_path)));
1858 RunAllPendingInMessageLoops(); 1853 task_environment_.RunUntilIdle();
1859 EXPECT_EQ(full_path, returned_path); 1854 EXPECT_EQ(full_path, returned_path);
1860 } 1855 }
1861 1856
1862 TEST_F(DownloadItemTest, StealDangerousDownloadAndKeep) { 1857 TEST_F(DownloadItemTest, StealDangerousDownloadAndKeep) {
1863 DownloadItemImpl* item = CreateDownloadItem(); 1858 DownloadItemImpl* item = CreateDownloadItem();
1864 MockDownloadFile* download_file = 1859 MockDownloadFile* download_file =
1865 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1860 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1866 ASSERT_TRUE(item->IsDangerous()); 1861 ASSERT_TRUE(item->IsDangerous());
1867 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); 1862 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt"));
1868 base::FilePath returned_path; 1863 base::FilePath returned_path;
1869 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); 1864 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path));
1870 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1865 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1871 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); 1866 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>());
1872 item->StealDangerousDownload( 1867 item->StealDangerousDownload(
1873 false, // delete_file_after_feedback 1868 false, // delete_file_after_feedback
1874 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1869 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1875 weak_ptr_factory.GetWeakPtr(), 1870 weak_ptr_factory.GetWeakPtr(),
1876 base::Unretained(&returned_path))); 1871 base::Unretained(&returned_path)));
1877 RunAllPendingInMessageLoops(); 1872 task_environment_.RunUntilIdle();
1878 EXPECT_NE(full_path, returned_path); 1873 EXPECT_NE(full_path, returned_path);
1879 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 1874 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
1880 } 1875 }
1881 1876
1882 TEST_F(DownloadItemTest, StealInterruptedContinuableDangerousDownload) { 1877 TEST_F(DownloadItemTest, StealInterruptedContinuableDangerousDownload) {
1883 base::FilePath returned_path; 1878 base::FilePath returned_path;
1884 DownloadItemImpl* item = CreateDownloadItem(); 1879 DownloadItemImpl* item = CreateDownloadItem();
1885 MockDownloadFile* download_file = 1880 MockDownloadFile* download_file =
1886 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1881 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1887 base::FilePath full_path = item->GetFullPath(); 1882 base::FilePath full_path = item->GetFullPath();
1888 EXPECT_FALSE(full_path.empty()); 1883 EXPECT_FALSE(full_path.empty());
1889 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); 1884 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path));
1890 EXPECT_CALL(*download_file, Detach()); 1885 EXPECT_CALL(*download_file, Detach());
1891 item->DestinationObserverAsWeakPtr()->DestinationError( 1886 item->DestinationObserverAsWeakPtr()->DestinationError(
1892 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1, 1887 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1,
1893 std::unique_ptr<crypto::SecureHash>()); 1888 std::unique_ptr<crypto::SecureHash>());
1894 ASSERT_TRUE(item->IsDangerous()); 1889 ASSERT_TRUE(item->IsDangerous());
1895 1890
1896 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1891 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1897 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1892 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1898 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); 1893 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>());
1899 item->StealDangerousDownload( 1894 item->StealDangerousDownload(
1900 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1895 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1901 weak_ptr_factory.GetWeakPtr(), 1896 weak_ptr_factory.GetWeakPtr(),
1902 base::Unretained(&returned_path))); 1897 base::Unretained(&returned_path)));
1903 RunAllPendingInMessageLoops(); 1898 task_environment_.RunUntilIdle();
1904 EXPECT_EQ(full_path, returned_path); 1899 EXPECT_EQ(full_path, returned_path);
1905 } 1900 }
1906 1901
1907 TEST_F(DownloadItemTest, StealInterruptedNonContinuableDangerousDownload) { 1902 TEST_F(DownloadItemTest, StealInterruptedNonContinuableDangerousDownload) {
1908 base::FilePath returned_path; 1903 base::FilePath returned_path;
1909 DownloadItemImpl* item = CreateDownloadItem(); 1904 DownloadItemImpl* item = CreateDownloadItem();
1910 MockDownloadFile* download_file = 1905 MockDownloadFile* download_file =
1911 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1906 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1912 EXPECT_CALL(*download_file, Cancel()); 1907 EXPECT_CALL(*download_file, Cancel());
1913 item->DestinationObserverAsWeakPtr()->DestinationError( 1908 item->DestinationObserverAsWeakPtr()->DestinationError(
1914 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, 1909 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1,
1915 std::unique_ptr<crypto::SecureHash>()); 1910 std::unique_ptr<crypto::SecureHash>());
1916 ASSERT_TRUE(item->IsDangerous()); 1911 ASSERT_TRUE(item->IsDangerous());
1917 1912
1918 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1913 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1919 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1914 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1920 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); 1915 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>());
1921 item->StealDangerousDownload( 1916 item->StealDangerousDownload(
1922 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1917 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1923 weak_ptr_factory.GetWeakPtr(), 1918 weak_ptr_factory.GetWeakPtr(),
1924 base::Unretained(&returned_path))); 1919 base::Unretained(&returned_path)));
1925 RunAllPendingInMessageLoops(); 1920 task_environment_.RunUntilIdle();
1926 EXPECT_TRUE(returned_path.empty()); 1921 EXPECT_TRUE(returned_path.empty());
1927 } 1922 }
1928 1923
1929 namespace { 1924 namespace {
1930 1925
1931 // The DownloadItemDestinationUpdateRaceTest fixture (defined below) is used to 1926 // The DownloadItemDestinationUpdateRaceTest fixture (defined below) is used to
1932 // test for race conditions between download destination events received via the 1927 // test for race conditions between download destination events received via the
1933 // DownloadDestinationObserver interface, and the target determination logic. 1928 // DownloadDestinationObserver interface, and the target determination logic.
1934 // 1929 //
1935 // The general control flow for DownloadItemImpl looks like this: 1930 // The general control flow for DownloadItemImpl looks like this:
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
2160 } // namespace 2155 } // namespace
2161 2156
2162 // Run through the DII workflow but the embedder cancels the download at target 2157 // Run through the DII workflow but the embedder cancels the download at target
2163 // determination. 2158 // determination.
2164 TEST_P(DownloadItemDestinationUpdateRaceTest, DownloadCancelledByUser) { 2159 TEST_P(DownloadItemDestinationUpdateRaceTest, DownloadCancelledByUser) {
2165 // Expect that the download file and the request will be cancelled as a 2160 // Expect that the download file and the request will be cancelled as a
2166 // result. 2161 // result.
2167 EXPECT_CALL(*file_, Cancel()); 2162 EXPECT_CALL(*file_, Cancel());
2168 EXPECT_CALL(*request_handle_, CancelRequest(_)); 2163 EXPECT_CALL(*request_handle_, CancelRequest(_));
2169 2164
2170 base::RunLoop download_start_loop;
2171 DownloadFile::InitializeCallback initialize_callback; 2165 DownloadFile::InitializeCallback initialize_callback;
2172 EXPECT_CALL(*file_, Initialize(_, _, _, _)) 2166 EXPECT_CALL(*file_, Initialize(_, _, _, _))
2173 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 2167 .WillOnce(SaveArg<0>(&initialize_callback));
2174 ScheduleClosure(download_start_loop.QuitClosure())));
2175 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); 2168 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
2176 download_start_loop.Run(); 2169 task_environment_.RunUntilIdle();
2177 2170
2178 base::WeakPtr<DownloadDestinationObserver> destination_observer = 2171 base::WeakPtr<DownloadDestinationObserver> destination_observer =
2179 item_->DestinationObserverAsWeakPtr(); 2172 item_->DestinationObserverAsWeakPtr();
2180 2173
2181 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 2174 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
2182 RunAllPendingInMessageLoops(); 2175 task_environment_.RunUntilIdle();
2183 2176
2184 base::RunLoop initialize_completion_loop;
2185 DownloadTargetCallback target_callback; 2177 DownloadTargetCallback target_callback;
2186 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) 2178 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _))
2187 .WillOnce( 2179 .WillOnce(SaveArg<1>(&target_callback));
2188 DoAll(SaveArg<1>(&target_callback),
2189 ScheduleClosure(initialize_completion_loop.QuitClosure())));
2190 ScheduleObservations(PostInitializeFileObservations(), destination_observer); 2180 ScheduleObservations(PostInitializeFileObservations(), destination_observer);
2191 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); 2181 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE);
2192 initialize_completion_loop.Run();
2193 2182
2194 RunAllPendingInMessageLoops(); 2183 task_environment_.RunUntilIdle();
2195 2184
2196 ASSERT_FALSE(target_callback.is_null()); 2185 ASSERT_FALSE(target_callback.is_null());
2197 ScheduleObservations(PostTargetDeterminationObservations(), 2186 ScheduleObservations(PostTargetDeterminationObservations(),
2198 destination_observer); 2187 destination_observer);
2199 target_callback.Run(base::FilePath(), 2188 target_callback.Run(base::FilePath(),
2200 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 2189 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2201 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(), 2190 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(),
2202 DOWNLOAD_INTERRUPT_REASON_NONE); 2191 DOWNLOAD_INTERRUPT_REASON_NONE);
2203 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState()); 2192 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState());
2204 RunAllPendingInMessageLoops(); 2193 task_environment_.RunUntilIdle();
2205 } 2194 }
2206 2195
2207 // Run through the DII workflow, but the intermediate rename fails. 2196 // Run through the DII workflow, but the intermediate rename fails.
2208 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) { 2197 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) {
2209 // Expect that the download file and the request will be cancelled as a 2198 // Expect that the download file and the request will be cancelled as a
2210 // result. 2199 // result.
2211 EXPECT_CALL(*file_, Cancel()); 2200 EXPECT_CALL(*file_, Cancel());
2212 EXPECT_CALL(*request_handle_, CancelRequest(_)); 2201 EXPECT_CALL(*request_handle_, CancelRequest(_));
2213 2202
2214 // Intermediate rename loop is not used immediately, but let's set up the 2203 // Intermediate rename loop is not used immediately, but let's set up the
2215 // DownloadFile expectations since we are about to transfer its ownership to 2204 // DownloadFile expectations since we are about to transfer its ownership to
2216 // the DownloadItem. 2205 // the DownloadItem.
2217 base::RunLoop intermediate_rename_loop;
2218 DownloadFile::RenameCompletionCallback intermediate_rename_callback; 2206 DownloadFile::RenameCompletionCallback intermediate_rename_callback;
2219 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) 2207 EXPECT_CALL(*file_, RenameAndUniquify(_, _))
2220 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), 2208 .WillOnce(SaveArg<1>(&intermediate_rename_callback));
2221 ScheduleClosure(intermediate_rename_loop.QuitClosure())));
2222 2209
2223 base::RunLoop download_start_loop;
2224 DownloadFile::InitializeCallback initialize_callback; 2210 DownloadFile::InitializeCallback initialize_callback;
2225 EXPECT_CALL(*file_, Initialize(_, _, _, _)) 2211 EXPECT_CALL(*file_, Initialize(_, _, _, _))
2226 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 2212 .WillOnce(SaveArg<0>(&initialize_callback));
2227 ScheduleClosure(download_start_loop.QuitClosure())));
2228 2213
2229 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); 2214 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
2230 download_start_loop.Run(); 2215 task_environment_.RunUntilIdle();
2216
2231 base::WeakPtr<DownloadDestinationObserver> destination_observer = 2217 base::WeakPtr<DownloadDestinationObserver> destination_observer =
2232 item_->DestinationObserverAsWeakPtr(); 2218 item_->DestinationObserverAsWeakPtr();
2233 2219
2234 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 2220 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
2235 RunAllPendingInMessageLoops(); 2221 task_environment_.RunUntilIdle();
2236 2222
2237 base::RunLoop initialize_completion_loop;
2238 DownloadTargetCallback target_callback; 2223 DownloadTargetCallback target_callback;
2239 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) 2224 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _))
2240 .WillOnce( 2225 .WillOnce(SaveArg<1>(&target_callback));
2241 DoAll(SaveArg<1>(&target_callback),
2242 ScheduleClosure(initialize_completion_loop.QuitClosure())));
2243 ScheduleObservations(PostInitializeFileObservations(), destination_observer); 2226 ScheduleObservations(PostInitializeFileObservations(), destination_observer);
2244 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); 2227 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE);
2245 initialize_completion_loop.Run();
2246 2228
2247 RunAllPendingInMessageLoops(); 2229 task_environment_.RunUntilIdle();
2248 ASSERT_FALSE(target_callback.is_null()); 2230 ASSERT_FALSE(target_callback.is_null());
2249 2231
2250 ScheduleObservations(PostTargetDeterminationObservations(), 2232 ScheduleObservations(PostTargetDeterminationObservations(),
2251 destination_observer); 2233 destination_observer);
2252 target_callback.Run(base::FilePath(kDummyTargetPath), 2234 target_callback.Run(base::FilePath(kDummyTargetPath),
2253 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 2235 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2254 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 2236 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2255 base::FilePath(kDummyIntermediatePath), 2237 base::FilePath(kDummyIntermediatePath),
2256 DOWNLOAD_INTERRUPT_REASON_NONE); 2238 DOWNLOAD_INTERRUPT_REASON_NONE);
2257 2239
2258 intermediate_rename_loop.Run(); 2240 task_environment_.RunUntilIdle();
2259 ASSERT_FALSE(intermediate_rename_callback.is_null()); 2241 ASSERT_FALSE(intermediate_rename_callback.is_null());
2260 2242
2261 ScheduleObservations(PostIntermediateRenameObservations(), 2243 ScheduleObservations(PostIntermediateRenameObservations(),
2262 destination_observer); 2244 destination_observer);
2263 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 2245 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED,
2264 base::FilePath()); 2246 base::FilePath());
2265 RunAllPendingInMessageLoops(); 2247 task_environment_.RunUntilIdle();
2266 2248
2267 EXPECT_EQ(DownloadItem::INTERRUPTED, item_->GetState()); 2249 EXPECT_EQ(DownloadItem::INTERRUPTED, item_->GetState());
2268 } 2250 }
2269 2251
2270 // Run through the DII workflow. Download file initialization, target 2252 // Run through the DII workflow. Download file initialization, target
2271 // determination and intermediate rename all succeed. 2253 // determination and intermediate rename all succeed.
2272 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) { 2254 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) {
2273 // We expect either that the download will fail (in which case the request and 2255 // We expect either that the download will fail (in which case the request and
2274 // the download file will be cancelled), or it will succeed (in which case the 2256 // the download file will be cancelled), or it will succeed (in which case the
2275 // DownloadFile will Detach()). It depends on the list of observations that 2257 // DownloadFile will Detach()). It depends on the list of observations that
2276 // are given to us. 2258 // are given to us.
2277 EXPECT_CALL(*file_, Cancel()).Times(::testing::AnyNumber()); 2259 EXPECT_CALL(*file_, Cancel()).Times(::testing::AnyNumber());
2278 EXPECT_CALL(*request_handle_, CancelRequest(_)).Times(::testing::AnyNumber()); 2260 EXPECT_CALL(*request_handle_, CancelRequest(_)).Times(::testing::AnyNumber());
2279 EXPECT_CALL(*file_, Detach()).Times(::testing::AnyNumber()); 2261 EXPECT_CALL(*file_, Detach()).Times(::testing::AnyNumber());
2280 2262
2281 EXPECT_CALL(*file_, FullPath()) 2263 EXPECT_CALL(*file_, FullPath())
2282 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyIntermediatePath))); 2264 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyIntermediatePath)));
2283 2265
2284 // Intermediate rename loop is not used immediately, but let's set up the 2266 // Intermediate rename loop is not used immediately, but let's set up the
2285 // DownloadFile expectations since we are about to transfer its ownership to 2267 // DownloadFile expectations since we are about to transfer its ownership to
2286 // the DownloadItem. 2268 // the DownloadItem.
2287 base::RunLoop intermediate_rename_loop;
2288 DownloadFile::RenameCompletionCallback intermediate_rename_callback; 2269 DownloadFile::RenameCompletionCallback intermediate_rename_callback;
2289 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) 2270 EXPECT_CALL(*file_, RenameAndUniquify(_, _))
2290 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), 2271 .WillOnce(SaveArg<1>(&intermediate_rename_callback));
2291 ScheduleClosure(intermediate_rename_loop.QuitClosure())));
2292 2272
2293 base::RunLoop download_start_loop;
2294 DownloadFile::InitializeCallback initialize_callback; 2273 DownloadFile::InitializeCallback initialize_callback;
2295 EXPECT_CALL(*file_, Initialize(_, _, _, _)) 2274 EXPECT_CALL(*file_, Initialize(_, _, _, _))
2296 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), 2275 .WillOnce(SaveArg<0>(&initialize_callback));
2297 ScheduleClosure(download_start_loop.QuitClosure())));
2298 2276
2299 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); 2277 item_->Start(std::move(file_), std::move(request_handle_), *create_info());
2300 download_start_loop.Run(); 2278 task_environment_.RunUntilIdle();
2279
2301 base::WeakPtr<DownloadDestinationObserver> destination_observer = 2280 base::WeakPtr<DownloadDestinationObserver> destination_observer =
2302 item_->DestinationObserverAsWeakPtr(); 2281 item_->DestinationObserverAsWeakPtr();
2303 2282
2304 ScheduleObservations(PreInitializeFileObservations(), destination_observer); 2283 ScheduleObservations(PreInitializeFileObservations(), destination_observer);
2305 RunAllPendingInMessageLoops(); 2284 task_environment_.RunUntilIdle();
2306 2285
2307 base::RunLoop initialize_completion_loop;
2308 DownloadTargetCallback target_callback; 2286 DownloadTargetCallback target_callback;
2309 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) 2287 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _))
2310 .WillOnce( 2288 .WillOnce(SaveArg<1>(&target_callback));
2311 DoAll(SaveArg<1>(&target_callback),
2312 ScheduleClosure(initialize_completion_loop.QuitClosure())));
2313 ScheduleObservations(PostInitializeFileObservations(), destination_observer); 2289 ScheduleObservations(PostInitializeFileObservations(), destination_observer);
2314 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); 2290 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE);
2315 initialize_completion_loop.Run();
2316 2291
2317 RunAllPendingInMessageLoops(); 2292 task_environment_.RunUntilIdle();
2318 ASSERT_FALSE(target_callback.is_null()); 2293 ASSERT_FALSE(target_callback.is_null());
2319 2294
2320 ScheduleObservations(PostTargetDeterminationObservations(), 2295 ScheduleObservations(PostTargetDeterminationObservations(),
2321 destination_observer); 2296 destination_observer);
2322 target_callback.Run(base::FilePath(kDummyTargetPath), 2297 target_callback.Run(base::FilePath(kDummyTargetPath),
2323 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 2298 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2324 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 2299 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2325 base::FilePath(kDummyIntermediatePath), 2300 base::FilePath(kDummyIntermediatePath),
2326 DOWNLOAD_INTERRUPT_REASON_NONE); 2301 DOWNLOAD_INTERRUPT_REASON_NONE);
2327 2302
2328 intermediate_rename_loop.Run(); 2303 task_environment_.RunUntilIdle();
2329 ASSERT_FALSE(intermediate_rename_callback.is_null()); 2304 ASSERT_FALSE(intermediate_rename_callback.is_null());
2330 2305
2331 // This may or may not be called, depending on whether there are any errors in 2306 // This may or may not be called, depending on whether there are any errors in
2332 // our action list. 2307 // our action list.
2333 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) 2308 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _))
2334 .Times(::testing::AnyNumber()); 2309 .Times(::testing::AnyNumber());
2335 2310
2336 ScheduleObservations(PostIntermediateRenameObservations(), 2311 ScheduleObservations(PostIntermediateRenameObservations(),
2337 destination_observer); 2312 destination_observer);
2338 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE, 2313 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE,
2339 base::FilePath(kDummyIntermediatePath)); 2314 base::FilePath(kDummyIntermediatePath));
2340 RunAllPendingInMessageLoops(); 2315 task_environment_.RunUntilIdle();
2341 2316
2342 // The state of the download depends on the observer events that were played 2317 // The state of the download depends on the observer events that were played
2343 // back to the DownloadItemImpl. Hence we can't establish a single expectation 2318 // back to the DownloadItemImpl. Hence we can't establish a single expectation
2344 // here. On Debug builds, the DCHECKs will verify that the state transitions 2319 // here. On Debug builds, the DCHECKs will verify that the state transitions
2345 // were correct. On Release builds, tests are expected to run to completion 2320 // were correct. On Release builds, tests are expected to run to completion
2346 // without crashing on success. 2321 // without crashing on success.
2347 EXPECT_TRUE(item_->GetState() == DownloadItem::IN_PROGRESS || 2322 EXPECT_TRUE(item_->GetState() == DownloadItem::IN_PROGRESS ||
2348 item_->GetState() == DownloadItem::INTERRUPTED); 2323 item_->GetState() == DownloadItem::INTERRUPTED);
2349 if (item_->GetState() == DownloadItem::INTERRUPTED) 2324 if (item_->GetState() == DownloadItem::INTERRUPTED)
2350 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item_->GetLastReason()); 2325 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item_->GetLastReason());
2351 2326
2352 item_->Cancel(true); 2327 item_->Cancel(true);
2353 RunAllPendingInMessageLoops(); 2328 task_environment_.RunUntilIdle();
2354 } 2329 }
2355 2330
2356 TEST(MockDownloadItem, Compiles) { 2331 TEST(MockDownloadItem, Compiles) {
2357 MockDownloadItem mock_item; 2332 MockDownloadItem mock_item;
2358 } 2333 }
2359 2334
2360 } // namespace content 2335 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698