Chromium Code Reviews| Index: chrome/browser/safe_browsing/notification_image_reporter_unittest.cc |
| diff --git a/chrome/browser/safe_browsing/notification_image_reporter_unittest.cc b/chrome/browser/safe_browsing/notification_image_reporter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0935ff233dcbf1d89eef89ff6ef9636ac1430f2b |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/notification_image_reporter_unittest.cc |
| @@ -0,0 +1,158 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/safe_browsing/notification_image_reporter.h" |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/run_loop.h" |
| +#include "base/test/scoped_feature_list.h" |
| +#include "chrome/browser/safe_browsing/mock_permission_report_sender.h" |
| +#include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| +#include "chrome/common/safe_browsing/csd.pb.h" |
| +#include "chrome/test/base/testing_browser_process.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "components/safe_browsing_db/safe_browsing_prefs.h" |
| +#include "components/safe_browsing_db/test_database_manager.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "third_party/skia/include/core/SkColor.h" |
| +#include "url/gurl.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace safe_browsing { |
| + |
| +namespace { |
| + |
| +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).
|
| + "https://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
| + "chrome-notification-image"; |
| + |
| +class TestingNotificationImageReporter : public NotificationImageReporter { |
| + public: |
| + TestingNotificationImageReporter( |
|
Peter Beverloo
2017/01/13 00:48:35
nit: explicit
johnme
2017/01/13 17:12:26
Done.
|
| + std::unique_ptr<net::ReportSender> report_sender) |
| + : NotificationImageReporter(std::move(report_sender)) {} |
| + |
| + protected: |
| + double GetReportChance() override { return 1.0; } |
| + |
| + private: |
| + ~TestingNotificationImageReporter() override {} |
| +}; |
| + |
| +class FakeSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| + public: |
| + bool MatchCsdWhitelistUrl(const GURL& url) override { return match_all_; } |
| + bool match_all_ = false; |
|
Peter Beverloo
2017/01/13 00:48:35
nit: unused?
johnme
2017/01/13 17:12:26
Done.
|
| + |
| + private: |
| + ~FakeSafeBrowsingDatabaseManager() override {} |
| +}; |
| + |
| +SkBitmap CreateBitmap(int width, int height) { |
| + SkBitmap bitmap; |
| + bitmap.allocN32Pixels(width, height); |
| + bitmap.eraseColor(SK_ColorGREEN); |
| + return bitmap; |
| +} |
| + |
| +} // namespace |
| + |
| +class NotificationImageReporterTest : public ::testing::Test { |
| + public: |
| + NotificationImageReporterTest() |
| + // Use REAL_IO_THREAD so DCHECK_CURRENTLY_ON distinguishes IO from UI. |
| + : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {} |
| + |
| + void SetUp() override; |
| + void TearDown() override; |
| + |
| + private: |
| + content::TestBrowserThreadBundle thread_bundle_; // Should be first member. |
| + |
| + protected: |
| + TestingProfile profile_; |
| + |
| + // Owned by |notification_image_reporter_|. |
| + MockPermissionReportSender* mock_report_sender_; |
| + |
| + scoped_refptr<TestingNotificationImageReporter> notification_image_reporter_; |
| + |
| + base::test::ScopedFeatureList feature_list_; |
| + |
| + private: |
| + void SetUpOnIO(); |
| +}; |
| + |
| +void NotificationImageReporterTest::SetUp() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + // Initialize SafeBrowsingService with FakeSafeBrowsingDatabaseManager. |
| + TestSafeBrowsingServiceFactory sb_service_factory; |
| + sb_service_factory.SetTestDatabaseManager( |
| + new FakeSafeBrowsingDatabaseManager()); |
| + SafeBrowsingService::RegisterFactory(&sb_service_factory); |
| + TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( |
| + sb_service_factory.CreateSafeBrowsingService()); |
| + SafeBrowsingService::RegisterFactory(nullptr); |
| + g_browser_process->safe_browsing_service()->Initialize(); |
| + base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. |
| + |
| + base::RunLoop run_loop; |
| + BrowserThread::PostTaskAndReply( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&NotificationImageReporterTest::SetUpOnIO, |
| + base::Unretained(this)), |
| + run_loop.QuitClosure()); |
| + run_loop.Run(); |
| +} |
| + |
| +void NotificationImageReporterTest::TearDown() { |
| + TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); |
| + base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. |
| + TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); |
| +} |
| + |
| +void NotificationImageReporterTest::SetUpOnIO() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + mock_report_sender_ = new MockPermissionReportSender; |
| + notification_image_reporter_ = new TestingNotificationImageReporter( |
| + base::WrapUnique(mock_report_sender_)); |
| +} |
| + |
| +TEST_F(NotificationImageReporterTest, ReportSuccess) { |
| + feature_list_.InitWithFeatures({safe_browsing::kOnlyShowScoutOptIn}, {}); |
| + InitializeSafeBrowsingPrefs(profile_.GetPrefs()); |
| + SetExtendedReportingPref(profile_.GetPrefs(), true); |
| + |
| + GURL origin("https://example.com"); |
| + SkBitmap bitmap = CreateBitmap(640 /* w */, 360 /* h */); |
| + notification_image_reporter_->ReportNotificationImageOnUI(&profile_, origin, |
| + bitmap); |
| + mock_report_sender_->WaitForReportSent(); |
| + |
| + EXPECT_EQ(GURL(kReportingUploadUrl), |
| + mock_report_sender_->latest_report_uri()); |
| + EXPECT_EQ("application/octet-stream", |
| + mock_report_sender_->latest_content_type()); |
| + |
| + NotificationImageReportRequest report; |
| + ASSERT_TRUE(report.ParseFromString(mock_report_sender_->latest_report())); |
| + EXPECT_EQ(origin.spec(), report.notification_origin()); |
| + ASSERT_TRUE(report.has_image()); |
| + EXPECT_GT(report.image().png_data().size(), 0U); |
| + ASSERT_TRUE(report.image().has_dimensions()); |
| + EXPECT_EQ(512, report.image().dimensions().width()); |
| + EXPECT_EQ(288, report.image().dimensions().height()); |
| + ASSERT_TRUE(report.image().has_original_dimensions()); |
| + EXPECT_EQ(640, report.image().original_dimensions().width()); |
| + EXPECT_EQ(360, report.image().original_dimensions().height()); |
| +} |
| + |
| +} // namespace safe_browsing |