| 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 <set> |
| 8 |
| 7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 9 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 10 #include "base/time.h" | 12 #include "base/time.h" |
| 11 #include "chrome/common/metrics/experiments_helper.h" | 13 #include "chrome/common/metrics/experiments_helper.h" |
| 12 #include "content/test/test_browser_thread.h" | 14 #include "content/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 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(trial->name(), |
| 21 base::FieldTrial::MakeNameGroupId(trial->name(), | 23 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 typedef std::set<experiments_helper::SelectedGroupId, |
| 76 experiments_helper::SelectedGroupIdCompare> SelectedGroupIdSet; |
| 77 std::string trial_one("trial one"); |
| 78 std::string group_one("group one"); |
| 79 std::string trial_two("trial two"); |
| 80 std::string group_two("group two"); |
| 81 |
| 82 base::FieldTrial::SelectedGroups selected_groups; |
| 83 base::FieldTrial::SelectedGroup selected_group; |
| 84 selected_group.trial = trial_one; |
| 85 selected_group.group = group_one; |
| 86 selected_groups.push_back(selected_group); |
| 87 |
| 88 selected_group.trial = trial_two; |
| 89 selected_group.group = group_two; |
| 90 selected_groups.push_back(selected_group); |
| 91 |
| 92 // Create our expected groups of IDs. |
| 93 SelectedGroupIdSet expected_groups; |
| 94 experiments_helper::SelectedGroupId name_group_id; |
| 95 name_group_id.name = testing::TestHashName(trial_one); |
| 96 name_group_id.group = testing::TestHashName(group_one); |
| 97 expected_groups.insert(name_group_id); |
| 98 name_group_id.name = testing::TestHashName(trial_two); |
| 99 name_group_id.group = testing::TestHashName(group_two); |
| 100 expected_groups.insert(name_group_id); |
| 101 |
| 102 std::vector<experiments_helper::SelectedGroupId> selected_group_ids; |
| 103 testing::TestGetFieldTrialSelectedGroupIdsForSelectedGroups( |
| 104 selected_groups, &selected_group_ids); |
| 105 EXPECT_EQ(2U, selected_group_ids.size()); |
| 106 for (size_t i = 0; i < selected_group_ids.size(); ++i) { |
| 107 SelectedGroupIdSet::iterator expected_group = |
| 108 expected_groups.find(selected_group_ids[i]); |
| 109 EXPECT_FALSE(expected_group == expected_groups.end()); |
| 110 expected_groups.erase(expected_group); |
| 111 } |
| 112 EXPECT_EQ(0U, expected_groups.size()); |
| 113 } |
| 114 |
| 52 // Test that if the trial is immediately disabled, GetGoogleExperimentID just | 115 // Test that if the trial is immediately disabled, GetGoogleExperimentID just |
| 53 // returns the empty ID. | 116 // returns the empty ID. |
| 54 TEST_F(ExperimentsHelperTest, DisableImmediately) { | 117 TEST_F(ExperimentsHelperTest, DisableImmediately) { |
| 55 int default_group_number = -1; | 118 int default_group_number = -1; |
| 56 scoped_refptr<base::FieldTrial> trial( | 119 scoped_refptr<base::FieldTrial> trial( |
| 57 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", | 120 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", |
| 58 next_year_, 12, 12, | 121 next_year_, 12, 12, |
| 59 &default_group_number)); | 122 &default_group_number)); |
| 60 trial->Disable(); | 123 trial->Disable(); |
| 61 | 124 |
| 62 ASSERT_EQ(default_group_number, trial->group()); | 125 ASSERT_EQ(default_group_number, trial->group()); |
| 63 ASSERT_EQ(experiments_helper::kEmptyGoogleExperimentID, | 126 ASSERT_EQ(experiments_helper::kEmptyGoogleExperimentID, |
| 64 GetIDForTrial(trial.get())); | 127 GetIDForTrial(trial.get())); |
| 65 } | 128 } |
| 66 | 129 |
| 67 // Test that successfully associating the FieldTrial with some ID, and then | 130 // Test that successfully associating the FieldTrial with some ID, and then |
| 68 // disabling the FieldTrial actually makes GetGoogleExperimentID correctly | 131 // disabling the FieldTrial actually makes GetGoogleExperimentID correctly |
| 69 // return the empty ID. | 132 // return the empty ID. |
| 70 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) { | 133 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) { |
| 71 const std::string default_name = "default"; | 134 const std::string default_name = "default"; |
| 72 const std::string non_default_name = "non_default"; | 135 const std::string non_default_name = "non_default"; |
| 73 | 136 |
| 74 scoped_refptr<base::FieldTrial> trial( | 137 scoped_refptr<base::FieldTrial> trial( |
| 75 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, | 138 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, |
| 76 next_year_, 12, 12, NULL)); | 139 next_year_, 12, 12, NULL)); |
| 77 trial->AppendGroup(non_default_name, 100); | 140 trial->AppendGroup(non_default_name, 100); |
| 78 experiments_helper::AssociateGoogleExperimentID( | 141 experiments_helper::AssociateGoogleExperimentID( |
| 79 base::FieldTrial::MakeNameGroupId(trial->name(), default_name), 123); | 142 trial->name(), default_name, 123); |
| 80 experiments_helper::AssociateGoogleExperimentID( | 143 experiments_helper::AssociateGoogleExperimentID( |
| 81 base::FieldTrial::MakeNameGroupId(trial->name(), non_default_name), 456); | 144 trial->name(), non_default_name, 456); |
| 82 ASSERT_EQ(non_default_name, trial->group_name()); | 145 ASSERT_EQ(non_default_name, trial->group_name()); |
| 83 ASSERT_EQ(456U, GetIDForTrial(trial.get())); | 146 ASSERT_EQ(456U, GetIDForTrial(trial.get())); |
| 84 trial->Disable(); | 147 trial->Disable(); |
| 85 ASSERT_EQ(default_name, trial->group_name()); | 148 ASSERT_EQ(default_name, trial->group_name()); |
| 86 ASSERT_EQ(123U, GetIDForTrial(trial.get())); | 149 ASSERT_EQ(123U, GetIDForTrial(trial.get())); |
| 87 } | 150 } |
| 88 | 151 |
| 89 // Test various successful association cases. | 152 // Test various successful association cases. |
| 90 TEST_F(ExperimentsHelperTest, AssociateGoogleExperimentID) { | 153 TEST_F(ExperimentsHelperTest, AssociateGoogleExperimentID) { |
| 91 const std::string default_name1 = "default1"; | 154 const std::string default_name1 = "default1"; |
| 92 scoped_refptr<base::FieldTrial> trial_true( | 155 scoped_refptr<base::FieldTrial> trial_true( |
| 93 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, | 156 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, |
| 94 next_year_, 12, 31, NULL)); | 157 next_year_, 12, 31, NULL)); |
| 95 const std::string winner = "TheWinner"; | 158 const std::string winner = "TheWinner"; |
| 96 int winner_group = trial_true->AppendGroup(winner, 10); | 159 int winner_group = trial_true->AppendGroup(winner, 10); |
| 97 | 160 |
| 98 // Set GoogleExperimentIDs so we can verify that they were chosen correctly. | 161 // Set GoogleExperimentIDs so we can verify that they were chosen correctly. |
| 99 experiments_helper::AssociateGoogleExperimentID( | 162 experiments_helper::AssociateGoogleExperimentID( |
| 100 base::FieldTrial::MakeNameGroupId(trial_true->name(), default_name1), | 163 trial_true->name(), default_name1, 123); |
| 101 123); | |
| 102 experiments_helper::AssociateGoogleExperimentID( | 164 experiments_helper::AssociateGoogleExperimentID( |
| 103 base::FieldTrial::MakeNameGroupId(trial_true->name(), winner), | 165 trial_true->name(), winner, 456); |
| 104 456); | |
| 105 | 166 |
| 106 EXPECT_EQ(winner_group, trial_true->group()); | 167 EXPECT_EQ(winner_group, trial_true->group()); |
| 107 EXPECT_EQ(winner, trial_true->group_name()); | 168 EXPECT_EQ(winner, trial_true->group_name()); |
| 108 EXPECT_EQ(456U, GetIDForTrial(trial_true.get())); | 169 EXPECT_EQ(456U, GetIDForTrial(trial_true.get())); |
| 109 | 170 |
| 110 const std::string default_name2 = "default2"; | 171 const std::string default_name2 = "default2"; |
| 111 scoped_refptr<base::FieldTrial> trial_false( | 172 scoped_refptr<base::FieldTrial> trial_false( |
| 112 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2, | 173 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2, |
| 113 next_year_, 12, 31, NULL)); | 174 next_year_, 12, 31, NULL)); |
| 114 const std::string loser = "ALoser"; | 175 const std::string loser = "ALoser"; |
| 115 int loser_group = trial_false->AppendGroup(loser, 0); | 176 int loser_group = trial_false->AppendGroup(loser, 0); |
| 116 | 177 |
| 117 experiments_helper::AssociateGoogleExperimentID( | 178 experiments_helper::AssociateGoogleExperimentID( |
| 118 base::FieldTrial::MakeNameGroupId(trial_false->name(), default_name2), | 179 trial_false->name(), default_name2, 123); |
| 119 123); | |
| 120 experiments_helper::AssociateGoogleExperimentID( | 180 experiments_helper::AssociateGoogleExperimentID( |
| 121 base::FieldTrial::MakeNameGroupId(trial_false->name(), loser), | 181 trial_false->name(), loser, 456); |
| 122 456); | |
| 123 | 182 |
| 124 EXPECT_NE(loser_group, trial_false->group()); | 183 EXPECT_NE(loser_group, trial_false->group()); |
| 125 EXPECT_EQ(123U, GetIDForTrial(trial_false.get())); | 184 EXPECT_EQ(123U, GetIDForTrial(trial_false.get())); |
| 126 } | 185 } |
| 127 | 186 |
| 128 // Test that not associating a FieldTrial with any IDs ensure that the empty ID | 187 // Test that not associating a FieldTrial with any IDs ensure that the empty ID |
| 129 // will be returned. | 188 // will be returned. |
| 130 TEST_F(ExperimentsHelperTest, NoAssociation) { | 189 TEST_F(ExperimentsHelperTest, NoAssociation) { |
| 131 const std::string default_name = "default"; | 190 const std::string default_name = "default"; |
| 132 scoped_refptr<base::FieldTrial> no_id_trial( | 191 scoped_refptr<base::FieldTrial> no_id_trial( |
| 133 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, | 192 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, |
| 134 next_year_, 12, 31, NULL)); | 193 next_year_, 12, 31, NULL)); |
| 135 const std::string winner = "TheWinner"; | 194 const std::string winner = "TheWinner"; |
| 136 int winner_group = no_id_trial->AppendGroup(winner, 10); | 195 int winner_group = no_id_trial->AppendGroup(winner, 10); |
| 137 | 196 |
| 138 // Ensure that despite the fact that a normal winner is elected, it does not | 197 // Ensure that despite the fact that a normal winner is elected, it does not |
| 139 // have a valid GoogleExperimentID associated with it. | 198 // have a valid GoogleExperimentID associated with it. |
| 140 EXPECT_EQ(winner_group, no_id_trial->group()); | 199 EXPECT_EQ(winner_group, no_id_trial->group()); |
| 141 EXPECT_EQ(winner, no_id_trial->group_name()); | 200 EXPECT_EQ(winner, no_id_trial->group_name()); |
| 142 EXPECT_EQ(experiments_helper::kEmptyGoogleExperimentID, | 201 EXPECT_EQ(experiments_helper::kEmptyGoogleExperimentID, |
| 143 GetIDForTrial(no_id_trial.get())); | 202 GetIDForTrial(no_id_trial.get())); |
| 144 } | 203 } |
| OLD | NEW |