| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/metrics/variations/variations_associated_data.h" | |
| 6 | |
| 7 #include "base/metrics/field_trial.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace chrome_variations { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 const VariationID TEST_VALUE_A = 3300200; | |
| 15 const VariationID TEST_VALUE_B = 3300201; | |
| 16 | |
| 17 // Convenience helper to retrieve the chrome_variations::VariationID for a | |
| 18 // FieldTrial. Note that this will do the group assignment in |trial| if not | |
| 19 // already done. | |
| 20 VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) { | |
| 21 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name()); | |
| 22 } | |
| 23 | |
| 24 // Tests whether a field trial is active (i.e. group() has been called on it). | |
| 25 bool IsFieldTrialActive(const std::string& trial_name) { | |
| 26 base::FieldTrial::ActiveGroups active_groups; | |
| 27 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | |
| 28 for (size_t i = 0; i < active_groups.size(); ++i) { | |
| 29 if (active_groups[i].trial_name == trial_name) | |
| 30 return true; | |
| 31 } | |
| 32 return false; | |
| 33 } | |
| 34 | |
| 35 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. | |
| 36 scoped_refptr<base::FieldTrial> CreateFieldTrial( | |
| 37 const std::string& trial_name, | |
| 38 int total_probability, | |
| 39 const std::string& default_group_name, | |
| 40 int* default_group_number) { | |
| 41 return base::FieldTrialList::FactoryGetFieldTrial( | |
| 42 trial_name, total_probability, default_group_name, | |
| 43 base::FieldTrialList::kNoExpirationYear, 1, 1, | |
| 44 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 class VariationsAssociatedDataTest : public ::testing::Test { | |
| 50 public: | |
| 51 VariationsAssociatedDataTest() : field_trial_list_(NULL) { | |
| 52 } | |
| 53 | |
| 54 virtual ~VariationsAssociatedDataTest() { | |
| 55 // Ensure that the maps are cleared between tests, since they are stored as | |
| 56 // process singletons. | |
| 57 testing::ClearAllVariationIDs(); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 base::FieldTrialList field_trial_list_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest); | |
| 64 }; | |
| 65 | |
| 66 // Test that if the trial is immediately disabled, GetGoogleVariationID just | |
| 67 // returns the empty ID. | |
| 68 TEST_F(VariationsAssociatedDataTest, DisableImmediately) { | |
| 69 int default_group_number = -1; | |
| 70 scoped_refptr<base::FieldTrial> trial( | |
| 71 CreateFieldTrial("trial", 100, "default", &default_group_number)); | |
| 72 | |
| 73 ASSERT_EQ(default_group_number, trial->group()); | |
| 74 ASSERT_EQ(EMPTY_ID, GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial.get())); | |
| 75 } | |
| 76 | |
| 77 // Test that successfully associating the FieldTrial with some ID, and then | |
| 78 // disabling the FieldTrial actually makes GetGoogleVariationID correctly | |
| 79 // return the empty ID. | |
| 80 TEST_F(VariationsAssociatedDataTest, DisableAfterInitialization) { | |
| 81 const std::string default_name = "default"; | |
| 82 const std::string non_default_name = "non_default"; | |
| 83 | |
| 84 scoped_refptr<base::FieldTrial> trial( | |
| 85 CreateFieldTrial("trial", 100, default_name, NULL)); | |
| 86 | |
| 87 trial->AppendGroup(non_default_name, 100); | |
| 88 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial->trial_name(), | |
| 89 default_name, TEST_VALUE_A); | |
| 90 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial->trial_name(), | |
| 91 non_default_name, TEST_VALUE_B); | |
| 92 trial->Disable(); | |
| 93 ASSERT_EQ(default_name, trial->group_name()); | |
| 94 ASSERT_EQ(TEST_VALUE_A, GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial.get())); | |
| 95 } | |
| 96 | |
| 97 // Test various successful association cases. | |
| 98 TEST_F(VariationsAssociatedDataTest, AssociateGoogleVariationID) { | |
| 99 const std::string default_name1 = "default"; | |
| 100 scoped_refptr<base::FieldTrial> trial_true( | |
| 101 CreateFieldTrial("d1", 10, default_name1, NULL)); | |
| 102 const std::string winner = "TheWinner"; | |
| 103 int winner_group = trial_true->AppendGroup(winner, 10); | |
| 104 | |
| 105 // Set GoogleVariationIDs so we can verify that they were chosen correctly. | |
| 106 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(), | |
| 107 default_name1, TEST_VALUE_A); | |
| 108 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(), | |
| 109 winner, TEST_VALUE_B); | |
| 110 | |
| 111 EXPECT_EQ(winner_group, trial_true->group()); | |
| 112 EXPECT_EQ(winner, trial_true->group_name()); | |
| 113 EXPECT_EQ(TEST_VALUE_B, | |
| 114 GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get())); | |
| 115 | |
| 116 const std::string default_name2 = "default2"; | |
| 117 scoped_refptr<base::FieldTrial> trial_false( | |
| 118 CreateFieldTrial("d2", 10, default_name2, NULL)); | |
| 119 const std::string loser = "ALoser"; | |
| 120 const int loser_group = trial_false->AppendGroup(loser, 0); | |
| 121 | |
| 122 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_false->trial_name(), | |
| 123 default_name2, TEST_VALUE_A); | |
| 124 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_false->trial_name(), | |
| 125 loser, TEST_VALUE_B); | |
| 126 | |
| 127 EXPECT_NE(loser_group, trial_false->group()); | |
| 128 EXPECT_EQ(TEST_VALUE_A, | |
| 129 GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_false.get())); | |
| 130 } | |
| 131 | |
| 132 // Test that not associating a FieldTrial with any IDs ensure that the empty ID | |
| 133 // will be returned. | |
| 134 TEST_F(VariationsAssociatedDataTest, NoAssociation) { | |
| 135 const std::string default_name = "default"; | |
| 136 scoped_refptr<base::FieldTrial> no_id_trial( | |
| 137 CreateFieldTrial("d3", 10, default_name, NULL)); | |
| 138 | |
| 139 const std::string winner = "TheWinner"; | |
| 140 const int winner_group = no_id_trial->AppendGroup(winner, 10); | |
| 141 | |
| 142 // Ensure that despite the fact that a normal winner is elected, it does not | |
| 143 // have a valid VariationID associated with it. | |
| 144 EXPECT_EQ(winner_group, no_id_trial->group()); | |
| 145 EXPECT_EQ(winner, no_id_trial->group_name()); | |
| 146 EXPECT_EQ(EMPTY_ID, GetIDForTrial(GOOGLE_WEB_PROPERTIES, no_id_trial.get())); | |
| 147 } | |
| 148 | |
| 149 // Ensure that the AssociateGoogleVariationIDForce works as expected. | |
| 150 TEST_F(VariationsAssociatedDataTest, ForceAssociation) { | |
| 151 EXPECT_EQ(EMPTY_ID, | |
| 152 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group")); | |
| 153 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group", | |
| 154 TEST_VALUE_A); | |
| 155 EXPECT_EQ(TEST_VALUE_A, | |
| 156 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group")); | |
| 157 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group", | |
| 158 TEST_VALUE_B); | |
| 159 EXPECT_EQ(TEST_VALUE_A, | |
| 160 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group")); | |
| 161 AssociateGoogleVariationIDForce(GOOGLE_WEB_PROPERTIES, "trial", "group", | |
| 162 TEST_VALUE_B); | |
| 163 EXPECT_EQ(TEST_VALUE_B, | |
| 164 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group")); | |
| 165 } | |
| 166 | |
| 167 // Ensure that two collections can coexist without affecting each other. | |
| 168 TEST_F(VariationsAssociatedDataTest, CollectionsCoexist) { | |
| 169 const std::string default_name = "default"; | |
| 170 int default_group_number = -1; | |
| 171 scoped_refptr<base::FieldTrial> trial_true( | |
| 172 CreateFieldTrial("d1", 10, default_name, &default_group_number)); | |
| 173 ASSERT_EQ(default_group_number, trial_true->group()); | |
| 174 ASSERT_EQ(default_name, trial_true->group_name()); | |
| 175 | |
| 176 EXPECT_EQ(EMPTY_ID, | |
| 177 GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get())); | |
| 178 EXPECT_EQ(EMPTY_ID, | |
| 179 GetIDForTrial(GOOGLE_UPDATE_SERVICE, trial_true.get())); | |
| 180 | |
| 181 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(), | |
| 182 default_name, TEST_VALUE_A); | |
| 183 EXPECT_EQ(TEST_VALUE_A, | |
| 184 GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get())); | |
| 185 EXPECT_EQ(EMPTY_ID, | |
| 186 GetIDForTrial(GOOGLE_UPDATE_SERVICE, trial_true.get())); | |
| 187 | |
| 188 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, trial_true->trial_name(), | |
| 189 default_name, TEST_VALUE_A); | |
| 190 EXPECT_EQ(TEST_VALUE_A, | |
| 191 GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get())); | |
| 192 EXPECT_EQ(TEST_VALUE_A, | |
| 193 GetIDForTrial(GOOGLE_UPDATE_SERVICE, trial_true.get())); | |
| 194 } | |
| 195 | |
| 196 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams) { | |
| 197 const std::string kTrialName = "AssociateVariationParams"; | |
| 198 | |
| 199 { | |
| 200 std::map<std::string, std::string> params; | |
| 201 params["a"] = "10"; | |
| 202 params["b"] = "test"; | |
| 203 ASSERT_TRUE(AssociateVariationParams(kTrialName, "A", params)); | |
| 204 } | |
| 205 { | |
| 206 std::map<std::string, std::string> params; | |
| 207 params["a"] = "5"; | |
| 208 ASSERT_TRUE(AssociateVariationParams(kTrialName, "B", params)); | |
| 209 } | |
| 210 | |
| 211 base::FieldTrialList::CreateFieldTrial(kTrialName, "B"); | |
| 212 EXPECT_EQ("5", GetVariationParamValue(kTrialName, "a")); | |
| 213 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "b")); | |
| 214 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x")); | |
| 215 | |
| 216 std::map<std::string, std::string> params; | |
| 217 EXPECT_TRUE(GetVariationParams(kTrialName, ¶ms)); | |
| 218 EXPECT_EQ(1U, params.size()); | |
| 219 EXPECT_EQ("5", params["a"]); | |
| 220 } | |
| 221 | |
| 222 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams_Fail) { | |
| 223 const std::string kTrialName = "AssociateVariationParams_Fail"; | |
| 224 const std::string kGroupName = "A"; | |
| 225 | |
| 226 std::map<std::string, std::string> params; | |
| 227 params["a"] = "10"; | |
| 228 ASSERT_TRUE(AssociateVariationParams(kTrialName, kGroupName, params)); | |
| 229 params["a"] = "1"; | |
| 230 params["b"] = "2"; | |
| 231 ASSERT_FALSE(AssociateVariationParams(kTrialName, kGroupName, params)); | |
| 232 | |
| 233 base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName); | |
| 234 EXPECT_EQ("10", GetVariationParamValue(kTrialName, "a")); | |
| 235 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "b")); | |
| 236 } | |
| 237 | |
| 238 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams_TrialActiveFail) { | |
| 239 const std::string kTrialName = "AssociateVariationParams_TrialActiveFail"; | |
| 240 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | |
| 241 ASSERT_EQ("A", base::FieldTrialList::FindFullName(kTrialName)); | |
| 242 | |
| 243 std::map<std::string, std::string> params; | |
| 244 params["a"] = "10"; | |
| 245 EXPECT_FALSE(AssociateVariationParams(kTrialName, "B", params)); | |
| 246 EXPECT_FALSE(AssociateVariationParams(kTrialName, "A", params)); | |
| 247 } | |
| 248 | |
| 249 TEST_F(VariationsAssociatedDataTest, | |
| 250 AssociateVariationParams_DoesntActivateTrial) { | |
| 251 const std::string kTrialName = "AssociateVariationParams_DoesntActivateTrial"; | |
| 252 | |
| 253 ASSERT_FALSE(IsFieldTrialActive(kTrialName)); | |
| 254 scoped_refptr<base::FieldTrial> trial( | |
| 255 CreateFieldTrial(kTrialName, 100, "A", NULL)); | |
| 256 ASSERT_FALSE(IsFieldTrialActive(kTrialName)); | |
| 257 | |
| 258 std::map<std::string, std::string> params; | |
| 259 params["a"] = "10"; | |
| 260 EXPECT_TRUE(AssociateVariationParams(kTrialName, "A", params)); | |
| 261 ASSERT_FALSE(IsFieldTrialActive(kTrialName)); | |
| 262 } | |
| 263 | |
| 264 TEST_F(VariationsAssociatedDataTest, GetVariationParams_NoTrial) { | |
| 265 const std::string kTrialName = "GetVariationParams_NoParams"; | |
| 266 | |
| 267 std::map<std::string, std::string> params; | |
| 268 EXPECT_FALSE(GetVariationParams(kTrialName, ¶ms)); | |
| 269 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x")); | |
| 270 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "y")); | |
| 271 } | |
| 272 | |
| 273 TEST_F(VariationsAssociatedDataTest, GetVariationParams_NoParams) { | |
| 274 const std::string kTrialName = "GetVariationParams_NoParams"; | |
| 275 | |
| 276 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | |
| 277 | |
| 278 std::map<std::string, std::string> params; | |
| 279 EXPECT_FALSE(GetVariationParams(kTrialName, ¶ms)); | |
| 280 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x")); | |
| 281 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "y")); | |
| 282 } | |
| 283 | |
| 284 TEST_F(VariationsAssociatedDataTest, GetVariationParams_ActivatesTrial) { | |
| 285 const std::string kTrialName = "GetVariationParams_ActivatesTrial"; | |
| 286 | |
| 287 ASSERT_FALSE(IsFieldTrialActive(kTrialName)); | |
| 288 scoped_refptr<base::FieldTrial> trial( | |
| 289 CreateFieldTrial(kTrialName, 100, "A", NULL)); | |
| 290 ASSERT_FALSE(IsFieldTrialActive(kTrialName)); | |
| 291 | |
| 292 std::map<std::string, std::string> params; | |
| 293 EXPECT_FALSE(GetVariationParams(kTrialName, ¶ms)); | |
| 294 ASSERT_TRUE(IsFieldTrialActive(kTrialName)); | |
| 295 } | |
| 296 | |
| 297 TEST_F(VariationsAssociatedDataTest, GetVariationParamValue_ActivatesTrial) { | |
| 298 const std::string kTrialName = "GetVariationParamValue_ActivatesTrial"; | |
| 299 | |
| 300 ASSERT_FALSE(IsFieldTrialActive(kTrialName)); | |
| 301 scoped_refptr<base::FieldTrial> trial( | |
| 302 CreateFieldTrial(kTrialName, 100, "A", NULL)); | |
| 303 ASSERT_FALSE(IsFieldTrialActive(kTrialName)); | |
| 304 | |
| 305 std::map<std::string, std::string> params; | |
| 306 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x")); | |
| 307 ASSERT_TRUE(IsFieldTrialActive(kTrialName)); | |
| 308 } | |
| 309 | |
| 310 } // namespace chrome_variations | |
| OLD | NEW |