Chromium Code Reviews| 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 "chrome/browser/metrics/proto/study.pb.h" | 6 #include "chrome/browser/metrics/proto/study.pb.h" |
| 6 #include "chrome/browser/metrics/variations_service.h" | 7 #include "chrome/browser/metrics/variations_service.h" |
| 7 #include "chrome/common/chrome_version_info.h" | 8 #include "chrome/common/chrome_version_info.h" |
| 9 #include "chrome/common/pref_names.h" | |
| 10 #include "chrome/test/base/testing_pref_service.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 12 |
| 10 namespace chrome_variations { | 13 namespace chrome_variations { |
| 11 | 14 |
| 12 namespace { | 15 namespace { |
| 13 | 16 |
| 14 // Converts |time| to Study proto format. | 17 // Converts |time| to Study proto format. |
| 15 int64 TimeToProtoTime(const base::Time& time) { | 18 int64 TimeToProtoTime(const base::Time& time) { |
| 16 return (time - base::Time::UnixEpoch()).InSeconds(); | 19 return (time - base::Time::UnixEpoch()).InSeconds(); |
| 17 } | 20 } |
| 18 | 21 |
| 22 // Populates |seed| with simple test data. The resulting seed will contain one | |
| 23 // study called "test", which contains one experiment called "abc" with | |
| 24 // probability weight 100. |seed|'s study field will be cleared before adding | |
| 25 // the new study. | |
| 26 void CreateTestSeed(chrome_variations::TrialsSeed* seed) { | |
|
SteveT
2012/07/09 19:23:26
nit: DCHECK(seed);
SteveT
2012/07/09 19:23:26
You currently don't use these seeds more than once
jwd
2012/07/09 21:08:13
Done.
jwd
2012/07/09 21:08:13
Done.
| |
| 27 seed->clear_study(); | |
| 28 chrome_variations::Study* study = seed->add_study(); | |
| 29 study->set_name("test"); | |
| 30 study->set_default_experiment_name("abc"); | |
| 31 chrome_variations::Study_Experiment* experiment = study->add_experiment(); | |
| 32 experiment->set_name("abc"); | |
| 33 experiment->set_probability_weight(100); | |
| 34 } | |
| 35 | |
| 19 } // namespace | 36 } // namespace |
| 20 | 37 |
| 21 TEST(VariationsServiceTest, CheckStudyChannel) { | 38 TEST(VariationsServiceTest, CheckStudyChannel) { |
| 22 const chrome::VersionInfo::Channel channels[] = { | 39 const chrome::VersionInfo::Channel channels[] = { |
| 23 chrome::VersionInfo::CHANNEL_CANARY, | 40 chrome::VersionInfo::CHANNEL_CANARY, |
| 24 chrome::VersionInfo::CHANNEL_DEV, | 41 chrome::VersionInfo::CHANNEL_DEV, |
| 25 chrome::VersionInfo::CHANNEL_BETA, | 42 chrome::VersionInfo::CHANNEL_BETA, |
| 26 chrome::VersionInfo::CHANNEL_STABLE, | 43 chrome::VersionInfo::CHANNEL_STABLE, |
| 27 }; | 44 }; |
| 28 const Study_Channel study_channels[] = { | 45 const Study_Channel study_channels[] = { |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 &total_probability); | 342 &total_probability); |
| 326 ASSERT_TRUE(valid); | 343 ASSERT_TRUE(valid); |
| 327 Study_Experiment* repeated_group = study.add_experiment(); | 344 Study_Experiment* repeated_group = study.add_experiment(); |
| 328 repeated_group->set_name("abc"); | 345 repeated_group->set_name("abc"); |
| 329 repeated_group->set_probability_weight(1); | 346 repeated_group->set_probability_weight(1); |
| 330 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 347 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
| 331 &total_probability); | 348 &total_probability); |
| 332 EXPECT_FALSE(valid); | 349 EXPECT_FALSE(valid); |
| 333 } | 350 } |
| 334 | 351 |
| 352 TEST(VariationsServiceTest, StoreSeed) { | |
| 353 TestingPrefService pref_service; | |
| 354 | |
| 355 VariationsService::RegisterPrefs(&pref_service); | |
| 356 const base::Time now = base::Time::Now(); | |
| 357 | |
| 358 chrome_variations::TrialsSeed seed; | |
| 359 CreateTestSeed(&seed); | |
| 360 | |
| 361 VariationsService variations_service; | |
| 362 std::string serialized_seed; | |
| 363 seed.SerializeToString(&serialized_seed); | |
| 364 variations_service.StoreSeedData(serialized_seed, now, &pref_service); | |
| 365 // Make sure the pref was actually set. | |
| 366 EXPECT_FALSE( | |
| 367 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | |
| 368 | |
| 369 std::string loaded_serialized_seed = | |
| 370 pref_service.GetString(prefs::kVariationsSeed); | |
| 371 std::string decoded_serialized_seed; | |
| 372 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, | |
| 373 &decoded_serialized_seed)); | |
| 374 // Make sure the stored seed from pref is the same as the seed we created. | |
| 375 EXPECT_EQ(serialized_seed, decoded_serialized_seed); | |
| 376 | |
| 377 // Check if trying to store a bad seed leaves the pref unchanged. | |
| 378 pref_service.ClearPref(prefs::kVariationsSeed); | |
| 379 variations_service.StoreSeedData("this should fail", now, &pref_service); | |
|
SteveT
2012/07/09 19:23:26
Optional: Might be nice to have StoreSeedData retu
jwd
2012/07/09 21:08:13
Done.
| |
| 380 EXPECT_TRUE( | |
| 381 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | |
| 382 } | |
| 383 | |
| 384 TEST(VariationsServiceTest, LoadSeed) { | |
| 385 TestingPrefService pref_service; | |
| 386 | |
| 387 VariationsService::RegisterPrefs(&pref_service); | |
| 388 | |
| 389 // Store good seed data to test if loading from prefs works. | |
| 390 chrome_variations::TrialsSeed seed; | |
| 391 CreateTestSeed(&seed); | |
| 392 | |
| 393 std::string serialized_seed; | |
| 394 seed.SerializeToString(&serialized_seed); | |
| 395 std::string base64_serialized_seed; | |
| 396 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); | |
| 397 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); | |
| 398 | |
| 399 VariationsService variations_service; | |
| 400 chrome_variations::TrialsSeed loaded_seed; | |
| 401 EXPECT_TRUE( | |
| 402 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); | |
| 403 | |
| 404 std::string serialized_loaded_seed; | |
| 405 loaded_seed.SerializeToString(&serialized_loaded_seed); | |
| 406 // Check that the loaded data is the same as the original. | |
| 407 EXPECT_EQ(serialized_seed, serialized_loaded_seed); | |
| 408 // Make sure the pref hasn't been changed. | |
| 409 EXPECT_FALSE( | |
| 410 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | |
| 411 EXPECT_EQ(base64_serialized_seed, | |
| 412 pref_service.GetString(prefs::kVariationsSeed)); | |
| 413 | |
| 414 // Check that loading a bad seed returns false and clears the pref. | |
| 415 pref_service.ClearPref(prefs::kVariationsSeed); | |
| 416 pref_service.SetString(prefs::kVariationsSeed, "this should fail"); | |
|
SteveT
2012/07/09 19:23:26
Would this be useful to ensure that we're actually
jwd
2012/07/09 21:08:13
Done. Might as well.
| |
| 417 EXPECT_FALSE( | |
| 418 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); | |
| 419 EXPECT_TRUE( | |
| 420 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | |
| 421 } | |
| 422 | |
| 335 } // namespace chrome_variations | 423 } // namespace chrome_variations |
| OLD | NEW |