Chromium Code Reviews| 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 // Tests for the Experiment Helpers. | 5 // Tests for the Experiment Helpers. |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "chrome/common/metrics/experiments_helper.h" | 11 #include "chrome/common/metrics/experiments_helper.h" |
| 12 #include "content/test/test_browser_thread.h" | 12 #include "content/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 #include <set> | |
| 16 | |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 // Convenience helper to retrieve the GoogleExperimentID for a FieldTrial. Note | 19 // Convenience helper to retrieve the GoogleExperimentID for a FieldTrial. Note |
| 18 // that this will do the group assignment in |trial| if not already done. | 20 // that this will do the group assignment in |trial| if not already done. |
| 19 experiments_helper::GoogleExperimentID GetIDForTrial(base::FieldTrial* trial) { | 21 experiments_helper::GoogleExperimentID GetIDForTrial(base::FieldTrial* trial) { |
| 20 return experiments_helper::GetGoogleExperimentID( | 22 return experiments_helper::GetGoogleExperimentID( |
| 21 base::FieldTrial::MakeNameGroupId(trial->name(), | 23 testing::TestMakeNameGroupId(trial->name(), trial->group_name())); |
| 22 trial->group_name())); | |
| 23 } | 24 } |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 class ExperimentsHelperTest : public ::testing::Test { | 28 class ExperimentsHelperTest : public ::testing::Test { |
| 28 public: | 29 public: |
| 29 ExperimentsHelperTest() { | 30 ExperimentsHelperTest() { |
| 30 // Since the API can only be called on the UI thread, we have to fake that | 31 // Since the API can only be called on the UI thread, we have to fake that |
| 31 // we're on it. | 32 // we're on it. |
| 32 ui_thread_.reset(new content::TestBrowserThread( | 33 ui_thread_.reset(new content::TestBrowserThread( |
| 33 content::BrowserThread::UI, &message_loop_)); | 34 content::BrowserThread::UI, &message_loop_)); |
| 34 | 35 |
| 35 base::Time now = base::Time::NowFromSystemTime(); | 36 base::Time now = base::Time::NowFromSystemTime(); |
| 36 base::TimeDelta oneYear = base::TimeDelta::FromDays(365); | 37 base::TimeDelta oneYear = base::TimeDelta::FromDays(365); |
| 37 base::Time::Exploded exploded; | 38 base::Time::Exploded exploded; |
| 38 | 39 |
| 39 base::Time next_year_time = now + oneYear; | 40 base::Time next_year_time = now + oneYear; |
| 40 next_year_time.LocalExplode(&exploded); | 41 next_year_time.LocalExplode(&exploded); |
| 41 next_year_ = exploded.year; | 42 next_year_ = exploded.year; |
| 42 } | 43 } |
| 43 | 44 |
| 44 protected: | 45 protected: |
| 45 int next_year_; | 46 int next_year_; |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 MessageLoop message_loop_; | 49 MessageLoop message_loop_; |
| 49 scoped_ptr<content::TestBrowserThread> ui_thread_; | 50 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 50 }; | 51 }; |
| 51 | 52 |
| 53 TEST_F(ExperimentsHelperTest, HashName) { | |
| 54 // Make sure hashing is stable on all platforms. | |
| 55 struct { | |
| 56 const char* name; | |
| 57 uint32 hash_value; | |
| 58 } known_hashes[] = { | |
| 59 {"a", 937752454u}, | |
| 60 {"1", 723085877u}, | |
| 61 {"Trial Name", 2713117220u}, | |
| 62 {"Group Name", 3201815843u}, | |
| 63 {"My Favorite Experiment", 3722155194u}, | |
| 64 {"My Awesome Group Name", 4109503236u}, | |
| 65 {"abcdefghijklmonpqrstuvwxyz", 787728696u}, | |
| 66 {"0123456789ABCDEF", 348858318U} | |
| 67 }; | |
| 68 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) { | |
| 69 EXPECT_EQ(known_hashes[i].hash_value, | |
| 70 testing::TestHashName(known_hashes[i].name)); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 TEST_F(ExperimentsHelperTest, GetFieldTrialSelectedGroups) { | |
| 75 std::string trial_one("trial one"); | |
| 76 std::string group_one("group one"); | |
| 77 std::string trial_two("trial two"); | |
| 78 std::string group_two("group two"); | |
| 79 | |
| 80 base::FieldTrial::SelectedGroups selected_groups; | |
| 81 base::FieldTrial::SelectedGroup selected_group; | |
| 82 selected_group.trial = trial_one; | |
| 83 selected_group.group = group_one; | |
| 84 selected_groups.push_back(selected_group); | |
| 85 | |
| 86 selected_group.trial = trial_two; | |
| 87 selected_group.group = group_two; | |
| 88 selected_groups.push_back(selected_group); | |
| 89 | |
| 90 // Create our expected groups of IDs. | |
| 91 std::set<experiments_helper::NameGroupId, | |
| 92 experiments_helper::NameGroupIdCompare> expected_groups; | |
| 93 expected_groups.insert(testing::TestMakeNameGroupId(trial_one, group_one)); | |
| 94 expected_groups.insert(testing::TestMakeNameGroupId(trial_two, group_two)); | |
| 95 | |
| 96 std::vector<experiments_helper::NameGroupId> name_group_ids; | |
| 97 testing::TestGetFieldTrialNameGroupIdsForSelectedGroups(selected_groups, | |
| 98 &name_group_ids); | |
| 99 EXPECT_EQ(2U, name_group_ids.size()); | |
| 100 for (size_t i = 0; i < name_group_ids.size(); ++i) { | |
| 101 std::set<experiments_helper::NameGroupId, | |
| 102 experiments_helper::NameGroupIdCompare>::iterator it = | |
| 103 expected_groups.find(name_group_ids[i]); | |
| 104 EXPECT_NE(it, expected_groups.end()); | |
| 105 expected_groups.erase(it); | |
|
MAD
2012/04/26 19:40:13
I think it's dangerous for some container classes
SteveT
2012/04/26 21:06:03
Oops, maybe it's not clear, but there are actually
MAD
2012/04/27 17:33:22
OK, that's fine... The trick I propose would preve
SteveT
2012/04/27 17:59:33
Good point. I've renamed it to expected_group, whi
| |
| 106 } | |
| 107 EXPECT_EQ(0U, expected_groups.size()); | |
| 108 } | |
| 109 | |
| 52 // Test that if the trial is immediately disabled, GetGoogleExperimentID just | 110 // Test that if the trial is immediately disabled, GetGoogleExperimentID just |
| 53 // returns the empty ID. | 111 // returns the empty ID. |
| 54 TEST_F(ExperimentsHelperTest, DisableImmediately) { | 112 TEST_F(ExperimentsHelperTest, DisableImmediately) { |
| 55 int default_group_number = -1; | 113 int default_group_number = -1; |
| 56 scoped_refptr<base::FieldTrial> trial( | 114 scoped_refptr<base::FieldTrial> trial( |
| 57 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", | 115 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", |
| 58 next_year_, 12, 12, | 116 next_year_, 12, 12, |
| 59 &default_group_number)); | 117 &default_group_number)); |
| 60 trial->Disable(); | 118 trial->Disable(); |
| 61 | 119 |
| 62 ASSERT_EQ(default_group_number, trial->group()); | 120 ASSERT_EQ(default_group_number, trial->group()); |
| 63 ASSERT_EQ(experiments_helper::kEmptyGoogleExperimentID, | 121 ASSERT_EQ(experiments_helper::kEmptyGoogleExperimentID, |
| 64 GetIDForTrial(trial.get())); | 122 GetIDForTrial(trial.get())); |
| 65 } | 123 } |
| 66 | 124 |
| 67 // Test that successfully associating the FieldTrial with some ID, and then | 125 // Test that successfully associating the FieldTrial with some ID, and then |
| 68 // disabling the FieldTrial actually makes GetGoogleExperimentID correctly | 126 // disabling the FieldTrial actually makes GetGoogleExperimentID correctly |
| 69 // return the empty ID. | 127 // return the empty ID. |
| 70 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) { | 128 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) { |
| 71 const std::string default_name = "default"; | 129 const std::string default_name = "default"; |
| 72 const std::string non_default_name = "non_default"; | 130 const std::string non_default_name = "non_default"; |
| 73 | 131 |
| 74 scoped_refptr<base::FieldTrial> trial( | 132 scoped_refptr<base::FieldTrial> trial( |
| 75 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, | 133 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, |
| 76 next_year_, 12, 12, NULL)); | 134 next_year_, 12, 12, NULL)); |
| 77 trial->AppendGroup(non_default_name, 100); | 135 trial->AppendGroup(non_default_name, 100); |
| 78 experiments_helper::AssociateGoogleExperimentID( | 136 experiments_helper::AssociateGoogleExperimentID( |
| 79 base::FieldTrial::MakeNameGroupId(trial->name(), default_name), 123); | 137 testing::TestMakeNameGroupId(trial->name(), default_name), 123); |
| 80 experiments_helper::AssociateGoogleExperimentID( | 138 experiments_helper::AssociateGoogleExperimentID( |
| 81 base::FieldTrial::MakeNameGroupId(trial->name(), non_default_name), 456); | 139 testing::TestMakeNameGroupId(trial->name(), non_default_name), 456); |
| 82 ASSERT_EQ(non_default_name, trial->group_name()); | 140 ASSERT_EQ(non_default_name, trial->group_name()); |
| 83 ASSERT_EQ(456U, GetIDForTrial(trial.get())); | 141 ASSERT_EQ(456U, GetIDForTrial(trial.get())); |
| 84 trial->Disable(); | 142 trial->Disable(); |
| 85 ASSERT_EQ(default_name, trial->group_name()); | 143 ASSERT_EQ(default_name, trial->group_name()); |
| 86 ASSERT_EQ(123U, GetIDForTrial(trial.get())); | 144 ASSERT_EQ(123U, GetIDForTrial(trial.get())); |
| 87 } | 145 } |
| 88 | 146 |
| 89 // Test various successful association cases. | 147 // Test various successful association cases. |
| 90 TEST_F(ExperimentsHelperTest, AssociateGoogleExperimentID) { | 148 TEST_F(ExperimentsHelperTest, AssociateGoogleExperimentID) { |
| 91 const std::string default_name1 = "default1"; | 149 const std::string default_name1 = "default1"; |
| 92 scoped_refptr<base::FieldTrial> trial_true( | 150 scoped_refptr<base::FieldTrial> trial_true( |
| 93 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, | 151 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, |
| 94 next_year_, 12, 31, NULL)); | 152 next_year_, 12, 31, NULL)); |
| 95 const std::string winner = "TheWinner"; | 153 const std::string winner = "TheWinner"; |
| 96 int winner_group = trial_true->AppendGroup(winner, 10); | 154 int winner_group = trial_true->AppendGroup(winner, 10); |
| 97 | 155 |
| 98 // Set GoogleExperimentIDs so we can verify that they were chosen correctly. | 156 // Set GoogleExperimentIDs so we can verify that they were chosen correctly. |
| 99 experiments_helper::AssociateGoogleExperimentID( | 157 experiments_helper::AssociateGoogleExperimentID( |
| 100 base::FieldTrial::MakeNameGroupId(trial_true->name(), default_name1), | 158 testing::TestMakeNameGroupId(trial_true->name(), default_name1), 123); |
| 101 123); | |
| 102 experiments_helper::AssociateGoogleExperimentID( | 159 experiments_helper::AssociateGoogleExperimentID( |
| 103 base::FieldTrial::MakeNameGroupId(trial_true->name(), winner), | 160 testing::TestMakeNameGroupId(trial_true->name(), winner), 456); |
| 104 456); | |
| 105 | 161 |
| 106 EXPECT_EQ(winner_group, trial_true->group()); | 162 EXPECT_EQ(winner_group, trial_true->group()); |
| 107 EXPECT_EQ(winner, trial_true->group_name()); | 163 EXPECT_EQ(winner, trial_true->group_name()); |
| 108 EXPECT_EQ(456U, GetIDForTrial(trial_true.get())); | 164 EXPECT_EQ(456U, GetIDForTrial(trial_true.get())); |
| 109 | 165 |
| 110 const std::string default_name2 = "default2"; | 166 const std::string default_name2 = "default2"; |
| 111 scoped_refptr<base::FieldTrial> trial_false( | 167 scoped_refptr<base::FieldTrial> trial_false( |
| 112 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2, | 168 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2, |
| 113 next_year_, 12, 31, NULL)); | 169 next_year_, 12, 31, NULL)); |
| 114 const std::string loser = "ALoser"; | 170 const std::string loser = "ALoser"; |
| 115 int loser_group = trial_false->AppendGroup(loser, 0); | 171 int loser_group = trial_false->AppendGroup(loser, 0); |
| 116 | 172 |
| 117 experiments_helper::AssociateGoogleExperimentID( | 173 experiments_helper::AssociateGoogleExperimentID( |
| 118 base::FieldTrial::MakeNameGroupId(trial_false->name(), default_name2), | 174 testing::TestMakeNameGroupId(trial_false->name(), default_name2), 123); |
| 119 123); | |
| 120 experiments_helper::AssociateGoogleExperimentID( | 175 experiments_helper::AssociateGoogleExperimentID( |
| 121 base::FieldTrial::MakeNameGroupId(trial_false->name(), loser), | 176 testing::TestMakeNameGroupId(trial_false->name(), loser), 456); |
| 122 456); | |
| 123 | 177 |
| 124 EXPECT_NE(loser_group, trial_false->group()); | 178 EXPECT_NE(loser_group, trial_false->group()); |
| 125 EXPECT_EQ(123U, GetIDForTrial(trial_false.get())); | 179 EXPECT_EQ(123U, GetIDForTrial(trial_false.get())); |
| 126 } | 180 } |
| 127 | 181 |
| 128 // Test that not associating a FieldTrial with any IDs ensure that the empty ID | 182 // Test that not associating a FieldTrial with any IDs ensure that the empty ID |
| 129 // will be returned. | 183 // will be returned. |
| 130 TEST_F(ExperimentsHelperTest, NoAssociation) { | 184 TEST_F(ExperimentsHelperTest, NoAssociation) { |
| 131 const std::string default_name = "default"; | 185 const std::string default_name = "default"; |
| 132 scoped_refptr<base::FieldTrial> no_id_trial( | 186 scoped_refptr<base::FieldTrial> no_id_trial( |
| 133 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, | 187 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, |
| 134 next_year_, 12, 31, NULL)); | 188 next_year_, 12, 31, NULL)); |
| 135 const std::string winner = "TheWinner"; | 189 const std::string winner = "TheWinner"; |
| 136 int winner_group = no_id_trial->AppendGroup(winner, 10); | 190 int winner_group = no_id_trial->AppendGroup(winner, 10); |
| 137 | 191 |
| 138 // Ensure that despite the fact that a normal winner is elected, it does not | 192 // Ensure that despite the fact that a normal winner is elected, it does not |
| 139 // have a valid GoogleExperimentID associated with it. | 193 // have a valid GoogleExperimentID associated with it. |
| 140 EXPECT_EQ(winner_group, no_id_trial->group()); | 194 EXPECT_EQ(winner_group, no_id_trial->group()); |
| 141 EXPECT_EQ(winner, no_id_trial->group_name()); | 195 EXPECT_EQ(winner, no_id_trial->group_name()); |
| 142 EXPECT_EQ(experiments_helper::kEmptyGoogleExperimentID, | 196 EXPECT_EQ(experiments_helper::kEmptyGoogleExperimentID, |
| 143 GetIDForTrial(no_id_trial.get())); | 197 GetIDForTrial(no_id_trial.get())); |
| 144 } | 198 } |
| OLD | NEW |