Commit 65c74008 authored by asanka@chromium.org's avatar asanka@chromium.org

[Downloads] Add real observers to MockDownloadItem.

Managing observer lists is needed by all the tests that end up calling
DownloadItem::AddObserver. This CL adds real observers to
MockDownloadItem so that tests don't need to mock this behavior.

BUG=367311

Review URL: https://codereview.chromium.org/248713004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269585 0039d316-1c4b-4281-b951-d872f2087c98
parent cac03112
...@@ -85,7 +85,6 @@ TEST_F(AllDownloadItemNotifierTest, ...@@ -85,7 +85,6 @@ TEST_F(AllDownloadItemNotifierTest,
items.push_back(&item()); items.push_back(&item());
EXPECT_CALL(manager(), GetAllDownloads(_)) EXPECT_CALL(manager(), GetAllDownloads(_))
.WillOnce(SetArgPointee<0>(items)); .WillOnce(SetArgPointee<0>(items));
EXPECT_CALL(item(), AddObserver(_));
SetNotifier(); SetNotifier();
EXPECT_CALL(observer(), OnDownloadUpdated(&manager(), &item())); EXPECT_CALL(observer(), OnDownloadUpdated(&manager(), &item()));
...@@ -97,7 +96,6 @@ TEST_F(AllDownloadItemNotifierTest, ...@@ -97,7 +96,6 @@ TEST_F(AllDownloadItemNotifierTest,
EXPECT_CALL(observer(), OnDownloadRemoved(&manager(), &item())); EXPECT_CALL(observer(), OnDownloadRemoved(&manager(), &item()));
NotifierAsItemObserver()->OnDownloadRemoved(&item()); NotifierAsItemObserver()->OnDownloadRemoved(&item());
EXPECT_CALL(item(), RemoveObserver(NotifierAsItemObserver()));
EXPECT_CALL(manager(), RemoveObserver(NotifierAsManagerObserver())); EXPECT_CALL(manager(), RemoveObserver(NotifierAsManagerObserver()));
ClearNotifier(); ClearNotifier();
} }
...@@ -107,7 +105,6 @@ TEST_F(AllDownloadItemNotifierTest, ...@@ -107,7 +105,6 @@ TEST_F(AllDownloadItemNotifierTest,
EXPECT_CALL(manager(), GetAllDownloads(_)); EXPECT_CALL(manager(), GetAllDownloads(_));
SetNotifier(); SetNotifier();
EXPECT_CALL(item(), AddObserver(NotifierAsItemObserver()));
EXPECT_CALL(observer(), OnDownloadCreated(&manager(), &item())); EXPECT_CALL(observer(), OnDownloadCreated(&manager(), &item()));
NotifierAsManagerObserver()->OnDownloadCreated( NotifierAsManagerObserver()->OnDownloadCreated(
&manager(), &item()); &manager(), &item());
...@@ -115,8 +112,5 @@ TEST_F(AllDownloadItemNotifierTest, ...@@ -115,8 +112,5 @@ TEST_F(AllDownloadItemNotifierTest,
EXPECT_CALL(manager(), RemoveObserver(NotifierAsManagerObserver())); EXPECT_CALL(manager(), RemoveObserver(NotifierAsManagerObserver()));
NotifierAsManagerObserver()->ManagerGoingDown(&manager()); NotifierAsManagerObserver()->ManagerGoingDown(&manager());
EXPECT_CALL(item(), RemoveObserver(NotifierAsItemObserver()));
NotifierAsItemObserver()->OnDownloadDestroyed(&item());
ClearNotifier(); ClearNotifier();
} }
...@@ -24,8 +24,7 @@ using ::testing::SaveArg; ...@@ -24,8 +24,7 @@ using ::testing::SaveArg;
class DownloadDangerPromptTest : public InProcessBrowserTest { class DownloadDangerPromptTest : public InProcessBrowserTest {
public: public:
DownloadDangerPromptTest() DownloadDangerPromptTest()
: download_observer_(NULL), : prompt_(NULL),
prompt_(NULL),
expected_action_(DownloadDangerPrompt::CANCEL), expected_action_(DownloadDangerPrompt::CANCEL),
did_receive_callback_(false) { did_receive_callback_(false) {
} }
...@@ -66,19 +65,12 @@ class DownloadDangerPromptTest : public InProcessBrowserTest { ...@@ -66,19 +65,12 @@ class DownloadDangerPromptTest : public InProcessBrowserTest {
content::MockDownloadItem& download() { return download_; } content::MockDownloadItem& download() { return download_; }
content::DownloadItem::Observer* download_observer() {
return download_observer_;
}
DownloadDangerPrompt* prompt() { return prompt_; } DownloadDangerPrompt* prompt() { return prompt_; }
private: private:
void SetUpDownloadItemExpectations() { void SetUpDownloadItemExpectations() {
EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return(
base::FilePath(FILE_PATH_LITERAL("evil.exe")))); base::FilePath(FILE_PATH_LITERAL("evil.exe"))));
EXPECT_CALL(download_, AddObserver(_))
.WillOnce(SaveArg<0>(&download_observer_));
EXPECT_CALL(download_, RemoveObserver(Eq(ByRef(download_observer_))));
EXPECT_CALL(download_, GetDangerType()) EXPECT_CALL(download_, GetDangerType())
.WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL));
} }
...@@ -100,7 +92,6 @@ class DownloadDangerPromptTest : public InProcessBrowserTest { ...@@ -100,7 +92,6 @@ class DownloadDangerPromptTest : public InProcessBrowserTest {
} }
content::MockDownloadItem download_; content::MockDownloadItem download_;
content::DownloadItem::Observer* download_observer_;
DownloadDangerPrompt* prompt_; DownloadDangerPrompt* prompt_;
DownloadDangerPrompt::Action expected_action_; DownloadDangerPrompt::Action expected_action_;
bool did_receive_callback_; bool did_receive_callback_;
...@@ -125,7 +116,7 @@ IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) { ...@@ -125,7 +116,7 @@ IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) {
// dialog should DISMISS itself. // dialog should DISMISS itself.
SetUpExpectations(DownloadDangerPrompt::DISMISS); SetUpExpectations(DownloadDangerPrompt::DISMISS);
EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false)); EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false));
download_observer()->OnDownloadUpdated(&download()); download().NotifyObserversDownloadUpdated();
VerifyExpectations(); VerifyExpectations();
// If the download is in a terminal state then the dialog should DISMISS // If the download is in a terminal state then the dialog should DISMISS
...@@ -133,7 +124,7 @@ IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) { ...@@ -133,7 +124,7 @@ IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) {
SetUpExpectations(DownloadDangerPrompt::DISMISS); SetUpExpectations(DownloadDangerPrompt::DISMISS);
EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true));
EXPECT_CALL(download(), IsDone()).WillOnce(Return(true)); EXPECT_CALL(download(), IsDone()).WillOnce(Return(true));
download_observer()->OnDownloadUpdated(&download()); download().NotifyObserversDownloadUpdated();
VerifyExpectations(); VerifyExpectations();
// If the download is dangerous and is not in a terminal state, don't dismiss // If the download is dangerous and is not in a terminal state, don't dismiss
...@@ -141,7 +132,7 @@ IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) { ...@@ -141,7 +132,7 @@ IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) {
SetUpExpectations(DownloadDangerPrompt::ACCEPT); SetUpExpectations(DownloadDangerPrompt::ACCEPT);
EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true));
EXPECT_CALL(download(), IsDone()).WillOnce(Return(false)); EXPECT_CALL(download(), IsDone()).WillOnce(Return(false));
download_observer()->OnDownloadUpdated(&download()); download().NotifyObserversDownloadUpdated();
SimulatePromptAction(DownloadDangerPrompt::ACCEPT); SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
VerifyExpectations(); VerifyExpectations();
......
...@@ -198,7 +198,6 @@ class DownloadHistoryTest : public testing::Test { ...@@ -198,7 +198,6 @@ class DownloadHistoryTest : public testing::Test {
manager_(new content::MockDownloadManager()), manager_(new content::MockDownloadManager()),
history_(NULL), history_(NULL),
manager_observer_(NULL), manager_observer_(NULL),
item_observer_(NULL),
download_created_index_(0) {} download_created_index_(0) {}
virtual ~DownloadHistoryTest() { virtual ~DownloadHistoryTest() {
STLDeleteElements(&items_); STLDeleteElements(&items_);
...@@ -220,15 +219,6 @@ class DownloadHistoryTest : public testing::Test { ...@@ -220,15 +219,6 @@ class DownloadHistoryTest : public testing::Test {
return manager_observer_; return manager_observer_;
} }
// Relies on the same object observing all download items.
void SetItemObserver(
content::DownloadItem::Observer* item_observer) {
item_observer_ = item_observer;
}
content::DownloadItem::Observer* item_observer() {
return item_observer_;
}
void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
CHECK(infos.get()); CHECK(infos.get());
...@@ -432,10 +422,6 @@ class DownloadHistoryTest : public testing::Test { ...@@ -432,10 +422,6 @@ class DownloadHistoryTest : public testing::Test {
Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE));
EXPECT_CALL(manager(), GetDownload(id)) EXPECT_CALL(manager(), GetDownload(id))
.WillRepeatedly(Return(&item(index))); .WillRepeatedly(Return(&item(index)));
EXPECT_CALL(item(index), AddObserver(_))
.WillOnce(WithArg<0>(
Invoke(this, &DownloadHistoryTest::SetItemObserver)));
EXPECT_CALL(item(index), RemoveObserver(_));
EXPECT_CALL(item(index), IsTemporary()).WillRepeatedly(Return(false)); EXPECT_CALL(item(index), IsTemporary()).WillRepeatedly(Return(false));
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
new extensions::DownloadedByExtension( new extensions::DownloadedByExtension(
...@@ -458,7 +444,6 @@ class DownloadHistoryTest : public testing::Test { ...@@ -458,7 +444,6 @@ class DownloadHistoryTest : public testing::Test {
FakeHistoryAdapter* history_; FakeHistoryAdapter* history_;
scoped_ptr<DownloadHistory> download_history_; scoped_ptr<DownloadHistory> download_history_;
content::DownloadManager::Observer* manager_observer_; content::DownloadManager::Observer* manager_observer_;
content::DownloadItem::Observer* item_observer_;
size_t download_created_index_; size_t download_created_index_;
DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest);
...@@ -486,14 +471,14 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { ...@@ -486,14 +471,14 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) {
// Pretend that something changed on the item. // Pretend that something changed on the item.
EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
info.opened = true; info.opened = true;
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// Pretend that the user removed the item. // Pretend that the user removed the item.
IdSet ids; IdSet ids;
ids.insert(info.id); ids.insert(info.id);
item_observer()->OnDownloadRemoved(&item(0)); item(0).NotifyObserversDownloadRemoved();
ExpectDownloadsRemoved(ids); ExpectDownloadsRemoved(ids);
} }
...@@ -517,14 +502,14 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { ...@@ -517,14 +502,14 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) {
// Pretend that something changed on the item. // Pretend that something changed on the item.
EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
info.opened = true; info.opened = true;
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// Pretend that the user removed the item. // Pretend that the user removed the item.
IdSet ids; IdSet ids;
ids.insert(info.id); ids.insert(info.id);
item_observer()->OnDownloadRemoved(&item(0)); item(0).NotifyObserversDownloadRemoved();
ExpectDownloadsRemoved(ids); ExpectDownloadsRemoved(ids);
} }
...@@ -550,72 +535,72 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) { ...@@ -550,72 +535,72 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) {
// current_path // current_path
EXPECT_CALL(item(0), GetFullPath()).WillRepeatedly(ReturnRefOfCopy(new_path)); EXPECT_CALL(item(0), GetFullPath()).WillRepeatedly(ReturnRefOfCopy(new_path));
info.current_path = new_path; info.current_path = new_path;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// target_path // target_path
EXPECT_CALL(item(0), GetTargetFilePath()) EXPECT_CALL(item(0), GetTargetFilePath())
.WillRepeatedly(ReturnRefOfCopy(new_path)); .WillRepeatedly(ReturnRefOfCopy(new_path));
info.target_path = new_path; info.target_path = new_path;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// end_time // end_time
EXPECT_CALL(item(0), GetEndTime()).WillRepeatedly(Return(new_time)); EXPECT_CALL(item(0), GetEndTime()).WillRepeatedly(Return(new_time));
info.end_time = new_time; info.end_time = new_time;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// received_bytes // received_bytes
EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(101)); EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(101));
info.received_bytes = 101; info.received_bytes = 101;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// total_bytes // total_bytes
EXPECT_CALL(item(0), GetTotalBytes()).WillRepeatedly(Return(102)); EXPECT_CALL(item(0), GetTotalBytes()).WillRepeatedly(Return(102));
info.total_bytes = 102; info.total_bytes = 102;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// etag // etag
EXPECT_CALL(item(0), GetETag()).WillRepeatedly(ReturnRefOfCopy(new_etag)); EXPECT_CALL(item(0), GetETag()).WillRepeatedly(ReturnRefOfCopy(new_etag));
info.etag = new_etag; info.etag = new_etag;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// last_modified // last_modified
EXPECT_CALL(item(0), GetLastModifiedTime()) EXPECT_CALL(item(0), GetLastModifiedTime())
.WillRepeatedly(ReturnRefOfCopy(new_last_modifed)); .WillRepeatedly(ReturnRefOfCopy(new_last_modifed));
info.last_modified = new_last_modifed; info.last_modified = new_last_modifed;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// state // state
EXPECT_CALL(item(0), GetState()) EXPECT_CALL(item(0), GetState())
.WillRepeatedly(Return(content::DownloadItem::INTERRUPTED)); .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED));
info.state = content::DownloadItem::INTERRUPTED; info.state = content::DownloadItem::INTERRUPTED;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// danger_type // danger_type
EXPECT_CALL(item(0), GetDangerType()) EXPECT_CALL(item(0), GetDangerType())
.WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT)); .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT));
info.danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; info.danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// interrupt_reason // interrupt_reason
EXPECT_CALL(item(0), GetLastReason()) EXPECT_CALL(item(0), GetLastReason())
.WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED)); .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED));
info.interrupt_reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; info.interrupt_reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
// opened // opened
EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
info.opened = true; info.opened = true;
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
} }
...@@ -642,7 +627,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { ...@@ -642,7 +627,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) {
// Pretend the item was marked temporary. DownloadHistory should remove it // Pretend the item was marked temporary. DownloadHistory should remove it
// from history and start ignoring it. // from history and start ignoring it.
EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(true)); EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(true));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
IdSet ids; IdSet ids;
ids.insert(info.id); ids.insert(info.id);
ExpectDownloadsRemoved(ids); ExpectDownloadsRemoved(ids);
...@@ -650,19 +635,19 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { ...@@ -650,19 +635,19 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) {
// Change something that would make DownloadHistory call UpdateDownload if the // Change something that would make DownloadHistory call UpdateDownload if the
// item weren't temporary. // item weren't temporary.
EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(4200)); EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(4200));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
ExpectNoDownloadUpdated(); ExpectNoDownloadUpdated();
// Changing a temporary item back to a non-temporary item should make // Changing a temporary item back to a non-temporary item should make
// DownloadHistory call CreateDownload. // DownloadHistory call CreateDownload.
EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(false)); EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(false));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
info.received_bytes = 4200; info.received_bytes = 4200;
ExpectDownloadCreated(info); ExpectDownloadCreated(info);
EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
info.received_bytes = 100; info.received_bytes = 100;
ExpectDownloadUpdated(info); ExpectDownloadUpdated(info);
} }
...@@ -690,7 +675,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) { ...@@ -690,7 +675,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) {
// Instead of calling RemoveDownloads() immediately, DownloadHistory should // Instead of calling RemoveDownloads() immediately, DownloadHistory should
// add the item's id to removed_while_adding_. Then, ItemAdded should // add the item's id to removed_while_adding_. Then, ItemAdded should
// immediately remove the item's record from history. // immediately remove the item's record from history.
item_observer()->OnDownloadRemoved(&item(0)); item(0).NotifyObserversDownloadRemoved();
EXPECT_CALL(manager(), GetDownload(item(0).GetId())) EXPECT_CALL(manager(), GetDownload(item(0).GetId()))
.WillRepeatedly(Return(static_cast<content::DownloadItem*>(NULL))); .WillRepeatedly(Return(static_cast<content::DownloadItem*>(NULL)));
ExpectNoDownloadsRemoved(); ExpectNoDownloadsRemoved();
...@@ -733,8 +718,8 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Multiple) { ...@@ -733,8 +718,8 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Multiple) {
IdSet ids; IdSet ids;
ids.insert(info0.id); ids.insert(info0.id);
ids.insert(info1.id); ids.insert(info1.id);
item_observer()->OnDownloadRemoved(&item(0)); item(0).NotifyObserversDownloadRemoved();
item_observer()->OnDownloadRemoved(&item(1)); item(1).NotifyObserversDownloadRemoved();
ExpectDownloadsRemoved(ids); ExpectDownloadsRemoved(ids);
} }
...@@ -757,7 +742,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { ...@@ -757,7 +742,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) {
EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0)));
EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
info.received_bytes = 100; info.received_bytes = 100;
ExpectDownloadCreated(info); ExpectDownloadCreated(info);
EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
...@@ -785,7 +770,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) { ...@@ -785,7 +770,7 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) {
// Pretend that something changed on the item. // Pretend that something changed on the item.
EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
item_observer()->OnDownloadUpdated(&item(0)); item(0).NotifyObserversDownloadUpdated();
FinishCreateDownload(); FinishCreateDownload();
EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
......
...@@ -54,10 +54,6 @@ class DownloadStatusUpdaterTest : public testing::Test { ...@@ -54,10 +54,6 @@ class DownloadStatusUpdaterTest : public testing::Test {
virtual ~DownloadStatusUpdaterTest() { virtual ~DownloadStatusUpdaterTest() {
for (size_t mgr_idx = 0; mgr_idx < managers_.size(); ++mgr_idx) { for (size_t mgr_idx = 0; mgr_idx < managers_.size(); ++mgr_idx) {
EXPECT_CALL(*Manager(mgr_idx), RemoveObserver(_)); EXPECT_CALL(*Manager(mgr_idx), RemoveObserver(_));
for (size_t item_idx = 0; item_idx < manager_items_[mgr_idx].size();
++item_idx) {
EXPECT_CALL(*Item(mgr_idx, item_idx), RemoveObserver(_));
}
} }
delete updater_; delete updater_;
...@@ -116,8 +112,6 @@ class DownloadStatusUpdaterTest : public testing::Test { ...@@ -116,8 +112,6 @@ class DownloadStatusUpdaterTest : public testing::Test {
i < in_progress_count ? content::DownloadItem::IN_PROGRESS i < in_progress_count ? content::DownloadItem::IN_PROGRESS
: content::DownloadItem::CANCELLED; : content::DownloadItem::CANCELLED;
EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(state)); EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(state));
EXPECT_CALL(*item, AddObserver(_))
.WillOnce(Return());
manager_items_[manager_index].push_back(item); manager_items_[manager_index].push_back(item);
} }
EXPECT_CALL(*manager, GetAllDownloads(_)) EXPECT_CALL(*manager, GetAllDownloads(_))
......
...@@ -53,10 +53,6 @@ class DownloadUIControllerTest : public testing::Test { ...@@ -53,10 +53,6 @@ class DownloadUIControllerTest : public testing::Test {
// testing::Test // testing::Test
virtual void SetUp() OVERRIDE; virtual void SetUp() OVERRIDE;
// Returns a MockDownloadItem that has AddObserver and RemoveObserver
// expectations set up to store the observer in |item_observer_|.
scoped_ptr<MockDownloadItem> GetMockDownload();
// Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned
// delegate results in the DownloadItem* being stored in |received_item_|. // delegate results in the DownloadItem* being stored in |received_item_|.
scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); scoped_ptr<DownloadUIController::Delegate> GetTestDelegate();
...@@ -65,13 +61,11 @@ class DownloadUIControllerTest : public testing::Test { ...@@ -65,13 +61,11 @@ class DownloadUIControllerTest : public testing::Test {
content::DownloadManager::Observer* manager_observer() { content::DownloadManager::Observer* manager_observer() {
return manager_observer_; return manager_observer_;
} }
content::DownloadItem::Observer* item_observer() { return item_observer_; }
content::DownloadItem* received_item() { return received_item_; } content::DownloadItem* received_item() { return received_item_; }
private: private:
scoped_ptr<MockDownloadManager> manager_; scoped_ptr<MockDownloadManager> manager_;
content::DownloadManager::Observer* manager_observer_; content::DownloadManager::Observer* manager_observer_;
content::DownloadItem::Observer* item_observer_;
content::DownloadItem* received_item_; content::DownloadItem* received_item_;
base::WeakPtrFactory<content::DownloadItem*> receiver_factory_; base::WeakPtrFactory<content::DownloadItem*> receiver_factory_;
...@@ -79,7 +73,6 @@ class DownloadUIControllerTest : public testing::Test { ...@@ -79,7 +73,6 @@ class DownloadUIControllerTest : public testing::Test {
DownloadUIControllerTest::DownloadUIControllerTest() DownloadUIControllerTest::DownloadUIControllerTest()
: manager_observer_(NULL), : manager_observer_(NULL),
item_observer_(NULL),
received_item_(NULL), received_item_(NULL),
receiver_factory_(&received_item_) { receiver_factory_(&received_item_) {
} }
...@@ -94,17 +87,6 @@ void DownloadUIControllerTest::SetUp() { ...@@ -94,17 +87,6 @@ void DownloadUIControllerTest::SetUp() {
EXPECT_CALL(*manager_, GetAllDownloads(_)); EXPECT_CALL(*manager_, GetAllDownloads(_));
} }
scoped_ptr<MockDownloadItem> DownloadUIControllerTest::GetMockDownload() {
scoped_ptr<MockDownloadItem> item(
new testing::StrictMock<MockDownloadItem>());
EXPECT_CALL(*item, AddObserver(_))
.WillOnce(SaveArg<0>(&item_observer_));
EXPECT_CALL(*item, RemoveObserver(_))
.WillOnce(Assign(&item_observer_,
static_cast<content::DownloadItem::Observer*>(NULL)));
return item.Pass();
}
scoped_ptr<DownloadUIController::Delegate> scoped_ptr<DownloadUIController::Delegate>
DownloadUIControllerTest::GetTestDelegate() { DownloadUIControllerTest::GetTestDelegate() {
scoped_ptr<DownloadUIController::Delegate> delegate( scoped_ptr<DownloadUIController::Delegate> delegate(
...@@ -116,7 +98,7 @@ DownloadUIControllerTest::GetTestDelegate() { ...@@ -116,7 +98,7 @@ DownloadUIControllerTest::GetTestDelegate() {
// presented to the UI when GetTargetFilePath() returns a non-empty path. // presented to the UI when GetTargetFilePath() returns a non-empty path.
// I.e. once the download target has been determined. // I.e. once the download target has been determined.
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
scoped_ptr<MockDownloadItem> item = GetMockDownload(); scoped_ptr<MockDownloadItem> item(new MockDownloadItem);
DownloadUIController controller(manager(), GetTestDelegate()); DownloadUIController controller(manager(), GetTestDelegate());
EXPECT_CALL(*item, GetTargetFilePath()) EXPECT_CALL(*item, GetTargetFilePath())
.WillOnce(ReturnRefOfCopy(base::FilePath())); .WillOnce(ReturnRefOfCopy(base::FilePath()));
...@@ -129,12 +111,11 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { ...@@ -129,12 +111,11 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
// The destination for the download hasn't been determined yet. It should not // The destination for the download hasn't been determined yet. It should not
// be displayed. // be displayed.
EXPECT_FALSE(received_item()); EXPECT_FALSE(received_item());
ASSERT_TRUE(item_observer());
// Once the destination has been determined, then it should be displayed. // Once the destination has been determined, then it should be displayed.
EXPECT_CALL(*item, GetTargetFilePath()) EXPECT_CALL(*item, GetTargetFilePath())
.WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
item_observer()->OnDownloadUpdated(item.get()); item->NotifyObserversDownloadUpdated();
EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item());
} }
...@@ -143,7 +124,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { ...@@ -143,7 +124,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
// state should be displayed in the UI immediately without requiring an // state should be displayed in the UI immediately without requiring an
// additional OnDownloadUpdated() notification. // additional OnDownloadUpdated() notification.
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
scoped_ptr<MockDownloadItem> item = GetMockDownload(); scoped_ptr<MockDownloadItem> item(new MockDownloadItem);
DownloadUIController controller(manager(), GetTestDelegate()); DownloadUIController controller(manager(), GetTestDelegate());
EXPECT_CALL(*item, GetTargetFilePath()) EXPECT_CALL(*item, GetTargetFilePath())
.WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
...@@ -158,7 +139,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { ...@@ -158,7 +139,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
// History downloads (downloads that are not in IN_PROGRESS on create) should // History downloads (downloads that are not in IN_PROGRESS on create) should
// not be displayed on the shelf. // not be displayed on the shelf.
TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) {
scoped_ptr<MockDownloadItem> item = GetMockDownload(); scoped_ptr<MockDownloadItem> item(new MockDownloadItem);
DownloadUIController controller(manager(), GetTestDelegate()); DownloadUIController controller(manager(), GetTestDelegate());
EXPECT_CALL(*item, GetState()) EXPECT_CALL(*item, GetState())
.WillRepeatedly(Return(content::DownloadItem::COMPLETE)); .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
...@@ -167,7 +148,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { ...@@ -167,7 +148,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) {
manager_observer()->OnDownloadCreated(manager(), item.get()); manager_observer()->OnDownloadCreated(manager(), item.get());
EXPECT_FALSE(received_item()); EXPECT_FALSE(received_item());
item_observer()->OnDownloadUpdated(item.get()); item->NotifyObserversDownloadUpdated();
EXPECT_FALSE(received_item()); EXPECT_FALSE(received_item());
} }
......
...@@ -25,19 +25,15 @@ class PluginInstallerTest : public ChromeRenderViewHostTestHarness { ...@@ -25,19 +25,15 @@ class PluginInstallerTest : public ChromeRenderViewHostTestHarness {
virtual void TearDown() OVERRIDE; virtual void TearDown() OVERRIDE;
PluginInstaller* installer() { return installer_.get(); } PluginInstaller* installer() { return installer_.get(); }
content::DownloadItem::Observer* last_download_item_observer() {
return last_download_item_observer_;
}
scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem(); scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem();
private: private:
scoped_ptr<PluginInstaller> installer_; scoped_ptr<PluginInstaller> installer_;
content::DownloadItem::Observer* last_download_item_observer_;
}; };
PluginInstallerTest::PluginInstallerTest() PluginInstallerTest::PluginInstallerTest() {
: last_download_item_observer_(NULL) {} }
void PluginInstallerTest::SetUp() { void PluginInstallerTest::SetUp() {
content::RenderViewHostTestHarness::SetUp(); content::RenderViewHostTestHarness::SetUp();
...@@ -53,11 +49,6 @@ scoped_ptr<content::MockDownloadItem> ...@@ -53,11 +49,6 @@ scoped_ptr<content::MockDownloadItem>
PluginInstallerTest::CreateMockDownloadItem() { PluginInstallerTest::CreateMockDownloadItem() {
scoped_ptr<content::MockDownloadItem> mock_download_item( scoped_ptr<content::MockDownloadItem> mock_download_item(
new testing::StrictMock<content::MockDownloadItem>()); new testing::StrictMock<content::MockDownloadItem>());
ON_CALL(*mock_download_item, AddObserver(_))
.WillByDefault(testing::SaveArg<0>(&last_download_item_observer_));
ON_CALL(*mock_download_item, RemoveObserver(_)).WillByDefault(
testing::Assign(&last_download_item_observer_,
static_cast<content::DownloadItem::Observer*>(NULL)));
ON_CALL(*mock_download_item, GetState()) ON_CALL(*mock_download_item, GetState())
.WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS)); .WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS));
return mock_download_item.Pass(); return mock_download_item.Pass();
...@@ -119,7 +110,6 @@ TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) { ...@@ -119,7 +110,6 @@ TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) {
InvokeOnStartedCallback(download_item.get(), InvokeOnStartedCallback(download_item.get(),
content::DOWNLOAD_INTERRUPT_REASON_NONE), content::DOWNLOAD_INTERRUPT_REASON_NONE),
InvokeClosure(run_loop.QuitClosure()))); InvokeClosure(run_loop.QuitClosure())));
EXPECT_CALL(*download_item, AddObserver(_));
EXPECT_CALL(*download_item, SetOpenWhenComplete(_)); EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
TestPluginInstallerObserver installer_observer(installer()); TestPluginInstallerObserver installer_observer(installer());
...@@ -127,14 +117,12 @@ TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) { ...@@ -127,14 +117,12 @@ TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) {
GURL(kTestUrl), web_contents(), &mock_download_manager); GURL(kTestUrl), web_contents(), &mock_download_manager);
run_loop.Run(); run_loop.Run();
ASSERT_TRUE(last_download_item_observer());
EXPECT_TRUE(installer_observer.download_started()); EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished()); EXPECT_FALSE(installer_observer.download_finished());
EXPECT_CALL(*download_item, GetState()) EXPECT_CALL(*download_item, GetState())
.WillOnce(testing::Return(content::DownloadItem::COMPLETE)); .WillOnce(testing::Return(content::DownloadItem::COMPLETE));
EXPECT_CALL(*download_item, RemoveObserver(_)); download_item->NotifyObserversDownloadUpdated();
last_download_item_observer()->OnDownloadUpdated(download_item.get());
EXPECT_TRUE(installer_observer.download_finished()); EXPECT_TRUE(installer_observer.download_finished());
} }
...@@ -159,7 +147,6 @@ TEST_F(PluginInstallerTest, StartInstalling_FailedStart) { ...@@ -159,7 +147,6 @@ TEST_F(PluginInstallerTest, StartInstalling_FailedStart) {
GURL(kTestUrl), web_contents(), &mock_download_manager); GURL(kTestUrl), web_contents(), &mock_download_manager);
run_loop.Run(); run_loop.Run();
EXPECT_FALSE(last_download_item_observer());
EXPECT_TRUE(installer_observer.download_started()); EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished()); EXPECT_FALSE(installer_observer.download_finished());
EXPECT_EQ("Error 20: NETWORK_FAILED", installer_observer.download_error()); EXPECT_EQ("Error 20: NETWORK_FAILED", installer_observer.download_error());
...@@ -180,7 +167,6 @@ TEST_F(PluginInstallerTest, StartInstalling_Interrupted) { ...@@ -180,7 +167,6 @@ TEST_F(PluginInstallerTest, StartInstalling_Interrupted) {
InvokeOnStartedCallback(download_item.get(), InvokeOnStartedCallback(download_item.get(),
content::DOWNLOAD_INTERRUPT_REASON_NONE), content::DOWNLOAD_INTERRUPT_REASON_NONE),
InvokeClosure(run_loop.QuitClosure()))); InvokeClosure(run_loop.QuitClosure())));
EXPECT_CALL(*download_item, AddObserver(_));
EXPECT_CALL(*download_item, SetOpenWhenComplete(_)); EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
TestPluginInstallerObserver installer_observer(installer()); TestPluginInstallerObserver installer_observer(installer());
...@@ -188,18 +174,15 @@ TEST_F(PluginInstallerTest, StartInstalling_Interrupted) { ...@@ -188,18 +174,15 @@ TEST_F(PluginInstallerTest, StartInstalling_Interrupted) {
GURL(kTestUrl), web_contents(), &mock_download_manager); GURL(kTestUrl), web_contents(), &mock_download_manager);
run_loop.Run(); run_loop.Run();
ASSERT_TRUE(last_download_item_observer());
EXPECT_TRUE(installer_observer.download_started()); EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished()); EXPECT_FALSE(installer_observer.download_finished());
EXPECT_CALL(*download_item, GetState()) EXPECT_CALL(*download_item, GetState())
.WillOnce(testing::Return(content::DownloadItem::INTERRUPTED)); .WillOnce(testing::Return(content::DownloadItem::INTERRUPTED));
EXPECT_CALL(*download_item, RemoveObserver(_));
EXPECT_CALL(*download_item, GetLastReason()).WillOnce( EXPECT_CALL(*download_item, GetLastReason()).WillOnce(
testing::Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); testing::Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
last_download_item_observer()->OnDownloadUpdated(download_item.get()); download_item->NotifyObserversDownloadUpdated();
EXPECT_FALSE(last_download_item_observer());
EXPECT_TRUE(installer_observer.download_started()); EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished()); EXPECT_FALSE(installer_observer.download_finished());
EXPECT_EQ("NETWORK_FAILED", installer_observer.download_error()); EXPECT_EQ("NETWORK_FAILED", installer_observer.download_error());
......
...@@ -365,8 +365,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { ...@@ -365,8 +365,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) {
GURL referrer("http://www.google.com/"); GURL referrer("http://www.google.com/");
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_));
EXPECT_CALL(item, RemoveObserver(_));
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -383,8 +381,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { ...@@ -383,8 +381,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) {
Mock::VerifyAndClearExpectations(&item); Mock::VerifyAndClearExpectations(&item);
url_chain.push_back(GURL("file://www.google.com/")); url_chain.push_back(GURL("file://www.google.com/"));
EXPECT_CALL(item, AddObserver(_));
EXPECT_CALL(item, RemoveObserver(_));
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -417,8 +413,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { ...@@ -417,8 +413,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) {
GURL referrer; GURL referrer;
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(4);
EXPECT_CALL(item, RemoveObserver(_)).Times(4);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -509,8 +503,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { ...@@ -509,8 +503,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) {
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_));
EXPECT_CALL(item, RemoveObserver(_));
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -555,8 +547,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) { ...@@ -555,8 +547,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) {
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(6);
EXPECT_CALL(item, RemoveObserver(_)).Times(6);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -705,8 +695,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) { ...@@ -705,8 +695,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadHTTPS) {
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(1);
EXPECT_CALL(item, RemoveObserver(_)).Times(1);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_exe));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -760,8 +748,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadZip) { ...@@ -760,8 +748,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadZip) {
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(3);
EXPECT_CALL(item, RemoveObserver(_)).Times(3);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_zip)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_zip));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -844,8 +830,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadCorruptZip) { ...@@ -844,8 +830,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadCorruptZip) {
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(1);
EXPECT_CALL(item, RemoveObserver(_)).Times(1);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_zip)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_zip));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -893,8 +877,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) { ...@@ -893,8 +877,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientCrxDownloadSuccess) {
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(1);
EXPECT_CALL(item, RemoveObserver(_)).Times(1);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(a_tmp));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_crx)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(a_crx));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -937,8 +919,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) { ...@@ -937,8 +919,6 @@ TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) {
std::string remote_address = "10.11.12.13"; std::string remote_address = "10.11.12.13";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(1);
EXPECT_CALL(item, RemoveObserver(_)).Times(1);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -1026,8 +1006,6 @@ TEST_F(DownloadProtectionServiceTest, ...@@ -1026,8 +1006,6 @@ TEST_F(DownloadProtectionServiceTest,
std::string remote_address = "10.11.12.13"; std::string remote_address = "10.11.12.13";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(1);
EXPECT_CALL(item, RemoveObserver(_)).Times(1);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -1110,8 +1088,6 @@ TEST_F(DownloadProtectionServiceTest, ...@@ -1110,8 +1088,6 @@ TEST_F(DownloadProtectionServiceTest,
std::string remote_address = "10.11.12.13"; std::string remote_address = "10.11.12.13";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(2);
EXPECT_CALL(item, RemoveObserver(_)).Times(2);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -1348,8 +1324,6 @@ TEST_F(DownloadProtectionServiceTest, TestDownloadRequestTimeout) { ...@@ -1348,8 +1324,6 @@ TEST_F(DownloadProtectionServiceTest, TestDownloadRequestTimeout) {
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).Times(1);
EXPECT_CALL(item, RemoveObserver(_)).Times(1);
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path)); EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
...@@ -1391,39 +1365,36 @@ TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) { ...@@ -1391,39 +1365,36 @@ TEST_F(DownloadProtectionServiceTest, TestDownloadItemDestroyed) {
base::FilePath final_path(FILE_PATH_LITERAL("a.exe")); base::FilePath final_path(FILE_PATH_LITERAL("a.exe"));
std::string hash = "hash"; std::string hash = "hash";
content::MockDownloadItem item; {
content::DownloadItem::Observer* observer = NULL; content::MockDownloadItem item;
EXPECT_CALL(item, AddObserver(_)).WillOnce(SaveArg<0>(&observer)); EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path));
EXPECT_CALL(item, RemoveObserver(_)).WillOnce(Assign( EXPECT_CALL(item, GetTargetFilePath())
&observer, static_cast<content::DownloadItem::Observer*>(NULL))); .WillRepeatedly(ReturnRef(final_path));
EXPECT_CALL(item, GetFullPath()).WillRepeatedly(ReturnRef(tmp_path)); EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain));
EXPECT_CALL(item, GetTargetFilePath()).WillRepeatedly(ReturnRef(final_path)); EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer));
EXPECT_CALL(item, GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
EXPECT_CALL(item, GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); EXPECT_CALL(item, GetTabReferrerUrl())
EXPECT_CALL(item, GetTabUrl()).WillRepeatedly(ReturnRef(GURL::EmptyGURL())); .WillRepeatedly(ReturnRef(GURL::EmptyGURL()));
EXPECT_CALL(item, GetTabReferrerUrl()) EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash));
.WillRepeatedly(ReturnRef(GURL::EmptyGURL())); EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100));
EXPECT_CALL(item, GetHash()).WillRepeatedly(ReturnRef(hash)); EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
EXPECT_CALL(item, GetReceivedBytes()).WillRepeatedly(Return(100)); EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return(""));
EXPECT_CALL(item, HasUserGesture()).WillRepeatedly(Return(true));
EXPECT_CALL(item, GetRemoteAddress()).WillRepeatedly(Return("")); EXPECT_CALL(*sb_service_->mock_database_manager(),
MatchDownloadWhitelistUrl(_))
EXPECT_CALL(*sb_service_->mock_database_manager(), .WillRepeatedly(Return(false));
MatchDownloadWhitelistUrl(_)) EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path, _));
.WillRepeatedly(Return(false)); EXPECT_CALL(*binary_feature_extractor_.get(),
EXPECT_CALL(*binary_feature_extractor_.get(), CheckSignature(tmp_path, _)); ExtractImageHeaders(tmp_path, _));
EXPECT_CALL(*binary_feature_extractor_.get(),
ExtractImageHeaders(tmp_path, _));
download_service_->CheckClientDownload(
&item,
base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback,
base::Unretained(this)));
ASSERT_TRUE(observer != NULL); download_service_->CheckClientDownload(
observer->OnDownloadDestroyed(&item); &item,
base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback,
base::Unretained(this)));
// MockDownloadItem going out of scope triggers the OnDownloadDestroyed
// notification.
}
EXPECT_TRUE(observer == NULL);
EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE)); EXPECT_TRUE(IsResult(DownloadProtectionService::SAFE));
} }
......
...@@ -7,6 +7,29 @@ ...@@ -7,6 +7,29 @@
namespace content { namespace content {
MockDownloadItem::MockDownloadItem() {} MockDownloadItem::MockDownloadItem() {}
MockDownloadItem::~MockDownloadItem() {}
MockDownloadItem::~MockDownloadItem() {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadDestroyed(this));
}
void MockDownloadItem::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void MockDownloadItem::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void MockDownloadItem::NotifyObserversDownloadOpened() {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this));
}
void MockDownloadItem::NotifyObserversDownloadRemoved() {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadRemoved(this));
}
void MockDownloadItem::NotifyObserversDownloadUpdated() {
FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
}
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CONTENT_PUBLIC_TEST_MOCK_DOWNLOAD_ITEM_H_ #define CONTENT_PUBLIC_TEST_MOCK_DOWNLOAD_ITEM_H_
#include "base/callback.h" #include "base/callback.h"
#include "base/observer_list.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "content/public/browser/download_interrupt_reasons.h" #include "content/public/browser/download_interrupt_reasons.h"
#include "content/public/browser/download_item.h" #include "content/public/browser/download_item.h"
...@@ -19,8 +20,20 @@ class MockDownloadItem : public DownloadItem { ...@@ -19,8 +20,20 @@ class MockDownloadItem : public DownloadItem {
public: public:
MockDownloadItem(); MockDownloadItem();
virtual ~MockDownloadItem(); virtual ~MockDownloadItem();
MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*));
MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*)); // Management of observer lists is common in tests. So Add/RemoveObserver
// methods are not mocks. In addition, any registered observers will receive a
// OnDownloadDestroyed() notification when the mock is destroyed.
virtual void AddObserver(Observer* observer) OVERRIDE;
virtual void RemoveObserver(Observer* observer) OVERRIDE;
// Dispatches an OnDownloadOpened() notification to observers.
void NotifyObserversDownloadOpened();
// Dispatches an OnDownloadRemoved() notification to observers.
void NotifyObserversDownloadRemoved();
// Dispatches an OnDownloadUpdated() notification to observers.
void NotifyObserversDownloadUpdated();
MOCK_METHOD0(UpdateObservers, void()); MOCK_METHOD0(UpdateObservers, void());
MOCK_METHOD0(ValidateDangerousDownload, void()); MOCK_METHOD0(ValidateDangerousDownload, void());
MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&)); MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&));
...@@ -87,6 +100,9 @@ class MockDownloadItem : public DownloadItem { ...@@ -87,6 +100,9 @@ class MockDownloadItem : public DownloadItem {
MOCK_METHOD1(SetOpened, void(bool)); MOCK_METHOD1(SetOpened, void(bool));
MOCK_METHOD1(SetDisplayName, void(const base::FilePath&)); MOCK_METHOD1(SetDisplayName, void(const base::FilePath&));
MOCK_CONST_METHOD1(DebugString, std::string(bool)); MOCK_CONST_METHOD1(DebugString, std::string(bool));
private:
ObserverList<Observer> observers_;
}; };
} // namespace content } // namespace content
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment