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); | |
Alexei Svitkine (slow)
2014/11/10 19:28:47
Could you also add a test that de-serializing the
Georges Khalil
2014/11/10 20:50:34
Already exists from a previous CL that landed, tes
Alexei Svitkine (slow)
2014/11/10 20:52:35
That doesn't test that the two formats match thoug
Georges Khalil
2014/11/10 22:36:10
New test added (StatesStringFormat).
| |
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 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1019 // Trying to instantiate a one-time randomized field trial before the | 1087 // Trying to instantiate a one-time randomized field trial before the |
1020 // FieldTrialList is created should crash. | 1088 // FieldTrialList is created should crash. |
1021 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( | 1089 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( |
1022 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, | 1090 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, |
1023 base::FieldTrialList::kNoExpirationYear, 1, 1, | 1091 base::FieldTrialList::kNoExpirationYear, 1, 1, |
1024 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); | 1092 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); |
1025 } | 1093 } |
1026 #endif | 1094 #endif |
1027 | 1095 |
1028 } // namespace base | 1096 } // namespace base |
OLD | NEW |