Chromium Code Reviews| Index: components/variations/variations_seed_processor_unittest.cc |
| diff --git a/components/variations/variations_seed_processor_unittest.cc b/components/variations/variations_seed_processor_unittest.cc |
| index 833b5e0493abcac9340c2cd6fd118515f54c11ca..0b2184c020353579a12990f6a81abe9772ced111 100644 |
| --- a/components/variations/variations_seed_processor_unittest.cc |
| +++ b/components/variations/variations_seed_processor_unittest.cc |
| @@ -6,8 +6,10 @@ |
| #include <vector> |
| +#include "base/bind.h" |
| #include "base/command_line.h" |
| #include "base/strings/string_split.h" |
| +#include "base/strings/utf_string_conversions.h" |
| #include "components/variations/processed_study.h" |
| #include "components/variations/variations_associated_data.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -74,6 +76,32 @@ bool IsFieldTrialActive(const std::string& trial_name) { |
| return false; |
| } |
| +class TestOverrideStringCallback { |
| + public: |
| + typedef std::map<uint32_t, base::string16> OverrideMap; |
| + |
| + TestOverrideStringCallback() : |
| + callback_(base::Bind( |
| + &TestOverrideStringCallback::override, base::Unretained(this))) { |
| + } |
| + |
| + const VariationsSeedProcessor::UIStringOverrideCallback& callback() const { |
| + return callback_; |
| + } |
| + |
| + const OverrideMap& overrides() const { return overrides_; } |
| + |
| + private: |
| + void override(uint32_t hash, const base::string16& string) { |
|
Alexei Svitkine (slow)
2014/07/03 17:54:41
Nit: I don't think this should use hacker_style. I
jwd
2014/07/07 15:52:21
Done.
|
| + overrides_[hash] = string; |
| + } |
| + |
| + VariationsSeedProcessor::UIStringOverrideCallback callback_; |
| + OverrideMap overrides_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestOverrideStringCallback); |
| +}; |
| + |
| } // namespace |
| class VariationsSeedProcessorTest : public ::testing::Test { |
| @@ -91,12 +119,16 @@ class VariationsSeedProcessorTest : public ::testing::Test { |
| bool CreateTrialFromStudy(const Study* study) { |
| ProcessedStudy processed_study; |
| if (processed_study.Init(study, false)) { |
| - VariationsSeedProcessor().CreateTrialFromStudy(processed_study); |
| + VariationsSeedProcessor().CreateTrialFromStudy( |
| + processed_study, override_callback_.callback()); |
| return true; |
| } |
| return false; |
| } |
| + protected: |
| + TestOverrideStringCallback override_callback_; |
| + |
| private: |
| DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessorTest); |
| }; |
| @@ -196,7 +228,8 @@ TEST_F(VariationsSeedProcessorTest, |
| study1->set_expiry_date(TimeToProtoTime(year_ago)); |
| seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(), |
| version, Study_Channel_STABLE, |
| - Study_FormFactor_DESKTOP, ""); |
| + Study_FormFactor_DESKTOP, "", |
| + override_callback_.callback()); |
| EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName)); |
| } |
| @@ -208,11 +241,73 @@ TEST_F(VariationsSeedProcessorTest, |
| study2->set_expiry_date(TimeToProtoTime(year_ago)); |
| seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(), |
| version, Study_Channel_STABLE, |
| - Study_FormFactor_DESKTOP, ""); |
| + Study_FormFactor_DESKTOP, "", |
| + override_callback_.callback()); |
| EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName)); |
| } |
| } |
| +TEST_F(VariationsSeedProcessorTest, OverrideUIStrings) { |
| + base::FieldTrialList field_trial_list(NULL); |
| + |
| + Study study; |
| + study.set_name("Study1"); |
| + study.set_default_experiment_name("B"); |
| + study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO); |
| + |
| + Study_Experiment* experiment1 = AddExperiment("A", 0, &study); |
| + Study_Experiment_OverrideUIString* override = |
| + experiment1->add_override_ui_string(); |
| + |
| + override->set_name_hash(1234); |
| + override->set_value("test"); |
| + |
| + Study_Experiment* experiment2 = AddExperiment("B", 1, &study); |
| + |
| + EXPECT_TRUE(CreateTrialFromStudy(&study)); |
| + |
| + const TestOverrideStringCallback::OverrideMap& overrides = |
| + override_callback_.overrides(); |
| + |
| + EXPECT_TRUE(overrides.empty()); |
| + |
| + study.set_name("Study2"); |
| + experiment1->set_probability_weight(1); |
| + experiment2->set_probability_weight(0); |
| + |
| + EXPECT_TRUE(CreateTrialFromStudy(&study)); |
| + |
| + EXPECT_EQ(1u, overrides.size()); |
| + TestOverrideStringCallback::OverrideMap::const_iterator it = |
| + overrides.find(1234); |
| + EXPECT_NE(overrides.end(), it); |
| + EXPECT_EQ(base::ASCIIToUTF16("test"), it->second); |
| +} |
| + |
| +TEST_F(VariationsSeedProcessorTest, OverrideUIStringsWithForcingFlag) { |
| + Study study = CreateStudyWithFlagGroups(100, 0, 0); |
| + ASSERT_EQ(kForcingFlag1, study.experiment(1).forcing_flag()); |
| + |
| + study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO); |
| + Study_Experiment_OverrideUIString* override = |
| + study.mutable_experiment(1)->add_override_ui_string(); |
| + override->set_name_hash(1234); |
| + override->set_value("test"); |
| + |
| + CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); |
| + base::FieldTrialList field_trial_list(NULL); |
| + EXPECT_TRUE(CreateTrialFromStudy(&study)); |
| + EXPECT_EQ(kFlagGroup1Name, base::FieldTrialList::FindFullName(study.name())); |
| + |
| + const TestOverrideStringCallback::OverrideMap& overrides = |
| + override_callback_.overrides(); |
| + EXPECT_EQ(1u, overrides.size()); |
| + TestOverrideStringCallback::OverrideMap::const_iterator it = |
| + overrides.find(1234); |
| + EXPECT_NE(overrides.end(), it); |
| + EXPECT_EQ(base::ASCIIToUTF16("test"), it->second); |
| +} |
| + |
| TEST_F(VariationsSeedProcessorTest, ValidateStudy) { |
| Study study; |
| study.set_default_experiment_name("def"); |
| @@ -323,7 +418,8 @@ TEST_F(VariationsSeedProcessorTest, StartsActive) { |
| seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(), |
| base::Version("20.0.0.0"), |
| Study_Channel_STABLE, |
| - Study_FormFactor_DESKTOP, ""); |
| + Study_FormFactor_DESKTOP, "", |
| + override_callback_.callback()); |
| // Non-specified and ACTIVATION_EXPLICIT should not start active, but |
| // ACTIVATION_AUTO should. |