| 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 Variations Helpers. |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/common/metrics/experiments_helper.h" | 14 #include "chrome/common/metrics/variations_util.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace experiments_helper { | 18 namespace chrome_variations { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Convenience helper to retrieve the chrome_variations::VariationID for a | 22 // Convenience helper to retrieve the chrome_variations::VariationID for a |
| 23 // FieldTrial. Note that this will do the group assignment in |trial| if not | 23 // FieldTrial. Note that this will do the group assignment in |trial| if not |
| 24 // already done. | 24 // already done. |
| 25 chrome_variations::VariationID GetIDForTrial(base::FieldTrial* trial) { | 25 chrome_variations::VariationID GetIDForTrial(base::FieldTrial* trial) { |
| 26 return GetGoogleVariationID(trial->name(), trial->group_name()); | 26 return GetGoogleVariationID(trial->name(), trial->group_name()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 class ExperimentsHelperTest : public ::testing::Test { | 31 class VariationsHelperTest : public ::testing::Test { |
| 32 public: | 32 public: |
| 33 ExperimentsHelperTest() { | 33 VariationsHelperTest() { |
| 34 // Since the API can only be called on the UI thread, we have to fake that | 34 // Since the API can only be called on the UI thread, we have to fake that |
| 35 // we're on it. | 35 // we're on it. |
| 36 ui_thread_.reset(new content::TestBrowserThread( | 36 ui_thread_.reset(new content::TestBrowserThread( |
| 37 content::BrowserThread::UI, &message_loop_)); | 37 content::BrowserThread::UI, &message_loop_)); |
| 38 | 38 |
| 39 base::Time now = base::Time::NowFromSystemTime(); | 39 base::Time now = base::Time::NowFromSystemTime(); |
| 40 base::TimeDelta oneYear = base::TimeDelta::FromDays(365); | 40 base::TimeDelta oneYear = base::TimeDelta::FromDays(365); |
| 41 base::Time::Exploded exploded; | 41 base::Time::Exploded exploded; |
| 42 | 42 |
| 43 base::Time next_year_time = now + oneYear; | 43 base::Time next_year_time = now + oneYear; |
| 44 next_year_time.LocalExplode(&exploded); | 44 next_year_time.LocalExplode(&exploded); |
| 45 next_year_ = exploded.year; | 45 next_year_ = exploded.year; |
| 46 } | 46 } |
| 47 | 47 |
| 48 protected: | 48 protected: |
| 49 int next_year_; | 49 int next_year_; |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 MessageLoop message_loop_; | 52 MessageLoop message_loop_; |
| 53 scoped_ptr<content::TestBrowserThread> ui_thread_; | 53 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 TEST_F(ExperimentsHelperTest, HashName) { | 56 TEST_F(VariationsHelperTest, HashName) { |
| 57 // Make sure hashing is stable on all platforms. | 57 // Make sure hashing is stable on all platforms. |
| 58 struct { | 58 struct { |
| 59 const char* name; | 59 const char* name; |
| 60 uint32 hash_value; | 60 uint32 hash_value; |
| 61 } known_hashes[] = { | 61 } known_hashes[] = { |
| 62 {"a", 937752454u}, | 62 {"a", 937752454u}, |
| 63 {"1", 723085877u}, | 63 {"1", 723085877u}, |
| 64 {"Trial Name", 2713117220u}, | 64 {"Trial Name", 2713117220u}, |
| 65 {"Group Name", 3201815843u}, | 65 {"Group Name", 3201815843u}, |
| 66 {"My Favorite Experiment", 3722155194u}, | 66 {"My Favorite Experiment", 3722155194u}, |
| 67 {"My Awesome Group Name", 4109503236u}, | 67 {"My Awesome Group Name", 4109503236u}, |
| 68 {"abcdefghijklmonpqrstuvwxyz", 787728696u}, | 68 {"abcdefghijklmonpqrstuvwxyz", 787728696u}, |
| 69 {"0123456789ABCDEF", 348858318U} | 69 {"0123456789ABCDEF", 348858318U} |
| 70 }; | 70 }; |
| 71 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) { | 71 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) { |
| 72 EXPECT_EQ(known_hashes[i].hash_value, | 72 EXPECT_EQ(known_hashes[i].hash_value, |
| 73 testing::TestHashName(known_hashes[i].name)); | 73 testing::TestHashName(known_hashes[i].name)); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 TEST_F(ExperimentsHelperTest, GetFieldTrialSelectedGroups) { | 77 TEST_F(VariationsHelperTest, GetFieldTrialSelectedGroups) { |
| 78 typedef std::set<SelectedGroupId, SelectedGroupIdCompare> SelectedGroupIdSet; | 78 typedef std::set<SelectedGroupId, SelectedGroupIdCompare> SelectedGroupIdSet; |
| 79 std::string trial_one("trial one"); | 79 std::string trial_one("trial one"); |
| 80 std::string group_one("group one"); | 80 std::string group_one("group one"); |
| 81 std::string trial_two("trial two"); | 81 std::string trial_two("trial two"); |
| 82 std::string group_two("group two"); | 82 std::string group_two("group two"); |
| 83 | 83 |
| 84 base::FieldTrial::SelectedGroups selected_groups; | 84 base::FieldTrial::SelectedGroups selected_groups; |
| 85 base::FieldTrial::SelectedGroup selected_group; | 85 base::FieldTrial::SelectedGroup selected_group; |
| 86 selected_group.trial = trial_one; | 86 selected_group.trial = trial_one; |
| 87 selected_group.group = group_one; | 87 selected_group.group = group_one; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 109 SelectedGroupIdSet::iterator expected_group = | 109 SelectedGroupIdSet::iterator expected_group = |
| 110 expected_groups.find(selected_group_ids[i]); | 110 expected_groups.find(selected_group_ids[i]); |
| 111 EXPECT_FALSE(expected_group == expected_groups.end()); | 111 EXPECT_FALSE(expected_group == expected_groups.end()); |
| 112 expected_groups.erase(expected_group); | 112 expected_groups.erase(expected_group); |
| 113 } | 113 } |
| 114 EXPECT_EQ(0U, expected_groups.size()); | 114 EXPECT_EQ(0U, expected_groups.size()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Test that if the trial is immediately disabled, GetGoogleVariationID just | 117 // Test that if the trial is immediately disabled, GetGoogleVariationID just |
| 118 // returns the empty ID. | 118 // returns the empty ID. |
| 119 TEST_F(ExperimentsHelperTest, DisableImmediately) { | 119 TEST_F(VariationsHelperTest, DisableImmediately) { |
| 120 int default_group_number = -1; | 120 int default_group_number = -1; |
| 121 scoped_refptr<base::FieldTrial> trial( | 121 scoped_refptr<base::FieldTrial> trial( |
| 122 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", | 122 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", |
| 123 next_year_, 12, 12, | 123 next_year_, 12, 12, |
| 124 &default_group_number)); | 124 &default_group_number)); |
| 125 trial->Disable(); | 125 trial->Disable(); |
| 126 | 126 |
| 127 ASSERT_EQ(default_group_number, trial->group()); | 127 ASSERT_EQ(default_group_number, trial->group()); |
| 128 ASSERT_EQ(chrome_variations::kEmptyID, GetIDForTrial(trial.get())); | 128 ASSERT_EQ(chrome_variations::kEmptyID, GetIDForTrial(trial.get())); |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Test that successfully associating the FieldTrial with some ID, and then | 131 // Test that successfully associating the FieldTrial with some ID, and then |
| 132 // disabling the FieldTrial actually makes GetGoogleVariationID correctly | 132 // disabling the FieldTrial actually makes GetGoogleVariationID correctly |
| 133 // return the empty ID. | 133 // return the empty ID. |
| 134 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) { | 134 TEST_F(VariationsHelperTest, DisableAfterInitialization) { |
| 135 const std::string default_name = "default"; | 135 const std::string default_name = "default"; |
| 136 const std::string non_default_name = "non_default"; | 136 const std::string non_default_name = "non_default"; |
| 137 | 137 |
| 138 scoped_refptr<base::FieldTrial> trial( | 138 scoped_refptr<base::FieldTrial> trial( |
| 139 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, | 139 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, |
| 140 next_year_, 12, 12, NULL)); | 140 next_year_, 12, 12, NULL)); |
| 141 trial->AppendGroup(non_default_name, 100); | 141 trial->AppendGroup(non_default_name, 100); |
| 142 AssociateGoogleVariationID( | 142 AssociateGoogleVariationID( |
| 143 trial->name(), default_name, chrome_variations::kTestValueA); | 143 trial->name(), default_name, chrome_variations::kTestValueA); |
| 144 AssociateGoogleVariationID( | 144 AssociateGoogleVariationID( |
| 145 trial->name(), non_default_name, chrome_variations::kTestValueB); | 145 trial->name(), non_default_name, chrome_variations::kTestValueB); |
| 146 ASSERT_EQ(non_default_name, trial->group_name()); | 146 ASSERT_EQ(non_default_name, trial->group_name()); |
| 147 ASSERT_EQ(chrome_variations::kTestValueB, GetIDForTrial(trial.get())); | 147 ASSERT_EQ(chrome_variations::kTestValueB, GetIDForTrial(trial.get())); |
| 148 trial->Disable(); | 148 trial->Disable(); |
| 149 ASSERT_EQ(default_name, trial->group_name()); | 149 ASSERT_EQ(default_name, trial->group_name()); |
| 150 ASSERT_EQ(chrome_variations::kTestValueA, GetIDForTrial(trial.get())); | 150 ASSERT_EQ(chrome_variations::kTestValueA, GetIDForTrial(trial.get())); |
| 151 } | 151 } |
| 152 | 152 |
| 153 // Test various successful association cases. | 153 // Test various successful association cases. |
| 154 TEST_F(ExperimentsHelperTest, AssociateGoogleVariationID) { | 154 TEST_F(VariationsHelperTest, AssociateGoogleVariationID) { |
| 155 const std::string default_name1 = "default1"; | 155 const std::string default_name1 = "default1"; |
| 156 scoped_refptr<base::FieldTrial> trial_true( | 156 scoped_refptr<base::FieldTrial> trial_true( |
| 157 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, | 157 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, |
| 158 next_year_, 12, 31, NULL)); | 158 next_year_, 12, 31, NULL)); |
| 159 const std::string winner = "TheWinner"; | 159 const std::string winner = "TheWinner"; |
| 160 int winner_group = trial_true->AppendGroup(winner, 10); | 160 int winner_group = trial_true->AppendGroup(winner, 10); |
| 161 | 161 |
| 162 // Set GoogleVariationIDs so we can verify that they were chosen correctly. | 162 // Set GoogleVariationIDs so we can verify that they were chosen correctly. |
| 163 AssociateGoogleVariationID( | 163 AssociateGoogleVariationID( |
| 164 trial_true->name(), default_name1, chrome_variations::kTestValueA); | 164 trial_true->name(), default_name1, chrome_variations::kTestValueA); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 180 trial_false->name(), default_name2, chrome_variations::kTestValueA); | 180 trial_false->name(), default_name2, chrome_variations::kTestValueA); |
| 181 AssociateGoogleVariationID( | 181 AssociateGoogleVariationID( |
| 182 trial_false->name(), loser, chrome_variations::kTestValueB); | 182 trial_false->name(), loser, chrome_variations::kTestValueB); |
| 183 | 183 |
| 184 EXPECT_NE(loser_group, trial_false->group()); | 184 EXPECT_NE(loser_group, trial_false->group()); |
| 185 EXPECT_EQ(chrome_variations::kTestValueA, GetIDForTrial(trial_false.get())); | 185 EXPECT_EQ(chrome_variations::kTestValueA, GetIDForTrial(trial_false.get())); |
| 186 } | 186 } |
| 187 | 187 |
| 188 // Test that not associating a FieldTrial with any IDs ensure that the empty ID | 188 // Test that not associating a FieldTrial with any IDs ensure that the empty ID |
| 189 // will be returned. | 189 // will be returned. |
| 190 TEST_F(ExperimentsHelperTest, NoAssociation) { | 190 TEST_F(VariationsHelperTest, NoAssociation) { |
| 191 const std::string default_name = "default"; | 191 const std::string default_name = "default"; |
| 192 scoped_refptr<base::FieldTrial> no_id_trial( | 192 scoped_refptr<base::FieldTrial> no_id_trial( |
| 193 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, | 193 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, |
| 194 next_year_, 12, 31, NULL)); | 194 next_year_, 12, 31, NULL)); |
| 195 const std::string winner = "TheWinner"; | 195 const std::string winner = "TheWinner"; |
| 196 int winner_group = no_id_trial->AppendGroup(winner, 10); | 196 int winner_group = no_id_trial->AppendGroup(winner, 10); |
| 197 | 197 |
| 198 // Ensure that despite the fact that a normal winner is elected, it does not | 198 // Ensure that despite the fact that a normal winner is elected, it does not |
| 199 // have a valid chrome_variations::VariationID associated with it. | 199 // have a valid chrome_variations::VariationID associated with it. |
| 200 EXPECT_EQ(winner_group, no_id_trial->group()); | 200 EXPECT_EQ(winner_group, no_id_trial->group()); |
| 201 EXPECT_EQ(winner, no_id_trial->group_name()); | 201 EXPECT_EQ(winner, no_id_trial->group_name()); |
| 202 EXPECT_EQ(chrome_variations::kEmptyID, GetIDForTrial(no_id_trial.get())); | 202 EXPECT_EQ(chrome_variations::kEmptyID, GetIDForTrial(no_id_trial.get())); |
| 203 } | 203 } |
| 204 | 204 |
| 205 // Ensure that the AssociateGoogleVariationIDForce works as expected. | 205 // Ensure that the AssociateGoogleVariationIDForce works as expected. |
| 206 TEST_F(ExperimentsHelperTest, ForceAssociation) { | 206 TEST_F(VariationsHelperTest, ForceAssociation) { |
| 207 EXPECT_EQ(chrome_variations::kEmptyID, | 207 EXPECT_EQ(chrome_variations::kEmptyID, |
| 208 GetGoogleVariationID("trial", "group")); | 208 GetGoogleVariationID("trial", "group")); |
| 209 AssociateGoogleVariationID("trial", "group", | 209 AssociateGoogleVariationID("trial", "group", |
| 210 chrome_variations::kTestValueA); | 210 chrome_variations::kTestValueA); |
| 211 EXPECT_EQ(chrome_variations::kTestValueA, | 211 EXPECT_EQ(chrome_variations::kTestValueA, |
| 212 GetGoogleVariationID("trial", "group")); | 212 GetGoogleVariationID("trial", "group")); |
| 213 AssociateGoogleVariationID("trial", "group", chrome_variations::kTestValueB); | 213 AssociateGoogleVariationID("trial", "group", chrome_variations::kTestValueB); |
| 214 EXPECT_EQ(chrome_variations::kTestValueA, | 214 EXPECT_EQ(chrome_variations::kTestValueA, |
| 215 GetGoogleVariationID("trial", "group")); | 215 GetGoogleVariationID("trial", "group")); |
| 216 AssociateGoogleVariationIDForce("trial", "group", | 216 AssociateGoogleVariationIDForce("trial", "group", |
| 217 chrome_variations::kTestValueB); | 217 chrome_variations::kTestValueB); |
| 218 EXPECT_EQ(chrome_variations::kTestValueB, | 218 EXPECT_EQ(chrome_variations::kTestValueB, |
| 219 GetGoogleVariationID("trial", "group")); | 219 GetGoogleVariationID("trial", "group")); |
| 220 } | 220 } |
| 221 | 221 |
| 222 TEST_F(ExperimentsHelperTest, GenerateExperimentChunks) { | 222 TEST_F(VariationsHelperTest, GenerateExperimentChunks) { |
| 223 const char* kExperimentStrings[] = { | 223 const char* kExperimentStrings[] = { |
| 224 "1d3048f1-9de009d0", | 224 "1d3048f1-9de009d0", |
| 225 "cd73da34-cf196cb", | 225 "cd73da34-cf196cb", |
| 226 "6214fa18-9e6dc24d", | 226 "6214fa18-9e6dc24d", |
| 227 "4dcb0cd6-d31c4ca1", | 227 "4dcb0cd6-d31c4ca1", |
| 228 "9d5bce6-30d7d8ac", | 228 "9d5bce6-30d7d8ac", |
| 229 }; | 229 }; |
| 230 const char* kExpectedChunks1[] = { | 230 const char* kExpectedChunks1[] = { |
| 231 "1d3048f1-9de009d0", | 231 "1d3048f1-9de009d0", |
| 232 }; | 232 }; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 259 }; | 259 }; |
| 260 | 260 |
| 261 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 261 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 262 ASSERT_LE(cases[i].strings_length, arraysize(kExperimentStrings)); | 262 ASSERT_LE(cases[i].strings_length, arraysize(kExperimentStrings)); |
| 263 | 263 |
| 264 std::vector<string16> experiments; | 264 std::vector<string16> experiments; |
| 265 for (size_t j = 0; j < cases[i].strings_length; ++j) | 265 for (size_t j = 0; j < cases[i].strings_length; ++j) |
| 266 experiments.push_back(UTF8ToUTF16(kExperimentStrings[j])); | 266 experiments.push_back(UTF8ToUTF16(kExperimentStrings[j])); |
| 267 | 267 |
| 268 std::vector<string16> chunks; | 268 std::vector<string16> chunks; |
| 269 GenerateExperimentChunks(experiments, &chunks); | 269 GenerateVariationChunks(experiments, &chunks); |
| 270 ASSERT_EQ(cases[i].expected_chunks_length, chunks.size()); | 270 ASSERT_EQ(cases[i].expected_chunks_length, chunks.size()); |
| 271 for (size_t j = 0; j < chunks.size(); ++j) | 271 for (size_t j = 0; j < chunks.size(); ++j) |
| 272 EXPECT_EQ(UTF8ToUTF16(cases[i].expected_chunks[j]), chunks[j]); | 272 EXPECT_EQ(UTF8ToUTF16(cases[i].expected_chunks[j]), chunks[j]); |
| 273 } | 273 } |
| 274 } | 274 } |
| 275 | 275 |
| 276 } // namespace experiments_helper | 276 } // namespace chrome_variations |
| OLD | NEW |