| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/variations/variations_associated_data.h" | 5 #include "components/variations/variations_associated_data.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/test/scoped_feature_list.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace variations { | 13 namespace variations { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 const VariationID TEST_VALUE_A = 3300200; | 17 const VariationID TEST_VALUE_A = 3300200; |
| 17 const VariationID TEST_VALUE_B = 3300201; | 18 const VariationID TEST_VALUE_B = 3300201; |
| 18 | 19 |
| 19 // Convenience helper to retrieve the variations::VariationID for a FieldTrial. | 20 // Convenience helper to retrieve the variations::VariationID for a FieldTrial. |
| 20 // Note that this will do the group assignment in |trial| if not already done. | 21 // Note that this will do the group assignment in |trial| if not already done. |
| 21 VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) { | 22 VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) { |
| 22 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name()); | 23 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name()); |
| 23 } | 24 } |
| 24 | 25 |
| 25 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. | 26 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. |
| 26 scoped_refptr<base::FieldTrial> CreateFieldTrial( | 27 scoped_refptr<base::FieldTrial> CreateFieldTrial( |
| 27 const std::string& trial_name, | 28 const std::string& trial_name, |
| 28 int total_probability, | 29 int total_probability, |
| 29 const std::string& default_group_name, | 30 const std::string& default_group_name, |
| 30 int* default_group_number) { | 31 int* default_group_number) { |
| 31 return base::FieldTrialList::FactoryGetFieldTrial( | 32 return base::FieldTrialList::FactoryGetFieldTrial( |
| 32 trial_name, total_probability, default_group_name, | 33 trial_name, total_probability, default_group_name, |
| 33 base::FieldTrialList::kNoExpirationYear, 1, 1, | 34 base::FieldTrialList::kNoExpirationYear, 1, 1, |
| 34 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); | 35 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); |
| 35 } | 36 } |
| 36 | 37 |
| 37 void CreateFeatureWithTrial(const base::Feature& feature, | |
| 38 base::FeatureList::OverrideState override_state, | |
| 39 base::FieldTrial* trial) { | |
| 40 base::FeatureList::ClearInstanceForTesting(); | |
| 41 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 42 feature_list->RegisterFieldTrialOverride(feature.name, override_state, | |
| 43 trial); | |
| 44 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 45 } | |
| 46 | |
| 47 } // namespace | 38 } // namespace |
| 48 | 39 |
| 49 class VariationsAssociatedDataTest : public ::testing::Test { | 40 class VariationsAssociatedDataTest : public ::testing::Test { |
| 50 public: | 41 public: |
| 51 VariationsAssociatedDataTest() : field_trial_list_(NULL) { | 42 VariationsAssociatedDataTest() : field_trial_list_(NULL) { |
| 52 } | 43 } |
| 53 | 44 |
| 54 ~VariationsAssociatedDataTest() override { | 45 ~VariationsAssociatedDataTest() override { |
| 55 // Ensure that the maps are cleared between tests, since they are stored as | 46 // Ensure that the maps are cleared between tests, since they are stored as |
| 56 // process singletons. | 47 // process singletons. |
| 57 testing::ClearAllVariationIDs(); | 48 testing::ClearAllVariationIDs(); |
| 58 testing::ClearAllVariationParams(); | 49 testing::ClearAllVariationParams(); |
| 59 } | 50 } |
| 60 | 51 |
| 52 void CreateFeatureWithTrial(const base::Feature& feature, |
| 53 base::FeatureList::OverrideState override_state, |
| 54 base::FieldTrial* trial) { |
| 55 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 56 feature_list->RegisterFieldTrialOverride(feature.name, override_state, |
| 57 trial); |
| 58 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); |
| 59 } |
| 60 |
| 61 private: | 61 private: |
| 62 base::FieldTrialList field_trial_list_; | 62 base::FieldTrialList field_trial_list_; |
| 63 base::test::ScopedFeatureList scoped_feature_list_; |
| 63 | 64 |
| 64 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest); | 65 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest); |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 // Test that if the trial is immediately disabled, GetGoogleVariationID just | 68 // Test that if the trial is immediately disabled, GetGoogleVariationID just |
| 68 // returns the empty ID. | 69 // returns the empty ID. |
| 69 TEST_F(VariationsAssociatedDataTest, DisableImmediately) { | 70 TEST_F(VariationsAssociatedDataTest, DisableImmediately) { |
| 70 int default_group_number = -1; | 71 int default_group_number = -1; |
| 71 scoped_refptr<base::FieldTrial> trial( | 72 scoped_refptr<base::FieldTrial> trial( |
| 72 CreateFieldTrial("trial", 100, "default", &default_group_number)); | 73 CreateFieldTrial("trial", 100, "default", &default_group_number)); |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 CreateFieldTrial(kTrialName, 100, "A", NULL)); | 425 CreateFieldTrial(kTrialName, 100, "A", NULL)); |
| 425 | 426 |
| 426 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE, | 427 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 427 trial.get()); | 428 trial.get()); |
| 428 | 429 |
| 429 std::map<std::string, std::string> actualParams; | 430 std::map<std::string, std::string> actualParams; |
| 430 EXPECT_EQ(std::string(), GetVariationParamValueByFeature(kFeature, "x")); | 431 EXPECT_EQ(std::string(), GetVariationParamValueByFeature(kFeature, "x")); |
| 431 } | 432 } |
| 432 | 433 |
| 433 } // namespace variations | 434 } // namespace variations |
| OLD | NEW |