Commit 98fda4ce authored by engedy's avatar engedy Committed by Commit bot

Split the ScopedSubresourceFilterFeatureToggle into two helper classes.

The ScopedSubresourceFilterFeatureToggle remains, but is reduced to be
responsible for overriding only the base::FeatureList state.

The ScopedSubresourceFilterConfigurator is introduced as the new way for
overriding the configuration returned by GetActiveConfigurations(), and has the
following advantages:
  -- It does so regardless of the current field trial, feature list, and/or
     variation parameters states, thus the configuration can be changed
     multiple times during a single test.
  -- It can be used without a global base::FieldTrialList singleton, or setting
     up variations parameters.
  -- It will allow setting multiple simultaneous configurations conveniently
     without needing to first encode them as variation parameters.

BUG=708181

Review-Url: https://codereview.chromium.org/2838193002
Cr-Commit-Position: refs/heads/master@{#468939}
parent 76928f07
......@@ -85,6 +85,13 @@ class SubresourceFilterMockComponentUpdateService
DISALLOW_COPY_AND_ASSIGN(SubresourceFilterMockComponentUpdateService);
};
subresource_filter::Configuration CreateConfigUsingRulesetFlavor(
const char* ruleset_flavor) {
subresource_filter::Configuration config;
config.ruleset_flavor = ruleset_flavor;
return config;
}
} // namespace
namespace component_updater {
......@@ -166,11 +173,8 @@ class SubresourceFilterComponentInstallerTest : public PlatformTest {
void ExpectInstallerTag(const char* expected_tag,
const char* ruleset_flavor) {
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(base::FeatureList::OVERRIDE_ENABLE_FEATURE,
{{subresource_filter::kRulesetFlavorParameterName,
ruleset_flavor}});
subresource_filter::testing::ScopedSubresourceFilterConfigurator
scoped_configuration(CreateConfigUsingRulesetFlavor(ruleset_flavor));
EXPECT_EQ(expected_tag,
SubresourceFilterComponentInstallerTraits::GetInstallerTag());
}
......@@ -190,11 +194,8 @@ class SubresourceFilterComponentInstallerTest : public PlatformTest {
TEST_F(SubresourceFilterComponentInstallerTest,
TestComponentRegistrationWhenFeatureDisabled) {
base::FieldTrialList field_trial_list(nullptr);
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(base::FeatureList::OVERRIDE_DISABLE_FEATURE,
subresource_filter::kActivationLevelEnabled,
subresource_filter::kActivationScopeNoSites);
scoped_feature(base::FeatureList::OVERRIDE_DISABLE_FEATURE);
std::unique_ptr<SubresourceFilterMockComponentUpdateService>
component_updater(new SubresourceFilterMockComponentUpdateService());
EXPECT_CALL(*component_updater, RegisterComponent(testing::_)).Times(0);
......@@ -204,11 +205,8 @@ TEST_F(SubresourceFilterComponentInstallerTest,
TEST_F(SubresourceFilterComponentInstallerTest,
TestComponentRegistrationWhenFeatureEnabled) {
base::FieldTrialList field_trial_list(nullptr);
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(base::FeatureList::OVERRIDE_ENABLE_FEATURE,
subresource_filter::kActivationLevelDisabled,
subresource_filter::kActivationScopeNoSites);
scoped_feature(base::FeatureList::OVERRIDE_ENABLE_FEATURE);
std::unique_ptr<SubresourceFilterMockComponentUpdateService>
component_updater(new SubresourceFilterMockComponentUpdateService());
EXPECT_CALL(*component_updater, RegisterComponent(testing::_))
......@@ -274,10 +272,8 @@ TEST_F(SubresourceFilterComponentInstallerTest, InstallerTag) {
}
TEST_F(SubresourceFilterComponentInstallerTest, InstallerAttributesDefault) {
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(base::FeatureList::OVERRIDE_ENABLE_FEATURE,
std::map<std::string, std::string>());
subresource_filter::testing::ScopedSubresourceFilterConfigurator
scoped_configuration((subresource_filter::Configuration()));
EXPECT_EQ(update_client::InstallerAttributes(), GetInstallerAttributes());
}
......@@ -285,25 +281,10 @@ TEST_F(SubresourceFilterComponentInstallerTest, InstallerAttributesCustomTag) {
constexpr char kTagKey[] = "tag";
constexpr char kTagValue[] = "a";
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(
base::FeatureList::OVERRIDE_ENABLE_FEATURE,
{{subresource_filter::kRulesetFlavorParameterName, kTagValue}});
subresource_filter::testing::ScopedSubresourceFilterConfigurator
scoped_configuration(CreateConfigUsingRulesetFlavor(kTagValue));
EXPECT_EQ(update_client::InstallerAttributes({{kTagKey, kTagValue}}),
GetInstallerAttributes());
}
TEST_F(SubresourceFilterComponentInstallerTest,
InstallerAttributesFeatureDisabled) {
constexpr char kTagValue[] = "test_value";
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(
base::FeatureList::OVERRIDE_USE_DEFAULT,
{{subresource_filter::kRulesetFlavorParameterName, kTagValue}});
EXPECT_EQ(update_client::InstallerAttributes(), GetInstallerAttributes());
}
} // namespace component_updater
......@@ -62,6 +62,9 @@
#include "components/safe_browsing_db/v4_protocol_manager_util.h"
#include "components/subresource_filter/core/browser/subresource_filter_features.h"
#include "components/subresource_filter/core/browser/subresource_filter_features_test_support.h"
#include "components/subresource_filter/core/common/activation_level.h"
#include "components/subresource_filter/core/common/activation_list.h"
#include "components/subresource_filter/core/common/activation_state.h"
#include "components/subresource_filter/core/common/test_ruleset_creator.h"
#include "content/public/browser/interstitial_page.h"
#include "content/public/browser/navigation_entry.h"
......@@ -904,12 +907,11 @@ IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, MainFrameHitWithReferrer) {
}
IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SubresourceFilterEndToEndTest) {
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(
base::FeatureList::OVERRIDE_ENABLE_FEATURE,
subresource_filter::kActivationLevelEnabled,
subresource_filter::kActivationScopeActivationList,
subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
subresource_filter::testing::ScopedSubresourceFilterConfigurator
scoped_configuration(subresource_filter::Configuration(
subresource_filter::ActivationLevel::ENABLED,
subresource_filter::ActivationScope::ACTIVATION_LIST,
subresource_filter::ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL));
subresource_filter::testing::TestRulesetCreator ruleset_creator;
subresource_filter::testing::TestRulesetPair test_ruleset_pair;
......@@ -1981,12 +1983,11 @@ IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, MainFrameHitWithReferrer) {
IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
SubresourceFilterEndToEndTest) {
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle
scoped_feature_toggle(
base::FeatureList::OVERRIDE_ENABLE_FEATURE,
subresource_filter::kActivationLevelEnabled,
subresource_filter::kActivationScopeActivationList,
subresource_filter::kActivationListSocialEngineeringAdsInterstitial);
subresource_filter::testing::ScopedSubresourceFilterConfigurator
scoped_configuration(subresource_filter::Configuration(
subresource_filter::ActivationLevel::ENABLED,
subresource_filter::ActivationScope::ACTIVATION_LIST,
subresource_filter::ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL));
subresource_filter::testing::TestRulesetCreator ruleset_creator;
subresource_filter::testing::TestRulesetPair test_ruleset_pair;
......
......@@ -9,7 +9,6 @@
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_task_runner_handle.h"
......@@ -36,11 +35,15 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace {
using subresource_filter::testing::ScopedSubresourceFilterConfigurator;
} // namespace
// End to end unit test harness of (most of) the browser process portions of the
// subresource filtering code.
class SubresourceFilterTest : public ChromeRenderViewHostTestHarness {
public:
SubresourceFilterTest() : field_trial_list_(nullptr) {}
SubresourceFilterTest() {}
~SubresourceFilterTest() override {}
// ChromeRenderViewHostTestHarness:
......@@ -50,12 +53,11 @@ class SubresourceFilterTest : public ChromeRenderViewHostTestHarness {
// Ensure correct features.
scoped_feature_list_.InitFromCommandLine("SafeBrowsingV4OnlyEnabled", "");
scoped_feature_toggle_.reset(
new subresource_filter::testing::ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OVERRIDE_ENABLE_FEATURE,
subresource_filter::kActivationLevelEnabled,
subresource_filter::kActivationScopeActivationList,
subresource_filter::kActivationListSubresourceFilter));
scoped_configuration_.ResetConfiguration(subresource_filter::Configuration(
subresource_filter::ActivationLevel::ENABLED,
subresource_filter::ActivationScope::ACTIVATION_LIST,
subresource_filter::ActivationList::SUBRESOURCE_FILTER));
NavigateAndCommit(GURL("https://example.first"));
// Set up safe browsing service with the fake database manager.
......@@ -155,12 +157,9 @@ class SubresourceFilterTest : public ChromeRenderViewHostTestHarness {
private:
base::ScopedTempDir ruleset_service_dir_;
base::FieldTrialList field_trial_list_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<
subresource_filter::testing::ScopedSubresourceFilterFeatureToggle>
scoped_feature_toggle_;
TestingPrefServiceSimple pref_service_;
base::test::ScopedFeatureList scoped_feature_list_;
ScopedSubresourceFilterConfigurator scoped_configuration_;
scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_;
......
......@@ -20,7 +20,7 @@
#include "components/safe_browsing_db/v4_store.h"
namespace subresource_filter {
class SubresourceFilterBrowserTestImpl;
class SubresourceFilterBrowserTest;
}
namespace safe_browsing {
......@@ -165,7 +165,7 @@ class V4Database {
const std::unique_ptr<StoreMap> store_map_;
private:
friend class subresource_filter::SubresourceFilterBrowserTestImpl;
friend class subresource_filter::SubresourceFilterBrowserTest;
friend class V4DatabaseFactory;
friend class V4DatabaseTest;
friend class V4SafeBrowsingServiceTest;
......
......@@ -15,6 +15,9 @@
#include "components/subresource_filter/content/browser/subresource_filter_client.h"
#include "components/subresource_filter/core/browser/subresource_filter_features.h"
#include "components/subresource_filter/core/browser/subresource_filter_features_test_support.h"
#include "components/subresource_filter/core/common/activation_level.h"
#include "components/subresource_filter/core/common/activation_list.h"
#include "components/subresource_filter/core/common/activation_state.h"
#include "components/subresource_filter/core/common/test_ruleset_creator.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
......@@ -109,10 +112,9 @@ class SubresourceFilterSafeBrowsingActivationThrottleTest
void SetUp() override {
content::RenderViewHostTestHarness::SetUp();
scoped_feature_toggle_.reset(
new testing::ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
kActivationScopeActivationList, kActivationListSubresourceFilter));
scoped_configuration_.ResetConfiguration(Configuration(
ActivationLevel::ENABLED, ActivationScope::ACTIVATION_LIST,
ActivationList::SUBRESOURCE_FILTER));
// Note: Using NiceMock to allow uninteresting calls and suppress warnings.
auto client =
base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>();
......@@ -175,8 +177,7 @@ class SubresourceFilterSafeBrowsingActivationThrottleTest
private:
base::FieldTrialList field_trial_list_;
std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle>
scoped_feature_toggle_;
testing::ScopedSubresourceFilterConfigurator scoped_configuration_;
std::unique_ptr<content::NavigationSimulator> navigation_simulator_;
scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_;
base::HistogramTester tester_;
......
......@@ -51,6 +51,7 @@ source_set("unit_tests") {
"//components/prefs:test_support",
"//components/subresource_filter/core/common:test_support",
"//components/subresource_filter/core/common/proto",
"//components/variations",
"//testing/gmock",
"//testing/gtest",
"//third_party/protobuf:protobuf_lite",
......
......@@ -153,8 +153,14 @@ const char kSuppressNotificationsParameterName[] = "suppress_notifications";
const char kWhitelistSiteOnReloadParameterName[] = "whitelist_site_on_reload";
Configuration::Configuration() = default;
Configuration::~Configuration() = default;
Configuration::Configuration(ActivationLevel activation_level,
ActivationScope activation_scope,
ActivationList activation_list)
: activation_level(activation_level),
activation_scope(activation_scope),
activation_list(activation_list) {}
Configuration::Configuration(Configuration&&) = default;
Configuration::~Configuration() = default;
Configuration& Configuration::operator=(Configuration&&) = default;
ConfigurationList::ConfigurationList(Configuration config)
......@@ -172,9 +178,12 @@ scoped_refptr<ConfigurationList> GetActiveConfigurations() {
namespace testing {
void ClearCachedActiveConfigurations() {
scoped_refptr<ConfigurationList> GetAndSetActivateConfigurations(
scoped_refptr<ConfigurationList> new_configs) {
base::AutoLock lock(g_active_configurations_lock.Get());
g_active_configurations.Get() = nullptr;
auto old_configs = std::move(g_active_configurations.Get());
g_active_configurations.Get() = std::move(new_configs);
return old_configs;
}
} // namespace testing
......
......@@ -18,8 +18,11 @@ namespace subresource_filter {
// should operate.
struct Configuration {
Configuration();
~Configuration();
Configuration(ActivationLevel activation_level,
ActivationScope activation_scope,
ActivationList activation_list = ActivationList::NONE);
Configuration(Configuration&&);
~Configuration();
Configuration& operator=(Configuration&&);
// The maximum degree to which subresource filtering should be activated on
......@@ -47,8 +50,8 @@ struct Configuration {
// be suppressed in the UI.
bool should_suppress_notifications = false;
// The ruleset flavor to download through the component updater, or the empty
// string if the default ruleset should be used.
// The ruleset flavor to download through the component updater. This or the
// empty string if the default ruleset should be used.
std::string ruleset_flavor;
// Whether to whitelist a site when a page loaded from that site is reloaded.
......@@ -75,15 +78,16 @@ class ConfigurationList : public base::RefCountedThreadSafe<ConfigurationList> {
// Retrieves all currently enabled subresource filtering configurations. The
// configurations are parsed on first access and then the result is cached.
//
// In tests, however, the config may change in-between navigations, so callers
// should not hold on to the result for long.
// In tests, however, the config may be altered in-between navigations, so
// callers should not hold on to the result for long.
scoped_refptr<ConfigurationList> GetActiveConfigurations();
namespace testing {
// Clears the cached active ConfigurationList so that it will be recomputed on
// next access. Used in tests when the variation parameters are altered.
void ClearCachedActiveConfigurations();
// Returns the currently cached active ConfigurationList, if any, and replaces
// it with |new_configs|, which may be nullptr to clear the cache.
scoped_refptr<ConfigurationList> GetAndSetActivateConfigurations(
scoped_refptr<ConfigurationList> new_configs);
} // namespace testing
......
......@@ -4,80 +4,47 @@
#include "components/subresource_filter/core/browser/subresource_filter_features_test_support.h"
#include <map>
#include <memory>
#include <utility>
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "components/subresource_filter/core/browser/subresource_filter_features.h"
#include "components/variations/variations_associated_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
namespace subresource_filter {
namespace testing {
namespace {
constexpr const char kTestFieldTrialName[] = "FieldTrialNameShouldNotMatter";
constexpr const char kTestExperimentGroupName[] = "GroupNameShouldNotMatter";
} // namespace
// ScopedSubresourceFilterConfigurator ----------------------------------------
ScopedSubresourceFilterFeatureToggle::ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OverrideState feature_state,
const std::string& maximum_activation_level,
const std::string& activation_scope,
const std::string& activation_lists,
const std::string& performance_measurement_rate,
const std::string& suppress_notifications,
const std::string& whitelist_site_on_reload)
: ScopedSubresourceFilterFeatureToggle(
feature_state,
{{kActivationLevelParameterName, maximum_activation_level},
{kActivationScopeParameterName, activation_scope},
{kActivationListsParameterName, activation_lists},
{kPerformanceMeasurementRateParameterName,
performance_measurement_rate},
{kSuppressNotificationsParameterName, suppress_notifications},
{kWhitelistSiteOnReloadParameterName, whitelist_site_on_reload}}) {}
ScopedSubresourceFilterFeatureToggle::ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OverrideState feature_state,
std::map<std::string, std::string> variation_params) {
EXPECT_TRUE(base::AssociateFieldTrialParams(
kTestFieldTrialName, kTestExperimentGroupName, variation_params));
base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial(
kTestFieldTrialName, kTestExperimentGroupName);
ScopedSubresourceFilterConfigurator::ScopedSubresourceFilterConfigurator(
scoped_refptr<ConfigurationList> configs)
: original_config_(GetAndSetActivateConfigurations(configs)) {}
std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
feature_list->RegisterFieldTrialOverride(kSafeBrowsingSubresourceFilter.name,
feature_state, field_trial);
ScopedSubresourceFilterConfigurator::ScopedSubresourceFilterConfigurator(
Configuration config)
: ScopedSubresourceFilterConfigurator(
base::MakeShared<ConfigurationList>(std::move(config))) {}
// Since we are adding a scoped feature list after browser start, copy over
// the existing feature list to prevent inconsistency.
base::FeatureList* existing_feature_list = base::FeatureList::GetInstance();
if (existing_feature_list) {
std::string enabled_features;
std::string disabled_features;
base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features,
&disabled_features);
feature_list->InitializeFromCommandLine(enabled_features,
disabled_features);
}
scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
ScopedSubresourceFilterConfigurator::~ScopedSubresourceFilterConfigurator() {
GetAndSetActivateConfigurations(std::move(original_config_));
}
// Force the active ConfigurationList to be reparsed on next access so that
// the variation parameters come into effect.
ClearCachedActiveConfigurations();
void ScopedSubresourceFilterConfigurator::ResetConfiguration(
Configuration config) {
GetAndSetActivateConfigurations(
base::MakeShared<ConfigurationList>(std::move(config)));
}
ScopedSubresourceFilterFeatureToggle::~ScopedSubresourceFilterFeatureToggle() {
variations::testing::ClearAllVariationParams();
// ScopedSubresourceFilterFeatureToggle ---------------------------------------
// Force the active ConfigurationList to be reparsed on next access, so that
// the overrides from this instance are no longer in effect.
ClearCachedActiveConfigurations();
ScopedSubresourceFilterFeatureToggle::ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OverrideState feature_state) {
if (feature_state == base::FeatureList::OVERRIDE_ENABLE_FEATURE)
scoped_feature_list_.InitAndEnableFeature(kSafeBrowsingSubresourceFilter);
else if (feature_state == base::FeatureList::OVERRIDE_DISABLE_FEATURE)
scoped_feature_list_.InitAndDisableFeature(kSafeBrowsingSubresourceFilter);
}
ScopedSubresourceFilterFeatureToggle::~ScopedSubresourceFilterFeatureToggle() {}
} // namespace testing
} // namespace subresource_filter
......@@ -5,37 +5,51 @@
#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURES_TEST_SUPPORT_H_
#define COMPONENTS_SUBRESOURCE_FILTER_CORE_BROWSER_SUBRESOURCE_FILTER_FEATURES_TEST_SUPPORT_H_
#include <map>
#include <string>
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/test/scoped_feature_list.h"
#include "components/subresource_filter/core/browser/subresource_filter_features.h"
namespace subresource_filter {
namespace testing {
// Helper to override the state of the |kSafeBrowsingSubresourceFilter| feature,
// and its variation parameters, e.g., maximum activation level and activation
// scope. Expects a pre-existing global base::FieldTrialList singleton.
class ScopedSubresourceFilterFeatureToggle {
// Helper class to override the active subresource filtering configuration to be
// used in tests while the instance is in scope.
//
// Configuration overrides can be nested, and will take effect regardless of
// field trial, feature, and/or variation parameter states.
class ScopedSubresourceFilterConfigurator {
public:
ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OverrideState feature_state,
const std::string& maximum_activation_level,
const std::string& activation_scope,
const std::string& activation_lists = std::string(),
const std::string& performance_measurement_rate = std::string(),
const std::string& suppress_notifications = std::string(),
const std::string& whitelist_site_on_reload = std::string());
ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OverrideState feature_state,
std::map<std::string, std::string> variation_params);
explicit ScopedSubresourceFilterConfigurator(
scoped_refptr<ConfigurationList> configs = nullptr);
explicit ScopedSubresourceFilterConfigurator(Configuration config);
~ScopedSubresourceFilterConfigurator();
void ResetConfiguration(Configuration config);
private:
scoped_refptr<ConfigurationList> original_config_;
DISALLOW_COPY_AND_ASSIGN(ScopedSubresourceFilterConfigurator);
};
// Helper class to override the state of the |kSafeBrowsingSubresourceFilter|
// feature.
//
// Clears the active subresource filtering configuration override, upon
// construction, if any, and restores it on destruction. So while the instance
// is in scope, calls to GetActiveConfigurations() will default to returning the
// hard-coded configuration corresponding to the forced feature state. Tests
// that need to toggle both the feature and override the active configuration
// should therefore do so in that order.
class ScopedSubresourceFilterFeatureToggle {
public:
explicit ScopedSubresourceFilterFeatureToggle(
base::FeatureList::OverrideState subresource_filter_state);
~ScopedSubresourceFilterFeatureToggle();
private:
ScopedSubresourceFilterConfigurator scoped_configuration_;
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(ScopedSubresourceFilterFeatureToggle);
......
......@@ -4,13 +4,60 @@
#include "components/subresource_filter/core/browser/subresource_filter_features.h"
#include <map>
#include <memory>
#include <string>
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "components/subresource_filter/core/browser/subresource_filter_features_test_support.h"
#include "components/variations/variations_associated_data.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace subresource_filter {
namespace testing {
namespace {
constexpr const char kTestFieldTrialName[] = "FieldTrialNameShouldNotMatter";
constexpr const char kTestExperimentGroupName[] = "GroupNameShouldNotMatter";
class ScopedExperimentalStateToggle {
public:
ScopedExperimentalStateToggle(
base::FeatureList::OverrideState feature_state,
std::map<std::string, std::string> variation_params)
: field_trial_list_(nullptr /* entropy_provider */),
scoped_configurator_(nullptr) {
EXPECT_TRUE(base::AssociateFieldTrialParams(
kTestFieldTrialName, kTestExperimentGroupName, variation_params));
base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial(
kTestFieldTrialName, kTestExperimentGroupName);
std::unique_ptr<base::FeatureList> feature_list =
base::MakeUnique<base::FeatureList>();
feature_list->RegisterFieldTrialOverride(
kSafeBrowsingSubresourceFilter.name, feature_state, field_trial);
scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
}
~ScopedExperimentalStateToggle() {
variations::testing::ClearAllVariationParams();
}
private:
base::FieldTrialList field_trial_list_;
ScopedSubresourceFilterConfigurator scoped_configurator_;
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(ScopedExperimentalStateToggle);
};
} // namespace
} // namespace testing
TEST(SubresourceFilterFeaturesTest, ActivationLevel) {
const struct {
......@@ -36,11 +83,11 @@ TEST(SubresourceFilterFeaturesTest, ActivationLevel) {
SCOPED_TRACE(::testing::Message("ActivationLevelParam = \"")
<< test_case.activation_level_param << "\"");
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
test_case.activation_level_param, kActivationScopeNoSites);
{{kActivationLevelParameterName, test_case.activation_level_param},
{kActivationScopeParameterName, kActivationScopeNoSites}});
const auto active_configurations = GetActiveConfigurations();
const Configuration& actual_configuration =
......@@ -75,11 +122,11 @@ TEST(SubresourceFilterFeaturesTest, ActivationScope) {
SCOPED_TRACE(::testing::Message("ActivationScopeParam = \"")
<< test_case.activation_scope_param << "\"");
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
kActivationLevelDisabled, test_case.activation_scope_param);
{{kActivationLevelParameterName, kActivationLevelDisabled},
{kActivationScopeParameterName, test_case.activation_scope_param}});
const auto active_configurations = GetActiveConfigurations();
const Configuration& actual_configuration =
......@@ -128,11 +175,11 @@ TEST(SubresourceFilterFeaturesTest, ActivationLevelAndScope) {
kActivationScopeAllSites, ActivationScope::NO_SITES}};
for (const auto& test_case : kTestCases) {
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
test_case.activation_level_param, test_case.activation_scope_param);
{{kActivationLevelParameterName, test_case.activation_level_param},
{kActivationScopeParameterName, test_case.activation_scope_param}});
const auto active_configurations = GetActiveConfigurations();
const Configuration& actual_configuration =
......@@ -185,12 +232,12 @@ TEST(SubresourceFilterFeaturesTest, ActivationList) {
SCOPED_TRACE(::testing::Message("ActivationListParam = \"")
<< test_case.activation_list_param << "\"");
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
kActivationLevelDisabled, kActivationScopeNoSites,
test_case.activation_list_param);
{{kActivationLevelParameterName, kActivationLevelDisabled},
{kActivationScopeParameterName, kActivationScopeNoSites},
{kActivationListsParameterName, test_case.activation_list_param}});
const auto active_configurations = GetActiveConfigurations();
const Configuration& actual_configuration =
......@@ -224,8 +271,7 @@ TEST(SubresourceFilterFeaturesTest, PerfMeasurementRate) {
SCOPED_TRACE(::testing::Message("PerfMeasurementParam = \"")
<< test_case.perf_measurement_param << "\"");
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
{{kPerformanceMeasurementRateParameterName,
......@@ -260,8 +306,7 @@ TEST(SubresourceFilterFeaturesTest, SuppressNotifications) {
SCOPED_TRACE(::testing::Message("SuppressNotificationsParam = \"")
<< test_case.suppress_notifications_param << "\"");
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
{{kSuppressNotificationsParameterName,
......@@ -296,8 +341,7 @@ TEST(SubresourceFilterFeaturesTest, WhitelistSiteOnReload) {
SCOPED_TRACE(::testing::Message("WhitelistSiteOnReloadParam = \"")
<< test_case.whitelist_site_on_reload_param << "\"");
base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
{{kWhitelistSiteOnReloadParameterName,
......@@ -311,4 +355,30 @@ TEST(SubresourceFilterFeaturesTest, WhitelistSiteOnReload) {
}
}
TEST(SubresourceFilterFeaturesTest, RulesetFlavor) {
const struct {
bool feature_enabled;
const char* ruleset_flavor_param;
const char* expected_ruleset_flavor_value;
} kTestCases[] = {
{false, "", ""}, {false, "a", ""}, {false, "test value", ""},
{true, "", ""}, {true, "a", "a"}, {true, "test value", "test value"}};
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
SCOPED_TRACE(::testing::Message("Flavor = \"")
<< test_case.ruleset_flavor_param << "\"");
testing::ScopedExperimentalStateToggle scoped_experimental_state(
test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
: base::FeatureList::OVERRIDE_USE_DEFAULT,
{{kRulesetFlavorParameterName, test_case.ruleset_flavor_param}});
const auto active_configurations = GetActiveConfigurations();
const Configuration& actual_configuration =
active_configurations->the_one_and_only();
EXPECT_EQ(std::string(test_case.expected_ruleset_flavor_value),
actual_configuration.ruleset_flavor);
}
}
} // namespace subresource_filter
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment