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

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

Issue 1251243003: Support restricting browsing data removal for downloads by origin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Remove unneeded DCHECK; Initialize and clear download URLs along test fixture life cycle. Created 5 years, 4 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 <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 19 matching lines...) Expand all
30 #include "content/public/browser/download_manager_delegate.h" 30 #include "content/public/browser/download_manager_delegate.h"
31 #include "content/public/browser/zoom_level_delegate.h" 31 #include "content/public/browser/zoom_level_delegate.h"
32 #include "content/public/test/mock_download_item.h" 32 #include "content/public/test/mock_download_item.h"
33 #include "content/public/test/test_browser_context.h" 33 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread.h" 34 #include "content/public/test/test_browser_thread.h"
35 #include "net/base/net_util.h" 35 #include "net/base/net_util.h"
36 #include "net/log/net_log.h" 36 #include "net/log/net_log.h"
37 #include "testing/gmock/include/gmock/gmock.h" 37 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gmock_mutant.h" 38 #include "testing/gmock_mutant.h"
39 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "url/origin.h"
40 41
41 using ::testing::AllOf; 42 using ::testing::AllOf;
42 using ::testing::DoAll; 43 using ::testing::DoAll;
43 using ::testing::Eq; 44 using ::testing::Eq;
44 using ::testing::Ref; 45 using ::testing::Ref;
45 using ::testing::Return; 46 using ::testing::Return;
46 using ::testing::ReturnRef; 47 using ::testing::ReturnRef;
47 using ::testing::SetArgPointee; 48 using ::testing::SetArgPointee;
48 using ::testing::StrictMock; 49 using ::testing::StrictMock;
49 using ::testing::_; 50 using ::testing::_;
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 NULL, mock_browser_context_.get())); 477 NULL, mock_browser_context_.get()));
477 download_manager_->SetDownloadItemFactoryForTesting( 478 download_manager_->SetDownloadItemFactoryForTesting(
478 scoped_ptr<DownloadItemFactory>( 479 scoped_ptr<DownloadItemFactory>(
479 mock_download_item_factory_.get()).Pass()); 480 mock_download_item_factory_.get()).Pass());
480 download_manager_->SetDownloadFileFactoryForTesting( 481 download_manager_->SetDownloadFileFactoryForTesting(
481 scoped_ptr<DownloadFileFactory>( 482 scoped_ptr<DownloadFileFactory>(
482 mock_download_file_factory_.get()).Pass()); 483 mock_download_file_factory_.get()).Pass());
483 observer_.reset(new MockDownloadManagerObserver()); 484 observer_.reset(new MockDownloadManagerObserver());
484 download_manager_->AddObserver(observer_.get()); 485 download_manager_->AddObserver(observer_.get());
485 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); 486 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
487 download_urls_.push_back(GURL("http://www.url1.com"));
488 download_urls_.push_back(GURL("http://www.url2.com"));
489 download_urls_.push_back(GURL("http://www.url3.com"));
490 download_urls_.push_back(GURL("http://www.url4.com"));
486 } 491 }
487 492
488 void TearDown() override { 493 void TearDown() override {
489 while (MockDownloadItemImpl* 494 while (MockDownloadItemImpl*
490 item = mock_download_item_factory_->PopItem()) { 495 item = mock_download_item_factory_->PopItem()) {
491 EXPECT_CALL(*item, GetState()) 496 EXPECT_CALL(*item, GetState())
492 .WillOnce(Return(DownloadItem::CANCELLED)); 497 .WillOnce(Return(DownloadItem::CANCELLED));
493 } 498 }
494 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) 499 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
495 .WillOnce(Return()); 500 .WillOnce(Return());
496 501
497 download_manager_->Shutdown(); 502 download_manager_->Shutdown();
498 download_manager_.reset(); 503 download_manager_.reset();
499 message_loop_.RunUntilIdle(); 504 message_loop_.RunUntilIdle();
500 ASSERT_EQ(NULL, mock_download_item_factory_.get()); 505 ASSERT_EQ(NULL, mock_download_item_factory_.get());
501 ASSERT_EQ(NULL, mock_download_file_factory_.get()); 506 ASSERT_EQ(NULL, mock_download_file_factory_.get());
502 message_loop_.RunUntilIdle(); 507 message_loop_.RunUntilIdle();
503 mock_download_manager_delegate_.reset(); 508 mock_download_manager_delegate_.reset();
504 mock_browser_context_.reset(); 509 mock_browser_context_.reset();
510 download_urls_.clear();
505 } 511 }
506 512
507 // Returns download id. 513 // Returns download id.
508 MockDownloadItemImpl& AddItemToManager() { 514 MockDownloadItemImpl& AddItemToManager() {
509 DownloadCreateInfo info; 515 DownloadCreateInfo info;
510 516
511 // Args are ignored except for download id, so everything else can be 517 // Args are ignored except for download id, so everything else can be
512 // null. 518 // null.
513 uint32 id = next_download_id_; 519 uint32 id = next_download_id_;
514 ++next_download_id_; 520 ++next_download_id_;
515 info.request_handle = DownloadRequestHandle(); 521 info.request_handle = DownloadRequestHandle();
516 download_manager_->CreateActiveItem(id, info); 522 download_manager_->CreateActiveItem(id, info);
517 DCHECK(mock_download_item_factory_->GetItem(id)); 523 DCHECK(mock_download_item_factory_->GetItem(id));
518 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); 524 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
519 // Satisfy expectation. If the item is created in StartDownload(), 525 // Satisfy expectation. If the item is created in StartDownload(),
520 // we call Start on it immediately, so we need to set that expectation 526 // we call Start on it immediately, so we need to set that expectation
521 // in the factory. 527 // in the factory.
522 scoped_ptr<DownloadRequestHandleInterface> req_handle; 528 scoped_ptr<DownloadRequestHandleInterface> req_handle;
523 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass()); 529 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass());
530 DCHECK(id < download_urls_.size());
531 EXPECT_CALL(item, GetURL()).WillRepeatedly(ReturnRef(download_urls_[id]));
524 532
525 return item; 533 return item;
526 } 534 }
527 535
528 MockDownloadItemImpl& GetMockDownloadItem(int id) { 536 MockDownloadItemImpl& GetMockDownloadItem(int id) {
529 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); 537 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
530 538
531 DCHECK(itemp); 539 DCHECK(itemp);
532 return *itemp; 540 return *itemp;
533 } 541 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 scoped_ptr<DownloadManagerImpl> download_manager_; 577 scoped_ptr<DownloadManagerImpl> download_manager_;
570 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_; 578 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
571 579
572 // Target detetermined callback. 580 // Target detetermined callback.
573 bool callback_called_; 581 bool callback_called_;
574 base::FilePath target_path_; 582 base::FilePath target_path_;
575 DownloadItem::TargetDisposition target_disposition_; 583 DownloadItem::TargetDisposition target_disposition_;
576 DownloadDangerType danger_type_; 584 DownloadDangerType danger_type_;
577 base::FilePath intermediate_path_; 585 base::FilePath intermediate_path_;
578 586
587 std::vector<GURL> download_urls_;
588
579 private: 589 private:
580 base::MessageLoopForUI message_loop_; 590 base::MessageLoopForUI message_loop_;
581 TestBrowserThread ui_thread_; 591 TestBrowserThread ui_thread_;
582 TestBrowserThread file_thread_; 592 TestBrowserThread file_thread_;
583 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; 593 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
584 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; 594 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
585 scoped_ptr<MockBrowserContext> mock_browser_context_; 595 scoped_ptr<MockBrowserContext> mock_browser_context_;
586 scoped_ptr<MockDownloadManagerObserver> observer_; 596 scoped_ptr<MockDownloadManagerObserver> observer_;
587 uint32 next_download_id_; 597 uint32 next_download_id_;
588 598
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 EXPECT_CALL(GetMockDownloadItem(2), Remove()) 697 EXPECT_CALL(GetMockDownloadItem(2), Remove())
688 .WillOnce(Return()); 698 .WillOnce(Return());
689 EXPECT_CALL(GetMockDownloadItem(3), Remove()) 699 EXPECT_CALL(GetMockDownloadItem(3), Remove())
690 .Times(0); 700 .Times(0);
691 701
692 download_manager_->RemoveAllDownloads(); 702 download_manager_->RemoveAllDownloads();
693 // Because we're mocking the download item, the Remove call doesn't 703 // Because we're mocking the download item, the Remove call doesn't
694 // result in them being removed from the DownloadManager list. 704 // result in them being removed from the DownloadManager list.
695 } 705 }
696 706
707 // Confirm that only downloads with same origin are removed.
708 TEST_F(DownloadManagerTest, RemoveSameOriginDownloads) {
709 base::Time now(base::Time::Now());
710 for (uint32 i = 0; i < 2; ++i) {
711 MockDownloadItemImpl& item(AddItemToManager());
712 EXPECT_CALL(item, GetStartTime()).WillRepeatedly(Return(now));
713 EXPECT_CALL(item, GetState())
714 .WillRepeatedly(Return(DownloadItem::COMPLETE));
715 }
716
717 EXPECT_CALL(GetMockDownloadItem(0), Remove());
718 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0);
719
720 url::Origin origin_to_clear(download_urls_[0]);
721 int remove_count = download_manager_->RemoveDownloadsByOriginAndTime(
722 origin_to_clear, base::Time(), base::Time::Max());
723 EXPECT_EQ(remove_count, 1);
724 }
725
697 } // namespace content 726 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_manager_impl.cc ('k') | content/public/browser/browser_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698