Home | History | Annotate | Download | only in variations
      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 "components/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     testing::ClearAllVariationParams();
     59   }
     60 
     61  private:
     62   base::FieldTrialList field_trial_list_;
     63 
     64   DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest);
     65 };
     66 
     67 // Test that if the trial is immediately disabled, GetGoogleVariationID just
     68 // returns the empty ID.
     69 TEST_F(VariationsAssociatedDataTest, DisableImmediately) {
     70   int default_group_number = -1;
     71   scoped_refptr<base::FieldTrial> trial(
     72       CreateFieldTrial("trial", 100, "default", &default_group_number));
     73 
     74   ASSERT_EQ(default_group_number, trial->group());
     75   ASSERT_EQ(EMPTY_ID, GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial.get()));
     76 }
     77 
     78 // Test that successfully associating the FieldTrial with some ID, and then
     79 // disabling the FieldTrial actually makes GetGoogleVariationID correctly
     80 // return the empty ID.
     81 TEST_F(VariationsAssociatedDataTest, DisableAfterInitialization) {
     82   const std::string default_name = "default";
     83   const std::string non_default_name = "non_default";
     84 
     85   scoped_refptr<base::FieldTrial> trial(
     86       CreateFieldTrial("trial", 100, default_name, NULL));
     87 
     88   trial->AppendGroup(non_default_name, 100);
     89   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial->trial_name(),
     90       default_name, TEST_VALUE_A);
     91   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial->trial_name(),
     92       non_default_name, TEST_VALUE_B);
     93   trial->Disable();
     94   ASSERT_EQ(default_name, trial->group_name());
     95   ASSERT_EQ(TEST_VALUE_A, GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial.get()));
     96 }
     97 
     98 // Test various successful association cases.
     99 TEST_F(VariationsAssociatedDataTest, AssociateGoogleVariationID) {
    100   const std::string default_name1 = "default";
    101   scoped_refptr<base::FieldTrial> trial_true(
    102       CreateFieldTrial("d1", 10, default_name1, NULL));
    103   const std::string winner = "TheWinner";
    104   int winner_group = trial_true->AppendGroup(winner, 10);
    105 
    106   // Set GoogleVariationIDs so we can verify that they were chosen correctly.
    107   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(),
    108       default_name1, TEST_VALUE_A);
    109   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(),
    110       winner, TEST_VALUE_B);
    111 
    112   EXPECT_EQ(winner_group, trial_true->group());
    113   EXPECT_EQ(winner, trial_true->group_name());
    114   EXPECT_EQ(TEST_VALUE_B,
    115             GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get()));
    116 
    117   const std::string default_name2 = "default2";
    118   scoped_refptr<base::FieldTrial> trial_false(
    119       CreateFieldTrial("d2", 10, default_name2, NULL));
    120   const std::string loser = "ALoser";
    121   const int loser_group = trial_false->AppendGroup(loser, 0);
    122 
    123   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_false->trial_name(),
    124       default_name2, TEST_VALUE_A);
    125   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_false->trial_name(),
    126       loser, TEST_VALUE_B);
    127 
    128   EXPECT_NE(loser_group, trial_false->group());
    129   EXPECT_EQ(TEST_VALUE_A,
    130             GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_false.get()));
    131 }
    132 
    133 // Test that not associating a FieldTrial with any IDs ensure that the empty ID
    134 // will be returned.
    135 TEST_F(VariationsAssociatedDataTest, NoAssociation) {
    136   const std::string default_name = "default";
    137   scoped_refptr<base::FieldTrial> no_id_trial(
    138       CreateFieldTrial("d3", 10, default_name, NULL));
    139 
    140   const std::string winner = "TheWinner";
    141   const int winner_group = no_id_trial->AppendGroup(winner, 10);
    142 
    143   // Ensure that despite the fact that a normal winner is elected, it does not
    144   // have a valid VariationID associated with it.
    145   EXPECT_EQ(winner_group, no_id_trial->group());
    146   EXPECT_EQ(winner, no_id_trial->group_name());
    147   EXPECT_EQ(EMPTY_ID, GetIDForTrial(GOOGLE_WEB_PROPERTIES, no_id_trial.get()));
    148 }
    149 
    150 // Ensure that the AssociateGoogleVariationIDForce works as expected.
    151 TEST_F(VariationsAssociatedDataTest, ForceAssociation) {
    152   EXPECT_EQ(EMPTY_ID,
    153             GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
    154   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group",
    155                              TEST_VALUE_A);
    156   EXPECT_EQ(TEST_VALUE_A,
    157             GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
    158   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group",
    159                              TEST_VALUE_B);
    160   EXPECT_EQ(TEST_VALUE_A,
    161             GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
    162   AssociateGoogleVariationIDForce(GOOGLE_WEB_PROPERTIES, "trial", "group",
    163                                   TEST_VALUE_B);
    164   EXPECT_EQ(TEST_VALUE_B,
    165             GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
    166 }
    167 
    168 // Ensure that two collections can coexist without affecting each other.
    169 TEST_F(VariationsAssociatedDataTest, CollectionsCoexist) {
    170   const std::string default_name = "default";
    171   int default_group_number = -1;
    172   scoped_refptr<base::FieldTrial> trial_true(
    173       CreateFieldTrial("d1", 10, default_name, &default_group_number));
    174   ASSERT_EQ(default_group_number, trial_true->group());
    175   ASSERT_EQ(default_name, trial_true->group_name());
    176 
    177   EXPECT_EQ(EMPTY_ID,
    178             GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get()));
    179   EXPECT_EQ(EMPTY_ID,
    180             GetIDForTrial(GOOGLE_UPDATE_SERVICE, trial_true.get()));
    181 
    182   AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(),
    183       default_name, TEST_VALUE_A);
    184   EXPECT_EQ(TEST_VALUE_A,
    185             GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get()));
    186   EXPECT_EQ(EMPTY_ID,
    187             GetIDForTrial(GOOGLE_UPDATE_SERVICE, trial_true.get()));
    188 
    189   AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, trial_true->trial_name(),
    190       default_name, TEST_VALUE_A);
    191   EXPECT_EQ(TEST_VALUE_A,
    192             GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get()));
    193   EXPECT_EQ(TEST_VALUE_A,
    194             GetIDForTrial(GOOGLE_UPDATE_SERVICE, trial_true.get()));
    195 }
    196 
    197 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams) {
    198   const std::string kTrialName = "AssociateVariationParams";
    199 
    200   {
    201     std::map<std::string, std::string> params;
    202     params["a"] = "10";
    203     params["b"] = "test";
    204     ASSERT_TRUE(AssociateVariationParams(kTrialName, "A", params));
    205   }
    206   {
    207     std::map<std::string, std::string> params;
    208     params["a"] = "5";
    209     ASSERT_TRUE(AssociateVariationParams(kTrialName, "B", params));
    210   }
    211 
    212   base::FieldTrialList::CreateFieldTrial(kTrialName, "B");
    213   EXPECT_EQ("5", GetVariationParamValue(kTrialName, "a"));
    214   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "b"));
    215   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
    216 
    217   std::map<std::string, std::string> params;
    218   EXPECT_TRUE(GetVariationParams(kTrialName, &params));
    219   EXPECT_EQ(1U, params.size());
    220   EXPECT_EQ("5", params["a"]);
    221 }
    222 
    223 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams_Fail) {
    224   const std::string kTrialName = "AssociateVariationParams_Fail";
    225   const std::string kGroupName = "A";
    226 
    227   std::map<std::string, std::string> params;
    228   params["a"] = "10";
    229   ASSERT_TRUE(AssociateVariationParams(kTrialName, kGroupName, params));
    230   params["a"] = "1";
    231   params["b"] = "2";
    232   ASSERT_FALSE(AssociateVariationParams(kTrialName, kGroupName, params));
    233 
    234   base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName);
    235   EXPECT_EQ("10", GetVariationParamValue(kTrialName, "a"));
    236   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "b"));
    237 }
    238 
    239 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams_TrialActiveFail) {
    240   const std::string kTrialName = "AssociateVariationParams_TrialActiveFail";
    241   base::FieldTrialList::CreateFieldTrial(kTrialName, "A");
    242   ASSERT_EQ("A", base::FieldTrialList::FindFullName(kTrialName));
    243 
    244   std::map<std::string, std::string> params;
    245   params["a"] = "10";
    246   EXPECT_FALSE(AssociateVariationParams(kTrialName, "B", params));
    247   EXPECT_FALSE(AssociateVariationParams(kTrialName, "A", params));
    248 }
    249 
    250 TEST_F(VariationsAssociatedDataTest,
    251        AssociateVariationParams_DoesntActivateTrial) {
    252   const std::string kTrialName = "AssociateVariationParams_DoesntActivateTrial";
    253 
    254   ASSERT_FALSE(IsFieldTrialActive(kTrialName));
    255   scoped_refptr<base::FieldTrial> trial(
    256       CreateFieldTrial(kTrialName, 100, "A", NULL));
    257   ASSERT_FALSE(IsFieldTrialActive(kTrialName));
    258 
    259   std::map<std::string, std::string> params;
    260   params["a"] = "10";
    261   EXPECT_TRUE(AssociateVariationParams(kTrialName, "A", params));
    262   ASSERT_FALSE(IsFieldTrialActive(kTrialName));
    263 }
    264 
    265 TEST_F(VariationsAssociatedDataTest, GetVariationParams_NoTrial) {
    266   const std::string kTrialName = "GetVariationParams_NoParams";
    267 
    268   std::map<std::string, std::string> params;
    269   EXPECT_FALSE(GetVariationParams(kTrialName, &params));
    270   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
    271   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "y"));
    272 }
    273 
    274 TEST_F(VariationsAssociatedDataTest, GetVariationParams_NoParams) {
    275   const std::string kTrialName = "GetVariationParams_NoParams";
    276 
    277   base::FieldTrialList::CreateFieldTrial(kTrialName, "A");
    278 
    279   std::map<std::string, std::string> params;
    280   EXPECT_FALSE(GetVariationParams(kTrialName, &params));
    281   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
    282   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "y"));
    283 }
    284 
    285 TEST_F(VariationsAssociatedDataTest, GetVariationParams_ActivatesTrial) {
    286   const std::string kTrialName = "GetVariationParams_ActivatesTrial";
    287 
    288   ASSERT_FALSE(IsFieldTrialActive(kTrialName));
    289   scoped_refptr<base::FieldTrial> trial(
    290       CreateFieldTrial(kTrialName, 100, "A", NULL));
    291   ASSERT_FALSE(IsFieldTrialActive(kTrialName));
    292 
    293   std::map<std::string, std::string> params;
    294   EXPECT_FALSE(GetVariationParams(kTrialName, &params));
    295   ASSERT_TRUE(IsFieldTrialActive(kTrialName));
    296 }
    297 
    298 TEST_F(VariationsAssociatedDataTest, GetVariationParamValue_ActivatesTrial) {
    299   const std::string kTrialName = "GetVariationParamValue_ActivatesTrial";
    300 
    301   ASSERT_FALSE(IsFieldTrialActive(kTrialName));
    302   scoped_refptr<base::FieldTrial> trial(
    303       CreateFieldTrial(kTrialName, 100, "A", NULL));
    304   ASSERT_FALSE(IsFieldTrialActive(kTrialName));
    305 
    306   std::map<std::string, std::string> params;
    307   EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
    308   ASSERT_TRUE(IsFieldTrialActive(kTrialName));
    309 }
    310 
    311 }  // namespace chrome_variations
    312