1 // Copyright (c) 2012 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 "base/metrics/field_trial.h"
6 
7 #include <stddef.h>
8 
9 #include "base/build_time.h"
10 #include "base/macros.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/rand_util.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace base {
19 
20 namespace {
21 
22 // Default group name used by several tests.
23 const char kDefaultGroupName[] = "DefaultGroup";
24 
25 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date.
CreateFieldTrial(const std::string & trial_name,int total_probability,const std::string & default_group_name,int * default_group_number)26 scoped_refptr<base::FieldTrial> CreateFieldTrial(
27     const std::string& trial_name,
28     int total_probability,
29     const std::string& default_group_name,
30     int* default_group_number) {
31   return FieldTrialList::FactoryGetFieldTrial(
32       trial_name, total_probability, default_group_name,
33       base::FieldTrialList::kNoExpirationYear, 1, 1,
34       base::FieldTrial::SESSION_RANDOMIZED, default_group_number);
35 }
36 
OneYearBeforeBuildTime()37 int OneYearBeforeBuildTime() {
38   Time one_year_before_build_time = GetBuildTime() - TimeDelta::FromDays(365);
39   Time::Exploded exploded;
40   one_year_before_build_time.LocalExplode(&exploded);
41   return exploded.year;
42 }
43 
44 // FieldTrialList::Observer implementation for testing.
45 class TestFieldTrialObserver : public FieldTrialList::Observer {
46  public:
TestFieldTrialObserver()47   TestFieldTrialObserver() {
48     FieldTrialList::AddObserver(this);
49   }
50 
~TestFieldTrialObserver()51   ~TestFieldTrialObserver() override { FieldTrialList::RemoveObserver(this); }
52 
OnFieldTrialGroupFinalized(const std::string & trial,const std::string & group)53   void OnFieldTrialGroupFinalized(const std::string& trial,
54                                   const std::string& group) override {
55     trial_name_ = trial;
56     group_name_ = group;
57   }
58 
trial_name() const59   const std::string& trial_name() const { return trial_name_; }
group_name() const60   const std::string& group_name() const { return group_name_; }
61 
62  private:
63   std::string trial_name_;
64   std::string group_name_;
65 
66   DISALLOW_COPY_AND_ASSIGN(TestFieldTrialObserver);
67 };
68 
69 }  // namespace
70 
71 class FieldTrialTest : public testing::Test {
72  public:
FieldTrialTest()73   FieldTrialTest() : trial_list_(NULL) {}
74 
75  private:
76   MessageLoop message_loop_;
77   FieldTrialList trial_list_;
78 
79   DISALLOW_COPY_AND_ASSIGN(FieldTrialTest);
80 };
81 
82 // Test registration, and also check that destructors are called for trials
83 // (and that Valgrind doesn't catch us leaking).
TEST_F(FieldTrialTest,Registration)84 TEST_F(FieldTrialTest, Registration) {
85   const char name1[] = "name 1 test";
86   const char name2[] = "name 2 test";
87   EXPECT_FALSE(FieldTrialList::Find(name1));
88   EXPECT_FALSE(FieldTrialList::Find(name2));
89 
90   scoped_refptr<FieldTrial> trial1 =
91       CreateFieldTrial(name1, 10, "default name 1 test", NULL);
92   EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_);
93   EXPECT_EQ(name1, trial1->trial_name());
94   EXPECT_EQ("", trial1->group_name_internal());
95 
96   trial1->AppendGroup(std::string(), 7);
97 
98   EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1));
99   EXPECT_FALSE(FieldTrialList::Find(name2));
100 
101   scoped_refptr<FieldTrial> trial2 =
102       CreateFieldTrial(name2, 10, "default name 2 test", NULL);
103   EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_);
104   EXPECT_EQ(name2, trial2->trial_name());
105   EXPECT_EQ("", trial2->group_name_internal());
106 
107   trial2->AppendGroup("a first group", 7);
108 
109   EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1));
110   EXPECT_EQ(trial2.get(), FieldTrialList::Find(name2));
111   // Note: FieldTrialList should delete the objects at shutdown.
112 }
113 
TEST_F(FieldTrialTest,AbsoluteProbabilities)114 TEST_F(FieldTrialTest, AbsoluteProbabilities) {
115   char always_true[] = " always true";
116   char default_always_true[] = " default always true";
117   char always_false[] = " always false";
118   char default_always_false[] = " default always false";
119   for (int i = 1; i < 250; ++i) {
120     // Try lots of names, by changing the first character of the name.
121     char c = static_cast<char>(i);
122     always_true[0] = c;
123     default_always_true[0] = c;
124     always_false[0] = c;
125     default_always_false[0] = c;
126 
127     scoped_refptr<FieldTrial> trial_true =
128         CreateFieldTrial(always_true, 10, default_always_true, NULL);
129     const std::string winner = "TheWinner";
130     int winner_group = trial_true->AppendGroup(winner, 10);
131 
132     EXPECT_EQ(winner_group, trial_true->group());
133     EXPECT_EQ(winner, trial_true->group_name());
134 
135     scoped_refptr<FieldTrial> trial_false =
136         CreateFieldTrial(always_false, 10, default_always_false, NULL);
137     int loser_group = trial_false->AppendGroup("ALoser", 0);
138 
139     EXPECT_NE(loser_group, trial_false->group());
140   }
141 }
142 
TEST_F(FieldTrialTest,RemainingProbability)143 TEST_F(FieldTrialTest, RemainingProbability) {
144   // First create a test that hasn't had a winner yet.
145   const std::string winner = "Winner";
146   const std::string loser = "Loser";
147   scoped_refptr<FieldTrial> trial;
148   int counter = 0;
149   int default_group_number = -1;
150   do {
151     std::string name = StringPrintf("trial%d", ++counter);
152     trial = CreateFieldTrial(name, 10, winner, &default_group_number);
153     trial->AppendGroup(loser, 5);  // 50% chance of not being chosen.
154     // If a group is not assigned, group_ will be kNotFinalized.
155   } while (trial->group_ != FieldTrial::kNotFinalized);
156 
157   // And that 'default' group (winner) should always win.
158   EXPECT_EQ(default_group_number, trial->group());
159 
160   // And that winner should ALWAYS win.
161   EXPECT_EQ(winner, trial->group_name());
162 }
163 
TEST_F(FieldTrialTest,FiftyFiftyProbability)164 TEST_F(FieldTrialTest, FiftyFiftyProbability) {
165   // Check that even with small divisors, we have the proper probabilities, and
166   // all outcomes are possible.  Since this is a 50-50 test, it should get both
167   // outcomes in a few tries, but we'll try no more than 100 times (and be flaky
168   // with probability around 1 in 2^99).
169   bool first_winner = false;
170   bool second_winner = false;
171   int counter = 0;
172   do {
173     std::string name = base::StringPrintf("FiftyFifty%d", ++counter);
174     std::string default_group_name = base::StringPrintf("Default FiftyFifty%d",
175                                                         ++counter);
176     scoped_refptr<FieldTrial> trial =
177         CreateFieldTrial(name, 2, default_group_name, NULL);
178     trial->AppendGroup("first", 1);  // 50% chance of being chosen.
179     // If group_ is kNotFinalized, then a group assignement hasn't been done.
180     if (trial->group_ != FieldTrial::kNotFinalized) {
181       first_winner = true;
182       continue;
183     }
184     trial->AppendGroup("second", 1);  // Always chosen at this point.
185     EXPECT_NE(FieldTrial::kNotFinalized, trial->group());
186     second_winner = true;
187   } while ((!second_winner || !first_winner) && counter < 100);
188   EXPECT_TRUE(second_winner);
189   EXPECT_TRUE(first_winner);
190 }
191 
TEST_F(FieldTrialTest,MiddleProbabilities)192 TEST_F(FieldTrialTest, MiddleProbabilities) {
193   char name[] = " same name";
194   char default_group_name[] = " default same name";
195   bool false_event_seen = false;
196   bool true_event_seen = false;
197   for (int i = 1; i < 250; ++i) {
198     char c = static_cast<char>(i);
199     name[0] = c;
200     default_group_name[0] = c;
201     scoped_refptr<FieldTrial> trial =
202         CreateFieldTrial(name, 10, default_group_name, NULL);
203     int might_win = trial->AppendGroup("MightWin", 5);
204 
205     if (trial->group() == might_win) {
206       true_event_seen = true;
207     } else {
208       false_event_seen = true;
209     }
210     if (false_event_seen && true_event_seen)
211       return;  // Successful test!!!
212   }
213   // Very surprising to get here. Probability should be around 1 in 2 ** 250.
214   // One of the following will fail.
215   EXPECT_TRUE(false_event_seen);
216   EXPECT_TRUE(true_event_seen);
217 }
218 
TEST_F(FieldTrialTest,OneWinner)219 TEST_F(FieldTrialTest, OneWinner) {
220   char name[] = "Some name";
221   char default_group_name[] = "Default some name";
222   int group_count(10);
223 
224   int default_group_number = -1;
225   scoped_refptr<FieldTrial> trial =
226       CreateFieldTrial(name, group_count, default_group_name, NULL);
227   int winner_index(-2);
228   std::string winner_name;
229 
230   for (int i = 1; i <= group_count; ++i) {
231     int might_win = trial->AppendGroup(std::string(), 1);
232 
233     // Because we keep appending groups, we want to see if the last group that
234     // was added has been assigned or not.
235     if (trial->group_ == might_win) {
236       EXPECT_EQ(-2, winner_index);
237       winner_index = might_win;
238       StringAppendF(&winner_name, "%d", might_win);
239       EXPECT_EQ(winner_name, trial->group_name());
240     }
241   }
242   EXPECT_GE(winner_index, 0);
243   // Since all groups cover the total probability, we should not have
244   // chosen the default group.
245   EXPECT_NE(trial->group(), default_group_number);
246   EXPECT_EQ(trial->group(), winner_index);
247   EXPECT_EQ(trial->group_name(), winner_name);
248 }
249 
TEST_F(FieldTrialTest,DisableProbability)250 TEST_F(FieldTrialTest, DisableProbability) {
251   const std::string default_group_name = "Default group";
252   const std::string loser = "Loser";
253   const std::string name = "Trial";
254 
255   // Create a field trail that has expired.
256   int default_group_number = -1;
257   FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
258       name, 1000000000, default_group_name, OneYearBeforeBuildTime(), 1, 1,
259       FieldTrial::SESSION_RANDOMIZED,
260       &default_group_number);
261   trial->AppendGroup(loser, 999999999);  // 99.9999999% chance of being chosen.
262 
263   // Because trial has expired, we should always be in the default group.
264   EXPECT_EQ(default_group_number, trial->group());
265 
266   // And that default_group_name should ALWAYS win.
267   EXPECT_EQ(default_group_name, trial->group_name());
268 }
269 
TEST_F(FieldTrialTest,ActiveGroups)270 TEST_F(FieldTrialTest, ActiveGroups) {
271   std::string no_group("No Group");
272   scoped_refptr<FieldTrial> trial =
273       CreateFieldTrial(no_group, 10, "Default", NULL);
274 
275   // There is no winner yet, so no NameGroupId should be returned.
276   FieldTrial::ActiveGroup active_group;
277   EXPECT_FALSE(trial->GetActiveGroup(&active_group));
278 
279   // Create a single winning group.
280   std::string one_winner("One Winner");
281   trial = CreateFieldTrial(one_winner, 10, "Default", NULL);
282   std::string winner("Winner");
283   trial->AppendGroup(winner, 10);
284   EXPECT_FALSE(trial->GetActiveGroup(&active_group));
285   // Finalize the group selection by accessing the selected group.
286   trial->group();
287   EXPECT_TRUE(trial->GetActiveGroup(&active_group));
288   EXPECT_EQ(one_winner, active_group.trial_name);
289   EXPECT_EQ(winner, active_group.group_name);
290 
291   std::string multi_group("MultiGroup");
292   scoped_refptr<FieldTrial> multi_group_trial =
293       CreateFieldTrial(multi_group, 9, "Default", NULL);
294 
295   multi_group_trial->AppendGroup("Me", 3);
296   multi_group_trial->AppendGroup("You", 3);
297   multi_group_trial->AppendGroup("Them", 3);
298   EXPECT_FALSE(multi_group_trial->GetActiveGroup(&active_group));
299   // Finalize the group selection by accessing the selected group.
300   multi_group_trial->group();
301   EXPECT_TRUE(multi_group_trial->GetActiveGroup(&active_group));
302   EXPECT_EQ(multi_group, active_group.trial_name);
303   EXPECT_EQ(multi_group_trial->group_name(), active_group.group_name);
304 
305   // Now check if the list is built properly...
306   FieldTrial::ActiveGroups active_groups;
307   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
308   EXPECT_EQ(2U, active_groups.size());
309   for (size_t i = 0; i < active_groups.size(); ++i) {
310     // Order is not guaranteed, so check all values.
311     EXPECT_NE(no_group, active_groups[i].trial_name);
312     EXPECT_TRUE(one_winner != active_groups[i].trial_name ||
313                 winner == active_groups[i].group_name);
314     EXPECT_TRUE(multi_group != active_groups[i].trial_name ||
315                 multi_group_trial->group_name() == active_groups[i].group_name);
316   }
317 }
318 
TEST_F(FieldTrialTest,GetActiveFieldTrialGroupsFromString)319 TEST_F(FieldTrialTest, GetActiveFieldTrialGroupsFromString) {
320   FieldTrial::ActiveGroups active_groups;
321   FieldTrialList::GetActiveFieldTrialGroupsFromString("*A/X/B/Y/*C/Z",
322                                                       &active_groups);
323   ASSERT_EQ(2U, active_groups.size());
324   EXPECT_EQ("A", active_groups[0].trial_name);
325   EXPECT_EQ("X", active_groups[0].group_name);
326   EXPECT_EQ("C", active_groups[1].trial_name);
327   EXPECT_EQ("Z", active_groups[1].group_name);
328 }
329 
TEST_F(FieldTrialTest,AllGroups)330 TEST_F(FieldTrialTest, AllGroups) {
331   FieldTrial::State field_trial_state;
332   std::string one_winner("One Winner");
333   scoped_refptr<FieldTrial> trial =
334       CreateFieldTrial(one_winner, 10, "Default", NULL);
335   std::string winner("Winner");
336   trial->AppendGroup(winner, 10);
337   EXPECT_TRUE(trial->GetState(&field_trial_state));
338   EXPECT_EQ(one_winner, field_trial_state.trial_name);
339   EXPECT_EQ(winner, field_trial_state.group_name);
340   trial->group();
341   EXPECT_TRUE(trial->GetState(&field_trial_state));
342   EXPECT_EQ(one_winner, field_trial_state.trial_name);
343   EXPECT_EQ(winner, field_trial_state.group_name);
344 
345   std::string multi_group("MultiGroup");
346   scoped_refptr<FieldTrial> multi_group_trial =
347       CreateFieldTrial(multi_group, 9, "Default", NULL);
348 
349   multi_group_trial->AppendGroup("Me", 3);
350   multi_group_trial->AppendGroup("You", 3);
351   multi_group_trial->AppendGroup("Them", 3);
352   EXPECT_TRUE(multi_group_trial->GetState(&field_trial_state));
353   // Finalize the group selection by accessing the selected group.
354   multi_group_trial->group();
355   EXPECT_TRUE(multi_group_trial->GetState(&field_trial_state));
356   EXPECT_EQ(multi_group, field_trial_state.trial_name);
357   EXPECT_EQ(multi_group_trial->group_name(), field_trial_state.group_name);
358 }
359 
TEST_F(FieldTrialTest,ActiveGroupsNotFinalized)360 TEST_F(FieldTrialTest, ActiveGroupsNotFinalized) {
361   const char kTrialName[] = "TestTrial";
362   const char kSecondaryGroupName[] = "SecondaryGroup";
363 
364   int default_group = -1;
365   scoped_refptr<FieldTrial> trial =
366       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
367   const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50);
368 
369   // Before |group()| is called, |GetActiveGroup()| should return false.
370   FieldTrial::ActiveGroup active_group;
371   EXPECT_FALSE(trial->GetActiveGroup(&active_group));
372 
373   // |GetActiveFieldTrialGroups()| should also not include the trial.
374   FieldTrial::ActiveGroups active_groups;
375   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
376   EXPECT_TRUE(active_groups.empty());
377 
378   // After |group()| has been called, both APIs should succeed.
379   const int chosen_group = trial->group();
380   EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group);
381 
382   EXPECT_TRUE(trial->GetActiveGroup(&active_group));
383   EXPECT_EQ(kTrialName, active_group.trial_name);
384   if (chosen_group == default_group)
385     EXPECT_EQ(kDefaultGroupName, active_group.group_name);
386   else
387     EXPECT_EQ(kSecondaryGroupName, active_group.group_name);
388 
389   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
390   ASSERT_EQ(1U, active_groups.size());
391   EXPECT_EQ(kTrialName, active_groups[0].trial_name);
392   EXPECT_EQ(active_group.group_name, active_groups[0].group_name);
393 }
394 
TEST_F(FieldTrialTest,GetGroupNameWithoutActivation)395 TEST_F(FieldTrialTest, GetGroupNameWithoutActivation) {
396   const char kTrialName[] = "TestTrial";
397   const char kSecondaryGroupName[] = "SecondaryGroup";
398 
399   int default_group = -1;
400   scoped_refptr<FieldTrial> trial =
401       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
402   trial->AppendGroup(kSecondaryGroupName, 50);
403 
404   // The trial should start inactive.
405   EXPECT_FALSE(FieldTrialList::IsTrialActive(kTrialName));
406 
407   // Calling |GetGroupNameWithoutActivation()| should not activate the trial.
408   std::string group_name = trial->GetGroupNameWithoutActivation();
409   EXPECT_FALSE(group_name.empty());
410   EXPECT_FALSE(FieldTrialList::IsTrialActive(kTrialName));
411 
412   // Calling |group_name()| should activate it and return the same group name.
413   EXPECT_EQ(group_name, trial->group_name());
414   EXPECT_TRUE(FieldTrialList::IsTrialActive(kTrialName));
415 }
416 
TEST_F(FieldTrialTest,Save)417 TEST_F(FieldTrialTest, Save) {
418   std::string save_string;
419 
420   scoped_refptr<FieldTrial> trial =
421       CreateFieldTrial("Some name", 10, "Default some name", NULL);
422   // There is no winner yet, so no textual group name is associated with trial.
423   // In this case, the trial should not be included.
424   EXPECT_EQ("", trial->group_name_internal());
425   FieldTrialList::StatesToString(&save_string);
426   EXPECT_EQ("", save_string);
427   save_string.clear();
428 
429   // Create a winning group.
430   trial->AppendGroup("Winner", 10);
431   // Finalize the group selection by accessing the selected group.
432   trial->group();
433   FieldTrialList::StatesToString(&save_string);
434   EXPECT_EQ("Some name/Winner/", save_string);
435   save_string.clear();
436 
437   // Create a second trial and winning group.
438   scoped_refptr<FieldTrial> trial2 =
439       CreateFieldTrial("xxx", 10, "Default xxx", NULL);
440   trial2->AppendGroup("yyyy", 10);
441   // Finalize the group selection by accessing the selected group.
442   trial2->group();
443 
444   FieldTrialList::StatesToString(&save_string);
445   // We assume names are alphabetized... though this is not critical.
446   EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string);
447   save_string.clear();
448 
449   // Create a third trial with only the default group.
450   scoped_refptr<FieldTrial> trial3 =
451       CreateFieldTrial("zzz", 10, "default", NULL);
452   // Finalize the group selection by accessing the selected group.
453   trial3->group();
454 
455   FieldTrialList::StatesToString(&save_string);
456   EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string);
457 }
458 
TEST_F(FieldTrialTest,SaveAll)459 TEST_F(FieldTrialTest, SaveAll) {
460   std::string save_string;
461 
462   scoped_refptr<FieldTrial> trial =
463       CreateFieldTrial("Some name", 10, "Default some name", nullptr);
464   EXPECT_EQ("", trial->group_name_internal());
465   FieldTrialList::AllStatesToString(&save_string);
466   EXPECT_EQ("Some name/Default some name/", save_string);
467   // Getting all states should have finalized the trial.
468   EXPECT_EQ("Default some name", trial->group_name_internal());
469   save_string.clear();
470 
471   // Create a winning group.
472   trial = CreateFieldTrial("trial2", 10, "Default some name", nullptr);
473   trial->AppendGroup("Winner", 10);
474   // Finalize the group selection by accessing the selected group.
475   trial->group();
476   FieldTrialList::AllStatesToString(&save_string);
477   EXPECT_EQ("Some name/Default some name/*trial2/Winner/", save_string);
478   save_string.clear();
479 
480   // Create a second trial and winning group.
481   scoped_refptr<FieldTrial> trial2 =
482       CreateFieldTrial("xxx", 10, "Default xxx", nullptr);
483   trial2->AppendGroup("yyyy", 10);
484   // Finalize the group selection by accessing the selected group.
485   trial2->group();
486 
487   FieldTrialList::AllStatesToString(&save_string);
488   // We assume names are alphabetized... though this is not critical.
489   EXPECT_EQ("Some name/Default some name/*trial2/Winner/*xxx/yyyy/",
490             save_string);
491   save_string.clear();
492 
493   // Create a third trial with only the default group.
494   scoped_refptr<FieldTrial> trial3 =
495       CreateFieldTrial("zzz", 10, "default", NULL);
496 
497   FieldTrialList::AllStatesToString(&save_string);
498   EXPECT_EQ("Some name/Default some name/*trial2/Winner/*xxx/yyyy/zzz/default/",
499             save_string);
500 }
501 
TEST_F(FieldTrialTest,Restore)502 TEST_F(FieldTrialTest, Restore) {
503   ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
504   ASSERT_FALSE(FieldTrialList::TrialExists("xxx"));
505 
506   FieldTrialList::CreateTrialsFromString("Some_name/Winner/xxx/yyyy/",
507                                          std::set<std::string>());
508 
509   FieldTrial* trial = FieldTrialList::Find("Some_name");
510   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
511   EXPECT_EQ("Winner", trial->group_name());
512   EXPECT_EQ("Some_name", trial->trial_name());
513 
514   trial = FieldTrialList::Find("xxx");
515   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
516   EXPECT_EQ("yyyy", trial->group_name());
517   EXPECT_EQ("xxx", trial->trial_name());
518 }
519 
TEST_F(FieldTrialTest,RestoreNotEndingWithSlash)520 TEST_F(FieldTrialTest, RestoreNotEndingWithSlash) {
521   EXPECT_TRUE(FieldTrialList::CreateTrialsFromString("tname/gname",
522                                                      std::set<std::string>()));
523 
524   FieldTrial* trial = FieldTrialList::Find("tname");
525   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
526   EXPECT_EQ("gname", trial->group_name());
527   EXPECT_EQ("tname", trial->trial_name());
528 }
529 
TEST_F(FieldTrialTest,BogusRestore)530 TEST_F(FieldTrialTest, BogusRestore) {
531   EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("MissingSlash",
532                                                       std::set<std::string>()));
533   EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("MissingGroupName/",
534                                                       std::set<std::string>()));
535   EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("noname, only group/",
536                                                       std::set<std::string>()));
537   EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("/emptyname",
538                                                       std::set<std::string>()));
539   EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("*/emptyname",
540                                                       std::set<std::string>()));
541 }
542 
TEST_F(FieldTrialTest,DuplicateRestore)543 TEST_F(FieldTrialTest, DuplicateRestore) {
544   scoped_refptr<FieldTrial> trial =
545       CreateFieldTrial("Some name", 10, "Default", NULL);
546   trial->AppendGroup("Winner", 10);
547   // Finalize the group selection by accessing the selected group.
548   trial->group();
549   std::string save_string;
550   FieldTrialList::StatesToString(&save_string);
551   EXPECT_EQ("Some name/Winner/", save_string);
552 
553   // It is OK if we redundantly specify a winner.
554   EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(save_string,
555                                                      std::set<std::string>()));
556 
557   // But it is an error to try to change to a different winner.
558   EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("Some name/Loser/",
559                                                       std::set<std::string>()));
560 }
561 
TEST_F(FieldTrialTest,CreateTrialsFromStringNotActive)562 TEST_F(FieldTrialTest, CreateTrialsFromStringNotActive) {
563   ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
564   ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
565   ASSERT_TRUE(FieldTrialList::CreateTrialsFromString("Abc/def/Xyz/zyx/",
566                                                      std::set<std::string>()));
567 
568   FieldTrial::ActiveGroups active_groups;
569   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
570   ASSERT_TRUE(active_groups.empty());
571 
572   // Check that the values still get returned and querying them activates them.
573   EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
574   EXPECT_EQ("zyx", FieldTrialList::FindFullName("Xyz"));
575 
576   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
577   ASSERT_EQ(2U, active_groups.size());
578   EXPECT_EQ("Abc", active_groups[0].trial_name);
579   EXPECT_EQ("def", active_groups[0].group_name);
580   EXPECT_EQ("Xyz", active_groups[1].trial_name);
581   EXPECT_EQ("zyx", active_groups[1].group_name);
582 }
583 
TEST_F(FieldTrialTest,CreateTrialsFromStringForceActivation)584 TEST_F(FieldTrialTest, CreateTrialsFromStringForceActivation) {
585   ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
586   ASSERT_FALSE(FieldTrialList::TrialExists("def"));
587   ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
588   ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
589       "*Abc/cba/def/fed/*Xyz/zyx/", std::set<std::string>()));
590 
591   FieldTrial::ActiveGroups active_groups;
592   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
593   ASSERT_EQ(2U, active_groups.size());
594   EXPECT_EQ("Abc", active_groups[0].trial_name);
595   EXPECT_EQ("cba", active_groups[0].group_name);
596   EXPECT_EQ("Xyz", active_groups[1].trial_name);
597   EXPECT_EQ("zyx", active_groups[1].group_name);
598 }
599 
TEST_F(FieldTrialTest,CreateTrialsFromStringNotActiveObserver)600 TEST_F(FieldTrialTest, CreateTrialsFromStringNotActiveObserver) {
601   ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
602 
603   TestFieldTrialObserver observer;
604   ASSERT_TRUE(FieldTrialList::CreateTrialsFromString("Abc/def/",
605                                                      std::set<std::string>()));
606   RunLoop().RunUntilIdle();
607   // Observer shouldn't be notified.
608   EXPECT_TRUE(observer.trial_name().empty());
609 
610   // Check that the values still get returned and querying them activates them.
611   EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
612 
613   RunLoop().RunUntilIdle();
614   EXPECT_EQ("Abc", observer.trial_name());
615   EXPECT_EQ("def", observer.group_name());
616 }
617 
TEST_F(FieldTrialTest,CreateTrialsFromStringWithIgnoredFieldTrials)618 TEST_F(FieldTrialTest, CreateTrialsFromStringWithIgnoredFieldTrials) {
619   ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
620   ASSERT_FALSE(FieldTrialList::TrialExists("Foo"));
621   ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
622   ASSERT_FALSE(FieldTrialList::TrialExists("Bar"));
623   ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
624 
625   std::set<std::string> ignored_trial_names;
626   ignored_trial_names.insert("Unaccepted1");
627   ignored_trial_names.insert("Unaccepted2");
628   ignored_trial_names.insert("Unaccepted3");
629 
630   FieldTrialList::CreateTrialsFromString(
631       "Unaccepted1/Unaccepted1_name/"
632       "Foo/Foo_name/"
633       "Unaccepted2/Unaccepted2_name/"
634       "Bar/Bar_name/"
635       "Unaccepted3/Unaccepted3_name/",
636       ignored_trial_names);
637 
638   EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
639   EXPECT_TRUE(FieldTrialList::TrialExists("Foo"));
640   EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
641   EXPECT_TRUE(FieldTrialList::TrialExists("Bar"));
642   EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
643 
644   FieldTrial::ActiveGroups active_groups;
645   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
646   EXPECT_TRUE(active_groups.empty());
647 
648   FieldTrial* trial = FieldTrialList::Find("Foo");
649   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
650   EXPECT_EQ("Foo", trial->trial_name());
651   EXPECT_EQ("Foo_name", trial->group_name());
652 
653   trial = FieldTrialList::Find("Bar");
654   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
655   EXPECT_EQ("Bar", trial->trial_name());
656   EXPECT_EQ("Bar_name", trial->group_name());
657 }
658 
TEST_F(FieldTrialTest,CreateFieldTrial)659 TEST_F(FieldTrialTest, CreateFieldTrial) {
660   ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
661 
662   FieldTrialList::CreateFieldTrial("Some_name", "Winner");
663 
664   FieldTrial* trial = FieldTrialList::Find("Some_name");
665   ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
666   EXPECT_EQ("Winner", trial->group_name());
667   EXPECT_EQ("Some_name", trial->trial_name());
668 }
669 
TEST_F(FieldTrialTest,CreateFieldTrialIsNotActive)670 TEST_F(FieldTrialTest, CreateFieldTrialIsNotActive) {
671   const char kTrialName[] = "CreateFieldTrialIsActiveTrial";
672   const char kWinnerGroup[] = "Winner";
673   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
674   FieldTrialList::CreateFieldTrial(kTrialName, kWinnerGroup);
675 
676   FieldTrial::ActiveGroups active_groups;
677   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
678   EXPECT_TRUE(active_groups.empty());
679 }
680 
TEST_F(FieldTrialTest,DuplicateFieldTrial)681 TEST_F(FieldTrialTest, DuplicateFieldTrial) {
682   scoped_refptr<FieldTrial> trial =
683       CreateFieldTrial("Some_name", 10, "Default", NULL);
684   trial->AppendGroup("Winner", 10);
685 
686   // It is OK if we redundantly specify a winner.
687   FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner");
688   EXPECT_TRUE(trial1 != NULL);
689 
690   // But it is an error to try to change to a different winner.
691   FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser");
692   EXPECT_TRUE(trial2 == NULL);
693 }
694 
TEST_F(FieldTrialTest,DisableImmediately)695 TEST_F(FieldTrialTest, DisableImmediately) {
696   int default_group_number = -1;
697   scoped_refptr<FieldTrial> trial =
698       CreateFieldTrial("trial", 100, "default", &default_group_number);
699   trial->Disable();
700   ASSERT_EQ("default", trial->group_name());
701   ASSERT_EQ(default_group_number, trial->group());
702 }
703 
TEST_F(FieldTrialTest,DisableAfterInitialization)704 TEST_F(FieldTrialTest, DisableAfterInitialization) {
705   scoped_refptr<FieldTrial> trial =
706       CreateFieldTrial("trial", 100, "default", NULL);
707   trial->AppendGroup("non_default", 100);
708   trial->Disable();
709   ASSERT_EQ("default", trial->group_name());
710 }
711 
TEST_F(FieldTrialTest,ForcedFieldTrials)712 TEST_F(FieldTrialTest, ForcedFieldTrials) {
713   // Validate we keep the forced choice.
714   FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Use the",
715                                                               "Force");
716   EXPECT_STREQ("Force", forced_trial->group_name().c_str());
717 
718   int default_group_number = -1;
719   scoped_refptr<FieldTrial> factory_trial =
720       CreateFieldTrial("Use the", 1000, "default", &default_group_number);
721   EXPECT_EQ(factory_trial.get(), forced_trial);
722 
723   int chosen_group = factory_trial->AppendGroup("Force", 100);
724   EXPECT_EQ(chosen_group, factory_trial->group());
725   int not_chosen_group = factory_trial->AppendGroup("Dark Side", 100);
726   EXPECT_NE(chosen_group, not_chosen_group);
727 
728   // Since we didn't force the default group, we should not be returned the
729   // chosen group as the default group.
730   EXPECT_NE(default_group_number, chosen_group);
731   int new_group = factory_trial->AppendGroup("Duck Tape", 800);
732   EXPECT_NE(chosen_group, new_group);
733   // The new group should not be the default group either.
734   EXPECT_NE(default_group_number, new_group);
735 }
736 
TEST_F(FieldTrialTest,ForcedFieldTrialsDefaultGroup)737 TEST_F(FieldTrialTest, ForcedFieldTrialsDefaultGroup) {
738   // Forcing the default should use the proper group ID.
739   FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Trial Name",
740                                                               "Default");
741   int default_group_number = -1;
742   scoped_refptr<FieldTrial> factory_trial =
743       CreateFieldTrial("Trial Name", 1000, "Default", &default_group_number);
744   EXPECT_EQ(forced_trial, factory_trial.get());
745 
746   int other_group = factory_trial->AppendGroup("Not Default", 100);
747   EXPECT_STREQ("Default", factory_trial->group_name().c_str());
748   EXPECT_EQ(default_group_number, factory_trial->group());
749   EXPECT_NE(other_group, factory_trial->group());
750 
751   int new_other_group = factory_trial->AppendGroup("Not Default Either", 800);
752   EXPECT_NE(new_other_group, factory_trial->group());
753 }
754 
TEST_F(FieldTrialTest,SetForced)755 TEST_F(FieldTrialTest, SetForced) {
756   // Start by setting a trial for which we ensure a winner...
757   int default_group_number = -1;
758   scoped_refptr<FieldTrial> forced_trial =
759       CreateFieldTrial("Use the", 1, "default", &default_group_number);
760   EXPECT_EQ(forced_trial, forced_trial);
761 
762   int forced_group = forced_trial->AppendGroup("Force", 1);
763   EXPECT_EQ(forced_group, forced_trial->group());
764 
765   // Now force it.
766   forced_trial->SetForced();
767 
768   // Now try to set it up differently as a hard coded registration would.
769   scoped_refptr<FieldTrial> hard_coded_trial =
770       CreateFieldTrial("Use the", 1, "default", &default_group_number);
771   EXPECT_EQ(hard_coded_trial, forced_trial);
772 
773   int would_lose_group = hard_coded_trial->AppendGroup("Force", 0);
774   EXPECT_EQ(forced_group, hard_coded_trial->group());
775   EXPECT_EQ(forced_group, would_lose_group);
776 
777   // Same thing if we would have done it to win again.
778   scoped_refptr<FieldTrial> other_hard_coded_trial =
779       CreateFieldTrial("Use the", 1, "default", &default_group_number);
780   EXPECT_EQ(other_hard_coded_trial, forced_trial);
781 
782   int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1);
783   EXPECT_EQ(forced_group, other_hard_coded_trial->group());
784   EXPECT_EQ(forced_group, would_win_group);
785 }
786 
TEST_F(FieldTrialTest,SetForcedDefaultOnly)787 TEST_F(FieldTrialTest, SetForcedDefaultOnly) {
788   const char kTrialName[] = "SetForcedDefaultOnly";
789   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
790 
791   int default_group = -1;
792   scoped_refptr<FieldTrial> trial =
793       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
794   trial->SetForced();
795 
796   trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
797   EXPECT_EQ(default_group, trial->group());
798   EXPECT_EQ(kDefaultGroupName, trial->group_name());
799 }
800 
TEST_F(FieldTrialTest,SetForcedDefaultWithExtraGroup)801 TEST_F(FieldTrialTest, SetForcedDefaultWithExtraGroup) {
802   const char kTrialName[] = "SetForcedDefaultWithExtraGroup";
803   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
804 
805   int default_group = -1;
806   scoped_refptr<FieldTrial> trial =
807       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
808   trial->SetForced();
809 
810   trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
811   const int extra_group = trial->AppendGroup("Extra", 100);
812   EXPECT_EQ(default_group, trial->group());
813   EXPECT_NE(extra_group, trial->group());
814   EXPECT_EQ(kDefaultGroupName, trial->group_name());
815 }
816 
TEST_F(FieldTrialTest,SetForcedTurnFeatureOn)817 TEST_F(FieldTrialTest, SetForcedTurnFeatureOn) {
818   const char kTrialName[] = "SetForcedTurnFeatureOn";
819   const char kExtraGroupName[] = "Extra";
820   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
821 
822   // Simulate a server-side (forced) config that turns the feature on when the
823   // original hard-coded config had it disabled.
824   scoped_refptr<FieldTrial> forced_trial =
825       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
826   forced_trial->AppendGroup(kExtraGroupName, 100);
827   forced_trial->SetForced();
828 
829   int default_group = -1;
830   scoped_refptr<FieldTrial> client_trial =
831       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
832   const int extra_group = client_trial->AppendGroup(kExtraGroupName, 0);
833   EXPECT_NE(default_group, extra_group);
834 
835   EXPECT_FALSE(client_trial->group_reported_);
836   EXPECT_EQ(extra_group, client_trial->group());
837   EXPECT_TRUE(client_trial->group_reported_);
838   EXPECT_EQ(kExtraGroupName, client_trial->group_name());
839 }
840 
TEST_F(FieldTrialTest,SetForcedTurnFeatureOff)841 TEST_F(FieldTrialTest, SetForcedTurnFeatureOff) {
842   const char kTrialName[] = "SetForcedTurnFeatureOff";
843   const char kExtraGroupName[] = "Extra";
844   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
845 
846   // Simulate a server-side (forced) config that turns the feature off when the
847   // original hard-coded config had it enabled.
848   scoped_refptr<FieldTrial> forced_trial =
849       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
850   forced_trial->AppendGroup(kExtraGroupName, 0);
851   forced_trial->SetForced();
852 
853   int default_group = -1;
854   scoped_refptr<FieldTrial> client_trial =
855       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
856   const int extra_group = client_trial->AppendGroup(kExtraGroupName, 100);
857   EXPECT_NE(default_group, extra_group);
858 
859   EXPECT_FALSE(client_trial->group_reported_);
860   EXPECT_EQ(default_group, client_trial->group());
861   EXPECT_TRUE(client_trial->group_reported_);
862   EXPECT_EQ(kDefaultGroupName, client_trial->group_name());
863 }
864 
TEST_F(FieldTrialTest,SetForcedChangeDefault_Default)865 TEST_F(FieldTrialTest, SetForcedChangeDefault_Default) {
866   const char kTrialName[] = "SetForcedDefaultGroupChange";
867   const char kGroupAName[] = "A";
868   const char kGroupBName[] = "B";
869   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
870 
871   // Simulate a server-side (forced) config that switches which group is default
872   // and ensures that the non-forced code receives the correct group numbers.
873   scoped_refptr<FieldTrial> forced_trial =
874       CreateFieldTrial(kTrialName, 100, kGroupAName, NULL);
875   forced_trial->AppendGroup(kGroupBName, 100);
876   forced_trial->SetForced();
877 
878   int default_group = -1;
879   scoped_refptr<FieldTrial> client_trial =
880       CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group);
881   const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
882   EXPECT_NE(default_group, extra_group);
883 
884   EXPECT_FALSE(client_trial->group_reported_);
885   EXPECT_EQ(default_group, client_trial->group());
886   EXPECT_TRUE(client_trial->group_reported_);
887   EXPECT_EQ(kGroupBName, client_trial->group_name());
888 }
889 
TEST_F(FieldTrialTest,SetForcedChangeDefault_NonDefault)890 TEST_F(FieldTrialTest, SetForcedChangeDefault_NonDefault) {
891   const char kTrialName[] = "SetForcedDefaultGroupChange";
892   const char kGroupAName[] = "A";
893   const char kGroupBName[] = "B";
894   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
895 
896   // Simulate a server-side (forced) config that switches which group is default
897   // and ensures that the non-forced code receives the correct group numbers.
898   scoped_refptr<FieldTrial> forced_trial =
899       CreateFieldTrial(kTrialName, 100, kGroupAName, NULL);
900   forced_trial->AppendGroup(kGroupBName, 0);
901   forced_trial->SetForced();
902 
903   int default_group = -1;
904   scoped_refptr<FieldTrial> client_trial =
905       CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group);
906   const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
907   EXPECT_NE(default_group, extra_group);
908 
909   EXPECT_FALSE(client_trial->group_reported_);
910   EXPECT_EQ(extra_group, client_trial->group());
911   EXPECT_TRUE(client_trial->group_reported_);
912   EXPECT_EQ(kGroupAName, client_trial->group_name());
913 }
914 
TEST_F(FieldTrialTest,Observe)915 TEST_F(FieldTrialTest, Observe) {
916   const char kTrialName[] = "TrialToObserve1";
917   const char kSecondaryGroupName[] = "SecondaryGroup";
918 
919   TestFieldTrialObserver observer;
920   int default_group = -1;
921   scoped_refptr<FieldTrial> trial =
922       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
923   const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50);
924   const int chosen_group = trial->group();
925   EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group);
926 
927   RunLoop().RunUntilIdle();
928   EXPECT_EQ(kTrialName, observer.trial_name());
929   if (chosen_group == default_group)
930     EXPECT_EQ(kDefaultGroupName, observer.group_name());
931   else
932     EXPECT_EQ(kSecondaryGroupName, observer.group_name());
933 }
934 
TEST_F(FieldTrialTest,ObserveDisabled)935 TEST_F(FieldTrialTest, ObserveDisabled) {
936   const char kTrialName[] = "TrialToObserve2";
937 
938   TestFieldTrialObserver observer;
939   int default_group = -1;
940   scoped_refptr<FieldTrial> trial =
941       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
942   trial->AppendGroup("A", 25);
943   trial->AppendGroup("B", 25);
944   trial->AppendGroup("C", 25);
945   trial->Disable();
946 
947   // Observer shouldn't be notified of a disabled trial.
948   RunLoop().RunUntilIdle();
949   EXPECT_TRUE(observer.trial_name().empty());
950   EXPECT_TRUE(observer.group_name().empty());
951 
952   // Observer shouldn't be notified even after a |group()| call.
953   EXPECT_EQ(default_group, trial->group());
954   RunLoop().RunUntilIdle();
955   EXPECT_TRUE(observer.trial_name().empty());
956   EXPECT_TRUE(observer.group_name().empty());
957 }
958 
TEST_F(FieldTrialTest,ObserveForcedDisabled)959 TEST_F(FieldTrialTest, ObserveForcedDisabled) {
960   const char kTrialName[] = "TrialToObserve3";
961 
962   TestFieldTrialObserver observer;
963   int default_group = -1;
964   scoped_refptr<FieldTrial> trial =
965       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
966   trial->AppendGroup("A", 25);
967   trial->AppendGroup("B", 25);
968   trial->AppendGroup("C", 25);
969   trial->SetForced();
970   trial->Disable();
971 
972   // Observer shouldn't be notified of a disabled trial, even when forced.
973   RunLoop().RunUntilIdle();
974   EXPECT_TRUE(observer.trial_name().empty());
975   EXPECT_TRUE(observer.group_name().empty());
976 
977   // Observer shouldn't be notified even after a |group()| call.
978   EXPECT_EQ(default_group, trial->group());
979   RunLoop().RunUntilIdle();
980   EXPECT_TRUE(observer.trial_name().empty());
981   EXPECT_TRUE(observer.group_name().empty());
982 }
983 
TEST_F(FieldTrialTest,DisabledTrialNotActive)984 TEST_F(FieldTrialTest, DisabledTrialNotActive) {
985   const char kTrialName[] = "DisabledTrial";
986   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
987 
988   scoped_refptr<FieldTrial> trial =
989       CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
990   trial->AppendGroup("X", 50);
991   trial->Disable();
992 
993   // Ensure the trial is not listed as active.
994   FieldTrial::ActiveGroups active_groups;
995   FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
996   EXPECT_TRUE(active_groups.empty());
997 
998   // Ensure the trial is not listed in the |StatesToString()| result.
999   std::string states;
1000   FieldTrialList::StatesToString(&states);
1001   EXPECT_TRUE(states.empty());
1002 }
1003 
TEST_F(FieldTrialTest,ExpirationYearNotExpired)1004 TEST_F(FieldTrialTest, ExpirationYearNotExpired) {
1005   const char kTrialName[] = "NotExpired";
1006   const char kGroupName[] = "Group2";
1007   const int kProbability = 100;
1008   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
1009 
1010   scoped_refptr<FieldTrial> trial =
1011       CreateFieldTrial(kTrialName, kProbability, kDefaultGroupName, NULL);
1012   trial->AppendGroup(kGroupName, kProbability);
1013   EXPECT_EQ(kGroupName, trial->group_name());
1014 }
1015 
TEST_F(FieldTrialTest,FloatBoundariesGiveEqualGroupSizes)1016 TEST_F(FieldTrialTest, FloatBoundariesGiveEqualGroupSizes) {
1017   const int kBucketCount = 100;
1018 
1019   // Try each boundary value |i / 100.0| as the entropy value.
1020   for (int i = 0; i < kBucketCount; ++i) {
1021     const double entropy = i / static_cast<double>(kBucketCount);
1022 
1023     scoped_refptr<base::FieldTrial> trial(
1024         new base::FieldTrial("test", kBucketCount, "default", entropy));
1025     for (int j = 0; j < kBucketCount; ++j)
1026       trial->AppendGroup(base::IntToString(j), 1);
1027 
1028     EXPECT_EQ(base::IntToString(i), trial->group_name());
1029   }
1030 }
1031 
TEST_F(FieldTrialTest,DoesNotSurpassTotalProbability)1032 TEST_F(FieldTrialTest, DoesNotSurpassTotalProbability) {
1033   const double kEntropyValue = 1.0 - 1e-9;
1034   ASSERT_LT(kEntropyValue, 1.0);
1035 
1036   scoped_refptr<base::FieldTrial> trial(
1037       new base::FieldTrial("test", 2, "default", kEntropyValue));
1038   trial->AppendGroup("1", 1);
1039   trial->AppendGroup("2", 1);
1040 
1041   EXPECT_EQ("2", trial->group_name());
1042 }
1043 
TEST_F(FieldTrialTest,CreateSimulatedFieldTrial)1044 TEST_F(FieldTrialTest, CreateSimulatedFieldTrial) {
1045   const char kTrialName[] = "CreateSimulatedFieldTrial";
1046   ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
1047 
1048   // Different cases to test, e.g. default vs. non default group being chosen.
1049   struct {
1050     double entropy_value;
1051     const char* expected_group;
1052   } test_cases[] = {
1053     { 0.4, "A" },
1054     { 0.85, "B" },
1055     { 0.95, kDefaultGroupName },
1056   };
1057 
1058   for (size_t i = 0; i < arraysize(test_cases); ++i) {
1059     TestFieldTrialObserver observer;
1060     scoped_refptr<FieldTrial> trial(
1061        FieldTrial::CreateSimulatedFieldTrial(kTrialName, 100, kDefaultGroupName,
1062                                              test_cases[i].entropy_value));
1063     trial->AppendGroup("A", 80);
1064     trial->AppendGroup("B", 10);
1065     EXPECT_EQ(test_cases[i].expected_group, trial->group_name());
1066 
1067     // Field trial shouldn't have been registered with the list.
1068     EXPECT_FALSE(FieldTrialList::TrialExists(kTrialName));
1069     EXPECT_EQ(0u, FieldTrialList::GetFieldTrialCount());
1070 
1071     // Observer shouldn't have been notified.
1072     RunLoop().RunUntilIdle();
1073     EXPECT_TRUE(observer.trial_name().empty());
1074 
1075     // The trial shouldn't be in the active set of trials.
1076     FieldTrial::ActiveGroups active_groups;
1077     FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
1078     EXPECT_TRUE(active_groups.empty());
1079 
1080     // The trial shouldn't be listed in the |StatesToString()| result.
1081     std::string states;
1082     FieldTrialList::StatesToString(&states);
1083     EXPECT_TRUE(states.empty());
1084   }
1085 }
1086 
TEST(FieldTrialTestWithoutList,StatesStringFormat)1087 TEST(FieldTrialTestWithoutList, StatesStringFormat) {
1088   std::string save_string;
1089 
1090   // Scoping the first FieldTrialList, as we need another one to test the
1091   // importing function.
1092   {
1093     FieldTrialList field_trial_list(NULL);
1094     scoped_refptr<FieldTrial> trial =
1095         CreateFieldTrial("Abc", 10, "Default some name", NULL);
1096     trial->AppendGroup("cba", 10);
1097     trial->group();
1098     scoped_refptr<FieldTrial> trial2 =
1099         CreateFieldTrial("Xyz", 10, "Default xxx", NULL);
1100     trial2->AppendGroup("zyx", 10);
1101     trial2->group();
1102     scoped_refptr<FieldTrial> trial3 =
1103         CreateFieldTrial("zzz", 10, "default", NULL);
1104 
1105     FieldTrialList::AllStatesToString(&save_string);
1106   }
1107 
1108   // Starting with a new blank FieldTrialList.
1109   FieldTrialList field_trial_list(NULL);
1110   ASSERT_TRUE(field_trial_list.CreateTrialsFromString(save_string,
1111                                                       std::set<std::string>()));
1112 
1113   FieldTrial::ActiveGroups active_groups;
1114   field_trial_list.GetActiveFieldTrialGroups(&active_groups);
1115   ASSERT_EQ(2U, active_groups.size());
1116   EXPECT_EQ("Abc", active_groups[0].trial_name);
1117   EXPECT_EQ("cba", active_groups[0].group_name);
1118   EXPECT_EQ("Xyz", active_groups[1].trial_name);
1119   EXPECT_EQ("zyx", active_groups[1].group_name);
1120   EXPECT_TRUE(field_trial_list.TrialExists("zzz"));
1121 }
1122 
1123 #if GTEST_HAS_DEATH_TEST
TEST(FieldTrialDeathTest,OneTimeRandomizedTrialWithoutFieldTrialList)1124 TEST(FieldTrialDeathTest, OneTimeRandomizedTrialWithoutFieldTrialList) {
1125   // Trying to instantiate a one-time randomized field trial before the
1126   // FieldTrialList is created should crash.
1127   EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial(
1128       "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName,
1129       base::FieldTrialList::kNoExpirationYear, 1, 1,
1130       base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), "");
1131 }
1132 #endif
1133 
1134 }  // namespace base
1135