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/study_filtering.h" | 5 #include "components/variations/study_filtering.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "components/variations/processed_study.h" | 10 #include "components/variations/processed_study.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 } test_cases[] = { | 131 } test_cases[] = { |
132 {"en-US", true, false, false}, | 132 {"en-US", true, false, false}, |
133 {"en-US,en-CA,fr", true, true, true}, | 133 {"en-US,en-CA,fr", true, true, true}, |
134 {"en-US,en-CA,en-GB", true, true, false}, | 134 {"en-US,en-CA,en-GB", true, true, false}, |
135 {"en-GB,en-CA,en-US", true, true, false}, | 135 {"en-GB,en-CA,en-US", true, true, false}, |
136 {"ja,kr,vi", false, false, false}, | 136 {"ja,kr,vi", false, false, false}, |
137 {"fr-CA", false, false, false}, | 137 {"fr-CA", false, false, false}, |
138 {"", true, true, true}, | 138 {"", true, true, true}, |
139 }; | 139 }; |
140 | 140 |
141 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 141 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
142 std::vector<std::string> filter_locales; | 142 std::vector<std::string> filter_locales; |
143 Study_Filter filter; | 143 Study_Filter filter; |
144 base::SplitString(test_cases[i].filter_locales, ',', &filter_locales); | 144 base::SplitString(test_cases[i].filter_locales, ',', &filter_locales); |
145 for (size_t j = 0; j < filter_locales.size(); ++j) | 145 for (size_t j = 0; j < filter_locales.size(); ++j) |
146 filter.add_locale(filter_locales[j]); | 146 filter.add_locale(filter_locales[j]); |
147 EXPECT_EQ(test_cases[i].en_us_result, | 147 EXPECT_EQ(test_cases[i].en_us_result, |
148 internal::CheckStudyLocale(filter, "en-US")); | 148 internal::CheckStudyLocale(filter, "en-US")); |
149 EXPECT_EQ(test_cases[i].en_ca_result, | 149 EXPECT_EQ(test_cases[i].en_ca_result, |
150 internal::CheckStudyLocale(filter, "en-CA")); | 150 internal::CheckStudyLocale(filter, "en-CA")); |
151 EXPECT_EQ(test_cases[i].fr_result, | 151 EXPECT_EQ(test_cases[i].fr_result, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 { now - delta, true }, | 211 { now - delta, true }, |
212 { now, true }, | 212 { now, true }, |
213 { now + delta, false }, | 213 { now + delta, false }, |
214 }; | 214 }; |
215 | 215 |
216 Study_Filter filter; | 216 Study_Filter filter; |
217 | 217 |
218 // Start date not set should result in true. | 218 // Start date not set should result in true. |
219 EXPECT_TRUE(internal::CheckStudyStartDate(filter, now)); | 219 EXPECT_TRUE(internal::CheckStudyStartDate(filter, now)); |
220 | 220 |
221 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) { | 221 for (size_t i = 0; i < arraysize(start_test_cases); ++i) { |
222 filter.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); | 222 filter.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); |
223 const bool result = internal::CheckStudyStartDate(filter, now); | 223 const bool result = internal::CheckStudyStartDate(filter, now); |
224 EXPECT_EQ(start_test_cases[i].expected_result, result) | 224 EXPECT_EQ(start_test_cases[i].expected_result, result) |
225 << "Case " << i << " failed!"; | 225 << "Case " << i << " failed!"; |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 TEST(VariationsStudyFilteringTest, CheckStudyVersion) { | 229 TEST(VariationsStudyFilteringTest, CheckStudyVersion) { |
230 const struct { | 230 const struct { |
231 const char* min_version; | 231 const char* min_version; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 { "2.4.*", "2.3.4", true }, | 266 { "2.4.*", "2.3.4", true }, |
267 { "1.3.*", "2.3.4", false }, | 267 { "1.3.*", "2.3.4", false }, |
268 { "1.*", "2.3.4", false }, | 268 { "1.*", "2.3.4", false }, |
269 }; | 269 }; |
270 | 270 |
271 Study_Filter filter; | 271 Study_Filter filter; |
272 | 272 |
273 // Min/max version not set should result in true. | 273 // Min/max version not set should result in true. |
274 EXPECT_TRUE(internal::CheckStudyVersion(filter, base::Version("1.2.3"))); | 274 EXPECT_TRUE(internal::CheckStudyVersion(filter, base::Version("1.2.3"))); |
275 | 275 |
276 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { | 276 for (size_t i = 0; i < arraysize(min_test_cases); ++i) { |
277 filter.set_min_version(min_test_cases[i].min_version); | 277 filter.set_min_version(min_test_cases[i].min_version); |
278 const bool result = | 278 const bool result = |
279 internal::CheckStudyVersion(filter, Version(min_test_cases[i].version)); | 279 internal::CheckStudyVersion(filter, Version(min_test_cases[i].version)); |
280 EXPECT_EQ(min_test_cases[i].expected_result, result) << | 280 EXPECT_EQ(min_test_cases[i].expected_result, result) << |
281 "Min. version case " << i << " failed!"; | 281 "Min. version case " << i << " failed!"; |
282 } | 282 } |
283 filter.clear_min_version(); | 283 filter.clear_min_version(); |
284 | 284 |
285 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) { | 285 for (size_t i = 0; i < arraysize(max_test_cases); ++i) { |
286 filter.set_max_version(max_test_cases[i].max_version); | 286 filter.set_max_version(max_test_cases[i].max_version); |
287 const bool result = | 287 const bool result = |
288 internal::CheckStudyVersion(filter, Version(max_test_cases[i].version)); | 288 internal::CheckStudyVersion(filter, Version(max_test_cases[i].version)); |
289 EXPECT_EQ(max_test_cases[i].expected_result, result) << | 289 EXPECT_EQ(max_test_cases[i].expected_result, result) << |
290 "Max version case " << i << " failed!"; | 290 "Max version case " << i << " failed!"; |
291 } | 291 } |
292 | 292 |
293 // Check intersection semantics. | 293 // Check intersection semantics. |
294 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { | 294 for (size_t i = 0; i < arraysize(min_test_cases); ++i) { |
295 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(max_test_cases); ++j) { | 295 for (size_t j = 0; j < arraysize(max_test_cases); ++j) { |
296 filter.set_min_version(min_test_cases[i].min_version); | 296 filter.set_min_version(min_test_cases[i].min_version); |
297 filter.set_max_version(max_test_cases[j].max_version); | 297 filter.set_max_version(max_test_cases[j].max_version); |
298 | 298 |
299 if (!min_test_cases[i].expected_result) { | 299 if (!min_test_cases[i].expected_result) { |
300 const bool result = | 300 const bool result = |
301 internal::CheckStudyVersion( | 301 internal::CheckStudyVersion( |
302 filter, Version(min_test_cases[i].version)); | 302 filter, Version(min_test_cases[i].version)); |
303 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; | 303 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; |
304 } | 304 } |
305 | 305 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 {"", "apple,pear,orange", "fancy INTEL pear GRAPE device", false}, | 346 {"", "apple,pear,orange", "fancy INTEL pear GRAPE device", false}, |
347 // Substring, so still invalid. | 347 // Substring, so still invalid. |
348 {"", "apple,pear,orange", "fancy INTEL SNapple device", false}, | 348 {"", "apple,pear,orange", "fancy INTEL SNapple device", false}, |
349 // Empty. | 349 // Empty. |
350 {"", "apple,pear,orange", "", true}, | 350 {"", "apple,pear,orange", "", true}, |
351 | 351 |
352 // Not testing when both are set as it should never occur and should be | 352 // Not testing when both are set as it should never occur and should be |
353 // considered undefined. | 353 // considered undefined. |
354 }; | 354 }; |
355 | 355 |
356 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 356 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
357 Study_Filter filter; | 357 Study_Filter filter; |
358 std::vector<std::string> hardware_class; | 358 std::vector<std::string> hardware_class; |
359 base::SplitString(test_cases[i].hardware_class, ',', &hardware_class); | 359 base::SplitString(test_cases[i].hardware_class, ',', &hardware_class); |
360 for (size_t j = 0; j < hardware_class.size(); ++j) | 360 for (size_t j = 0; j < hardware_class.size(); ++j) |
361 filter.add_hardware_class(hardware_class[j]); | 361 filter.add_hardware_class(hardware_class[j]); |
362 | 362 |
363 std::vector<std::string> exclude_hardware_class; | 363 std::vector<std::string> exclude_hardware_class; |
364 base::SplitString(test_cases[i].exclude_hardware_class, ',', | 364 base::SplitString(test_cases[i].exclude_hardware_class, ',', |
365 &exclude_hardware_class); | 365 &exclude_hardware_class); |
366 for (size_t j = 0; j < exclude_hardware_class.size(); ++j) | 366 for (size_t j = 0; j < exclude_hardware_class.size(); ++j) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 { now - delta, true }, | 417 { now - delta, true }, |
418 { now, true }, | 418 { now, true }, |
419 { now + delta, false }, | 419 { now + delta, false }, |
420 }; | 420 }; |
421 | 421 |
422 Study study; | 422 Study study; |
423 | 423 |
424 // Expiry date not set should result in false. | 424 // Expiry date not set should result in false. |
425 EXPECT_FALSE(internal::IsStudyExpired(study, now)); | 425 EXPECT_FALSE(internal::IsStudyExpired(study, now)); |
426 | 426 |
427 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { | 427 for (size_t i = 0; i < arraysize(expiry_test_cases); ++i) { |
428 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); | 428 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); |
429 const bool result = internal::IsStudyExpired(study, now); | 429 const bool result = internal::IsStudyExpired(study, now); |
430 EXPECT_EQ(expiry_test_cases[i].expected_result, result) | 430 EXPECT_EQ(expiry_test_cases[i].expected_result, result) |
431 << "Case " << i << " failed!"; | 431 << "Case " << i << " failed!"; |
432 } | 432 } |
433 } | 433 } |
434 | 434 |
435 TEST(VariationsStudyFilteringTest, ValidateStudy) { | 435 TEST(VariationsStudyFilteringTest, ValidateStudy) { |
436 Study study; | 436 Study study; |
437 study.set_default_experiment_name("def"); | 437 study.set_default_experiment_name("def"); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 | 470 |
471 default_group->set_name("def"); | 471 default_group->set_name("def"); |
472 EXPECT_TRUE(processed_study.Init(&study, false)); | 472 EXPECT_TRUE(processed_study.Init(&study, false)); |
473 Study_Experiment* repeated_group = study.add_experiment(); | 473 Study_Experiment* repeated_group = study.add_experiment(); |
474 repeated_group->set_name("abc"); | 474 repeated_group->set_name("abc"); |
475 repeated_group->set_probability_weight(1); | 475 repeated_group->set_probability_weight(1); |
476 EXPECT_FALSE(processed_study.Init(&study, false)); | 476 EXPECT_FALSE(processed_study.Init(&study, false)); |
477 } | 477 } |
478 | 478 |
479 } // namespace variations | 479 } // namespace variations |
OLD | NEW |