Suppress tabs permission warning if there is already a browsingHistory warning.
[chromium-blink-merge.git] / components / variations / study_filtering_unittest.cc
blob84c5cf7c9b449181240c546cfef5f8a888165dd1
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 chrome_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_UNSAFE(test_cases); ++i) {
142 std::vector<std::string> filter_locales;
143 Study_Filter filter;
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 };
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_UNSAFE(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_UNSAFE(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_UNSAFE(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_UNSAFE(min_test_cases); ++i) {
295 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(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, FilterAndValidateStudies) {
317 const std::string kTrial1Name = "A";
318 const std::string kGroup1Name = "Group1";
319 const std::string kTrial3Name = "B";
321 VariationsSeed seed;
322 Study* study1 = seed.add_study();
323 study1->set_name(kTrial1Name);
324 study1->set_default_experiment_name("Default");
325 AddExperiment(kGroup1Name, 100, study1);
326 AddExperiment("Default", 0, study1);
328 Study* study2 = seed.add_study();
329 *study2 = *study1;
330 study2->mutable_experiment(0)->set_name("Bam");
331 ASSERT_EQ(seed.study(0).name(), seed.study(1).name());
333 Study* study3 = seed.add_study();
334 study3->set_name(kTrial3Name);
335 study3->set_default_experiment_name("Default");
336 AddExperiment("A", 10, study3);
337 AddExperiment("Default", 25, study3);
339 std::vector<ProcessedStudy> processed_studies;
340 FilterAndValidateStudies(
341 seed, "en-CA", base::Time::Now(), base::Version("20.0.0.0"),
342 Study_Channel_STABLE, Study_FormFactor_DESKTOP, &processed_studies);
344 // Check that only the first kTrial1Name study was kept.
345 ASSERT_EQ(2U, processed_studies.size());
346 EXPECT_EQ(kTrial1Name, processed_studies[0].study()->name());
347 EXPECT_EQ(kGroup1Name, processed_studies[0].study()->experiment(0).name());
348 EXPECT_EQ(kTrial3Name, processed_studies[1].study()->name());
351 TEST(VariationsStudyFilteringTest, IsStudyExpired) {
352 const base::Time now = base::Time::Now();
353 const base::TimeDelta delta = base::TimeDelta::FromHours(1);
354 const struct {
355 const base::Time expiry_date;
356 bool expected_result;
357 } expiry_test_cases[] = {
358 { now - delta, true },
359 { now, true },
360 { now + delta, false },
363 Study study;
365 // Expiry date not set should result in false.
366 EXPECT_FALSE(internal::IsStudyExpired(study, now));
368 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) {
369 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date));
370 const bool result = internal::IsStudyExpired(study, now);
371 EXPECT_EQ(expiry_test_cases[i].expected_result, result)
372 << "Case " << i << " failed!";
376 TEST(VariationsStudyFilteringTest, ValidateStudy) {
377 Study study;
378 study.set_default_experiment_name("def");
379 AddExperiment("abc", 100, &study);
380 Study_Experiment* default_group = AddExperiment("def", 200, &study);
382 ProcessedStudy processed_study;
383 EXPECT_TRUE(processed_study.Init(&study, false));
384 EXPECT_EQ(300, processed_study.total_probability());
386 // Min version checks.
387 study.mutable_filter()->set_min_version("1.2.3.*");
388 EXPECT_TRUE(processed_study.Init(&study, false));
389 study.mutable_filter()->set_min_version("1.*.3");
390 EXPECT_FALSE(processed_study.Init(&study, false));
391 study.mutable_filter()->set_min_version("1.2.3");
392 EXPECT_TRUE(processed_study.Init(&study, false));
394 // Max version checks.
395 study.mutable_filter()->set_max_version("2.3.4.*");
396 EXPECT_TRUE(processed_study.Init(&study, false));
397 study.mutable_filter()->set_max_version("*.3");
398 EXPECT_FALSE(processed_study.Init(&study, false));
399 study.mutable_filter()->set_max_version("2.3.4");
400 EXPECT_TRUE(processed_study.Init(&study, false));
402 study.clear_default_experiment_name();
403 EXPECT_FALSE(processed_study.Init(&study, false));
405 study.set_default_experiment_name("xyz");
406 EXPECT_FALSE(processed_study.Init(&study, false));
408 study.set_default_experiment_name("def");
409 default_group->clear_name();
410 EXPECT_FALSE(processed_study.Init(&study, false));
412 default_group->set_name("def");
413 EXPECT_TRUE(processed_study.Init(&study, false));
414 Study_Experiment* repeated_group = study.add_experiment();
415 repeated_group->set_name("abc");
416 repeated_group->set_probability_weight(1);
417 EXPECT_FALSE(processed_study.Init(&study, false));
420 } // namespace chrome_variations