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
3 // found in the LICENSE file.
5 #include "components/variations/variations_seed_processor.h"
9 #include "base/command_line.h"
10 #include "base/strings/string_split.h"
11 #include "components/variations/variations_associated_data.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace chrome_variations {
18 // Converts |time| to Study proto format.
19 int64 TimeToProtoTime(const base::Time& time) {
20 return (time - base::Time::UnixEpoch()).InSeconds();
23 // Constants for testing associating command line flags with trial groups.
24 const char kFlagStudyName[] = "flag_test_trial";
25 const char kFlagGroup1Name[] = "flag_group1";
26 const char kFlagGroup2Name[] = "flag_group2";
27 const char kNonFlagGroupName[] = "non_flag_group";
28 const char kForcingFlag1[] = "flag_test1";
29 const char kForcingFlag2[] = "flag_test2";
31 // Adds an experiment to |study| with the specified |name| and |probability|.
32 Study_Experiment* AddExperiment(const std::string& name, int probability,
34 Study_Experiment* experiment = study->add_experiment();
35 experiment->set_name(name);
36 experiment->set_probability_weight(probability);
40 // Populates |study| with test data used for testing associating command line
41 // flags with trials groups. The study will contain three groups, a default
42 // group that isn't associated with a flag, and two other groups, both
43 // associated with different flags.
44 Study CreateStudyWithFlagGroups(int default_group_probability,
45 int flag_group1_probability,
46 int flag_group2_probability) {
47 DCHECK_GE(default_group_probability, 0);
48 DCHECK_GE(flag_group1_probability, 0);
49 DCHECK_GE(flag_group2_probability, 0);
51 study.set_name(kFlagStudyName);
52 study.set_default_experiment_name(kNonFlagGroupName);
54 AddExperiment(kNonFlagGroupName, default_group_probability, &study);
55 AddExperiment(kFlagGroup1Name, flag_group1_probability, &study)
56 ->set_forcing_flag(kForcingFlag1);
57 AddExperiment(kFlagGroup2Name, flag_group2_probability, &study)
58 ->set_forcing_flag(kForcingFlag2);
63 // Tests whether a field trial is active (i.e. group() has been called on it).
64 bool IsFieldTrialActive(const std::string& trial_name) {
65 base::FieldTrial::ActiveGroups active_groups;
66 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
67 for (size_t i = 0; i < active_groups.size(); ++i) {
68 if (active_groups[i].trial_name == trial_name)
76 TEST(VariationsSeedProcessorTest, CheckStudyChannel) {
77 VariationsSeedProcessor seed_processor;
79 const Study_Channel channels[] = {
85 bool channel_added[arraysize(channels)] = { 0 };
89 // Check in the forwarded order. The loop cond is <= arraysize(channels)
90 // instead of < so that the result of adding the last channel gets checked.
91 for (size_t i = 0; i <= arraysize(channels); ++i) {
92 for (size_t j = 0; j < arraysize(channels); ++j) {
93 const bool expected = channel_added[j] || filter.channel_size() == 0;
94 const bool result = seed_processor.CheckStudyChannel(filter, channels[j]);
95 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
98 if (i < arraysize(channels)) {
99 filter.add_channel(channels[i]);
100 channel_added[i] = true;
104 // Do the same check in the reverse order.
105 filter.clear_channel();
106 memset(&channel_added, 0, sizeof(channel_added));
107 for (size_t i = 0; i <= arraysize(channels); ++i) {
108 for (size_t j = 0; j < arraysize(channels); ++j) {
109 const bool expected = channel_added[j] || filter.channel_size() == 0;
110 const bool result = seed_processor.CheckStudyChannel(filter, channels[j]);
111 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
114 if (i < arraysize(channels)) {
115 const int index = arraysize(channels) - i - 1;
116 filter.add_channel(channels[index]);
117 channel_added[index] = true;
122 TEST(VariationsSeedProcessorTest, CheckStudyLocale) {
123 VariationsSeedProcessor seed_processor;
126 const char* filter_locales;
131 {"en-US", true, false, false},
132 {"en-US,en-CA,fr", true, true, true},
133 {"en-US,en-CA,en-GB", true, true, false},
134 {"en-GB,en-CA,en-US", true, true, false},
135 {"ja,kr,vi", false, false, false},
136 {"fr-CA", false, false, false},
137 {"", true, true, true},
140 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
141 std::vector<std::string> filter_locales;
143 base::SplitString(test_cases[i].filter_locales, ',', &filter_locales);
144 for (size_t j = 0; j < filter_locales.size(); ++j)
145 filter.add_locale(filter_locales[j]);
146 EXPECT_EQ(test_cases[i].en_us_result,
147 seed_processor.CheckStudyLocale(filter, "en-US"));
148 EXPECT_EQ(test_cases[i].en_ca_result,
149 seed_processor.CheckStudyLocale(filter, "en-CA"));
150 EXPECT_EQ(test_cases[i].fr_result,
151 seed_processor.CheckStudyLocale(filter, "fr"));
155 TEST(VariationsSeedProcessorTest, CheckStudyPlatform) {
156 VariationsSeedProcessor seed_processor;
158 const Study_Platform platforms[] = {
159 Study_Platform_PLATFORM_WINDOWS,
160 Study_Platform_PLATFORM_MAC,
161 Study_Platform_PLATFORM_LINUX,
162 Study_Platform_PLATFORM_CHROMEOS,
163 Study_Platform_PLATFORM_ANDROID,
164 Study_Platform_PLATFORM_IOS,
166 ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE,
167 static_cast<int>(arraysize(platforms)));
168 bool platform_added[arraysize(platforms)] = { 0 };
172 // Check in the forwarded order. The loop cond is <= arraysize(platforms)
173 // instead of < so that the result of adding the last channel gets checked.
174 for (size_t i = 0; i <= arraysize(platforms); ++i) {
175 for (size_t j = 0; j < arraysize(platforms); ++j) {
176 const bool expected = platform_added[j] || filter.platform_size() == 0;
177 const bool result = seed_processor.CheckStudyPlatform(filter,
179 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
182 if (i < arraysize(platforms)) {
183 filter.add_platform(platforms[i]);
184 platform_added[i] = true;
188 // Do the same check in the reverse order.
189 filter.clear_platform();
190 memset(&platform_added, 0, sizeof(platform_added));
191 for (size_t i = 0; i <= arraysize(platforms); ++i) {
192 for (size_t j = 0; j < arraysize(platforms); ++j) {
193 const bool expected = platform_added[j] || filter.platform_size() == 0;
194 const bool result = seed_processor.CheckStudyPlatform(filter,
196 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
199 if (i < arraysize(platforms)) {
200 const int index = arraysize(platforms) - i - 1;
201 filter.add_platform(platforms[index]);
202 platform_added[index] = true;
207 TEST(VariationsSeedProcessorTest, CheckStudyStartDate) {
208 VariationsSeedProcessor seed_processor;
210 const base::Time now = base::Time::Now();
211 const base::TimeDelta delta = base::TimeDelta::FromHours(1);
213 const base::Time start_date;
214 bool expected_result;
215 } start_test_cases[] = {
216 { now - delta, true },
218 { now + delta, false },
223 // Start date not set should result in true.
224 EXPECT_TRUE(seed_processor.CheckStudyStartDate(filter, now));
226 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) {
227 filter.set_start_date(TimeToProtoTime(start_test_cases[i].start_date));
228 const bool result = seed_processor.CheckStudyStartDate(filter, now);
229 EXPECT_EQ(start_test_cases[i].expected_result, result)
230 << "Case " << i << " failed!";
234 TEST(VariationsSeedProcessorTest, CheckStudyVersion) {
235 VariationsSeedProcessor seed_processor;
238 const char* min_version;
240 bool expected_result;
241 } min_test_cases[] = {
242 { "1.2.2", "1.2.3", true },
243 { "1.2.3", "1.2.3", true },
244 { "1.2.4", "1.2.3", false },
245 { "1.3.2", "1.2.3", false },
246 { "2.1.2", "1.2.3", false },
247 { "0.3.4", "1.2.3", true },
249 { "1.*", "1.2.3", true },
250 { "1.2.*", "1.2.3", true },
251 { "1.2.3.*", "1.2.3", true },
252 { "1.2.4.*", "1.2.3", false },
253 { "2.*", "1.2.3", false },
254 { "0.3.*", "1.2.3", true },
258 const char* max_version;
260 bool expected_result;
261 } max_test_cases[] = {
262 { "1.2.2", "1.2.3", false },
263 { "1.2.3", "1.2.3", true },
264 { "1.2.4", "1.2.3", true },
265 { "2.1.1", "1.2.3", true },
266 { "2.1.1", "2.3.4", false },
268 { "2.1.*", "2.3.4", false },
269 { "2.*", "2.3.4", true },
270 { "2.3.*", "2.3.4", true },
271 { "2.3.4.*", "2.3.4", true },
272 { "2.3.4.0.*", "2.3.4", true },
273 { "2.4.*", "2.3.4", true },
274 { "1.3.*", "2.3.4", false },
275 { "1.*", "2.3.4", false },
280 // Min/max version not set should result in true.
281 EXPECT_TRUE(seed_processor.CheckStudyVersion(filter, base::Version("1.2.3")));
283 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) {
284 filter.set_min_version(min_test_cases[i].min_version);
286 seed_processor.CheckStudyVersion(filter,
287 Version(min_test_cases[i].version));
288 EXPECT_EQ(min_test_cases[i].expected_result, result) <<
289 "Min. version case " << i << " failed!";
291 filter.clear_min_version();
293 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) {
294 filter.set_max_version(max_test_cases[i].max_version);
296 seed_processor.CheckStudyVersion(filter,
297 Version(max_test_cases[i].version));
298 EXPECT_EQ(max_test_cases[i].expected_result, result) <<
299 "Max version case " << i << " failed!";
302 // Check intersection semantics.
303 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) {
304 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(max_test_cases); ++j) {
305 filter.set_min_version(min_test_cases[i].min_version);
306 filter.set_max_version(max_test_cases[j].max_version);
308 if (!min_test_cases[i].expected_result) {
310 seed_processor.CheckStudyVersion(
311 filter, Version(min_test_cases[i].version));
312 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
315 if (!max_test_cases[j].expected_result) {
317 seed_processor.CheckStudyVersion(
318 filter, Version(max_test_cases[j].version));
319 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
325 // Test that the group for kForcingFlag1 is forced.
326 TEST(VariationsSeedProcessorTest, ForceGroupWithFlag1) {
327 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
329 base::FieldTrialList field_trial_list(NULL);
331 Study study = CreateStudyWithFlagGroups(100, 0, 0);
332 VariationsSeedProcessor().CreateTrialFromStudy(study, false);
334 EXPECT_EQ(kFlagGroup1Name,
335 base::FieldTrialList::FindFullName(kFlagStudyName));
338 // Test that the group for kForcingFlag2 is forced.
339 TEST(VariationsSeedProcessorTest, ForceGroupWithFlag2) {
340 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2);
342 base::FieldTrialList field_trial_list(NULL);
344 Study study = CreateStudyWithFlagGroups(100, 0, 0);
345 VariationsSeedProcessor().CreateTrialFromStudy(study, false);
347 EXPECT_EQ(kFlagGroup2Name,
348 base::FieldTrialList::FindFullName(kFlagStudyName));
351 TEST(VariationsSeedProcessorTest, ForceGroup_ChooseFirstGroupWithFlag) {
352 // Add the flag to the command line arguments so the flag group is forced.
353 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
354 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2);
356 base::FieldTrialList field_trial_list(NULL);
358 Study study = CreateStudyWithFlagGroups(100, 0, 0);
359 VariationsSeedProcessor().CreateTrialFromStudy(study, false);
361 EXPECT_EQ(kFlagGroup1Name,
362 base::FieldTrialList::FindFullName(kFlagStudyName));
365 TEST(VariationsSeedProcessorTest, ForceGroup_DontChooseGroupWithFlag) {
366 base::FieldTrialList field_trial_list(NULL);
368 // The two flag groups are given high probability, which would normally make
369 // them very likely to be chosen. They won't be chosen since flag groups are
370 // never chosen when their flag isn't present.
371 Study study = CreateStudyWithFlagGroups(1, 999, 999);
372 VariationsSeedProcessor().CreateTrialFromStudy(study, false);
373 EXPECT_EQ(kNonFlagGroupName,
374 base::FieldTrialList::FindFullName(kFlagStudyName));
377 TEST(VariationsSeedProcessorTest, IsStudyExpired) {
378 VariationsSeedProcessor seed_processor;
380 const base::Time now = base::Time::Now();
381 const base::TimeDelta delta = base::TimeDelta::FromHours(1);
383 const base::Time expiry_date;
384 bool expected_result;
385 } expiry_test_cases[] = {
386 { now - delta, true },
388 { now + delta, false },
393 // Expiry date not set should result in false.
394 EXPECT_FALSE(seed_processor.IsStudyExpired(study, now));
396 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) {
397 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date));
398 const bool result = seed_processor.IsStudyExpired(study, now);
399 EXPECT_EQ(expiry_test_cases[i].expected_result, result)
400 << "Case " << i << " failed!";
404 TEST(VariationsSeedProcessorTest, NonExpiredStudyPrioritizedOverExpiredStudy) {
405 VariationsSeedProcessor seed_processor;
407 const std::string kTrialName = "A";
408 const std::string kGroup1Name = "Group1";
411 Study* study1 = seed.add_study();
412 study1->set_name(kTrialName);
413 study1->set_default_experiment_name("Default");
414 AddExperiment(kGroup1Name, 100, study1);
415 AddExperiment("Default", 0, study1);
416 Study* study2 = seed.add_study();
418 ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
420 const base::Time year_ago =
421 base::Time::Now() - base::TimeDelta::FromDays(365);
423 const base::Version version("20.0.0.0");
425 // Check that adding [expired, non-expired] activates the non-expired one.
426 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
428 base::FieldTrialList field_trial_list(NULL);
429 study1->set_expiry_date(TimeToProtoTime(year_ago));
430 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(),
431 version, Study_Channel_STABLE);
432 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName));
435 // Check that adding [non-expired, expired] activates the non-expired one.
436 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
438 base::FieldTrialList field_trial_list(NULL);
439 study1->clear_expiry_date();
440 study2->set_expiry_date(TimeToProtoTime(year_ago));
441 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(),
442 version, Study_Channel_STABLE);
443 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName));
447 TEST(VariationsSeedProcessorTest, ValidateStudy) {
448 VariationsSeedProcessor seed_processor;
451 study.set_default_experiment_name("def");
452 AddExperiment("abc", 100, &study);
453 Study_Experiment* default_group = AddExperiment("def", 200, &study);
455 base::FieldTrial::Probability total_probability = 0;
456 bool valid = seed_processor.ValidateStudyAndComputeTotalProbability(
457 study, &total_probability);
459 EXPECT_EQ(300, total_probability);
461 // Min version checks.
462 study.mutable_filter()->set_min_version("1.2.3.*");
463 valid = seed_processor.ValidateStudyAndComputeTotalProbability(
464 study, &total_probability);
466 study.mutable_filter()->set_min_version("1.*.3");
467 valid = seed_processor.ValidateStudyAndComputeTotalProbability(
468 study, &total_probability);
470 study.mutable_filter()->set_min_version("1.2.3");
471 valid = seed_processor.ValidateStudyAndComputeTotalProbability(
472 study, &total_probability);
475 // Max version checks.
476 study.mutable_filter()->set_max_version("2.3.4.*");
477 valid = seed_processor.ValidateStudyAndComputeTotalProbability(
478 study, &total_probability);
480 study.mutable_filter()->set_max_version("*.3");
481 valid = seed_processor.ValidateStudyAndComputeTotalProbability(
482 study, &total_probability);
484 study.mutable_filter()->set_max_version("2.3.4");
485 valid = seed_processor.ValidateStudyAndComputeTotalProbability(
486 study, &total_probability);
489 study.clear_default_experiment_name();
490 valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
494 study.set_default_experiment_name("xyz");
495 valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
499 study.set_default_experiment_name("def");
500 default_group->clear_name();
501 valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
505 default_group->set_name("def");
506 valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
509 Study_Experiment* repeated_group = study.add_experiment();
510 repeated_group->set_name("abc");
511 repeated_group->set_probability_weight(1);
512 valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
517 TEST(VariationsSeedProcessorTest, VariationParams) {
518 base::FieldTrialList field_trial_list(NULL);
519 VariationsSeedProcessor seed_processor;
522 study.set_name("Study1");
523 study.set_default_experiment_name("B");
525 Study_Experiment* experiment1 = AddExperiment("A", 1, &study);
526 Study_Experiment_Param* param = experiment1->add_param();
527 param->set_name("x");
528 param->set_value("y");
530 Study_Experiment* experiment2 = AddExperiment("B", 0, &study);
532 seed_processor.CreateTrialFromStudy(study, false);
533 EXPECT_EQ("y", GetVariationParamValue("Study1", "x"));
535 study.set_name("Study2");
536 experiment1->set_probability_weight(0);
537 experiment2->set_probability_weight(1);
538 seed_processor.CreateTrialFromStudy(study, false);
539 EXPECT_EQ(std::string(), GetVariationParamValue("Study2", "x"));
542 TEST(VariationsSeedProcessorTest, StartsActive) {
543 base::FieldTrialList field_trial_list(NULL);
546 Study* study1 = seed.add_study();
547 study1->set_name("A");
548 study1->set_default_experiment_name("Default");
549 AddExperiment("AA", 100, study1);
550 AddExperiment("Default", 0, study1);
552 Study* study2 = seed.add_study();
553 study2->set_name("B");
554 study2->set_default_experiment_name("Default");
555 AddExperiment("BB", 100, study2);
556 AddExperiment("Default", 0, study2);
557 study2->set_activation_type(Study_ActivationType_ACTIVATION_AUTO);
559 Study* study3 = seed.add_study();
560 study3->set_name("C");
561 study3->set_default_experiment_name("Default");
562 AddExperiment("CC", 100, study3);
563 AddExperiment("Default", 0, study3);
564 study3->set_activation_type(Study_ActivationType_ACTIVATION_EXPLICIT);
566 VariationsSeedProcessor seed_processor;
567 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(),
568 base::Version("20.0.0.0"),
569 Study_Channel_STABLE);
571 // Non-specified and ACTIVATION_EXPLICIT should not start active, but
572 // ACTIVATION_AUTO should.
573 EXPECT_FALSE(IsFieldTrialActive("A"));
574 EXPECT_TRUE(IsFieldTrialActive("B"));
575 EXPECT_FALSE(IsFieldTrialActive("C"));
577 EXPECT_EQ("AA", base::FieldTrialList::FindFullName("A"));
578 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B"));
579 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C"));
581 // Now, all studies should be active.
582 EXPECT_TRUE(IsFieldTrialActive("A"));
583 EXPECT_TRUE(IsFieldTrialActive("B"));
584 EXPECT_TRUE(IsFieldTrialActive("C"));
587 } // namespace chrome_variations