Chromium Code Reviews| Index: base/feature_list_unittest.cc |
| diff --git a/base/feature_list_unittest.cc b/base/feature_list_unittest.cc |
| index c9423ce776a7a00773dd0a2535e91a7603b95170..3abeee409950052f887949d34af54c04ffb9ba66 100644 |
| --- a/base/feature_list_unittest.cc |
| +++ b/base/feature_list_unittest.cc |
| @@ -4,6 +4,7 @@ |
| #include "base/feature_list.h" |
| +#include "base/metrics/field_trial.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace base { |
| @@ -20,6 +21,18 @@ struct Feature kFeatureOffByDefault { |
| kFeatureOffByDefaultName, FEATURE_DISABLED_BY_DEFAULT |
| }; |
| +// Tests whether a field trial is active (i.e. group() has been called on it), |
| +// using public FieldTrial API (which doesn't expose this state on the object). |
| +bool IsFieldTrialActive(FieldTrial* trial) { |
| + base::FieldTrial::ActiveGroups active_groups; |
| + base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
| + for (size_t i = 0; i < active_groups.size(); ++i) { |
| + if (active_groups[i].trial_name == trial->trial_name()) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| } // namespace |
| class FeatureListTest : public testing::Test { |
| @@ -105,4 +118,134 @@ TEST_F(FeatureListTest, CheckFeatureIdentity) { |
| EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault2)); |
| } |
| +TEST_F(FeatureListTest, FieldTrialOverrides) { |
| + struct { |
| + FeatureList::OverrideState trial1_state; |
| + FeatureList::OverrideState trial2_state; |
| + } test_cases[] = { |
| + {FeatureList::OVERRIDE_DISABLE_FEATURE, |
| + FeatureList::OVERRIDE_DISABLE_FEATURE}, |
| + {FeatureList::OVERRIDE_DISABLE_FEATURE, |
| + FeatureList::OVERRIDE_ENABLE_FEATURE}, |
| + {FeatureList::OVERRIDE_ENABLE_FEATURE, |
| + FeatureList::OVERRIDE_DISABLE_FEATURE}, |
| + {FeatureList::OVERRIDE_ENABLE_FEATURE, |
| + FeatureList::OVERRIDE_ENABLE_FEATURE}, |
| + }; |
| + |
| + FieldTrial::ActiveGroup active_group; |
| + for (size_t i = 0; i < arraysize(test_cases); i++) { |
|
Ilya Sherman
2015/09/16 00:50:44
nit: Add a SCOPED_TRACE or equivalent somewhere?
Ilya Sherman
2015/09/16 00:50:44
nit: ++i
Alexei Svitkine (slow)
2015/09/22 21:19:59
Done.
Alexei Svitkine (slow)
2015/09/22 21:19:59
Done.
|
| + const auto& test_case = test_cases[i]; |
| + |
| + ClearFeatureListInstance(); |
| + |
| + FieldTrialList field_trial_list(nullptr); |
| + scoped_ptr<FeatureList> feature_list(new FeatureList); |
| + |
| + FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("TrialExample1", "A"); |
| + FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("TrialExample2", "B"); |
| + feature_list->RegisterFieldTrialOverride(kFeatureOnByDefaultName, |
| + test_case.trial1_state, trial1); |
| + feature_list->RegisterFieldTrialOverride(kFeatureOffByDefaultName, |
| + test_case.trial2_state, trial2); |
| + RegisterFeatureListInstance(feature_list.Pass()); |
| + |
| + // Initially, neither trial should be active. |
| + EXPECT_FALSE(IsFieldTrialActive(trial1)); |
| + EXPECT_FALSE(IsFieldTrialActive(trial2)); |
| + |
| + const bool expected_enabled_1 = |
| + (test_case.trial1_state == FeatureList::OVERRIDE_ENABLE_FEATURE); |
| + EXPECT_EQ(expected_enabled_1, FeatureList::IsEnabled(kFeatureOnByDefault)); |
| + // The above should have activated |trial1|. |
| + EXPECT_TRUE(IsFieldTrialActive(trial1)); |
| + EXPECT_FALSE(IsFieldTrialActive(trial2)); |
| + |
| + const bool expected_enabled_2 = |
| + (test_case.trial2_state == FeatureList::OVERRIDE_ENABLE_FEATURE); |
| + EXPECT_EQ(expected_enabled_2, FeatureList::IsEnabled(kFeatureOffByDefault)); |
| + // The above should have activated |trial2|. |
| + EXPECT_TRUE(IsFieldTrialActive(trial1)); |
| + EXPECT_TRUE(IsFieldTrialActive(trial2)); |
| + } |
| +} |
| + |
| +TEST_F(FeatureListTest, CommandLineTakesPrecedenceOverFieldTrial) { |
| + ClearFeatureListInstance(); |
| + |
| + FieldTrialList field_trial_list(nullptr); |
| + scoped_ptr<FeatureList> feature_list(new FeatureList); |
| + |
| + // The feature is explicitly enabled on the command-line. |
| + feature_list->InitializeFromCommandLine(kFeatureOffByDefaultName, ""); |
| + |
| + // But the FieldTrial would set the feature to disabled. |
| + FieldTrial* trial = FieldTrialList::CreateFieldTrial("TrialExample2", "A"); |
| + feature_list->RegisterFieldTrialOverride( |
| + kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, trial); |
| + RegisterFeatureListInstance(feature_list.Pass()); |
| + |
| + EXPECT_FALSE(IsFieldTrialActive(trial)); |
| + // Command-line should take precedence. |
| + EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| + // Since the feature is on due to the command-line, and not as a result of the |
| + // field trial, the field trial should not be activated (since the Associate* |
| + // API wasn't used.) |
| + EXPECT_FALSE(IsFieldTrialActive(trial)); |
| +} |
| + |
| +TEST_F(FeatureListTest, AssociateReportingFieldTrial) { |
| + struct { |
| + const char* enable_features; |
| + const char* disable_features; |
| + bool expected_enable_trial_created; |
| + bool expected_disable_trial_created; |
| + } test_cases[] = { |
| + // If no enable/disable flags are specified, no trials should be created. |
| + {"", "", false, false}, |
| + // Enabling the feature should result in the enable trial created. |
| + {kFeatureOffByDefaultName, "", true, false}, |
| + // Disabling the feature should result in the disable trial created. |
| + {"", kFeatureOffByDefaultName, false, true}, |
| + }; |
| + |
| + const char kTrialName[] = "ForcingTrial"; |
| + const char kForcedOnGroupName[] = "ForcedOn"; |
| + const char kForcedOffGroupName[] = "ForcedOff"; |
| + |
| + for (size_t i = 0; i < arraysize(test_cases); i++) { |
|
Ilya Sherman
2015/09/16 00:50:44
nit: ++i
Alexei Svitkine (slow)
2015/09/22 21:19:59
Done.
|
| + const auto& test_case = test_cases[i]; |
| + |
| + ClearFeatureListInstance(); |
| + |
| + FieldTrialList field_trial_list(nullptr); |
| + scoped_ptr<FeatureList> feature_list(new FeatureList); |
| + feature_list->InitializeFromCommandLine(test_case.enable_features, |
| + test_case.disable_features); |
| + |
| + FieldTrial* enable_trial = feature_list->AssociateReportingFieldTrial( |
| + kFeatureOffByDefaultName, FeatureList::OVERRIDE_ENABLE_FEATURE, |
| + kTrialName, kForcedOnGroupName); |
| + EXPECT_EQ(test_case.expected_enable_trial_created, enable_trial != nullptr); |
| + FieldTrial* disable_trial = feature_list->AssociateReportingFieldTrial( |
| + kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, |
| + kTrialName, kForcedOffGroupName); |
| + EXPECT_EQ(test_case.expected_disable_trial_created, |
| + disable_trial != nullptr); |
| + RegisterFeatureListInstance(feature_list.Pass()); |
| + |
| + if (disable_trial) { |
| + EXPECT_FALSE(IsFieldTrialActive(disable_trial)); |
| + EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| + EXPECT_TRUE(IsFieldTrialActive(disable_trial)); |
| + EXPECT_EQ(kForcedOffGroupName, disable_trial->group_name()); |
| + } else if (enable_trial) { |
| + EXPECT_FALSE(IsFieldTrialActive(enable_trial)); |
| + EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| + EXPECT_TRUE(IsFieldTrialActive(enable_trial)); |
| + EXPECT_EQ(kForcedOnGroupName, enable_trial->group_name()); |
| + } |
| + } |
| +} |
| + |
| } // namespace base |