Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / variations / variations_seed_processor_unittest.cc
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.
4
5 #include "components/variations/variations_seed_processor.h"
6
7 #include <vector>
8
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"
14
15 namespace chrome_variations {
16
17 namespace {
18
19 // Converts |time| to Study proto format.
20 int64 TimeToProtoTime(const base::Time& time) {
21   return (time - base::Time::UnixEpoch()).InSeconds();
22 }
23
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";
31
32 const VariationID kExperimentId = 123;
33
34 // Adds an experiment to |study| with the specified |name| and |probability|.
35 Study_Experiment* AddExperiment(const std::string& name, int probability,
36                                 Study* study) {
37   Study_Experiment* experiment = study->add_experiment();
38   experiment->set_name(name);
39   experiment->set_probability_weight(probability);
40   return experiment;
41 }
42
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);
53   Study study;
54   study.set_name(kFlagStudyName);
55   study.set_default_experiment_name(kNonFlagGroupName);
56
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);
62
63   return study;
64 }
65
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)
72       return true;
73   }
74   return false;
75 }
76
77 }  // namespace
78
79 class VariationsSeedProcessorTest : public ::testing::Test {
80  public:
81   VariationsSeedProcessorTest() {
82   }
83
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();
89   }
90
91   bool CreateTrialFromStudy(const Study* study) {
92     ProcessedStudy processed_study;
93     if (processed_study.Init(study, false)) {
94       VariationsSeedProcessor().CreateTrialFromStudy(processed_study);
95       return true;
96     }
97     return false;
98   }
99
100  private:
101   DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessorTest);
102 };
103
104 TEST_F(VariationsSeedProcessorTest, AllowForceGroupAndVariationId) {
105   CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
106
107   base::FieldTrialList field_trial_list(NULL);
108
109   Study study = CreateStudyWithFlagGroups(100, 0, 0);
110   study.mutable_experiment(1)->set_google_web_experiment_id(kExperimentId);
111
112   EXPECT_TRUE(CreateTrialFromStudy(&study));
113   EXPECT_EQ(kFlagGroup1Name,
114             base::FieldTrialList::FindFullName(kFlagStudyName));
115
116   VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, kFlagStudyName,
117                                         kFlagGroup1Name);
118   EXPECT_EQ(kExperimentId, id);
119 }
120
121 TEST_F(VariationsSeedProcessorTest, CheckStudyChannel) {
122   VariationsSeedProcessor seed_processor;
123
124   const Study_Channel channels[] = {
125     Study_Channel_CANARY,
126     Study_Channel_DEV,
127     Study_Channel_BETA,
128     Study_Channel_STABLE,
129   };
130   bool channel_added[arraysize(channels)] = { 0 };
131
132   Study_Filter filter;
133
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!";
141     }
142
143     if (i < arraysize(channels)) {
144       filter.add_channel(channels[i]);
145       channel_added[i] = true;
146     }
147   }
148
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!";
157     }
158
159     if (i < arraysize(channels)) {
160       const int index = arraysize(channels) - i - 1;
161       filter.add_channel(channels[index]);
162       channel_added[index] = true;
163     }
164   }
165 }
166
167 TEST_F(VariationsSeedProcessorTest, CheckStudyFormFactor) {
168   VariationsSeedProcessor seed_processor;
169
170   const Study_FormFactor form_factors[] = {
171     Study_FormFactor_DESKTOP,
172     Study_FormFactor_PHONE,
173     Study_FormFactor_TABLET,
174   };
175
176   ASSERT_EQ(Study_FormFactor_FormFactor_ARRAYSIZE,
177             static_cast<int>(arraysize(form_factors)));
178
179   bool form_factor_added[arraysize(form_factors)] = { 0 };
180   Study_Filter filter;
181
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,
187                                                               form_factors[j]);
188       EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
189     }
190
191     if (i < arraysize(form_factors)) {
192       filter.add_form_factor(form_factors[i]);
193       form_factor_added[i] = true;
194     }
195   }
196
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,
205                                                               form_factors[j]);
206       EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
207     }
208
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;
213     }
214   }
215 }
216
217 TEST_F(VariationsSeedProcessorTest, CheckStudyLocale) {
218   VariationsSeedProcessor seed_processor;
219
220   struct {
221     const char* filter_locales;
222     bool en_us_result;
223     bool en_ca_result;
224     bool fr_result;
225   } test_cases[] = {
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},
233   };
234
235   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
236     std::vector<std::string> filter_locales;
237     Study_Filter filter;
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"));
247   }
248 }
249
250 TEST_F(VariationsSeedProcessorTest, CheckStudyPlatform) {
251   VariationsSeedProcessor seed_processor;
252
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,
260   };
261   ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE,
262             static_cast<int>(arraysize(platforms)));
263   bool platform_added[arraysize(platforms)] = { 0 };
264
265   Study_Filter filter;
266
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,
273                                                             platforms[j]);
274       EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
275     }
276
277     if (i < arraysize(platforms)) {
278       filter.add_platform(platforms[i]);
279       platform_added[i] = true;
280     }
281   }
282
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,
290                                                             platforms[j]);
291       EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
292     }
293
294     if (i < arraysize(platforms)) {
295       const int index = arraysize(platforms) - i - 1;
296       filter.add_platform(platforms[index]);
297       platform_added[index] = true;
298     }
299   }
300 }
301
302 TEST_F(VariationsSeedProcessorTest, CheckStudyStartDate) {
303   VariationsSeedProcessor seed_processor;
304
305   const base::Time now = base::Time::Now();
306   const base::TimeDelta delta = base::TimeDelta::FromHours(1);
307   const struct {
308     const base::Time start_date;
309     bool expected_result;
310   } start_test_cases[] = {
311     { now - delta, true },
312     { now, true },
313     { now + delta, false },
314   };
315
316   Study_Filter filter;
317
318   // Start date not set should result in true.
319   EXPECT_TRUE(seed_processor.CheckStudyStartDate(filter, now));
320
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!";
326   }
327 }
328
329 TEST_F(VariationsSeedProcessorTest, CheckStudyVersion) {
330   VariationsSeedProcessor seed_processor;
331
332   const struct {
333     const char* min_version;
334     const char* 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 },
343     // Wildcards.
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 },
350   };
351
352   const struct {
353     const char* max_version;
354     const char* 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 },
362     // Wildcards
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 },
371   };
372
373   Study_Filter filter;
374
375   // Min/max version not set should result in true.
376   EXPECT_TRUE(seed_processor.CheckStudyVersion(filter, base::Version("1.2.3")));
377
378   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) {
379     filter.set_min_version(min_test_cases[i].min_version);
380     const bool result =
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!";
385   }
386   filter.clear_min_version();
387
388   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) {
389     filter.set_max_version(max_test_cases[i].max_version);
390     const bool result =
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!";
395   }
396
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);
402
403       if (!min_test_cases[i].expected_result) {
404         const bool result =
405             seed_processor.CheckStudyVersion(
406                 filter, Version(min_test_cases[i].version));
407         EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
408       }
409
410       if (!max_test_cases[j].expected_result) {
411         const bool result =
412             seed_processor.CheckStudyVersion(
413                 filter, Version(max_test_cases[j].version));
414         EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
415       }
416     }
417   }
418 }
419
420 TEST_F(VariationsSeedProcessorTest, FilterAndValidateStudies) {
421   const std::string kTrial1Name = "A";
422   const std::string kGroup1Name = "Group1";
423   const std::string kTrial3Name = "B";
424
425   VariationsSeed seed;
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);
431
432   Study* study2 = seed.add_study();
433   *study2 = *study1;
434   study2->mutable_experiment(0)->set_name("Bam");
435   ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
436
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);
442
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);
447
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());
453 }
454
455 // Test that the group for kForcingFlag1 is forced.
456 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag1) {
457   CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
458
459   base::FieldTrialList field_trial_list(NULL);
460
461   Study study = CreateStudyWithFlagGroups(100, 0, 0);
462   EXPECT_TRUE(CreateTrialFromStudy(&study));
463   EXPECT_EQ(kFlagGroup1Name,
464             base::FieldTrialList::FindFullName(kFlagStudyName));
465 }
466
467 // Test that the group for kForcingFlag2 is forced.
468 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag2) {
469   CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2);
470
471   base::FieldTrialList field_trial_list(NULL);
472
473   Study study = CreateStudyWithFlagGroups(100, 0, 0);
474   EXPECT_TRUE(CreateTrialFromStudy(&study));
475   EXPECT_EQ(kFlagGroup2Name,
476             base::FieldTrialList::FindFullName(kFlagStudyName));
477 }
478
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);
483
484   base::FieldTrialList field_trial_list(NULL);
485
486   Study study = CreateStudyWithFlagGroups(100, 0, 0);
487   EXPECT_TRUE(CreateTrialFromStudy(&study));
488   EXPECT_EQ(kFlagGroup1Name,
489             base::FieldTrialList::FindFullName(kFlagStudyName));
490 }
491
492 TEST_F(VariationsSeedProcessorTest, ForceGroup_DontChooseGroupWithFlag) {
493   base::FieldTrialList field_trial_list(NULL);
494
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));
502 }
503
504 TEST_F(VariationsSeedProcessorTest, IsStudyExpired) {
505   VariationsSeedProcessor seed_processor;
506
507   const base::Time now = base::Time::Now();
508   const base::TimeDelta delta = base::TimeDelta::FromHours(1);
509   const struct {
510     const base::Time expiry_date;
511     bool expected_result;
512   } expiry_test_cases[] = {
513     { now - delta, true },
514     { now, true },
515     { now + delta, false },
516   };
517
518   Study study;
519
520   // Expiry date not set should result in false.
521   EXPECT_FALSE(seed_processor.IsStudyExpired(study, now));
522
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!";
528   }
529 }
530
531 TEST_F(VariationsSeedProcessorTest,
532        NonExpiredStudyPrioritizedOverExpiredStudy) {
533   VariationsSeedProcessor seed_processor;
534
535   const std::string kTrialName = "A";
536   const std::string kGroup1Name = "Group1";
537
538   VariationsSeed seed;
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();
545   *study2 = *study1;
546   ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
547
548   const base::Time year_ago =
549       base::Time::Now() - base::TimeDelta::FromDays(365);
550
551   const base::Version version("20.0.0.0");
552
553   // Check that adding [expired, non-expired] activates the non-expired one.
554   ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
555   {
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));
562   }
563
564   // Check that adding [non-expired, expired] activates the non-expired one.
565   ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
566   {
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));
574   }
575 }
576
577 TEST_F(VariationsSeedProcessorTest, ValidateStudy) {
578   Study study;
579   study.set_default_experiment_name("def");
580   AddExperiment("abc", 100, &study);
581   Study_Experiment* default_group = AddExperiment("def", 200, &study);
582
583   ProcessedStudy processed_study;
584   EXPECT_TRUE(processed_study.Init(&study, false));
585   EXPECT_EQ(300, processed_study.total_probability());
586
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));
594
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));
602
603   study.clear_default_experiment_name();
604   EXPECT_FALSE(processed_study.Init(&study, false));
605
606   study.set_default_experiment_name("xyz");
607   EXPECT_FALSE(processed_study.Init(&study, false));
608
609   study.set_default_experiment_name("def");
610   default_group->clear_name();
611   EXPECT_FALSE(processed_study.Init(&study, false));
612
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));
619 }
620
621 TEST_F(VariationsSeedProcessorTest, VariationParams) {
622   base::FieldTrialList field_trial_list(NULL);
623
624   Study study;
625   study.set_name("Study1");
626   study.set_default_experiment_name("B");
627
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");
632
633   Study_Experiment* experiment2 = AddExperiment("B", 0, &study);
634
635   EXPECT_TRUE(CreateTrialFromStudy(&study));
636   EXPECT_EQ("y", GetVariationParamValue("Study1", "x"));
637
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"));
643 }
644
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");
651
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"));
657 }
658
659 TEST_F(VariationsSeedProcessorTest, StartsActive) {
660   base::FieldTrialList field_trial_list(NULL);
661
662   VariationsSeed seed;
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);
668
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);
675
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);
682
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);
688
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"));
694
695   EXPECT_EQ("AA", base::FieldTrialList::FindFullName("A"));
696   EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B"));
697   EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C"));
698
699   // Now, all studies should be active.
700   EXPECT_TRUE(IsFieldTrialActive("A"));
701   EXPECT_TRUE(IsFieldTrialActive("B"));
702   EXPECT_TRUE(IsFieldTrialActive("C"));
703 }
704
705 TEST_F(VariationsSeedProcessorTest, StartsActiveWithFlag) {
706   CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
707
708   base::FieldTrialList field_trial_list(NULL);
709
710   Study study = CreateStudyWithFlagGroups(100, 0, 0);
711   study.set_activation_type(Study_ActivationType_ACTIVATION_AUTO);
712
713   EXPECT_TRUE(CreateTrialFromStudy(&study));
714   EXPECT_TRUE(IsFieldTrialActive(kFlagStudyName));
715
716   EXPECT_EQ(kFlagGroup1Name,
717             base::FieldTrialList::FindFullName(kFlagStudyName));
718 }
719
720 }  // namespace chrome_variations