OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/field_trial.h" | 5 #include "base/metrics/field_trial.h" |
6 | 6 |
7 #include "base/build_time.h" | 7 #include "base/build_time.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 for (size_t i = 0; i < active_groups.size(); ++i) { | 304 for (size_t i = 0; i < active_groups.size(); ++i) { |
305 // Order is not guaranteed, so check all values. | 305 // Order is not guaranteed, so check all values. |
306 EXPECT_NE(no_group, active_groups[i].trial_name); | 306 EXPECT_NE(no_group, active_groups[i].trial_name); |
307 EXPECT_TRUE(one_winner != active_groups[i].trial_name || | 307 EXPECT_TRUE(one_winner != active_groups[i].trial_name || |
308 winner == active_groups[i].group_name); | 308 winner == active_groups[i].group_name); |
309 EXPECT_TRUE(multi_group != active_groups[i].trial_name || | 309 EXPECT_TRUE(multi_group != active_groups[i].trial_name || |
310 multi_group_trial->group_name() == active_groups[i].group_name); | 310 multi_group_trial->group_name() == active_groups[i].group_name); |
311 } | 311 } |
312 } | 312 } |
313 | 313 |
| 314 TEST_F(FieldTrialTest, AllGroups) { |
| 315 FieldTrial::FieldTrialState field_trial_state; |
| 316 std::string one_winner("One Winner"); |
| 317 scoped_refptr<FieldTrial> trial = |
| 318 CreateFieldTrial(one_winner, 10, "Default", NULL); |
| 319 std::string winner("Winner"); |
| 320 trial->AppendGroup(winner, 10); |
| 321 EXPECT_TRUE(trial->GetState(&field_trial_state)); |
| 322 EXPECT_EQ(one_winner, field_trial_state.trial_name); |
| 323 EXPECT_EQ(winner, field_trial_state.group_name); |
| 324 trial->group(); |
| 325 EXPECT_TRUE(trial->GetState(&field_trial_state)); |
| 326 EXPECT_EQ(one_winner, field_trial_state.trial_name); |
| 327 EXPECT_EQ(winner, field_trial_state.group_name); |
| 328 |
| 329 std::string multi_group("MultiGroup"); |
| 330 scoped_refptr<FieldTrial> multi_group_trial = |
| 331 CreateFieldTrial(multi_group, 9, "Default", NULL); |
| 332 |
| 333 multi_group_trial->AppendGroup("Me", 3); |
| 334 multi_group_trial->AppendGroup("You", 3); |
| 335 multi_group_trial->AppendGroup("Them", 3); |
| 336 EXPECT_TRUE(multi_group_trial->GetState(&field_trial_state)); |
| 337 // Finalize the group selection by accessing the selected group. |
| 338 multi_group_trial->group(); |
| 339 EXPECT_TRUE(multi_group_trial->GetState(&field_trial_state)); |
| 340 EXPECT_EQ(multi_group, field_trial_state.trial_name); |
| 341 EXPECT_EQ(multi_group_trial->group_name(), field_trial_state.group_name); |
| 342 } |
| 343 |
314 TEST_F(FieldTrialTest, ActiveGroupsNotFinalized) { | 344 TEST_F(FieldTrialTest, ActiveGroupsNotFinalized) { |
315 const char kTrialName[] = "TestTrial"; | 345 const char kTrialName[] = "TestTrial"; |
316 const char kSecondaryGroupName[] = "SecondaryGroup"; | 346 const char kSecondaryGroupName[] = "SecondaryGroup"; |
317 | 347 |
318 int default_group = -1; | 348 int default_group = -1; |
319 scoped_refptr<FieldTrial> trial = | 349 scoped_refptr<FieldTrial> trial = |
320 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 350 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
321 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50); | 351 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50); |
322 | 352 |
323 // Before |group()| is called, |GetActiveGroup()| should return false. | 353 // Before |group()| is called, |GetActiveGroup()| should return false. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 // Create a third trial with only the default group. | 411 // Create a third trial with only the default group. |
382 scoped_refptr<FieldTrial> trial3 = | 412 scoped_refptr<FieldTrial> trial3 = |
383 CreateFieldTrial("zzz", 10, "default", NULL); | 413 CreateFieldTrial("zzz", 10, "default", NULL); |
384 // Finalize the group selection by accessing the selected group. | 414 // Finalize the group selection by accessing the selected group. |
385 trial3->group(); | 415 trial3->group(); |
386 | 416 |
387 FieldTrialList::StatesToString(&save_string); | 417 FieldTrialList::StatesToString(&save_string); |
388 EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string); | 418 EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string); |
389 } | 419 } |
390 | 420 |
| 421 TEST_F(FieldTrialTest, SaveAll) { |
| 422 std::string save_string; |
| 423 |
| 424 scoped_refptr<FieldTrial> trial = |
| 425 CreateFieldTrial("Some name", 10, "Default some name", NULL); |
| 426 EXPECT_EQ("", trial->group_name_internal()); |
| 427 FieldTrialList::AllStatesToString(&save_string); |
| 428 EXPECT_EQ("Some name/Default some name/", save_string); |
| 429 save_string.clear(); |
| 430 |
| 431 // Create a winning group. |
| 432 trial->AppendGroup("Winner", 10); |
| 433 // Finalize the group selection by accessing the selected group. |
| 434 trial->group(); |
| 435 FieldTrialList::AllStatesToString(&save_string); |
| 436 EXPECT_EQ("*Some name/Winner/", save_string); |
| 437 save_string.clear(); |
| 438 |
| 439 // Create a second trial and winning group. |
| 440 scoped_refptr<FieldTrial> trial2 = |
| 441 CreateFieldTrial("xxx", 10, "Default xxx", NULL); |
| 442 trial2->AppendGroup("yyyy", 10); |
| 443 // Finalize the group selection by accessing the selected group. |
| 444 trial2->group(); |
| 445 |
| 446 FieldTrialList::AllStatesToString(&save_string); |
| 447 // We assume names are alphabetized... though this is not critical. |
| 448 EXPECT_EQ("*Some name/Winner/*xxx/yyyy/", save_string); |
| 449 save_string.clear(); |
| 450 |
| 451 // Create a third trial with only the default group. |
| 452 scoped_refptr<FieldTrial> trial3 = |
| 453 CreateFieldTrial("zzz", 10, "default", NULL); |
| 454 |
| 455 FieldTrialList::AllStatesToString(&save_string); |
| 456 EXPECT_EQ("*Some name/Winner/*xxx/yyyy/zzz/default/", save_string); |
| 457 } |
| 458 |
391 TEST_F(FieldTrialTest, Restore) { | 459 TEST_F(FieldTrialTest, Restore) { |
392 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name")); | 460 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name")); |
393 ASSERT_FALSE(FieldTrialList::TrialExists("xxx")); | 461 ASSERT_FALSE(FieldTrialList::TrialExists("xxx")); |
394 | 462 |
395 FieldTrialList::CreateTrialsFromString("Some_name/Winner/xxx/yyyy/", | 463 FieldTrialList::CreateTrialsFromString("Some_name/Winner/xxx/yyyy/", |
396 FieldTrialList::DONT_ACTIVATE_TRIALS, | 464 FieldTrialList::DONT_ACTIVATE_TRIALS, |
397 std::set<std::string>()); | 465 std::set<std::string>()); |
398 | 466 |
399 FieldTrial* trial = FieldTrialList::Find("Some_name"); | 467 FieldTrial* trial = FieldTrialList::Find("Some_name"); |
400 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); | 468 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 1075 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
1008 EXPECT_TRUE(active_groups.empty()); | 1076 EXPECT_TRUE(active_groups.empty()); |
1009 | 1077 |
1010 // The trial shouldn't be listed in the |StatesToString()| result. | 1078 // The trial shouldn't be listed in the |StatesToString()| result. |
1011 std::string states; | 1079 std::string states; |
1012 FieldTrialList::StatesToString(&states); | 1080 FieldTrialList::StatesToString(&states); |
1013 EXPECT_TRUE(states.empty()); | 1081 EXPECT_TRUE(states.empty()); |
1014 } | 1082 } |
1015 } | 1083 } |
1016 | 1084 |
| 1085 TEST(FieldTrialTestWithoutList, StatesStringFormat) { |
| 1086 std::string save_string; |
| 1087 |
| 1088 // Scoping the first FieldTrialList, as we need another one to test the |
| 1089 // importing function. |
| 1090 { |
| 1091 FieldTrialList field_trial_list(NULL); |
| 1092 scoped_refptr<FieldTrial> trial = |
| 1093 CreateFieldTrial("Abc", 10, "Default some name", NULL); |
| 1094 trial->AppendGroup("cba", 10); |
| 1095 trial->group(); |
| 1096 scoped_refptr<FieldTrial> trial2 = |
| 1097 CreateFieldTrial("Xyz", 10, "Default xxx", NULL); |
| 1098 trial2->AppendGroup("zyx", 10); |
| 1099 trial2->group(); |
| 1100 scoped_refptr<FieldTrial> trial3 = |
| 1101 CreateFieldTrial("zzz", 10, "default", NULL); |
| 1102 |
| 1103 FieldTrialList::AllStatesToString(&save_string); |
| 1104 } |
| 1105 |
| 1106 // Starting with a new blank FieldTrialList. |
| 1107 FieldTrialList field_trial_list(NULL); |
| 1108 ASSERT_TRUE(field_trial_list.CreateTrialsFromString( |
| 1109 save_string, FieldTrialList::DONT_ACTIVATE_TRIALS, |
| 1110 std::set<std::string>())); |
| 1111 |
| 1112 FieldTrial::ActiveGroups active_groups; |
| 1113 field_trial_list.GetActiveFieldTrialGroups(&active_groups); |
| 1114 ASSERT_EQ(2U, active_groups.size()); |
| 1115 EXPECT_EQ("Abc", active_groups[0].trial_name); |
| 1116 EXPECT_EQ("cba", active_groups[0].group_name); |
| 1117 EXPECT_EQ("Xyz", active_groups[1].trial_name); |
| 1118 EXPECT_EQ("zyx", active_groups[1].group_name); |
| 1119 EXPECT_TRUE(field_trial_list.TrialExists("zzz")); |
| 1120 } |
| 1121 |
1017 #if GTEST_HAS_DEATH_TEST | 1122 #if GTEST_HAS_DEATH_TEST |
1018 TEST(FieldTrialDeathTest, OneTimeRandomizedTrialWithoutFieldTrialList) { | 1123 TEST(FieldTrialDeathTest, OneTimeRandomizedTrialWithoutFieldTrialList) { |
1019 // Trying to instantiate a one-time randomized field trial before the | 1124 // Trying to instantiate a one-time randomized field trial before the |
1020 // FieldTrialList is created should crash. | 1125 // FieldTrialList is created should crash. |
1021 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( | 1126 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( |
1022 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, | 1127 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, |
1023 base::FieldTrialList::kNoExpirationYear, 1, 1, | 1128 base::FieldTrialList::kNoExpirationYear, 1, 1, |
1024 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); | 1129 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); |
1025 } | 1130 } |
1026 #endif | 1131 #endif |
1027 | 1132 |
1028 } // namespace base | 1133 } // namespace base |
OLD | NEW |