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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 } // namespace | 66 } // namespace |
67 | 67 |
68 class FieldTrialTest : public testing::Test { | 68 class FieldTrialTest : public testing::Test { |
69 public: | 69 public: |
70 FieldTrialTest() : trial_list_(NULL) {} | 70 FieldTrialTest() : trial_list_(NULL) {} |
71 | 71 |
72 private: | 72 private: |
73 MessageLoop message_loop_; | 73 MessageLoop message_loop_; |
74 FieldTrialList trial_list_; | 74 FieldTrialList trial_list_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(FieldTrialTest); |
75 }; | 77 }; |
76 | 78 |
77 // Test registration, and also check that destructors are called for trials | 79 // Test registration, and also check that destructors are called for trials |
78 // (and that Valgrind doesn't catch us leaking). | 80 // (and that Valgrind doesn't catch us leaking). |
79 TEST_F(FieldTrialTest, Registration) { | 81 TEST_F(FieldTrialTest, Registration) { |
80 const char name1[] = "name 1 test"; | 82 const char name1[] = "name 1 test"; |
81 const char name2[] = "name 2 test"; | 83 const char name2[] = "name 2 test"; |
82 EXPECT_FALSE(FieldTrialList::Find(name1)); | 84 EXPECT_FALSE(FieldTrialList::Find(name1)); |
83 EXPECT_FALSE(FieldTrialList::Find(name2)); | 85 EXPECT_FALSE(FieldTrialList::Find(name2)); |
84 | 86 |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 EXPECT_EQ(kDefaultGroupName, active_group.group_name); | 371 EXPECT_EQ(kDefaultGroupName, active_group.group_name); |
370 else | 372 else |
371 EXPECT_EQ(kSecondaryGroupName, active_group.group_name); | 373 EXPECT_EQ(kSecondaryGroupName, active_group.group_name); |
372 | 374 |
373 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 375 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
374 ASSERT_EQ(1U, active_groups.size()); | 376 ASSERT_EQ(1U, active_groups.size()); |
375 EXPECT_EQ(kTrialName, active_groups[0].trial_name); | 377 EXPECT_EQ(kTrialName, active_groups[0].trial_name); |
376 EXPECT_EQ(active_group.group_name, active_groups[0].group_name); | 378 EXPECT_EQ(active_group.group_name, active_groups[0].group_name); |
377 } | 379 } |
378 | 380 |
| 381 TEST_F(FieldTrialTest, GetGroupNameWithoutActivation) { |
| 382 const char kTrialName[] = "TestTrial"; |
| 383 const char kSecondaryGroupName[] = "SecondaryGroup"; |
| 384 |
| 385 int default_group = -1; |
| 386 scoped_refptr<FieldTrial> trial = |
| 387 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
| 388 trial->AppendGroup(kSecondaryGroupName, 50); |
| 389 |
| 390 // The trial should start inactive. |
| 391 EXPECT_FALSE(FieldTrialList::IsTrialActive(kTrialName)); |
| 392 |
| 393 // Calling |GetGroupNameWithoutActivation()| should not activate the trial. |
| 394 std::string group_name = trial->GetGroupNameWithoutActivation(); |
| 395 EXPECT_FALSE(group_name.empty()); |
| 396 EXPECT_FALSE(FieldTrialList::IsTrialActive(kTrialName)); |
| 397 |
| 398 // Calling |group_name()| should activate it and return the same group name. |
| 399 EXPECT_EQ(group_name, trial->group_name()); |
| 400 EXPECT_TRUE(FieldTrialList::IsTrialActive(kTrialName)); |
| 401 } |
| 402 |
379 TEST_F(FieldTrialTest, Save) { | 403 TEST_F(FieldTrialTest, Save) { |
380 std::string save_string; | 404 std::string save_string; |
381 | 405 |
382 scoped_refptr<FieldTrial> trial = | 406 scoped_refptr<FieldTrial> trial = |
383 CreateFieldTrial("Some name", 10, "Default some name", NULL); | 407 CreateFieldTrial("Some name", 10, "Default some name", NULL); |
384 // There is no winner yet, so no textual group name is associated with trial. | 408 // There is no winner yet, so no textual group name is associated with trial. |
385 // In this case, the trial should not be included. | 409 // In this case, the trial should not be included. |
386 EXPECT_EQ("", trial->group_name_internal()); | 410 EXPECT_EQ("", trial->group_name_internal()); |
387 FieldTrialList::StatesToString(&save_string); | 411 FieldTrialList::StatesToString(&save_string); |
388 EXPECT_EQ("", save_string); | 412 EXPECT_EQ("", save_string); |
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1124 // Trying to instantiate a one-time randomized field trial before the | 1148 // Trying to instantiate a one-time randomized field trial before the |
1125 // FieldTrialList is created should crash. | 1149 // FieldTrialList is created should crash. |
1126 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( | 1150 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( |
1127 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, | 1151 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, |
1128 base::FieldTrialList::kNoExpirationYear, 1, 1, | 1152 base::FieldTrialList::kNoExpirationYear, 1, 1, |
1129 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); | 1153 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); |
1130 } | 1154 } |
1131 #endif | 1155 #endif |
1132 | 1156 |
1133 } // namespace base | 1157 } // namespace base |
OLD | NEW |