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_seed_processor.h" | 5 #include "components/variations/variations_seed_processor.h" |
6 | 6 |
| 7 #include <map> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
| 10 #include "base/bind.h" |
9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
10 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
11 #include "components/variations/processed_study.h" | 14 #include "components/variations/processed_study.h" |
12 #include "components/variations/variations_associated_data.h" | 15 #include "components/variations/variations_associated_data.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
14 | 17 |
15 namespace chrome_variations { | 18 namespace chrome_variations { |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
19 // Converts |time| to Study proto format. | 22 // Converts |time| to Study proto format. |
20 int64 TimeToProtoTime(const base::Time& time) { | 23 int64 TimeToProtoTime(const base::Time& time) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 bool IsFieldTrialActive(const std::string& trial_name) { | 70 bool IsFieldTrialActive(const std::string& trial_name) { |
68 base::FieldTrial::ActiveGroups active_groups; | 71 base::FieldTrial::ActiveGroups active_groups; |
69 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 72 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
70 for (size_t i = 0; i < active_groups.size(); ++i) { | 73 for (size_t i = 0; i < active_groups.size(); ++i) { |
71 if (active_groups[i].trial_name == trial_name) | 74 if (active_groups[i].trial_name == trial_name) |
72 return true; | 75 return true; |
73 } | 76 } |
74 return false; | 77 return false; |
75 } | 78 } |
76 | 79 |
| 80 class TestOverrideStringCallback { |
| 81 public: |
| 82 typedef std::map<uint32_t, base::string16> OverrideMap; |
| 83 |
| 84 TestOverrideStringCallback() |
| 85 : callback_(base::Bind(&TestOverrideStringCallback::Override, |
| 86 base::Unretained(this))) {} |
| 87 |
| 88 virtual ~TestOverrideStringCallback() {} |
| 89 |
| 90 const VariationsSeedProcessor::UIStringOverrideCallback& callback() const { |
| 91 return callback_; |
| 92 } |
| 93 |
| 94 const OverrideMap& overrides() const { return overrides_; } |
| 95 |
| 96 private: |
| 97 void Override(uint32_t hash, const base::string16& string) { |
| 98 overrides_[hash] = string; |
| 99 } |
| 100 |
| 101 VariationsSeedProcessor::UIStringOverrideCallback callback_; |
| 102 OverrideMap overrides_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(TestOverrideStringCallback); |
| 105 }; |
| 106 |
77 } // namespace | 107 } // namespace |
78 | 108 |
79 class VariationsSeedProcessorTest : public ::testing::Test { | 109 class VariationsSeedProcessorTest : public ::testing::Test { |
80 public: | 110 public: |
81 VariationsSeedProcessorTest() { | 111 VariationsSeedProcessorTest() { |
82 } | 112 } |
83 | 113 |
84 virtual ~VariationsSeedProcessorTest() { | 114 virtual ~VariationsSeedProcessorTest() { |
85 // Ensure that the maps are cleared between tests, since they are stored as | 115 // Ensure that the maps are cleared between tests, since they are stored as |
86 // process singletons. | 116 // process singletons. |
87 testing::ClearAllVariationIDs(); | 117 testing::ClearAllVariationIDs(); |
88 testing::ClearAllVariationParams(); | 118 testing::ClearAllVariationParams(); |
89 } | 119 } |
90 | 120 |
91 bool CreateTrialFromStudy(const Study* study) { | 121 bool CreateTrialFromStudy(const Study* study) { |
92 ProcessedStudy processed_study; | 122 ProcessedStudy processed_study; |
93 if (processed_study.Init(study, false)) { | 123 if (processed_study.Init(study, false)) { |
94 VariationsSeedProcessor().CreateTrialFromStudy(processed_study); | 124 VariationsSeedProcessor().CreateTrialFromStudy( |
| 125 processed_study, override_callback_.callback()); |
95 return true; | 126 return true; |
96 } | 127 } |
97 return false; | 128 return false; |
98 } | 129 } |
99 | 130 |
| 131 protected: |
| 132 TestOverrideStringCallback override_callback_; |
| 133 |
100 private: | 134 private: |
101 DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessorTest); | 135 DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessorTest); |
102 }; | 136 }; |
103 | 137 |
104 TEST_F(VariationsSeedProcessorTest, AllowForceGroupAndVariationId) { | 138 TEST_F(VariationsSeedProcessorTest, AllowForceGroupAndVariationId) { |
105 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); | 139 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); |
106 | 140 |
107 base::FieldTrialList field_trial_list(NULL); | 141 base::FieldTrialList field_trial_list(NULL); |
108 | 142 |
109 Study study = CreateStudyWithFlagGroups(100, 0, 0); | 143 Study study = CreateStudyWithFlagGroups(100, 0, 0); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 const base::Time year_ago = | 221 const base::Time year_ago = |
188 base::Time::Now() - base::TimeDelta::FromDays(365); | 222 base::Time::Now() - base::TimeDelta::FromDays(365); |
189 | 223 |
190 const base::Version version("20.0.0.0"); | 224 const base::Version version("20.0.0.0"); |
191 | 225 |
192 // Check that adding [expired, non-expired] activates the non-expired one. | 226 // Check that adding [expired, non-expired] activates the non-expired one. |
193 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName)); | 227 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName)); |
194 { | 228 { |
195 base::FieldTrialList field_trial_list(NULL); | 229 base::FieldTrialList field_trial_list(NULL); |
196 study1->set_expiry_date(TimeToProtoTime(year_ago)); | 230 study1->set_expiry_date(TimeToProtoTime(year_ago)); |
197 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(), | 231 seed_processor.CreateTrialsFromSeed(seed, |
198 version, Study_Channel_STABLE, | 232 "en-CA", |
199 Study_FormFactor_DESKTOP, ""); | 233 base::Time::Now(), |
| 234 version, |
| 235 Study_Channel_STABLE, |
| 236 Study_FormFactor_DESKTOP, |
| 237 "", |
| 238 override_callback_.callback()); |
200 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName)); | 239 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName)); |
201 } | 240 } |
202 | 241 |
203 // Check that adding [non-expired, expired] activates the non-expired one. | 242 // Check that adding [non-expired, expired] activates the non-expired one. |
204 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName)); | 243 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName)); |
205 { | 244 { |
206 base::FieldTrialList field_trial_list(NULL); | 245 base::FieldTrialList field_trial_list(NULL); |
207 study1->clear_expiry_date(); | 246 study1->clear_expiry_date(); |
208 study2->set_expiry_date(TimeToProtoTime(year_ago)); | 247 study2->set_expiry_date(TimeToProtoTime(year_ago)); |
209 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(), | 248 seed_processor.CreateTrialsFromSeed(seed, |
210 version, Study_Channel_STABLE, | 249 "en-CA", |
211 Study_FormFactor_DESKTOP, ""); | 250 base::Time::Now(), |
| 251 version, |
| 252 Study_Channel_STABLE, |
| 253 Study_FormFactor_DESKTOP, |
| 254 "", |
| 255 override_callback_.callback()); |
212 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName)); | 256 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName)); |
213 } | 257 } |
214 } | 258 } |
215 | 259 |
| 260 TEST_F(VariationsSeedProcessorTest, OverrideUIStrings) { |
| 261 base::FieldTrialList field_trial_list(NULL); |
| 262 |
| 263 Study study; |
| 264 study.set_name("Study1"); |
| 265 study.set_default_experiment_name("B"); |
| 266 study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO); |
| 267 |
| 268 Study_Experiment* experiment1 = AddExperiment("A", 0, &study); |
| 269 Study_Experiment_OverrideUIString* override = |
| 270 experiment1->add_override_ui_string(); |
| 271 |
| 272 override->set_name_hash(1234); |
| 273 override->set_value("test"); |
| 274 |
| 275 Study_Experiment* experiment2 = AddExperiment("B", 1, &study); |
| 276 |
| 277 EXPECT_TRUE(CreateTrialFromStudy(&study)); |
| 278 |
| 279 const TestOverrideStringCallback::OverrideMap& overrides = |
| 280 override_callback_.overrides(); |
| 281 |
| 282 EXPECT_TRUE(overrides.empty()); |
| 283 |
| 284 study.set_name("Study2"); |
| 285 experiment1->set_probability_weight(1); |
| 286 experiment2->set_probability_weight(0); |
| 287 |
| 288 EXPECT_TRUE(CreateTrialFromStudy(&study)); |
| 289 |
| 290 EXPECT_EQ(1u, overrides.size()); |
| 291 TestOverrideStringCallback::OverrideMap::const_iterator it = |
| 292 overrides.find(1234); |
| 293 EXPECT_EQ(base::ASCIIToUTF16("test"), it->second); |
| 294 } |
| 295 |
| 296 TEST_F(VariationsSeedProcessorTest, OverrideUIStringsWithForcingFlag) { |
| 297 Study study = CreateStudyWithFlagGroups(100, 0, 0); |
| 298 ASSERT_EQ(kForcingFlag1, study.experiment(1).forcing_flag()); |
| 299 |
| 300 study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO); |
| 301 Study_Experiment_OverrideUIString* override = |
| 302 study.mutable_experiment(1)->add_override_ui_string(); |
| 303 override->set_name_hash(1234); |
| 304 override->set_value("test"); |
| 305 |
| 306 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); |
| 307 base::FieldTrialList field_trial_list(NULL); |
| 308 EXPECT_TRUE(CreateTrialFromStudy(&study)); |
| 309 EXPECT_EQ(kFlagGroup1Name, base::FieldTrialList::FindFullName(study.name())); |
| 310 |
| 311 const TestOverrideStringCallback::OverrideMap& overrides = |
| 312 override_callback_.overrides(); |
| 313 EXPECT_EQ(1u, overrides.size()); |
| 314 TestOverrideStringCallback::OverrideMap::const_iterator it = |
| 315 overrides.find(1234); |
| 316 EXPECT_EQ(base::ASCIIToUTF16("test"), it->second); |
| 317 } |
| 318 |
216 TEST_F(VariationsSeedProcessorTest, ValidateStudy) { | 319 TEST_F(VariationsSeedProcessorTest, ValidateStudy) { |
217 Study study; | 320 Study study; |
218 study.set_default_experiment_name("def"); | 321 study.set_default_experiment_name("def"); |
219 AddExperiment("abc", 100, &study); | 322 AddExperiment("abc", 100, &study); |
220 Study_Experiment* default_group = AddExperiment("def", 200, &study); | 323 Study_Experiment* default_group = AddExperiment("def", 200, &study); |
221 | 324 |
222 ProcessedStudy processed_study; | 325 ProcessedStudy processed_study; |
223 EXPECT_TRUE(processed_study.Init(&study, false)); | 326 EXPECT_TRUE(processed_study.Init(&study, false)); |
224 EXPECT_EQ(300, processed_study.total_probability()); | 327 EXPECT_EQ(300, processed_study.total_probability()); |
225 | 328 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 study2->set_activation_type(Study_ActivationType_ACTIVATION_AUTO); | 416 study2->set_activation_type(Study_ActivationType_ACTIVATION_AUTO); |
314 | 417 |
315 Study* study3 = seed.add_study(); | 418 Study* study3 = seed.add_study(); |
316 study3->set_name("C"); | 419 study3->set_name("C"); |
317 study3->set_default_experiment_name("Default"); | 420 study3->set_default_experiment_name("Default"); |
318 AddExperiment("CC", 100, study3); | 421 AddExperiment("CC", 100, study3); |
319 AddExperiment("Default", 0, study3); | 422 AddExperiment("Default", 0, study3); |
320 study3->set_activation_type(Study_ActivationType_ACTIVATION_EXPLICIT); | 423 study3->set_activation_type(Study_ActivationType_ACTIVATION_EXPLICIT); |
321 | 424 |
322 VariationsSeedProcessor seed_processor; | 425 VariationsSeedProcessor seed_processor; |
323 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(), | 426 seed_processor.CreateTrialsFromSeed(seed, |
| 427 "en-CA", |
| 428 base::Time::Now(), |
324 base::Version("20.0.0.0"), | 429 base::Version("20.0.0.0"), |
325 Study_Channel_STABLE, | 430 Study_Channel_STABLE, |
326 Study_FormFactor_DESKTOP, ""); | 431 Study_FormFactor_DESKTOP, |
| 432 "", |
| 433 override_callback_.callback()); |
327 | 434 |
328 // Non-specified and ACTIVATION_EXPLICIT should not start active, but | 435 // Non-specified and ACTIVATION_EXPLICIT should not start active, but |
329 // ACTIVATION_AUTO should. | 436 // ACTIVATION_AUTO should. |
330 EXPECT_FALSE(IsFieldTrialActive("A")); | 437 EXPECT_FALSE(IsFieldTrialActive("A")); |
331 EXPECT_TRUE(IsFieldTrialActive("B")); | 438 EXPECT_TRUE(IsFieldTrialActive("B")); |
332 EXPECT_FALSE(IsFieldTrialActive("C")); | 439 EXPECT_FALSE(IsFieldTrialActive("C")); |
333 | 440 |
334 EXPECT_EQ("AA", base::FieldTrialList::FindFullName("A")); | 441 EXPECT_EQ("AA", base::FieldTrialList::FindFullName("A")); |
335 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B")); | 442 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B")); |
336 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C")); | 443 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C")); |
(...skipping 13 matching lines...) Expand all Loading... |
350 study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO); | 457 study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO); |
351 | 458 |
352 EXPECT_TRUE(CreateTrialFromStudy(&study)); | 459 EXPECT_TRUE(CreateTrialFromStudy(&study)); |
353 EXPECT_TRUE(IsFieldTrialActive(kFlagStudyName)); | 460 EXPECT_TRUE(IsFieldTrialActive(kFlagStudyName)); |
354 | 461 |
355 EXPECT_EQ(kFlagGroup1Name, | 462 EXPECT_EQ(kFlagGroup1Name, |
356 base::FieldTrialList::FindFullName(kFlagStudyName)); | 463 base::FieldTrialList::FindFullName(kFlagStudyName)); |
357 } | 464 } |
358 | 465 |
359 } // namespace chrome_variations | 466 } // namespace chrome_variations |
OLD | NEW |