gn vim: apply to *.gni also
[chromium-blink-merge.git] / base / metrics / field_trial_unittest.cc
blobce95c2ae15b12e510a0abc47b0a6157dc94f1c60
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.
5 #include "base/metrics/field_trial.h"
7 #include "base/build_time.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/rand_util.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace base {
17 namespace {
19 // Default group name used by several tests.
20 const char kDefaultGroupName[] = "DefaultGroup";
22 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date.
23 scoped_refptr<base::FieldTrial> CreateFieldTrial(
24 const std::string& trial_name,
25 int total_probability,
26 const std::string& default_group_name,
27 int* default_group_number) {
28 return FieldTrialList::FactoryGetFieldTrial(
29 trial_name, total_probability, default_group_name,
30 base::FieldTrialList::kNoExpirationYear, 1, 1,
31 base::FieldTrial::SESSION_RANDOMIZED, default_group_number);
34 int OneYearBeforeBuildTime() {
35 Time one_year_before_build_time = GetBuildTime() - TimeDelta::FromDays(365);
36 Time::Exploded exploded;
37 one_year_before_build_time.LocalExplode(&exploded);
38 return exploded.year;
41 // FieldTrialList::Observer implementation for testing.
42 class TestFieldTrialObserver : public FieldTrialList::Observer {
43 public:
44 TestFieldTrialObserver() {
45 FieldTrialList::AddObserver(this);
48 ~TestFieldTrialObserver() override { FieldTrialList::RemoveObserver(this); }
50 void OnFieldTrialGroupFinalized(const std::string& trial,
51 const std::string& group) override {
52 trial_name_ = trial;
53 group_name_ = group;
56 const std::string& trial_name() const { return trial_name_; }
57 const std::string& group_name() const { return group_name_; }
59 private:
60 std::string trial_name_;
61 std::string group_name_;
63 DISALLOW_COPY_AND_ASSIGN(TestFieldTrialObserver);
66 } // namespace
68 class FieldTrialTest : public testing::Test {
69 public:
70 FieldTrialTest() : trial_list_(NULL) {}
72 private:
73 MessageLoop message_loop_;
74 FieldTrialList trial_list_;
77 // Test registration, and also check that destructors are called for trials
78 // (and that Valgrind doesn't catch us leaking).
79 TEST_F(FieldTrialTest, Registration) {
80 const char name1[] = "name 1 test";
81 const char name2[] = "name 2 test";
82 EXPECT_FALSE(FieldTrialList::Find(name1));
83 EXPECT_FALSE(FieldTrialList::Find(name2));
85 scoped_refptr<FieldTrial> trial1 =
86 CreateFieldTrial(name1, 10, "default name 1 test", NULL);
87 EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_);
88 EXPECT_EQ(name1, trial1->trial_name());
89 EXPECT_EQ("", trial1->group_name_internal());
91 trial1->AppendGroup(std::string(), 7);
93 EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1));
94 EXPECT_FALSE(FieldTrialList::Find(name2));
96 scoped_refptr<FieldTrial> trial2 =
97 CreateFieldTrial(name2, 10, "default name 2 test", NULL);
98 EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_);
99 EXPECT_EQ(name2, trial2->trial_name());
100 EXPECT_EQ("", trial2->group_name_internal());
102 trial2->AppendGroup("a first group", 7);
104 EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1));
105 EXPECT_EQ(trial2.get(), FieldTrialList::Find(name2));
106 // Note: FieldTrialList should delete the objects at shutdown.
109 TEST_F(FieldTrialTest, AbsoluteProbabilities) {
110 char always_true[] = " always true";
111 char default_always_true[] = " default always true";
112 char always_false[] = " always false";
113 char default_always_false[] = " default always false";
114 for (int i = 1; i < 250; ++i) {
115 // Try lots of names, by changing the first character of the name.
116 char c = static_cast<char>(i);
117 always_true[0] = c;
118 default_always_true[0] = c;
119 always_false[0] = c;
120 default_always_false[0] = c;
122 scoped_refptr<FieldTrial> trial_true =
123 CreateFieldTrial(always_true, 10, default_always_true, NULL);
124 const std::string winner = "TheWinner";
125 int winner_group = trial_true->AppendGroup(winner, 10);
127 EXPECT_EQ(winner_group, trial_true->group());
128 EXPECT_EQ(winner, trial_true->group_name());
130 scoped_refptr<FieldTrial> trial_false =
131 CreateFieldTrial(always_false, 10, default_always_false, NULL);
132 int loser_group = trial_false->AppendGroup("ALoser", 0);
134 EXPECT_NE(loser_group, trial_false->group());
138 TEST_F(FieldTrialTest, RemainingProbability) {
139 // First create a test that hasn't had a winner yet.
140 const std::string winner = "Winner";
141 const std::string loser = "Loser";
142 scoped_refptr<FieldTrial> trial;
143 int counter = 0;
144 int default_group_number = -1;
145 do {
146 std::string name = StringPrintf("trial%d", ++counter);
147 trial = CreateFieldTrial(name, 10, winner, &default_group_number);
148 trial->AppendGroup(loser, 5); // 50% chance of not being chosen.
149 // If a group is not assigned, group_ will be kNotFinalized.
150 } while (trial->group_ != FieldTrial::kNotFinalized);
152 // And that 'default' group (winner) should always win.
153 EXPECT_EQ(default_group_number, trial->group());
155 // And that winner should ALWAYS win.
156 EXPECT_EQ(winner, trial->group_name());
159 TEST_F(FieldTrialTest, FiftyFiftyProbability) {
160 // Check that even with small divisors, we have the proper probabilities, and
161 // all outcomes are possible. Since this is a 50-50 test, it should get both
162 // outcomes in a few tries, but we'll try no more than 100 times (and be flaky
163 // with probability around 1 in 2^99).
164 bool first_winner = false;
165 bool second_winner = false;
166 int counter = 0;
167 do {
168 std::string name = base::StringPrintf("FiftyFifty%d", ++counter);
169 std::string default_group_name = base::StringPrintf("Default FiftyFifty%d",
170 ++counter);
171 scoped_refptr<FieldTrial> trial =
172 CreateFieldTrial(name, 2, default_group_name, NULL);
173 trial->AppendGroup("first", 1); // 50% chance of being chosen.
174 // If group_ is kNotFinalized, then a group assignement hasn't been done.
175 if (trial->group_ != FieldTrial::kNotFinalized) {
176 first_winner = true;
177 continue;
179 trial->AppendGroup("second", 1); // Always chosen at this point.
180 EXPECT_NE(FieldTrial::kNotFinalized, trial->group());
181 second_winner = true;
182 } while ((!second_winner || !first_winner) && counter < 100);
183 EXPECT_TRUE(second_winner);
184 EXPECT_TRUE(first_winner);
187 TEST_F(FieldTrialTest, MiddleProbabilities) {
188 char name[] = " same name";
189 char default_group_name[] = " default same name";
190 bool false_event_seen = false;
191 bool true_event_seen = false;
192 for (int i = 1; i < 250; ++i) {
193 char c = static_cast<char>(i);
194 name[0] = c;
195 default_group_name[0] = c;
196 scoped_refptr<FieldTrial> trial =
197 CreateFieldTrial(name, 10, default_group_name, NULL);
198 int might_win = trial->AppendGroup("MightWin", 5);
200 if (trial->group() == might_win) {
201 true_event_seen = true;
202 } else {
203 false_event_seen = true;
205 if (false_event_seen && true_event_seen)
206 return; // Successful test!!!
208 // Very surprising to get here. Probability should be around 1 in 2 ** 250.
209 // One of the following will fail.
210 EXPECT_TRUE(false_event_seen);
211 EXPECT_TRUE(true_event_seen);
214 TEST_F(FieldTrialTest, OneWinner) {
215 char name[] = "Some name";
216 char default_group_name[] = "Default some name";
217 int group_count(10);
219 int default_group_number = -1;
220 scoped_refptr<FieldTrial> trial =
221 CreateFieldTrial(name, group_count, default_group_name, NULL);
222 int winner_index(-2);
223 std::string winner_name;
225 for (int i = 1; i <= group_count; ++i) {
226 int might_win = trial->AppendGroup(std::string(), 1);
228 // Because we keep appending groups, we want to see if the last group that
229 // was added has been assigned or not.
230 if (trial->group_ == might_win) {
231 EXPECT_EQ(-2, winner_index);
232 winner_index = might_win;
233 StringAppendF(&winner_name, "%d", might_win);
234 EXPECT_EQ(winner_name, trial->group_name());
237 EXPECT_GE(winner_index, 0);
238 // Since all groups cover the total probability, we should not have
239 // chosen the default group.
240 EXPECT_NE(trial->group(), default_group_number);
241 EXPECT_EQ(trial->group(), winner_index);
242 EXPECT_EQ(trial->group_name(), winner_name);
245 TEST_F(FieldTrialTest, DisableProbability) {
246 const std::string default_group_name = "Default group";
247 const std::string loser = "Loser";
248 const std::string name = "Trial";
250 // Create a field trail that has expired.
251 int default_group_number = -1;
252 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
253 name, 1000000000, default_group_name, OneYearBeforeBuildTime(), 1, 1,
254 FieldTrial::SESSION_RANDOMIZED,
255 &default_group_number);
256 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen.
258 // Because trial has expired, we should always be in the default group.
259 EXPECT_EQ(default_group_number, trial->group());
261 // And that default_group_name should ALWAYS win.
262 EXPECT_EQ(default_group_name, trial->group_name());
265 TEST_F(FieldTrialTest, ActiveGroups) {
266 std::string no_group("No Group");
267 scoped_refptr<FieldTrial> trial =
268 CreateFieldTrial(no_group, 10, "Default", NULL);
270 // There is no winner yet, so no NameGroupId should be returned.
271 FieldTrial::ActiveGroup active_group;
272 EXPECT_FALSE(trial->GetActiveGroup(&active_group));
274 // Create a single winning group.
275 std::string one_winner("One Winner");
276 trial = CreateFieldTrial(one_winner, 10, "Default", NULL);
277 std::string winner("Winner");
278 trial->AppendGroup(winner, 10);
279 EXPECT_FALSE(trial->GetActiveGroup(&active_group));
280 // Finalize the group selection by accessing the selected group.
281 trial->group();
282 EXPECT_TRUE(trial->GetActiveGroup(&active_group));
283 EXPECT_EQ(one_winner, active_group.trial_name);
284 EXPECT_EQ(winner, active_group.group_name);
286 std::string multi_group("MultiGroup");
287 scoped_refptr<FieldTrial> multi_group_trial =
288 CreateFieldTrial(multi_group, 9, "Default", NULL);
290 multi_group_trial->AppendGroup("Me", 3);
291 multi_group_trial->AppendGroup("You", 3);
292 multi_group_trial->AppendGroup("Them", 3);
293 EXPECT_FALSE(multi_group_trial->GetActiveGroup(&active_group));
294 // Finalize the group selection by accessing the selected group.
295 multi_group_trial->group();
296 EXPECT_TRUE(multi_group_trial->GetActiveGroup(&active_group));
297 EXPECT_EQ(multi_group, active_group.trial_name);
298 EXPECT_EQ(multi_group_trial->group_name(), active_group.group_name);
300 // Now check if the list is built properly...
301 FieldTrial::ActiveGroups active_groups;
302 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
303 EXPECT_EQ(2U, active_groups.size());
304 for (size_t i = 0; i < active_groups.size(); ++i) {
305 // Order is not guaranteed, so check all values.
306 EXPECT_NE(no_group, active_groups[i].trial_name);
307 EXPECT_TRUE(one_winner != active_groups[i].trial_name ||
308 winner == active_groups[i].group_name);
309 EXPECT_TRUE(multi_group != active_groups[i].trial_name ||
310 multi_group_trial->group_name() == active_groups[i].group_name);
314 TEST_F(FieldTrialTest, ActiveGroupsNotFinalized) {
315 const char kTrialName[] = "TestTrial";
316 const char kSecondaryGroupName[] = "SecondaryGroup";
318 int default_group = -1;
319 scoped_refptr<FieldTrial> trial =
320 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
321 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50);
323 // Before |group()| is called, |GetActiveGroup()| should return false.
324 FieldTrial::ActiveGroup active_group;
325 EXPECT_FALSE(trial->GetActiveGroup(&active_group));
327 // |GetActiveFieldTrialGroups()| should also not include the trial.
328 FieldTrial::ActiveGroups active_groups;
329 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
330 EXPECT_TRUE(active_groups.empty());
332 // After |group()| has been called, both APIs should succeed.
333 const int chosen_group = trial->group();
334 EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group);
336 EXPECT_TRUE(trial->GetActiveGroup(&active_group));
337 EXPECT_EQ(kTrialName, active_group.trial_name);
338 if (chosen_group == default_group)
339 EXPECT_EQ(kDefaultGroupName, active_group.group_name);
340 else
341 EXPECT_EQ(kSecondaryGroupName, active_group.group_name);
343 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
344 ASSERT_EQ(1U, active_groups.size());
345 EXPECT_EQ(kTrialName, active_groups[0].trial_name);
346 EXPECT_EQ(active_group.group_name, active_groups[0].group_name);
349 TEST_F(FieldTrialTest, Save) {
350 std::string save_string;
352 scoped_refptr<FieldTrial> trial =
353 CreateFieldTrial("Some name", 10, "Default some name", NULL);
354 // There is no winner yet, so no textual group name is associated with trial.
355 // In this case, the trial should not be included.
356 EXPECT_EQ("", trial->group_name_internal());
357 FieldTrialList::StatesToString(&save_string);
358 EXPECT_EQ("", save_string);
359 save_string.clear();
361 // Create a winning group.
362 trial->AppendGroup("Winner", 10);
363 // Finalize the group selection by accessing the selected group.
364 trial->group();
365 FieldTrialList::StatesToString(&save_string);
366 EXPECT_EQ("Some name/Winner/", save_string);
367 save_string.clear();
369 // Create a second trial and winning group.
370 scoped_refptr<FieldTrial> trial2 =
371 CreateFieldTrial("xxx", 10, "Default xxx", NULL);
372 trial2->AppendGroup("yyyy", 10);
373 // Finalize the group selection by accessing the selected group.
374 trial2->group();
376 FieldTrialList::StatesToString(&save_string);
377 // We assume names are alphabetized... though this is not critical.
378 EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string);
379 save_string.clear();
381 // Create a third trial with only the default group.
382 scoped_refptr<FieldTrial> trial3 =
383 CreateFieldTrial("zzz", 10, "default", NULL);
384 // Finalize the group selection by accessing the selected group.
385 trial3->group();
387 FieldTrialList::StatesToString(&save_string);
388 EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string);
391 TEST_F(FieldTrialTest, Restore) {
392 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
393 ASSERT_FALSE(FieldTrialList::TrialExists("xxx"));
395 FieldTrialList::CreateTrialsFromString("Some_name/Winner/xxx/yyyy/",
396 FieldTrialList::DONT_ACTIVATE_TRIALS,
397 std::set<std::string>());
399 FieldTrial* trial = FieldTrialList::Find("Some_name");
400 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
401 EXPECT_EQ("Winner", trial->group_name());
402 EXPECT_EQ("Some_name", trial->trial_name());
404 trial = FieldTrialList::Find("xxx");
405 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
406 EXPECT_EQ("yyyy", trial->group_name());
407 EXPECT_EQ("xxx", trial->trial_name());
410 TEST_F(FieldTrialTest, RestoreNotEndingWithSlash) {
411 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(
412 "tname/gname", FieldTrialList::DONT_ACTIVATE_TRIALS,
413 std::set<std::string>()));
415 FieldTrial* trial = FieldTrialList::Find("tname");
416 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
417 EXPECT_EQ("gname", trial->group_name());
418 EXPECT_EQ("tname", trial->trial_name());
421 TEST_F(FieldTrialTest, BogusRestore) {
422 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
423 "MissingSlash", FieldTrialList::DONT_ACTIVATE_TRIALS,
424 std::set<std::string>()));
425 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
426 "MissingGroupName/", FieldTrialList::DONT_ACTIVATE_TRIALS,
427 std::set<std::string>()));
428 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
429 "noname, only group/", FieldTrialList::DONT_ACTIVATE_TRIALS,
430 std::set<std::string>()));
431 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
432 "/emptyname", FieldTrialList::DONT_ACTIVATE_TRIALS,
433 std::set<std::string>()));
434 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
435 "*/emptyname", FieldTrialList::DONT_ACTIVATE_TRIALS,
436 std::set<std::string>()));
439 TEST_F(FieldTrialTest, DuplicateRestore) {
440 scoped_refptr<FieldTrial> trial =
441 CreateFieldTrial("Some name", 10, "Default", NULL);
442 trial->AppendGroup("Winner", 10);
443 // Finalize the group selection by accessing the selected group.
444 trial->group();
445 std::string save_string;
446 FieldTrialList::StatesToString(&save_string);
447 EXPECT_EQ("Some name/Winner/", save_string);
449 // It is OK if we redundantly specify a winner.
450 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(
451 save_string, FieldTrialList::DONT_ACTIVATE_TRIALS,
452 std::set<std::string>()));
454 // But it is an error to try to change to a different winner.
455 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
456 "Some name/Loser/", FieldTrialList::DONT_ACTIVATE_TRIALS,
457 std::set<std::string>()));
460 TEST_F(FieldTrialTest, CreateTrialsFromStringActive) {
461 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
462 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
463 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
464 "Abc/def/Xyz/zyx/", FieldTrialList::ACTIVATE_TRIALS,
465 std::set<std::string>()));
467 FieldTrial::ActiveGroups active_groups;
468 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
469 ASSERT_EQ(2U, active_groups.size());
470 EXPECT_EQ("Abc", active_groups[0].trial_name);
471 EXPECT_EQ("def", active_groups[0].group_name);
472 EXPECT_EQ("Xyz", active_groups[1].trial_name);
473 EXPECT_EQ("zyx", active_groups[1].group_name);
476 TEST_F(FieldTrialTest, CreateTrialsFromStringNotActive) {
477 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
478 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
479 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
480 "Abc/def/Xyz/zyx/", FieldTrialList::DONT_ACTIVATE_TRIALS,
481 std::set<std::string>()));
483 FieldTrial::ActiveGroups active_groups;
484 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
485 ASSERT_TRUE(active_groups.empty());
487 // Check that the values still get returned and querying them activates them.
488 EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
489 EXPECT_EQ("zyx", FieldTrialList::FindFullName("Xyz"));
491 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
492 ASSERT_EQ(2U, active_groups.size());
493 EXPECT_EQ("Abc", active_groups[0].trial_name);
494 EXPECT_EQ("def", active_groups[0].group_name);
495 EXPECT_EQ("Xyz", active_groups[1].trial_name);
496 EXPECT_EQ("zyx", active_groups[1].group_name);
499 TEST_F(FieldTrialTest, CreateTrialsFromStringForceActivation) {
500 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
501 ASSERT_FALSE(FieldTrialList::TrialExists("def"));
502 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
503 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
504 "*Abc/cba/def/fed/*Xyz/zyx/", FieldTrialList::DONT_ACTIVATE_TRIALS,
505 std::set<std::string>()));
507 FieldTrial::ActiveGroups active_groups;
508 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
509 ASSERT_EQ(2U, active_groups.size());
510 EXPECT_EQ("Abc", active_groups[0].trial_name);
511 EXPECT_EQ("cba", active_groups[0].group_name);
512 EXPECT_EQ("Xyz", active_groups[1].trial_name);
513 EXPECT_EQ("zyx", active_groups[1].group_name);
516 TEST_F(FieldTrialTest, CreateTrialsFromStringActiveObserver) {
517 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
519 TestFieldTrialObserver observer;
520 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
521 "Abc/def/", FieldTrialList::ACTIVATE_TRIALS, std::set<std::string>()));
523 RunLoop().RunUntilIdle();
524 EXPECT_EQ("Abc", observer.trial_name());
525 EXPECT_EQ("def", observer.group_name());
528 TEST_F(FieldTrialTest, CreateTrialsFromStringNotActiveObserver) {
529 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
531 TestFieldTrialObserver observer;
532 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
533 "Abc/def/", FieldTrialList::DONT_ACTIVATE_TRIALS,
534 std::set<std::string>()));
535 RunLoop().RunUntilIdle();
536 // Observer shouldn't be notified.
537 EXPECT_TRUE(observer.trial_name().empty());
539 // Check that the values still get returned and querying them activates them.
540 EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
542 RunLoop().RunUntilIdle();
543 EXPECT_EQ("Abc", observer.trial_name());
544 EXPECT_EQ("def", observer.group_name());
547 TEST_F(FieldTrialTest, CreateTrialsFromStringWithIgnoredFieldTrials) {
548 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
549 ASSERT_FALSE(FieldTrialList::TrialExists("Foo"));
550 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
551 ASSERT_FALSE(FieldTrialList::TrialExists("Bar"));
552 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
554 std::set<std::string> ignored_trial_names;
555 ignored_trial_names.insert("Unaccepted1");
556 ignored_trial_names.insert("Unaccepted2");
557 ignored_trial_names.insert("Unaccepted3");
559 FieldTrialList::CreateTrialsFromString(
560 "Unaccepted1/Unaccepted1_name/"
561 "Foo/Foo_name/"
562 "Unaccepted2/Unaccepted2_name/"
563 "Bar/Bar_name/"
564 "Unaccepted3/Unaccepted3_name/",
565 FieldTrialList::DONT_ACTIVATE_TRIALS,
566 ignored_trial_names);
568 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
569 EXPECT_TRUE(FieldTrialList::TrialExists("Foo"));
570 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
571 EXPECT_TRUE(FieldTrialList::TrialExists("Bar"));
572 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
574 FieldTrial::ActiveGroups active_groups;
575 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
576 EXPECT_TRUE(active_groups.empty());
578 FieldTrial* trial = FieldTrialList::Find("Foo");
579 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
580 EXPECT_EQ("Foo", trial->trial_name());
581 EXPECT_EQ("Foo_name", trial->group_name());
583 trial = FieldTrialList::Find("Bar");
584 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
585 EXPECT_EQ("Bar", trial->trial_name());
586 EXPECT_EQ("Bar_name", trial->group_name());
589 TEST_F(FieldTrialTest, CreateFieldTrial) {
590 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
592 FieldTrialList::CreateFieldTrial("Some_name", "Winner");
594 FieldTrial* trial = FieldTrialList::Find("Some_name");
595 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
596 EXPECT_EQ("Winner", trial->group_name());
597 EXPECT_EQ("Some_name", trial->trial_name());
600 TEST_F(FieldTrialTest, CreateFieldTrialIsNotActive) {
601 const char kTrialName[] = "CreateFieldTrialIsActiveTrial";
602 const char kWinnerGroup[] = "Winner";
603 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
604 FieldTrialList::CreateFieldTrial(kTrialName, kWinnerGroup);
606 FieldTrial::ActiveGroups active_groups;
607 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
608 EXPECT_TRUE(active_groups.empty());
611 TEST_F(FieldTrialTest, DuplicateFieldTrial) {
612 scoped_refptr<FieldTrial> trial =
613 CreateFieldTrial("Some_name", 10, "Default", NULL);
614 trial->AppendGroup("Winner", 10);
616 // It is OK if we redundantly specify a winner.
617 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner");
618 EXPECT_TRUE(trial1 != NULL);
620 // But it is an error to try to change to a different winner.
621 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser");
622 EXPECT_TRUE(trial2 == NULL);
625 TEST_F(FieldTrialTest, DisableImmediately) {
626 int default_group_number = -1;
627 scoped_refptr<FieldTrial> trial =
628 CreateFieldTrial("trial", 100, "default", &default_group_number);
629 trial->Disable();
630 ASSERT_EQ("default", trial->group_name());
631 ASSERT_EQ(default_group_number, trial->group());
634 TEST_F(FieldTrialTest, DisableAfterInitialization) {
635 scoped_refptr<FieldTrial> trial =
636 CreateFieldTrial("trial", 100, "default", NULL);
637 trial->AppendGroup("non_default", 100);
638 trial->Disable();
639 ASSERT_EQ("default", trial->group_name());
642 TEST_F(FieldTrialTest, ForcedFieldTrials) {
643 // Validate we keep the forced choice.
644 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Use the",
645 "Force");
646 EXPECT_STREQ("Force", forced_trial->group_name().c_str());
648 int default_group_number = -1;
649 scoped_refptr<FieldTrial> factory_trial =
650 CreateFieldTrial("Use the", 1000, "default", &default_group_number);
651 EXPECT_EQ(factory_trial.get(), forced_trial);
653 int chosen_group = factory_trial->AppendGroup("Force", 100);
654 EXPECT_EQ(chosen_group, factory_trial->group());
655 int not_chosen_group = factory_trial->AppendGroup("Dark Side", 100);
656 EXPECT_NE(chosen_group, not_chosen_group);
658 // Since we didn't force the default group, we should not be returned the
659 // chosen group as the default group.
660 EXPECT_NE(default_group_number, chosen_group);
661 int new_group = factory_trial->AppendGroup("Duck Tape", 800);
662 EXPECT_NE(chosen_group, new_group);
663 // The new group should not be the default group either.
664 EXPECT_NE(default_group_number, new_group);
667 TEST_F(FieldTrialTest, ForcedFieldTrialsDefaultGroup) {
668 // Forcing the default should use the proper group ID.
669 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Trial Name",
670 "Default");
671 int default_group_number = -1;
672 scoped_refptr<FieldTrial> factory_trial =
673 CreateFieldTrial("Trial Name", 1000, "Default", &default_group_number);
674 EXPECT_EQ(forced_trial, factory_trial.get());
676 int other_group = factory_trial->AppendGroup("Not Default", 100);
677 EXPECT_STREQ("Default", factory_trial->group_name().c_str());
678 EXPECT_EQ(default_group_number, factory_trial->group());
679 EXPECT_NE(other_group, factory_trial->group());
681 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800);
682 EXPECT_NE(new_other_group, factory_trial->group());
685 TEST_F(FieldTrialTest, SetForced) {
686 // Start by setting a trial for which we ensure a winner...
687 int default_group_number = -1;
688 scoped_refptr<FieldTrial> forced_trial =
689 CreateFieldTrial("Use the", 1, "default", &default_group_number);
690 EXPECT_EQ(forced_trial, forced_trial);
692 int forced_group = forced_trial->AppendGroup("Force", 1);
693 EXPECT_EQ(forced_group, forced_trial->group());
695 // Now force it.
696 forced_trial->SetForced();
698 // Now try to set it up differently as a hard coded registration would.
699 scoped_refptr<FieldTrial> hard_coded_trial =
700 CreateFieldTrial("Use the", 1, "default", &default_group_number);
701 EXPECT_EQ(hard_coded_trial, forced_trial);
703 int would_lose_group = hard_coded_trial->AppendGroup("Force", 0);
704 EXPECT_EQ(forced_group, hard_coded_trial->group());
705 EXPECT_EQ(forced_group, would_lose_group);
707 // Same thing if we would have done it to win again.
708 scoped_refptr<FieldTrial> other_hard_coded_trial =
709 CreateFieldTrial("Use the", 1, "default", &default_group_number);
710 EXPECT_EQ(other_hard_coded_trial, forced_trial);
712 int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1);
713 EXPECT_EQ(forced_group, other_hard_coded_trial->group());
714 EXPECT_EQ(forced_group, would_win_group);
717 TEST_F(FieldTrialTest, SetForcedDefaultOnly) {
718 const char kTrialName[] = "SetForcedDefaultOnly";
719 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
721 int default_group = -1;
722 scoped_refptr<FieldTrial> trial =
723 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
724 trial->SetForced();
726 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
727 EXPECT_EQ(default_group, trial->group());
728 EXPECT_EQ(kDefaultGroupName, trial->group_name());
731 TEST_F(FieldTrialTest, SetForcedDefaultWithExtraGroup) {
732 const char kTrialName[] = "SetForcedDefaultWithExtraGroup";
733 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
735 int default_group = -1;
736 scoped_refptr<FieldTrial> trial =
737 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
738 trial->SetForced();
740 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
741 const int extra_group = trial->AppendGroup("Extra", 100);
742 EXPECT_EQ(default_group, trial->group());
743 EXPECT_NE(extra_group, trial->group());
744 EXPECT_EQ(kDefaultGroupName, trial->group_name());
747 TEST_F(FieldTrialTest, SetForcedTurnFeatureOn) {
748 const char kTrialName[] = "SetForcedTurnFeatureOn";
749 const char kExtraGroupName[] = "Extra";
750 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
752 // Simulate a server-side (forced) config that turns the feature on when the
753 // original hard-coded config had it disabled.
754 scoped_refptr<FieldTrial> forced_trial =
755 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
756 forced_trial->AppendGroup(kExtraGroupName, 100);
757 forced_trial->SetForced();
759 int default_group = -1;
760 scoped_refptr<FieldTrial> client_trial =
761 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
762 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 0);
763 EXPECT_NE(default_group, extra_group);
765 EXPECT_FALSE(client_trial->group_reported_);
766 EXPECT_EQ(extra_group, client_trial->group());
767 EXPECT_TRUE(client_trial->group_reported_);
768 EXPECT_EQ(kExtraGroupName, client_trial->group_name());
771 TEST_F(FieldTrialTest, SetForcedTurnFeatureOff) {
772 const char kTrialName[] = "SetForcedTurnFeatureOff";
773 const char kExtraGroupName[] = "Extra";
774 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
776 // Simulate a server-side (forced) config that turns the feature off when the
777 // original hard-coded config had it enabled.
778 scoped_refptr<FieldTrial> forced_trial =
779 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
780 forced_trial->AppendGroup(kExtraGroupName, 0);
781 forced_trial->SetForced();
783 int default_group = -1;
784 scoped_refptr<FieldTrial> client_trial =
785 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
786 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 100);
787 EXPECT_NE(default_group, extra_group);
789 EXPECT_FALSE(client_trial->group_reported_);
790 EXPECT_EQ(default_group, client_trial->group());
791 EXPECT_TRUE(client_trial->group_reported_);
792 EXPECT_EQ(kDefaultGroupName, client_trial->group_name());
795 TEST_F(FieldTrialTest, SetForcedChangeDefault_Default) {
796 const char kTrialName[] = "SetForcedDefaultGroupChange";
797 const char kGroupAName[] = "A";
798 const char kGroupBName[] = "B";
799 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
801 // Simulate a server-side (forced) config that switches which group is default
802 // and ensures that the non-forced code receives the correct group numbers.
803 scoped_refptr<FieldTrial> forced_trial =
804 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL);
805 forced_trial->AppendGroup(kGroupBName, 100);
806 forced_trial->SetForced();
808 int default_group = -1;
809 scoped_refptr<FieldTrial> client_trial =
810 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group);
811 const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
812 EXPECT_NE(default_group, extra_group);
814 EXPECT_FALSE(client_trial->group_reported_);
815 EXPECT_EQ(default_group, client_trial->group());
816 EXPECT_TRUE(client_trial->group_reported_);
817 EXPECT_EQ(kGroupBName, client_trial->group_name());
820 TEST_F(FieldTrialTest, SetForcedChangeDefault_NonDefault) {
821 const char kTrialName[] = "SetForcedDefaultGroupChange";
822 const char kGroupAName[] = "A";
823 const char kGroupBName[] = "B";
824 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
826 // Simulate a server-side (forced) config that switches which group is default
827 // and ensures that the non-forced code receives the correct group numbers.
828 scoped_refptr<FieldTrial> forced_trial =
829 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL);
830 forced_trial->AppendGroup(kGroupBName, 0);
831 forced_trial->SetForced();
833 int default_group = -1;
834 scoped_refptr<FieldTrial> client_trial =
835 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group);
836 const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
837 EXPECT_NE(default_group, extra_group);
839 EXPECT_FALSE(client_trial->group_reported_);
840 EXPECT_EQ(extra_group, client_trial->group());
841 EXPECT_TRUE(client_trial->group_reported_);
842 EXPECT_EQ(kGroupAName, client_trial->group_name());
845 TEST_F(FieldTrialTest, Observe) {
846 const char kTrialName[] = "TrialToObserve1";
847 const char kSecondaryGroupName[] = "SecondaryGroup";
849 TestFieldTrialObserver observer;
850 int default_group = -1;
851 scoped_refptr<FieldTrial> trial =
852 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
853 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50);
854 const int chosen_group = trial->group();
855 EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group);
857 RunLoop().RunUntilIdle();
858 EXPECT_EQ(kTrialName, observer.trial_name());
859 if (chosen_group == default_group)
860 EXPECT_EQ(kDefaultGroupName, observer.group_name());
861 else
862 EXPECT_EQ(kSecondaryGroupName, observer.group_name());
865 TEST_F(FieldTrialTest, ObserveDisabled) {
866 const char kTrialName[] = "TrialToObserve2";
868 TestFieldTrialObserver observer;
869 int default_group = -1;
870 scoped_refptr<FieldTrial> trial =
871 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
872 trial->AppendGroup("A", 25);
873 trial->AppendGroup("B", 25);
874 trial->AppendGroup("C", 25);
875 trial->Disable();
877 // Observer shouldn't be notified of a disabled trial.
878 RunLoop().RunUntilIdle();
879 EXPECT_TRUE(observer.trial_name().empty());
880 EXPECT_TRUE(observer.group_name().empty());
882 // Observer shouldn't be notified even after a |group()| call.
883 EXPECT_EQ(default_group, trial->group());
884 RunLoop().RunUntilIdle();
885 EXPECT_TRUE(observer.trial_name().empty());
886 EXPECT_TRUE(observer.group_name().empty());
889 TEST_F(FieldTrialTest, ObserveForcedDisabled) {
890 const char kTrialName[] = "TrialToObserve3";
892 TestFieldTrialObserver observer;
893 int default_group = -1;
894 scoped_refptr<FieldTrial> trial =
895 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
896 trial->AppendGroup("A", 25);
897 trial->AppendGroup("B", 25);
898 trial->AppendGroup("C", 25);
899 trial->SetForced();
900 trial->Disable();
902 // Observer shouldn't be notified of a disabled trial, even when forced.
903 RunLoop().RunUntilIdle();
904 EXPECT_TRUE(observer.trial_name().empty());
905 EXPECT_TRUE(observer.group_name().empty());
907 // Observer shouldn't be notified even after a |group()| call.
908 EXPECT_EQ(default_group, trial->group());
909 RunLoop().RunUntilIdle();
910 EXPECT_TRUE(observer.trial_name().empty());
911 EXPECT_TRUE(observer.group_name().empty());
914 TEST_F(FieldTrialTest, DisabledTrialNotActive) {
915 const char kTrialName[] = "DisabledTrial";
916 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
918 scoped_refptr<FieldTrial> trial =
919 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
920 trial->AppendGroup("X", 50);
921 trial->Disable();
923 // Ensure the trial is not listed as active.
924 FieldTrial::ActiveGroups active_groups;
925 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
926 EXPECT_TRUE(active_groups.empty());
928 // Ensure the trial is not listed in the |StatesToString()| result.
929 std::string states;
930 FieldTrialList::StatesToString(&states);
931 EXPECT_TRUE(states.empty());
934 TEST_F(FieldTrialTest, ExpirationYearNotExpired) {
935 const char kTrialName[] = "NotExpired";
936 const char kGroupName[] = "Group2";
937 const int kProbability = 100;
938 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
940 scoped_refptr<FieldTrial> trial =
941 CreateFieldTrial(kTrialName, kProbability, kDefaultGroupName, NULL);
942 trial->AppendGroup(kGroupName, kProbability);
943 EXPECT_EQ(kGroupName, trial->group_name());
946 TEST_F(FieldTrialTest, FloatBoundariesGiveEqualGroupSizes) {
947 const int kBucketCount = 100;
949 // Try each boundary value |i / 100.0| as the entropy value.
950 for (int i = 0; i < kBucketCount; ++i) {
951 const double entropy = i / static_cast<double>(kBucketCount);
953 scoped_refptr<base::FieldTrial> trial(
954 new base::FieldTrial("test", kBucketCount, "default", entropy));
955 for (int j = 0; j < kBucketCount; ++j)
956 trial->AppendGroup(base::StringPrintf("%d", j), 1);
958 EXPECT_EQ(base::StringPrintf("%d", i), trial->group_name());
962 TEST_F(FieldTrialTest, DoesNotSurpassTotalProbability) {
963 const double kEntropyValue = 1.0 - 1e-9;
964 ASSERT_LT(kEntropyValue, 1.0);
966 scoped_refptr<base::FieldTrial> trial(
967 new base::FieldTrial("test", 2, "default", kEntropyValue));
968 trial->AppendGroup("1", 1);
969 trial->AppendGroup("2", 1);
971 EXPECT_EQ("2", trial->group_name());
974 TEST_F(FieldTrialTest, CreateSimulatedFieldTrial) {
975 const char kTrialName[] = "CreateSimulatedFieldTrial";
976 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
978 // Different cases to test, e.g. default vs. non default group being chosen.
979 struct {
980 double entropy_value;
981 const char* expected_group;
982 } test_cases[] = {
983 { 0.4, "A" },
984 { 0.85, "B" },
985 { 0.95, kDefaultGroupName },
988 for (size_t i = 0; i < arraysize(test_cases); ++i) {
989 TestFieldTrialObserver observer;
990 scoped_refptr<FieldTrial> trial(
991 FieldTrial::CreateSimulatedFieldTrial(kTrialName, 100, kDefaultGroupName,
992 test_cases[i].entropy_value));
993 trial->AppendGroup("A", 80);
994 trial->AppendGroup("B", 10);
995 EXPECT_EQ(test_cases[i].expected_group, trial->group_name());
997 // Field trial shouldn't have been registered with the list.
998 EXPECT_FALSE(FieldTrialList::TrialExists(kTrialName));
999 EXPECT_EQ(0u, FieldTrialList::GetFieldTrialCount());
1001 // Observer shouldn't have been notified.
1002 RunLoop().RunUntilIdle();
1003 EXPECT_TRUE(observer.trial_name().empty());
1005 // The trial shouldn't be in the active set of trials.
1006 FieldTrial::ActiveGroups active_groups;
1007 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
1008 EXPECT_TRUE(active_groups.empty());
1010 // The trial shouldn't be listed in the |StatesToString()| result.
1011 std::string states;
1012 FieldTrialList::StatesToString(&states);
1013 EXPECT_TRUE(states.empty());
1017 #if GTEST_HAS_DEATH_TEST
1018 TEST(FieldTrialDeathTest, OneTimeRandomizedTrialWithoutFieldTrialList) {
1019 // Trying to instantiate a one-time randomized field trial before the
1020 // FieldTrialList is created should crash.
1021 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial(
1022 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName,
1023 base::FieldTrialList::kNoExpirationYear, 1, 1,
1024 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), "");
1026 #endif
1028 } // namespace base