OLD | NEW |
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/browser/metrics/proto/study.pb.h" | 5 #include "chrome/browser/metrics/proto/study.pb.h" |
6 #include "chrome/browser/metrics/variations_service.h" | 6 #include "chrome/browser/metrics/variations_service.h" |
7 #include "chrome/common/chrome_version_info.h" | 7 #include "chrome/common/chrome_version_info.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 const char* min_version; | 123 const char* min_version; |
124 const char* version; | 124 const char* version; |
125 bool expected_result; | 125 bool expected_result; |
126 } min_test_cases[] = { | 126 } min_test_cases[] = { |
127 { "1.2.2", "1.2.3", true }, | 127 { "1.2.2", "1.2.3", true }, |
128 { "1.2.3", "1.2.3", true }, | 128 { "1.2.3", "1.2.3", true }, |
129 { "1.2.4", "1.2.3", false }, | 129 { "1.2.4", "1.2.3", false }, |
130 { "1.3.2", "1.2.3", false }, | 130 { "1.3.2", "1.2.3", false }, |
131 { "2.1.2", "1.2.3", false }, | 131 { "2.1.2", "1.2.3", false }, |
132 { "0.3.4", "1.2.3", true }, | 132 { "0.3.4", "1.2.3", true }, |
| 133 // Wildcards. |
| 134 { "1.*", "1.2.3", true }, |
| 135 { "1.2.*", "1.2.3", true }, |
| 136 { "1.2.3.*", "1.2.3", true }, |
| 137 { "1.2.4.*", "1.2.3", false }, |
| 138 { "2.*", "1.2.3", false }, |
| 139 { "0.3.*", "1.2.3", true }, |
133 }; | 140 }; |
134 | 141 |
135 const struct { | 142 const struct { |
136 const char* max_version; | 143 const char* max_version; |
137 const char* version; | 144 const char* version; |
138 bool expected_result; | 145 bool expected_result; |
139 } max_test_cases[] = { | 146 } max_test_cases[] = { |
140 { "1.2.2", "1.2.3", false }, | 147 { "1.2.2", "1.2.3", false }, |
141 { "1.2.3", "1.2.3", true }, | 148 { "1.2.3", "1.2.3", true }, |
142 { "1.2.4", "1.2.3", true }, | 149 { "1.2.4", "1.2.3", true }, |
143 { "2.1.1", "1.2.3", true }, | 150 { "2.1.1", "1.2.3", true }, |
144 { "2.1.1", "2.3.4", false }, | 151 { "2.1.1", "2.3.4", false }, |
| 152 // Wildcards |
| 153 { "2.1.*", "2.3.4", false }, |
| 154 { "2.*", "2.3.4", true }, |
| 155 { "2.3.*", "2.3.4", true }, |
| 156 { "2.3.4.*", "2.3.4", true }, |
| 157 { "2.3.4.0.*", "2.3.4", true }, |
| 158 { "2.4.*", "2.3.4", true }, |
| 159 { "1.3.*", "2.3.4", false }, |
| 160 { "1.*", "2.3.4", false }, |
145 }; | 161 }; |
146 | 162 |
147 chrome_variations::Study_Filter filter; | 163 chrome_variations::Study_Filter filter; |
148 | 164 |
149 // Min/max version not set should result in true. | 165 // Min/max version not set should result in true. |
150 EXPECT_TRUE(VariationsService::CheckStudyVersion(filter, "1.2.3")); | 166 EXPECT_TRUE(VariationsService::CheckStudyVersion(filter, "1.2.3")); |
151 | 167 |
152 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { | 168 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { |
153 filter.set_min_version(min_test_cases[i].min_version); | 169 filter.set_min_version(min_test_cases[i].min_version); |
154 const bool result = | 170 const bool result = |
155 VariationsService::CheckStudyVersion(filter, min_test_cases[i].version); | 171 VariationsService::CheckStudyVersion(filter, min_test_cases[i].version); |
156 EXPECT_EQ(min_test_cases[i].expected_result, result) << | 172 EXPECT_EQ(min_test_cases[i].expected_result, result) << |
157 "Case " << i << " failed!"; | 173 "Min. version case " << i << " failed!"; |
158 } | 174 } |
159 filter.clear_min_version(); | 175 filter.clear_min_version(); |
160 | 176 |
161 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) { | 177 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) { |
162 filter.set_max_version(max_test_cases[i].max_version); | 178 filter.set_max_version(max_test_cases[i].max_version); |
163 const bool result = | 179 const bool result = |
164 VariationsService::CheckStudyVersion(filter, max_test_cases[i].version); | 180 VariationsService::CheckStudyVersion(filter, max_test_cases[i].version); |
165 EXPECT_EQ(max_test_cases[i].expected_result, result) << | 181 EXPECT_EQ(max_test_cases[i].expected_result, result) << |
166 "Case " << i << " failed!"; | 182 "Max version case " << i << " failed!"; |
167 } | 183 } |
168 | 184 |
169 // Check intersection semantics. | 185 // Check intersection semantics. |
170 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { | 186 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { |
171 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(max_test_cases); ++j) { | 187 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(max_test_cases); ++j) { |
172 filter.set_min_version(min_test_cases[i].min_version); | 188 filter.set_min_version(min_test_cases[i].min_version); |
173 filter.set_max_version(max_test_cases[j].max_version); | 189 filter.set_max_version(max_test_cases[j].max_version); |
174 | 190 |
175 if (!min_test_cases[i].expected_result) { | 191 if (!min_test_cases[i].expected_result) { |
176 const bool result = | 192 const bool result = |
177 VariationsService::CheckStudyVersion(filter, | 193 VariationsService::CheckStudyVersion(filter, |
178 min_test_cases[i].version); | 194 min_test_cases[i].version); |
179 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; | 195 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; |
180 } | 196 } |
181 | 197 |
182 if (!max_test_cases[j].expected_result) { | 198 if (!max_test_cases[j].expected_result) { |
183 const bool result = | 199 const bool result = |
184 VariationsService::CheckStudyVersion(filter, | 200 VariationsService::CheckStudyVersion(filter, |
185 max_test_cases[j].version); | 201 max_test_cases[j].version); |
186 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; | 202 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; |
187 } | 203 } |
188 } | 204 } |
189 } | 205 } |
190 } | 206 } |
191 | 207 |
192 // The current client logic does not handle version number strings containing | |
193 // wildcards. Check that any such values received from the server result in the | |
194 // study being disqualified. | |
195 TEST(VariationsServiceTest, CheckStudyVersionWildcards) { | |
196 chrome_variations::Study_Filter filter; | |
197 | |
198 filter.set_min_version("1.0.*"); | |
199 EXPECT_FALSE(VariationsService::CheckStudyVersion(filter, "1.2.3")); | |
200 | |
201 filter.clear_min_version(); | |
202 filter.set_max_version("2.0.*"); | |
203 EXPECT_FALSE(VariationsService::CheckStudyVersion(filter, "1.2.3")); | |
204 } | |
205 | |
206 TEST(VariationsServiceTest, CheckStudyStartDate) { | 208 TEST(VariationsServiceTest, CheckStudyStartDate) { |
207 const base::Time now = base::Time::Now(); | 209 const base::Time now = base::Time::Now(); |
208 const base::TimeDelta delta = base::TimeDelta::FromHours(1); | 210 const base::TimeDelta delta = base::TimeDelta::FromHours(1); |
209 const struct { | 211 const struct { |
210 const base::Time start_date; | 212 const base::Time start_date; |
211 bool expected_result; | 213 bool expected_result; |
212 } start_test_cases[] = { | 214 } start_test_cases[] = { |
213 { now - delta, true }, | 215 { now - delta, true }, |
214 { now, true }, | 216 { now, true }, |
215 { now + delta, false }, | 217 { now + delta, false }, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 chrome_variations::Study_Experiment* default_group = study.add_experiment(); | 266 chrome_variations::Study_Experiment* default_group = study.add_experiment(); |
265 default_group->set_name("def"); | 267 default_group->set_name("def"); |
266 default_group->set_probability_weight(200); | 268 default_group->set_probability_weight(200); |
267 | 269 |
268 base::FieldTrial::Probability total_probability = 0; | 270 base::FieldTrial::Probability total_probability = 0; |
269 bool valid = VariationsService::ValidateStudyAndComputeTotalProbability( | 271 bool valid = VariationsService::ValidateStudyAndComputeTotalProbability( |
270 study, &total_probability); | 272 study, &total_probability); |
271 EXPECT_TRUE(valid); | 273 EXPECT_TRUE(valid); |
272 EXPECT_EQ(300, total_probability); | 274 EXPECT_EQ(300, total_probability); |
273 | 275 |
| 276 // Min version checks. |
| 277 study.mutable_filter()->set_min_version("1.2.3.*"); |
| 278 valid = VariationsService::ValidateStudyAndComputeTotalProbability( |
| 279 study, &total_probability); |
| 280 EXPECT_TRUE(valid); |
| 281 study.mutable_filter()->set_min_version("1.*.3"); |
| 282 valid = VariationsService::ValidateStudyAndComputeTotalProbability( |
| 283 study, &total_probability); |
| 284 EXPECT_FALSE(valid); |
| 285 study.mutable_filter()->set_min_version("1.2.3"); |
| 286 valid = VariationsService::ValidateStudyAndComputeTotalProbability( |
| 287 study, &total_probability); |
| 288 EXPECT_TRUE(valid); |
| 289 |
| 290 // Max version checks. |
| 291 study.mutable_filter()->set_max_version("2.3.4.*"); |
| 292 valid = VariationsService::ValidateStudyAndComputeTotalProbability( |
| 293 study, &total_probability); |
| 294 EXPECT_TRUE(valid); |
| 295 study.mutable_filter()->set_max_version("*.3"); |
| 296 valid = VariationsService::ValidateStudyAndComputeTotalProbability( |
| 297 study, &total_probability); |
| 298 EXPECT_FALSE(valid); |
| 299 study.mutable_filter()->set_max_version("2.3.4"); |
| 300 valid = VariationsService::ValidateStudyAndComputeTotalProbability( |
| 301 study, &total_probability); |
| 302 EXPECT_TRUE(valid); |
| 303 |
274 study.clear_default_experiment_name(); | 304 study.clear_default_experiment_name(); |
275 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 305 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
276 &total_probability); | 306 &total_probability); |
277 EXPECT_FALSE(valid); | 307 EXPECT_FALSE(valid); |
278 | 308 |
279 study.set_default_experiment_name("xyz"); | 309 study.set_default_experiment_name("xyz"); |
280 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 310 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
281 &total_probability); | 311 &total_probability); |
282 EXPECT_FALSE(valid); | 312 EXPECT_FALSE(valid); |
283 | 313 |
284 study.set_default_experiment_name("def"); | 314 study.set_default_experiment_name("def"); |
285 default_group->clear_name(); | 315 default_group->clear_name(); |
286 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 316 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
287 &total_probability); | 317 &total_probability); |
288 EXPECT_FALSE(valid); | 318 EXPECT_FALSE(valid); |
289 | 319 |
290 default_group->set_name("def"); | 320 default_group->set_name("def"); |
291 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 321 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
292 &total_probability); | 322 &total_probability); |
293 ASSERT_TRUE(valid); | 323 ASSERT_TRUE(valid); |
294 chrome_variations::Study_Experiment* repeated_group = study.add_experiment(); | 324 chrome_variations::Study_Experiment* repeated_group = study.add_experiment(); |
295 repeated_group->set_name("abc"); | 325 repeated_group->set_name("abc"); |
296 repeated_group->set_probability_weight(1); | 326 repeated_group->set_probability_weight(1); |
297 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 327 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
298 &total_probability); | 328 &total_probability); |
299 EXPECT_FALSE(valid); | 329 EXPECT_FALSE(valid); |
300 } | 330 } |
OLD | NEW |