- add sources.
[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/variations_associated_data.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace chrome_variations {
15
16 namespace {
17
18 // Converts |time| to Study proto format.
19 int64 TimeToProtoTime(const base::Time& time) {
20   return (time - base::Time::UnixEpoch()).InSeconds();
21 }
22
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";
30
31 // Adds an experiment to |study| with the specified |name| and |probability|.
32 Study_Experiment* AddExperiment(const std::string& name, int probability,
33                                 Study* study) {
34   Study_Experiment* experiment = study->add_experiment();
35   experiment->set_name(name);
36   experiment->set_probability_weight(probability);
37   return experiment;
38 }
39
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);
50   Study study;
51   study.set_name(kFlagStudyName);
52   study.set_default_experiment_name(kNonFlagGroupName);
53
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);
59
60   return study;
61 }
62
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)
69       return true;
70   }
71   return false;
72 }
73
74 }  // namespace
75
76 TEST(VariationsSeedProcessorTest, CheckStudyChannel) {
77   VariationsSeedProcessor seed_processor;
78
79   const Study_Channel channels[] = {
80     Study_Channel_CANARY,
81     Study_Channel_DEV,
82     Study_Channel_BETA,
83     Study_Channel_STABLE,
84   };
85   bool channel_added[arraysize(channels)] = { 0 };
86
87   Study_Filter filter;
88
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!";
96     }
97
98     if (i < arraysize(channels)) {
99       filter.add_channel(channels[i]);
100       channel_added[i] = true;
101     }
102   }
103
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!";
112     }
113
114     if (i < arraysize(channels)) {
115       const int index = arraysize(channels) - i - 1;
116       filter.add_channel(channels[index]);
117       channel_added[index] = true;
118     }
119   }
120 }
121
122 TEST(VariationsSeedProcessorTest, CheckStudyLocale) {
123   VariationsSeedProcessor seed_processor;
124
125   struct {
126     const char* filter_locales;
127     bool en_us_result;
128     bool en_ca_result;
129     bool fr_result;
130   } test_cases[] = {
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},
138   };
139
140   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
141     std::vector<std::string> filter_locales;
142     Study_Filter filter;
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"));
152   }
153 }
154
155 TEST(VariationsSeedProcessorTest, CheckStudyPlatform) {
156   VariationsSeedProcessor seed_processor;
157
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,
165   };
166   ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE,
167             static_cast<int>(arraysize(platforms)));
168   bool platform_added[arraysize(platforms)] = { 0 };
169
170   Study_Filter filter;
171
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,
178                                                             platforms[j]);
179       EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
180     }
181
182     if (i < arraysize(platforms)) {
183       filter.add_platform(platforms[i]);
184       platform_added[i] = true;
185     }
186   }
187
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,
195                                                             platforms[j]);
196       EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!";
197     }
198
199     if (i < arraysize(platforms)) {
200       const int index = arraysize(platforms) - i - 1;
201       filter.add_platform(platforms[index]);
202       platform_added[index] = true;
203     }
204   }
205 }
206
207 TEST(VariationsSeedProcessorTest, CheckStudyStartDate) {
208   VariationsSeedProcessor seed_processor;
209
210   const base::Time now = base::Time::Now();
211   const base::TimeDelta delta = base::TimeDelta::FromHours(1);
212   const struct {
213     const base::Time start_date;
214     bool expected_result;
215   } start_test_cases[] = {
216     { now - delta, true },
217     { now, true },
218     { now + delta, false },
219   };
220
221   Study_Filter filter;
222
223   // Start date not set should result in true.
224   EXPECT_TRUE(seed_processor.CheckStudyStartDate(filter, now));
225
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!";
231   }
232 }
233
234 TEST(VariationsSeedProcessorTest, CheckStudyVersion) {
235   VariationsSeedProcessor seed_processor;
236
237   const struct {
238     const char* min_version;
239     const char* 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 },
248     // Wildcards.
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 },
255   };
256
257   const struct {
258     const char* max_version;
259     const char* 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 },
267     // Wildcards
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 },
276   };
277
278   Study_Filter filter;
279
280   // Min/max version not set should result in true.
281   EXPECT_TRUE(seed_processor.CheckStudyVersion(filter, base::Version("1.2.3")));
282
283   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) {
284     filter.set_min_version(min_test_cases[i].min_version);
285     const bool result =
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!";
290   }
291   filter.clear_min_version();
292
293   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) {
294     filter.set_max_version(max_test_cases[i].max_version);
295     const bool result =
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!";
300   }
301
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);
307
308       if (!min_test_cases[i].expected_result) {
309         const bool result =
310             seed_processor.CheckStudyVersion(
311                 filter, Version(min_test_cases[i].version));
312         EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
313       }
314
315       if (!max_test_cases[j].expected_result) {
316         const bool result =
317             seed_processor.CheckStudyVersion(
318                 filter, Version(max_test_cases[j].version));
319         EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
320       }
321     }
322   }
323 }
324
325 // Test that the group for kForcingFlag1 is forced.
326 TEST(VariationsSeedProcessorTest, ForceGroupWithFlag1) {
327   CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
328
329   base::FieldTrialList field_trial_list(NULL);
330
331   Study study = CreateStudyWithFlagGroups(100, 0, 0);
332   VariationsSeedProcessor().CreateTrialFromStudy(study, false);
333
334   EXPECT_EQ(kFlagGroup1Name,
335             base::FieldTrialList::FindFullName(kFlagStudyName));
336 }
337
338 // Test that the group for kForcingFlag2 is forced.
339 TEST(VariationsSeedProcessorTest, ForceGroupWithFlag2) {
340   CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2);
341
342   base::FieldTrialList field_trial_list(NULL);
343
344   Study study = CreateStudyWithFlagGroups(100, 0, 0);
345   VariationsSeedProcessor().CreateTrialFromStudy(study, false);
346
347   EXPECT_EQ(kFlagGroup2Name,
348             base::FieldTrialList::FindFullName(kFlagStudyName));
349 }
350
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);
355
356   base::FieldTrialList field_trial_list(NULL);
357
358   Study study = CreateStudyWithFlagGroups(100, 0, 0);
359   VariationsSeedProcessor().CreateTrialFromStudy(study, false);
360
361   EXPECT_EQ(kFlagGroup1Name,
362             base::FieldTrialList::FindFullName(kFlagStudyName));
363 }
364
365 TEST(VariationsSeedProcessorTest, ForceGroup_DontChooseGroupWithFlag) {
366   base::FieldTrialList field_trial_list(NULL);
367
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));
375 }
376
377 TEST(VariationsSeedProcessorTest, IsStudyExpired) {
378   VariationsSeedProcessor seed_processor;
379
380   const base::Time now = base::Time::Now();
381   const base::TimeDelta delta = base::TimeDelta::FromHours(1);
382   const struct {
383     const base::Time expiry_date;
384     bool expected_result;
385   } expiry_test_cases[] = {
386     { now - delta, true },
387     { now, true },
388     { now + delta, false },
389   };
390
391   Study study;
392
393   // Expiry date not set should result in false.
394   EXPECT_FALSE(seed_processor.IsStudyExpired(study, now));
395
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!";
401   }
402 }
403
404 TEST(VariationsSeedProcessorTest, NonExpiredStudyPrioritizedOverExpiredStudy) {
405   VariationsSeedProcessor seed_processor;
406
407   const std::string kTrialName = "A";
408   const std::string kGroup1Name = "Group1";
409
410   VariationsSeed seed;
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();
417   *study2 = *study1;
418   ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
419
420   const base::Time year_ago =
421       base::Time::Now() - base::TimeDelta::FromDays(365);
422
423   const base::Version version("20.0.0.0");
424
425   // Check that adding [expired, non-expired] activates the non-expired one.
426   ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
427   {
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));
433   }
434
435   // Check that adding [non-expired, expired] activates the non-expired one.
436   ASSERT_EQ(std::string(), base::FieldTrialList::FindFullName(kTrialName));
437   {
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));
444   }
445 }
446
447 TEST(VariationsSeedProcessorTest, ValidateStudy) {
448   VariationsSeedProcessor seed_processor;
449
450   Study study;
451   study.set_default_experiment_name("def");
452   AddExperiment("abc", 100, &study);
453   Study_Experiment* default_group = AddExperiment("def", 200, &study);
454
455   base::FieldTrial::Probability total_probability = 0;
456   bool valid = seed_processor.ValidateStudyAndComputeTotalProbability(
457       study, &total_probability);
458   EXPECT_TRUE(valid);
459   EXPECT_EQ(300, total_probability);
460
461   // Min version checks.
462   study.mutable_filter()->set_min_version("1.2.3.*");
463   valid = seed_processor.ValidateStudyAndComputeTotalProbability(
464       study, &total_probability);
465   EXPECT_TRUE(valid);
466   study.mutable_filter()->set_min_version("1.*.3");
467   valid = seed_processor.ValidateStudyAndComputeTotalProbability(
468       study, &total_probability);
469   EXPECT_FALSE(valid);
470   study.mutable_filter()->set_min_version("1.2.3");
471   valid = seed_processor.ValidateStudyAndComputeTotalProbability(
472       study, &total_probability);
473   EXPECT_TRUE(valid);
474
475   // Max version checks.
476   study.mutable_filter()->set_max_version("2.3.4.*");
477   valid = seed_processor.ValidateStudyAndComputeTotalProbability(
478       study, &total_probability);
479   EXPECT_TRUE(valid);
480   study.mutable_filter()->set_max_version("*.3");
481   valid = seed_processor.ValidateStudyAndComputeTotalProbability(
482       study, &total_probability);
483   EXPECT_FALSE(valid);
484   study.mutable_filter()->set_max_version("2.3.4");
485   valid = seed_processor.ValidateStudyAndComputeTotalProbability(
486       study, &total_probability);
487   EXPECT_TRUE(valid);
488
489   study.clear_default_experiment_name();
490   valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
491       &total_probability);
492   EXPECT_FALSE(valid);
493
494   study.set_default_experiment_name("xyz");
495   valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
496       &total_probability);
497   EXPECT_FALSE(valid);
498
499   study.set_default_experiment_name("def");
500   default_group->clear_name();
501   valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
502       &total_probability);
503   EXPECT_FALSE(valid);
504
505   default_group->set_name("def");
506   valid = seed_processor.ValidateStudyAndComputeTotalProbability(study,
507       &total_probability);
508   ASSERT_TRUE(valid);
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,
513       &total_probability);
514   EXPECT_FALSE(valid);
515 }
516
517 TEST(VariationsSeedProcessorTest, VariationParams) {
518   base::FieldTrialList field_trial_list(NULL);
519   VariationsSeedProcessor seed_processor;
520
521   Study study;
522   study.set_name("Study1");
523   study.set_default_experiment_name("B");
524
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");
529
530   Study_Experiment* experiment2 = AddExperiment("B", 0, &study);
531
532   seed_processor.CreateTrialFromStudy(study, false);
533   EXPECT_EQ("y", GetVariationParamValue("Study1", "x"));
534
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"));
540 }
541
542 TEST(VariationsSeedProcessorTest, StartsActive) {
543   base::FieldTrialList field_trial_list(NULL);
544
545   VariationsSeed seed;
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);
551
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);
558
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);
565
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);
570
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"));
576
577   EXPECT_EQ("AA", base::FieldTrialList::FindFullName("A"));
578   EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B"));
579   EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C"));
580
581   // Now, all studies should be active.
582   EXPECT_TRUE(IsFieldTrialActive("A"));
583   EXPECT_TRUE(IsFieldTrialActive("B"));
584   EXPECT_TRUE(IsFieldTrialActive("C"));
585 }
586
587 }  // namespace chrome_variations