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/processed_study.h"
12 #include "components/variations/variations_associated_data.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace chrome_variations {
19 // Converts |time| to Study proto format.
20 int64 TimeToProtoTime(const base::Time& time) {
21 return (time - base::Time::UnixEpoch()).InSeconds();
24 // Constants for testing associating command line flags with trial groups.
25 const char kFlagStudyName[] = "flag_test_trial";
26 const char kFlagGroup1Name[] = "flag_group1";
27 const char kFlagGroup2Name[] = "flag_group2";
28 const char kNonFlagGroupName[] = "non_flag_group";
29 const char kForcingFlag1[] = "flag_test1";
30 const char kForcingFlag2[] = "flag_test2";
32 const VariationID kExperimentId = 123;
34 // Adds an experiment to |study| with the specified |name| and |probability|.
35 Study_Experiment* AddExperiment(const std::string& name, int probability,
37 Study_Experiment* experiment = study->add_experiment();
38 experiment->set_name(name);
39 experiment->set_probability_weight(probability);
43 // Populates |study| with test data used for testing associating command line
44 // flags with trials groups. The study will contain three groups, a default
45 // group that isn't associated with a flag, and two other groups, both
46 // associated with different flags.
47 Study CreateStudyWithFlagGroups(int default_group_probability,
48 int flag_group1_probability,
49 int flag_group2_probability) {
50 DCHECK_GE(default_group_probability, 0);
51 DCHECK_GE(flag_group1_probability, 0);
52 DCHECK_GE(flag_group2_probability, 0);
54 study.set_name(kFlagStudyName);
55 study.set_default_experiment_name(kNonFlagGroupName);
57 AddExperiment(kNonFlagGroupName, default_group_probability, &study);
58 AddExperiment(kFlagGroup1Name, flag_group1_probability, &study)
59 ->set_forcing_flag(kForcingFlag1);
60 AddExperiment(kFlagGroup2Name, flag_group2_probability, &study)
61 ->set_forcing_flag(kForcingFlag2);
66 // Tests whether a field trial is active (i.e. group() has been called on it).
67 bool IsFieldTrialActive(const std::string& trial_name) {
68 base::FieldTrial::ActiveGroups active_groups;
69 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
70 for (size_t i = 0; i < active_groups.size(); ++i) {
71 if (active_groups[i].trial_name == trial_name)
79 class VariationsSeedProcessorTest : public ::testing::Test {
81 VariationsSeedProcessorTest() {
84 virtual ~VariationsSeedProcessorTest() {
85 // Ensure that the maps are cleared between tests, since they are stored as
86 // process singletons.
87 testing::ClearAllVariationIDs();
88 testing::ClearAllVariationParams();
91 bool CreateTrialFromStudy(const Study* study) {
92 ProcessedStudy processed_study;
93 if (processed_study.Init(study, false)) {
94 VariationsSeedProcessor().CreateTrialFromStudy(processed_study);
101 DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessorTest);
104 TEST_F(VariationsSeedProcessorTest, AllowForceGroupAndVariationId) {
105 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
107 base::FieldTrialList field_trial_list(NULL);
109 Study study = CreateStudyWithFlagGroups(100, 0, 0);
110 study.mutable_experiment(1)->set_google_web_experiment_id(kExperimentId);
112 EXPECT_TRUE(CreateTrialFromStudy(&study));
113 EXPECT_EQ(kFlagGroup1Name,
114 base::FieldTrialList::FindFullName(kFlagStudyName));
116 VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, kFlagStudyName,
118 EXPECT_EQ(kExperimentId, id);
121 TEST_F(VariationsSeedProcessorTest, CheckStudyChannel) {
122 VariationsSeedProcessor seed_processor;
124 const Study_Channel channels[] = {
125 Study_Channel_CANARY,
128 Study_Channel_STABLE,
130 bool channel_added[arraysize(channels)] = { 0 };
134 // Check in the forwarded order. The loop cond is <= arraysize(channels)
135 // instead of < so that the result of adding the last channel gets checked.
136 for (size_t i = 0; i <= arraysize(channels); ++i) {
137 for (size_t j = 0; j < arraysize(channels); ++j) {
138 const bool expected = channel_added[j] || filter.channel_size() == 0;
139 const bool result = seed_processor.CheckStudyChannel(filter, channels[j]);
140 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
143 if (i < arraysize(channels)) {
144 filter.add_channel(channels[i]);
145 channel_added[i] = true;
149 // Do the same check in the reverse order.
150 filter.clear_channel();
151 memset(&channel_added, 0, sizeof(channel_added));
152 for (size_t i = 0; i <= arraysize(channels); ++i) {
153 for (size_t j = 0; j < arraysize(channels); ++j) {
154 const bool expected = channel_added[j] || filter.channel_size() == 0;
155 const bool result = seed_processor.CheckStudyChannel(filter, channels[j]);
156 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
159 if (i < arraysize(channels)) {
160 const int index = arraysize(channels) - i - 1;
161 filter.add_channel(channels[index]);
162 channel_added[index] = true;
167 TEST_F(VariationsSeedProcessorTest, CheckStudyFormFactor) {
168 VariationsSeedProcessor seed_processor;
170 const Study_FormFactor form_factors[] = {
171 Study_FormFactor_DESKTOP,
172 Study_FormFactor_PHONE,
173 Study_FormFactor_TABLET,
176 ASSERT_EQ(Study_FormFactor_FormFactor_ARRAYSIZE,
177 static_cast<int>(arraysize(form_factors)));
179 bool form_factor_added[arraysize(form_factors)] = { 0 };
182 for (size_t i = 0; i <= arraysize(form_factors); ++i) {
183 for (size_t j = 0; j < arraysize(form_factors); ++j) {
184 const bool expected = form_factor_added[j] ||
185 filter.form_factor_size() == 0;
186 const bool result = seed_processor.CheckStudyFormFactor(filter,
188 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
191 if (i < arraysize(form_factors)) {
192 filter.add_form_factor(form_factors[i]);
193 form_factor_added[i] = true;
197 // Do the same check in the reverse order.
198 filter.clear_form_factor();
199 memset(&form_factor_added, 0, sizeof(form_factor_added));
200 for (size_t i = 0; i <= arraysize(form_factors); ++i) {
201 for (size_t j = 0; j < arraysize(form_factors); ++j) {
202 const bool expected = form_factor_added[j] ||
203 filter.form_factor_size() == 0;
204 const bool result = seed_processor.CheckStudyFormFactor(filter,
206 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
209 if (i < arraysize(form_factors)) {
210 const int index = arraysize(form_factors) - i - 1;;
211 filter.add_form_factor(form_factors[index]);
212 form_factor_added[index] = true;
217 TEST_F(VariationsSeedProcessorTest, CheckStudyLocale) {
218 VariationsSeedProcessor seed_processor;
221 const char* filter_locales;
226 {"en-US", true, false, false},
227 {"en-US,en-CA,fr", true, true, true},
228 {"en-US,en-CA,en-GB", true, true, false},
229 {"en-GB,en-CA,en-US", true, true, false},
230 {"ja,kr,vi", false, false, false},
231 {"fr-CA", false, false, false},
232 {"", true, true, true},
235 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
236 std::vector<std::string> filter_locales;
238 base::SplitString(test_cases[i].filter_locales, ',', &filter_locales);
239 for (size_t j = 0; j < filter_locales.size(); ++j)
240 filter.add_locale(filter_locales[j]);
241 EXPECT_EQ(test_cases[i].en_us_result,
242 seed_processor.CheckStudyLocale(filter, "en-US"));
243 EXPECT_EQ(test_cases[i].en_ca_result,
244 seed_processor.CheckStudyLocale(filter, "en-CA"));
245 EXPECT_EQ(test_cases[i].fr_result,
246 seed_processor.CheckStudyLocale(filter, "fr"));
250 TEST_F(VariationsSeedProcessorTest, CheckStudyPlatform) {
251 VariationsSeedProcessor seed_processor;
253 const Study_Platform platforms[] = {
254 Study_Platform_PLATFORM_WINDOWS,
255 Study_Platform_PLATFORM_MAC,
256 Study_Platform_PLATFORM_LINUX,
257 Study_Platform_PLATFORM_CHROMEOS,
258 Study_Platform_PLATFORM_ANDROID,
259 Study_Platform_PLATFORM_IOS,
261 ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE,
262 static_cast<int>(arraysize(platforms)));
263 bool platform_added[arraysize(platforms)] = { 0 };
267 // Check in the forwarded order. The loop cond is <= arraysize(platforms)
268 // instead of < so that the result of adding the last channel gets checked.
269 for (size_t i = 0; i <= arraysize(platforms); ++i) {
270 for (size_t j = 0; j < arraysize(platforms); ++j) {
271 const bool expected = platform_added[j] || filter.platform_size() == 0;
272 const bool result = seed_processor.CheckStudyPlatform(filter,
274 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
277 if (i < arraysize(platforms)) {
278 filter.add_platform(platforms[i]);
279 platform_added[i] = true;
283 // Do the same check in the reverse order.
284 filter.clear_platform();
285 memset(&platform_added, 0, sizeof(platform_added));
286 for (size_t i = 0; i <= arraysize(platforms); ++i) {
287 for (size_t j = 0; j < arraysize(platforms); ++j) {
288 const bool expected = platform_added[j] || filter.platform_size() == 0;
289 const bool result = seed_processor.CheckStudyPlatform(filter,
291 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
294 if (i < arraysize(platforms)) {
295 const int index = arraysize(platforms) - i - 1;
296 filter.add_platform(platforms[index]);
297 platform_added[index] = true;
302 TEST_F(VariationsSeedProcessorTest, CheckStudyStartDate) {
303 VariationsSeedProcessor seed_processor;
305 const base::Time now = base::Time::Now();
306 const base::TimeDelta delta = base::TimeDelta::FromHours(1);
308 const base::Time start_date;
309 bool expected_result;
310 } start_test_cases[] = {
311 { now - delta, true },
313 { now + delta, false },
318 // Start date not set should result in true.
319 EXPECT_TRUE(seed_processor.CheckStudyStartDate(filter, now));
321 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) {
322 filter.set_start_date(TimeToProtoTime(start_test_cases[i].start_date));
323 const bool result = seed_processor.CheckStudyStartDate(filter, now);
324 EXPECT_EQ(start_test_cases[i].expected_result, result)
325 << "Case " << i << " failed!";
329 TEST_F(VariationsSeedProcessorTest, CheckStudyVersion) {
330 VariationsSeedProcessor seed_processor;
333 const char* min_version;
335 bool expected_result;
336 } min_test_cases[] = {
337 { "1.2.2", "1.2.3", true },
338 { "1.2.3", "1.2.3", true },
339 { "1.2.4", "1.2.3", false },
340 { "1.3.2", "1.2.3", false },
341 { "2.1.2", "1.2.3", false },
342 { "0.3.4", "1.2.3", true },
344 { "1.*", "1.2.3", true },
345 { "1.2.*", "1.2.3", true },
346 { "1.2.3.*", "1.2.3", true },
347 { "1.2.4.*", "1.2.3", false },
348 { "2.*", "1.2.3", false },
349 { "0.3.*", "1.2.3", true },
353 const char* max_version;
355 bool expected_result;
356 } max_test_cases[] = {
357 { "1.2.2", "1.2.3", false },
358 { "1.2.3", "1.2.3", true },
359 { "1.2.4", "1.2.3", true },
360 { "2.1.1", "1.2.3", true },
361 { "2.1.1", "2.3.4", false },
363 { "2.1.*", "2.3.4", false },
364 { "2.*", "2.3.4", true },
365 { "2.3.*", "2.3.4", true },
366 { "2.3.4.*", "2.3.4", true },
367 { "2.3.4.0.*", "2.3.4", true },
368 { "2.4.*", "2.3.4", true },
369 { "1.3.*", "2.3.4", false },
370 { "1.*", "2.3.4", false },
375 // Min/max version not set should result in true.
376 EXPECT_TRUE(seed_processor.CheckStudyVersion(filter, base::Version("1.2.3")));
378 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) {
379 filter.set_min_version(min_test_cases[i].min_version);
381 seed_processor.CheckStudyVersion(filter,
382 Version(min_test_cases[i].version));
383 EXPECT_EQ(min_test_cases[i].expected_result, result) <<
384 "Min. version case " << i << " failed!";
386 filter.clear_min_version();
388 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) {
389 filter.set_max_version(max_test_cases[i].max_version);
391 seed_processor.CheckStudyVersion(filter,
392 Version(max_test_cases[i].version));
393 EXPECT_EQ(max_test_cases[i].expected_result, result) <<
394 "Max version case " << i << " failed!";
397 // Check intersection semantics.
398 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) {
399 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(max_test_cases); ++j) {
400 filter.set_min_version(min_test_cases[i].min_version);
401 filter.set_max_version(max_test_cases[j].max_version);
403 if (!min_test_cases[i].expected_result) {
405 seed_processor.CheckStudyVersion(
406 filter, Version(min_test_cases[i].version));
407 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
410 if (!max_test_cases[j].expected_result) {
412 seed_processor.CheckStudyVersion(
413 filter, Version(max_test_cases[j].version));
414 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
420 TEST_F(VariationsSeedProcessorTest, FilterAndValidateStudies) {
421 const std::string kTrial1Name = "A";
422 const std::string kGroup1Name = "Group1";
423 const std::string kTrial3Name = "B";
426 Study* study1 = seed.add_study();
427 study1->set_name(kTrial1Name);
428 study1->set_default_experiment_name("Default");
429 AddExperiment(kGroup1Name, 100, study1);
430 AddExperiment("Default", 0, study1);
432 Study* study2 = seed.add_study();
434 study2->mutable_experiment(0)->set_name("Bam");
435 ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
437 Study* study3 = seed.add_study();
438 study3->set_name(kTrial3Name);
439 study3->set_default_experiment_name("Default");
440 AddExperiment("A", 10, study3);
441 AddExperiment("Default", 25, study3);
443 std::vector<ProcessedStudy> processed_studies;
444 VariationsSeedProcessor().FilterAndValidateStudies(
445 seed, "en-CA", base::Time::Now(), base::Version("20.0.0.0"),
446 Study_Channel_STABLE, Study_FormFactor_DESKTOP, &processed_studies);
448 // Check that only the first kTrial1Name study was kept.
449 ASSERT_EQ(2U, processed_studies.size());
450 EXPECT_EQ(kTrial1Name, processed_studies[0].study()->name());
451 EXPECT_EQ(kGroup1Name, processed_studies[0].study()->experiment(0).name());
452 EXPECT_EQ(kTrial3Name, processed_studies[1].study()->name());
455 // Test that the group for kForcingFlag1 is forced.
456 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag1) {
457 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
459 base::FieldTrialList field_trial_list(NULL);
461 Study study = CreateStudyWithFlagGroups(100, 0, 0);
462 EXPECT_TRUE(CreateTrialFromStudy(&study));
463 EXPECT_EQ(kFlagGroup1Name,
464 base::FieldTrialList::FindFullName(kFlagStudyName));
467 // Test that the group for kForcingFlag2 is forced.
468 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag2) {
469 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2);
471 base::FieldTrialList field_trial_list(NULL);
473 Study study = CreateStudyWithFlagGroups(100, 0, 0);
474 EXPECT_TRUE(CreateTrialFromStudy(&study));
475 EXPECT_EQ(kFlagGroup2Name,
476 base::FieldTrialList::FindFullName(kFlagStudyName));
479 TEST_F(VariationsSeedProcessorTest, ForceGroup_ChooseFirstGroupWithFlag) {
480 // Add the flag to the command line arguments so the flag group is forced.
481 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
482 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2);
484 base::FieldTrialList field_trial_list(NULL);
486 Study study = CreateStudyWithFlagGroups(100, 0, 0);
487 EXPECT_TRUE(CreateTrialFromStudy(&study));
488 EXPECT_EQ(kFlagGroup1Name,
489 base::FieldTrialList::FindFullName(kFlagStudyName));
492 TEST_F(VariationsSeedProcessorTest, ForceGroup_DontChooseGroupWithFlag) {
493 base::FieldTrialList field_trial_list(NULL);
495 // The two flag groups are given high probability, which would normally make
496 // them very likely to be chosen. They won't be chosen since flag groups are
497 // never chosen when their flag isn't present.
498 Study study = CreateStudyWithFlagGroups(1, 999, 999);
499 EXPECT_TRUE(CreateTrialFromStudy(&study));
500 EXPECT_EQ(kNonFlagGroupName,
501 base::FieldTrialList::FindFullName(kFlagStudyName));
504 TEST_F(VariationsSeedProcessorTest, IsStudyExpired) {
505 VariationsSeedProcessor seed_processor;
507 const base::Time now = base::Time::Now();
508 const base::TimeDelta delta = base::TimeDelta::FromHours(1);
510 const base::Time expiry_date;
511 bool expected_result;
512 } expiry_test_cases[] = {
513 { now - delta, true },
515 { now + delta, false },
520 // Expiry date not set should result in false.
521 EXPECT_FALSE(seed_processor.IsStudyExpired(study, now));
523 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) {
524 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date));
525 const bool result = seed_processor.IsStudyExpired(study, now);
526 EXPECT_EQ(expiry_test_cases[i].expected_result, result)
527 << "Case " << i << " failed!";
531 TEST_F(VariationsSeedProcessorTest,
532 NonExpiredStudyPrioritizedOverExpiredStudy) {
533 VariationsSeedProcessor seed_processor;
535 const std::string kTrialName = "A";
536 const std::string kGroup1Name = "Group1";
539 Study* study1 = seed.add_study();
540 study1->set_name(kTrialName);
541 study1->set_default_experiment_name("Default");
542 AddExperiment(kGroup1Name, 100, study1);
543 AddExperiment("Default", 0, study1);
544 Study* study2 = seed.add_study();
546 ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
548 const base::Time year_ago =
549 base::Time::Now() - base::TimeDelta::FromDays(365);
551 const base::Version version("20.0.0.0");
553 // Check that adding [expired, non-expired] activates the non-expired one.
554 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
556 base::FieldTrialList field_trial_list(NULL);
557 study1->set_expiry_date(TimeToProtoTime(year_ago));
558 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(),
559 version, Study_Channel_STABLE,
560 Study_FormFactor_DESKTOP);
561 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName));
564 // Check that adding [non-expired, expired] activates the non-expired one.
565 ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
567 base::FieldTrialList field_trial_list(NULL);
568 study1->clear_expiry_date();
569 study2->set_expiry_date(TimeToProtoTime(year_ago));
570 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(),
571 version, Study_Channel_STABLE,
572 Study_FormFactor_DESKTOP);
573 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName));
577 TEST_F(VariationsSeedProcessorTest, ValidateStudy) {
579 study.set_default_experiment_name("def");
580 AddExperiment("abc", 100, &study);
581 Study_Experiment* default_group = AddExperiment("def", 200, &study);
583 ProcessedStudy processed_study;
584 EXPECT_TRUE(processed_study.Init(&study, false));
585 EXPECT_EQ(300, processed_study.total_probability());
587 // Min version checks.
588 study.mutable_filter()->set_min_version("1.2.3.*");
589 EXPECT_TRUE(processed_study.Init(&study, false));
590 study.mutable_filter()->set_min_version("1.*.3");
591 EXPECT_FALSE(processed_study.Init(&study, false));
592 study.mutable_filter()->set_min_version("1.2.3");
593 EXPECT_TRUE(processed_study.Init(&study, false));
595 // Max version checks.
596 study.mutable_filter()->set_max_version("2.3.4.*");
597 EXPECT_TRUE(processed_study.Init(&study, false));
598 study.mutable_filter()->set_max_version("*.3");
599 EXPECT_FALSE(processed_study.Init(&study, false));
600 study.mutable_filter()->set_max_version("2.3.4");
601 EXPECT_TRUE(processed_study.Init(&study, false));
603 study.clear_default_experiment_name();
604 EXPECT_FALSE(processed_study.Init(&study, false));
606 study.set_default_experiment_name("xyz");
607 EXPECT_FALSE(processed_study.Init(&study, false));
609 study.set_default_experiment_name("def");
610 default_group->clear_name();
611 EXPECT_FALSE(processed_study.Init(&study, false));
613 default_group->set_name("def");
614 EXPECT_TRUE(processed_study.Init(&study, false));
615 Study_Experiment* repeated_group = study.add_experiment();
616 repeated_group->set_name("abc");
617 repeated_group->set_probability_weight(1);
618 EXPECT_FALSE(processed_study.Init(&study, false));
621 TEST_F(VariationsSeedProcessorTest, VariationParams) {
622 base::FieldTrialList field_trial_list(NULL);
625 study.set_name("Study1");
626 study.set_default_experiment_name("B");
628 Study_Experiment* experiment1 = AddExperiment("A", 1, &study);
629 Study_Experiment_Param* param = experiment1->add_param();
630 param->set_name("x");
631 param->set_value("y");
633 Study_Experiment* experiment2 = AddExperiment("B", 0, &study);
635 EXPECT_TRUE(CreateTrialFromStudy(&study));
636 EXPECT_EQ("y", GetVariationParamValue("Study1", "x"));
638 study.set_name("Study2");
639 experiment1->set_probability_weight(0);
640 experiment2->set_probability_weight(1);
641 EXPECT_TRUE(CreateTrialFromStudy(&study));
642 EXPECT_EQ(std::string(), GetVariationParamValue("Study2", "x"));
645 TEST_F(VariationsSeedProcessorTest, VariationParamsWithForcingFlag) {
646 Study study = CreateStudyWithFlagGroups(100, 0, 0);
647 ASSERT_EQ(kForcingFlag1, study.experiment(1).forcing_flag());
648 Study_Experiment_Param* param = study.mutable_experiment(1)->add_param();
649 param->set_name("x");
650 param->set_value("y");
652 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
653 base::FieldTrialList field_trial_list(NULL);
654 EXPECT_TRUE(CreateTrialFromStudy(&study));
655 EXPECT_EQ(kFlagGroup1Name, base::FieldTrialList::FindFullName(study.name()));
656 EXPECT_EQ("y", GetVariationParamValue(study.name(), "x"));
659 TEST_F(VariationsSeedProcessorTest, StartsActive) {
660 base::FieldTrialList field_trial_list(NULL);
663 Study* study1 = seed.add_study();
664 study1->set_name("A");
665 study1->set_default_experiment_name("Default");
666 AddExperiment("AA", 100, study1);
667 AddExperiment("Default", 0, study1);
669 Study* study2 = seed.add_study();
670 study2->set_name("B");
671 study2->set_default_experiment_name("Default");
672 AddExperiment("BB", 100, study2);
673 AddExperiment("Default", 0, study2);
674 study2->set_activation_type(Study_ActivationType_ACTIVATION_AUTO);
676 Study* study3 = seed.add_study();
677 study3->set_name("C");
678 study3->set_default_experiment_name("Default");
679 AddExperiment("CC", 100, study3);
680 AddExperiment("Default", 0, study3);
681 study3->set_activation_type(Study_ActivationType_ACTIVATION_EXPLICIT);
683 VariationsSeedProcessor seed_processor;
684 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(),
685 base::Version("20.0.0.0"),
686 Study_Channel_STABLE,
687 Study_FormFactor_DESKTOP);
689 // Non-specified and ACTIVATION_EXPLICIT should not start active, but
690 // ACTIVATION_AUTO should.
691 EXPECT_FALSE(IsFieldTrialActive("A"));
692 EXPECT_TRUE(IsFieldTrialActive("B"));
693 EXPECT_FALSE(IsFieldTrialActive("C"));
695 EXPECT_EQ("AA", base::FieldTrialList::FindFullName("A"));
696 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B"));
697 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C"));
699 // Now, all studies should be active.
700 EXPECT_TRUE(IsFieldTrialActive("A"));
701 EXPECT_TRUE(IsFieldTrialActive("B"));
702 EXPECT_TRUE(IsFieldTrialActive("C"));
705 TEST_F(VariationsSeedProcessorTest, StartsActiveWithFlag) {
706 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
708 base::FieldTrialList field_trial_list(NULL);
710 Study study = CreateStudyWithFlagGroups(100, 0, 0);
711 study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO);
713 EXPECT_TRUE(CreateTrialFromStudy(&study));
714 EXPECT_TRUE(IsFieldTrialActive(kFlagStudyName));
716 EXPECT_EQ(kFlagGroup1Name,
717 base::FieldTrialList::FindFullName(kFlagStudyName));
720 } // namespace chrome_variations