Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: chrome/common/metrics/variations/variations_util_unittest.cc

Issue 23579003: GCAPI should append to the existing experiment_labels instead of clobbering them. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review++ Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/common/metrics/variations/variations_util.h" 5 #include "chrome/common/metrics/variations/variations_util.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector>
9 10
10 #include "base/metrics/field_trial.h" 11 #include "base/metrics/field_trial.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "components/variations/metrics_util.h" 12 #include "components/variations/metrics_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
16 14
17 namespace chrome_variations { 15 namespace chrome_variations {
18 16
19 namespace {
20
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 // Tests whether a field trial is active (i.e. group() has been called on it).
27 bool IsFieldTrialActive(const std::string& trial_name) {
28 base::FieldTrial::ActiveGroups active_groups;
29 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
30 for (size_t i = 0; i < active_groups.size(); ++i) {
31 if (active_groups[i].trial_name == trial_name)
32 return true;
33 }
34 return false;
35 }
36
37 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date.
38 scoped_refptr<base::FieldTrial> CreateFieldTrial(
39 const std::string& trial_name,
40 int total_probability,
41 const std::string& default_group_name,
42 int* default_group_number) {
43 return base::FieldTrialList::FactoryGetFieldTrial(
44 trial_name, total_probability, default_group_name,
45 base::FieldTrialList::kNoExpirationYear, 1, 1,
46 base::FieldTrial::SESSION_RANDOMIZED, default_group_number);
47 }
48
49 } // namespace
50
51 class VariationsUtilTest : public ::testing::Test { 17 class VariationsUtilTest : public ::testing::Test {
52 public: 18 public:
53 VariationsUtilTest() : field_trial_list_(NULL) { 19 VariationsUtilTest() : field_trial_list_(NULL) {
54 } 20 }
55 21
56 virtual ~VariationsUtilTest() { 22 virtual ~VariationsUtilTest() {
57 // Ensure that the maps are cleared between tests, since they are stored as 23 // Ensure that the maps are cleared between tests, since they are stored as
58 // process singletons. 24 // process singletons.
59 testing::ClearAllVariationIDs(); 25 testing::ClearAllVariationIDs();
60 } 26 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 EXPECT_EQ(2U, active_group_ids.size()); 63 EXPECT_EQ(2U, active_group_ids.size());
98 for (size_t i = 0; i < active_group_ids.size(); ++i) { 64 for (size_t i = 0; i < active_group_ids.size(); ++i) {
99 ActiveGroupIdSet::iterator expected_group = 65 ActiveGroupIdSet::iterator expected_group =
100 expected_groups.find(active_group_ids[i]); 66 expected_groups.find(active_group_ids[i]);
101 EXPECT_FALSE(expected_group == expected_groups.end()); 67 EXPECT_FALSE(expected_group == expected_groups.end());
102 expected_groups.erase(expected_group); 68 expected_groups.erase(expected_group);
103 } 69 }
104 EXPECT_EQ(0U, expected_groups.size()); 70 EXPECT_EQ(0U, expected_groups.size());
105 } 71 }
106 72
107 TEST_F(VariationsUtilTest, BuildGoogleUpdateExperimentLabel) {
108 struct {
109 const char* active_group_pairs;
110 const char* expected_ids;
111 } test_cases[] = {
112 // Empty group.
113 {"", ""},
114 // Group of 1.
115 {"FieldTrialA#Default", "3300200"},
116 // Group of 1, doesn't have an associated ID.
117 {"FieldTrialA#DoesNotExist", ""},
118 // Group of 3.
119 {"FieldTrialA#Default#FieldTrialB#Group1#FieldTrialC#Default",
120 "3300200#3300201#3300202"},
121 // Group of 3, one doesn't have an associated ID.
122 {"FieldTrialA#Default#FieldTrialB#DoesNotExist#FieldTrialC#Default",
123 "3300200#3300202"},
124 // Group of 3, all three don't have an associated ID.
125 {"FieldTrialX#Default#FieldTrialB#DoesNotExist#FieldTrialC#Default",
126 "3300202"},
127 };
128
129 // Register a few VariationIDs.
130 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialA", "Default",
131 TEST_VALUE_A);
132 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialB", "Group1",
133 TEST_VALUE_B);
134 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialC", "Default",
135 TEST_VALUE_C);
136 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE, "FieldTrialD", "Default",
137 TEST_VALUE_D); // Not actually used.
138
139 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
140 // Parse the input groups.
141 base::FieldTrial::ActiveGroups groups;
142 std::vector<std::string> group_data;
143 base::SplitString(test_cases[i].active_group_pairs, '#', &group_data);
144 ASSERT_EQ(0U, group_data.size() % 2);
145 for (size_t j = 0; j < group_data.size(); j += 2) {
146 base::FieldTrial::ActiveGroup group;
147 group.trial_name = group_data[j];
148 group.group_name = group_data[j + 1];
149 groups.push_back(group);
150 }
151
152 // Parse the expected output.
153 std::vector<std::string> expected_ids_list;
154 base::SplitString(test_cases[i].expected_ids, '#', &expected_ids_list);
155
156 std::string experiment_labels_string = UTF16ToUTF8(
157 BuildGoogleUpdateExperimentLabel(groups));
158
159 // Split the VariationIDs from the labels for verification below.
160 std::vector<std::string> split_labels;
161 std::set<std::string> parsed_ids;
162 base::SplitString(experiment_labels_string, ';', &split_labels);
163 for (std::vector<std::string>::const_iterator it = split_labels.begin();
164 it != split_labels.end(); ++it) {
165 // The ID is precisely between the '=' and '|' characters in each label.
166 size_t index_of_equals = it->find('=');
167 size_t index_of_pipe = it->find('|');
168 ASSERT_NE(std::string::npos, index_of_equals);
169 ASSERT_NE(std::string::npos, index_of_pipe);
170 ASSERT_GT(index_of_pipe, index_of_equals);
171 parsed_ids.insert(it->substr(index_of_equals + 1,
172 index_of_pipe - index_of_equals - 1));
173 }
174
175 // Verify that the resulting string contains each of the expected labels,
176 // and nothing more. Note that the date is stripped out and ignored.
177 for (std::vector<std::string>::const_iterator it =
178 expected_ids_list.begin(); it != expected_ids_list.end(); ++it) {
179 std::set<std::string>::iterator it2 = parsed_ids.find(*it);
180 EXPECT_TRUE(parsed_ids.end() != it2);
181 parsed_ids.erase(it2);
182 }
183 EXPECT_TRUE(parsed_ids.empty());
184 } // for
185 }
186
187 TEST_F(VariationsUtilTest, CombineExperimentLabels) {
188 struct {
189 const char* variations_labels;
190 const char* other_labels;
191 const char* expected_label;
192 } test_cases[] = {
193 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT",
194 "C=D|Tue, 21 Jan 2014 15:30:21 GMT",
195 "C=D|Tue, 21 Jan 2014 15:30:21 GMT;A=B|Tue, 21 Jan 2014 15:30:21 GMT"},
196 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT",
197 "",
198 "A=B|Tue, 21 Jan 2014 15:30:21 GMT"},
199 {"",
200 "A=B|Tue, 21 Jan 2014 15:30:21 GMT",
201 "A=B|Tue, 21 Jan 2014 15:30:21 GMT"},
202 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT;C=D|Tue, 21 Jan 2014 15:30:21 GMT",
203 "P=Q|Tue, 21 Jan 2014 15:30:21 GMT;X=Y|Tue, 21 Jan 2014 15:30:21 GMT",
204 "P=Q|Tue, 21 Jan 2014 15:30:21 GMT;X=Y|Tue, 21 Jan 2014 15:30:21 GMT;"
205 "A=B|Tue, 21 Jan 2014 15:30:21 GMT;C=D|Tue, 21 Jan 2014 15:30:21 GMT"},
206 {"",
207 "",
208 ""},
209 };
210
211 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
212 std::string result = UTF16ToUTF8(CombineExperimentLabels(
213 ASCIIToUTF16(test_cases[i].variations_labels),
214 ASCIIToUTF16(test_cases[i].other_labels)));
215 EXPECT_EQ(test_cases[i].expected_label, result);
216 }
217 }
218
219 TEST_F(VariationsUtilTest, ExtractNonVariationLabels) {
220 struct {
221 const char* input_label;
222 const char* expected_output;
223 } test_cases[] = {
224 // Empty
225 {"", ""},
226 // One
227 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT",
228 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
229 // Three
230 {"CrVar1=123|Tue, 21 Jan 2014 15:30:21 GMT;"
231 "experiment1=456|Tue, 21 Jan 2014 15:30:21 GMT;"
232 "experiment2=789|Tue, 21 Jan 2014 15:30:21 GMT;"
233 "CrVar1=123|Tue, 21 Jan 2014 15:30:21 GMT",
234 "experiment1=456|Tue, 21 Jan 2014 15:30:21 GMT;"
235 "experiment2=789|Tue, 21 Jan 2014 15:30:21 GMT"},
236 // One and one Variation
237 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
238 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT",
239 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
240 // One and one Variation, flipped
241 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;"
242 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT",
243 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
244 // Sandwiched
245 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;"
246 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
247 "CrVar2=3310003|Tue, 21 Jan 2014 15:30:21 GMT;"
248 "CrVar3=3310004|Tue, 21 Jan 2014 15:30:21 GMT",
249 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
250 // Only Variations
251 {"CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;"
252 "CrVar2=3310003|Tue, 21 Jan 2014 15:30:21 GMT;"
253 "CrVar3=3310004|Tue, 21 Jan 2014 15:30:21 GMT",
254 ""},
255 // Empty values
256 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
257 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT",
258 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
259 // Trailing semicolon
260 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT;"
261 "CrVar1=3310002|Tue, 21 Jan 2014 15:30:21 GMT;", // Note the semi here.
262 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"},
263 // Semis
264 {";;;;", ""},
265 };
266
267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
268 std::string non_variation_labels = UTF16ToUTF8(
269 ExtractNonVariationLabels(ASCIIToUTF16(test_cases[i].input_label)));
270 EXPECT_EQ(test_cases[i].expected_output, non_variation_labels);
271 }
272 }
273
274 } // namespace chrome_variations 73 } // namespace chrome_variations
OLDNEW
« no previous file with comments | « chrome/common/metrics/variations/variations_util.cc ('k') | chrome/installer/gcapi/gcapi_omaha_experiment.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698