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/string_split.h" | 6 #include "base/string_split.h" |
6 #include "chrome/browser/metrics/proto/study.pb.h" | 7 #include "chrome/browser/metrics/proto/study.pb.h" |
7 #include "chrome/browser/metrics/variations_service.h" | 8 #include "chrome/browser/metrics/variations_service.h" |
8 #include "chrome/common/chrome_version_info.h" | 9 #include "chrome/common/chrome_version_info.h" |
| 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/test/base/testing_pref_service.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
10 | 13 |
11 namespace chrome_variations { | 14 namespace chrome_variations { |
12 | 15 |
13 namespace { | 16 namespace { |
14 | 17 |
15 // Converts |time| to Study proto format. | 18 // Converts |time| to Study proto format. |
16 int64 TimeToProtoTime(const base::Time& time) { | 19 int64 TimeToProtoTime(const base::Time& time) { |
17 return (time - base::Time::UnixEpoch()).InSeconds(); | 20 return (time - base::Time::UnixEpoch()).InSeconds(); |
18 } | 21 } |
19 | 22 |
| 23 // Populates |seed| with simple test data. The resulting seed will contain one |
| 24 // study called "test", which contains one experiment called "abc" with |
| 25 // probability weight 100. |seed|'s study field will be cleared before adding |
| 26 // the new study. |
| 27 chrome_variations::TrialsSeed CreateTestSeed() { |
| 28 chrome_variations::TrialsSeed seed; |
| 29 chrome_variations::Study* study = seed.add_study(); |
| 30 study->set_name("test"); |
| 31 study->set_default_experiment_name("abc"); |
| 32 chrome_variations::Study_Experiment* experiment = study->add_experiment(); |
| 33 experiment->set_name("abc"); |
| 34 experiment->set_probability_weight(100); |
| 35 return seed; |
| 36 } |
| 37 |
20 } // namespace | 38 } // namespace |
21 | 39 |
22 TEST(VariationsServiceTest, CheckStudyChannel) { | 40 TEST(VariationsServiceTest, CheckStudyChannel) { |
23 const chrome::VersionInfo::Channel channels[] = { | 41 const chrome::VersionInfo::Channel channels[] = { |
24 chrome::VersionInfo::CHANNEL_CANARY, | 42 chrome::VersionInfo::CHANNEL_CANARY, |
25 chrome::VersionInfo::CHANNEL_DEV, | 43 chrome::VersionInfo::CHANNEL_DEV, |
26 chrome::VersionInfo::CHANNEL_BETA, | 44 chrome::VersionInfo::CHANNEL_BETA, |
27 chrome::VersionInfo::CHANNEL_STABLE, | 45 chrome::VersionInfo::CHANNEL_STABLE, |
28 }; | 46 }; |
29 const Study_Channel study_channels[] = { | 47 const Study_Channel study_channels[] = { |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now)); | 301 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now)); |
284 | 302 |
285 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { | 303 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { |
286 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); | 304 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); |
287 const bool result = VariationsService::IsStudyExpired(study, now); | 305 const bool result = VariationsService::IsStudyExpired(study, now); |
288 EXPECT_EQ(expiry_test_cases[i].expected_result, result) | 306 EXPECT_EQ(expiry_test_cases[i].expected_result, result) |
289 << "Case " << i << " failed!"; | 307 << "Case " << i << " failed!"; |
290 } | 308 } |
291 } | 309 } |
292 | 310 |
| 311 TEST(VariationsServiceTest, LoadSeed) { |
| 312 TestingPrefService pref_service; |
| 313 |
| 314 VariationsService::RegisterPrefs(&pref_service); |
| 315 |
| 316 // Store good seed data to test if loading from prefs works. |
| 317 chrome_variations::TrialsSeed seed = CreateTestSeed(); |
| 318 |
| 319 std::string serialized_seed; |
| 320 seed.SerializeToString(&serialized_seed); |
| 321 std::string base64_serialized_seed; |
| 322 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); |
| 323 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); |
| 324 |
| 325 VariationsService variations_service; |
| 326 chrome_variations::TrialsSeed loaded_seed; |
| 327 EXPECT_TRUE( |
| 328 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); |
| 329 |
| 330 std::string serialized_loaded_seed; |
| 331 loaded_seed.SerializeToString(&serialized_loaded_seed); |
| 332 // Check that the loaded data is the same as the original. |
| 333 EXPECT_EQ(serialized_seed, serialized_loaded_seed); |
| 334 // Make sure the pref hasn't been changed. |
| 335 EXPECT_FALSE( |
| 336 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 337 EXPECT_EQ(base64_serialized_seed, |
| 338 pref_service.GetString(prefs::kVariationsSeed)); |
| 339 |
| 340 // Check that loading a bad seed returns false and clears the pref. |
| 341 pref_service.ClearPref(prefs::kVariationsSeed); |
| 342 pref_service.SetString(prefs::kVariationsSeed, "this should fail"); |
| 343 EXPECT_FALSE( |
| 344 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 345 EXPECT_FALSE( |
| 346 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); |
| 347 EXPECT_TRUE( |
| 348 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 349 } |
| 350 |
| 351 TEST(VariationsServiceTest, StoreSeed) { |
| 352 TestingPrefService pref_service; |
| 353 |
| 354 VariationsService::RegisterPrefs(&pref_service); |
| 355 const base::Time now = base::Time::Now(); |
| 356 |
| 357 chrome_variations::TrialsSeed seed = CreateTestSeed(); |
| 358 |
| 359 VariationsService variations_service; |
| 360 std::string serialized_seed; |
| 361 seed.SerializeToString(&serialized_seed); |
| 362 EXPECT_TRUE( |
| 363 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); |
| 364 // Make sure the pref was actually set. |
| 365 EXPECT_FALSE( |
| 366 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 367 |
| 368 std::string loaded_serialized_seed = |
| 369 pref_service.GetString(prefs::kVariationsSeed); |
| 370 std::string decoded_serialized_seed; |
| 371 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, |
| 372 &decoded_serialized_seed)); |
| 373 // Make sure the stored seed from pref is the same as the seed we created. |
| 374 EXPECT_EQ(serialized_seed, decoded_serialized_seed); |
| 375 |
| 376 // Check if trying to store a bad seed leaves the pref unchanged. |
| 377 pref_service.ClearPref(prefs::kVariationsSeed); |
| 378 EXPECT_FALSE( |
| 379 variations_service.StoreSeedData("this should fail", now, &pref_service)); |
| 380 EXPECT_TRUE( |
| 381 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 382 } |
| 383 |
293 TEST(VariationsServiceTest, ValidateStudy) { | 384 TEST(VariationsServiceTest, ValidateStudy) { |
294 Study study; | 385 Study study; |
295 study.set_default_experiment_name("def"); | 386 study.set_default_experiment_name("def"); |
296 | 387 |
297 Study_Experiment* experiment = study.add_experiment(); | 388 Study_Experiment* experiment = study.add_experiment(); |
298 experiment->set_name("abc"); | 389 experiment->set_name("abc"); |
299 experiment->set_probability_weight(100); | 390 experiment->set_probability_weight(100); |
300 | 391 |
301 Study_Experiment* default_group = study.add_experiment(); | 392 Study_Experiment* default_group = study.add_experiment(); |
302 default_group->set_name("def"); | 393 default_group->set_name("def"); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 ASSERT_TRUE(valid); | 449 ASSERT_TRUE(valid); |
359 Study_Experiment* repeated_group = study.add_experiment(); | 450 Study_Experiment* repeated_group = study.add_experiment(); |
360 repeated_group->set_name("abc"); | 451 repeated_group->set_name("abc"); |
361 repeated_group->set_probability_weight(1); | 452 repeated_group->set_probability_weight(1); |
362 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 453 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
363 &total_probability); | 454 &total_probability); |
364 EXPECT_FALSE(valid); | 455 EXPECT_FALSE(valid); |
365 } | 456 } |
366 | 457 |
367 } // namespace chrome_variations | 458 } // namespace chrome_variations |
OLD | NEW |