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
3 // found in the LICENSE file.
5 #include "chrome/common/variations/experiment_labels.h"
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"
18 namespace chrome_variations
{
20 TEST(ExperimentLabelsTest
, BuildGoogleUpdateExperimentLabel
) {
21 const variations::VariationID TEST_VALUE_A
= 3300200;
22 const variations::VariationID TEST_VALUE_B
= 3300201;
23 const variations::VariationID TEST_VALUE_C
= 3300202;
24 const variations::VariationID TEST_VALUE_D
= 3300203;
27 const char* active_group_pairs
;
28 const char* expected_ids
;
33 {"FieldTrialA#Default", "3300200"},
34 // Group of 1, doesn't have an associated ID.
35 {"FieldTrialA#DoesNotExist", ""},
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",
42 // Group of 3, all three don't have an associated ID.
43 {"FieldTrialX#Default#FieldTrialB#DoesNotExist#FieldTrialC#Default",
47 // Register a few VariationIDs.
48 AssociateGoogleVariationID(variations::GOOGLE_UPDATE_SERVICE
, "FieldTrialA",
49 "Default", TEST_VALUE_A
);
50 AssociateGoogleVariationID(variations::GOOGLE_UPDATE_SERVICE
, "FieldTrialB",
51 "Group1", TEST_VALUE_B
);
52 AssociateGoogleVariationID(variations::GOOGLE_UPDATE_SERVICE
, "FieldTrialC",
53 "Default", TEST_VALUE_C
);
54 AssociateGoogleVariationID(variations::GOOGLE_UPDATE_SERVICE
, "FieldTrialD",
55 "Default", TEST_VALUE_D
); // Not actually used.
57 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
58 // Parse the input groups.
59 base::FieldTrial::ActiveGroups groups
;
60 std::vector
<std::string
> group_data
= base::SplitString(
61 test_cases
[i
].active_group_pairs
, "#",
62 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
63 ASSERT_EQ(0U, group_data
.size() % 2);
64 for (size_t j
= 0; j
< group_data
.size(); j
+= 2) {
65 base::FieldTrial::ActiveGroup group
;
66 group
.trial_name
= group_data
[j
];
67 group
.group_name
= group_data
[j
+ 1];
68 groups
.push_back(group
);
71 // Parse the expected output.
72 std::vector
<std::string
> expected_ids_list
= base::SplitString(
73 test_cases
[i
].expected_ids
, "#",
74 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
76 std::string experiment_labels_string
= base::UTF16ToUTF8(
77 BuildGoogleUpdateExperimentLabel(groups
));
79 // Split the VariationIDs from the labels for verification below.
80 std::set
<std::string
> parsed_ids
;
81 for (const std::string
& label
: base::SplitString(
82 experiment_labels_string
, ";",
83 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
)) {
84 // The ID is precisely between the '=' and '|' characters in each label.
85 size_t index_of_equals
= label
.find('=');
86 size_t index_of_pipe
= label
.find('|');
87 ASSERT_NE(std::string::npos
, index_of_equals
);
88 ASSERT_NE(std::string::npos
, index_of_pipe
);
89 ASSERT_GT(index_of_pipe
, index_of_equals
);
90 parsed_ids
.insert(label
.substr(index_of_equals
+ 1,
91 index_of_pipe
- index_of_equals
- 1));
94 // Verify that the resulting string contains each of the expected labels,
95 // and nothing more. Note that the date is stripped out and ignored.
96 for (std::vector
<std::string
>::const_iterator it
=
97 expected_ids_list
.begin(); it
!= expected_ids_list
.end(); ++it
) {
98 std::set
<std::string
>::iterator it2
= parsed_ids
.find(*it
);
99 EXPECT_TRUE(parsed_ids
.end() != it2
);
100 parsed_ids
.erase(it2
);
102 EXPECT_TRUE(parsed_ids
.empty());
106 TEST(ExperimentLabelsTest
, CombineExperimentLabels
) {
108 const char* variations_labels
;
109 const char* other_labels
;
110 const char* expected_label
;
112 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT",
113 "C=D|Tue, 21 Jan 2014 15:30:21 GMT",
114 "C=D|Tue, 21 Jan 2014 15:30:21 GMT;A=B|Tue, 21 Jan 2014 15:30:21 GMT"},
115 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT",
117 "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"},
121 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT;C=D|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 "P=Q|Tue, 21 Jan 2014 15:30:21 GMT;X=Y|Tue, 21 Jan 2014 15:30:21 GMT;"
124 "A=B|Tue, 21 Jan 2014 15:30:21 GMT;C=D|Tue, 21 Jan 2014 15:30:21 GMT"},
130 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
131 std::string result
= base::UTF16ToUTF8(CombineExperimentLabels(
132 base::ASCIIToUTF16(test_cases
[i
].variations_labels
),
133 base::ASCIIToUTF16(test_cases
[i
].other_labels
)));
134 EXPECT_EQ(test_cases
[i
].expected_label
, result
);
138 TEST(ExperimentLabelsTest
, ExtractNonVariationLabels
) {
140 const char* input_label
;
141 const char* expected_output
;
146 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT",
147 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
149 {"CrVar1=123|Tue, 21 Jan 2014 15:30:21 GMT;"
150 "experiment1=456|Tue, 21 Jan 2014 15:30:21 GMT;"
151 "experiment2=789|Tue, 21 Jan 2014 15:30:21 GMT;"
152 "CrVar1=123|Tue, 21 Jan 2014 15:30:21 GMT",
153 "experiment1=456|Tue, 21 Jan 2014 15:30:21 GMT;"
154 "experiment2=789|Tue, 21 Jan 2014 15:30:21 GMT"},
155 // One and one Variation
156 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
157 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT",
158 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
159 // One and one Variation, flipped
160 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;"
161 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT",
162 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
164 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;"
165 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
166 "CrVar2=3310003|Tue, 21 Jan 2014 15:30:21 GMT;"
167 "CrVar3=3310004|Tue, 21 Jan 2014 15:30:21 GMT",
168 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
170 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;"
171 "CrVar2=3310003|Tue, 21 Jan 2014 15:30:21 GMT;"
172 "CrVar3=3310004|Tue, 21 Jan 2014 15:30:21 GMT",
175 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
176 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT",
177 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
178 // Trailing semicolon
179 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
180 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;", // Note the semi here.
181 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
186 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
187 std::string non_variation_labels
= base::UTF16ToUTF8(
188 ExtractNonVariationLabels(
189 base::ASCIIToUTF16(test_cases
[i
].input_label
)));
190 EXPECT_EQ(test_cases
[i
].expected_output
, non_variation_labels
);
194 } // namespace chrome_variations