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