Adds support for the embedded_test_server in webview's app_shell_browsertest.
[chromium-blink-merge.git] / base / metrics / field_trial_unittest.cc
blob52ce8554db06e98ff1ba88b98d8994072e491c80
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/message_loop/message_loop.h"
8 #include "base/rand_util.h"
9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
16 namespace {
18 // Default group name used by several tests.
19 const char kDefaultGroupName[] = "DefaultGroup";
21 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date.
22 scoped_refptr<base::FieldTrial> CreateFieldTrial(
23 const std::string& trial_name,
24 int total_probability,
25 const std::string& default_group_name,
26 int* default_group_number) {
27 return FieldTrialList::FactoryGetFieldTrial(
28 trial_name, total_probability, default_group_name,
29 base::FieldTrialList::kNoExpirationYear, 1, 1,
30 base::FieldTrial::SESSION_RANDOMIZED, default_group_number);
33 int GetLastYear() {
34 Time last_year_time = Time::NowFromSystemTime() - TimeDelta::FromDays(365);
35 Time::Exploded exploded;
36 last_year_time.LocalExplode(&exploded);
37 return exploded.year;
40 // FieldTrialList::Observer implementation for testing.
41 class TestFieldTrialObserver : public FieldTrialList::Observer {
42 public:
43 TestFieldTrialObserver() {
44 FieldTrialList::AddObserver(this);
47 virtual ~TestFieldTrialObserver() {
48 FieldTrialList::RemoveObserver(this);
51 virtual void OnFieldTrialGroupFinalized(const std::string& trial,
52 const std::string& group) OVERRIDE {
53 trial_name_ = trial;
54 group_name_ = group;
57 const std::string& trial_name() const { return trial_name_; }
58 const std::string& group_name() const { return group_name_; }
60 private:
61 std::string trial_name_;
62 std::string group_name_;
64 DISALLOW_COPY_AND_ASSIGN(TestFieldTrialObserver);
67 } // namespace
69 class FieldTrialTest : public testing::Test {
70 public:
71 FieldTrialTest() : trial_list_(NULL) {}
73 private:
74 MessageLoop message_loop_;
75 FieldTrialList trial_list_;
78 // Test registration, and also check that destructors are called for trials
79 // (and that Valgrind doesn't catch us leaking).
80 TEST_F(FieldTrialTest, Registration) {
81 const char* name1 = "name 1 test";
82 const char* name2 = "name 2 test";
83 EXPECT_FALSE(FieldTrialList::Find(name1));
84 EXPECT_FALSE(FieldTrialList::Find(name2));
86 scoped_refptr<FieldTrial> trial1 =
87 CreateFieldTrial(name1, 10, "default name 1 test", NULL);
88 EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_);
89 EXPECT_EQ(name1, trial1->trial_name());
90 EXPECT_EQ("", trial1->group_name_internal());
92 trial1->AppendGroup(std::string(), 7);
94 EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1));
95 EXPECT_FALSE(FieldTrialList::Find(name2));
97 scoped_refptr<FieldTrial> trial2 =
98 CreateFieldTrial(name2, 10, "default name 2 test", NULL);
99 EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_);
100 EXPECT_EQ(name2, trial2->trial_name());
101 EXPECT_EQ("", trial2->group_name_internal());
103 trial2->AppendGroup("a first group", 7);
105 EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1));
106 EXPECT_EQ(trial2.get(), FieldTrialList::Find(name2));
107 // Note: FieldTrialList should delete the objects at shutdown.
110 TEST_F(FieldTrialTest, AbsoluteProbabilities) {
111 char always_true[] = " always true";
112 char default_always_true[] = " default always true";
113 char always_false[] = " always false";
114 char default_always_false[] = " default always false";
115 for (int i = 1; i < 250; ++i) {
116 // Try lots of names, by changing the first character of the name.
117 char c = static_cast<char>(i);
118 always_true[0] = c;
119 default_always_true[0] = c;
120 always_false[0] = c;
121 default_always_false[0] = c;
123 scoped_refptr<FieldTrial> trial_true =
124 CreateFieldTrial(always_true, 10, default_always_true, NULL);
125 const std::string winner = "TheWinner";
126 int winner_group = trial_true->AppendGroup(winner, 10);
128 EXPECT_EQ(winner_group, trial_true->group());
129 EXPECT_EQ(winner, trial_true->group_name());
131 scoped_refptr<FieldTrial> trial_false =
132 CreateFieldTrial(always_false, 10, default_always_false, NULL);
133 int loser_group = trial_false->AppendGroup("ALoser", 0);
135 EXPECT_NE(loser_group, trial_false->group());
139 TEST_F(FieldTrialTest, RemainingProbability) {
140 // First create a test that hasn't had a winner yet.
141 const std::string winner = "Winner";
142 const std::string loser = "Loser";
143 scoped_refptr<FieldTrial> trial;
144 int counter = 0;
145 int default_group_number = -1;
146 do {
147 std::string name = StringPrintf("trial%d", ++counter);
148 trial = CreateFieldTrial(name, 10, winner, &default_group_number);
149 trial->AppendGroup(loser, 5); // 50% chance of not being chosen.
150 // If a group is not assigned, group_ will be kNotFinalized.
151 } while (trial->group_ != FieldTrial::kNotFinalized);
153 // And that 'default' group (winner) should always win.
154 EXPECT_EQ(default_group_number, trial->group());
156 // And that winner should ALWAYS win.
157 EXPECT_EQ(winner, trial->group_name());
160 TEST_F(FieldTrialTest, FiftyFiftyProbability) {
161 // Check that even with small divisors, we have the proper probabilities, and
162 // all outcomes are possible. Since this is a 50-50 test, it should get both
163 // outcomes in a few tries, but we'll try no more than 100 times (and be flaky
164 // with probability around 1 in 2^99).
165 bool first_winner = false;
166 bool second_winner = false;
167 int counter = 0;
168 do {
169 std::string name = base::StringPrintf("FiftyFifty%d", ++counter);
170 std::string default_group_name = base::StringPrintf("Default FiftyFifty%d",
171 ++counter);
172 scoped_refptr<FieldTrial> trial =
173 CreateFieldTrial(name, 2, default_group_name, NULL);
174 trial->AppendGroup("first", 1); // 50% chance of being chosen.
175 // If group_ is kNotFinalized, then a group assignement hasn't been done.
176 if (trial->group_ != FieldTrial::kNotFinalized) {
177 first_winner = true;
178 continue;
180 trial->AppendGroup("second", 1); // Always chosen at this point.
181 EXPECT_NE(FieldTrial::kNotFinalized, trial->group());
182 second_winner = true;
183 } while ((!second_winner || !first_winner) && counter < 100);
184 EXPECT_TRUE(second_winner);
185 EXPECT_TRUE(first_winner);
188 TEST_F(FieldTrialTest, MiddleProbabilities) {
189 char name[] = " same name";
190 char default_group_name[] = " default same name";
191 bool false_event_seen = false;
192 bool true_event_seen = false;
193 for (int i = 1; i < 250; ++i) {
194 char c = static_cast<char>(i);
195 name[0] = c;
196 default_group_name[0] = c;
197 scoped_refptr<FieldTrial> trial =
198 CreateFieldTrial(name, 10, default_group_name, NULL);
199 int might_win = trial->AppendGroup("MightWin", 5);
201 if (trial->group() == might_win) {
202 true_event_seen = true;
203 } else {
204 false_event_seen = true;
206 if (false_event_seen && true_event_seen)
207 return; // Successful test!!!
209 // Very surprising to get here. Probability should be around 1 in 2 ** 250.
210 // One of the following will fail.
211 EXPECT_TRUE(false_event_seen);
212 EXPECT_TRUE(true_event_seen);
215 TEST_F(FieldTrialTest, OneWinner) {
216 char name[] = "Some name";
217 char default_group_name[] = "Default some name";
218 int group_count(10);
220 int default_group_number = -1;
221 scoped_refptr<FieldTrial> trial =
222 CreateFieldTrial(name, group_count, default_group_name, NULL);
223 int winner_index(-2);
224 std::string winner_name;
226 for (int i = 1; i <= group_count; ++i) {
227 int might_win = trial->AppendGroup(std::string(), 1);
229 // Because we keep appending groups, we want to see if the last group that
230 // was added has been assigned or not.
231 if (trial->group_ == might_win) {
232 EXPECT_EQ(-2, winner_index);
233 winner_index = might_win;
234 StringAppendF(&winner_name, "%d", might_win);
235 EXPECT_EQ(winner_name, trial->group_name());
238 EXPECT_GE(winner_index, 0);
239 // Since all groups cover the total probability, we should not have
240 // chosen the default group.
241 EXPECT_NE(trial->group(), default_group_number);
242 EXPECT_EQ(trial->group(), winner_index);
243 EXPECT_EQ(trial->group_name(), winner_name);
246 TEST_F(FieldTrialTest, DisableProbability) {
247 const std::string default_group_name = "Default group";
248 const std::string loser = "Loser";
249 const std::string name = "Trial";
251 // Create a field trail that has expired.
252 int default_group_number = -1;
253 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
254 name, 1000000000, default_group_name, GetLastYear(), 1, 1,
255 FieldTrial::SESSION_RANDOMIZED,
256 &default_group_number);
257 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen.
259 // Because trial has expired, we should always be in the default group.
260 EXPECT_EQ(default_group_number, trial->group());
262 // And that default_group_name should ALWAYS win.
263 EXPECT_EQ(default_group_name, trial->group_name());
266 TEST_F(FieldTrialTest, ActiveGroups) {
267 std::string no_group("No Group");
268 scoped_refptr<FieldTrial> trial =
269 CreateFieldTrial(no_group, 10, "Default", NULL);
271 // There is no winner yet, so no NameGroupId should be returned.
272 FieldTrial::ActiveGroup active_group;
273 EXPECT_FALSE(trial->GetActiveGroup(&active_group));
275 // Create a single winning group.
276 std::string one_winner("One Winner");
277 trial = CreateFieldTrial(one_winner, 10, "Default", NULL);
278 std::string winner("Winner");
279 trial->AppendGroup(winner, 10);
280 EXPECT_FALSE(trial->GetActiveGroup(&active_group));
281 // Finalize the group selection by accessing the selected group.
282 trial->group();
283 EXPECT_TRUE(trial->GetActiveGroup(&active_group));
284 EXPECT_EQ(one_winner, active_group.trial_name);
285 EXPECT_EQ(winner, active_group.group_name);
287 std::string multi_group("MultiGroup");
288 scoped_refptr<FieldTrial> multi_group_trial =
289 CreateFieldTrial(multi_group, 9, "Default", NULL);
291 multi_group_trial->AppendGroup("Me", 3);
292 multi_group_trial->AppendGroup("You", 3);
293 multi_group_trial->AppendGroup("Them", 3);
294 EXPECT_FALSE(multi_group_trial->GetActiveGroup(&active_group));
295 // Finalize the group selection by accessing the selected group.
296 multi_group_trial->group();
297 EXPECT_TRUE(multi_group_trial->GetActiveGroup(&active_group));
298 EXPECT_EQ(multi_group, active_group.trial_name);
299 EXPECT_EQ(multi_group_trial->group_name(), active_group.group_name);
301 // Now check if the list is built properly...
302 FieldTrial::ActiveGroups active_groups;
303 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
304 EXPECT_EQ(2U, active_groups.size());
305 for (size_t i = 0; i < active_groups.size(); ++i) {
306 // Order is not guaranteed, so check all values.
307 EXPECT_NE(no_group, active_groups[i].trial_name);
308 EXPECT_TRUE(one_winner != active_groups[i].trial_name ||
309 winner == active_groups[i].group_name);
310 EXPECT_TRUE(multi_group != active_groups[i].trial_name ||
311 multi_group_trial->group_name() == active_groups[i].group_name);
315 TEST_F(FieldTrialTest, ActiveGroupsNotFinalized) {
316 const char kTrialName[] = "TestTrial";
317 const char kSecondaryGroupName[] = "SecondaryGroup";
319 int default_group = -1;
320 scoped_refptr<FieldTrial> trial =
321 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
322 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50);
324 // Before |group()| is called, |GetActiveGroup()| should return false.
325 FieldTrial::ActiveGroup active_group;
326 EXPECT_FALSE(trial->GetActiveGroup(&active_group));
328 // |GetActiveFieldTrialGroups()| should also not include the trial.
329 FieldTrial::ActiveGroups active_groups;
330 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
331 EXPECT_TRUE(active_groups.empty());
333 // After |group()| has been called, both APIs should succeed.
334 const int chosen_group = trial->group();
335 EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group);
337 EXPECT_TRUE(trial->GetActiveGroup(&active_group));
338 EXPECT_EQ(kTrialName, active_group.trial_name);
339 if (chosen_group == default_group)
340 EXPECT_EQ(kDefaultGroupName, active_group.group_name);
341 else
342 EXPECT_EQ(kSecondaryGroupName, active_group.group_name);
344 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
345 ASSERT_EQ(1U, active_groups.size());
346 EXPECT_EQ(kTrialName, active_groups[0].trial_name);
347 EXPECT_EQ(active_group.group_name, active_groups[0].group_name);
350 TEST_F(FieldTrialTest, Save) {
351 std::string save_string;
353 scoped_refptr<FieldTrial> trial =
354 CreateFieldTrial("Some name", 10, "Default some name", NULL);
355 // There is no winner yet, so no textual group name is associated with trial.
356 // In this case, the trial should not be included.
357 EXPECT_EQ("", trial->group_name_internal());
358 FieldTrialList::StatesToString(&save_string);
359 EXPECT_EQ("", save_string);
360 save_string.clear();
362 // Create a winning group.
363 trial->AppendGroup("Winner", 10);
364 // Finalize the group selection by accessing the selected group.
365 trial->group();
366 FieldTrialList::StatesToString(&save_string);
367 EXPECT_EQ("Some name/Winner/", save_string);
368 save_string.clear();
370 // Create a second trial and winning group.
371 scoped_refptr<FieldTrial> trial2 =
372 CreateFieldTrial("xxx", 10, "Default xxx", NULL);
373 trial2->AppendGroup("yyyy", 10);
374 // Finalize the group selection by accessing the selected group.
375 trial2->group();
377 FieldTrialList::StatesToString(&save_string);
378 // We assume names are alphabetized... though this is not critical.
379 EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string);
380 save_string.clear();
382 // Create a third trial with only the default group.
383 scoped_refptr<FieldTrial> trial3 =
384 CreateFieldTrial("zzz", 10, "default", NULL);
385 // Finalize the group selection by accessing the selected group.
386 trial3->group();
388 FieldTrialList::StatesToString(&save_string);
389 EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string);
392 TEST_F(FieldTrialTest, Restore) {
393 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
394 ASSERT_FALSE(FieldTrialList::TrialExists("xxx"));
396 FieldTrialList::CreateTrialsFromString("Some_name/Winner/xxx/yyyy/",
397 FieldTrialList::DONT_ACTIVATE_TRIALS,
398 std::set<std::string>());
400 FieldTrial* trial = FieldTrialList::Find("Some_name");
401 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
402 EXPECT_EQ("Winner", trial->group_name());
403 EXPECT_EQ("Some_name", trial->trial_name());
405 trial = FieldTrialList::Find("xxx");
406 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
407 EXPECT_EQ("yyyy", trial->group_name());
408 EXPECT_EQ("xxx", trial->trial_name());
411 TEST_F(FieldTrialTest, RestoreNotEndingWithSlash) {
412 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(
413 "tname/gname", FieldTrialList::DONT_ACTIVATE_TRIALS,
414 std::set<std::string>()));
416 FieldTrial* trial = FieldTrialList::Find("tname");
417 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
418 EXPECT_EQ("gname", trial->group_name());
419 EXPECT_EQ("tname", trial->trial_name());
422 TEST_F(FieldTrialTest, BogusRestore) {
423 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
424 "MissingSlash", FieldTrialList::DONT_ACTIVATE_TRIALS,
425 std::set<std::string>()));
426 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
427 "MissingGroupName/", FieldTrialList::DONT_ACTIVATE_TRIALS,
428 std::set<std::string>()));
429 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
430 "noname, only group/", FieldTrialList::DONT_ACTIVATE_TRIALS,
431 std::set<std::string>()));
434 TEST_F(FieldTrialTest, DuplicateRestore) {
435 scoped_refptr<FieldTrial> trial =
436 CreateFieldTrial("Some name", 10, "Default", NULL);
437 trial->AppendGroup("Winner", 10);
438 // Finalize the group selection by accessing the selected group.
439 trial->group();
440 std::string save_string;
441 FieldTrialList::StatesToString(&save_string);
442 EXPECT_EQ("Some name/Winner/", save_string);
444 // It is OK if we redundantly specify a winner.
445 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(
446 save_string, FieldTrialList::DONT_ACTIVATE_TRIALS,
447 std::set<std::string>()));
449 // But it is an error to try to change to a different winner.
450 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
451 "Some name/Loser/", FieldTrialList::DONT_ACTIVATE_TRIALS,
452 std::set<std::string>()));
455 TEST_F(FieldTrialTest, CreateTrialsFromStringActive) {
456 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
457 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
458 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
459 "Abc/def/Xyz/zyx/", FieldTrialList::ACTIVATE_TRIALS,
460 std::set<std::string>()));
462 FieldTrial::ActiveGroups active_groups;
463 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
464 ASSERT_EQ(2U, active_groups.size());
465 EXPECT_EQ("Abc", active_groups[0].trial_name);
466 EXPECT_EQ("def", active_groups[0].group_name);
467 EXPECT_EQ("Xyz", active_groups[1].trial_name);
468 EXPECT_EQ("zyx", active_groups[1].group_name);
471 TEST_F(FieldTrialTest, CreateTrialsFromStringNotActive) {
472 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
473 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
474 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
475 "Abc/def/Xyz/zyx/", FieldTrialList::DONT_ACTIVATE_TRIALS,
476 std::set<std::string>()));
478 FieldTrial::ActiveGroups active_groups;
479 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
480 ASSERT_TRUE(active_groups.empty());
482 // Check that the values still get returned and querying them activates them.
483 EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
484 EXPECT_EQ("zyx", FieldTrialList::FindFullName("Xyz"));
486 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
487 ASSERT_EQ(2U, active_groups.size());
488 EXPECT_EQ("Abc", active_groups[0].trial_name);
489 EXPECT_EQ("def", active_groups[0].group_name);
490 EXPECT_EQ("Xyz", active_groups[1].trial_name);
491 EXPECT_EQ("zyx", active_groups[1].group_name);
494 TEST_F(FieldTrialTest, CreateTrialsFromStringActiveObserver) {
495 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
497 TestFieldTrialObserver observer;
498 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
499 "Abc/def/", FieldTrialList::ACTIVATE_TRIALS, std::set<std::string>()));
501 RunLoop().RunUntilIdle();
502 EXPECT_EQ("Abc", observer.trial_name());
503 EXPECT_EQ("def", observer.group_name());
506 TEST_F(FieldTrialTest, CreateTrialsFromStringNotActiveObserver) {
507 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
509 TestFieldTrialObserver observer;
510 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
511 "Abc/def/", FieldTrialList::DONT_ACTIVATE_TRIALS,
512 std::set<std::string>()));
513 RunLoop().RunUntilIdle();
514 // Observer shouldn't be notified.
515 EXPECT_TRUE(observer.trial_name().empty());
517 // Check that the values still get returned and querying them activates them.
518 EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
520 RunLoop().RunUntilIdle();
521 EXPECT_EQ("Abc", observer.trial_name());
522 EXPECT_EQ("def", observer.group_name());
525 TEST_F(FieldTrialTest, CreateTrialsFromStringWithIgnoredFieldTrials) {
526 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
527 ASSERT_FALSE(FieldTrialList::TrialExists("Foo"));
528 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
529 ASSERT_FALSE(FieldTrialList::TrialExists("Bar"));
530 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
532 std::set<std::string> ignored_trial_names;
533 ignored_trial_names.insert("Unaccepted1");
534 ignored_trial_names.insert("Unaccepted2");
535 ignored_trial_names.insert("Unaccepted3");
537 FieldTrialList::CreateTrialsFromString(
538 "Unaccepted1/Unaccepted1_name/"
539 "Foo/Foo_name/"
540 "Unaccepted2/Unaccepted2_name/"
541 "Bar/Bar_name/"
542 "Unaccepted3/Unaccepted3_name/",
543 FieldTrialList::DONT_ACTIVATE_TRIALS,
544 ignored_trial_names);
546 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
547 EXPECT_TRUE(FieldTrialList::TrialExists("Foo"));
548 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
549 EXPECT_TRUE(FieldTrialList::TrialExists("Bar"));
550 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
552 FieldTrial::ActiveGroups active_groups;
553 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
554 EXPECT_TRUE(active_groups.empty());
556 FieldTrial* trial = FieldTrialList::Find("Foo");
557 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
558 EXPECT_EQ("Foo", trial->trial_name());
559 EXPECT_EQ("Foo_name", trial->group_name());
561 trial = FieldTrialList::Find("Bar");
562 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
563 EXPECT_EQ("Bar", trial->trial_name());
564 EXPECT_EQ("Bar_name", trial->group_name());
567 TEST_F(FieldTrialTest, CreateFieldTrial) {
568 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
570 FieldTrialList::CreateFieldTrial("Some_name", "Winner");
572 FieldTrial* trial = FieldTrialList::Find("Some_name");
573 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
574 EXPECT_EQ("Winner", trial->group_name());
575 EXPECT_EQ("Some_name", trial->trial_name());
578 TEST_F(FieldTrialTest, CreateFieldTrialIsNotActive) {
579 const char kTrialName[] = "CreateFieldTrialIsActiveTrial";
580 const char kWinnerGroup[] = "Winner";
581 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
582 FieldTrialList::CreateFieldTrial(kTrialName, kWinnerGroup);
584 FieldTrial::ActiveGroups active_groups;
585 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
586 EXPECT_TRUE(active_groups.empty());
589 TEST_F(FieldTrialTest, DuplicateFieldTrial) {
590 scoped_refptr<FieldTrial> trial =
591 CreateFieldTrial("Some_name", 10, "Default", NULL);
592 trial->AppendGroup("Winner", 10);
594 // It is OK if we redundantly specify a winner.
595 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner");
596 EXPECT_TRUE(trial1 != NULL);
598 // But it is an error to try to change to a different winner.
599 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser");
600 EXPECT_TRUE(trial2 == NULL);
603 TEST_F(FieldTrialTest, DisableImmediately) {
604 int default_group_number = -1;
605 scoped_refptr<FieldTrial> trial =
606 CreateFieldTrial("trial", 100, "default", &default_group_number);
607 trial->Disable();
608 ASSERT_EQ("default", trial->group_name());
609 ASSERT_EQ(default_group_number, trial->group());
612 TEST_F(FieldTrialTest, DisableAfterInitialization) {
613 scoped_refptr<FieldTrial> trial =
614 CreateFieldTrial("trial", 100, "default", NULL);
615 trial->AppendGroup("non_default", 100);
616 trial->Disable();
617 ASSERT_EQ("default", trial->group_name());
620 TEST_F(FieldTrialTest, ForcedFieldTrials) {
621 // Validate we keep the forced choice.
622 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Use the",
623 "Force");
624 EXPECT_STREQ("Force", forced_trial->group_name().c_str());
626 int default_group_number = -1;
627 scoped_refptr<FieldTrial> factory_trial =
628 CreateFieldTrial("Use the", 1000, "default", &default_group_number);
629 EXPECT_EQ(factory_trial.get(), forced_trial);
631 int chosen_group = factory_trial->AppendGroup("Force", 100);
632 EXPECT_EQ(chosen_group, factory_trial->group());
633 int not_chosen_group = factory_trial->AppendGroup("Dark Side", 100);
634 EXPECT_NE(chosen_group, not_chosen_group);
636 // Since we didn't force the default group, we should not be returned the
637 // chosen group as the default group.
638 EXPECT_NE(default_group_number, chosen_group);
639 int new_group = factory_trial->AppendGroup("Duck Tape", 800);
640 EXPECT_NE(chosen_group, new_group);
641 // The new group should not be the default group either.
642 EXPECT_NE(default_group_number, new_group);
645 TEST_F(FieldTrialTest, ForcedFieldTrialsDefaultGroup) {
646 // Forcing the default should use the proper group ID.
647 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Trial Name",
648 "Default");
649 int default_group_number = -1;
650 scoped_refptr<FieldTrial> factory_trial =
651 CreateFieldTrial("Trial Name", 1000, "Default", &default_group_number);
652 EXPECT_EQ(forced_trial, factory_trial.get());
654 int other_group = factory_trial->AppendGroup("Not Default", 100);
655 EXPECT_STREQ("Default", factory_trial->group_name().c_str());
656 EXPECT_EQ(default_group_number, factory_trial->group());
657 EXPECT_NE(other_group, factory_trial->group());
659 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800);
660 EXPECT_NE(new_other_group, factory_trial->group());
663 TEST_F(FieldTrialTest, SetForced) {
664 // Start by setting a trial for which we ensure a winner...
665 int default_group_number = -1;
666 scoped_refptr<FieldTrial> forced_trial =
667 CreateFieldTrial("Use the", 1, "default", &default_group_number);
668 EXPECT_EQ(forced_trial, forced_trial);
670 int forced_group = forced_trial->AppendGroup("Force", 1);
671 EXPECT_EQ(forced_group, forced_trial->group());
673 // Now force it.
674 forced_trial->SetForced();
676 // Now try to set it up differently as a hard coded registration would.
677 scoped_refptr<FieldTrial> hard_coded_trial =
678 CreateFieldTrial("Use the", 1, "default", &default_group_number);
679 EXPECT_EQ(hard_coded_trial, forced_trial);
681 int would_lose_group = hard_coded_trial->AppendGroup("Force", 0);
682 EXPECT_EQ(forced_group, hard_coded_trial->group());
683 EXPECT_EQ(forced_group, would_lose_group);
685 // Same thing if we would have done it to win again.
686 scoped_refptr<FieldTrial> other_hard_coded_trial =
687 CreateFieldTrial("Use the", 1, "default", &default_group_number);
688 EXPECT_EQ(other_hard_coded_trial, forced_trial);
690 int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1);
691 EXPECT_EQ(forced_group, other_hard_coded_trial->group());
692 EXPECT_EQ(forced_group, would_win_group);
695 TEST_F(FieldTrialTest, SetForcedDefaultOnly) {
696 const char kTrialName[] = "SetForcedDefaultOnly";
697 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
699 int default_group = -1;
700 scoped_refptr<FieldTrial> trial =
701 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
702 trial->SetForced();
704 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
705 EXPECT_EQ(default_group, trial->group());
706 EXPECT_EQ(kDefaultGroupName, trial->group_name());
709 TEST_F(FieldTrialTest, SetForcedDefaultWithExtraGroup) {
710 const char kTrialName[] = "SetForcedDefaultWithExtraGroup";
711 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
713 int default_group = -1;
714 scoped_refptr<FieldTrial> trial =
715 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
716 trial->SetForced();
718 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
719 const int extra_group = trial->AppendGroup("Extra", 100);
720 EXPECT_EQ(default_group, trial->group());
721 EXPECT_NE(extra_group, trial->group());
722 EXPECT_EQ(kDefaultGroupName, trial->group_name());
725 TEST_F(FieldTrialTest, SetForcedTurnFeatureOn) {
726 const char kTrialName[] = "SetForcedTurnFeatureOn";
727 const char kExtraGroupName[] = "Extra";
728 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
730 // Simulate a server-side (forced) config that turns the feature on when the
731 // original hard-coded config had it disabled.
732 scoped_refptr<FieldTrial> forced_trial =
733 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
734 forced_trial->AppendGroup(kExtraGroupName, 100);
735 forced_trial->SetForced();
737 int default_group = -1;
738 scoped_refptr<FieldTrial> client_trial =
739 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
740 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 0);
741 EXPECT_NE(default_group, extra_group);
743 EXPECT_FALSE(client_trial->group_reported_);
744 EXPECT_EQ(extra_group, client_trial->group());
745 EXPECT_TRUE(client_trial->group_reported_);
746 EXPECT_EQ(kExtraGroupName, client_trial->group_name());
749 TEST_F(FieldTrialTest, SetForcedTurnFeatureOff) {
750 const char kTrialName[] = "SetForcedTurnFeatureOff";
751 const char kExtraGroupName[] = "Extra";
752 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
754 // Simulate a server-side (forced) config that turns the feature off when the
755 // original hard-coded config had it enabled.
756 scoped_refptr<FieldTrial> forced_trial =
757 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
758 forced_trial->AppendGroup(kExtraGroupName, 0);
759 forced_trial->SetForced();
761 int default_group = -1;
762 scoped_refptr<FieldTrial> client_trial =
763 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
764 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 100);
765 EXPECT_NE(default_group, extra_group);
767 EXPECT_FALSE(client_trial->group_reported_);
768 EXPECT_EQ(default_group, client_trial->group());
769 EXPECT_TRUE(client_trial->group_reported_);
770 EXPECT_EQ(kDefaultGroupName, client_trial->group_name());
773 TEST_F(FieldTrialTest, SetForcedChangeDefault_Default) {
774 const char kTrialName[] = "SetForcedDefaultGroupChange";
775 const char kGroupAName[] = "A";
776 const char kGroupBName[] = "B";
777 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
779 // Simulate a server-side (forced) config that switches which group is default
780 // and ensures that the non-forced code receives the correct group numbers.
781 scoped_refptr<FieldTrial> forced_trial =
782 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL);
783 forced_trial->AppendGroup(kGroupBName, 100);
784 forced_trial->SetForced();
786 int default_group = -1;
787 scoped_refptr<FieldTrial> client_trial =
788 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group);
789 const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
790 EXPECT_NE(default_group, extra_group);
792 EXPECT_FALSE(client_trial->group_reported_);
793 EXPECT_EQ(default_group, client_trial->group());
794 EXPECT_TRUE(client_trial->group_reported_);
795 EXPECT_EQ(kGroupBName, client_trial->group_name());
798 TEST_F(FieldTrialTest, SetForcedChangeDefault_NonDefault) {
799 const char kTrialName[] = "SetForcedDefaultGroupChange";
800 const char kGroupAName[] = "A";
801 const char kGroupBName[] = "B";
802 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
804 // Simulate a server-side (forced) config that switches which group is default
805 // and ensures that the non-forced code receives the correct group numbers.
806 scoped_refptr<FieldTrial> forced_trial =
807 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL);
808 forced_trial->AppendGroup(kGroupBName, 0);
809 forced_trial->SetForced();
811 int default_group = -1;
812 scoped_refptr<FieldTrial> client_trial =
813 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group);
814 const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
815 EXPECT_NE(default_group, extra_group);
817 EXPECT_FALSE(client_trial->group_reported_);
818 EXPECT_EQ(extra_group, client_trial->group());
819 EXPECT_TRUE(client_trial->group_reported_);
820 EXPECT_EQ(kGroupAName, client_trial->group_name());
823 TEST_F(FieldTrialTest, Observe) {
824 const char kTrialName[] = "TrialToObserve1";
825 const char kSecondaryGroupName[] = "SecondaryGroup";
827 TestFieldTrialObserver observer;
828 int default_group = -1;
829 scoped_refptr<FieldTrial> trial =
830 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
831 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50);
832 const int chosen_group = trial->group();
833 EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group);
835 RunLoop().RunUntilIdle();
836 EXPECT_EQ(kTrialName, observer.trial_name());
837 if (chosen_group == default_group)
838 EXPECT_EQ(kDefaultGroupName, observer.group_name());
839 else
840 EXPECT_EQ(kSecondaryGroupName, observer.group_name());
843 TEST_F(FieldTrialTest, ObserveDisabled) {
844 const char kTrialName[] = "TrialToObserve2";
846 TestFieldTrialObserver observer;
847 int default_group = -1;
848 scoped_refptr<FieldTrial> trial =
849 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
850 trial->AppendGroup("A", 25);
851 trial->AppendGroup("B", 25);
852 trial->AppendGroup("C", 25);
853 trial->Disable();
855 // Observer shouldn't be notified of a disabled trial.
856 RunLoop().RunUntilIdle();
857 EXPECT_TRUE(observer.trial_name().empty());
858 EXPECT_TRUE(observer.group_name().empty());
860 // Observer shouldn't be notified even after a |group()| call.
861 EXPECT_EQ(default_group, trial->group());
862 RunLoop().RunUntilIdle();
863 EXPECT_TRUE(observer.trial_name().empty());
864 EXPECT_TRUE(observer.group_name().empty());
867 TEST_F(FieldTrialTest, ObserveForcedDisabled) {
868 const char kTrialName[] = "TrialToObserve3";
870 TestFieldTrialObserver observer;
871 int default_group = -1;
872 scoped_refptr<FieldTrial> trial =
873 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group);
874 trial->AppendGroup("A", 25);
875 trial->AppendGroup("B", 25);
876 trial->AppendGroup("C", 25);
877 trial->SetForced();
878 trial->Disable();
880 // Observer shouldn't be notified of a disabled trial, even when forced.
881 RunLoop().RunUntilIdle();
882 EXPECT_TRUE(observer.trial_name().empty());
883 EXPECT_TRUE(observer.group_name().empty());
885 // Observer shouldn't be notified even after a |group()| call.
886 EXPECT_EQ(default_group, trial->group());
887 RunLoop().RunUntilIdle();
888 EXPECT_TRUE(observer.trial_name().empty());
889 EXPECT_TRUE(observer.group_name().empty());
892 TEST_F(FieldTrialTest, DisabledTrialNotActive) {
893 const char kTrialName[] = "DisabledTrial";
894 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
896 scoped_refptr<FieldTrial> trial =
897 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL);
898 trial->AppendGroup("X", 50);
899 trial->Disable();
901 // Ensure the trial is not listed as active.
902 FieldTrial::ActiveGroups active_groups;
903 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
904 EXPECT_TRUE(active_groups.empty());
906 // Ensure the trial is not listed in the |StatesToString()| result.
907 std::string states;
908 FieldTrialList::StatesToString(&states);
909 EXPECT_TRUE(states.empty());
912 TEST_F(FieldTrialTest, ExpirationYearNotExpired) {
913 const char kTrialName[] = "NotExpired";
914 const char kGroupName[] = "Group2";
915 const int kProbability = 100;
916 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
918 scoped_refptr<FieldTrial> trial =
919 CreateFieldTrial(kTrialName, kProbability, kDefaultGroupName, NULL);
920 trial->AppendGroup(kGroupName, kProbability);
921 EXPECT_EQ(kGroupName, trial->group_name());
924 TEST_F(FieldTrialTest, FloatBoundariesGiveEqualGroupSizes) {
925 const int kBucketCount = 100;
927 // Try each boundary value |i / 100.0| as the entropy value.
928 for (int i = 0; i < kBucketCount; ++i) {
929 const double entropy = i / static_cast<double>(kBucketCount);
931 scoped_refptr<base::FieldTrial> trial(
932 new base::FieldTrial("test", kBucketCount, "default", entropy));
933 for (int j = 0; j < kBucketCount; ++j)
934 trial->AppendGroup(base::StringPrintf("%d", j), 1);
936 EXPECT_EQ(base::StringPrintf("%d", i), trial->group_name());
940 TEST_F(FieldTrialTest, DoesNotSurpassTotalProbability) {
941 const double kEntropyValue = 1.0 - 1e-9;
942 ASSERT_LT(kEntropyValue, 1.0);
944 scoped_refptr<base::FieldTrial> trial(
945 new base::FieldTrial("test", 2, "default", kEntropyValue));
946 trial->AppendGroup("1", 1);
947 trial->AppendGroup("2", 1);
949 EXPECT_EQ("2", trial->group_name());
952 TEST_F(FieldTrialTest, CreateSimulatedFieldTrial) {
953 const char kTrialName[] = "CreateSimulatedFieldTrial";
954 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
956 // Different cases to test, e.g. default vs. non default group being chosen.
957 struct {
958 double entropy_value;
959 const char* expected_group;
960 } test_cases[] = {
961 { 0.4, "A" },
962 { 0.85, "B" },
963 { 0.95, kDefaultGroupName },
966 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
967 TestFieldTrialObserver observer;
968 scoped_refptr<FieldTrial> trial(
969 FieldTrial::CreateSimulatedFieldTrial(kTrialName, 100, kDefaultGroupName,
970 test_cases[i].entropy_value));
971 trial->AppendGroup("A", 80);
972 trial->AppendGroup("B", 10);
973 EXPECT_EQ(test_cases[i].expected_group, trial->group_name());
975 // Field trial shouldn't have been registered with the list.
976 EXPECT_FALSE(FieldTrialList::TrialExists(kTrialName));
977 EXPECT_EQ(0u, FieldTrialList::GetFieldTrialCount());
979 // Observer shouldn't have been notified.
980 RunLoop().RunUntilIdle();
981 EXPECT_TRUE(observer.trial_name().empty());
983 // The trial shouldn't be in the active set of trials.
984 FieldTrial::ActiveGroups active_groups;
985 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
986 EXPECT_TRUE(active_groups.empty());
988 // The trial shouldn't be listed in the |StatesToString()| result.
989 std::string states;
990 FieldTrialList::StatesToString(&states);
991 EXPECT_TRUE(states.empty());
995 } // namespace base