variations_associated_data_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "components/variations/variations_associated_data.h"
  5. #include "base/metrics/field_trial.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace variations {
  8. namespace {
  9. const VariationID TEST_VALUE_A = 3300200;
  10. const VariationID TEST_VALUE_B = 3300201;
  11. // Convenience helper to retrieve the variations::VariationID for a FieldTrial.
  12. // Note that this will do the group assignment in |trial| if not already done.
  13. VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) {
  14. return GetGoogleVariationID(key, trial->trial_name(), trial->group_name());
  15. }
  16. // Call FieldTrialList::FactoryGetFieldTrial().
  17. scoped_refptr<base::FieldTrial> CreateFieldTrial(
  18. const std::string& trial_name,
  19. int total_probability,
  20. const std::string& default_group_name,
  21. int* default_group_number) {
  22. return base::FieldTrialList::FactoryGetFieldTrial(
  23. trial_name, total_probability, default_group_name,
  24. base::FieldTrial::SESSION_RANDOMIZED, default_group_number);
  25. }
  26. } // namespace
  27. class VariationsAssociatedDataTest : public ::testing::Test {
  28. public:
  29. VariationsAssociatedDataTest() {}
  30. VariationsAssociatedDataTest(const VariationsAssociatedDataTest&) = delete;
  31. VariationsAssociatedDataTest& operator=(const VariationsAssociatedDataTest&) =
  32. delete;
  33. ~VariationsAssociatedDataTest() override {
  34. // Ensure that the maps are cleared between tests, since they are stored as
  35. // process singletons.
  36. testing::ClearAllVariationIDs();
  37. }
  38. };
  39. // Test that if the trial is immediately disabled, GetGoogleVariationID just
  40. // returns the empty ID.
  41. TEST_F(VariationsAssociatedDataTest, DisableImmediately) {
  42. int default_group_number = -1;
  43. scoped_refptr<base::FieldTrial> trial(
  44. CreateFieldTrial("trial", 100, "default", &default_group_number));
  45. ASSERT_EQ(default_group_number, trial->group());
  46. for (int i = 0; i < ID_COLLECTION_COUNT; ++i) {
  47. ASSERT_EQ(EMPTY_ID,
  48. GetIDForTrial(static_cast<IDCollectionKey>(i), trial.get()));
  49. }
  50. }
  51. // Test that successfully associating the FieldTrial with some ID, and then
  52. // disabling the FieldTrial actually makes GetGoogleVariationID correctly
  53. // return the empty ID.
  54. TEST_F(VariationsAssociatedDataTest, DisableAfterInitialization) {
  55. const std::string default_name = "default";
  56. const std::string non_default_name = "non_default";
  57. scoped_refptr<base::FieldTrial> trial(
  58. CreateFieldTrial("trial", 100, default_name, nullptr));
  59. trial->AppendGroup(non_default_name, 100);
  60. AssociateGoogleVariationID(GOOGLE_APP, trial->trial_name(), default_name,
  61. TEST_VALUE_A);
  62. AssociateGoogleVariationID(GOOGLE_APP, trial->trial_name(), non_default_name,
  63. TEST_VALUE_B);
  64. trial->Disable();
  65. ASSERT_EQ(default_name, trial->group_name());
  66. ASSERT_EQ(TEST_VALUE_A, GetIDForTrial(GOOGLE_APP, trial.get()));
  67. }
  68. // Test various successful association cases.
  69. TEST_F(VariationsAssociatedDataTest, AssociateGoogleVariationID) {
  70. const std::string default_name1 = "default";
  71. scoped_refptr<base::FieldTrial> trial_true(
  72. CreateFieldTrial("d1", 10, default_name1, nullptr));
  73. const std::string winner = "TheWinner";
  74. int winner_group = trial_true->AppendGroup(winner, 10);
  75. // Set GoogleVariationIDs so we can verify that they were chosen correctly.
  76. AssociateGoogleVariationID(GOOGLE_APP, trial_true->trial_name(),
  77. default_name1, TEST_VALUE_A);
  78. AssociateGoogleVariationID(GOOGLE_APP, trial_true->trial_name(), winner,
  79. TEST_VALUE_B);
  80. EXPECT_EQ(winner_group, trial_true->group());
  81. EXPECT_EQ(winner, trial_true->group_name());
  82. EXPECT_EQ(TEST_VALUE_B, GetIDForTrial(GOOGLE_APP, trial_true.get()));
  83. const std::string default_name2 = "default2";
  84. scoped_refptr<base::FieldTrial> trial_false(
  85. CreateFieldTrial("d2", 10, default_name2, nullptr));
  86. const std::string loser = "ALoser";
  87. const int loser_group = trial_false->AppendGroup(loser, 0);
  88. AssociateGoogleVariationID(GOOGLE_APP, trial_false->trial_name(),
  89. default_name2, TEST_VALUE_A);
  90. AssociateGoogleVariationID(GOOGLE_APP, trial_false->trial_name(), loser,
  91. TEST_VALUE_B);
  92. EXPECT_NE(loser_group, trial_false->group());
  93. EXPECT_EQ(TEST_VALUE_A, GetIDForTrial(GOOGLE_APP, trial_false.get()));
  94. }
  95. // Test that not associating a FieldTrial with any IDs ensure that the empty ID
  96. // will be returned.
  97. TEST_F(VariationsAssociatedDataTest, NoAssociation) {
  98. const std::string default_name = "default";
  99. scoped_refptr<base::FieldTrial> no_id_trial(
  100. CreateFieldTrial("d3", 10, default_name, nullptr));
  101. const std::string winner = "TheWinner";
  102. const int winner_group = no_id_trial->AppendGroup(winner, 10);
  103. // Ensure that despite the fact that a normal winner is elected, it does not
  104. // have a valid VariationID associated with it.
  105. EXPECT_EQ(winner_group, no_id_trial->group());
  106. EXPECT_EQ(winner, no_id_trial->group_name());
  107. for (int i = 0; i < ID_COLLECTION_COUNT; ++i) {
  108. ASSERT_EQ(EMPTY_ID, GetIDForTrial(static_cast<IDCollectionKey>(i),
  109. no_id_trial.get()));
  110. }
  111. }
  112. // Ensure that the AssociateGoogleVariationIDForce works as expected.
  113. TEST_F(VariationsAssociatedDataTest, ForceAssociation) {
  114. EXPECT_EQ(EMPTY_ID, GetGoogleVariationID(GOOGLE_APP, "trial", "group"));
  115. AssociateGoogleVariationID(GOOGLE_APP, "trial", "group", TEST_VALUE_A);
  116. EXPECT_EQ(TEST_VALUE_A, GetGoogleVariationID(GOOGLE_APP, "trial", "group"));
  117. AssociateGoogleVariationID(GOOGLE_APP, "trial", "group", TEST_VALUE_B);
  118. EXPECT_EQ(TEST_VALUE_A, GetGoogleVariationID(GOOGLE_APP, "trial", "group"));
  119. AssociateGoogleVariationIDForce(GOOGLE_APP, "trial", "group", TEST_VALUE_B);
  120. EXPECT_EQ(TEST_VALUE_B, GetGoogleVariationID(GOOGLE_APP, "trial", "group"));
  121. }
  122. } // namespace variations