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"
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
);
34 Time last_year_time
= Time::NowFromSystemTime() - TimeDelta::FromDays(365);
35 Time::Exploded exploded
;
36 last_year_time
.LocalExplode(&exploded
);
40 // FieldTrialList::Observer implementation for testing.
41 class TestFieldTrialObserver
: public FieldTrialList::Observer
{
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
{
57 const std::string
& trial_name() const { return trial_name_
; }
58 const std::string
& group_name() const { return group_name_
; }
61 std::string trial_name_
;
62 std::string group_name_
;
64 DISALLOW_COPY_AND_ASSIGN(TestFieldTrialObserver
);
69 class FieldTrialTest
: public testing::Test
{
71 FieldTrialTest() : trial_list_(NULL
) {}
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.
118 default_always_true
[0] = i
;
120 default_always_false
[0] = i
;
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
;
144 int default_group_number
= -1;
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;
168 std::string name
= base::StringPrintf("FiftyFifty%d", ++counter
);
169 std::string default_group_name
= base::StringPrintf("Default FiftyFifty%d",
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
) {
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
) {
194 default_group_name
[0] = i
;
195 scoped_refptr
<FieldTrial
> trial
=
196 CreateFieldTrial(name
, 10, default_group_name
, NULL
);
197 int might_win
= trial
->AppendGroup("MightWin", 5);
199 if (trial
->group() == might_win
) {
200 true_event_seen
= true;
202 false_event_seen
= true;
204 if (false_event_seen
&& true_event_seen
)
205 return; // Successful test!!!
207 // Very surprising to get here. Probability should be around 1 in 2 ** 250.
208 // One of the following will fail.
209 EXPECT_TRUE(false_event_seen
);
210 EXPECT_TRUE(true_event_seen
);
213 TEST_F(FieldTrialTest
, OneWinner
) {
214 char name
[] = "Some name";
215 char default_group_name
[] = "Default some name";
218 int default_group_number
= -1;
219 scoped_refptr
<FieldTrial
> trial
=
220 CreateFieldTrial(name
, group_count
, default_group_name
, NULL
);
221 int winner_index(-2);
222 std::string winner_name
;
224 for (int i
= 1; i
<= group_count
; ++i
) {
225 int might_win
= trial
->AppendGroup(std::string(), 1);
227 // Because we keep appending groups, we want to see if the last group that
228 // was added has been assigned or not.
229 if (trial
->group_
== might_win
) {
230 EXPECT_EQ(-2, winner_index
);
231 winner_index
= might_win
;
232 StringAppendF(&winner_name
, "%d", might_win
);
233 EXPECT_EQ(winner_name
, trial
->group_name());
236 EXPECT_GE(winner_index
, 0);
237 // Since all groups cover the total probability, we should not have
238 // chosen the default group.
239 EXPECT_NE(trial
->group(), default_group_number
);
240 EXPECT_EQ(trial
->group(), winner_index
);
241 EXPECT_EQ(trial
->group_name(), winner_name
);
244 TEST_F(FieldTrialTest
, DisableProbability
) {
245 const std::string default_group_name
= "Default group";
246 const std::string loser
= "Loser";
247 const std::string name
= "Trial";
249 // Create a field trail that has expired.
250 int default_group_number
= -1;
251 FieldTrial
* trial
= FieldTrialList::FactoryGetFieldTrial(
252 name
, 1000000000, default_group_name
, GetLastYear(), 1, 1,
253 FieldTrial::SESSION_RANDOMIZED
,
254 &default_group_number
);
255 trial
->AppendGroup(loser
, 999999999); // 99.9999999% chance of being chosen.
257 // Because trial has expired, we should always be in the default group.
258 EXPECT_EQ(default_group_number
, trial
->group());
260 // And that default_group_name should ALWAYS win.
261 EXPECT_EQ(default_group_name
, trial
->group_name());
264 TEST_F(FieldTrialTest
, ActiveGroups
) {
265 std::string
no_group("No Group");
266 scoped_refptr
<FieldTrial
> trial
=
267 CreateFieldTrial(no_group
, 10, "Default", NULL
);
269 // There is no winner yet, so no NameGroupId should be returned.
270 FieldTrial::ActiveGroup active_group
;
271 EXPECT_FALSE(trial
->GetActiveGroup(&active_group
));
273 // Create a single winning group.
274 std::string
one_winner("One Winner");
275 trial
= CreateFieldTrial(one_winner
, 10, "Default", NULL
);
276 std::string
winner("Winner");
277 trial
->AppendGroup(winner
, 10);
278 EXPECT_FALSE(trial
->GetActiveGroup(&active_group
));
279 // Finalize the group selection by accessing the selected group.
281 EXPECT_TRUE(trial
->GetActiveGroup(&active_group
));
282 EXPECT_EQ(one_winner
, active_group
.trial_name
);
283 EXPECT_EQ(winner
, active_group
.group_name
);
285 std::string
multi_group("MultiGroup");
286 scoped_refptr
<FieldTrial
> multi_group_trial
=
287 CreateFieldTrial(multi_group
, 9, "Default", NULL
);
289 multi_group_trial
->AppendGroup("Me", 3);
290 multi_group_trial
->AppendGroup("You", 3);
291 multi_group_trial
->AppendGroup("Them", 3);
292 EXPECT_FALSE(multi_group_trial
->GetActiveGroup(&active_group
));
293 // Finalize the group selection by accessing the selected group.
294 multi_group_trial
->group();
295 EXPECT_TRUE(multi_group_trial
->GetActiveGroup(&active_group
));
296 EXPECT_EQ(multi_group
, active_group
.trial_name
);
297 EXPECT_EQ(multi_group_trial
->group_name(), active_group
.group_name
);
299 // Now check if the list is built properly...
300 FieldTrial::ActiveGroups active_groups
;
301 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
302 EXPECT_EQ(2U, active_groups
.size());
303 for (size_t i
= 0; i
< active_groups
.size(); ++i
) {
304 // Order is not guaranteed, so check all values.
305 EXPECT_NE(no_group
, active_groups
[i
].trial_name
);
306 EXPECT_TRUE(one_winner
!= active_groups
[i
].trial_name
||
307 winner
== active_groups
[i
].group_name
);
308 EXPECT_TRUE(multi_group
!= active_groups
[i
].trial_name
||
309 multi_group_trial
->group_name() == active_groups
[i
].group_name
);
313 TEST_F(FieldTrialTest
, ActiveGroupsNotFinalized
) {
314 const char kTrialName
[] = "TestTrial";
315 const char kSecondaryGroupName
[] = "SecondaryGroup";
317 int default_group
= -1;
318 scoped_refptr
<FieldTrial
> trial
=
319 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
320 const int secondary_group
= trial
->AppendGroup(kSecondaryGroupName
, 50);
322 // Before |group()| is called, |GetActiveGroup()| should return false.
323 FieldTrial::ActiveGroup active_group
;
324 EXPECT_FALSE(trial
->GetActiveGroup(&active_group
));
326 // |GetActiveFieldTrialGroups()| should also not include the trial.
327 FieldTrial::ActiveGroups active_groups
;
328 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
329 EXPECT_TRUE(active_groups
.empty());
331 // After |group()| has been called, both APIs should succeed.
332 const int chosen_group
= trial
->group();
333 EXPECT_TRUE(chosen_group
== default_group
|| chosen_group
== secondary_group
);
335 EXPECT_TRUE(trial
->GetActiveGroup(&active_group
));
336 EXPECT_EQ(kTrialName
, active_group
.trial_name
);
337 if (chosen_group
== default_group
)
338 EXPECT_EQ(kDefaultGroupName
, active_group
.group_name
);
340 EXPECT_EQ(kSecondaryGroupName
, active_group
.group_name
);
342 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
343 ASSERT_EQ(1U, active_groups
.size());
344 EXPECT_EQ(kTrialName
, active_groups
[0].trial_name
);
345 EXPECT_EQ(active_group
.group_name
, active_groups
[0].group_name
);
348 TEST_F(FieldTrialTest
, Save
) {
349 std::string save_string
;
351 scoped_refptr
<FieldTrial
> trial
=
352 CreateFieldTrial("Some name", 10, "Default some name", NULL
);
353 // There is no winner yet, so no textual group name is associated with trial.
354 // In this case, the trial should not be included.
355 EXPECT_EQ("", trial
->group_name_internal());
356 FieldTrialList::StatesToString(&save_string
);
357 EXPECT_EQ("", save_string
);
360 // Create a winning group.
361 trial
->AppendGroup("Winner", 10);
362 // Finalize the group selection by accessing the selected group.
364 FieldTrialList::StatesToString(&save_string
);
365 EXPECT_EQ("Some name/Winner/", save_string
);
368 // Create a second trial and winning group.
369 scoped_refptr
<FieldTrial
> trial2
=
370 CreateFieldTrial("xxx", 10, "Default xxx", NULL
);
371 trial2
->AppendGroup("yyyy", 10);
372 // Finalize the group selection by accessing the selected group.
375 FieldTrialList::StatesToString(&save_string
);
376 // We assume names are alphabetized... though this is not critical.
377 EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string
);
380 // Create a third trial with only the default group.
381 scoped_refptr
<FieldTrial
> trial3
=
382 CreateFieldTrial("zzz", 10, "default", NULL
);
383 // Finalize the group selection by accessing the selected group.
386 FieldTrialList::StatesToString(&save_string
);
387 EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string
);
390 TEST_F(FieldTrialTest
, Restore
) {
391 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
392 ASSERT_FALSE(FieldTrialList::TrialExists("xxx"));
394 FieldTrialList::CreateTrialsFromString("Some_name/Winner/xxx/yyyy/",
395 FieldTrialList::DONT_ACTIVATE_TRIALS
,
396 std::set
<std::string
>());
398 FieldTrial
* trial
= FieldTrialList::Find("Some_name");
399 ASSERT_NE(static_cast<FieldTrial
*>(NULL
), trial
);
400 EXPECT_EQ("Winner", trial
->group_name());
401 EXPECT_EQ("Some_name", trial
->trial_name());
403 trial
= FieldTrialList::Find("xxx");
404 ASSERT_NE(static_cast<FieldTrial
*>(NULL
), trial
);
405 EXPECT_EQ("yyyy", trial
->group_name());
406 EXPECT_EQ("xxx", trial
->trial_name());
409 TEST_F(FieldTrialTest
, BogusRestore
) {
410 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
411 "MissingSlash", FieldTrialList::DONT_ACTIVATE_TRIALS
,
412 std::set
<std::string
>()));
413 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
414 "MissingGroupName/", FieldTrialList::DONT_ACTIVATE_TRIALS
,
415 std::set
<std::string
>()));
416 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
417 "MissingFinalSlash/gname", FieldTrialList::DONT_ACTIVATE_TRIALS
,
418 std::set
<std::string
>()));
419 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
420 "noname, only group/", FieldTrialList::DONT_ACTIVATE_TRIALS
,
421 std::set
<std::string
>()));
424 TEST_F(FieldTrialTest
, DuplicateRestore
) {
425 scoped_refptr
<FieldTrial
> trial
=
426 CreateFieldTrial("Some name", 10, "Default", NULL
);
427 trial
->AppendGroup("Winner", 10);
428 // Finalize the group selection by accessing the selected group.
430 std::string save_string
;
431 FieldTrialList::StatesToString(&save_string
);
432 EXPECT_EQ("Some name/Winner/", save_string
);
434 // It is OK if we redundantly specify a winner.
435 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(
436 save_string
, FieldTrialList::DONT_ACTIVATE_TRIALS
,
437 std::set
<std::string
>()));
439 // But it is an error to try to change to a different winner.
440 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
441 "Some name/Loser/", FieldTrialList::DONT_ACTIVATE_TRIALS
,
442 std::set
<std::string
>()));
445 TEST_F(FieldTrialTest
, CreateTrialsFromStringActive
) {
446 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
447 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
448 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
449 "Abc/def/Xyz/zyx/", FieldTrialList::ACTIVATE_TRIALS
,
450 std::set
<std::string
>()));
452 FieldTrial::ActiveGroups active_groups
;
453 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
454 ASSERT_EQ(2U, active_groups
.size());
455 EXPECT_EQ("Abc", active_groups
[0].trial_name
);
456 EXPECT_EQ("def", active_groups
[0].group_name
);
457 EXPECT_EQ("Xyz", active_groups
[1].trial_name
);
458 EXPECT_EQ("zyx", active_groups
[1].group_name
);
461 TEST_F(FieldTrialTest
, CreateTrialsFromStringNotActive
) {
462 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
463 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
464 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
465 "Abc/def/Xyz/zyx/", FieldTrialList::DONT_ACTIVATE_TRIALS
,
466 std::set
<std::string
>()));
468 FieldTrial::ActiveGroups active_groups
;
469 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
470 ASSERT_TRUE(active_groups
.empty());
472 // Check that the values still get returned and querying them activates them.
473 EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
474 EXPECT_EQ("zyx", FieldTrialList::FindFullName("Xyz"));
476 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
477 ASSERT_EQ(2U, active_groups
.size());
478 EXPECT_EQ("Abc", active_groups
[0].trial_name
);
479 EXPECT_EQ("def", active_groups
[0].group_name
);
480 EXPECT_EQ("Xyz", active_groups
[1].trial_name
);
481 EXPECT_EQ("zyx", active_groups
[1].group_name
);
484 TEST_F(FieldTrialTest
, CreateTrialsFromStringActiveObserver
) {
485 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
487 TestFieldTrialObserver observer
;
488 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
489 "Abc/def/", FieldTrialList::ACTIVATE_TRIALS
, std::set
<std::string
>()));
491 RunLoop().RunUntilIdle();
492 EXPECT_EQ("Abc", observer
.trial_name());
493 EXPECT_EQ("def", observer
.group_name());
496 TEST_F(FieldTrialTest
, CreateTrialsFromStringNotActiveObserver
) {
497 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
499 TestFieldTrialObserver observer
;
500 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString(
501 "Abc/def/", FieldTrialList::DONT_ACTIVATE_TRIALS
,
502 std::set
<std::string
>()));
503 RunLoop().RunUntilIdle();
504 // Observer shouldn't be notified.
505 EXPECT_TRUE(observer
.trial_name().empty());
507 // Check that the values still get returned and querying them activates them.
508 EXPECT_EQ("def", FieldTrialList::FindFullName("Abc"));
510 RunLoop().RunUntilIdle();
511 EXPECT_EQ("Abc", observer
.trial_name());
512 EXPECT_EQ("def", observer
.group_name());
515 TEST_F(FieldTrialTest
, CreateTrialsFromStringWithIgnoredFieldTrials
) {
516 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
517 ASSERT_FALSE(FieldTrialList::TrialExists("Foo"));
518 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
519 ASSERT_FALSE(FieldTrialList::TrialExists("Bar"));
520 ASSERT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
522 std::set
<std::string
> ignored_trial_names
;
523 ignored_trial_names
.insert("Unaccepted1");
524 ignored_trial_names
.insert("Unaccepted2");
525 ignored_trial_names
.insert("Unaccepted3");
527 FieldTrialList::CreateTrialsFromString(
528 "Unaccepted1/Unaccepted1_name/"
530 "Unaccepted2/Unaccepted2_name/"
532 "Unaccepted3/Unaccepted3_name/",
533 FieldTrialList::DONT_ACTIVATE_TRIALS
,
534 ignored_trial_names
);
536 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted1"));
537 EXPECT_TRUE(FieldTrialList::TrialExists("Foo"));
538 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted2"));
539 EXPECT_TRUE(FieldTrialList::TrialExists("Bar"));
540 EXPECT_FALSE(FieldTrialList::TrialExists("Unaccepted3"));
542 FieldTrial::ActiveGroups active_groups
;
543 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
544 EXPECT_TRUE(active_groups
.empty());
546 FieldTrial
* trial
= FieldTrialList::Find("Foo");
547 ASSERT_NE(static_cast<FieldTrial
*>(NULL
), trial
);
548 EXPECT_EQ("Foo", trial
->trial_name());
549 EXPECT_EQ("Foo_name", trial
->group_name());
551 trial
= FieldTrialList::Find("Bar");
552 ASSERT_NE(static_cast<FieldTrial
*>(NULL
), trial
);
553 EXPECT_EQ("Bar", trial
->trial_name());
554 EXPECT_EQ("Bar_name", trial
->group_name());
557 TEST_F(FieldTrialTest
, CreateFieldTrial
) {
558 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
560 FieldTrialList::CreateFieldTrial("Some_name", "Winner");
562 FieldTrial
* trial
= FieldTrialList::Find("Some_name");
563 ASSERT_NE(static_cast<FieldTrial
*>(NULL
), trial
);
564 EXPECT_EQ("Winner", trial
->group_name());
565 EXPECT_EQ("Some_name", trial
->trial_name());
568 TEST_F(FieldTrialTest
, CreateFieldTrialIsNotActive
) {
569 const char kTrialName
[] = "CreateFieldTrialIsActiveTrial";
570 const char kWinnerGroup
[] = "Winner";
571 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
572 FieldTrialList::CreateFieldTrial(kTrialName
, kWinnerGroup
);
574 FieldTrial::ActiveGroups active_groups
;
575 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
576 EXPECT_TRUE(active_groups
.empty());
579 TEST_F(FieldTrialTest
, DuplicateFieldTrial
) {
580 scoped_refptr
<FieldTrial
> trial
=
581 CreateFieldTrial("Some_name", 10, "Default", NULL
);
582 trial
->AppendGroup("Winner", 10);
584 // It is OK if we redundantly specify a winner.
585 FieldTrial
* trial1
= FieldTrialList::CreateFieldTrial("Some_name", "Winner");
586 EXPECT_TRUE(trial1
!= NULL
);
588 // But it is an error to try to change to a different winner.
589 FieldTrial
* trial2
= FieldTrialList::CreateFieldTrial("Some_name", "Loser");
590 EXPECT_TRUE(trial2
== NULL
);
593 TEST_F(FieldTrialTest
, DisableImmediately
) {
594 int default_group_number
= -1;
595 scoped_refptr
<FieldTrial
> trial
=
596 CreateFieldTrial("trial", 100, "default", &default_group_number
);
598 ASSERT_EQ("default", trial
->group_name());
599 ASSERT_EQ(default_group_number
, trial
->group());
602 TEST_F(FieldTrialTest
, DisableAfterInitialization
) {
603 scoped_refptr
<FieldTrial
> trial
=
604 CreateFieldTrial("trial", 100, "default", NULL
);
605 trial
->AppendGroup("non_default", 100);
607 ASSERT_EQ("default", trial
->group_name());
610 TEST_F(FieldTrialTest
, ForcedFieldTrials
) {
611 // Validate we keep the forced choice.
612 FieldTrial
* forced_trial
= FieldTrialList::CreateFieldTrial("Use the",
614 EXPECT_STREQ("Force", forced_trial
->group_name().c_str());
616 int default_group_number
= -1;
617 scoped_refptr
<FieldTrial
> factory_trial
=
618 CreateFieldTrial("Use the", 1000, "default", &default_group_number
);
619 EXPECT_EQ(factory_trial
.get(), forced_trial
);
621 int chosen_group
= factory_trial
->AppendGroup("Force", 100);
622 EXPECT_EQ(chosen_group
, factory_trial
->group());
623 int not_chosen_group
= factory_trial
->AppendGroup("Dark Side", 100);
624 EXPECT_NE(chosen_group
, not_chosen_group
);
626 // Since we didn't force the default group, we should not be returned the
627 // chosen group as the default group.
628 EXPECT_NE(default_group_number
, chosen_group
);
629 int new_group
= factory_trial
->AppendGroup("Duck Tape", 800);
630 EXPECT_NE(chosen_group
, new_group
);
631 // The new group should not be the default group either.
632 EXPECT_NE(default_group_number
, new_group
);
635 TEST_F(FieldTrialTest
, ForcedFieldTrialsDefaultGroup
) {
636 // Forcing the default should use the proper group ID.
637 FieldTrial
* forced_trial
= FieldTrialList::CreateFieldTrial("Trial Name",
639 int default_group_number
= -1;
640 scoped_refptr
<FieldTrial
> factory_trial
=
641 CreateFieldTrial("Trial Name", 1000, "Default", &default_group_number
);
642 EXPECT_EQ(forced_trial
, factory_trial
.get());
644 int other_group
= factory_trial
->AppendGroup("Not Default", 100);
645 EXPECT_STREQ("Default", factory_trial
->group_name().c_str());
646 EXPECT_EQ(default_group_number
, factory_trial
->group());
647 EXPECT_NE(other_group
, factory_trial
->group());
649 int new_other_group
= factory_trial
->AppendGroup("Not Default Either", 800);
650 EXPECT_NE(new_other_group
, factory_trial
->group());
653 TEST_F(FieldTrialTest
, SetForced
) {
654 // Start by setting a trial for which we ensure a winner...
655 int default_group_number
= -1;
656 scoped_refptr
<FieldTrial
> forced_trial
=
657 CreateFieldTrial("Use the", 1, "default", &default_group_number
);
658 EXPECT_EQ(forced_trial
, forced_trial
);
660 int forced_group
= forced_trial
->AppendGroup("Force", 1);
661 EXPECT_EQ(forced_group
, forced_trial
->group());
664 forced_trial
->SetForced();
666 // Now try to set it up differently as a hard coded registration would.
667 scoped_refptr
<FieldTrial
> hard_coded_trial
=
668 CreateFieldTrial("Use the", 1, "default", &default_group_number
);
669 EXPECT_EQ(hard_coded_trial
, forced_trial
);
671 int would_lose_group
= hard_coded_trial
->AppendGroup("Force", 0);
672 EXPECT_EQ(forced_group
, hard_coded_trial
->group());
673 EXPECT_EQ(forced_group
, would_lose_group
);
675 // Same thing if we would have done it to win again.
676 scoped_refptr
<FieldTrial
> other_hard_coded_trial
=
677 CreateFieldTrial("Use the", 1, "default", &default_group_number
);
678 EXPECT_EQ(other_hard_coded_trial
, forced_trial
);
680 int would_win_group
= other_hard_coded_trial
->AppendGroup("Force", 1);
681 EXPECT_EQ(forced_group
, other_hard_coded_trial
->group());
682 EXPECT_EQ(forced_group
, would_win_group
);
685 TEST_F(FieldTrialTest
, SetForcedDefaultOnly
) {
686 const char kTrialName
[] = "SetForcedDefaultOnly";
687 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
689 int default_group
= -1;
690 scoped_refptr
<FieldTrial
> trial
=
691 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
694 trial
= CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, NULL
);
695 EXPECT_EQ(default_group
, trial
->group());
696 EXPECT_EQ(kDefaultGroupName
, trial
->group_name());
699 TEST_F(FieldTrialTest
, SetForcedDefaultWithExtraGroup
) {
700 const char kTrialName
[] = "SetForcedDefaultWithExtraGroup";
701 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
703 int default_group
= -1;
704 scoped_refptr
<FieldTrial
> trial
=
705 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
708 trial
= CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, NULL
);
709 const int extra_group
= trial
->AppendGroup("Extra", 100);
710 EXPECT_EQ(default_group
, trial
->group());
711 EXPECT_NE(extra_group
, trial
->group());
712 EXPECT_EQ(kDefaultGroupName
, trial
->group_name());
715 TEST_F(FieldTrialTest
, SetForcedTurnFeatureOn
) {
716 const char kTrialName
[] = "SetForcedTurnFeatureOn";
717 const char kExtraGroupName
[] = "Extra";
718 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
720 // Simulate a server-side (forced) config that turns the feature on when the
721 // original hard-coded config had it disabled.
722 scoped_refptr
<FieldTrial
> forced_trial
=
723 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, NULL
);
724 forced_trial
->AppendGroup(kExtraGroupName
, 100);
725 forced_trial
->SetForced();
727 int default_group
= -1;
728 scoped_refptr
<FieldTrial
> client_trial
=
729 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
730 const int extra_group
= client_trial
->AppendGroup(kExtraGroupName
, 0);
731 EXPECT_NE(default_group
, extra_group
);
733 EXPECT_FALSE(client_trial
->group_reported_
);
734 EXPECT_EQ(extra_group
, client_trial
->group());
735 EXPECT_TRUE(client_trial
->group_reported_
);
736 EXPECT_EQ(kExtraGroupName
, client_trial
->group_name());
739 TEST_F(FieldTrialTest
, SetForcedTurnFeatureOff
) {
740 const char kTrialName
[] = "SetForcedTurnFeatureOff";
741 const char kExtraGroupName
[] = "Extra";
742 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
744 // Simulate a server-side (forced) config that turns the feature off when the
745 // original hard-coded config had it enabled.
746 scoped_refptr
<FieldTrial
> forced_trial
=
747 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, NULL
);
748 forced_trial
->AppendGroup(kExtraGroupName
, 0);
749 forced_trial
->SetForced();
751 int default_group
= -1;
752 scoped_refptr
<FieldTrial
> client_trial
=
753 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
754 const int extra_group
= client_trial
->AppendGroup(kExtraGroupName
, 100);
755 EXPECT_NE(default_group
, extra_group
);
757 EXPECT_FALSE(client_trial
->group_reported_
);
758 EXPECT_EQ(default_group
, client_trial
->group());
759 EXPECT_TRUE(client_trial
->group_reported_
);
760 EXPECT_EQ(kDefaultGroupName
, client_trial
->group_name());
763 TEST_F(FieldTrialTest
, SetForcedChangeDefault_Default
) {
764 const char kTrialName
[] = "SetForcedDefaultGroupChange";
765 const char kGroupAName
[] = "A";
766 const char kGroupBName
[] = "B";
767 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
769 // Simulate a server-side (forced) config that switches which group is default
770 // and ensures that the non-forced code receives the correct group numbers.
771 scoped_refptr
<FieldTrial
> forced_trial
=
772 CreateFieldTrial(kTrialName
, 100, kGroupAName
, NULL
);
773 forced_trial
->AppendGroup(kGroupBName
, 100);
774 forced_trial
->SetForced();
776 int default_group
= -1;
777 scoped_refptr
<FieldTrial
> client_trial
=
778 CreateFieldTrial(kTrialName
, 100, kGroupBName
, &default_group
);
779 const int extra_group
= client_trial
->AppendGroup(kGroupAName
, 50);
780 EXPECT_NE(default_group
, extra_group
);
782 EXPECT_FALSE(client_trial
->group_reported_
);
783 EXPECT_EQ(default_group
, client_trial
->group());
784 EXPECT_TRUE(client_trial
->group_reported_
);
785 EXPECT_EQ(kGroupBName
, client_trial
->group_name());
788 TEST_F(FieldTrialTest
, SetForcedChangeDefault_NonDefault
) {
789 const char kTrialName
[] = "SetForcedDefaultGroupChange";
790 const char kGroupAName
[] = "A";
791 const char kGroupBName
[] = "B";
792 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
794 // Simulate a server-side (forced) config that switches which group is default
795 // and ensures that the non-forced code receives the correct group numbers.
796 scoped_refptr
<FieldTrial
> forced_trial
=
797 CreateFieldTrial(kTrialName
, 100, kGroupAName
, NULL
);
798 forced_trial
->AppendGroup(kGroupBName
, 0);
799 forced_trial
->SetForced();
801 int default_group
= -1;
802 scoped_refptr
<FieldTrial
> client_trial
=
803 CreateFieldTrial(kTrialName
, 100, kGroupBName
, &default_group
);
804 const int extra_group
= client_trial
->AppendGroup(kGroupAName
, 50);
805 EXPECT_NE(default_group
, extra_group
);
807 EXPECT_FALSE(client_trial
->group_reported_
);
808 EXPECT_EQ(extra_group
, client_trial
->group());
809 EXPECT_TRUE(client_trial
->group_reported_
);
810 EXPECT_EQ(kGroupAName
, client_trial
->group_name());
813 TEST_F(FieldTrialTest
, Observe
) {
814 const char kTrialName
[] = "TrialToObserve1";
815 const char kSecondaryGroupName
[] = "SecondaryGroup";
817 TestFieldTrialObserver observer
;
818 int default_group
= -1;
819 scoped_refptr
<FieldTrial
> trial
=
820 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
821 const int secondary_group
= trial
->AppendGroup(kSecondaryGroupName
, 50);
822 const int chosen_group
= trial
->group();
823 EXPECT_TRUE(chosen_group
== default_group
|| chosen_group
== secondary_group
);
825 RunLoop().RunUntilIdle();
826 EXPECT_EQ(kTrialName
, observer
.trial_name());
827 if (chosen_group
== default_group
)
828 EXPECT_EQ(kDefaultGroupName
, observer
.group_name());
830 EXPECT_EQ(kSecondaryGroupName
, observer
.group_name());
833 TEST_F(FieldTrialTest
, ObserveDisabled
) {
834 const char kTrialName
[] = "TrialToObserve2";
836 TestFieldTrialObserver observer
;
837 int default_group
= -1;
838 scoped_refptr
<FieldTrial
> trial
=
839 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
840 trial
->AppendGroup("A", 25);
841 trial
->AppendGroup("B", 25);
842 trial
->AppendGroup("C", 25);
845 // Observer shouldn't be notified of a disabled trial.
846 RunLoop().RunUntilIdle();
847 EXPECT_TRUE(observer
.trial_name().empty());
848 EXPECT_TRUE(observer
.group_name().empty());
850 // Observer shouldn't be notified even after a |group()| call.
851 EXPECT_EQ(default_group
, trial
->group());
852 RunLoop().RunUntilIdle();
853 EXPECT_TRUE(observer
.trial_name().empty());
854 EXPECT_TRUE(observer
.group_name().empty());
857 TEST_F(FieldTrialTest
, ObserveForcedDisabled
) {
858 const char kTrialName
[] = "TrialToObserve3";
860 TestFieldTrialObserver observer
;
861 int default_group
= -1;
862 scoped_refptr
<FieldTrial
> trial
=
863 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, &default_group
);
864 trial
->AppendGroup("A", 25);
865 trial
->AppendGroup("B", 25);
866 trial
->AppendGroup("C", 25);
870 // Observer shouldn't be notified of a disabled trial, even when forced.
871 RunLoop().RunUntilIdle();
872 EXPECT_TRUE(observer
.trial_name().empty());
873 EXPECT_TRUE(observer
.group_name().empty());
875 // Observer shouldn't be notified even after a |group()| call.
876 EXPECT_EQ(default_group
, trial
->group());
877 RunLoop().RunUntilIdle();
878 EXPECT_TRUE(observer
.trial_name().empty());
879 EXPECT_TRUE(observer
.group_name().empty());
882 TEST_F(FieldTrialTest
, DisabledTrialNotActive
) {
883 const char kTrialName
[] = "DisabledTrial";
884 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
886 scoped_refptr
<FieldTrial
> trial
=
887 CreateFieldTrial(kTrialName
, 100, kDefaultGroupName
, NULL
);
888 trial
->AppendGroup("X", 50);
891 // Ensure the trial is not listed as active.
892 FieldTrial::ActiveGroups active_groups
;
893 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
894 EXPECT_TRUE(active_groups
.empty());
896 // Ensure the trial is not listed in the |StatesToString()| result.
898 FieldTrialList::StatesToString(&states
);
899 EXPECT_TRUE(states
.empty());
902 TEST_F(FieldTrialTest
, ExpirationYearNotExpired
) {
903 const char kTrialName
[] = "NotExpired";
904 const char kGroupName
[] = "Group2";
905 const int kProbability
= 100;
906 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
908 scoped_refptr
<FieldTrial
> trial
=
909 CreateFieldTrial(kTrialName
, kProbability
, kDefaultGroupName
, NULL
);
910 trial
->AppendGroup(kGroupName
, kProbability
);
911 EXPECT_EQ(kGroupName
, trial
->group_name());
914 TEST_F(FieldTrialTest
, FloatBoundariesGiveEqualGroupSizes
) {
915 const int kBucketCount
= 100;
917 // Try each boundary value |i / 100.0| as the entropy value.
918 for (int i
= 0; i
< kBucketCount
; ++i
) {
919 const double entropy
= i
/ static_cast<double>(kBucketCount
);
921 scoped_refptr
<base::FieldTrial
> trial(
922 new base::FieldTrial("test", kBucketCount
, "default", entropy
));
923 for (int j
= 0; j
< kBucketCount
; ++j
)
924 trial
->AppendGroup(base::StringPrintf("%d", j
), 1);
926 EXPECT_EQ(base::StringPrintf("%d", i
), trial
->group_name());
930 TEST_F(FieldTrialTest
, DoesNotSurpassTotalProbability
) {
931 const double kEntropyValue
= 1.0 - 1e-9;
932 ASSERT_LT(kEntropyValue
, 1.0);
934 scoped_refptr
<base::FieldTrial
> trial(
935 new base::FieldTrial("test", 2, "default", kEntropyValue
));
936 trial
->AppendGroup("1", 1);
937 trial
->AppendGroup("2", 1);
939 EXPECT_EQ("2", trial
->group_name());
942 TEST_F(FieldTrialTest
, CreateSimulatedFieldTrial
) {
943 const char kTrialName
[] = "CreateSimulatedFieldTrial";
944 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName
));
946 // Different cases to test, e.g. default vs. non default group being chosen.
948 double entropy_value
;
949 const char* expected_group
;
953 { 0.95, kDefaultGroupName
},
956 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_cases
); ++i
) {
957 TestFieldTrialObserver observer
;
958 scoped_refptr
<FieldTrial
> trial(
959 FieldTrial::CreateSimulatedFieldTrial(kTrialName
, 100, kDefaultGroupName
,
960 test_cases
[i
].entropy_value
));
961 trial
->AppendGroup("A", 80);
962 trial
->AppendGroup("B", 10);
963 EXPECT_EQ(test_cases
[i
].expected_group
, trial
->group_name());
965 // Field trial shouldn't have been registered with the list.
966 EXPECT_FALSE(FieldTrialList::TrialExists(kTrialName
));
967 EXPECT_EQ(0u, FieldTrialList::GetFieldTrialCount());
969 // Observer shouldn't have been notified.
970 RunLoop().RunUntilIdle();
971 EXPECT_TRUE(observer
.trial_name().empty());
973 // The trial shouldn't be in the active set of trials.
974 FieldTrial::ActiveGroups active_groups
;
975 FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
976 EXPECT_TRUE(active_groups
.empty());
978 // The trial shouldn't be listed in the |StatesToString()| result.
980 FieldTrialList::StatesToString(&states
);
981 EXPECT_TRUE(states
.empty());