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

Side by Side Diff: base/metrics/field_trial_unittest.cc

Issue 9705074: Supporting command line argument to force field trials (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed some ooopss... 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
« no previous file with comments | « base/metrics/field_trial.cc ('k') | chrome/browser/autocomplete/autocomplete_field_trial.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Test of FieldTrial class 5 // Test of FieldTrial class
6 6
7 #include "base/metrics/field_trial.h" 7 #include "base/metrics/field_trial.h"
8 8
9 #include "base/rand_util.h" 9 #include "base/rand_util.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
(...skipping 29 matching lines...) Expand all
40 }; 40 };
41 41
42 // Test registration, and also check that destructors are called for trials 42 // Test registration, and also check that destructors are called for trials
43 // (and that Valgrind doesn't catch us leaking). 43 // (and that Valgrind doesn't catch us leaking).
44 TEST_F(FieldTrialTest, Registration) { 44 TEST_F(FieldTrialTest, Registration) {
45 const char* name1 = "name 1 test"; 45 const char* name1 = "name 1 test";
46 const char* name2 = "name 2 test"; 46 const char* name2 = "name 2 test";
47 EXPECT_FALSE(FieldTrialList::Find(name1)); 47 EXPECT_FALSE(FieldTrialList::Find(name1));
48 EXPECT_FALSE(FieldTrialList::Find(name2)); 48 EXPECT_FALSE(FieldTrialList::Find(name2));
49 49
50 FieldTrial* trial1 = 50 FieldTrial* trial1 = FieldTrialList::FactoryGetFieldTrial(
51 new FieldTrial(name1, 10, "default name 1 test", next_year_, 12, 31); 51 name1, 10, "default name 1 test", next_year_, 12, 31, NULL);
52 EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_); 52 EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_);
53 EXPECT_EQ(name1, trial1->name()); 53 EXPECT_EQ(name1, trial1->name());
54 EXPECT_EQ("", trial1->group_name_internal()); 54 EXPECT_EQ("", trial1->group_name_internal());
55 55
56 trial1->AppendGroup("", 7); 56 trial1->AppendGroup("", 7);
57 57
58 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); 58 EXPECT_EQ(trial1, FieldTrialList::Find(name1));
59 EXPECT_FALSE(FieldTrialList::Find(name2)); 59 EXPECT_FALSE(FieldTrialList::Find(name2));
60 60
61 FieldTrial* trial2 = 61 FieldTrial* trial2 = FieldTrialList::FactoryGetFieldTrial(
62 new FieldTrial(name2, 10, "default name 2 test", next_year_, 12, 31); 62 name2, 10, "default name 2 test", next_year_, 12, 31, NULL);
63 EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_); 63 EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_);
64 EXPECT_EQ(name2, trial2->name()); 64 EXPECT_EQ(name2, trial2->name());
65 EXPECT_EQ("", trial2->group_name_internal()); 65 EXPECT_EQ("", trial2->group_name_internal());
66 66
67 trial2->AppendGroup("a first group", 7); 67 trial2->AppendGroup("a first group", 7);
68 68
69 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); 69 EXPECT_EQ(trial1, FieldTrialList::Find(name1));
70 EXPECT_EQ(trial2, FieldTrialList::Find(name2)); 70 EXPECT_EQ(trial2, FieldTrialList::Find(name2));
71 // Note: FieldTrialList should delete the objects at shutdown. 71 // Note: FieldTrialList should delete the objects at shutdown.
72 } 72 }
73 73
74 TEST_F(FieldTrialTest, AbsoluteProbabilities) { 74 TEST_F(FieldTrialTest, AbsoluteProbabilities) {
75 char always_true[] = " always true"; 75 char always_true[] = " always true";
76 char default_always_true[] = " default always true"; 76 char default_always_true[] = " default always true";
77 char always_false[] = " always false"; 77 char always_false[] = " always false";
78 char default_always_false[] = " default always false"; 78 char default_always_false[] = " default always false";
79 for (int i = 1; i < 250; ++i) { 79 for (int i = 1; i < 250; ++i) {
80 // Try lots of names, by changing the first character of the name. 80 // Try lots of names, by changing the first character of the name.
81 always_true[0] = i; 81 always_true[0] = i;
82 default_always_true[0] = i; 82 default_always_true[0] = i;
83 always_false[0] = i; 83 always_false[0] = i;
84 default_always_false[0] = i; 84 default_always_false[0] = i;
85 85
86 FieldTrial* trial_true = 86 FieldTrial* trial_true = FieldTrialList::FactoryGetFieldTrial(
87 new FieldTrial( 87 always_true, 10, default_always_true, next_year_, 12, 31, NULL);
88 always_true, 10, default_always_true, next_year_, 12, 31);
89 const std::string winner = "TheWinner"; 88 const std::string winner = "TheWinner";
90 int winner_group = trial_true->AppendGroup(winner, 10); 89 int winner_group = trial_true->AppendGroup(winner, 10);
91 90
92 EXPECT_EQ(winner_group, trial_true->group()); 91 EXPECT_EQ(winner_group, trial_true->group());
93 EXPECT_EQ(winner, trial_true->group_name()); 92 EXPECT_EQ(winner, trial_true->group_name());
94 93
95 FieldTrial* trial_false = 94 FieldTrial* trial_false = FieldTrialList::FactoryGetFieldTrial(
96 new FieldTrial( 95 always_false, 10, default_always_false, next_year_, 12, 31, NULL);
97 always_false, 10, default_always_false, next_year_, 12, 31);
98 int loser_group = trial_false->AppendGroup("ALoser", 0); 96 int loser_group = trial_false->AppendGroup("ALoser", 0);
99 97
100 EXPECT_NE(loser_group, trial_false->group()); 98 EXPECT_NE(loser_group, trial_false->group());
101 } 99 }
102 } 100 }
103 101
104 TEST_F(FieldTrialTest, RemainingProbability) { 102 TEST_F(FieldTrialTest, RemainingProbability) {
105 // First create a test that hasn't had a winner yet. 103 // First create a test that hasn't had a winner yet.
106 const std::string winner = "Winner"; 104 const std::string winner = "Winner";
107 const std::string loser = "Loser"; 105 const std::string loser = "Loser";
108 scoped_refptr<FieldTrial> trial; 106 scoped_refptr<FieldTrial> trial;
109 int counter = 0; 107 int counter = 0;
108 int default_group_number = -1;
110 do { 109 do {
111 std::string name = StringPrintf("trial%d", ++counter); 110 std::string name = StringPrintf("trial%d", ++counter);
112 trial = new FieldTrial(name, 10, winner, next_year_, 12, 31); 111 trial = FieldTrialList::FactoryGetFieldTrial(
112 name, 10, winner, next_year_, 12, 31, &default_group_number);
113 trial->AppendGroup(loser, 5); // 50% chance of not being chosen. 113 trial->AppendGroup(loser, 5); // 50% chance of not being chosen.
114 // If a group is not assigned, group_ will be kNotFinalized. 114 // If a group is not assigned, group_ will be kNotFinalized.
115 } while (trial->group_ != FieldTrial::kNotFinalized); 115 } while (trial->group_ != FieldTrial::kNotFinalized);
116 116
117 // And that 'default' group (winner) should always win. 117 // And that 'default' group (winner) should always win.
118 EXPECT_EQ(FieldTrial::kDefaultGroupNumber, trial->group()); 118 EXPECT_EQ(default_group_number, trial->group());
119 119
120 // And that winner should ALWAYS win. 120 // And that winner should ALWAYS win.
121 EXPECT_EQ(winner, trial->group_name()); 121 EXPECT_EQ(winner, trial->group_name());
122 } 122 }
123 123
124 TEST_F(FieldTrialTest, FiftyFiftyProbability) { 124 TEST_F(FieldTrialTest, FiftyFiftyProbability) {
125 // Check that even with small divisors, we have the proper probabilities, and 125 // Check that even with small divisors, we have the proper probabilities, and
126 // all outcomes are possible. Since this is a 50-50 test, it should get both 126 // all outcomes are possible. Since this is a 50-50 test, it should get both
127 // outcomes in a few tries, but we'll try no more than 100 times (and be flaky 127 // outcomes in a few tries, but we'll try no more than 100 times (and be flaky
128 // with probability around 1 in 2^99). 128 // with probability around 1 in 2^99).
129 bool first_winner = false; 129 bool first_winner = false;
130 bool second_winner = false; 130 bool second_winner = false;
131 int counter = 0; 131 int counter = 0;
132 do { 132 do {
133 std::string name = base::StringPrintf("FiftyFifty%d", ++counter); 133 std::string name = base::StringPrintf("FiftyFifty%d", ++counter);
134 std::string default_group_name = base::StringPrintf("Default FiftyFifty%d", 134 std::string default_group_name = base::StringPrintf("Default FiftyFifty%d",
135 ++counter); 135 ++counter);
136 scoped_refptr<FieldTrial> trial( 136 scoped_refptr<FieldTrial> trial(FieldTrialList::FactoryGetFieldTrial(
137 new FieldTrial(name, 2, default_group_name, next_year_, 12, 31)); 137 name, 2, default_group_name, next_year_, 12, 31, NULL));
138 trial->AppendGroup("first", 1); // 50% chance of being chosen. 138 trial->AppendGroup("first", 1); // 50% chance of being chosen.
139 // If group_ is kNotFinalized, then a group assignement hasn't been done. 139 // If group_ is kNotFinalized, then a group assignement hasn't been done.
140 if (trial->group_ != FieldTrial::kNotFinalized) { 140 if (trial->group_ != FieldTrial::kNotFinalized) {
141 first_winner = true; 141 first_winner = true;
142 continue; 142 continue;
143 } 143 }
144 trial->AppendGroup("second", 1); // Always chosen at this point. 144 trial->AppendGroup("second", 1); // Always chosen at this point.
145 EXPECT_NE(FieldTrial::kNotFinalized, trial->group()); 145 EXPECT_NE(FieldTrial::kNotFinalized, trial->group());
146 second_winner = true; 146 second_winner = true;
147 } while ((!second_winner || !first_winner) && counter < 100); 147 } while ((!second_winner || !first_winner) && counter < 100);
148 EXPECT_TRUE(second_winner); 148 EXPECT_TRUE(second_winner);
149 EXPECT_TRUE(first_winner); 149 EXPECT_TRUE(first_winner);
150 } 150 }
151 151
152 TEST_F(FieldTrialTest, MiddleProbabilities) { 152 TEST_F(FieldTrialTest, MiddleProbabilities) {
153 char name[] = " same name"; 153 char name[] = " same name";
154 char default_group_name[] = " default same name"; 154 char default_group_name[] = " default same name";
155 bool false_event_seen = false; 155 bool false_event_seen = false;
156 bool true_event_seen = false; 156 bool true_event_seen = false;
157 for (int i = 1; i < 250; ++i) { 157 for (int i = 1; i < 250; ++i) {
158 name[0] = i; 158 name[0] = i;
159 default_group_name[0] = i; 159 default_group_name[0] = i;
160 FieldTrial* trial = 160 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
161 new FieldTrial(name, 10, default_group_name, next_year_, 12, 31); 161 name, 10, default_group_name, next_year_, 12, 31, NULL);
162 int might_win = trial->AppendGroup("MightWin", 5); 162 int might_win = trial->AppendGroup("MightWin", 5);
163 163
164 if (trial->group() == might_win) { 164 if (trial->group() == might_win) {
165 true_event_seen = true; 165 true_event_seen = true;
166 } else { 166 } else {
167 false_event_seen = true; 167 false_event_seen = true;
168 } 168 }
169 if (false_event_seen && true_event_seen) 169 if (false_event_seen && true_event_seen)
170 return; // Successful test!!! 170 return; // Successful test!!!
171 } 171 }
172 // Very surprising to get here. Probability should be around 1 in 2 ** 250. 172 // Very surprising to get here. Probability should be around 1 in 2 ** 250.
173 // One of the following will fail. 173 // One of the following will fail.
174 EXPECT_TRUE(false_event_seen); 174 EXPECT_TRUE(false_event_seen);
175 EXPECT_TRUE(true_event_seen); 175 EXPECT_TRUE(true_event_seen);
176 } 176 }
177 177
178 TEST_F(FieldTrialTest, OneWinner) { 178 TEST_F(FieldTrialTest, OneWinner) {
179 char name[] = "Some name"; 179 char name[] = "Some name";
180 char default_group_name[] = "Default some name"; 180 char default_group_name[] = "Default some name";
181 int group_count(10); 181 int group_count(10);
182 182
183 FieldTrial* trial = 183 int default_group_number = -1;
184 new FieldTrial( 184 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
185 name, group_count, default_group_name, next_year_, 12, 31); 185 name, group_count, default_group_name, next_year_, 12, 31,
186 &default_group_number);
186 int winner_index(-2); 187 int winner_index(-2);
187 std::string winner_name; 188 std::string winner_name;
188 189
189 for (int i = 1; i <= group_count; ++i) { 190 for (int i = 1; i <= group_count; ++i) {
190 int might_win = trial->AppendGroup("", 1); 191 int might_win = trial->AppendGroup("", 1);
191 192
192 // Because we keep appending groups, we want to see if the last group that 193 // Because we keep appending groups, we want to see if the last group that
193 // was added has been assigned or not. 194 // was added has been assigned or not.
194 if (trial->group_ == might_win) { 195 if (trial->group_ == might_win) {
195 EXPECT_EQ(-2, winner_index); 196 EXPECT_EQ(-2, winner_index);
196 winner_index = might_win; 197 winner_index = might_win;
197 StringAppendF(&winner_name, "%d", might_win); 198 StringAppendF(&winner_name, "%d", might_win);
198 EXPECT_EQ(winner_name, trial->group_name()); 199 EXPECT_EQ(winner_name, trial->group_name());
199 } 200 }
200 } 201 }
201 EXPECT_GE(winner_index, 0); 202 EXPECT_GE(winner_index, 0);
203 // Since all groups cover the total probability, we should not have
204 // chosen the default group.
205 EXPECT_NE(trial->group(), default_group_number);
202 EXPECT_EQ(trial->group(), winner_index); 206 EXPECT_EQ(trial->group(), winner_index);
203 EXPECT_EQ(trial->group_name(), winner_name); 207 EXPECT_EQ(trial->group_name(), winner_name);
204 } 208 }
205 209
206 TEST_F(FieldTrialTest, DisableProbability) { 210 TEST_F(FieldTrialTest, DisableProbability) {
207 const std::string default_group_name = "Default group"; 211 const std::string default_group_name = "Default group";
208 const std::string loser = "Loser"; 212 const std::string loser = "Loser";
209 const std::string name = "Trial"; 213 const std::string name = "Trial";
210 214
211 // Create a field trail that has expired. 215 // Create a field trail that has expired.
216 int default_group_number = -1;
212 scoped_refptr<FieldTrial> trial; 217 scoped_refptr<FieldTrial> trial;
213 trial = new FieldTrial( 218 trial = FieldTrialList::FactoryGetFieldTrial(
214 name, 1000000000, default_group_name, last_year_, 1, 1); 219 name, 1000000000, default_group_name, last_year_, 1, 1,
220 &default_group_number);
215 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen. 221 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen.
216 222
217 // Because trial has expired, we should always be in the default group. 223 // Because trial has expired, we should always be in the default group.
218 EXPECT_EQ(FieldTrial::kDefaultGroupNumber, trial->group()); 224 EXPECT_EQ(default_group_number, trial->group());
219 225
220 // And that default_group_name should ALWAYS win. 226 // And that default_group_name should ALWAYS win.
221 EXPECT_EQ(default_group_name, trial->group_name()); 227 EXPECT_EQ(default_group_name, trial->group_name());
222 } 228 }
223 229
224 TEST_F(FieldTrialTest, HashName) { 230 TEST_F(FieldTrialTest, HashName) {
225 // Make sure hashing is stable on all platforms. 231 // Make sure hashing is stable on all platforms.
226 struct { 232 struct {
227 const char* name; 233 const char* name;
228 uint32 hash_value; 234 uint32 hash_value;
229 } known_hashes[] = { 235 } known_hashes[] = {
230 {"a", 937752454u}, 236 {"a", 937752454u},
231 {"1", 723085877u}, 237 {"1", 723085877u},
232 {"Trial Name", 2713117220u}, 238 {"Trial Name", 2713117220u},
233 {"Group Name", 3201815843u}, 239 {"Group Name", 3201815843u},
234 {"My Favorite Experiment", 3722155194u}, 240 {"My Favorite Experiment", 3722155194u},
235 {"My Awesome Group Name", 4109503236u}, 241 {"My Awesome Group Name", 4109503236u},
236 {"abcdefghijklmonpqrstuvwxyz", 787728696u}, 242 {"abcdefghijklmonpqrstuvwxyz", 787728696u},
237 {"0123456789ABCDEF", 348858318U} 243 {"0123456789ABCDEF", 348858318U}
238 }; 244 };
239 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) { 245 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) {
240 EXPECT_EQ(known_hashes[i].hash_value, 246 EXPECT_EQ(known_hashes[i].hash_value,
241 FieldTrial::HashName(known_hashes[i].name)); 247 FieldTrial::HashName(known_hashes[i].name));
242 } 248 }
243 } 249 }
244 250
245 TEST_F(FieldTrialTest, NameGroupIds) { 251 TEST_F(FieldTrialTest, NameGroupIds) {
246 std::string no_group("No Group"); 252 std::string no_group("No Group");
247 uint32 no_group_id = FieldTrial::HashName(no_group); 253 uint32 no_group_id = FieldTrial::HashName(no_group);
248 scoped_refptr<FieldTrial> trial(new FieldTrial( 254 scoped_refptr<FieldTrial> trial(FieldTrialList::FactoryGetFieldTrial(
249 no_group, 10, "Default", next_year_, 12, 31)); 255 no_group, 10, "Default", next_year_, 12, 31, NULL));
250 256
251 // There is no winner yet, so no NameGroupId should be returned. 257 // There is no winner yet, so no NameGroupId should be returned.
252 FieldTrial::NameGroupId name_group_id; 258 FieldTrial::NameGroupId name_group_id;
253 EXPECT_FALSE(trial->GetNameGroupId(&name_group_id)); 259 EXPECT_FALSE(trial->GetNameGroupId(&name_group_id));
254 260
255 // Create a single winning group. 261 // Create a single winning group.
256 std::string one_winner("One Winner"); 262 std::string one_winner("One Winner");
257 uint32 one_winner_id = FieldTrial::HashName(one_winner); 263 uint32 one_winner_id = FieldTrial::HashName(one_winner);
258 trial = new FieldTrial(one_winner, 10, "Default", next_year_, 12, 31); 264 trial = FieldTrialList::FactoryGetFieldTrial(
265 one_winner, 10, "Default", next_year_, 12, 31, NULL);
259 std::string winner("Winner"); 266 std::string winner("Winner");
260 uint32 winner_group_id = FieldTrial::HashName(winner); 267 uint32 winner_group_id = FieldTrial::HashName(winner);
261 trial->AppendGroup(winner, 10); 268 trial->AppendGroup(winner, 10);
262 EXPECT_TRUE(trial->GetNameGroupId(&name_group_id)); 269 EXPECT_TRUE(trial->GetNameGroupId(&name_group_id));
263 EXPECT_EQ(one_winner_id, name_group_id.name); 270 EXPECT_EQ(one_winner_id, name_group_id.name);
264 EXPECT_EQ(winner_group_id, name_group_id.group); 271 EXPECT_EQ(winner_group_id, name_group_id.group);
265 272
266 std::string multi_group("MultiGroup"); 273 std::string multi_group("MultiGroup");
267 uint32 multi_group_id = FieldTrial::HashName(multi_group); 274 uint32 multi_group_id = FieldTrial::HashName(multi_group);
268 scoped_refptr<FieldTrial> multi_group_trial = 275 scoped_refptr<FieldTrial> multi_group_trial =
269 new FieldTrial(multi_group, 9, "Default", next_year_, 12, 31); 276 FieldTrialList::FactoryGetFieldTrial(multi_group, 9, "Default",
277 next_year_, 12, 31, NULL);
270 278
271 multi_group_trial->AppendGroup("Me", 3); 279 multi_group_trial->AppendGroup("Me", 3);
272 multi_group_trial->AppendGroup("You", 3); 280 multi_group_trial->AppendGroup("You", 3);
273 multi_group_trial->AppendGroup("Them", 3); 281 multi_group_trial->AppendGroup("Them", 3);
274 EXPECT_TRUE(multi_group_trial->GetNameGroupId(&name_group_id)); 282 EXPECT_TRUE(multi_group_trial->GetNameGroupId(&name_group_id));
275 EXPECT_EQ(multi_group_id, name_group_id.name); 283 EXPECT_EQ(multi_group_id, name_group_id.name);
276 uint32 multi_group_winner_id = 284 uint32 multi_group_winner_id =
277 FieldTrial::HashName(multi_group_trial->group_name()); 285 FieldTrial::HashName(multi_group_trial->group_name());
278 EXPECT_EQ(multi_group_winner_id, name_group_id.group); 286 EXPECT_EQ(multi_group_winner_id, name_group_id.group);
279 287
280 // Now check if the list is built properly... 288 // Now check if the list is built properly...
281 std::vector<FieldTrial::NameGroupId> name_group_ids; 289 std::vector<FieldTrial::NameGroupId> name_group_ids;
282 FieldTrialList::GetFieldTrialNameGroupIds(&name_group_ids); 290 FieldTrialList::GetFieldTrialNameGroupIds(&name_group_ids);
283 EXPECT_EQ(2U, name_group_ids.size()); 291 EXPECT_EQ(2U, name_group_ids.size());
284 for (size_t i = 0; i < name_group_ids.size(); ++i) { 292 for (size_t i = 0; i < name_group_ids.size(); ++i) {
285 // Order is not guaranteed, so check all values. 293 // Order is not guaranteed, so check all values.
286 EXPECT_NE(no_group_id, name_group_ids[i].name); 294 EXPECT_NE(no_group_id, name_group_ids[i].name);
287 EXPECT_TRUE(one_winner_id != name_group_ids[i].name || 295 EXPECT_TRUE(one_winner_id != name_group_ids[i].name ||
288 winner_group_id == name_group_ids[i].group); 296 winner_group_id == name_group_ids[i].group);
289 EXPECT_TRUE(multi_group_id != name_group_ids[i].name || 297 EXPECT_TRUE(multi_group_id != name_group_ids[i].name ||
290 multi_group_winner_id == name_group_ids[i].group); 298 multi_group_winner_id == name_group_ids[i].group);
291 } 299 }
292 } 300 }
293 301
294 TEST_F(FieldTrialTest, Save) { 302 TEST_F(FieldTrialTest, Save) {
295 std::string save_string; 303 std::string save_string;
296 304
297 FieldTrial* trial = 305 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
298 new FieldTrial( 306 "Some name", 10, "Default some name", next_year_, 12, 31, NULL);
299 "Some name", 10, "Default some name", next_year_, 12, 31);
300 // There is no winner yet, so no textual group name is associated with trial. 307 // There is no winner yet, so no textual group name is associated with trial.
301 // In this case, the trial should not be included. 308 // In this case, the trial should not be included.
302 EXPECT_EQ("", trial->group_name_internal()); 309 EXPECT_EQ("", trial->group_name_internal());
303 FieldTrialList::StatesToString(&save_string); 310 FieldTrialList::StatesToString(&save_string);
304 EXPECT_EQ("", save_string); 311 EXPECT_EQ("", save_string);
305 save_string.clear(); 312 save_string.clear();
306 313
307 // Create a winning group. 314 // Create a winning group.
308 trial->AppendGroup("Winner", 10); 315 trial->AppendGroup("Winner", 10);
309 FieldTrialList::StatesToString(&save_string); 316 FieldTrialList::StatesToString(&save_string);
310 EXPECT_EQ("Some name/Winner/", save_string); 317 EXPECT_EQ("Some name/Winner/", save_string);
311 save_string.clear(); 318 save_string.clear();
312 319
313 // Create a second trial and winning group. 320 // Create a second trial and winning group.
314 FieldTrial* trial2 = 321 FieldTrial* trial2 = FieldTrialList::FactoryGetFieldTrial(
315 new FieldTrial("xxx", 10, "Default xxx", next_year_, 12, 31); 322 "xxx", 10, "Default xxx", next_year_, 12, 31, NULL);
316 trial2->AppendGroup("yyyy", 10); 323 trial2->AppendGroup("yyyy", 10);
317 324
318 FieldTrialList::StatesToString(&save_string); 325 FieldTrialList::StatesToString(&save_string);
319 // We assume names are alphabetized... though this is not critical. 326 // We assume names are alphabetized... though this is not critical.
320 EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string); 327 EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string);
321 } 328 }
322 329
323 TEST_F(FieldTrialTest, Restore) { 330 TEST_F(FieldTrialTest, Restore) {
324 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL); 331 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL);
325 EXPECT_TRUE(FieldTrialList::Find("xxx") == NULL); 332 EXPECT_TRUE(FieldTrialList::Find("xxx") == NULL);
326 333
327 FieldTrialList::CreateTrialsInChildProcess("Some_name/Winner/xxx/yyyy/"); 334 FieldTrialList::CreateTrialsFromString("Some_name/Winner/xxx/yyyy/");
328 335
329 FieldTrial* trial = FieldTrialList::Find("Some_name"); 336 FieldTrial* trial = FieldTrialList::Find("Some_name");
330 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); 337 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
331 EXPECT_EQ("Winner", trial->group_name()); 338 EXPECT_EQ("Winner", trial->group_name());
332 EXPECT_EQ("Some_name", trial->name()); 339 EXPECT_EQ("Some_name", trial->name());
333 340
334 trial = FieldTrialList::Find("xxx"); 341 trial = FieldTrialList::Find("xxx");
335 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); 342 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
336 EXPECT_EQ("yyyy", trial->group_name()); 343 EXPECT_EQ("yyyy", trial->group_name());
337 EXPECT_EQ("xxx", trial->name()); 344 EXPECT_EQ("xxx", trial->name());
338 } 345 }
339 346
340 TEST_F(FieldTrialTest, BogusRestore) { 347 TEST_F(FieldTrialTest, BogusRestore) {
341 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingSlash")); 348 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("MissingSlash"));
342 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess("MissingGroupName/")); 349 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("MissingGroupName/"));
343 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess( 350 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
344 "MissingFinalSlash/gname")); 351 "MissingFinalSlash/gname"));
345 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess( 352 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString(
346 "noname, only group/")); 353 "noname, only group/"));
347 } 354 }
348 355
349 TEST_F(FieldTrialTest, DuplicateRestore) { 356 TEST_F(FieldTrialTest, DuplicateRestore) {
350 FieldTrial* trial = 357 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
351 new FieldTrial( 358 "Some name", 10, "Default some name", next_year_, 12, 31, NULL);
352 "Some name", 10, "Default some name", next_year_, 12, 31);
353 trial->AppendGroup("Winner", 10); 359 trial->AppendGroup("Winner", 10);
354 std::string save_string; 360 std::string save_string;
355 FieldTrialList::StatesToString(&save_string); 361 FieldTrialList::StatesToString(&save_string);
356 EXPECT_EQ("Some name/Winner/", save_string); 362 EXPECT_EQ("Some name/Winner/", save_string);
357 363
358 // It is OK if we redundantly specify a winner. 364 // It is OK if we redundantly specify a winner.
359 EXPECT_TRUE(FieldTrialList::CreateTrialsInChildProcess(save_string)); 365 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString(save_string));
360 366
361 // But it is an error to try to change to a different winner. 367 // But it is an error to try to change to a different winner.
362 EXPECT_FALSE(FieldTrialList::CreateTrialsInChildProcess( 368 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString("Some name/Loser/"));
363 "Some name/Loser/"));
364 } 369 }
365 370
366 TEST_F(FieldTrialTest, CreateFieldTrial) { 371 TEST_F(FieldTrialTest, CreateFieldTrial) {
367 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL); 372 EXPECT_TRUE(FieldTrialList::Find("Some_name") == NULL);
368 373
369 FieldTrialList::CreateFieldTrial("Some_name", "Winner"); 374 FieldTrialList::CreateFieldTrial("Some_name", "Winner");
370 375
371 FieldTrial* trial = FieldTrialList::Find("Some_name"); 376 FieldTrial* trial = FieldTrialList::Find("Some_name");
372 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial); 377 ASSERT_NE(static_cast<FieldTrial*>(NULL), trial);
373 EXPECT_EQ("Winner", trial->group_name()); 378 EXPECT_EQ("Winner", trial->group_name());
374 EXPECT_EQ("Some_name", trial->name()); 379 EXPECT_EQ("Some_name", trial->name());
375 } 380 }
376 381
377 TEST_F(FieldTrialTest, DuplicateFieldTrial) { 382 TEST_F(FieldTrialTest, DuplicateFieldTrial) {
378 FieldTrial* trial = 383 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
379 new FieldTrial( 384 "Some_name", 10, "Default some name", next_year_, 12, 31, NULL);
380 "Some_name", 10, "Default some name", next_year_, 12, 31);
381 trial->AppendGroup("Winner", 10); 385 trial->AppendGroup("Winner", 10);
382 386
383 // It is OK if we redundantly specify a winner. 387 // It is OK if we redundantly specify a winner.
384 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner"); 388 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner");
385 EXPECT_TRUE(trial1 != NULL); 389 EXPECT_TRUE(trial1 != NULL);
386 390
387 // But it is an error to try to change to a different winner. 391 // But it is an error to try to change to a different winner.
388 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser"); 392 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser");
389 EXPECT_TRUE(trial2 == NULL); 393 EXPECT_TRUE(trial2 == NULL);
390 } 394 }
391 395
392 TEST_F(FieldTrialTest, MakeName) { 396 TEST_F(FieldTrialTest, MakeName) {
393 FieldTrial* trial = 397 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
394 new FieldTrial("Field Trial", 10, "Winner", next_year_, 12, 31); 398 "Field Trial", 10, "Winner", next_year_, 12, 31, NULL);
395 trial->group(); 399 trial->group();
396 EXPECT_EQ("Histogram_Winner", 400 EXPECT_EQ("Histogram_Winner",
397 FieldTrial::MakeName("Histogram", "Field Trial")); 401 FieldTrial::MakeName("Histogram", "Field Trial"));
398 } 402 }
399 403
400 TEST_F(FieldTrialTest, HashClientId) { 404 TEST_F(FieldTrialTest, HashClientId) {
401 double results[] = { 405 double results[] = {
402 FieldTrial::HashClientId("hi", "1"), 406 FieldTrial::HashClientId("hi", "1"),
403 FieldTrial::HashClientId("there", "1"), 407 FieldTrial::HashClientId("there", "1"),
404 }; 408 };
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 } 464 }
461 465
462 TEST_F(FieldTrialTest, UseOneTimeRandomization) { 466 TEST_F(FieldTrialTest, UseOneTimeRandomization) {
463 // Simply asserts that two trials using one-time randomization 467 // Simply asserts that two trials using one-time randomization
464 // that have different names, normally generate different results. 468 // that have different names, normally generate different results.
465 // 469 //
466 // Note that depending on the one-time random initialization, they 470 // Note that depending on the one-time random initialization, they
467 // _might_ actually give the same result, but we know that given 471 // _might_ actually give the same result, but we know that given
468 // the particular client_id we use for unit tests they won't. 472 // the particular client_id we use for unit tests they won't.
469 scoped_refptr<FieldTrial> trials[] = { 473 scoped_refptr<FieldTrial> trials[] = {
470 new FieldTrial("one", 100, "default", next_year_, 1, 1), 474 FieldTrialList::FactoryGetFieldTrial("one", 100, "default",
471 new FieldTrial("two", 100, "default", next_year_, 1, 1), 475 next_year_, 1, 1, NULL),
476 FieldTrialList::FactoryGetFieldTrial("two", 100, "default",
477 next_year_, 1, 1, NULL),
472 }; 478 };
473 479
474 for (size_t i = 0; i < arraysize(trials); ++i) { 480 for (size_t i = 0; i < arraysize(trials); ++i) {
475 trials[i]->UseOneTimeRandomization(); 481 trials[i]->UseOneTimeRandomization();
476 482
477 for (int j = 0; j < 100; ++j) { 483 for (int j = 0; j < 100; ++j) {
478 trials[i]->AppendGroup("", 1); 484 trials[i]->AppendGroup("", 1);
479 } 485 }
480 } 486 }
481 487
482 // The trials are most likely to give different results since they have 488 // The trials are most likely to give different results since they have
483 // different names. 489 // different names.
484 ASSERT_NE(trials[0]->group(), trials[1]->group()); 490 ASSERT_NE(trials[0]->group(), trials[1]->group());
485 ASSERT_NE(trials[0]->group_name(), trials[1]->group_name()); 491 ASSERT_NE(trials[0]->group_name(), trials[1]->group_name());
486 } 492 }
487 493
488 TEST_F(FieldTrialTest, DisableImmediately) { 494 TEST_F(FieldTrialTest, DisableImmediately) {
489 FieldTrial* trial = 495 int default_group_number = -1;
490 new FieldTrial("trial", 100, "default", next_year_, 12, 31); 496 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial(
497 "trial", 100, "default", next_year_, 12, 31, &default_group_number);
491 trial->Disable(); 498 trial->Disable();
492 ASSERT_EQ("default", trial->group_name()); 499 ASSERT_EQ("default", trial->group_name());
493 ASSERT_EQ(FieldTrial::kDefaultGroupNumber, trial->group()); 500 ASSERT_EQ(default_group_number, trial->group());
494 } 501 }
495 502
496 TEST_F(FieldTrialTest, DisableAfterInitialization) { 503 TEST_F(FieldTrialTest, DisableAfterInitialization) {
497 FieldTrial* trial = 504 FieldTrial* trial =
498 new FieldTrial("trial", 100, "default", next_year_, 12, 31); 505 FieldTrialList::FactoryGetFieldTrial("trial", 100, "default",
506 next_year_, 12, 31, NULL);
499 trial->AppendGroup("non_default", 100); 507 trial->AppendGroup("non_default", 100);
500 ASSERT_EQ("non_default", trial->group_name()); 508 ASSERT_EQ("non_default", trial->group_name());
501 trial->Disable(); 509 trial->Disable();
502 ASSERT_EQ("default", trial->group_name()); 510 ASSERT_EQ("default", trial->group_name());
503 } 511 }
504 512
513 TEST_F(FieldTrialTest, ForcedFieldTrials) {
514 // Validate we keep the forced choice.
515 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Use the",
516 "Force");
517 EXPECT_STREQ("Force", forced_trial->group_name().c_str());
518
519 int default_group_number = -1;
520 FieldTrial* factory_trial = FieldTrialList::FactoryGetFieldTrial(
521 "Use the", 1000, "default", next_year_, 12, 31, &default_group_number);
522 EXPECT_EQ(factory_trial, forced_trial);
523
524 int chosen_group = factory_trial->AppendGroup("Force", 100);
525 EXPECT_EQ(chosen_group, factory_trial->group());
526 int not_chosen_group = factory_trial->AppendGroup("Dark Side", 100);
527 EXPECT_NE(chosen_group, not_chosen_group);
528
529 // Since we didn't force the default group, we should not be returned the
530 // chosen group as the default group.
531 EXPECT_NE(default_group_number, chosen_group);
532 int new_group = factory_trial->AppendGroup("Duck Tape", 800);
533 EXPECT_NE(chosen_group, new_group);
534 // The new group should not be the default group either.
535 EXPECT_NE(default_group_number, new_group);
536
537 // Forcing the default should use the proper group ID.
538 forced_trial = FieldTrialList::CreateFieldTrial("Trial Name", "Default");
539 factory_trial = FieldTrialList::FactoryGetFieldTrial(
540 "Trial Name", 1000, "Default", next_year_, 12, 31, &default_group_number);
541 EXPECT_EQ(forced_trial, factory_trial);
542
543 int other_group = factory_trial->AppendGroup("Not Default", 100);
544 EXPECT_STREQ("Default", factory_trial->group_name().c_str());
545 EXPECT_EQ(default_group_number, factory_trial->group());
546 EXPECT_NE(other_group, factory_trial->group());
547
548 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800);
549 EXPECT_NE(new_other_group, factory_trial->group());
550 }
551
505 } // namespace base 552 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/field_trial.cc ('k') | chrome/browser/autocomplete/autocomplete_field_trial.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698