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

Unified Diff: chrome/browser/download/download_danger_prompt_browsertest.cc

Issue 1436273002: Send safe browsing ThreatDetails to track download CTR when user tries to recover blocked downloads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
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..a0cde01e4622eff7a4b2dbe244b3e4389b849028 100644
--- a/chrome/browser/download/download_danger_prompt_browsertest.cc
+++ b/chrome/browser/download/download_danger_prompt_browsertest.cc
@@ -6,10 +6,13 @@
#include "base/files/file_path.h"
#include "chrome/browser/download/download_danger_prompt.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/safe_browsing/database_manager.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,17 +26,71 @@ using ::testing::Eq;
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::SaveArg;
+using safe_browsing::ClientSafeBrowsingReportRequest;
+using safe_browsing::SafeBrowsingService;
+
+const char kTestDownloadUrl[] = "http://evildownload.com";
+
+class FakeSafeBrowsingService : public SafeBrowsingService {
+ public:
+ FakeSafeBrowsingService() {}
+
+ void SendDownloadRecoveryReport(const std::string& report) override {
+ report_ = report;
+ }
+
+ std::string GetDownloadRecoveryReport() const { 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_(nullptr) {}
+ ~TestSafeBrowsingServiceFactory() override {}
+
+ SafeBrowsingService* CreateSafeBrowsingService() override {
+ if (!fake_safe_browsing_service_) {
+ fake_safe_browsing_service_ = new FakeSafeBrowsingService();
+ }
+ return fake_safe_browsing_service_.get();
+ }
+
+ scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service() {
+ return fake_safe_browsing_service_;
+ }
+
+ private:
+ scoped_refptr<FakeSafeBrowsingService> fake_safe_browsing_service_;
+};
class DownloadDangerPromptTest : public InProcessBrowserTest {
public:
DownloadDangerPromptTest()
- : prompt_(NULL),
- expected_action_(DownloadDangerPrompt::CANCEL),
- did_receive_callback_(false) {
- }
+ : prompt_(nullptr),
+ expected_action_(DownloadDangerPrompt::CANCEL),
+ did_receive_callback_(false),
+ test_safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
+ report_sent_(false) {}
~DownloadDangerPromptTest() override {}
+ void SetUp() override {
+ SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get());
+ InProcessBrowserTest::SetUp();
+ }
+
+ void TearDown() override {
+ SafeBrowsingService::RegisterFactory(nullptr);
+ InProcessBrowserTest::TearDown();
+ }
+
// Opens a new tab and waits for navigations to finish. If there are pending
// navigations, the constrained prompt might be dismissed when the navigation
// completes.
@@ -49,7 +106,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 +119,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_,
+ test_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 +143,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_,
@@ -90,13 +165,16 @@ class DownloadDangerPromptTest : public InProcessBrowserTest {
EXPECT_FALSE(did_receive_callback_);
EXPECT_EQ(expected_action_, action);
did_receive_callback_ = true;
- prompt_ = NULL;
+ prompt_ = nullptr;
}
content::MockDownloadItem download_;
DownloadDangerPrompt* prompt_;
DownloadDangerPrompt::Action expected_action_;
bool did_receive_callback_;
+ scoped_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_;
+ std::string expected_serialized_report_;
+ bool report_sent_;
DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest);
};
@@ -109,7 +187,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())
« no previous file with comments | « chrome/browser/download/download_danger_prompt.cc ('k') | chrome/browser/safe_browsing/safe_browsing_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698