Chromium Code Reviews| Index: chrome/browser/download/download_danger_prompt_browsertest.cc |
| diff --git a/chrome/browser/download/download_danger_prompt_browsertest.cc b/chrome/browser/download/download_danger_prompt_browsertest.cc |
| index 2b33e0e7e29eed67d4d2c660cc8ffa0e157ee051..c825102afe3cd750f19c379d0144631c262aa127 100644 |
| --- a/chrome/browser/download/download_danger_prompt_browsertest.cc |
| +++ b/chrome/browser/download/download_danger_prompt_browsertest.cc |
| @@ -4,12 +4,15 @@ |
| #include "base/bind.h" |
| #include "base/files/file_path.h" |
| +#include "base/memory/weak_ptr.h" |
| #include "chrome/browser/download/download_danger_prompt.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/browser_commands.h" |
| #include "chrome/browser/ui/browser_tabstrip.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/common/safe_browsing/csd.pb.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| #include "content/public/test/mock_download_item.h" |
| @@ -23,13 +26,58 @@ using ::testing::Eq; |
| using ::testing::Return; |
| using ::testing::ReturnRef; |
| using ::testing::SaveArg; |
| +using safe_browsing::ClientSafeBrowsingReportRequest; |
| +using safe_browsing::SafeBrowsingService; |
| + |
| +static const char* kTestDownloadUrl = "http://evildownload.com"; |
| + |
| +class FakeSafeBrowsingService : public SafeBrowsingService { |
| + public: |
| + FakeSafeBrowsingService() {} |
| + |
| + void SendDownloadRecoveryReport(const std::string& report) override { |
| + report_ = report; |
| + } |
| + |
| + std::string GetDownloadRecoveryReport() { return report_; } |
| + |
| + protected: |
| + ~FakeSafeBrowsingService() override {} |
| + |
| + private: |
| + std::string report_; |
| +}; |
| + |
| +// Factory that creates FakeSafeBrowsingService instances. |
| +class TestSafeBrowsingServiceFactory |
| + : public safe_browsing::SafeBrowsingServiceFactory { |
| + public: |
| + TestSafeBrowsingServiceFactory() : fake_safe_browsing_service_(NULL) {} |
| + ~TestSafeBrowsingServiceFactory() override {} |
| + |
| + SafeBrowsingService* CreateSafeBrowsingService() override { |
| + fake_safe_browsing_service_ = new FakeSafeBrowsingService(); |
| + return fake_safe_browsing_service_; |
| + } |
| + |
| + FakeSafeBrowsingService* fake_safe_browsing_service() { |
| + return fake_safe_browsing_service_; |
| + } |
| + |
| + private: |
| + FakeSafeBrowsingService* fake_safe_browsing_service_; |
|
Nathan Parker
2015/11/23 21:08:57
scoped_ptr<> ?
Jialiu Lin
2015/11/23 23:35:13
Good point. Changed to scoped_refptr<> instead sin
|
| +}; |
| class DownloadDangerPromptTest : public InProcessBrowserTest { |
| public: |
| DownloadDangerPromptTest() |
| - : prompt_(NULL), |
| - expected_action_(DownloadDangerPrompt::CANCEL), |
| - did_receive_callback_(false) { |
| + : prompt_(NULL), |
| + expected_action_(DownloadDangerPrompt::CANCEL), |
| + did_receive_callback_(false), |
| + safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), |
| + report_sent_(false) { |
| + safe_browsing::SafeBrowsingService::RegisterFactory( |
| + safe_browsing_factory_.get()); |
| } |
| ~DownloadDangerPromptTest() override {} |
| @@ -49,7 +97,10 @@ class DownloadDangerPromptTest : public InProcessBrowserTest { |
| did_receive_callback_ = false; |
| expected_action_ = expected_action; |
| SetUpDownloadItemExpectations(); |
| + SetUpSafeBrowsingReportExpectations(expected_action == |
| + DownloadDangerPrompt::ACCEPT); |
| CreatePrompt(); |
| + report_sent_ = false; |
| } |
| void VerifyExpectations() { |
| @@ -59,10 +110,16 @@ class DownloadDangerPromptTest : public InProcessBrowserTest { |
| EXPECT_TRUE(did_receive_callback_); |
| EXPECT_FALSE(prompt_); |
| testing::Mock::VerifyAndClearExpectations(&download_); |
| + if (report_sent_) { |
| + EXPECT_EQ(expected_serialized_report_, |
| + safe_browsing_factory_->fake_safe_browsing_service() |
| + ->GetDownloadRecoveryReport()); |
| + } |
| } |
| void SimulatePromptAction(DownloadDangerPrompt::Action action) { |
| prompt_->InvokeActionForTesting(action); |
| + report_sent_ = true; |
| } |
| content::MockDownloadItem& download() { return download_; } |
| @@ -77,6 +134,15 @@ class DownloadDangerPromptTest : public InProcessBrowserTest { |
| .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); |
| } |
| + void SetUpSafeBrowsingReportExpectations(bool did_proceed) { |
| + ClientSafeBrowsingReportRequest expected_report; |
| + expected_report.set_url(GURL(kTestDownloadUrl).spec()); |
| + expected_report.set_type( |
| + ClientSafeBrowsingReportRequest::MALICIOUS_DOWNLOAD_RECOVERY); |
| + expected_report.set_did_proceed(did_proceed); |
| + expected_report.SerializeToString(&expected_serialized_report_); |
| + } |
| + |
| void CreatePrompt() { |
| prompt_ = DownloadDangerPrompt::Create( |
| &download_, |
| @@ -97,6 +163,9 @@ class DownloadDangerPromptTest : public InProcessBrowserTest { |
| DownloadDangerPrompt* prompt_; |
| DownloadDangerPrompt::Action expected_action_; |
| bool did_receive_callback_; |
| + scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_; |
| + std::string expected_serialized_report_; |
| + bool report_sent_; |
| DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
| }; |
| @@ -109,7 +178,8 @@ class DownloadDangerPromptTest : public InProcessBrowserTest { |
| #endif |
| IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { |
| // ExperienceSampling: Set default actions for DownloadItem methods we need. |
| - ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| + GURL download_url(kTestDownloadUrl); |
| + ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); |
| ON_CALL(download(), GetReferrerUrl()) |
| .WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| ON_CALL(download(), GetBrowserContext()) |