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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 }; | 76 }; |
77 | 77 |
78 // Test registration, and also check that destructors are called for trials | 78 // Test registration, and also check that destructors are called for trials |
79 // (and that Valgrind doesn't catch us leaking). | 79 // (and that Valgrind doesn't catch us leaking). |
80 TEST_F(FieldTrialTest, Registration) { | 80 TEST_F(FieldTrialTest, Registration) { |
81 const char* name1 = "name 1 test"; | 81 const char* name1 = "name 1 test"; |
82 const char* name2 = "name 2 test"; | 82 const char* name2 = "name 2 test"; |
83 EXPECT_FALSE(FieldTrialList::Find(name1)); | 83 EXPECT_FALSE(FieldTrialList::Find(name1)); |
84 EXPECT_FALSE(FieldTrialList::Find(name2)); | 84 EXPECT_FALSE(FieldTrialList::Find(name2)); |
85 | 85 |
86 FieldTrial* trial1 = CreateFieldTrial(name1, 10, "default name 1 test", NULL); | 86 scoped_refptr<FieldTrial> trial1 = |
| 87 CreateFieldTrial(name1, 10, "default name 1 test", NULL); |
87 EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_); | 88 EXPECT_EQ(FieldTrial::kNotFinalized, trial1->group_); |
88 EXPECT_EQ(name1, trial1->trial_name()); | 89 EXPECT_EQ(name1, trial1->trial_name()); |
89 EXPECT_EQ("", trial1->group_name_internal()); | 90 EXPECT_EQ("", trial1->group_name_internal()); |
90 | 91 |
91 trial1->AppendGroup(std::string(), 7); | 92 trial1->AppendGroup(std::string(), 7); |
92 | 93 |
93 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); | 94 EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1)); |
94 EXPECT_FALSE(FieldTrialList::Find(name2)); | 95 EXPECT_FALSE(FieldTrialList::Find(name2)); |
95 | 96 |
96 FieldTrial* trial2 = CreateFieldTrial(name2, 10, "default name 2 test", NULL); | 97 scoped_refptr<FieldTrial> trial2 = |
| 98 CreateFieldTrial(name2, 10, "default name 2 test", NULL); |
97 EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_); | 99 EXPECT_EQ(FieldTrial::kNotFinalized, trial2->group_); |
98 EXPECT_EQ(name2, trial2->trial_name()); | 100 EXPECT_EQ(name2, trial2->trial_name()); |
99 EXPECT_EQ("", trial2->group_name_internal()); | 101 EXPECT_EQ("", trial2->group_name_internal()); |
100 | 102 |
101 trial2->AppendGroup("a first group", 7); | 103 trial2->AppendGroup("a first group", 7); |
102 | 104 |
103 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); | 105 EXPECT_EQ(trial1.get(), FieldTrialList::Find(name1)); |
104 EXPECT_EQ(trial2, FieldTrialList::Find(name2)); | 106 EXPECT_EQ(trial2.get(), FieldTrialList::Find(name2)); |
105 // Note: FieldTrialList should delete the objects at shutdown. | 107 // Note: FieldTrialList should delete the objects at shutdown. |
106 } | 108 } |
107 | 109 |
108 TEST_F(FieldTrialTest, AbsoluteProbabilities) { | 110 TEST_F(FieldTrialTest, AbsoluteProbabilities) { |
109 char always_true[] = " always true"; | 111 char always_true[] = " always true"; |
110 char default_always_true[] = " default always true"; | 112 char default_always_true[] = " default always true"; |
111 char always_false[] = " always false"; | 113 char always_false[] = " always false"; |
112 char default_always_false[] = " default always false"; | 114 char default_always_false[] = " default always false"; |
113 for (int i = 1; i < 250; ++i) { | 115 for (int i = 1; i < 250; ++i) { |
114 // Try lots of names, by changing the first character of the name. | 116 // Try lots of names, by changing the first character of the name. |
115 always_true[0] = i; | 117 always_true[0] = i; |
116 default_always_true[0] = i; | 118 default_always_true[0] = i; |
117 always_false[0] = i; | 119 always_false[0] = i; |
118 default_always_false[0] = i; | 120 default_always_false[0] = i; |
119 | 121 |
120 FieldTrial* trial_true = | 122 scoped_refptr<FieldTrial> trial_true = |
121 CreateFieldTrial(always_true, 10, default_always_true, NULL); | 123 CreateFieldTrial(always_true, 10, default_always_true, NULL); |
122 const std::string winner = "TheWinner"; | 124 const std::string winner = "TheWinner"; |
123 int winner_group = trial_true->AppendGroup(winner, 10); | 125 int winner_group = trial_true->AppendGroup(winner, 10); |
124 | 126 |
125 EXPECT_EQ(winner_group, trial_true->group()); | 127 EXPECT_EQ(winner_group, trial_true->group()); |
126 EXPECT_EQ(winner, trial_true->group_name()); | 128 EXPECT_EQ(winner, trial_true->group_name()); |
127 | 129 |
128 FieldTrial* trial_false = | 130 scoped_refptr<FieldTrial> trial_false = |
129 CreateFieldTrial(always_false, 10, default_always_false, NULL); | 131 CreateFieldTrial(always_false, 10, default_always_false, NULL); |
130 int loser_group = trial_false->AppendGroup("ALoser", 0); | 132 int loser_group = trial_false->AppendGroup("ALoser", 0); |
131 | 133 |
132 EXPECT_NE(loser_group, trial_false->group()); | 134 EXPECT_NE(loser_group, trial_false->group()); |
133 } | 135 } |
134 } | 136 } |
135 | 137 |
136 TEST_F(FieldTrialTest, RemainingProbability) { | 138 TEST_F(FieldTrialTest, RemainingProbability) { |
137 // First create a test that hasn't had a winner yet. | 139 // First create a test that hasn't had a winner yet. |
138 const std::string winner = "Winner"; | 140 const std::string winner = "Winner"; |
(...skipping 20 matching lines...) Expand all Loading... |
159 // all outcomes are possible. Since this is a 50-50 test, it should get both | 161 // all outcomes are possible. Since this is a 50-50 test, it should get both |
160 // outcomes in a few tries, but we'll try no more than 100 times (and be flaky | 162 // outcomes in a few tries, but we'll try no more than 100 times (and be flaky |
161 // with probability around 1 in 2^99). | 163 // with probability around 1 in 2^99). |
162 bool first_winner = false; | 164 bool first_winner = false; |
163 bool second_winner = false; | 165 bool second_winner = false; |
164 int counter = 0; | 166 int counter = 0; |
165 do { | 167 do { |
166 std::string name = base::StringPrintf("FiftyFifty%d", ++counter); | 168 std::string name = base::StringPrintf("FiftyFifty%d", ++counter); |
167 std::string default_group_name = base::StringPrintf("Default FiftyFifty%d", | 169 std::string default_group_name = base::StringPrintf("Default FiftyFifty%d", |
168 ++counter); | 170 ++counter); |
169 FieldTrial* trial = CreateFieldTrial(name, 2, default_group_name, NULL); | 171 scoped_refptr<FieldTrial> trial = |
| 172 CreateFieldTrial(name, 2, default_group_name, NULL); |
170 trial->AppendGroup("first", 1); // 50% chance of being chosen. | 173 trial->AppendGroup("first", 1); // 50% chance of being chosen. |
171 // If group_ is kNotFinalized, then a group assignement hasn't been done. | 174 // If group_ is kNotFinalized, then a group assignement hasn't been done. |
172 if (trial->group_ != FieldTrial::kNotFinalized) { | 175 if (trial->group_ != FieldTrial::kNotFinalized) { |
173 first_winner = true; | 176 first_winner = true; |
174 continue; | 177 continue; |
175 } | 178 } |
176 trial->AppendGroup("second", 1); // Always chosen at this point. | 179 trial->AppendGroup("second", 1); // Always chosen at this point. |
177 EXPECT_NE(FieldTrial::kNotFinalized, trial->group()); | 180 EXPECT_NE(FieldTrial::kNotFinalized, trial->group()); |
178 second_winner = true; | 181 second_winner = true; |
179 } while ((!second_winner || !first_winner) && counter < 100); | 182 } while ((!second_winner || !first_winner) && counter < 100); |
180 EXPECT_TRUE(second_winner); | 183 EXPECT_TRUE(second_winner); |
181 EXPECT_TRUE(first_winner); | 184 EXPECT_TRUE(first_winner); |
182 } | 185 } |
183 | 186 |
184 TEST_F(FieldTrialTest, MiddleProbabilities) { | 187 TEST_F(FieldTrialTest, MiddleProbabilities) { |
185 char name[] = " same name"; | 188 char name[] = " same name"; |
186 char default_group_name[] = " default same name"; | 189 char default_group_name[] = " default same name"; |
187 bool false_event_seen = false; | 190 bool false_event_seen = false; |
188 bool true_event_seen = false; | 191 bool true_event_seen = false; |
189 for (int i = 1; i < 250; ++i) { | 192 for (int i = 1; i < 250; ++i) { |
190 name[0] = i; | 193 name[0] = i; |
191 default_group_name[0] = i; | 194 default_group_name[0] = i; |
192 FieldTrial* trial = CreateFieldTrial(name, 10, default_group_name, NULL); | 195 scoped_refptr<FieldTrial> trial = |
| 196 CreateFieldTrial(name, 10, default_group_name, NULL); |
193 int might_win = trial->AppendGroup("MightWin", 5); | 197 int might_win = trial->AppendGroup("MightWin", 5); |
194 | 198 |
195 if (trial->group() == might_win) { | 199 if (trial->group() == might_win) { |
196 true_event_seen = true; | 200 true_event_seen = true; |
197 } else { | 201 } else { |
198 false_event_seen = true; | 202 false_event_seen = true; |
199 } | 203 } |
200 if (false_event_seen && true_event_seen) | 204 if (false_event_seen && true_event_seen) |
201 return; // Successful test!!! | 205 return; // Successful test!!! |
202 } | 206 } |
203 // Very surprising to get here. Probability should be around 1 in 2 ** 250. | 207 // Very surprising to get here. Probability should be around 1 in 2 ** 250. |
204 // One of the following will fail. | 208 // One of the following will fail. |
205 EXPECT_TRUE(false_event_seen); | 209 EXPECT_TRUE(false_event_seen); |
206 EXPECT_TRUE(true_event_seen); | 210 EXPECT_TRUE(true_event_seen); |
207 } | 211 } |
208 | 212 |
209 TEST_F(FieldTrialTest, OneWinner) { | 213 TEST_F(FieldTrialTest, OneWinner) { |
210 char name[] = "Some name"; | 214 char name[] = "Some name"; |
211 char default_group_name[] = "Default some name"; | 215 char default_group_name[] = "Default some name"; |
212 int group_count(10); | 216 int group_count(10); |
213 | 217 |
214 int default_group_number = -1; | 218 int default_group_number = -1; |
215 FieldTrial* trial = | 219 scoped_refptr<FieldTrial> trial = |
216 CreateFieldTrial(name, group_count, default_group_name, NULL); | 220 CreateFieldTrial(name, group_count, default_group_name, NULL); |
217 int winner_index(-2); | 221 int winner_index(-2); |
218 std::string winner_name; | 222 std::string winner_name; |
219 | 223 |
220 for (int i = 1; i <= group_count; ++i) { | 224 for (int i = 1; i <= group_count; ++i) { |
221 int might_win = trial->AppendGroup(std::string(), 1); | 225 int might_win = trial->AppendGroup(std::string(), 1); |
222 | 226 |
223 // Because we keep appending groups, we want to see if the last group that | 227 // Because we keep appending groups, we want to see if the last group that |
224 // was added has been assigned or not. | 228 // was added has been assigned or not. |
225 if (trial->group_ == might_win) { | 229 if (trial->group_ == might_win) { |
(...skipping 26 matching lines...) Expand all Loading... |
252 | 256 |
253 // Because trial has expired, we should always be in the default group. | 257 // Because trial has expired, we should always be in the default group. |
254 EXPECT_EQ(default_group_number, trial->group()); | 258 EXPECT_EQ(default_group_number, trial->group()); |
255 | 259 |
256 // And that default_group_name should ALWAYS win. | 260 // And that default_group_name should ALWAYS win. |
257 EXPECT_EQ(default_group_name, trial->group_name()); | 261 EXPECT_EQ(default_group_name, trial->group_name()); |
258 } | 262 } |
259 | 263 |
260 TEST_F(FieldTrialTest, ActiveGroups) { | 264 TEST_F(FieldTrialTest, ActiveGroups) { |
261 std::string no_group("No Group"); | 265 std::string no_group("No Group"); |
262 FieldTrial* trial = CreateFieldTrial(no_group, 10, "Default", NULL); | 266 scoped_refptr<FieldTrial> trial = |
| 267 CreateFieldTrial(no_group, 10, "Default", NULL); |
263 | 268 |
264 // There is no winner yet, so no NameGroupId should be returned. | 269 // There is no winner yet, so no NameGroupId should be returned. |
265 FieldTrial::ActiveGroup active_group; | 270 FieldTrial::ActiveGroup active_group; |
266 EXPECT_FALSE(trial->GetActiveGroup(&active_group)); | 271 EXPECT_FALSE(trial->GetActiveGroup(&active_group)); |
267 | 272 |
268 // Create a single winning group. | 273 // Create a single winning group. |
269 std::string one_winner("One Winner"); | 274 std::string one_winner("One Winner"); |
270 trial = CreateFieldTrial(one_winner, 10, "Default", NULL); | 275 trial = CreateFieldTrial(one_winner, 10, "Default", NULL); |
271 std::string winner("Winner"); | 276 std::string winner("Winner"); |
272 trial->AppendGroup(winner, 10); | 277 trial->AppendGroup(winner, 10); |
273 EXPECT_FALSE(trial->GetActiveGroup(&active_group)); | 278 EXPECT_FALSE(trial->GetActiveGroup(&active_group)); |
274 // Finalize the group selection by accessing the selected group. | 279 // Finalize the group selection by accessing the selected group. |
275 trial->group(); | 280 trial->group(); |
276 EXPECT_TRUE(trial->GetActiveGroup(&active_group)); | 281 EXPECT_TRUE(trial->GetActiveGroup(&active_group)); |
277 EXPECT_EQ(one_winner, active_group.trial_name); | 282 EXPECT_EQ(one_winner, active_group.trial_name); |
278 EXPECT_EQ(winner, active_group.group_name); | 283 EXPECT_EQ(winner, active_group.group_name); |
279 | 284 |
280 std::string multi_group("MultiGroup"); | 285 std::string multi_group("MultiGroup"); |
281 FieldTrial* multi_group_trial = | 286 scoped_refptr<FieldTrial> multi_group_trial = |
282 CreateFieldTrial(multi_group, 9, "Default", NULL); | 287 CreateFieldTrial(multi_group, 9, "Default", NULL); |
283 | 288 |
284 multi_group_trial->AppendGroup("Me", 3); | 289 multi_group_trial->AppendGroup("Me", 3); |
285 multi_group_trial->AppendGroup("You", 3); | 290 multi_group_trial->AppendGroup("You", 3); |
286 multi_group_trial->AppendGroup("Them", 3); | 291 multi_group_trial->AppendGroup("Them", 3); |
287 EXPECT_FALSE(multi_group_trial->GetActiveGroup(&active_group)); | 292 EXPECT_FALSE(multi_group_trial->GetActiveGroup(&active_group)); |
288 // Finalize the group selection by accessing the selected group. | 293 // Finalize the group selection by accessing the selected group. |
289 multi_group_trial->group(); | 294 multi_group_trial->group(); |
290 EXPECT_TRUE(multi_group_trial->GetActiveGroup(&active_group)); | 295 EXPECT_TRUE(multi_group_trial->GetActiveGroup(&active_group)); |
291 EXPECT_EQ(multi_group, active_group.trial_name); | 296 EXPECT_EQ(multi_group, active_group.trial_name); |
(...skipping 11 matching lines...) Expand all Loading... |
303 EXPECT_TRUE(multi_group != active_groups[i].trial_name || | 308 EXPECT_TRUE(multi_group != active_groups[i].trial_name || |
304 multi_group_trial->group_name() == active_groups[i].group_name); | 309 multi_group_trial->group_name() == active_groups[i].group_name); |
305 } | 310 } |
306 } | 311 } |
307 | 312 |
308 TEST_F(FieldTrialTest, ActiveGroupsNotFinalized) { | 313 TEST_F(FieldTrialTest, ActiveGroupsNotFinalized) { |
309 const char kTrialName[] = "TestTrial"; | 314 const char kTrialName[] = "TestTrial"; |
310 const char kSecondaryGroupName[] = "SecondaryGroup"; | 315 const char kSecondaryGroupName[] = "SecondaryGroup"; |
311 | 316 |
312 int default_group = -1; | 317 int default_group = -1; |
313 FieldTrial* trial = | 318 scoped_refptr<FieldTrial> trial = |
314 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 319 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
315 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50); | 320 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50); |
316 | 321 |
317 // Before |group()| is called, |GetActiveGroup()| should return false. | 322 // Before |group()| is called, |GetActiveGroup()| should return false. |
318 FieldTrial::ActiveGroup active_group; | 323 FieldTrial::ActiveGroup active_group; |
319 EXPECT_FALSE(trial->GetActiveGroup(&active_group)); | 324 EXPECT_FALSE(trial->GetActiveGroup(&active_group)); |
320 | 325 |
321 // |GetActiveFieldTrialGroups()| should also not include the trial. | 326 // |GetActiveFieldTrialGroups()| should also not include the trial. |
322 FieldTrial::ActiveGroups active_groups; | 327 FieldTrial::ActiveGroups active_groups; |
323 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 328 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
(...skipping 12 matching lines...) Expand all Loading... |
336 | 341 |
337 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 342 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
338 ASSERT_EQ(1U, active_groups.size()); | 343 ASSERT_EQ(1U, active_groups.size()); |
339 EXPECT_EQ(kTrialName, active_groups[0].trial_name); | 344 EXPECT_EQ(kTrialName, active_groups[0].trial_name); |
340 EXPECT_EQ(active_group.group_name, active_groups[0].group_name); | 345 EXPECT_EQ(active_group.group_name, active_groups[0].group_name); |
341 } | 346 } |
342 | 347 |
343 TEST_F(FieldTrialTest, Save) { | 348 TEST_F(FieldTrialTest, Save) { |
344 std::string save_string; | 349 std::string save_string; |
345 | 350 |
346 FieldTrial* trial = | 351 scoped_refptr<FieldTrial> trial = |
347 CreateFieldTrial("Some name", 10, "Default some name", NULL); | 352 CreateFieldTrial("Some name", 10, "Default some name", NULL); |
348 // There is no winner yet, so no textual group name is associated with trial. | 353 // There is no winner yet, so no textual group name is associated with trial. |
349 // In this case, the trial should not be included. | 354 // In this case, the trial should not be included. |
350 EXPECT_EQ("", trial->group_name_internal()); | 355 EXPECT_EQ("", trial->group_name_internal()); |
351 FieldTrialList::StatesToString(&save_string); | 356 FieldTrialList::StatesToString(&save_string); |
352 EXPECT_EQ("", save_string); | 357 EXPECT_EQ("", save_string); |
353 save_string.clear(); | 358 save_string.clear(); |
354 | 359 |
355 // Create a winning group. | 360 // Create a winning group. |
356 trial->AppendGroup("Winner", 10); | 361 trial->AppendGroup("Winner", 10); |
357 // Finalize the group selection by accessing the selected group. | 362 // Finalize the group selection by accessing the selected group. |
358 trial->group(); | 363 trial->group(); |
359 FieldTrialList::StatesToString(&save_string); | 364 FieldTrialList::StatesToString(&save_string); |
360 EXPECT_EQ("Some name/Winner/", save_string); | 365 EXPECT_EQ("Some name/Winner/", save_string); |
361 save_string.clear(); | 366 save_string.clear(); |
362 | 367 |
363 // Create a second trial and winning group. | 368 // Create a second trial and winning group. |
364 FieldTrial* trial2 = CreateFieldTrial("xxx", 10, "Default xxx", NULL); | 369 scoped_refptr<FieldTrial> trial2 = |
| 370 CreateFieldTrial("xxx", 10, "Default xxx", NULL); |
365 trial2->AppendGroup("yyyy", 10); | 371 trial2->AppendGroup("yyyy", 10); |
366 // Finalize the group selection by accessing the selected group. | 372 // Finalize the group selection by accessing the selected group. |
367 trial2->group(); | 373 trial2->group(); |
368 | 374 |
369 FieldTrialList::StatesToString(&save_string); | 375 FieldTrialList::StatesToString(&save_string); |
370 // We assume names are alphabetized... though this is not critical. | 376 // We assume names are alphabetized... though this is not critical. |
371 EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string); | 377 EXPECT_EQ("Some name/Winner/xxx/yyyy/", save_string); |
372 save_string.clear(); | 378 save_string.clear(); |
373 | 379 |
374 // Create a third trial with only the default group. | 380 // Create a third trial with only the default group. |
375 FieldTrial* trial3 = CreateFieldTrial("zzz", 10, "default", NULL); | 381 scoped_refptr<FieldTrial> trial3 = |
| 382 CreateFieldTrial("zzz", 10, "default", NULL); |
376 // Finalize the group selection by accessing the selected group. | 383 // Finalize the group selection by accessing the selected group. |
377 trial3->group(); | 384 trial3->group(); |
378 | 385 |
379 FieldTrialList::StatesToString(&save_string); | 386 FieldTrialList::StatesToString(&save_string); |
380 EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string); | 387 EXPECT_EQ("Some name/Winner/xxx/yyyy/zzz/default/", save_string); |
381 } | 388 } |
382 | 389 |
383 TEST_F(FieldTrialTest, Restore) { | 390 TEST_F(FieldTrialTest, Restore) { |
384 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name")); | 391 ASSERT_FALSE(FieldTrialList::TrialExists("Some_name")); |
385 ASSERT_FALSE(FieldTrialList::TrialExists("xxx")); | 392 ASSERT_FALSE(FieldTrialList::TrialExists("xxx")); |
(...skipping 22 matching lines...) Expand all Loading... |
408 std::set<std::string>())); | 415 std::set<std::string>())); |
409 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString( | 416 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString( |
410 "MissingFinalSlash/gname", FieldTrialList::DONT_ACTIVATE_TRIALS, | 417 "MissingFinalSlash/gname", FieldTrialList::DONT_ACTIVATE_TRIALS, |
411 std::set<std::string>())); | 418 std::set<std::string>())); |
412 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString( | 419 EXPECT_FALSE(FieldTrialList::CreateTrialsFromString( |
413 "noname, only group/", FieldTrialList::DONT_ACTIVATE_TRIALS, | 420 "noname, only group/", FieldTrialList::DONT_ACTIVATE_TRIALS, |
414 std::set<std::string>())); | 421 std::set<std::string>())); |
415 } | 422 } |
416 | 423 |
417 TEST_F(FieldTrialTest, DuplicateRestore) { | 424 TEST_F(FieldTrialTest, DuplicateRestore) { |
418 FieldTrial* trial = CreateFieldTrial("Some name", 10, "Default", NULL); | 425 scoped_refptr<FieldTrial> trial = |
| 426 CreateFieldTrial("Some name", 10, "Default", NULL); |
419 trial->AppendGroup("Winner", 10); | 427 trial->AppendGroup("Winner", 10); |
420 // Finalize the group selection by accessing the selected group. | 428 // Finalize the group selection by accessing the selected group. |
421 trial->group(); | 429 trial->group(); |
422 std::string save_string; | 430 std::string save_string; |
423 FieldTrialList::StatesToString(&save_string); | 431 FieldTrialList::StatesToString(&save_string); |
424 EXPECT_EQ("Some name/Winner/", save_string); | 432 EXPECT_EQ("Some name/Winner/", save_string); |
425 | 433 |
426 // It is OK if we redundantly specify a winner. | 434 // It is OK if we redundantly specify a winner. |
427 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString( | 435 EXPECT_TRUE(FieldTrialList::CreateTrialsFromString( |
428 save_string, FieldTrialList::DONT_ACTIVATE_TRIALS, | 436 save_string, FieldTrialList::DONT_ACTIVATE_TRIALS, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 const char kWinnerGroup[] = "Winner"; | 570 const char kWinnerGroup[] = "Winner"; |
563 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 571 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
564 FieldTrialList::CreateFieldTrial(kTrialName, kWinnerGroup); | 572 FieldTrialList::CreateFieldTrial(kTrialName, kWinnerGroup); |
565 | 573 |
566 FieldTrial::ActiveGroups active_groups; | 574 FieldTrial::ActiveGroups active_groups; |
567 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 575 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
568 EXPECT_TRUE(active_groups.empty()); | 576 EXPECT_TRUE(active_groups.empty()); |
569 } | 577 } |
570 | 578 |
571 TEST_F(FieldTrialTest, DuplicateFieldTrial) { | 579 TEST_F(FieldTrialTest, DuplicateFieldTrial) { |
572 FieldTrial* trial = CreateFieldTrial("Some_name", 10, "Default", NULL); | 580 scoped_refptr<FieldTrial> trial = |
| 581 CreateFieldTrial("Some_name", 10, "Default", NULL); |
573 trial->AppendGroup("Winner", 10); | 582 trial->AppendGroup("Winner", 10); |
574 | 583 |
575 // It is OK if we redundantly specify a winner. | 584 // It is OK if we redundantly specify a winner. |
576 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner"); | 585 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("Some_name", "Winner"); |
577 EXPECT_TRUE(trial1 != NULL); | 586 EXPECT_TRUE(trial1 != NULL); |
578 | 587 |
579 // But it is an error to try to change to a different winner. | 588 // But it is an error to try to change to a different winner. |
580 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser"); | 589 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("Some_name", "Loser"); |
581 EXPECT_TRUE(trial2 == NULL); | 590 EXPECT_TRUE(trial2 == NULL); |
582 } | 591 } |
583 | 592 |
584 TEST_F(FieldTrialTest, DisableImmediately) { | 593 TEST_F(FieldTrialTest, DisableImmediately) { |
585 int default_group_number = -1; | 594 int default_group_number = -1; |
586 FieldTrial* trial = | 595 scoped_refptr<FieldTrial> trial = |
587 CreateFieldTrial("trial", 100, "default", &default_group_number); | 596 CreateFieldTrial("trial", 100, "default", &default_group_number); |
588 trial->Disable(); | 597 trial->Disable(); |
589 ASSERT_EQ("default", trial->group_name()); | 598 ASSERT_EQ("default", trial->group_name()); |
590 ASSERT_EQ(default_group_number, trial->group()); | 599 ASSERT_EQ(default_group_number, trial->group()); |
591 } | 600 } |
592 | 601 |
593 TEST_F(FieldTrialTest, DisableAfterInitialization) { | 602 TEST_F(FieldTrialTest, DisableAfterInitialization) { |
594 FieldTrial* trial = CreateFieldTrial("trial", 100, "default", NULL); | 603 scoped_refptr<FieldTrial> trial = |
| 604 CreateFieldTrial("trial", 100, "default", NULL); |
595 trial->AppendGroup("non_default", 100); | 605 trial->AppendGroup("non_default", 100); |
596 trial->Disable(); | 606 trial->Disable(); |
597 ASSERT_EQ("default", trial->group_name()); | 607 ASSERT_EQ("default", trial->group_name()); |
598 } | 608 } |
599 | 609 |
600 TEST_F(FieldTrialTest, ForcedFieldTrials) { | 610 TEST_F(FieldTrialTest, ForcedFieldTrials) { |
601 // Validate we keep the forced choice. | 611 // Validate we keep the forced choice. |
602 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Use the", | 612 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Use the", |
603 "Force"); | 613 "Force"); |
604 EXPECT_STREQ("Force", forced_trial->group_name().c_str()); | 614 EXPECT_STREQ("Force", forced_trial->group_name().c_str()); |
605 | 615 |
606 int default_group_number = -1; | 616 int default_group_number = -1; |
607 FieldTrial* factory_trial = | 617 scoped_refptr<FieldTrial> factory_trial = |
608 CreateFieldTrial("Use the", 1000, "default", &default_group_number); | 618 CreateFieldTrial("Use the", 1000, "default", &default_group_number); |
609 EXPECT_EQ(factory_trial, forced_trial); | 619 EXPECT_EQ(factory_trial.get(), forced_trial); |
610 | 620 |
611 int chosen_group = factory_trial->AppendGroup("Force", 100); | 621 int chosen_group = factory_trial->AppendGroup("Force", 100); |
612 EXPECT_EQ(chosen_group, factory_trial->group()); | 622 EXPECT_EQ(chosen_group, factory_trial->group()); |
613 int not_chosen_group = factory_trial->AppendGroup("Dark Side", 100); | 623 int not_chosen_group = factory_trial->AppendGroup("Dark Side", 100); |
614 EXPECT_NE(chosen_group, not_chosen_group); | 624 EXPECT_NE(chosen_group, not_chosen_group); |
615 | 625 |
616 // Since we didn't force the default group, we should not be returned the | 626 // Since we didn't force the default group, we should not be returned the |
617 // chosen group as the default group. | 627 // chosen group as the default group. |
618 EXPECT_NE(default_group_number, chosen_group); | 628 EXPECT_NE(default_group_number, chosen_group); |
619 int new_group = factory_trial->AppendGroup("Duck Tape", 800); | 629 int new_group = factory_trial->AppendGroup("Duck Tape", 800); |
620 EXPECT_NE(chosen_group, new_group); | 630 EXPECT_NE(chosen_group, new_group); |
621 // The new group should not be the default group either. | 631 // The new group should not be the default group either. |
622 EXPECT_NE(default_group_number, new_group); | 632 EXPECT_NE(default_group_number, new_group); |
623 } | 633 } |
624 | 634 |
625 TEST_F(FieldTrialTest, ForcedFieldTrialsDefaultGroup) { | 635 TEST_F(FieldTrialTest, ForcedFieldTrialsDefaultGroup) { |
626 // Forcing the default should use the proper group ID. | 636 // Forcing the default should use the proper group ID. |
627 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Trial Name", | 637 FieldTrial* forced_trial = FieldTrialList::CreateFieldTrial("Trial Name", |
628 "Default"); | 638 "Default"); |
629 int default_group_number = -1; | 639 int default_group_number = -1; |
630 FieldTrial* factory_trial = | 640 scoped_refptr<FieldTrial> factory_trial = |
631 CreateFieldTrial("Trial Name", 1000, "Default", &default_group_number); | 641 CreateFieldTrial("Trial Name", 1000, "Default", &default_group_number); |
632 EXPECT_EQ(forced_trial, factory_trial); | 642 EXPECT_EQ(forced_trial, factory_trial.get()); |
633 | 643 |
634 int other_group = factory_trial->AppendGroup("Not Default", 100); | 644 int other_group = factory_trial->AppendGroup("Not Default", 100); |
635 EXPECT_STREQ("Default", factory_trial->group_name().c_str()); | 645 EXPECT_STREQ("Default", factory_trial->group_name().c_str()); |
636 EXPECT_EQ(default_group_number, factory_trial->group()); | 646 EXPECT_EQ(default_group_number, factory_trial->group()); |
637 EXPECT_NE(other_group, factory_trial->group()); | 647 EXPECT_NE(other_group, factory_trial->group()); |
638 | 648 |
639 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800); | 649 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800); |
640 EXPECT_NE(new_other_group, factory_trial->group()); | 650 EXPECT_NE(new_other_group, factory_trial->group()); |
641 } | 651 } |
642 | 652 |
643 TEST_F(FieldTrialTest, SetForced) { | 653 TEST_F(FieldTrialTest, SetForced) { |
644 // Start by setting a trial for which we ensure a winner... | 654 // Start by setting a trial for which we ensure a winner... |
645 int default_group_number = -1; | 655 int default_group_number = -1; |
646 FieldTrial* forced_trial = | 656 scoped_refptr<FieldTrial> forced_trial = |
647 CreateFieldTrial("Use the", 1, "default", &default_group_number); | 657 CreateFieldTrial("Use the", 1, "default", &default_group_number); |
648 EXPECT_EQ(forced_trial, forced_trial); | 658 EXPECT_EQ(forced_trial, forced_trial); |
649 | 659 |
650 int forced_group = forced_trial->AppendGroup("Force", 1); | 660 int forced_group = forced_trial->AppendGroup("Force", 1); |
651 EXPECT_EQ(forced_group, forced_trial->group()); | 661 EXPECT_EQ(forced_group, forced_trial->group()); |
652 | 662 |
653 // Now force it. | 663 // Now force it. |
654 forced_trial->SetForced(); | 664 forced_trial->SetForced(); |
655 | 665 |
656 // Now try to set it up differently as a hard coded registration would. | 666 // Now try to set it up differently as a hard coded registration would. |
657 FieldTrial* hard_coded_trial = | 667 scoped_refptr<FieldTrial> hard_coded_trial = |
658 CreateFieldTrial("Use the", 1, "default", &default_group_number); | 668 CreateFieldTrial("Use the", 1, "default", &default_group_number); |
659 EXPECT_EQ(hard_coded_trial, forced_trial); | 669 EXPECT_EQ(hard_coded_trial, forced_trial); |
660 | 670 |
661 int would_lose_group = hard_coded_trial->AppendGroup("Force", 0); | 671 int would_lose_group = hard_coded_trial->AppendGroup("Force", 0); |
662 EXPECT_EQ(forced_group, hard_coded_trial->group()); | 672 EXPECT_EQ(forced_group, hard_coded_trial->group()); |
663 EXPECT_EQ(forced_group, would_lose_group); | 673 EXPECT_EQ(forced_group, would_lose_group); |
664 | 674 |
665 // Same thing if we would have done it to win again. | 675 // Same thing if we would have done it to win again. |
666 FieldTrial* other_hard_coded_trial = | 676 scoped_refptr<FieldTrial> other_hard_coded_trial = |
667 CreateFieldTrial("Use the", 1, "default", &default_group_number); | 677 CreateFieldTrial("Use the", 1, "default", &default_group_number); |
668 EXPECT_EQ(other_hard_coded_trial, forced_trial); | 678 EXPECT_EQ(other_hard_coded_trial, forced_trial); |
669 | 679 |
670 int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1); | 680 int would_win_group = other_hard_coded_trial->AppendGroup("Force", 1); |
671 EXPECT_EQ(forced_group, other_hard_coded_trial->group()); | 681 EXPECT_EQ(forced_group, other_hard_coded_trial->group()); |
672 EXPECT_EQ(forced_group, would_win_group); | 682 EXPECT_EQ(forced_group, would_win_group); |
673 } | 683 } |
674 | 684 |
675 TEST_F(FieldTrialTest, SetForcedDefaultOnly) { | 685 TEST_F(FieldTrialTest, SetForcedDefaultOnly) { |
676 const char kTrialName[] = "SetForcedDefaultOnly"; | 686 const char kTrialName[] = "SetForcedDefaultOnly"; |
677 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 687 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
678 | 688 |
679 int default_group = -1; | 689 int default_group = -1; |
680 FieldTrial* trial = | 690 scoped_refptr<FieldTrial> trial = |
681 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 691 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
682 trial->SetForced(); | 692 trial->SetForced(); |
683 | 693 |
684 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); | 694 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); |
685 EXPECT_EQ(default_group, trial->group()); | 695 EXPECT_EQ(default_group, trial->group()); |
686 EXPECT_EQ(kDefaultGroupName, trial->group_name()); | 696 EXPECT_EQ(kDefaultGroupName, trial->group_name()); |
687 } | 697 } |
688 | 698 |
689 TEST_F(FieldTrialTest, SetForcedDefaultWithExtraGroup) { | 699 TEST_F(FieldTrialTest, SetForcedDefaultWithExtraGroup) { |
690 const char kTrialName[] = "SetForcedDefaultWithExtraGroup"; | 700 const char kTrialName[] = "SetForcedDefaultWithExtraGroup"; |
691 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 701 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
692 | 702 |
693 int default_group = -1; | 703 int default_group = -1; |
694 FieldTrial* trial = | 704 scoped_refptr<FieldTrial> trial = |
695 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 705 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
696 trial->SetForced(); | 706 trial->SetForced(); |
697 | 707 |
698 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); | 708 trial = CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); |
699 const int extra_group = trial->AppendGroup("Extra", 100); | 709 const int extra_group = trial->AppendGroup("Extra", 100); |
700 EXPECT_EQ(default_group, trial->group()); | 710 EXPECT_EQ(default_group, trial->group()); |
701 EXPECT_NE(extra_group, trial->group()); | 711 EXPECT_NE(extra_group, trial->group()); |
702 EXPECT_EQ(kDefaultGroupName, trial->group_name()); | 712 EXPECT_EQ(kDefaultGroupName, trial->group_name()); |
703 } | 713 } |
704 | 714 |
705 TEST_F(FieldTrialTest, SetForcedTurnFeatureOn) { | 715 TEST_F(FieldTrialTest, SetForcedTurnFeatureOn) { |
706 const char kTrialName[] = "SetForcedTurnFeatureOn"; | 716 const char kTrialName[] = "SetForcedTurnFeatureOn"; |
707 const char kExtraGroupName[] = "Extra"; | 717 const char kExtraGroupName[] = "Extra"; |
708 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 718 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
709 | 719 |
710 // Simulate a server-side (forced) config that turns the feature on when the | 720 // Simulate a server-side (forced) config that turns the feature on when the |
711 // original hard-coded config had it disabled. | 721 // original hard-coded config had it disabled. |
712 FieldTrial* forced_trial = | 722 scoped_refptr<FieldTrial> forced_trial = |
713 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); | 723 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); |
714 forced_trial->AppendGroup(kExtraGroupName, 100); | 724 forced_trial->AppendGroup(kExtraGroupName, 100); |
715 forced_trial->SetForced(); | 725 forced_trial->SetForced(); |
716 | 726 |
717 int default_group = -1; | 727 int default_group = -1; |
718 FieldTrial* client_trial = | 728 scoped_refptr<FieldTrial> client_trial = |
719 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 729 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
720 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 0); | 730 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 0); |
721 EXPECT_NE(default_group, extra_group); | 731 EXPECT_NE(default_group, extra_group); |
722 | 732 |
723 EXPECT_FALSE(client_trial->group_reported_); | 733 EXPECT_FALSE(client_trial->group_reported_); |
724 EXPECT_EQ(extra_group, client_trial->group()); | 734 EXPECT_EQ(extra_group, client_trial->group()); |
725 EXPECT_TRUE(client_trial->group_reported_); | 735 EXPECT_TRUE(client_trial->group_reported_); |
726 EXPECT_EQ(kExtraGroupName, client_trial->group_name()); | 736 EXPECT_EQ(kExtraGroupName, client_trial->group_name()); |
727 } | 737 } |
728 | 738 |
729 TEST_F(FieldTrialTest, SetForcedTurnFeatureOff) { | 739 TEST_F(FieldTrialTest, SetForcedTurnFeatureOff) { |
730 const char kTrialName[] = "SetForcedTurnFeatureOff"; | 740 const char kTrialName[] = "SetForcedTurnFeatureOff"; |
731 const char kExtraGroupName[] = "Extra"; | 741 const char kExtraGroupName[] = "Extra"; |
732 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 742 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
733 | 743 |
734 // Simulate a server-side (forced) config that turns the feature off when the | 744 // Simulate a server-side (forced) config that turns the feature off when the |
735 // original hard-coded config had it enabled. | 745 // original hard-coded config had it enabled. |
736 FieldTrial* forced_trial = | 746 scoped_refptr<FieldTrial> forced_trial = |
737 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); | 747 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); |
738 forced_trial->AppendGroup(kExtraGroupName, 0); | 748 forced_trial->AppendGroup(kExtraGroupName, 0); |
739 forced_trial->SetForced(); | 749 forced_trial->SetForced(); |
740 | 750 |
741 int default_group = -1; | 751 int default_group = -1; |
742 FieldTrial* client_trial = | 752 scoped_refptr<FieldTrial> client_trial = |
743 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 753 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
744 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 100); | 754 const int extra_group = client_trial->AppendGroup(kExtraGroupName, 100); |
745 EXPECT_NE(default_group, extra_group); | 755 EXPECT_NE(default_group, extra_group); |
746 | 756 |
747 EXPECT_FALSE(client_trial->group_reported_); | 757 EXPECT_FALSE(client_trial->group_reported_); |
748 EXPECT_EQ(default_group, client_trial->group()); | 758 EXPECT_EQ(default_group, client_trial->group()); |
749 EXPECT_TRUE(client_trial->group_reported_); | 759 EXPECT_TRUE(client_trial->group_reported_); |
750 EXPECT_EQ(kDefaultGroupName, client_trial->group_name()); | 760 EXPECT_EQ(kDefaultGroupName, client_trial->group_name()); |
751 } | 761 } |
752 | 762 |
753 TEST_F(FieldTrialTest, SetForcedChangeDefault_Default) { | 763 TEST_F(FieldTrialTest, SetForcedChangeDefault_Default) { |
754 const char kTrialName[] = "SetForcedDefaultGroupChange"; | 764 const char kTrialName[] = "SetForcedDefaultGroupChange"; |
755 const char kGroupAName[] = "A"; | 765 const char kGroupAName[] = "A"; |
756 const char kGroupBName[] = "B"; | 766 const char kGroupBName[] = "B"; |
757 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 767 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
758 | 768 |
759 // Simulate a server-side (forced) config that switches which group is default | 769 // Simulate a server-side (forced) config that switches which group is default |
760 // and ensures that the non-forced code receives the correct group numbers. | 770 // and ensures that the non-forced code receives the correct group numbers. |
761 FieldTrial* forced_trial = | 771 scoped_refptr<FieldTrial> forced_trial = |
762 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL); | 772 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL); |
763 forced_trial->AppendGroup(kGroupBName, 100); | 773 forced_trial->AppendGroup(kGroupBName, 100); |
764 forced_trial->SetForced(); | 774 forced_trial->SetForced(); |
765 | 775 |
766 int default_group = -1; | 776 int default_group = -1; |
767 FieldTrial* client_trial = | 777 scoped_refptr<FieldTrial> client_trial = |
768 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group); | 778 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group); |
769 const int extra_group = client_trial->AppendGroup(kGroupAName, 50); | 779 const int extra_group = client_trial->AppendGroup(kGroupAName, 50); |
770 EXPECT_NE(default_group, extra_group); | 780 EXPECT_NE(default_group, extra_group); |
771 | 781 |
772 EXPECT_FALSE(client_trial->group_reported_); | 782 EXPECT_FALSE(client_trial->group_reported_); |
773 EXPECT_EQ(default_group, client_trial->group()); | 783 EXPECT_EQ(default_group, client_trial->group()); |
774 EXPECT_TRUE(client_trial->group_reported_); | 784 EXPECT_TRUE(client_trial->group_reported_); |
775 EXPECT_EQ(kGroupBName, client_trial->group_name()); | 785 EXPECT_EQ(kGroupBName, client_trial->group_name()); |
776 } | 786 } |
777 | 787 |
778 TEST_F(FieldTrialTest, SetForcedChangeDefault_NonDefault) { | 788 TEST_F(FieldTrialTest, SetForcedChangeDefault_NonDefault) { |
779 const char kTrialName[] = "SetForcedDefaultGroupChange"; | 789 const char kTrialName[] = "SetForcedDefaultGroupChange"; |
780 const char kGroupAName[] = "A"; | 790 const char kGroupAName[] = "A"; |
781 const char kGroupBName[] = "B"; | 791 const char kGroupBName[] = "B"; |
782 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 792 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
783 | 793 |
784 // Simulate a server-side (forced) config that switches which group is default | 794 // Simulate a server-side (forced) config that switches which group is default |
785 // and ensures that the non-forced code receives the correct group numbers. | 795 // and ensures that the non-forced code receives the correct group numbers. |
786 FieldTrial* forced_trial = | 796 scoped_refptr<FieldTrial> forced_trial = |
787 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL); | 797 CreateFieldTrial(kTrialName, 100, kGroupAName, NULL); |
788 forced_trial->AppendGroup(kGroupBName, 0); | 798 forced_trial->AppendGroup(kGroupBName, 0); |
789 forced_trial->SetForced(); | 799 forced_trial->SetForced(); |
790 | 800 |
791 int default_group = -1; | 801 int default_group = -1; |
792 FieldTrial* client_trial = | 802 scoped_refptr<FieldTrial> client_trial = |
793 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group); | 803 CreateFieldTrial(kTrialName, 100, kGroupBName, &default_group); |
794 const int extra_group = client_trial->AppendGroup(kGroupAName, 50); | 804 const int extra_group = client_trial->AppendGroup(kGroupAName, 50); |
795 EXPECT_NE(default_group, extra_group); | 805 EXPECT_NE(default_group, extra_group); |
796 | 806 |
797 EXPECT_FALSE(client_trial->group_reported_); | 807 EXPECT_FALSE(client_trial->group_reported_); |
798 EXPECT_EQ(extra_group, client_trial->group()); | 808 EXPECT_EQ(extra_group, client_trial->group()); |
799 EXPECT_TRUE(client_trial->group_reported_); | 809 EXPECT_TRUE(client_trial->group_reported_); |
800 EXPECT_EQ(kGroupAName, client_trial->group_name()); | 810 EXPECT_EQ(kGroupAName, client_trial->group_name()); |
801 } | 811 } |
802 | 812 |
803 TEST_F(FieldTrialTest, Observe) { | 813 TEST_F(FieldTrialTest, Observe) { |
804 const char kTrialName[] = "TrialToObserve1"; | 814 const char kTrialName[] = "TrialToObserve1"; |
805 const char kSecondaryGroupName[] = "SecondaryGroup"; | 815 const char kSecondaryGroupName[] = "SecondaryGroup"; |
806 | 816 |
807 TestFieldTrialObserver observer; | 817 TestFieldTrialObserver observer; |
808 int default_group = -1; | 818 int default_group = -1; |
809 FieldTrial* trial = | 819 scoped_refptr<FieldTrial> trial = |
810 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 820 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
811 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50); | 821 const int secondary_group = trial->AppendGroup(kSecondaryGroupName, 50); |
812 const int chosen_group = trial->group(); | 822 const int chosen_group = trial->group(); |
813 EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group); | 823 EXPECT_TRUE(chosen_group == default_group || chosen_group == secondary_group); |
814 | 824 |
815 RunLoop().RunUntilIdle(); | 825 RunLoop().RunUntilIdle(); |
816 EXPECT_EQ(kTrialName, observer.trial_name()); | 826 EXPECT_EQ(kTrialName, observer.trial_name()); |
817 if (chosen_group == default_group) | 827 if (chosen_group == default_group) |
818 EXPECT_EQ(kDefaultGroupName, observer.group_name()); | 828 EXPECT_EQ(kDefaultGroupName, observer.group_name()); |
819 else | 829 else |
820 EXPECT_EQ(kSecondaryGroupName, observer.group_name()); | 830 EXPECT_EQ(kSecondaryGroupName, observer.group_name()); |
821 } | 831 } |
822 | 832 |
823 TEST_F(FieldTrialTest, ObserveDisabled) { | 833 TEST_F(FieldTrialTest, ObserveDisabled) { |
824 const char kTrialName[] = "TrialToObserve2"; | 834 const char kTrialName[] = "TrialToObserve2"; |
825 | 835 |
826 TestFieldTrialObserver observer; | 836 TestFieldTrialObserver observer; |
827 int default_group = -1; | 837 int default_group = -1; |
828 FieldTrial* trial = | 838 scoped_refptr<FieldTrial> trial = |
829 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 839 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
830 trial->AppendGroup("A", 25); | 840 trial->AppendGroup("A", 25); |
831 trial->AppendGroup("B", 25); | 841 trial->AppendGroup("B", 25); |
832 trial->AppendGroup("C", 25); | 842 trial->AppendGroup("C", 25); |
833 trial->Disable(); | 843 trial->Disable(); |
834 | 844 |
835 // Observer shouldn't be notified of a disabled trial. | 845 // Observer shouldn't be notified of a disabled trial. |
836 RunLoop().RunUntilIdle(); | 846 RunLoop().RunUntilIdle(); |
837 EXPECT_TRUE(observer.trial_name().empty()); | 847 EXPECT_TRUE(observer.trial_name().empty()); |
838 EXPECT_TRUE(observer.group_name().empty()); | 848 EXPECT_TRUE(observer.group_name().empty()); |
839 | 849 |
840 // Observer shouldn't be notified even after a |group()| call. | 850 // Observer shouldn't be notified even after a |group()| call. |
841 EXPECT_EQ(default_group, trial->group()); | 851 EXPECT_EQ(default_group, trial->group()); |
842 RunLoop().RunUntilIdle(); | 852 RunLoop().RunUntilIdle(); |
843 EXPECT_TRUE(observer.trial_name().empty()); | 853 EXPECT_TRUE(observer.trial_name().empty()); |
844 EXPECT_TRUE(observer.group_name().empty()); | 854 EXPECT_TRUE(observer.group_name().empty()); |
845 } | 855 } |
846 | 856 |
847 TEST_F(FieldTrialTest, ObserveForcedDisabled) { | 857 TEST_F(FieldTrialTest, ObserveForcedDisabled) { |
848 const char kTrialName[] = "TrialToObserve3"; | 858 const char kTrialName[] = "TrialToObserve3"; |
849 | 859 |
850 TestFieldTrialObserver observer; | 860 TestFieldTrialObserver observer; |
851 int default_group = -1; | 861 int default_group = -1; |
852 FieldTrial* trial = | 862 scoped_refptr<FieldTrial> trial = |
853 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); | 863 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, &default_group); |
854 trial->AppendGroup("A", 25); | 864 trial->AppendGroup("A", 25); |
855 trial->AppendGroup("B", 25); | 865 trial->AppendGroup("B", 25); |
856 trial->AppendGroup("C", 25); | 866 trial->AppendGroup("C", 25); |
857 trial->SetForced(); | 867 trial->SetForced(); |
858 trial->Disable(); | 868 trial->Disable(); |
859 | 869 |
860 // Observer shouldn't be notified of a disabled trial, even when forced. | 870 // Observer shouldn't be notified of a disabled trial, even when forced. |
861 RunLoop().RunUntilIdle(); | 871 RunLoop().RunUntilIdle(); |
862 EXPECT_TRUE(observer.trial_name().empty()); | 872 EXPECT_TRUE(observer.trial_name().empty()); |
863 EXPECT_TRUE(observer.group_name().empty()); | 873 EXPECT_TRUE(observer.group_name().empty()); |
864 | 874 |
865 // Observer shouldn't be notified even after a |group()| call. | 875 // Observer shouldn't be notified even after a |group()| call. |
866 EXPECT_EQ(default_group, trial->group()); | 876 EXPECT_EQ(default_group, trial->group()); |
867 RunLoop().RunUntilIdle(); | 877 RunLoop().RunUntilIdle(); |
868 EXPECT_TRUE(observer.trial_name().empty()); | 878 EXPECT_TRUE(observer.trial_name().empty()); |
869 EXPECT_TRUE(observer.group_name().empty()); | 879 EXPECT_TRUE(observer.group_name().empty()); |
870 } | 880 } |
871 | 881 |
872 TEST_F(FieldTrialTest, DisabledTrialNotActive) { | 882 TEST_F(FieldTrialTest, DisabledTrialNotActive) { |
873 const char kTrialName[] = "DisabledTrial"; | 883 const char kTrialName[] = "DisabledTrial"; |
874 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 884 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
875 | 885 |
876 FieldTrial* trial = | 886 scoped_refptr<FieldTrial> trial = |
877 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); | 887 CreateFieldTrial(kTrialName, 100, kDefaultGroupName, NULL); |
878 trial->AppendGroup("X", 50); | 888 trial->AppendGroup("X", 50); |
879 trial->Disable(); | 889 trial->Disable(); |
880 | 890 |
881 // Ensure the trial is not listed as active. | 891 // Ensure the trial is not listed as active. |
882 FieldTrial::ActiveGroups active_groups; | 892 FieldTrial::ActiveGroups active_groups; |
883 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 893 FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
884 EXPECT_TRUE(active_groups.empty()); | 894 EXPECT_TRUE(active_groups.empty()); |
885 | 895 |
886 // Ensure the trial is not listed in the |StatesToString()| result. | 896 // Ensure the trial is not listed in the |StatesToString()| result. |
887 std::string states; | 897 std::string states; |
888 FieldTrialList::StatesToString(&states); | 898 FieldTrialList::StatesToString(&states); |
889 EXPECT_TRUE(states.empty()); | 899 EXPECT_TRUE(states.empty()); |
890 } | 900 } |
891 | 901 |
892 TEST_F(FieldTrialTest, ExpirationYearNotExpired) { | 902 TEST_F(FieldTrialTest, ExpirationYearNotExpired) { |
893 const char kTrialName[] = "NotExpired"; | 903 const char kTrialName[] = "NotExpired"; |
894 const char kGroupName[] = "Group2"; | 904 const char kGroupName[] = "Group2"; |
895 const int kProbability = 100; | 905 const int kProbability = 100; |
896 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); | 906 ASSERT_FALSE(FieldTrialList::TrialExists(kTrialName)); |
897 | 907 |
898 FieldTrial* trial = | 908 scoped_refptr<FieldTrial> trial = |
899 CreateFieldTrial(kTrialName, kProbability, kDefaultGroupName, NULL); | 909 CreateFieldTrial(kTrialName, kProbability, kDefaultGroupName, NULL); |
900 trial->AppendGroup(kGroupName, kProbability); | 910 trial->AppendGroup(kGroupName, kProbability); |
901 EXPECT_EQ(kGroupName, trial->group_name()); | 911 EXPECT_EQ(kGroupName, trial->group_name()); |
902 } | 912 } |
903 | 913 |
904 TEST_F(FieldTrialTest, FloatBoundariesGiveEqualGroupSizes) { | 914 TEST_F(FieldTrialTest, FloatBoundariesGiveEqualGroupSizes) { |
905 const int kBucketCount = 100; | 915 const int kBucketCount = 100; |
906 | 916 |
907 // Try each boundary value |i / 100.0| as the entropy value. | 917 // Try each boundary value |i / 100.0| as the entropy value. |
908 for (int i = 0; i < kBucketCount; ++i) { | 918 for (int i = 0; i < kBucketCount; ++i) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
966 EXPECT_TRUE(active_groups.empty()); | 976 EXPECT_TRUE(active_groups.empty()); |
967 | 977 |
968 // The trial shouldn't be listed in the |StatesToString()| result. | 978 // The trial shouldn't be listed in the |StatesToString()| result. |
969 std::string states; | 979 std::string states; |
970 FieldTrialList::StatesToString(&states); | 980 FieldTrialList::StatesToString(&states); |
971 EXPECT_TRUE(states.empty()); | 981 EXPECT_TRUE(states.empty()); |
972 } | 982 } |
973 } | 983 } |
974 | 984 |
975 } // namespace base | 985 } // namespace base |
OLD | NEW |