Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / base / metrics / field_trial_unittest.cc
blobbb2c39f317b698c14a01ce7081df8203d3ca20fe
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.h"
8 #include "base/rand_util.h"
9 #include "base/run_loop.h"
10 #include "base/stringprintf.h"
11 #include "base/strings/string_number_conversions.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 // FieldTrialList::Observer implementation for testing.
22 class TestFieldTrialObserver : public FieldTrialList::Observer {
23 public:
24 TestFieldTrialObserver() {
25 FieldTrialList::AddObserver(this);
28 virtual ~TestFieldTrialObserver() {
29 FieldTrialList::RemoveObserver(this);
32 virtual void OnFieldTrialGroupFinalized(const std::string& trial,
33 const std::string& group) OVERRIDE {
34 trial_name_ = trial;
35 group_name_ = group;
38 const std::string& trial_name() const { return trial_name_; }
39 const std::string& group_name() const { return group_name_; }
41 private:
42 std::string trial_name_;
43 std::string group_name_;
45 DISALLOW_COPY_AND_ASSIGN(TestFieldTrialObserver);
48 } // namespace
50 class FieldTrialTest : public testing::Test {
51 public:
52 FieldTrialTest() : trial_list_(NULL) {
53 Time now = Time::NowFromSystemTime();
54 TimeDelta one_year = TimeDelta::FromDays(365);
55 Time::Exploded exploded;
57 Time next_year_time = now + one_year;
58 next_year_time.LocalExplode(&exploded);
59 next_year_ = exploded.year;
61 Time last_year_time = now - one_year;
62 last_year_time.LocalExplode(&exploded);
63 last_year_ = exploded.year;
66 protected:
67 int next_year_;
68 int last_year_;
69 MessageLoop message_loop_;
71 private:
72 FieldTrialList trial_list_;
75 // Test registration, and also check that destructors are called for trials
76 // (and that Valgrind doesn't catch us leaking).
77 TEST_F(FieldTrialTest, Registration) {
78 const char* name1 = "name 1 test";
79 const char* name2 = "name 2 test";
80 EXPECT_FALSE(FieldTrialList::Find(name1));
81 EXPECT_FALSE(FieldTrialList::Find(name2));
83 FieldTrial* trial1 = FieldTrialList::FactoryGetFieldTrial(
84 name1, 10, "default name 1 test", next_year_, 12, 31, NULL);
85 EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_);
86 EXPECT_EQ(name1, trial1->trial_name());
87 EXPECT_EQ("", trial1->group_name_internal());
89 trial1->AppendGroup(std::string(), 7);
91 EXPECT_EQ(trial1, FieldTrialList::Find(name1));
92 EXPECT_FALSE(FieldTrialList::Find(name2));
94 FieldTrial* trial2 = FieldTrialList::FactoryGetFieldTrial(
95 name2, 10, "default name 2 test", next_year_, 12, 31, NULL);
96 EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_);
97 EXPECT_EQ(name2, trial2->trial_name());
98 EXPECT_EQ("", trial2->group_name_internal());
100 trial2->AppendGroup("a first group", 7);
102 EXPECT_EQ(trial1, FieldTrialList::Find(name1));
103 EXPECT_EQ(trial2, FieldTrialList::Find(name2));
104 // Note: FieldTrialList should delete the objects at shutdown.
107 TEST_F(FieldTrialTest, AbsoluteProbabilities) {
108 char always_true[] = " always true";
109 char default_always_true[] = " default always true";
110 char always_false[] = " always false";
111 char default_always_false[] = " default always false";
112 for (int i = 1; i < 250; ++i) {
113 // Try lots of names, by changing the first character of the name.
114 always_true[0] = i;
115 default_always_true[0] = i;
116 always_false[0] = i;
117 default_always_false[0] = i;
119 FieldTrial* trial_true = FieldTrialList::FactoryGetFieldTrial(
120 always_true, 10, default_always_true, next_year_, 12, 31, NULL);
121 const std::string winner = "TheWinner";
122 int winner_group = trial_true->AppendGroup(winner, 10);
124 EXPECT_EQ(winner_group, trial_true->group());
125 EXPECT_EQ(winner, trial_true->group_name());
127 FieldTrial* trial_false = FieldTrialList::FactoryGetFieldTrial(
128 always_false, 10, default_always_false, next_year_, 12, 31, NULL);
129 int loser_group = trial_false->AppendGroup("ALoser", 0);
131 EXPECT_NE(loser_group, trial_false->group());
135 TEST_F(FieldTrialTest, RemainingProbability) {
136 // First create a test that hasn't had a winner yet.
137 const std::string winner = "Winner";
138 const std::string loser = "Loser";
139 scoped_refptr<FieldTrial> trial;
140 int counter = 0;
141 int default_group_number = -1;
142 do {
143 std::string name = StringPrintf("trial%d", ++counter);
144 trial = FieldTrialList::FactoryGetFieldTrial(
145 name, 10, winner, next_year_, 12, 31, &default_group_number);
146 trial->AppendGroup(loser, 5); // 50% chance of not being chosen.
147 // If a group is not assigned, group_ will be kNotFinalized.
148 } while (trial->group_ != FieldTrial::kNotFinalized);
150 // And that 'default' group (winner) should always win.
151 EXPECT_EQ(default_group_number, trial->group());
153 // And that winner should ALWAYS win.
154 EXPECT_EQ(winner, trial->group_name());
157 TEST_F(FieldTrialTest, FiftyFiftyProbability) {
158 // Check that even with small divisors, we have the proper probabilities, and
159 // all outcomes are possible. Since this is a 50-50 test, it should get both
160 // outcomes in a few tries, but we'll try no more than 100 times (and be flaky
161 // with probability around 1 in 2^99).
162 bool first_winner = false;
163 bool second_winner = false;
164 int counter = 0;
165 do {
166 std::string name = base::StringPrintf("FiftyFifty%d", ++counter);
167 std::string default_group_name = base::StringPrintf("Default FiftyFifty%d",
168 ++counter);
169 scoped_refptr<FieldTrial> trial(FieldTrialList::FactoryGetFieldTrial(
170 name, 2, default_group_name, next_year_, 12, 31, NULL));
171 trial->AppendGroup("first", 1); // 50% chance of being chosen.
172 // If group_ is kNotFinalized, then a group assignement hasn't been done.
173 if (trial->group_ != FieldTrial::kNotFinalized) {
174 first_winner = true;
175 continue;
177 trial->AppendGroup("second", 1); // Always chosen at this point.
178 EXPECT_NE(FieldTrial::kNotFinalized, trial->group());
179 second_winner = true;
180 } while ((!second_winner || !first_winner) && counter < 100);
181 EXPECT_TRUE(second_winner);
182 EXPECT_TRUE(first_winner);
185 TEST_F(FieldTrialTest, MiddleProbabilities) {
186 char name[] = " same name";
187 char default_group_name[] = " default same name";
188 bool false_event_seen = false;
189 bool true_event_seen = false;
190 for (int i = 1; i < 250; ++i) {
191 name[0] = i;
192 default_group_name[0] = i;
193 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
194 name, 10, default_group_name, next_year_, 12, 31, NULL);
195 int might_win = trial->AppendGroup("MightWin", 5);
197 if (trial->group() == might_win) {
198 true_event_seen = true;
199 } else {
200 false_event_seen = true;
202 if (false_event_seen && true_event_seen)
203 return; // Successful test!!!
205 // Very surprising to get here. Probability should be around 1 in 2 ** 250.
206 // One of the following will fail.
207 EXPECT_TRUE(false_event_seen);
208 EXPECT_TRUE(true_event_seen);
211 TEST_F(FieldTrialTest, OneWinner) {
212 char name[] = "Some name";
213 char default_group_name[] = "Default some name";
214 int group_count(10);
216 int default_group_number = -1;
217 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
218 name, group_count, default_group_name, next_year_, 12, 31,
219 &default_group_number);
220 int winner_index(-2);
221 std::string winner_name;
223 for (int i = 1; i <= group_count; ++i) {
224 int might_win = trial->AppendGroup(std::string(), 1);
226 // Because we keep appending groups, we want to see if the last group that
227 // was added has been assigned or not.
228 if (trial->group_ == might_win) {
229 EXPECT_EQ(-2, winner_index);
230 winner_index = might_win;
231 StringAppendF(&winner_name, "%d", might_win);
232 EXPECT_EQ(winner_name, trial->group_name());
235 EXPECT_GE(winner_index, 0);
236 // Since all groups cover the total probability, we should not have
237 // chosen the default group.
238 EXPECT_NE(trial->group(), default_group_number);
239 EXPECT_EQ(trial->group(), winner_index);
240 EXPECT_EQ(trial->group_name(), winner_name);
243 TEST_F(FieldTrialTest, DisableProbability) {
244 const std::string default_group_name = "Default group";
245 const std::string loser = "Loser";
246 const std::string name = "Trial";
248 // Create a field trail that has expired.
249 int default_group_number = -1;
250 scoped_refptr<FieldTrial> trial;
251 trial = FieldTrialList::FactoryGetFieldTrial(
252 name, 1000000000, default_group_name, last_year_, 1, 1,
253 &default_group_number);
254 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen.
256 // Because trial has expired, we should always be in the default group.
257 EXPECT_EQ(default_group_number, trial->group());
259 // And that default_group_name should ALWAYS win.
260 EXPECT_EQ(default_group_name, trial->group_name());
263 TEST_F(FieldTrialTest, ActiveGroups) {
264 std::string no_group("No Group");
265 scoped_refptr<FieldTrial> trial(FieldTrialList::FactoryGetFieldTrial(
266 no_group, 10, "Default", next_year_, 12, 31, NULL));
268 // There is no winner yet, so no NameGroupId should be returned.
269 FieldTrial::ActiveGroup active_group;
270 EXPECT_FALSE(trial->GetActiveGroup(&active_group));
272 // Create a single winning group.
273 std::string one_winner("One Winner");
274 trial = FieldTrialList::FactoryGetFieldTrial(
275 one_winner, 10, "Default", next_year_, 12, 31, 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.
280 trial->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 FieldTrialList::FactoryGetFieldTrial(multi_group, 9, "Default",
288 next_year_, 12, 31, 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 FieldTrial* trial =
320 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
321 next_year_, 12, 31, &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 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
354 "Some name", 10, "Default some name", next_year_, 12, 31, 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 FieldTrial* trial2 = FieldTrialList::FactoryGetFieldTrial(
372 "xxx", 10, "Default xxx", next_year_, 12, 31, 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 FieldTrial* trial3 = FieldTrialList::FactoryGetFieldTrial(
384 "zzz", 10, "default", next_year_, 12, 31, 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/");
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("MissingSlash"));
411 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("MissingGroupName/"));
412 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
413 "MissingFinalSlash/gname"));
414 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
415 "noname, only group/"));
418 TEST_F(FieldTrialTest, DuplicateRestore) {
419 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
420 "Some name", 10, "Default some name", next_year_, 12, 31, NULL);
421 trial->AppendGroup("Winner", 10);
422 // Finalize the group selection by accessing the selected group.
423 trial->group();
424 std::string save_string;
425 FieldTrialList::StatesToString(&save_string);
426 EXPECT_EQ("Some name/Winner/", save_string);
428 // It is OK if we redundantly specify a winner.
429 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(save_string));
431 // But it is an error to try to change to a different winner.
432 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("Some name/Loser/"));
435 TEST_F(FieldTrialTest, CreateTrialsFromStringAreActive) {
436 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
437 ASSERT_FALSE(FieldTrialList::TrialExists("Xyz"));
438 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString("Abc/def/Xyz/zyx/"));
440 FieldTrial::ActiveGroups active_groups;
441 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
442 ASSERT_EQ(2U, active_groups.size());
443 EXPECT_EQ("Abc", active_groups[0].trial_name);
444 EXPECT_EQ("def", active_groups[0].group_name);
445 EXPECT_EQ("Xyz", active_groups[1].trial_name);
446 EXPECT_EQ("zyx", active_groups[1].group_name);
449 TEST_F(FieldTrialTest, CreateTrialsFromStringObserver) {
450 ASSERT_FALSE(FieldTrialList::TrialExists("Abc"));
452 TestFieldTrialObserver observer;
453 ASSERT_TRUE(FieldTrialList::CreateTrialsFromString("Abc/def/"));
455 RunLoop().RunUntilIdle();
456 EXPECT_EQ("Abc", observer.trial_name());
457 EXPECT_EQ("def", observer.group_name());
460 TEST_F(FieldTrialTest, CreateFieldTrial) {
461 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name"));
463 FieldTrialList::CreateFieldTrial("Some_name", "Winner");
465 FieldTrial* trial = FieldTrialList::Find("Some_name");
466 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
467 EXPECT_EQ("Winner", trial->group_name());
468 EXPECT_EQ("Some_name", trial->trial_name());
471 TEST_F(FieldTrialTest, CreateFieldTrialIsNotActive) {
472 const char kTrialName[] = "CreateFieldTrialIsActiveTrial";
473 const char kWinnerGroup[] = "Winner";
474 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
475 FieldTrialList::CreateFieldTrial(kTrialName, kWinnerGroup);
477 FieldTrial::ActiveGroups active_groups;
478 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
479 EXPECT_TRUE(active_groups.empty());
482 TEST_F(FieldTrialTest, DuplicateFieldTrial) {
483 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
484 "Some_name", 10, "Default some name", next_year_, 12, 31, NULL);
485 trial->AppendGroup("Winner", 10);
487 // It is OK if we redundantly specify a winner.
488 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner");
489 EXPECT_TRUE(trial1 != NULL);
491 // But it is an error to try to change to a different winner.
492 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser");
493 EXPECT_TRUE(trial2 == NULL);
496 TEST_F(FieldTrialTest, MakeName) {
497 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
498 "Field Trial", 10, "Winner", next_year_, 12, 31, NULL);
499 trial->group();
500 EXPECT_EQ("Histogram_Winner",
501 FieldTrial::MakeName("Histogram", "Field Trial"));
504 TEST_F(FieldTrialTest, DisableImmediately) {
505 int default_group_number = -1;
506 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
507 "trial", 100, "default", next_year_, 12, 31, &default_group_number);
508 trial->Disable();
509 ASSERT_EQ("default", trial->group_name());
510 ASSERT_EQ(default_group_number, trial->group());
513 TEST_F(FieldTrialTest, DisableAfterInitialization) {
514 FieldTrial* trial =
515 FieldTrialList::FactoryGetFieldTrial("trial", 100, "default",
516 next_year_, 12, 31, NULL);
517 trial->AppendGroup("non_default", 100);
518 trial->Disable();
519 ASSERT_EQ("default", trial->group_name());
522 TEST_F(FieldTrialTest, ForcedFieldTrials) {
523 // Validate we keep the forced choice.
524 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Use the",
525 "Force");
526 EXPECT_STREQ("Force", forced_trial->group_name().c_str());
528 int default_group_number = -1;
529 FieldTrial* factory_trial = FieldTrialList::FactoryGetFieldTrial(
530 "Use the", 1000, "default", next_year_, 12, 31, &default_group_number);
531 EXPECT_EQ(factory_trial, forced_trial);
533 int chosen_group = factory_trial->AppendGroup("Force", 100);
534 EXPECT_EQ(chosen_group, factory_trial->group());
535 int not_chosen_group = factory_trial->AppendGroup("Dark Side", 100);
536 EXPECT_NE(chosen_group, not_chosen_group);
538 // Since we didn't force the default group, we should not be returned the
539 // chosen group as the default group.
540 EXPECT_NE(default_group_number, chosen_group);
541 int new_group = factory_trial->AppendGroup("Duck Tape", 800);
542 EXPECT_NE(chosen_group, new_group);
543 // The new group should not be the default group either.
544 EXPECT_NE(default_group_number, new_group);
547 TEST_F(FieldTrialTest, ForcedFieldTrialsDefaultGroup) {
548 // Forcing the default should use the proper group ID.
549 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Trial Name",
550 "Default");
551 int default_group_number = -1;
552 FieldTrial* factory_trial = FieldTrialList::FactoryGetFieldTrial(
553 "Trial Name", 1000, "Default", next_year_, 12, 31, &default_group_number);
554 EXPECT_EQ(forced_trial, factory_trial);
556 int other_group = factory_trial->AppendGroup("Not Default", 100);
557 EXPECT_STREQ("Default", factory_trial->group_name().c_str());
558 EXPECT_EQ(default_group_number, factory_trial->group());
559 EXPECT_NE(other_group, factory_trial->group());
561 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800);
562 EXPECT_NE(new_other_group, factory_trial->group());
565 TEST_F(FieldTrialTest, SetForced) {
566 // Start by setting a trial for which we ensure a winner...
567 int default_group_number = -1;
568 FieldTrial* forced_trial = FieldTrialList::FactoryGetFieldTrial(
569 "Use the", 1, "default", next_year_, 12, 31, &default_group_number);
570 EXPECT_EQ(forced_trial, forced_trial);
572 int forced_group = forced_trial->AppendGroup("Force", 1);
573 EXPECT_EQ(forced_group, forced_trial->group());
575 // Now force it.
576 forced_trial->SetForced();
578 // Now try to set it up differently as a hard coded registration would.
579 FieldTrial* hard_coded_trial = FieldTrialList::FactoryGetFieldTrial(
580 "Use the", 1, "default", next_year_, 12, 31, &default_group_number);
581 EXPECT_EQ(hard_coded_trial, forced_trial);
583 int would_lose_group = hard_coded_trial->AppendGroup("Force", 0);
584 EXPECT_EQ(forced_group, hard_coded_trial->group());
585 EXPECT_EQ(forced_group, would_lose_group);
587 // Same thing if we would have done it to win again.
588 FieldTrial* other_hard_coded_trial = FieldTrialList::FactoryGetFieldTrial(
589 "Use the", 1, "default", next_year_, 12, 31, &default_group_number);
590 EXPECT_EQ(other_hard_coded_trial, forced_trial);
592 int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1);
593 EXPECT_EQ(forced_group, other_hard_coded_trial->group());
594 EXPECT_EQ(forced_group, would_win_group);
597 TEST_F(FieldTrialTest, SetForcedDefaultOnly) {
598 const char kTrialName[] = "SetForcedDefaultOnly";
599 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
601 int default_group = -1;
602 FieldTrial* trial =
603 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
604 next_year_, 12, 31, &default_group);
605 trial->SetForced();
607 trial = FieldTrialList::FactoryGetFieldTrial(kTrialName, 100,
608 kDefaultGroupName, next_year_,
609 12, 31, NULL);
610 EXPECT_EQ(default_group, trial->group());
611 EXPECT_EQ(kDefaultGroupName, trial->group_name());
614 TEST_F(FieldTrialTest, SetForcedDefaultWithExtraGroup) {
615 const char kTrialName[] = "SetForcedDefaultWithExtraGroup";
616 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
618 int default_group = -1;
619 FieldTrial* trial =
620 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
621 next_year_, 12, 31, &default_group);
622 trial->SetForced();
624 trial = FieldTrialList::FactoryGetFieldTrial(kTrialName, 100,
625 kDefaultGroupName, next_year_,
626 12, 31, NULL);
627 const int extra_group = trial->AppendGroup("Extra", 100);
628 EXPECT_EQ(default_group, trial->group());
629 EXPECT_NE(extra_group, trial->group());
630 EXPECT_EQ(kDefaultGroupName, trial->group_name());
633 TEST_F(FieldTrialTest, SetForcedTurnFeatureOn) {
634 const char kTrialName[] = "SetForcedTurnFeatureOn";
635 const char kExtraGroupName[] = "Extra";
636 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
638 // Simulate a server-side (forced) config that turns the feature on when the
639 // original hard-coded config had it disabled.
640 FieldTrial* forced_trial =
641 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
642 next_year_, 12, 31, NULL);
643 forced_trial->AppendGroup(kExtraGroupName, 100);
644 forced_trial->SetForced();
646 int default_group = -1;
647 FieldTrial* client_trial =
648 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
649 next_year_, 12, 31, &default_group);
650 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 0);
651 EXPECT_NE(default_group, extra_group);
653 EXPECT_FALSE(client_trial->group_reported_);
654 EXPECT_EQ(extra_group, client_trial->group());
655 EXPECT_TRUE(client_trial->group_reported_);
656 EXPECT_EQ(kExtraGroupName, client_trial->group_name());
659 TEST_F(FieldTrialTest, SetForcedTurnFeatureOff) {
660 const char kTrialName[] = "SetForcedTurnFeatureOff";
661 const char kExtraGroupName[] = "Extra";
662 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
664 // Simulate a server-side (forced) config that turns the feature off when the
665 // original hard-coded config had it enabled.
666 FieldTrial* forced_trial =
667 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
668 next_year_, 12, 31, NULL);
669 forced_trial->AppendGroup(kExtraGroupName, 0);
670 forced_trial->SetForced();
672 int default_group = -1;
673 FieldTrial* client_trial =
674 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
675 next_year_, 12, 31, &default_group);
676 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 100);
677 EXPECT_NE(default_group, extra_group);
679 EXPECT_FALSE(client_trial->group_reported_);
680 EXPECT_EQ(default_group, client_trial->group());
681 EXPECT_TRUE(client_trial->group_reported_);
682 EXPECT_EQ(kDefaultGroupName, client_trial->group_name());
685 TEST_F(FieldTrialTest, SetForcedChangeDefault_Default) {
686 const char kTrialName[] = "SetForcedDefaultGroupChange";
687 const char kGroupAName[] = "A";
688 const char kGroupBName[] = "B";
689 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
691 // Simulate a server-side (forced) config that switches which group is default
692 // and ensures that the non-forced code receives the correct group numbers.
693 FieldTrial* forced_trial =
694 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kGroupAName,
695 next_year_, 12, 31, NULL);
696 forced_trial->AppendGroup(kGroupBName, 100);
697 forced_trial->SetForced();
699 int default_group = -1;
700 FieldTrial* client_trial =
701 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kGroupBName,
702 next_year_, 12, 31, &default_group);
703 const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
704 EXPECT_NE(default_group, extra_group);
706 EXPECT_FALSE(client_trial->group_reported_);
707 EXPECT_EQ(default_group, client_trial->group());
708 EXPECT_TRUE(client_trial->group_reported_);
709 EXPECT_EQ(kGroupBName, client_trial->group_name());
712 TEST_F(FieldTrialTest, SetForcedChangeDefault_NonDefault) {
713 const char kTrialName[] = "SetForcedDefaultGroupChange";
714 const char kGroupAName[] = "A";
715 const char kGroupBName[] = "B";
716 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
718 // Simulate a server-side (forced) config that switches which group is default
719 // and ensures that the non-forced code receives the correct group numbers.
720 FieldTrial* forced_trial =
721 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kGroupAName,
722 next_year_, 12, 31, NULL);
723 forced_trial->AppendGroup(kGroupBName, 0);
724 forced_trial->SetForced();
726 int default_group = -1;
727 FieldTrial* client_trial =
728 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kGroupBName,
729 next_year_, 12, 31, &default_group);
730 const int extra_group = client_trial->AppendGroup(kGroupAName, 50);
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(kGroupAName, client_trial->group_name());
739 TEST_F(FieldTrialTest, Observe) {
740 const char kTrialName[] = "TrialToObserve1";
741 const char kSecondaryGroupName[] = "SecondaryGroup";
743 TestFieldTrialObserver observer;
744 int default_group = -1;
745 FieldTrial* trial =
746 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
747 next_year_, 12, 31, &default_group);
748 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50);
749 const int chosen_group = trial->group();
750 EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group);
752 RunLoop().RunUntilIdle();
753 EXPECT_EQ(kTrialName, observer.trial_name());
754 if (chosen_group == default_group)
755 EXPECT_EQ(kDefaultGroupName, observer.group_name());
756 else
757 EXPECT_EQ(kSecondaryGroupName, observer.group_name());
760 TEST_F(FieldTrialTest, ObserveDisabled) {
761 const char kTrialName[] = "TrialToObserve2";
763 TestFieldTrialObserver observer;
764 int default_group = -1;
765 FieldTrial* trial =
766 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
767 next_year_, 12, 31, &default_group);
768 trial->AppendGroup("A", 25);
769 trial->AppendGroup("B", 25);
770 trial->AppendGroup("C", 25);
771 trial->Disable();
773 // Observer shouldn't be notified of a disabled trial.
774 RunLoop().RunUntilIdle();
775 EXPECT_TRUE(observer.trial_name().empty());
776 EXPECT_TRUE(observer.group_name().empty());
778 // Observer shouldn't be notified even after a |group()| call.
779 EXPECT_EQ(default_group, trial->group());
780 RunLoop().RunUntilIdle();
781 EXPECT_TRUE(observer.trial_name().empty());
782 EXPECT_TRUE(observer.group_name().empty());
785 TEST_F(FieldTrialTest, ObserveForcedDisabled) {
786 const char kTrialName[] = "TrialToObserve3";
788 TestFieldTrialObserver observer;
789 int default_group = -1;
790 FieldTrial* trial =
791 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
792 next_year_, 12, 31, &default_group);
793 trial->AppendGroup("A", 25);
794 trial->AppendGroup("B", 25);
795 trial->AppendGroup("C", 25);
796 trial->SetForced();
797 trial->Disable();
799 // Observer shouldn't be notified of a disabled trial, even when forced.
800 RunLoop().RunUntilIdle();
801 EXPECT_TRUE(observer.trial_name().empty());
802 EXPECT_TRUE(observer.group_name().empty());
804 // Observer shouldn't be notified even after a |group()| call.
805 EXPECT_EQ(default_group, trial->group());
806 RunLoop().RunUntilIdle();
807 EXPECT_TRUE(observer.trial_name().empty());
808 EXPECT_TRUE(observer.group_name().empty());
811 TEST_F(FieldTrialTest, DisabledTrialNotActive) {
812 const char kTrialName[] = "DisabledTrial";
813 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
815 FieldTrial* trial =
816 FieldTrialList::FactoryGetFieldTrial(kTrialName, 100, kDefaultGroupName,
817 next_year_, 12, 31, NULL);
818 trial->AppendGroup("X", 50);
819 trial->Disable();
821 // Ensure the trial is not listed as active.
822 FieldTrial::ActiveGroups active_groups;
823 FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
824 EXPECT_TRUE(active_groups.empty());
826 // Ensure the trial is not listed in the |StatesToString()| result.
827 std::string states;
828 FieldTrialList::StatesToString(&states);
829 EXPECT_TRUE(states.empty());
832 TEST_F(FieldTrialTest, ExpirationYearNotExpired) {
833 const char kTrialName[] = "NotExpired";
834 const char kGroupName[] = "Group2";
835 const int kProbability = 100;
836 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName));
838 FieldTrial* trial =
839 FieldTrialList::FactoryGetFieldTrial(
840 kTrialName, kProbability, kDefaultGroupName,
841 FieldTrialList::kNoExpirationYear, 1, 1, NULL);
842 trial->AppendGroup(kGroupName, kProbability);
843 EXPECT_EQ(kGroupName, trial->group_name());
846 } // namespace base