|
| 1 | +/* Copyright (c) 2020 The Brave Authors. All rights reserved. |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include "brave/browser/browsing_data/brave_browsing_data_remover_delegate.h" |
| 7 | + |
| 8 | +#include <memory> |
| 9 | +#include <string> |
| 10 | +#include <utility> |
| 11 | + |
| 12 | +#include "base/bind.h" |
| 13 | +#include "brave/components/brave_shields/browser/brave_shields_util.h" |
| 14 | +#include "brave/components/brave_shields/common/brave_shield_constants.h" |
| 15 | +#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h" |
| 16 | +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 17 | +#include "chrome/test/base/testing_profile.h" |
| 18 | +#include "components/content_settings/core/browser/host_content_settings_map.h" |
| 19 | +#include "content/public/test/browser_task_environment.h" |
| 20 | +#include "testing/gtest/include/gtest/gtest.h" |
| 21 | +#include "url/gurl.h" |
| 22 | + |
| 23 | +namespace { |
| 24 | +const GURL kBraveURL("https://www.brave.com"); |
| 25 | +const GURL kBatURL("https://basicattentiontoken.org"); |
| 26 | +const GURL kGoogleURL("https://www.google.com"); |
| 27 | +const GURL kAbcURL("https://www.abc.com"); |
| 28 | +} // namespace |
| 29 | + |
| 30 | +class BraveBrowsingDataRemoverDelegateTest : public testing::Test { |
| 31 | + public: |
| 32 | + void SetUp() override { |
| 33 | + profile_ = std::make_unique<TestingProfile>(); |
| 34 | + map_ = HostContentSettingsMapFactory::GetForProfile(profile()); |
| 35 | + } |
| 36 | + |
| 37 | + Profile* profile() { return profile_.get(); } |
| 38 | + |
| 39 | + HostContentSettingsMap* map() { return map_.get(); } |
| 40 | + |
| 41 | + BraveBrowsingDataRemoverDelegate* delegate() { |
| 42 | + return static_cast<BraveBrowsingDataRemoverDelegate*>( |
| 43 | + profile()->GetBrowsingDataRemoverDelegate()); |
| 44 | + } |
| 45 | + |
| 46 | + int GetShieldsSettingsCount() { |
| 47 | + int shields_settings_count = 0; |
| 48 | + for (const auto& resource_id : content_settings::GetShieldsResourceIDs()) { |
| 49 | + ContentSettingsForOneType settings; |
| 50 | + ContentSettingsType content_type = ContentSettingsType::PLUGINS; |
| 51 | + map()->GetSettingsForOneType(content_type, resource_id, &settings); |
| 52 | + shields_settings_count += settings.size(); |
| 53 | + } |
| 54 | + return shields_settings_count; |
| 55 | + } |
| 56 | + |
| 57 | + private: |
| 58 | + content::BrowserTaskEnvironment task_environment_; |
| 59 | + |
| 60 | + std::unique_ptr<TestingProfile> profile_; |
| 61 | + scoped_refptr<HostContentSettingsMap> map_; |
| 62 | +}; |
| 63 | + |
| 64 | +TEST_F(BraveBrowsingDataRemoverDelegateTest, ShieldsSettingsClearTest) { |
| 65 | + // Four settings are added. |
| 66 | + // First two settings are shields settings in PLUGINS type. |
| 67 | + // Javascript is not counted as shields type because it's stored to |
| 68 | + // JAVASCRIPT type instead of PLUGINS type. |
| 69 | + // Last one is PLUGINS type but it's flash resource not shields resource. |
| 70 | + map()->SetContentSettingDefaultScope( |
| 71 | + kBraveURL, GURL(), ContentSettingsType::PLUGINS, |
| 72 | + brave_shields::kHTTPUpgradableResources, CONTENT_SETTING_ALLOW); |
| 73 | + map()->SetContentSettingDefaultScope( |
| 74 | + kBatURL, GURL(), ContentSettingsType::PLUGINS, |
| 75 | + brave_shields::kFingerprinting, CONTENT_SETTING_ALLOW); |
| 76 | + map()->SetContentSettingCustomScope( |
| 77 | + brave_shields::GetPatternFromURL(kGoogleURL, true), |
| 78 | + ContentSettingsPattern::Wildcard(), |
| 79 | + ContentSettingsType::JAVASCRIPT, "", CONTENT_SETTING_BLOCK); |
| 80 | + map()->SetContentSettingDefaultScope( |
| 81 | + kAbcURL, GURL(), ContentSettingsType::PLUGINS, |
| 82 | + "", CONTENT_SETTING_ALLOW); |
| 83 | + |
| 84 | + const base::Time kNow = base::Time::Now(); |
| 85 | + const base::Time k1DaysOld = kNow - base::TimeDelta::FromDays(1); |
| 86 | + |
| 87 | + // Check current shields settings count is 2 and zero after clearing 1 day |
| 88 | + // time range. |
| 89 | + EXPECT_EQ(2, GetShieldsSettingsCount()); |
| 90 | + delegate()->ClearShieldsSettings(k1DaysOld, kNow); |
| 91 | + EXPECT_EQ(0, GetShieldsSettingsCount()); |
| 92 | +} |
0 commit comments