| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "components/variations/variations_associated_data.h" | 5 #include "components/variations/variations_associated_data.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace variations { | 12 namespace variations { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 const VariationID TEST_VALUE_A = 3300200; | 16 const VariationID TEST_VALUE_A = 3300200; |
| 16 const VariationID TEST_VALUE_B = 3300201; | 17 const VariationID TEST_VALUE_B = 3300201; |
| 17 | 18 |
| 18 // Convenience helper to retrieve the variations::VariationID for a FieldTrial. | 19 // Convenience helper to retrieve the variations::VariationID for a FieldTrial. |
| 19 // Note that this will do the group assignment in |trial| if not already done. | 20 // Note that this will do the group assignment in |trial| if not already done. |
| 20 VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) { | 21 VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) { |
| 21 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name()); | 22 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name()); |
| 22 } | 23 } |
| 23 | 24 |
| 24 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. | 25 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. |
| 25 scoped_refptr<base::FieldTrial> CreateFieldTrial( | 26 scoped_refptr<base::FieldTrial> CreateFieldTrial( |
| 26 const std::string& trial_name, | 27 const std::string& trial_name, |
| 27 int total_probability, | 28 int total_probability, |
| 28 const std::string& default_group_name, | 29 const std::string& default_group_name, |
| 29 int* default_group_number) { | 30 int* default_group_number) { |
| 30 return base::FieldTrialList::FactoryGetFieldTrial( | 31 return base::FieldTrialList::FactoryGetFieldTrial( |
| 31 trial_name, total_probability, default_group_name, | 32 trial_name, total_probability, default_group_name, |
| 32 base::FieldTrialList::kNoExpirationYear, 1, 1, | 33 base::FieldTrialList::kNoExpirationYear, 1, 1, |
| 33 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); | 34 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); |
| 34 } | 35 } |
| 35 | 36 |
| 37 void CreateFeatureWithTrial(const base::Feature& feature, |
| 38 base::FeatureList::OverrideState override_state, |
| 39 base::FieldTrial* trial) { |
| 40 base::FeatureList::ClearInstanceForTesting(); |
| 41 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 42 feature_list->RegisterFieldTrialOverride(feature.name, override_state, |
| 43 trial); |
| 44 base::FeatureList::SetInstance(std::move(feature_list)); |
| 45 } |
| 46 |
| 36 } // namespace | 47 } // namespace |
| 37 | 48 |
| 38 class VariationsAssociatedDataTest : public ::testing::Test { | 49 class VariationsAssociatedDataTest : public ::testing::Test { |
| 39 public: | 50 public: |
| 40 VariationsAssociatedDataTest() : field_trial_list_(NULL) { | 51 VariationsAssociatedDataTest() : field_trial_list_(NULL) { |
| 41 } | 52 } |
| 42 | 53 |
| 43 ~VariationsAssociatedDataTest() override { | 54 ~VariationsAssociatedDataTest() override { |
| 44 // Ensure that the maps are cleared between tests, since they are stored as | 55 // Ensure that the maps are cleared between tests, since they are stored as |
| 45 // process singletons. | 56 // process singletons. |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName)); | 350 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName)); |
| 340 scoped_refptr<base::FieldTrial> trial( | 351 scoped_refptr<base::FieldTrial> trial( |
| 341 CreateFieldTrial(kTrialName, 100, "A", NULL)); | 352 CreateFieldTrial(kTrialName, 100, "A", NULL)); |
| 342 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName)); | 353 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName)); |
| 343 | 354 |
| 344 std::map<std::string, std::string> params; | 355 std::map<std::string, std::string> params; |
| 345 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x")); | 356 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x")); |
| 346 ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName)); | 357 ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName)); |
| 347 } | 358 } |
| 348 | 359 |
| 360 TEST_F(VariationsAssociatedDataTest, GetVariationParamsByFeature) { |
| 361 const std::string kTrialName = "GetVariationParamsByFeature"; |
| 362 const base::Feature kFeature{"TestFeature", |
| 363 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 364 |
| 365 std::map<std::string, std::string> params; |
| 366 params["x"] = "1"; |
| 367 variations::AssociateVariationParams(kTrialName, "A", params); |
| 368 scoped_refptr<base::FieldTrial> trial( |
| 369 CreateFieldTrial(kTrialName, 100, "A", NULL)); |
| 370 |
| 371 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 372 trial.get()); |
| 373 |
| 374 std::map<std::string, std::string> actualParams; |
| 375 EXPECT_TRUE(GetVariationParamsByFeature(kFeature, &actualParams)); |
| 376 EXPECT_EQ(params, actualParams); |
| 377 } |
| 378 |
| 379 TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature) { |
| 380 const std::string kTrialName = "GetVariationParamsByFeature"; |
| 381 const base::Feature kFeature{"TestFeature", |
| 382 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 383 |
| 384 std::map<std::string, std::string> params; |
| 385 params["x"] = "1"; |
| 386 variations::AssociateVariationParams(kTrialName, "A", params); |
| 387 scoped_refptr<base::FieldTrial> trial( |
| 388 CreateFieldTrial(kTrialName, 100, "A", NULL)); |
| 389 |
| 390 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 391 trial.get()); |
| 392 |
| 393 std::map<std::string, std::string> actualParams; |
| 394 EXPECT_EQ(params["x"], GetVariationParamValueByFeature(kFeature, "x")); |
| 395 } |
| 396 |
| 397 TEST_F(VariationsAssociatedDataTest, GetVariationParamsByFeature_Disable) { |
| 398 const std::string kTrialName = "GetVariationParamsByFeature"; |
| 399 const base::Feature kFeature{"TestFeature", |
| 400 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 401 |
| 402 std::map<std::string, std::string> params; |
| 403 params["x"] = "1"; |
| 404 variations::AssociateVariationParams(kTrialName, "A", params); |
| 405 scoped_refptr<base::FieldTrial> trial( |
| 406 CreateFieldTrial(kTrialName, 100, "A", NULL)); |
| 407 |
| 408 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 409 trial.get()); |
| 410 |
| 411 std::map<std::string, std::string> actualParams; |
| 412 EXPECT_FALSE(GetVariationParamsByFeature(kFeature, &actualParams)); |
| 413 } |
| 414 |
| 415 TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature_Disable) { |
| 416 const std::string kTrialName = "GetVariationParamsByFeature"; |
| 417 const base::Feature kFeature{"TestFeature", |
| 418 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 419 |
| 420 std::map<std::string, std::string> params; |
| 421 params["x"] = "1"; |
| 422 variations::AssociateVariationParams(kTrialName, "A", params); |
| 423 scoped_refptr<base::FieldTrial> trial( |
| 424 CreateFieldTrial(kTrialName, 100, "A", NULL)); |
| 425 |
| 426 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 427 trial.get()); |
| 428 |
| 429 std::map<std::string, std::string> actualParams; |
| 430 EXPECT_EQ(std::string(), GetVariationParamValueByFeature(kFeature, "x")); |
| 431 } |
| 432 |
| 349 } // namespace variations | 433 } // namespace variations |
| OLD | NEW |