| 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 // 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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 &default_group_number); | 220 &default_group_number); |
| 221 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen. | 221 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen. |
| 222 | 222 |
| 223 // 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. |
| 224 EXPECT_EQ(default_group_number, trial->group()); | 224 EXPECT_EQ(default_group_number, trial->group()); |
| 225 | 225 |
| 226 // And that default_group_name should ALWAYS win. | 226 // And that default_group_name should ALWAYS win. |
| 227 EXPECT_EQ(default_group_name, trial->group_name()); | 227 EXPECT_EQ(default_group_name, trial->group_name()); |
| 228 } | 228 } |
| 229 | 229 |
| 230 TEST_F(FieldTrialTest, HashName) { | 230 TEST_F(FieldTrialTest, SelectedGroups) { |
| 231 // Make sure hashing is stable on all platforms. | |
| 232 struct { | |
| 233 const char* name; | |
| 234 uint32 hash_value; | |
| 235 } known_hashes[] = { | |
| 236 {"a", 937752454u}, | |
| 237 {"1", 723085877u}, | |
| 238 {"Trial Name", 2713117220u}, | |
| 239 {"Group Name", 3201815843u}, | |
| 240 {"My Favorite Experiment", 3722155194u}, | |
| 241 {"My Awesome Group Name", 4109503236u}, | |
| 242 {"abcdefghijklmonpqrstuvwxyz", 787728696u}, | |
| 243 {"0123456789ABCDEF", 348858318U} | |
| 244 }; | |
| 245 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) { | |
| 246 EXPECT_EQ(known_hashes[i].hash_value, | |
| 247 FieldTrial::HashName(known_hashes[i].name)); | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 TEST_F(FieldTrialTest, NameGroupIds) { | |
| 252 std::string no_group("No Group"); | 231 std::string no_group("No Group"); |
| 253 uint32 no_group_id = FieldTrial::HashName(no_group); | |
| 254 scoped_refptr<FieldTrial> trial(FieldTrialList::FactoryGetFieldTrial( | 232 scoped_refptr<FieldTrial> trial(FieldTrialList::FactoryGetFieldTrial( |
| 255 no_group, 10, "Default", next_year_, 12, 31, NULL)); | 233 no_group, 10, "Default", next_year_, 12, 31, NULL)); |
| 256 | 234 |
| 257 // There is no winner yet, so no NameGroupId should be returned. | 235 // There is no winner yet, so no NameGroupId should be returned. |
| 258 FieldTrial::NameGroupId name_group_id; | 236 FieldTrial::SelectedGroup selected_group; |
| 259 EXPECT_FALSE(trial->GetNameGroupId(&name_group_id)); | 237 EXPECT_FALSE(trial->GetSelectedGroup(&selected_group)); |
| 260 | 238 |
| 261 // Create a single winning group. | 239 // Create a single winning group. |
| 262 std::string one_winner("One Winner"); | 240 std::string one_winner("One Winner"); |
| 263 uint32 one_winner_id = FieldTrial::HashName(one_winner); | |
| 264 trial = FieldTrialList::FactoryGetFieldTrial( | 241 trial = FieldTrialList::FactoryGetFieldTrial( |
| 265 one_winner, 10, "Default", next_year_, 12, 31, NULL); | 242 one_winner, 10, "Default", next_year_, 12, 31, NULL); |
| 266 std::string winner("Winner"); | 243 std::string winner("Winner"); |
| 267 uint32 winner_group_id = FieldTrial::HashName(winner); | |
| 268 trial->AppendGroup(winner, 10); | 244 trial->AppendGroup(winner, 10); |
| 269 EXPECT_TRUE(trial->GetNameGroupId(&name_group_id)); | 245 EXPECT_TRUE(trial->GetSelectedGroup(&selected_group)); |
| 270 EXPECT_EQ(one_winner_id, name_group_id.name); | 246 EXPECT_EQ(one_winner, selected_group.trial); |
| 271 EXPECT_EQ(winner_group_id, name_group_id.group); | 247 EXPECT_EQ(winner, selected_group.group); |
| 272 | 248 |
| 273 std::string multi_group("MultiGroup"); | 249 std::string multi_group("MultiGroup"); |
| 274 uint32 multi_group_id = FieldTrial::HashName(multi_group); | |
| 275 scoped_refptr<FieldTrial> multi_group_trial = | 250 scoped_refptr<FieldTrial> multi_group_trial = |
| 276 FieldTrialList::FactoryGetFieldTrial(multi_group, 9, "Default", | 251 FieldTrialList::FactoryGetFieldTrial(multi_group, 9, "Default", |
| 277 next_year_, 12, 31, NULL); | 252 next_year_, 12, 31, NULL); |
| 278 | 253 |
| 279 multi_group_trial->AppendGroup("Me", 3); | 254 multi_group_trial->AppendGroup("Me", 3); |
| 280 multi_group_trial->AppendGroup("You", 3); | 255 multi_group_trial->AppendGroup("You", 3); |
| 281 multi_group_trial->AppendGroup("Them", 3); | 256 multi_group_trial->AppendGroup("Them", 3); |
| 282 EXPECT_TRUE(multi_group_trial->GetNameGroupId(&name_group_id)); | 257 EXPECT_TRUE(multi_group_trial->GetSelectedGroup(&selected_group)); |
| 283 EXPECT_EQ(multi_group_id, name_group_id.name); | 258 EXPECT_EQ(multi_group, selected_group.trial); |
| 284 uint32 multi_group_winner_id = | 259 EXPECT_EQ(multi_group_trial->group_name(), selected_group.group); |
| 285 FieldTrial::HashName(multi_group_trial->group_name()); | |
| 286 EXPECT_EQ(multi_group_winner_id, name_group_id.group); | |
| 287 | 260 |
| 288 // Now check if the list is built properly... | 261 // Now check if the list is built properly... |
| 289 std::vector<FieldTrial::NameGroupId> name_group_ids; | 262 std::vector<FieldTrial::SelectedGroup> selected_groups; |
| 290 FieldTrialList::GetFieldTrialNameGroupIds(&name_group_ids); | 263 FieldTrialList::GetFieldTrialSelectedGroups(&selected_groups); |
| 291 EXPECT_EQ(2U, name_group_ids.size()); | 264 EXPECT_EQ(2U, selected_groups.size()); |
| 292 for (size_t i = 0; i < name_group_ids.size(); ++i) { | 265 for (size_t i = 0; i < selected_groups.size(); ++i) { |
| 293 // Order is not guaranteed, so check all values. | 266 // Order is not guaranteed, so check all values. |
| 294 EXPECT_NE(no_group_id, name_group_ids[i].name); | 267 EXPECT_NE(no_group, selected_groups[i].trial); |
| 295 EXPECT_TRUE(one_winner_id != name_group_ids[i].name || | 268 EXPECT_TRUE(one_winner != selected_groups[i].trial || |
| 296 winner_group_id == name_group_ids[i].group); | 269 winner == selected_groups[i].group); |
| 297 EXPECT_TRUE(multi_group_id != name_group_ids[i].name || | 270 EXPECT_TRUE(multi_group != selected_groups[i].trial || |
| 298 multi_group_winner_id == name_group_ids[i].group); | 271 multi_group_trial->group_name() == selected_groups[i].group); |
| 299 } | 272 } |
| 300 } | 273 } |
| 301 | 274 |
| 302 TEST_F(FieldTrialTest, Save) { | 275 TEST_F(FieldTrialTest, Save) { |
| 303 std::string save_string; | 276 std::string save_string; |
| 304 | 277 |
| 305 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial( | 278 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial( |
| 306 "Some name", 10, "Default some name", next_year_, 12, 31, NULL); | 279 "Some name", 10, "Default some name", next_year_, 12, 31, NULL); |
| 307 // There is no winner yet, so no textual group name is associated with trial. | 280 // There is no winner yet, so no textual group name is associated with trial. |
| 308 // In this case, the trial should not be included. | 281 // In this case, the trial should not be included. |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 int other_group = factory_trial->AppendGroup("Not Default", 100); | 516 int other_group = factory_trial->AppendGroup("Not Default", 100); |
| 544 EXPECT_STREQ("Default", factory_trial->group_name().c_str()); | 517 EXPECT_STREQ("Default", factory_trial->group_name().c_str()); |
| 545 EXPECT_EQ(default_group_number, factory_trial->group()); | 518 EXPECT_EQ(default_group_number, factory_trial->group()); |
| 546 EXPECT_NE(other_group, factory_trial->group()); | 519 EXPECT_NE(other_group, factory_trial->group()); |
| 547 | 520 |
| 548 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800); | 521 int new_other_group = factory_trial->AppendGroup("Not Default Either", 800); |
| 549 EXPECT_NE(new_other_group, factory_trial->group()); | 522 EXPECT_NE(new_other_group, factory_trial->group()); |
| 550 } | 523 } |
| 551 | 524 |
| 552 } // namespace base | 525 } // namespace base |
| OLD | NEW |