Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: chrome/common/metrics/experiments_helper_unittest.cc

Issue 10151017: Remove the hash fields from FieldTrials. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Tests for the Experiment Helpers. 5 // Tests for the Experiment Helpers.
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "chrome/common/metrics/experiments_helper.h" 11 #include "chrome/common/metrics/experiments_helper.h"
12 #include "content/test/test_browser_thread.h" 12 #include "content/test/test_browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace { 15 namespace {
16 16
17 // Convenience helper to retrieve the GoogleExperimentID for a FieldTrial. Note 17 // Convenience helper to retrieve the GoogleExperimentID for a FieldTrial. Note
18 // that this will do the group assignment in |trial| if not already done. 18 // that this will do the group assignment in |trial| if not already done.
19 experiments_helper::GoogleExperimentID GetIDForTrial(base::FieldTrial* trial) { 19 experiments_helper::GoogleExperimentID GetIDForTrial(base::FieldTrial* trial) {
20 return experiments_helper::GetGoogleExperimentID( 20 return experiments_helper::GetGoogleExperimentID(
21 base::FieldTrial::MakeNameGroupId(trial->name(), 21 experiments_helper::MakeNameGroupId(trial->name(),
22 trial->group_name())); 22 trial->group_name()));
23 } 23 }
24 24
25 } // namespace 25 } // namespace
26 26
27 class ExperimentsHelperTest : public ::testing::Test { 27 class ExperimentsHelperTest : public ::testing::Test {
28 public: 28 public:
29 ExperimentsHelperTest() { 29 ExperimentsHelperTest() {
30 // Since the API can only be called on the UI thread, we have to fake that 30 // Since the API can only be called on the UI thread, we have to fake that
31 // we're on it. 31 // we're on it.
32 ui_thread_.reset(new content::TestBrowserThread( 32 ui_thread_.reset(new content::TestBrowserThread(
33 content::BrowserThread::UI, &message_loop_)); 33 content::BrowserThread::UI, &message_loop_));
34 34
35 base::Time now = base::Time::NowFromSystemTime(); 35 base::Time now = base::Time::NowFromSystemTime();
36 base::TimeDelta oneYear = base::TimeDelta::FromDays(365); 36 base::TimeDelta oneYear = base::TimeDelta::FromDays(365);
37 base::Time::Exploded exploded; 37 base::Time::Exploded exploded;
38 38
39 base::Time next_year_time = now + oneYear; 39 base::Time next_year_time = now + oneYear;
40 next_year_time.LocalExplode(&exploded); 40 next_year_time.LocalExplode(&exploded);
41 next_year_ = exploded.year; 41 next_year_ = exploded.year;
42 } 42 }
43 43
44 protected: 44 protected:
45 int next_year_; 45 int next_year_;
46 46
47 private: 47 private:
48 MessageLoop message_loop_; 48 MessageLoop message_loop_;
49 scoped_ptr<content::TestBrowserThread> ui_thread_; 49 scoped_ptr<content::TestBrowserThread> ui_thread_;
50 }; 50 };
51 51
52 TEST_F(ExperimentsHelperTest, HashName) {
53 // Make sure hashing is stable on all platforms.
54 struct {
55 const char* name;
56 uint32 hash_value;
57 } known_hashes[] = {
58 {"a", 937752454u},
59 {"1", 723085877u},
60 {"Trial Name", 2713117220u},
61 {"Group Name", 3201815843u},
62 {"My Favorite Experiment", 3722155194u},
63 {"My Awesome Group Name", 4109503236u},
64 {"abcdefghijklmonpqrstuvwxyz", 787728696u},
65 {"0123456789ABCDEF", 348858318U}
66 };
67 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) {
68 EXPECT_EQ(known_hashes[i].hash_value,
69 experiments_helper::TestingHashName(known_hashes[i].name));
70 }
71 }
72
73 TEST_F(ExperimentsHelperTest, GetFieldTrialSelectedGroups) {
74 std::string trial_one("trial one");
75 std::string group_one("group one");
76 std::string trial_two("trial two");
77 std::string group_two("group two");
78
79 experiments_helper::SelectedGroups selected_groups;
80 base::FieldTrial::SelectedGroup selected_group;
81 selected_group.trial = trial_one;
82 selected_group.group = group_one;
83 selected_groups.push_back(selected_group);
84
85 selected_group.trial = trial_two;
86 selected_group.group = group_two;
87 selected_groups.push_back(selected_group);
88
89 // Create our expected hashes.
90 uint32 trial_one_id = experiments_helper::TestingHashName(trial_one);
91 uint32 group_one_id = experiments_helper::TestingHashName(group_one);
92 uint32 trial_two_id = experiments_helper::TestingHashName(trial_two);
93 uint32 group_two_id = experiments_helper::TestingHashName(group_two);
94
95 std::vector<experiments_helper::NameGroupId> name_group_ids;
96 GetFieldTrialNameGroupIdsForSelectedGroups(selected_groups, &name_group_ids);
97 EXPECT_EQ(2U, name_group_ids.size());
98 for (size_t i = 0; i < name_group_ids.size(); ++i) {
99 EXPECT_TRUE(trial_one_id != name_group_ids[i].name ||
MAD 2012/04/25 20:03:22 This doesn't give an error if the returned vector
SteveT 2012/04/26 19:03:31 Right. Going to do this with a set, which requires
MAD 2012/04/26 19:40:12 You could also create a C style array on the stack
100 group_one_id == name_group_ids[i].group);
101 EXPECT_TRUE(trial_two_id != name_group_ids[i].name ||
102 group_two_id == name_group_ids[i].group);
103 }
104 }
105
52 // Test that if the trial is immediately disabled, GetGoogleExperimentID just 106 // Test that if the trial is immediately disabled, GetGoogleExperimentID just
53 // returns the empty ID. 107 // returns the empty ID.
54 TEST_F(ExperimentsHelperTest, DisableImmediately) { 108 TEST_F(ExperimentsHelperTest, DisableImmediately) {
55 int default_group_number = -1; 109 int default_group_number = -1;
56 scoped_refptr<base::FieldTrial> trial( 110 scoped_refptr<base::FieldTrial> trial(
57 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", 111 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default",
58 next_year_, 12, 12, 112 next_year_, 12, 12,
59 &default_group_number)); 113 &default_group_number));
60 trial->Disable(); 114 trial->Disable();
61 115
62 ASSERT_EQ(default_group_number, trial->group()); 116 ASSERT_EQ(default_group_number, trial->group());
63 ASSERT_EQ(experiments_helper::kEmptyGoogleExperimentID, 117 ASSERT_EQ(experiments_helper::kEmptyGoogleExperimentID,
64 GetIDForTrial(trial.get())); 118 GetIDForTrial(trial.get()));
65 } 119 }
66 120
67 // Test that successfully associating the FieldTrial with some ID, and then 121 // Test that successfully associating the FieldTrial with some ID, and then
68 // disabling the FieldTrial actually makes GetGoogleExperimentID correctly 122 // disabling the FieldTrial actually makes GetGoogleExperimentID correctly
69 // return the empty ID. 123 // return the empty ID.
70 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) { 124 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) {
71 const std::string default_name = "default"; 125 const std::string default_name = "default";
72 const std::string non_default_name = "non_default"; 126 const std::string non_default_name = "non_default";
73 127
74 scoped_refptr<base::FieldTrial> trial( 128 scoped_refptr<base::FieldTrial> trial(
75 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, 129 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name,
76 next_year_, 12, 12, NULL)); 130 next_year_, 12, 12, NULL));
77 trial->AppendGroup(non_default_name, 100); 131 trial->AppendGroup(non_default_name, 100);
78 experiments_helper::AssociateGoogleExperimentID( 132 experiments_helper::AssociateGoogleExperimentID(
79 base::FieldTrial::MakeNameGroupId(trial->name(), default_name), 123); 133 experiments_helper::MakeNameGroupId(trial->name(), default_name), 123);
80 experiments_helper::AssociateGoogleExperimentID( 134 experiments_helper::AssociateGoogleExperimentID(
81 base::FieldTrial::MakeNameGroupId(trial->name(), non_default_name), 456); 135 experiments_helper::MakeNameGroupId(trial->name(), non_default_name),
136 456);
82 ASSERT_EQ(non_default_name, trial->group_name()); 137 ASSERT_EQ(non_default_name, trial->group_name());
83 ASSERT_EQ(456U, GetIDForTrial(trial.get())); 138 ASSERT_EQ(456U, GetIDForTrial(trial.get()));
84 trial->Disable(); 139 trial->Disable();
85 ASSERT_EQ(default_name, trial->group_name()); 140 ASSERT_EQ(default_name, trial->group_name());
86 ASSERT_EQ(123U, GetIDForTrial(trial.get())); 141 ASSERT_EQ(123U, GetIDForTrial(trial.get()));
87 } 142 }
88 143
89 // Test various successful association cases. 144 // Test various successful association cases.
90 TEST_F(ExperimentsHelperTest, AssociateGoogleExperimentID) { 145 TEST_F(ExperimentsHelperTest, AssociateGoogleExperimentID) {
91 const std::string default_name1 = "default1"; 146 const std::string default_name1 = "default1";
92 scoped_refptr<base::FieldTrial> trial_true( 147 scoped_refptr<base::FieldTrial> trial_true(
93 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, 148 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1,
94 next_year_, 12, 31, NULL)); 149 next_year_, 12, 31, NULL));
95 const std::string winner = "TheWinner"; 150 const std::string winner = "TheWinner";
96 int winner_group = trial_true->AppendGroup(winner, 10); 151 int winner_group = trial_true->AppendGroup(winner, 10);
97 152
98 // Set GoogleExperimentIDs so we can verify that they were chosen correctly. 153 // Set GoogleExperimentIDs so we can verify that they were chosen correctly.
99 experiments_helper::AssociateGoogleExperimentID( 154 experiments_helper::AssociateGoogleExperimentID(
100 base::FieldTrial::MakeNameGroupId(trial_true->name(), default_name1), 155 experiments_helper::MakeNameGroupId(trial_true->name(), default_name1),
101 123); 156 123);
102 experiments_helper::AssociateGoogleExperimentID( 157 experiments_helper::AssociateGoogleExperimentID(
103 base::FieldTrial::MakeNameGroupId(trial_true->name(), winner), 158 experiments_helper::MakeNameGroupId(trial_true->name(), winner),
104 456); 159 456);
105 160
106 EXPECT_EQ(winner_group, trial_true->group()); 161 EXPECT_EQ(winner_group, trial_true->group());
107 EXPECT_EQ(winner, trial_true->group_name()); 162 EXPECT_EQ(winner, trial_true->group_name());
108 EXPECT_EQ(456U, GetIDForTrial(trial_true.get())); 163 EXPECT_EQ(456U, GetIDForTrial(trial_true.get()));
109 164
110 const std::string default_name2 = "default2"; 165 const std::string default_name2 = "default2";
111 scoped_refptr<base::FieldTrial> trial_false( 166 scoped_refptr<base::FieldTrial> trial_false(
112 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2, 167 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2,
113 next_year_, 12, 31, NULL)); 168 next_year_, 12, 31, NULL));
114 const std::string loser = "ALoser"; 169 const std::string loser = "ALoser";
115 int loser_group = trial_false->AppendGroup(loser, 0); 170 int loser_group = trial_false->AppendGroup(loser, 0);
116 171
117 experiments_helper::AssociateGoogleExperimentID( 172 experiments_helper::AssociateGoogleExperimentID(
118 base::FieldTrial::MakeNameGroupId(trial_false->name(), default_name2), 173 experiments_helper::MakeNameGroupId(trial_false->name(), default_name2),
119 123); 174 123);
120 experiments_helper::AssociateGoogleExperimentID( 175 experiments_helper::AssociateGoogleExperimentID(
121 base::FieldTrial::MakeNameGroupId(trial_false->name(), loser), 176 experiments_helper::MakeNameGroupId(trial_false->name(), loser),
122 456); 177 456);
123 178
124 EXPECT_NE(loser_group, trial_false->group()); 179 EXPECT_NE(loser_group, trial_false->group());
125 EXPECT_EQ(123U, GetIDForTrial(trial_false.get())); 180 EXPECT_EQ(123U, GetIDForTrial(trial_false.get()));
126 } 181 }
127 182
128 // Test that not associating a FieldTrial with any IDs ensure that the empty ID 183 // Test that not associating a FieldTrial with any IDs ensure that the empty ID
129 // will be returned. 184 // will be returned.
130 TEST_F(ExperimentsHelperTest, NoAssociation) { 185 TEST_F(ExperimentsHelperTest, NoAssociation) {
131 const std::string default_name = "default"; 186 const std::string default_name = "default";
132 scoped_refptr<base::FieldTrial> no_id_trial( 187 scoped_refptr<base::FieldTrial> no_id_trial(
133 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, 188 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name,
134 next_year_, 12, 31, NULL)); 189 next_year_, 12, 31, NULL));
135 const std::string winner = "TheWinner"; 190 const std::string winner = "TheWinner";
136 int winner_group = no_id_trial->AppendGroup(winner, 10); 191 int winner_group = no_id_trial->AppendGroup(winner, 10);
137 192
138 // Ensure that despite the fact that a normal winner is elected, it does not 193 // Ensure that despite the fact that a normal winner is elected, it does not
139 // have a valid GoogleExperimentID associated with it. 194 // have a valid GoogleExperimentID associated with it.
140 EXPECT_EQ(winner_group, no_id_trial->group()); 195 EXPECT_EQ(winner_group, no_id_trial->group());
141 EXPECT_EQ(winner, no_id_trial->group_name()); 196 EXPECT_EQ(winner, no_id_trial->group_name());
142 EXPECT_EQ(experiments_helper::kEmptyGoogleExperimentID, 197 EXPECT_EQ(experiments_helper::kEmptyGoogleExperimentID,
143 GetIDForTrial(no_id_trial.get())); 198 GetIDForTrial(no_id_trial.get()));
144 } 199 }
OLDNEW
« chrome/common/metrics/experiments_helper.cc ('K') | « chrome/common/metrics/experiments_helper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698