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

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

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

Powered by Google App Engine
This is Rietveld 408576698