Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / variations / study_filtering_unittest.cc
blob29b7c93ed8936541d8a13bad773d68f1f24943ea
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"
7 #include <vector>
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 {
15 namespace {
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,
24 Study* study) {
25 Study_Experiment* experiment = study->add_experiment();
26 experiment->set_name(name);
27 experiment->set_probability_weight(probability);
28 return experiment;
31 } // namespace
33 TEST(VariationsStudyFilteringTest, CheckStudyChannel) {
34 const Study_Channel channels[] = {
35 Study_Channel_CANARY,
36 Study_Channel_DEV,
37 Study_Channel_BETA,
38 Study_Channel_STABLE,
40 bool channel_added[arraysize(channels)] = { 0 };
42 Study_Filter filter;
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 };
88 Study_Filter filter;
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,
95 form_factors[j]);
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,
113 form_factors[j]);
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) {
126 struct {
127 const char* filter_locales;
128 bool en_us_result;
129 bool en_ca_result;
130 bool fr_result;
131 } test_cases[] = {
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 Study_Filter filter;
143 for (const std::string& locale : base::SplitString(
144 test_cases[i].filter_locales, ",",
145 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL))
146 filter.add_locale(locale);
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 };
169 Study_Filter filter;
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);
207 const struct {
208 const base::Time start_date;
209 bool expected_result;
210 } start_test_cases[] = {
211 { now - delta, true },
212 { now, true },
213 { now + delta, false },
216 Study_Filter filter;
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) {
230 const struct {
231 const char* min_version;
232 const char* 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 },
241 // Wildcards.
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 },
250 const struct {
251 const char* max_version;
252 const char* 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 },
260 // Wildcards
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 },
271 Study_Filter filter;
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);
278 const bool result =
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);
287 const bool result =
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) {
300 const bool 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) {
307 const bool result =
308 internal::CheckStudyVersion(
309 filter, Version(max_test_cases[j].version));
310 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!";
316 TEST(VariationsStudyFilteringTest, CheckStudyHardwareClass) {
317 struct {
318 const char* hardware_class;
319 const char* exclude_hardware_class;
320 const char* actual_hardware_class;
321 bool expected_result;
322 } test_cases[] = {
323 // Neither filtered nor excluded set:
324 // True since empty is always a match.
325 {"", "", "fancy INTEL pear device", true},
326 {"", "", "", true},
328 // Filtered set:
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},
341 // Excluded set:
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},
349 // Empty.
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) {
357 Study_Filter filter;
358 for (const std::string& cur : base::SplitString(
359 test_cases[i].hardware_class, ",",
360 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL))
361 filter.add_hardware_class(cur);
363 for (const std::string& cur : base::SplitString(
364 test_cases[i].exclude_hardware_class, ",",
365 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL))
366 filter.add_exclude_hardware_class(cur);
368 EXPECT_EQ(test_cases[i].expected_result,
369 internal::CheckStudyHardwareClass(
370 filter, test_cases[i].actual_hardware_class));
374 TEST(VariationsStudyFilteringTest, CheckStudyCountry) {
375 struct {
376 const char* country;
377 const char* exclude_country;
378 const char* actual_country;
379 bool expected_result;
380 } test_cases[] = {
381 // Neither filtered nor excluded set:
382 // True since empty is always a match.
383 {"", "", "us", true},
384 {"", "", "", true},
386 // Filtered set:
387 {"us", "", "us", true},
388 {"br,ca,us", "", "us", true},
389 {"br,ca,us", "", "in", false},
390 // Empty, which is what would happen if no country was returned from the
391 // server.
392 {"br,ca,us", "", "", false},
394 // Excluded set:
395 {"", "us", "us", false},
396 {"", "br,ca,us", "us", false},
397 {"", "br,ca,us", "in", true},
398 // Empty, which is what would happen if no country was returned from the
399 // server.
400 {"", "br,ca,us", "", true},
402 // Not testing when both are set as it should never occur and should be
403 // considered undefined.
406 for (const auto& test : test_cases) {
407 Study_Filter filter;
408 for (const std::string& country : base::SplitString(
409 test.country, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL))
410 filter.add_country(country);
412 for (const std::string& exclude_country : base::SplitString(
413 test.exclude_country, ",",
414 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL))
415 filter.add_exclude_country(exclude_country);
417 EXPECT_EQ(test.expected_result,
418 internal::CheckStudyCountry(filter, test.actual_country));
422 TEST(VariationsStudyFilteringTest, FilterAndValidateStudies) {
423 const std::string kTrial1Name = "A";
424 const std::string kGroup1Name = "Group1";
425 const std::string kTrial3Name = "B";
427 VariationsSeed seed;
428 Study* study1 = seed.add_study();
429 study1->set_name(kTrial1Name);
430 study1->set_default_experiment_name("Default");
431 AddExperiment(kGroup1Name, 100, study1);
432 AddExperiment("Default", 0, study1);
434 Study* study2 = seed.add_study();
435 *study2 = *study1;
436 study2->mutable_experiment(0)->set_name("Bam");
437 ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
439 Study* study3 = seed.add_study();
440 study3->set_name(kTrial3Name);
441 study3->set_default_experiment_name("Default");
442 AddExperiment("A", 10, study3);
443 AddExperiment("Default", 25, study3);
445 std::vector<ProcessedStudy> processed_studies;
446 FilterAndValidateStudies(seed, "en-CA", base::Time::Now(),
447 base::Version("20.0.0.0"), Study_Channel_STABLE,
448 Study_FormFactor_DESKTOP, "", "", "",
449 &processed_studies);
451 // Check that only the first kTrial1Name study was kept.
452 ASSERT_EQ(2U, processed_studies.size());
453 EXPECT_EQ(kTrial1Name, processed_studies[0].study()->name());
454 EXPECT_EQ(kGroup1Name, processed_studies[0].study()->experiment(0).name());
455 EXPECT_EQ(kTrial3Name, processed_studies[1].study()->name());
458 TEST(VariationsStudyFilteringTest, FilterAndValidateStudiesWithCountry) {
459 const char kSessionCountry[] = "ca";
460 const char kPermanentCountry[] = "us";
462 struct {
463 Study_Consistency consistency;
464 const char* filter_country;
465 const char* filter_exclude_country;
466 bool expect_study_kept;
467 } test_cases[] = {
468 // Country-agnostic studies should be kept regardless of country.
469 {Study_Consistency_SESSION, nullptr, nullptr, true},
470 {Study_Consistency_PERMANENT, nullptr, nullptr, true},
472 // Session-consistency studies should obey the country code in the seed.
473 {Study_Consistency_SESSION, kSessionCountry, nullptr, true},
474 {Study_Consistency_SESSION, nullptr, kSessionCountry, false},
475 {Study_Consistency_SESSION, kPermanentCountry, nullptr, false},
476 {Study_Consistency_SESSION, nullptr, kPermanentCountry, true},
478 // Permanent-consistency studies should obey the permanent-consistency
479 // country code.
480 {Study_Consistency_PERMANENT, kPermanentCountry, nullptr, true},
481 {Study_Consistency_PERMANENT, nullptr, kPermanentCountry, false},
482 {Study_Consistency_PERMANENT, kSessionCountry, nullptr, false},
483 {Study_Consistency_PERMANENT, nullptr, kSessionCountry, true},
486 for (const auto& test : test_cases) {
487 VariationsSeed seed;
488 Study* study = seed.add_study();
489 study->set_name("study");
490 study->set_default_experiment_name("Default");
491 AddExperiment("Default", 100, study);
492 study->set_consistency(test.consistency);
493 if (test.filter_country)
494 study->mutable_filter()->add_country(test.filter_country);
495 if (test.filter_exclude_country)
496 study->mutable_filter()->add_exclude_country(test.filter_exclude_country);
498 std::vector<ProcessedStudy> processed_studies;
499 FilterAndValidateStudies(seed, "en-CA", base::Time::Now(),
500 base::Version("20.0.0.0"), Study_Channel_STABLE,
501 Study_FormFactor_DESKTOP, "", kSessionCountry,
502 kPermanentCountry, &processed_studies);
504 EXPECT_EQ(test.expect_study_kept, !processed_studies.empty());
508 TEST(VariationsStudyFilteringTest, IsStudyExpired) {
509 const base::Time now = base::Time::Now();
510 const base::TimeDelta delta = base::TimeDelta::FromHours(1);
511 const struct {
512 const base::Time expiry_date;
513 bool expected_result;
514 } expiry_test_cases[] = {
515 { now - delta, true },
516 { now, true },
517 { now + delta, false },
520 Study study;
522 // Expiry date not set should result in false.
523 EXPECT_FALSE(internal::IsStudyExpired(study, now));
525 for (size_t i = 0; i < arraysize(expiry_test_cases); ++i) {
526 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date));
527 const bool result = internal::IsStudyExpired(study, now);
528 EXPECT_EQ(expiry_test_cases[i].expected_result, result)
529 << "Case " << i << " failed!";
533 TEST(VariationsStudyFilteringTest, ValidateStudy) {
534 Study study;
535 study.set_default_experiment_name("def");
536 AddExperiment("abc", 100, &study);
537 Study_Experiment* default_group = AddExperiment("def", 200, &study);
539 ProcessedStudy processed_study;
540 EXPECT_TRUE(processed_study.Init(&study, false));
541 EXPECT_EQ(300, processed_study.total_probability());
543 // Min version checks.
544 study.mutable_filter()->set_min_version("1.2.3.*");
545 EXPECT_TRUE(processed_study.Init(&study, false));
546 study.mutable_filter()->set_min_version("1.*.3");
547 EXPECT_FALSE(processed_study.Init(&study, false));
548 study.mutable_filter()->set_min_version("1.2.3");
549 EXPECT_TRUE(processed_study.Init(&study, false));
551 // Max version checks.
552 study.mutable_filter()->set_max_version("2.3.4.*");
553 EXPECT_TRUE(processed_study.Init(&study, false));
554 study.mutable_filter()->set_max_version("*.3");
555 EXPECT_FALSE(processed_study.Init(&study, false));
556 study.mutable_filter()->set_max_version("2.3.4");
557 EXPECT_TRUE(processed_study.Init(&study, false));
559 study.clear_default_experiment_name();
560 EXPECT_FALSE(processed_study.Init(&study, false));
562 study.set_default_experiment_name("xyz");
563 EXPECT_FALSE(processed_study.Init(&study, false));
565 study.set_default_experiment_name("def");
566 default_group->clear_name();
567 EXPECT_FALSE(processed_study.Init(&study, false));
569 default_group->set_name("def");
570 EXPECT_TRUE(processed_study.Init(&study, false));
571 Study_Experiment* repeated_group = study.add_experiment();
572 repeated_group->set_name("abc");
573 repeated_group->set_probability_weight(1);
574 EXPECT_FALSE(processed_study.Init(&study, false));
577 } // namespace variations