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