diff --git a/browser/browsing_data/counters/brave_site_settings_counter_unittest.cc b/browser/browsing_data/counters/brave_site_settings_counter_unittest.cc new file mode 100644 index 000000000000..120ecca8de8e --- /dev/null +++ b/browser/browsing_data/counters/brave_site_settings_counter_unittest.cc @@ -0,0 +1,113 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/browsing_data/counters/brave_site_settings_counter.h" + +#include +#include +#include + +#include "base/bind.h" +#include "base/test/simple_test_clock.h" +#include "brave/components/brave_shields/browser/brave_shields_util.h" +#include "brave/components/brave_shields/common/brave_shield_constants.h" +#include "brave/components/content_settings/core/browser/brave_host_content_settings_map.h" +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" +#include "chrome/browser/custom_handlers/protocol_handler_registry.h" +#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" +#include "chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.h" +#include "chrome/test/base/testing_profile.h" +#include "components/browsing_data/core/browsing_data_utils.h" +#include "components/browsing_data/core/pref_names.h" +#include "components/prefs/pref_service.h" +#include "content/public/test/browser_task_environment.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "url/gurl.h" + +#if !defined(OS_ANDROID) +#include "content/public/browser/host_zoom_map.h" +#endif + +namespace { +const GURL kBraveURL("https://www.brave.com"); +const GURL kBatURL("https://basicattentiontoken.org"); +const GURL kGoogleURL("https://www.google.com"); +const GURL kAbcURL("https://www.abc.com"); +} // namespace + +class BraveSiteSettingsCounterTest : public testing::Test { + public: + void SetUp() override { + profile_ = std::make_unique(); + map_ = static_cast( + HostContentSettingsMapFactory::GetForProfile(profile())); +#if !defined(OS_ANDROID) + auto* zoom_map = + content::HostZoomMap::GetDefaultForBrowserContext(profile()); +#else + auto* zoom_map = nullptr; +#endif + handler_registry_ = std::make_unique( + profile(), std::make_unique()); + counter_ = std::make_unique( + map(), zoom_map, handler_registry_.get(), profile_->GetPrefs()); + counter_->Init(profile()->GetPrefs(), + browsing_data::ClearBrowsingDataTab::ADVANCED, + base::BindRepeating(&BraveSiteSettingsCounterTest::Callback, + base::Unretained(this))); + } + + Profile* profile() { return profile_.get(); } + + BraveHostContentSettingsMap* map() { return map_.get(); } + + BraveSiteSettingsCounter* counter() { return counter_.get(); } + + browsing_data::BrowsingDataCounter::ResultInt GetResult() { + DCHECK(finished_); + return result_; + } + + void Callback( + std::unique_ptr result) { + DCHECK(result->Finished()); + finished_ = result->Finished(); + + result_ = static_cast( + result.get()) + ->Value(); + } + + private: + content::BrowserTaskEnvironment task_environment_; + std::unique_ptr profile_; + + scoped_refptr map_; + std::unique_ptr handler_registry_; + std::unique_ptr counter_; + bool finished_; + browsing_data::BrowsingDataCounter::ResultInt result_; +}; + +// Tests that the counter correctly counts each setting. +TEST_F(BraveSiteSettingsCounterTest, Count) { + // Check below four settings for different host are counted properly. + map()->SetContentSettingDefaultScope( + kBraveURL, GURL(), ContentSettingsType::PLUGINS, + brave_shields::kHTTPUpgradableResources, CONTENT_SETTING_ALLOW); + map()->SetContentSettingDefaultScope( + kBatURL, GURL(), ContentSettingsType::PLUGINS, + brave_shields::kFingerprinting, CONTENT_SETTING_ALLOW); + map()->SetContentSettingCustomScope( + brave_shields::GetPatternFromURL(kGoogleURL, true), + ContentSettingsPattern::Wildcard(), + ContentSettingsType::JAVASCRIPT, "", CONTENT_SETTING_BLOCK); + map()->SetContentSettingDefaultScope( + kAbcURL, GURL(), ContentSettingsType::PLUGINS, + "", CONTENT_SETTING_ALLOW); + + counter()->Restart(); + EXPECT_EQ(4, GetResult()); +} diff --git a/test/BUILD.gn b/test/BUILD.gn index 75bf06fdc0a8..707516ca61d7 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -70,6 +70,7 @@ test("brave_unit_tests") { sources = [ "//brave/browser/brave_content_browser_client_unittest.cc", "//brave/browser/brave_resources_util_unittest.cc", + "//brave/browser/browsing_data/counters/brave_site_settings_counter_unittest.cc", "//brave/browser/download/brave_download_item_model_unittest.cc", "//brave/browser/metrics/metrics_reporting_util_unittest_linux.cc", "//brave/browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc", @@ -105,6 +106,8 @@ test("brave_unit_tests") { "//components/bookmarks/browser/bookmark_model_unittest.cc", "../../components/domain_reliability/test_util.cc", "../../components/domain_reliability/test_util.h", + "//chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.cc", + "//chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.h", ] deps = [