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 "components/variations/study_filtering.h"
9 #include "base/strings/string_split.h"
10 #include "components/variations/processed_study.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace variations
{
17 // Converts |time| to Study proto format.
18 int64
TimeToProtoTime(const base::Time
& time
) {
19 return (time
- base::Time::UnixEpoch()).InSeconds();
22 // Adds an experiment to |study| with the specified |name| and |probability|.
23 Study_Experiment
* AddExperiment(const std::string
& name
, int probability
,
25 Study_Experiment
* experiment
= study
->add_experiment();
26 experiment
->set_name(name
);
27 experiment
->set_probability_weight(probability
);
33 TEST(VariationsStudyFilteringTest
, CheckStudyChannel
) {
34 const Study_Channel channels
[] = {
40 bool channel_added
[arraysize(channels
)] = { 0 };
44 // Check in the forwarded order. The loop cond is <= arraysize(channels)
45 // instead of < so that the result of adding the last channel gets checked.
46 for (size_t i
= 0; i
<= arraysize(channels
); ++i
) {
47 for (size_t j
= 0; j
< arraysize(channels
); ++j
) {
48 const bool expected
= channel_added
[j
] || filter
.channel_size() == 0;
49 const bool result
= internal::CheckStudyChannel(filter
, channels
[j
]);
50 EXPECT_EQ(expected
, result
) << "Case " << i
<< "," << j
<< " failed!";
53 if (i
< arraysize(channels
)) {
54 filter
.add_channel(channels
[i
]);
55 channel_added
[i
] = true;
59 // Do the same check in the reverse order.
60 filter
.clear_channel();
61 memset(&channel_added
, 0, sizeof(channel_added
));
62 for (size_t i
= 0; i
<= arraysize(channels
); ++i
) {
63 for (size_t j
= 0; j
< arraysize(channels
); ++j
) {
64 const bool expected
= channel_added
[j
] || filter
.channel_size() == 0;
65 const bool result
= internal::CheckStudyChannel(filter
, channels
[j
]);
66 EXPECT_EQ(expected
, result
) << "Case " << i
<< "," << j
<< " failed!";
69 if (i
< arraysize(channels
)) {
70 const int index
= arraysize(channels
) - i
- 1;
71 filter
.add_channel(channels
[index
]);
72 channel_added
[index
] = true;
77 TEST(VariationsStudyFilteringTest
, CheckStudyFormFactor
) {
78 const Study_FormFactor form_factors
[] = {
79 Study_FormFactor_DESKTOP
,
80 Study_FormFactor_PHONE
,
81 Study_FormFactor_TABLET
,
84 ASSERT_EQ(Study_FormFactor_FormFactor_ARRAYSIZE
,
85 static_cast<int>(arraysize(form_factors
)));
87 bool form_factor_added
[arraysize(form_factors
)] = { 0 };
90 for (size_t i
= 0; i
<= arraysize(form_factors
); ++i
) {
91 for (size_t j
= 0; j
< arraysize(form_factors
); ++j
) {
92 const bool expected
= form_factor_added
[j
] ||
93 filter
.form_factor_size() == 0;
94 const bool result
= internal::CheckStudyFormFactor(filter
,
96 EXPECT_EQ(expected
, result
) << "Case " << i
<< "," << j
<< " failed!";
99 if (i
< arraysize(form_factors
)) {
100 filter
.add_form_factor(form_factors
[i
]);
101 form_factor_added
[i
] = true;
105 // Do the same check in the reverse order.
106 filter
.clear_form_factor();
107 memset(&form_factor_added
, 0, sizeof(form_factor_added
));
108 for (size_t i
= 0; i
<= arraysize(form_factors
); ++i
) {
109 for (size_t j
= 0; j
< arraysize(form_factors
); ++j
) {
110 const bool expected
= form_factor_added
[j
] ||
111 filter
.form_factor_size() == 0;
112 const bool result
= internal::CheckStudyFormFactor(filter
,
114 EXPECT_EQ(expected
, result
) << "Case " << i
<< "," << j
<< " failed!";
117 if (i
< arraysize(form_factors
)) {
118 const int index
= arraysize(form_factors
) - i
- 1;;
119 filter
.add_form_factor(form_factors
[index
]);
120 form_factor_added
[index
] = true;
125 TEST(VariationsStudyFilteringTest
, CheckStudyLocale
) {
127 const char* filter_locales
;
132 {"en-US", true, false, false},
133 {"en-US,en-CA,fr", true, true, true},
134 {"en-US,en-CA,en-GB", true, true, false},
135 {"en-GB,en-CA,en-US", true, true, false},
136 {"ja,kr,vi", false, false, false},
137 {"fr-CA", false, false, false},
138 {"", true, true, true},
141 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
142 std::vector
<std::string
> filter_locales
;
144 base::SplitString(test_cases
[i
].filter_locales
, ',', &filter_locales
);
145 for (size_t j
= 0; j
< filter_locales
.size(); ++j
)
146 filter
.add_locale(filter_locales
[j
]);
147 EXPECT_EQ(test_cases
[i
].en_us_result
,
148 internal::CheckStudyLocale(filter
, "en-US"));
149 EXPECT_EQ(test_cases
[i
].en_ca_result
,
150 internal::CheckStudyLocale(filter
, "en-CA"));
151 EXPECT_EQ(test_cases
[i
].fr_result
,
152 internal::CheckStudyLocale(filter
, "fr"));
156 TEST(VariationsStudyFilteringTest
, CheckStudyPlatform
) {
157 const Study_Platform platforms
[] = {
158 Study_Platform_PLATFORM_WINDOWS
,
159 Study_Platform_PLATFORM_MAC
,
160 Study_Platform_PLATFORM_LINUX
,
161 Study_Platform_PLATFORM_CHROMEOS
,
162 Study_Platform_PLATFORM_ANDROID
,
163 Study_Platform_PLATFORM_IOS
,
165 ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE
,
166 static_cast<int>(arraysize(platforms
)));
167 bool platform_added
[arraysize(platforms
)] = { 0 };
171 // Check in the forwarded order. The loop cond is <= arraysize(platforms)
172 // instead of < so that the result of adding the last channel gets checked.
173 for (size_t i
= 0; i
<= arraysize(platforms
); ++i
) {
174 for (size_t j
= 0; j
< arraysize(platforms
); ++j
) {
175 const bool expected
= platform_added
[j
] || filter
.platform_size() == 0;
176 const bool result
= internal::CheckStudyPlatform(filter
, platforms
[j
]);
177 EXPECT_EQ(expected
, result
) << "Case " << i
<< "," << j
<< " failed!";
180 if (i
< arraysize(platforms
)) {
181 filter
.add_platform(platforms
[i
]);
182 platform_added
[i
] = true;
186 // Do the same check in the reverse order.
187 filter
.clear_platform();
188 memset(&platform_added
, 0, sizeof(platform_added
));
189 for (size_t i
= 0; i
<= arraysize(platforms
); ++i
) {
190 for (size_t j
= 0; j
< arraysize(platforms
); ++j
) {
191 const bool expected
= platform_added
[j
] || filter
.platform_size() == 0;
192 const bool result
= internal::CheckStudyPlatform(filter
, platforms
[j
]);
193 EXPECT_EQ(expected
, result
) << "Case " << i
<< "," << j
<< " failed!";
196 if (i
< arraysize(platforms
)) {
197 const int index
= arraysize(platforms
) - i
- 1;
198 filter
.add_platform(platforms
[index
]);
199 platform_added
[index
] = true;
204 TEST(VariationsStudyFilteringTest
, CheckStudyStartDate
) {
205 const base::Time now
= base::Time::Now();
206 const base::TimeDelta delta
= base::TimeDelta::FromHours(1);
208 const base::Time start_date
;
209 bool expected_result
;
210 } start_test_cases
[] = {
211 { now
- delta
, true },
213 { now
+ delta
, false },
218 // Start date not set should result in true.
219 EXPECT_TRUE(internal::CheckStudyStartDate(filter
, now
));
221 for (size_t i
= 0; i
< arraysize(start_test_cases
); ++i
) {
222 filter
.set_start_date(TimeToProtoTime(start_test_cases
[i
].start_date
));
223 const bool result
= internal::CheckStudyStartDate(filter
, now
);
224 EXPECT_EQ(start_test_cases
[i
].expected_result
, result
)
225 << "Case " << i
<< " failed!";
229 TEST(VariationsStudyFilteringTest
, CheckStudyVersion
) {
231 const char* min_version
;
233 bool expected_result
;
234 } min_test_cases
[] = {
235 { "1.2.2", "1.2.3", true },
236 { "1.2.3", "1.2.3", true },
237 { "1.2.4", "1.2.3", false },
238 { "1.3.2", "1.2.3", false },
239 { "2.1.2", "1.2.3", false },
240 { "0.3.4", "1.2.3", true },
242 { "1.*", "1.2.3", true },
243 { "1.2.*", "1.2.3", true },
244 { "1.2.3.*", "1.2.3", true },
245 { "1.2.4.*", "1.2.3", false },
246 { "2.*", "1.2.3", false },
247 { "0.3.*", "1.2.3", true },
251 const char* max_version
;
253 bool expected_result
;
254 } max_test_cases
[] = {
255 { "1.2.2", "1.2.3", false },
256 { "1.2.3", "1.2.3", true },
257 { "1.2.4", "1.2.3", true },
258 { "2.1.1", "1.2.3", true },
259 { "2.1.1", "2.3.4", false },
261 { "2.1.*", "2.3.4", false },
262 { "2.*", "2.3.4", true },
263 { "2.3.*", "2.3.4", true },
264 { "2.3.4.*", "2.3.4", true },
265 { "2.3.4.0.*", "2.3.4", true },
266 { "2.4.*", "2.3.4", true },
267 { "1.3.*", "2.3.4", false },
268 { "1.*", "2.3.4", false },
273 // Min/max version not set should result in true.
274 EXPECT_TRUE(internal::CheckStudyVersion(filter
, base::Version("1.2.3")));
276 for (size_t i
= 0; i
< arraysize(min_test_cases
); ++i
) {
277 filter
.set_min_version(min_test_cases
[i
].min_version
);
279 internal::CheckStudyVersion(filter
, Version(min_test_cases
[i
].version
));
280 EXPECT_EQ(min_test_cases
[i
].expected_result
, result
) <<
281 "Min. version case " << i
<< " failed!";
283 filter
.clear_min_version();
285 for (size_t i
= 0; i
< arraysize(max_test_cases
); ++i
) {
286 filter
.set_max_version(max_test_cases
[i
].max_version
);
288 internal::CheckStudyVersion(filter
, Version(max_test_cases
[i
].version
));
289 EXPECT_EQ(max_test_cases
[i
].expected_result
, result
) <<
290 "Max version case " << i
<< " failed!";
293 // Check intersection semantics.
294 for (size_t i
= 0; i
< arraysize(min_test_cases
); ++i
) {
295 for (size_t j
= 0; j
< arraysize(max_test_cases
); ++j
) {
296 filter
.set_min_version(min_test_cases
[i
].min_version
);
297 filter
.set_max_version(max_test_cases
[j
].max_version
);
299 if (!min_test_cases
[i
].expected_result
) {
301 internal::CheckStudyVersion(
302 filter
, Version(min_test_cases
[i
].version
));
303 EXPECT_FALSE(result
) << "Case " << i
<< "," << j
<< " failed!";
306 if (!max_test_cases
[j
].expected_result
) {
308 internal::CheckStudyVersion(
309 filter
, Version(max_test_cases
[j
].version
));
310 EXPECT_FALSE(result
) << "Case " << i
<< "," << j
<< " failed!";
316 TEST(VariationsStudyFilteringTest
, CheckStudyHardwareClass
) {
318 const char* hardware_class
;
319 const char* exclude_hardware_class
;
320 const char* actual_hardware_class
;
321 bool expected_result
;
323 // Neither filtered nor excluded set:
324 // True since empty is always a match.
325 {"", "", "fancy INTEL pear device", true},
329 {"apple,pear,orange", "", "apple", true},
330 {"apple,pear,orange", "", "fancy INTEL pear device", true},
331 {"apple,pear,orange", "", "fancy INTEL GRAPE device", false},
332 // Somehow tagged as both, but still valid.
333 {"apple,pear,orange", "", "fancy INTEL pear GRAPE device", true},
334 // Substring, so still valid.
335 {"apple,pear,orange", "", "fancy INTEL SNapple device", true},
336 // No issues with doubling.
337 {"apple,pear,orange", "", "fancy orange orange device", true},
338 // Empty, which is what would happen for non ChromeOS platforms.
339 {"apple,pear,orange", "", "", false},
342 {"", "apple,pear,orange", "apple", false},
343 {"", "apple,pear,orange", "fancy INTEL pear device", false},
344 {"", "apple,pear,orange", "fancy INTEL GRAPE device", true},
345 // Somehow tagged as both. Very excluded!
346 {"", "apple,pear,orange", "fancy INTEL pear GRAPE device", false},
347 // Substring, so still invalid.
348 {"", "apple,pear,orange", "fancy INTEL SNapple device", false},
350 {"", "apple,pear,orange", "", true},
352 // Not testing when both are set as it should never occur and should be
353 // considered undefined.
356 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
358 std::vector
<std::string
> hardware_class
;
359 base::SplitString(test_cases
[i
].hardware_class
, ',', &hardware_class
);
360 for (size_t j
= 0; j
< hardware_class
.size(); ++j
)
361 filter
.add_hardware_class(hardware_class
[j
]);
363 std::vector
<std::string
> exclude_hardware_class
;
364 base::SplitString(test_cases
[i
].exclude_hardware_class
, ',',
365 &exclude_hardware_class
);
366 for (size_t j
= 0; j
< exclude_hardware_class
.size(); ++j
)
367 filter
.add_exclude_hardware_class(exclude_hardware_class
[j
]);
369 EXPECT_EQ(test_cases
[i
].expected_result
,
370 internal::CheckStudyHardwareClass(
371 filter
, test_cases
[i
].actual_hardware_class
));
375 TEST(VariationsStudyFilteringTest
, CheckStudyCountry
) {
378 const char* exclude_country
;
379 const char* actual_country
;
380 bool expected_result
;
382 // Neither filtered nor excluded set:
383 // True since empty is always a match.
384 {"", "", "us", true},
388 {"us", "", "us", true},
389 {"br,ca,us", "", "us", true},
390 {"br,ca,us", "", "in", false},
391 // Empty, which is what would happen if no country was returned from the
393 {"br,ca,us", "", "", false},
396 {"", "us", "us", false},
397 {"", "br,ca,us", "us", false},
398 {"", "br,ca,us", "in", true},
399 // Empty, which is what would happen if no country was returned from the
401 {"", "br,ca,us", "", true},
403 // Not testing when both are set as it should never occur and should be
404 // considered undefined.
407 for (const auto& test
: test_cases
) {
409 std::vector
<std::string
> countries
;
410 base::SplitString(test
.country
, ',', &countries
);
411 for (const std::string
& country
: countries
)
412 filter
.add_country(country
);
414 std::vector
<std::string
> exclude_countries
;
415 base::SplitString(test
.exclude_country
, ',', &exclude_countries
);
416 for (const std::string
& exclude_country
: exclude_countries
)
417 filter
.add_exclude_country(exclude_country
);
419 EXPECT_EQ(test
.expected_result
,
420 internal::CheckStudyCountry(filter
, test
.actual_country
));
424 TEST(VariationsStudyFilteringTest
, FilterAndValidateStudies
) {
425 const std::string kTrial1Name
= "A";
426 const std::string kGroup1Name
= "Group1";
427 const std::string kTrial3Name
= "B";
430 Study
* study1
= seed
.add_study();
431 study1
->set_name(kTrial1Name
);
432 study1
->set_default_experiment_name("Default");
433 AddExperiment(kGroup1Name
, 100, study1
);
434 AddExperiment("Default", 0, study1
);
436 Study
* study2
= seed
.add_study();
438 study2
->mutable_experiment(0)->set_name("Bam");
439 ASSERT_EQ(seed
.study(0).name(), seed
.study(1).name());
441 Study
* study3
= seed
.add_study();
442 study3
->set_name(kTrial3Name
);
443 study3
->set_default_experiment_name("Default");
444 AddExperiment("A", 10, study3
);
445 AddExperiment("Default", 25, study3
);
447 std::vector
<ProcessedStudy
> processed_studies
;
448 FilterAndValidateStudies(seed
, "en-CA", base::Time::Now(),
449 base::Version("20.0.0.0"), Study_Channel_STABLE
,
450 Study_FormFactor_DESKTOP
, "", "",
453 // Check that only the first kTrial1Name study was kept.
454 ASSERT_EQ(2U, processed_studies
.size());
455 EXPECT_EQ(kTrial1Name
, processed_studies
[0].study()->name());
456 EXPECT_EQ(kGroup1Name
, processed_studies
[0].study()->experiment(0).name());
457 EXPECT_EQ(kTrial3Name
, processed_studies
[1].study()->name());
460 TEST(VariationsStudyFilteringTest
, FilterAndValidateStudiesWithCountry
) {
461 const char kSessionCountry
[] = "ca";
462 const char kPermanentCountry
[] = "us";
465 Study_Consistency consistency
;
466 const char* filter_country
;
467 const char* filter_exclude_country
;
468 bool expect_study_kept
;
470 // Country-agnostic studies should be kept regardless of country.
471 {Study_Consistency_SESSION
, nullptr, nullptr, true},
472 {Study_Consistency_PERMANENT
, nullptr, nullptr, true},
474 // Session-consistency studies should obey the country code in the seed.
475 {Study_Consistency_SESSION
, kSessionCountry
, nullptr, true},
476 {Study_Consistency_SESSION
, nullptr, kSessionCountry
, false},
477 {Study_Consistency_SESSION
, kPermanentCountry
, nullptr, false},
478 {Study_Consistency_SESSION
, nullptr, kPermanentCountry
, true},
480 // Permanent-consistency studies should obey the permanent-consistency
482 {Study_Consistency_PERMANENT
, kPermanentCountry
, nullptr, true},
483 {Study_Consistency_PERMANENT
, nullptr, kPermanentCountry
, false},
484 {Study_Consistency_PERMANENT
, kSessionCountry
, nullptr, false},
485 {Study_Consistency_PERMANENT
, nullptr, kSessionCountry
, true},
488 for (const auto& test
: test_cases
) {
490 seed
.set_country_code(kSessionCountry
);
491 Study
* study
= seed
.add_study();
492 study
->set_name("study");
493 study
->set_default_experiment_name("Default");
494 AddExperiment("Default", 100, study
);
495 study
->set_consistency(test
.consistency
);
496 if (test
.filter_country
)
497 study
->mutable_filter()->add_country(test
.filter_country
);
498 if (test
.filter_exclude_country
)
499 study
->mutable_filter()->add_exclude_country(test
.filter_exclude_country
);
501 std::vector
<ProcessedStudy
> processed_studies
;
502 FilterAndValidateStudies(seed
, "en-CA", base::Time::Now(),
503 base::Version("20.0.0.0"), Study_Channel_STABLE
,
504 Study_FormFactor_DESKTOP
, "", kPermanentCountry
,
507 EXPECT_EQ(test
.expect_study_kept
, !processed_studies
.empty());
511 TEST(VariationsStudyFilteringTest
, IsStudyExpired
) {
512 const base::Time now
= base::Time::Now();
513 const base::TimeDelta delta
= base::TimeDelta::FromHours(1);
515 const base::Time expiry_date
;
516 bool expected_result
;
517 } expiry_test_cases
[] = {
518 { now
- delta
, true },
520 { now
+ delta
, false },
525 // Expiry date not set should result in false.
526 EXPECT_FALSE(internal::IsStudyExpired(study
, now
));
528 for (size_t i
= 0; i
< arraysize(expiry_test_cases
); ++i
) {
529 study
.set_expiry_date(TimeToProtoTime(expiry_test_cases
[i
].expiry_date
));
530 const bool result
= internal::IsStudyExpired(study
, now
);
531 EXPECT_EQ(expiry_test_cases
[i
].expected_result
, result
)
532 << "Case " << i
<< " failed!";
536 TEST(VariationsStudyFilteringTest
, ValidateStudy
) {
538 study
.set_default_experiment_name("def");
539 AddExperiment("abc", 100, &study
);
540 Study_Experiment
* default_group
= AddExperiment("def", 200, &study
);
542 ProcessedStudy processed_study
;
543 EXPECT_TRUE(processed_study
.Init(&study
, false));
544 EXPECT_EQ(300, processed_study
.total_probability());
546 // Min version checks.
547 study
.mutable_filter()->set_min_version("1.2.3.*");
548 EXPECT_TRUE(processed_study
.Init(&study
, false));
549 study
.mutable_filter()->set_min_version("1.*.3");
550 EXPECT_FALSE(processed_study
.Init(&study
, false));
551 study
.mutable_filter()->set_min_version("1.2.3");
552 EXPECT_TRUE(processed_study
.Init(&study
, false));
554 // Max version checks.
555 study
.mutable_filter()->set_max_version("2.3.4.*");
556 EXPECT_TRUE(processed_study
.Init(&study
, false));
557 study
.mutable_filter()->set_max_version("*.3");
558 EXPECT_FALSE(processed_study
.Init(&study
, false));
559 study
.mutable_filter()->set_max_version("2.3.4");
560 EXPECT_TRUE(processed_study
.Init(&study
, false));
562 study
.clear_default_experiment_name();
563 EXPECT_FALSE(processed_study
.Init(&study
, false));
565 study
.set_default_experiment_name("xyz");
566 EXPECT_FALSE(processed_study
.Init(&study
, false));
568 study
.set_default_experiment_name("def");
569 default_group
->clear_name();
570 EXPECT_FALSE(processed_study
.Init(&study
, false));
572 default_group
->set_name("def");
573 EXPECT_TRUE(processed_study
.Init(&study
, false));
574 Study_Experiment
* repeated_group
= study
.add_experiment();
575 repeated_group
->set_name("abc");
576 repeated_group
->set_probability_weight(1);
577 EXPECT_FALSE(processed_study
.Init(&study
, false));
580 } // namespace variations