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/experiment_labels.h" 6 7 #include <set> 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/metrics/field_trial.h" 13 #include "base/strings/string_split.h" 14 #include "base/strings/utf_string_conversions.h" 15 #include "components/variations/variations_associated_data.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace chrome_variations { 19 20 TEST(ExperimentLabelsTest, BuildGoogleUpdateExperimentLabel) { 21 const VariationID TEST_VALUE_A = 3300200; 22 const VariationID TEST_VALUE_B = 3300201; 23 const VariationID TEST_VALUE_C = 3300202; 24 const VariationID TEST_VALUE_D = 3300203; 25 26 struct { 27 const char* active_group_pairs; 28 const char* expected_ids; 29 } test_cases[] = { 30 // Empty group. 31 {"", ""}, 32 // Group of 1. 33 {"FieldTrialA#Default", "3300200"}, 34 // Group of 1, doesn't have an associated ID. 35 {"FieldTrialA#DoesNotExist", ""}, 36 // Group of 3. 37 {"FieldTrialA#Default#FieldTrialB#Group1#FieldTrialC#Default", 38 "3300200#3300201#3300202"}, 39 // Group of 3, one doesn't have an associated ID. 40 {"FieldTrialA#Default#FieldTrialB#DoesNotExist#FieldTrialC#Default", 41 "3300200#3300202"}, 42 // Group of 3, all three don't have an associated ID. 43 {"FieldTrialX#Default#FieldTrialB#DoesNotExist#FieldTrialC#Default", 44 "3300202"}, 45 }; 46 47 // Register a few VariationIDs. 48 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialA", "Default", 49 TEST_VALUE_A); 50 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialB", "Group1", 51 TEST_VALUE_B); 52 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialC", "Default", 53 TEST_VALUE_C); 54 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialD", "Default", 55 TEST_VALUE_D); // Not actually used. 56 57 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 58 // Parse the input groups. 59 base::FieldTrial::ActiveGroups groups; 60 std::vector<std::string> group_data; 61 base::SplitString(test_cases[i].active_group_pairs, '#', &group_data); 62 ASSERT_EQ(0U, group_data.size() % 2); 63 for (size_t j = 0; j < group_data.size(); j += 2) { 64 base::FieldTrial::ActiveGroup group; 65 group.trial_name = group_data[j]; 66 group.group_name = group_data[j + 1]; 67 groups.push_back(group); 68 } 69 70 // Parse the expected output. 71 std::vector<std::string> expected_ids_list; 72 base::SplitString(test_cases[i].expected_ids, '#', &expected_ids_list); 73 74 std::string experiment_labels_string = UTF16ToUTF8( 75 BuildGoogleUpdateExperimentLabel(groups)); 76 77 // Split the VariationIDs from the labels for verification below. 78 std::vector<std::string> split_labels; 79 std::set<std::string> parsed_ids; 80 base::SplitString(experiment_labels_string, ';', &split_labels); 81 for (std::vector<std::string>::const_iterator it = split_labels.begin(); 82 it != split_labels.end(); ++it) { 83 // The ID is precisely between the '=' and '|' characters in each label. 84 size_t index_of_equals = it->find('='); 85 size_t index_of_pipe = it->find('|'); 86 ASSERT_NE(std::string::npos, index_of_equals); 87 ASSERT_NE(std::string::npos, index_of_pipe); 88 ASSERT_GT(index_of_pipe, index_of_equals); 89 parsed_ids.insert(it->substr(index_of_equals + 1, 90 index_of_pipe - index_of_equals - 1)); 91 } 92 93 // Verify that the resulting string contains each of the expected labels, 94 // and nothing more. Note that the date is stripped out and ignored. 95 for (std::vector<std::string>::const_iterator it = 96 expected_ids_list.begin(); it != expected_ids_list.end(); ++it) { 97 std::set<std::string>::iterator it2 = parsed_ids.find(*it); 98 EXPECT_TRUE(parsed_ids.end() != it2); 99 parsed_ids.erase(it2); 100 } 101 EXPECT_TRUE(parsed_ids.empty()); 102 } // for 103 } 104 105 TEST(ExperimentLabelsTest, CombineExperimentLabels) { 106 struct { 107 const char* variations_labels; 108 const char* other_labels; 109 const char* expected_label; 110 } test_cases[] = { 111 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT", 112 "C=D|Tue, 21 Jan 2014 15:30:21 GMT", 113 "C=D|Tue, 21 Jan 2014 15:30:21 GMT;A=B|Tue, 21 Jan 2014 15:30:21 GMT"}, 114 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT", 115 "", 116 "A=B|Tue, 21 Jan 2014 15:30:21 GMT"}, 117 {"", 118 "A=B|Tue, 21 Jan 2014 15:30:21 GMT", 119 "A=B|Tue, 21 Jan 2014 15:30:21 GMT"}, 120 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT;C=D|Tue, 21 Jan 2014 15:30:21 GMT", 121 "P=Q|Tue, 21 Jan 2014 15:30:21 GMT;X=Y|Tue, 21 Jan 2014 15:30:21 GMT", 122 "P=Q|Tue, 21 Jan 2014 15:30:21 GMT;X=Y|Tue, 21 Jan 2014 15:30:21 GMT;" 123 "A=B|Tue, 21 Jan 2014 15:30:21 GMT;C=D|Tue, 21 Jan 2014 15:30:21 GMT"}, 124 {"", 125 "", 126 ""}, 127 }; 128 129 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 130 std::string result = UTF16ToUTF8(CombineExperimentLabels( 131 ASCIIToUTF16(test_cases[i].variations_labels), 132 ASCIIToUTF16(test_cases[i].other_labels))); 133 EXPECT_EQ(test_cases[i].expected_label, result); 134 } 135 } 136 137 TEST(ExperimentLabelsTest, ExtractNonVariationLabels) { 138 struct { 139 const char* input_label; 140 const char* expected_output; 141 } test_cases[] = { 142 // Empty 143 {"", ""}, 144 // One 145 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT", 146 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, 147 // Three 148 {"CrVar1=123|Tue, 21 Jan 2014 15:30:21 GMT;" 149 "experiment1=456|Tue, 21 Jan 2014 15:30:21 GMT;" 150 "experiment2=789|Tue, 21 Jan 2014 15:30:21 GMT;" 151 "CrVar1=123|Tue, 21 Jan 2014 15:30:21 GMT", 152 "experiment1=456|Tue, 21 Jan 2014 15:30:21 GMT;" 153 "experiment2=789|Tue, 21 Jan 2014 15:30:21 GMT"}, 154 // One and one Variation 155 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;" 156 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT", 157 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, 158 // One and one Variation, flipped 159 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;" 160 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT", 161 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, 162 // Sandwiched 163 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;" 164 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;" 165 "CrVar2=3310003|Tue, 21 Jan 2014 15:30:21 GMT;" 166 "CrVar3=3310004|Tue, 21 Jan 2014 15:30:21 GMT", 167 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, 168 // Only Variations 169 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;" 170 "CrVar2=3310003|Tue, 21 Jan 2014 15:30:21 GMT;" 171 "CrVar3=3310004|Tue, 21 Jan 2014 15:30:21 GMT", 172 ""}, 173 // Empty values 174 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;" 175 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT", 176 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, 177 // Trailing semicolon 178 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;" 179 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;", // Note the semi here. 180 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, 181 // Semis 182 {";;;;", ""}, 183 }; 184 185 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 186 std::string non_variation_labels = UTF16ToUTF8( 187 ExtractNonVariationLabels(ASCIIToUTF16(test_cases[i].input_label))); 188 EXPECT_EQ(test_cases[i].expected_output, non_variation_labels); 189 } 190 } 191 192 } // namespace chrome_variations 193