OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/experiment_labels_win.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
9 | 10 |
| 11 #include "base/basictypes.h" |
10 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
11 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 15 #include "components/variations/variations_associated_data.h" |
14 #include "components/variations/metrics_util.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace chrome_variations { | 18 namespace chrome_variations { |
18 | 19 |
19 namespace { | 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; |
20 | 25 |
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 { | |
52 public: | |
53 VariationsUtilTest() : field_trial_list_(NULL) { | |
54 } | |
55 | |
56 virtual ~VariationsUtilTest() { | |
57 // Ensure that the maps are cleared between tests, since they are stored as | |
58 // process singletons. | |
59 testing::ClearAllVariationIDs(); | |
60 } | |
61 | |
62 private: | |
63 base::FieldTrialList field_trial_list_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(VariationsUtilTest); | |
66 }; | |
67 | |
68 TEST_F(VariationsUtilTest, GetFieldTrialActiveGroups) { | |
69 typedef std::set<ActiveGroupId, ActiveGroupIdCompare> ActiveGroupIdSet; | |
70 std::string trial_one("trial one"); | |
71 std::string group_one("group one"); | |
72 std::string trial_two("trial two"); | |
73 std::string group_two("group two"); | |
74 | |
75 base::FieldTrial::ActiveGroups active_groups; | |
76 base::FieldTrial::ActiveGroup active_group; | |
77 active_group.trial_name = trial_one; | |
78 active_group.group_name = group_one; | |
79 active_groups.push_back(active_group); | |
80 | |
81 active_group.trial_name = trial_two; | |
82 active_group.group_name = group_two; | |
83 active_groups.push_back(active_group); | |
84 | |
85 // Create our expected groups of IDs. | |
86 ActiveGroupIdSet expected_groups; | |
87 ActiveGroupId name_group_id; | |
88 name_group_id.name = metrics::HashName(trial_one); | |
89 name_group_id.group = metrics::HashName(group_one); | |
90 expected_groups.insert(name_group_id); | |
91 name_group_id.name = metrics::HashName(trial_two); | |
92 name_group_id.group = metrics::HashName(group_two); | |
93 expected_groups.insert(name_group_id); | |
94 | |
95 std::vector<ActiveGroupId> active_group_ids; | |
96 testing::TestGetFieldTrialActiveGroupIds(active_groups, &active_group_ids); | |
97 EXPECT_EQ(2U, active_group_ids.size()); | |
98 for (size_t i = 0; i < active_group_ids.size(); ++i) { | |
99 ActiveGroupIdSet::iterator expected_group = | |
100 expected_groups.find(active_group_ids[i]); | |
101 EXPECT_FALSE(expected_group == expected_groups.end()); | |
102 expected_groups.erase(expected_group); | |
103 } | |
104 EXPECT_EQ(0U, expected_groups.size()); | |
105 } | |
106 | |
107 TEST_F(VariationsUtilTest, BuildGoogleUpdateExperimentLabel) { | |
108 struct { | 26 struct { |
109 const char* active_group_pairs; | 27 const char* active_group_pairs; |
110 const char* expected_ids; | 28 const char* expected_ids; |
111 } test_cases[] = { | 29 } test_cases[] = { |
112 // Empty group. | 30 // Empty group. |
113 {"", ""}, | 31 {"", ""}, |
114 // Group of 1. | 32 // Group of 1. |
115 {"FieldTrialA#Default", "3300200"}, | 33 {"FieldTrialA#Default", "3300200"}, |
116 // Group of 1, doesn't have an associated ID. | 34 // Group of 1, doesn't have an associated ID. |
117 {"FieldTrialA#DoesNotExist", ""}, | 35 {"FieldTrialA#DoesNotExist", ""}, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 for (std::vector<std::string>::const_iterator it = | 95 for (std::vector<std::string>::const_iterator it = |
178 expected_ids_list.begin(); it != expected_ids_list.end(); ++it) { | 96 expected_ids_list.begin(); it != expected_ids_list.end(); ++it) { |
179 std::set<std::string>::iterator it2 = parsed_ids.find(*it); | 97 std::set<std::string>::iterator it2 = parsed_ids.find(*it); |
180 EXPECT_TRUE(parsed_ids.end() != it2); | 98 EXPECT_TRUE(parsed_ids.end() != it2); |
181 parsed_ids.erase(it2); | 99 parsed_ids.erase(it2); |
182 } | 100 } |
183 EXPECT_TRUE(parsed_ids.empty()); | 101 EXPECT_TRUE(parsed_ids.empty()); |
184 } // for | 102 } // for |
185 } | 103 } |
186 | 104 |
187 TEST_F(VariationsUtilTest, CombineExperimentLabels) { | 105 TEST(ExperimentLabelsTest, CombineExperimentLabels) { |
188 struct { | 106 struct { |
189 const char* variations_labels; | 107 const char* variations_labels; |
190 const char* other_labels; | 108 const char* other_labels; |
191 const char* expected_label; | 109 const char* expected_label; |
192 } test_cases[] = { | 110 } test_cases[] = { |
193 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT", | 111 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT", |
194 "C=D|Tue, 21 Jan 2014 15:30:21 GMT", | 112 "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"}, | 113 "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", | 114 {"A=B|Tue, 21 Jan 2014 15:30:21 GMT", |
197 "", | 115 "", |
(...skipping 11 matching lines...) Expand all Loading... |
209 }; | 127 }; |
210 | 128 |
211 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 129 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
212 std::string result = UTF16ToUTF8(CombineExperimentLabels( | 130 std::string result = UTF16ToUTF8(CombineExperimentLabels( |
213 ASCIIToUTF16(test_cases[i].variations_labels), | 131 ASCIIToUTF16(test_cases[i].variations_labels), |
214 ASCIIToUTF16(test_cases[i].other_labels))); | 132 ASCIIToUTF16(test_cases[i].other_labels))); |
215 EXPECT_EQ(test_cases[i].expected_label, result); | 133 EXPECT_EQ(test_cases[i].expected_label, result); |
216 } | 134 } |
217 } | 135 } |
218 | 136 |
219 TEST_F(VariationsUtilTest, ExtractNonVariationLabels) { | 137 TEST(ExperimentLabelsTest, ExtractNonVariationLabels) { |
220 struct { | 138 struct { |
221 const char* input_label; | 139 const char* input_label; |
222 const char* expected_output; | 140 const char* expected_output; |
223 } test_cases[] = { | 141 } test_cases[] = { |
224 // Empty | 142 // Empty |
225 {"", ""}, | 143 {"", ""}, |
226 // One | 144 // One |
227 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT", | 145 {"gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT", |
228 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, | 146 "gcapi_brand=123|Tue, 21 Jan 2014 15:30:21 GMT"}, |
229 // Three | 147 // Three |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 }; | 183 }; |
266 | 184 |
267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 185 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
268 std::string non_variation_labels = UTF16ToUTF8( | 186 std::string non_variation_labels = UTF16ToUTF8( |
269 ExtractNonVariationLabels(ASCIIToUTF16(test_cases[i].input_label))); | 187 ExtractNonVariationLabels(ASCIIToUTF16(test_cases[i].input_label))); |
270 EXPECT_EQ(test_cases[i].expected_output, non_variation_labels); | 188 EXPECT_EQ(test_cases[i].expected_output, non_variation_labels); |
271 } | 189 } |
272 } | 190 } |
273 | 191 |
274 } // namespace chrome_variations | 192 } // namespace chrome_variations |
OLD | NEW |