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 "base/base64.h" | 5 #include "base/base64.h" |
6 #include "base/string_split.h" | 6 #include "base/string_split.h" |
7 #include "chrome/browser/metrics/proto/study.pb.h" | 7 #include "chrome/browser/metrics/proto/study.pb.h" |
8 #include "chrome/browser/metrics/variations_service.h" | 8 #include "chrome/browser/metrics/variations_service.h" |
9 #include "chrome/common/chrome_version_info.h" | 9 #include "chrome/common/chrome_version_info.h" |
10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
11 #include "chrome/test/base/testing_pref_service.h" | 11 #include "chrome/test/base/testing_pref_service.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace chrome_variations { | 14 namespace chrome_variations { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Converts |time| to Study proto format. | 18 // Converts |time| to Study proto format. |
19 int64 TimeToProtoTime(const base::Time& time) { | 19 int64 TimeToProtoTime(const base::Time& time) { |
20 return (time - base::Time::UnixEpoch()).InSeconds(); | 20 return (time - base::Time::UnixEpoch()).InSeconds(); |
21 } | 21 } |
22 | 22 |
23 // Populates |seed| with simple test data. The resulting seed will contain one | 23 // Populates |seed| with simple test data. The resulting seed will contain one |
24 // study called "test", which contains one experiment called "abc" with | 24 // study called "test", which contains one experiment called "abc" with |
25 // probability weight 100. |seed|'s study field will be cleared before adding | 25 // probability weight 100. |seed|'s study field will be cleared before adding |
26 // the new study. | 26 // the new study. |
27 chrome_variations::TrialsSeed CreateTestSeed() { | 27 TrialsSeed CreateTestSeed() { |
28 chrome_variations::TrialsSeed seed; | 28 TrialsSeed seed; |
29 chrome_variations::Study* study = seed.add_study(); | 29 Study* study = seed.add_study(); |
30 study->set_name("test"); | 30 study->set_name("test"); |
31 study->set_default_experiment_name("abc"); | 31 study->set_default_experiment_name("abc"); |
32 chrome_variations::Study_Experiment* experiment = study->add_experiment(); | 32 Study_Experiment* experiment = study->add_experiment(); |
33 experiment->set_name("abc"); | 33 experiment->set_name("abc"); |
34 experiment->set_probability_weight(100); | 34 experiment->set_probability_weight(100); |
35 return seed; | 35 return seed; |
36 } | 36 } |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 TEST(VariationsServiceTest, CheckStudyChannel) { | 40 TEST(VariationsServiceTest, CheckStudyChannel) { |
41 const chrome::VersionInfo::Channel channels[] = { | 41 const chrome::VersionInfo::Channel channels[] = { |
42 chrome::VersionInfo::CHANNEL_CANARY, | 42 chrome::VersionInfo::CHANNEL_CANARY, |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 EXPECT_EQ(test_cases[i].en_us_result, | 115 EXPECT_EQ(test_cases[i].en_us_result, |
116 VariationsService::CheckStudyLocale(filter, "en-US")); | 116 VariationsService::CheckStudyLocale(filter, "en-US")); |
117 EXPECT_EQ(test_cases[i].en_ca_result, | 117 EXPECT_EQ(test_cases[i].en_ca_result, |
118 VariationsService::CheckStudyLocale(filter, "en-CA")); | 118 VariationsService::CheckStudyLocale(filter, "en-CA")); |
119 EXPECT_EQ(test_cases[i].fr_result, | 119 EXPECT_EQ(test_cases[i].fr_result, |
120 VariationsService::CheckStudyLocale(filter, "fr")); | 120 VariationsService::CheckStudyLocale(filter, "fr")); |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 TEST(VariationsServiceTest, CheckStudyPlatform) { | 124 TEST(VariationsServiceTest, CheckStudyPlatform) { |
125 const chrome_variations::Study_Platform platforms[] = { | 125 const Study_Platform platforms[] = { |
126 chrome_variations::Study_Platform_PLATFORM_WINDOWS, | 126 Study_Platform_PLATFORM_WINDOWS, |
127 chrome_variations::Study_Platform_PLATFORM_MAC, | 127 Study_Platform_PLATFORM_MAC, |
128 chrome_variations::Study_Platform_PLATFORM_LINUX, | 128 Study_Platform_PLATFORM_LINUX, |
129 chrome_variations::Study_Platform_PLATFORM_CHROMEOS, | 129 Study_Platform_PLATFORM_CHROMEOS, |
130 chrome_variations::Study_Platform_PLATFORM_ANDROID, | 130 Study_Platform_PLATFORM_ANDROID, |
131 chrome_variations::Study_Platform_PLATFORM_IOS, | 131 Study_Platform_PLATFORM_IOS, |
132 }; | 132 }; |
133 ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE, | 133 ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE, |
134 static_cast<int>(arraysize(platforms))); | 134 static_cast<int>(arraysize(platforms))); |
135 bool platform_added[arraysize(platforms)] = { 0 }; | 135 bool platform_added[arraysize(platforms)] = { 0 }; |
136 | 136 |
137 Study_Filter filter; | 137 Study_Filter filter; |
138 | 138 |
139 // Check in the forwarded order. The loop cond is <= arraysize(platforms) | 139 // Check in the forwarded order. The loop cond is <= arraysize(platforms) |
140 // instead of < so that the result of adding the last channel gets checked. | 140 // instead of < so that the result of adding the last channel gets checked. |
141 for (size_t i = 0; i <= arraysize(platforms); ++i) { | 141 for (size_t i = 0; i <= arraysize(platforms); ++i) { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 << "Case " << i << " failed!"; | 307 << "Case " << i << " failed!"; |
308 } | 308 } |
309 } | 309 } |
310 | 310 |
311 TEST(VariationsServiceTest, LoadSeed) { | 311 TEST(VariationsServiceTest, LoadSeed) { |
312 TestingPrefService pref_service; | 312 TestingPrefService pref_service; |
313 | 313 |
314 VariationsService::RegisterPrefs(&pref_service); | 314 VariationsService::RegisterPrefs(&pref_service); |
315 | 315 |
316 // Store good seed data to test if loading from prefs works. | 316 // Store good seed data to test if loading from prefs works. |
317 chrome_variations::TrialsSeed seed = CreateTestSeed(); | 317 TrialsSeed seed = CreateTestSeed(); |
318 | 318 |
319 std::string serialized_seed; | 319 std::string serialized_seed; |
320 seed.SerializeToString(&serialized_seed); | 320 seed.SerializeToString(&serialized_seed); |
321 std::string base64_serialized_seed; | 321 std::string base64_serialized_seed; |
322 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); | 322 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); |
323 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); | 323 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); |
324 | 324 |
325 VariationsService variations_service; | 325 VariationsService variations_service; |
326 chrome_variations::TrialsSeed loaded_seed; | 326 TrialsSeed loaded_seed; |
327 EXPECT_TRUE( | 327 EXPECT_TRUE( |
328 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); | 328 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); |
329 | 329 |
330 std::string serialized_loaded_seed; | 330 std::string serialized_loaded_seed; |
331 loaded_seed.SerializeToString(&serialized_loaded_seed); | 331 loaded_seed.SerializeToString(&serialized_loaded_seed); |
332 // Check that the loaded data is the same as the original. | 332 // Check that the loaded data is the same as the original. |
333 EXPECT_EQ(serialized_seed, serialized_loaded_seed); | 333 EXPECT_EQ(serialized_seed, serialized_loaded_seed); |
334 // Make sure the pref hasn't been changed. | 334 // Make sure the pref hasn't been changed. |
335 EXPECT_FALSE( | 335 EXPECT_FALSE( |
336 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 336 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
(...skipping 10 matching lines...) Expand all Loading... |
347 EXPECT_TRUE( | 347 EXPECT_TRUE( |
348 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 348 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
349 } | 349 } |
350 | 350 |
351 TEST(VariationsServiceTest, StoreSeed) { | 351 TEST(VariationsServiceTest, StoreSeed) { |
352 TestingPrefService pref_service; | 352 TestingPrefService pref_service; |
353 | 353 |
354 VariationsService::RegisterPrefs(&pref_service); | 354 VariationsService::RegisterPrefs(&pref_service); |
355 const base::Time now = base::Time::Now(); | 355 const base::Time now = base::Time::Now(); |
356 | 356 |
357 chrome_variations::TrialsSeed seed = CreateTestSeed(); | 357 TrialsSeed seed = CreateTestSeed(); |
358 | 358 |
359 VariationsService variations_service; | 359 VariationsService variations_service; |
360 std::string serialized_seed; | 360 std::string serialized_seed; |
361 seed.SerializeToString(&serialized_seed); | 361 seed.SerializeToString(&serialized_seed); |
362 EXPECT_TRUE( | 362 EXPECT_TRUE( |
363 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); | 363 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); |
364 // Make sure the pref was actually set. | 364 // Make sure the pref was actually set. |
365 EXPECT_FALSE( | 365 EXPECT_FALSE( |
366 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 366 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
367 | 367 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 ASSERT_TRUE(valid); | 449 ASSERT_TRUE(valid); |
450 Study_Experiment* repeated_group = study.add_experiment(); | 450 Study_Experiment* repeated_group = study.add_experiment(); |
451 repeated_group->set_name("abc"); | 451 repeated_group->set_name("abc"); |
452 repeated_group->set_probability_weight(1); | 452 repeated_group->set_probability_weight(1); |
453 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 453 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
454 &total_probability); | 454 &total_probability); |
455 EXPECT_FALSE(valid); | 455 EXPECT_FALSE(valid); |
456 } | 456 } |
457 | 457 |
458 } // namespace chrome_variations | 458 } // namespace chrome_variations |
OLD | NEW |