OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/safe_browsing/notification_image_reporter.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/test/scoped_feature_list.h" | |
11 #include "chrome/browser/safe_browsing/mock_permission_report_sender.h" | |
12 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | |
13 #include "chrome/common/safe_browsing/csd.pb.h" | |
14 #include "chrome/test/base/testing_browser_process.h" | |
15 #include "chrome/test/base/testing_profile.h" | |
16 #include "components/safe_browsing_db/safe_browsing_prefs.h" | |
17 #include "components/safe_browsing_db/test_database_manager.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "content/public/test/test_browser_thread_bundle.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 #include "third_party/skia/include/core/SkBitmap.h" | |
22 #include "third_party/skia/include/core/SkColor.h" | |
23 #include "url/gurl.h" | |
24 | |
25 using content::BrowserThread; | |
26 | |
27 namespace safe_browsing { | |
28 | |
29 namespace { | |
30 | |
31 const char kReportingUploadUrl[] = | |
Peter Beverloo
2017/01/13 00:48:35
nit: comment on the source of the duplication. (Or
johnme
2017/01/13 17:12:26
Done (static).
| |
32 "https://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | |
33 "chrome-notification-image"; | |
34 | |
35 class TestingNotificationImageReporter : public NotificationImageReporter { | |
36 public: | |
37 TestingNotificationImageReporter( | |
Peter Beverloo
2017/01/13 00:48:35
nit: explicit
johnme
2017/01/13 17:12:26
Done.
| |
38 std::unique_ptr<net::ReportSender> report_sender) | |
39 : NotificationImageReporter(std::move(report_sender)) {} | |
40 | |
41 protected: | |
42 double GetReportChance() override { return 1.0; } | |
43 | |
44 private: | |
45 ~TestingNotificationImageReporter() override {} | |
46 }; | |
47 | |
48 class FakeSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { | |
49 public: | |
50 bool MatchCsdWhitelistUrl(const GURL& url) override { return match_all_; } | |
51 bool match_all_ = false; | |
Peter Beverloo
2017/01/13 00:48:35
nit: unused?
johnme
2017/01/13 17:12:26
Done.
| |
52 | |
53 private: | |
54 ~FakeSafeBrowsingDatabaseManager() override {} | |
55 }; | |
56 | |
57 SkBitmap CreateBitmap(int width, int height) { | |
58 SkBitmap bitmap; | |
59 bitmap.allocN32Pixels(width, height); | |
60 bitmap.eraseColor(SK_ColorGREEN); | |
61 return bitmap; | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 class NotificationImageReporterTest : public ::testing::Test { | |
67 public: | |
68 NotificationImageReporterTest() | |
69 // Use REAL_IO_THREAD so DCHECK_CURRENTLY_ON distinguishes IO from UI. | |
70 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} | |
71 | |
72 void SetUp() override; | |
73 void TearDown() override; | |
74 | |
75 private: | |
76 content::TestBrowserThreadBundle thread_bundle_; // Should be first member. | |
77 | |
78 protected: | |
79 TestingProfile profile_; | |
80 | |
81 // Owned by |notification_image_reporter_|. | |
82 MockPermissionReportSender* mock_report_sender_; | |
83 | |
84 scoped_refptr<TestingNotificationImageReporter> notification_image_reporter_; | |
85 | |
86 base::test::ScopedFeatureList feature_list_; | |
87 | |
88 private: | |
89 void SetUpOnIO(); | |
90 }; | |
91 | |
92 void NotificationImageReporterTest::SetUp() { | |
93 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
94 | |
95 // Initialize SafeBrowsingService with FakeSafeBrowsingDatabaseManager. | |
96 TestSafeBrowsingServiceFactory sb_service_factory; | |
97 sb_service_factory.SetTestDatabaseManager( | |
98 new FakeSafeBrowsingDatabaseManager()); | |
99 SafeBrowsingService::RegisterFactory(&sb_service_factory); | |
100 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( | |
101 sb_service_factory.CreateSafeBrowsingService()); | |
102 SafeBrowsingService::RegisterFactory(nullptr); | |
103 g_browser_process->safe_browsing_service()->Initialize(); | |
104 base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. | |
105 | |
106 base::RunLoop run_loop; | |
107 BrowserThread::PostTaskAndReply( | |
108 BrowserThread::IO, FROM_HERE, | |
109 base::Bind(&NotificationImageReporterTest::SetUpOnIO, | |
110 base::Unretained(this)), | |
111 run_loop.QuitClosure()); | |
112 run_loop.Run(); | |
113 } | |
114 | |
115 void NotificationImageReporterTest::TearDown() { | |
116 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); | |
117 base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. | |
118 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); | |
119 } | |
120 | |
121 void NotificationImageReporterTest::SetUpOnIO() { | |
122 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
123 | |
124 mock_report_sender_ = new MockPermissionReportSender; | |
125 notification_image_reporter_ = new TestingNotificationImageReporter( | |
126 base::WrapUnique(mock_report_sender_)); | |
127 } | |
128 | |
129 TEST_F(NotificationImageReporterTest, ReportSuccess) { | |
130 feature_list_.InitWithFeatures({safe_browsing::kOnlyShowScoutOptIn}, {}); | |
131 InitializeSafeBrowsingPrefs(profile_.GetPrefs()); | |
132 SetExtendedReportingPref(profile_.GetPrefs(), true); | |
133 | |
134 GURL origin("https://example.com"); | |
135 SkBitmap bitmap = CreateBitmap(640 /* w */, 360 /* h */); | |
136 notification_image_reporter_->ReportNotificationImageOnUI(&profile_, origin, | |
137 bitmap); | |
138 mock_report_sender_->WaitForReportSent(); | |
139 | |
140 EXPECT_EQ(GURL(kReportingUploadUrl), | |
141 mock_report_sender_->latest_report_uri()); | |
142 EXPECT_EQ("application/octet-stream", | |
143 mock_report_sender_->latest_content_type()); | |
144 | |
145 NotificationImageReportRequest report; | |
146 ASSERT_TRUE(report.ParseFromString(mock_report_sender_->latest_report())); | |
147 EXPECT_EQ(origin.spec(), report.notification_origin()); | |
148 ASSERT_TRUE(report.has_image()); | |
149 EXPECT_GT(report.image().png_data().size(), 0U); | |
150 ASSERT_TRUE(report.image().has_dimensions()); | |
151 EXPECT_EQ(512, report.image().dimensions().width()); | |
152 EXPECT_EQ(288, report.image().dimensions().height()); | |
153 ASSERT_TRUE(report.image().has_original_dimensions()); | |
154 EXPECT_EQ(640, report.image().original_dimensions().width()); | |
155 EXPECT_EQ(360, report.image().original_dimensions().height()); | |
156 } | |
157 | |
158 } // namespace safe_browsing | |
OLD | NEW |