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 20 matching lines...) Expand all Loading... | |
31 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); | 31 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); |
32 } | 32 } |
33 | 33 |
34 int OneYearBeforeBuildTime() { | 34 int OneYearBeforeBuildTime() { |
35 Time one_year_before_build_time = GetBuildTime() - TimeDelta::FromDays(365); | 35 Time one_year_before_build_time = GetBuildTime() - TimeDelta::FromDays(365); |
36 Time::Exploded exploded; | 36 Time::Exploded exploded; |
37 one_year_before_build_time.LocalExplode(&exploded); | 37 one_year_before_build_time.LocalExplode(&exploded); |
38 return exploded.year; | 38 return exploded.year; |
39 } | 39 } |
40 | 40 |
41 // Tests whether a field trial is active (i.e. group() has been called on it), | |
42 // using public FieldTrial API (which doesn't expose this state on the object). | |
43 bool IsFieldTrialActive(FieldTrial* trial) { | |
44 base::FieldTrial::ActiveGroups active_groups; | |
45 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | |
46 for (size_t i = 0; i < active_groups.size(); ++i) { | |
47 if (active_groups[i].trial_name == trial->trial_name()) | |
48 return true; | |
49 } | |
50 return false; | |
51 } | |
Ilya Sherman
2015/09/16 00:50:44
nit: Perhaps create a helper file to contain this
Alexei Svitkine (slow)
2015/09/23 17:33:32
Doing this in a separate CL: https://codereview.ch
| |
52 | |
41 // FieldTrialList::Observer implementation for testing. | 53 // FieldTrialList::Observer implementation for testing. |
42 class TestFieldTrialObserver : public FieldTrialList::Observer { | 54 class TestFieldTrialObserver : public FieldTrialList::Observer { |
43 public: | 55 public: |
44 TestFieldTrialObserver() { | 56 TestFieldTrialObserver() { |
45 FieldTrialList::AddObserver(this); | 57 FieldTrialList::AddObserver(this); |
46 } | 58 } |
47 | 59 |
48 ~TestFieldTrialObserver() override { FieldTrialList::RemoveObserver(this); } | 60 ~TestFieldTrialObserver() override { FieldTrialList::RemoveObserver(this); } |
49 | 61 |
50 void OnFieldTrialGroupFinalized(const std::string& trial, | 62 void OnFieldTrialGroupFinalized(const std::string& trial, |
(...skipping 14 matching lines...) Expand all Loading... | |
65 | 77 |
66 } // namespace | 78 } // namespace |
67 | 79 |
68 class FieldTrialTest : public testing::Test { | 80 class FieldTrialTest : public testing::Test { |
69 public: | 81 public: |
70 FieldTrialTest() : trial_list_(NULL) {} | 82 FieldTrialTest() : trial_list_(NULL) {} |
71 | 83 |
72 private: | 84 private: |
73 MessageLoop message_loop_; | 85 MessageLoop message_loop_; |
74 FieldTrialList trial_list_; | 86 FieldTrialList trial_list_; |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(FieldTrialTest); | |
75 }; | 89 }; |
76 | 90 |
77 // Test registration, and also check that destructors are called for trials | 91 // Test registration, and also check that destructors are called for trials |
78 // (and that Valgrind doesn't catch us leaking). | 92 // (and that Valgrind doesn't catch us leaking). |
79 TEST_F(FieldTrialTest, Registration) { | 93 TEST_F(FieldTrialTest, Registration) { |
80 const char name1[] = "name 1 test"; | 94 const char name1[] = "name 1 test"; |
81 const char name2[] = "name 2 test"; | 95 const char name2[] = "name 2 test"; |
82 EXPECT_FALSE(FieldTrialList::Find(name1)); | 96 EXPECT_FALSE(FieldTrialList::Find(name1)); |
83 EXPECT_FALSE(FieldTrialList::Find(name2)); | 97 EXPECT_FALSE(FieldTrialList::Find(name2)); |
84 | 98 |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 EXPECT_EQ(kDefaultGroupName, active_group.group_name); | 383 EXPECT_EQ(kDefaultGroupName, active_group.group_name); |
370 else | 384 else |
371 EXPECT_EQ(kSecondaryGroupName, active_group.group_name); | 385 EXPECT_EQ(kSecondaryGroupName, active_group.group_name); |
372 | 386 |
373 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 387 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
374 ASSERT_EQ(1U, active_groups.size()); | 388 ASSERT_EQ(1U, active_groups.size()); |
375 EXPECT_EQ(kTrialName, active_groups[0].trial_name); | 389 EXPECT_EQ(kTrialName, active_groups[0].trial_name); |
376 EXPECT_EQ(active_group.group_name, active_groups[0].group_name); | 390 EXPECT_EQ(active_group.group_name, active_groups[0].group_name); |
377 } | 391 } |
378 | 392 |
393 TEST_F(FieldTrialTest, GetGroupNameWithoutActivation) { | |
394 const char kTrialName[] = "TestTrial"; | |
395 const char kSecondaryGroupName[] = "SecondaryGroup"; | |
396 | |
397 int default_group = -1; | |
398 scoped_refptr<FieldTrial> trial = | |
399 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | |
400 trial->AppendGroup(kSecondaryGroupName, 50); | |
401 | |
402 // The trial starts inactive, so |GetActiveGroup()| should return false. | |
Ilya Sherman
2015/09/16 00:50:44
This comment seems like it was probably written be
Alexei Svitkine (slow)
2015/09/22 21:19:59
Done.
| |
403 EXPECT_FALSE(IsFieldTrialActive(trial.get())); | |
404 | |
405 // Calling |GetGroupNameWithoutActivation()| should not activate the trial. | |
406 std::string group_name = trial->GetGroupNameWithoutActivation(); | |
407 EXPECT_FALSE(group_name.empty()); | |
408 EXPECT_FALSE(IsFieldTrialActive(trial.get())); | |
409 | |
410 // Calling |group_name()| should activate it and return the same group name. | |
411 EXPECT_EQ(group_name, trial->group_name()); | |
412 EXPECT_TRUE(IsFieldTrialActive(trial.get())); | |
413 } | |
414 | |
379 TEST_F(FieldTrialTest, Save) { | 415 TEST_F(FieldTrialTest, Save) { |
380 std::string save_string; | 416 std::string save_string; |
381 | 417 |
382 scoped_refptr<FieldTrial> trial = | 418 scoped_refptr<FieldTrial> trial = |
383 CreateFieldTrial("Some name", 10, "Default some name", NULL); | 419 CreateFieldTrial("Some name", 10, "Default some name", NULL); |
384 // There is no winner yet, so no textual group name is associated with trial. | 420 // There is no winner yet, so no textual group name is associated with trial. |
385 // In this case, the trial should not be included. | 421 // In this case, the trial should not be included. |
386 EXPECT_EQ("", trial->group_name_internal()); | 422 EXPECT_EQ("", trial->group_name_internal()); |
387 FieldTrialList::StatesToString(&save_string); | 423 FieldTrialList::StatesToString(&save_string); |
388 EXPECT_EQ("", save_string); | 424 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 | 1160 // Trying to instantiate a one-time randomized field trial before the |
1125 // FieldTrialList is created should crash. | 1161 // FieldTrialList is created should crash. |
1126 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( | 1162 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( |
1127 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, | 1163 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, |
1128 base::FieldTrialList::kNoExpirationYear, 1, 1, | 1164 base::FieldTrialList::kNoExpirationYear, 1, 1, |
1129 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); | 1165 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); |
1130 } | 1166 } |
1131 #endif | 1167 #endif |
1132 | 1168 |
1133 } // namespace base | 1169 } // namespace base |
OLD | NEW |