Skip to content

Commit

Permalink
Add tests for BraveSiteSettingsCount
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed Feb 18, 2020
1 parent 4b07326 commit dfa51b5
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
113 changes: 113 additions & 0 deletions browser/browsing_data/counters/brave_site_settings_counter_unittest.cc
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <string>
#include <utility>

#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<TestingProfile>();
map_ = static_cast<BraveHostContentSettingsMap*>(
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<ProtocolHandlerRegistry>(
profile(), std::make_unique<TestProtocolHandlerRegistryDelegate>());
counter_ = std::make_unique<BraveSiteSettingsCounter>(
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<browsing_data::BrowsingDataCounter::Result> result) {
DCHECK(result->Finished());
finished_ = result->Finished();

result_ = static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>(
result.get())
->Value();
}

private:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_;

scoped_refptr<BraveHostContentSettingsMap> map_;
std::unique_ptr<ProtocolHandlerRegistry> handler_registry_;
std::unique_ptr<BraveSiteSettingsCounter> 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());
}
3 changes: 3 additions & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit dfa51b5

Please sign in to comment.