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 "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/resource_request_allowed_notifier_te st_util.h" | |
| 8 #include "chrome/browser/metrics/variations/variations_service.h" | 9 #include "chrome/browser/metrics/variations/variations_service.h" |
| 9 #include "chrome/common/chrome_version_info.h" | 10 #include "chrome/common/chrome_version_info.h" |
| 10 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/testing_browser_process.h" | |
| 11 #include "chrome/test/base/testing_pref_service.h" | 13 #include "chrome/test/base/testing_pref_service.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 namespace chrome_variations { | 17 namespace chrome_variations { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 // A test class used to validate expected functionality in VariationsService. | 21 // A test class used to validate expected functionality in VariationsService. |
| 20 class TestVariationsService : public VariationsService { | 22 class TestVariationsService : public VariationsService { |
| 21 public: | 23 public: |
| 22 TestVariationsService() : VariationsService(), | 24 explicit TestVariationsService(TestRequestAllowedNotifier* test_notifier) |
| 23 fetch_attempted_(false) { | 25 : VariationsService(test_notifier), |
| 26 fetch_attempted_(false) { | |
| 27 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. | |
| 28 SetCreateTrialsFromSeedCalledForTesting(true); | |
| 24 } | 29 } |
| 25 virtual ~TestVariationsService() {} | 30 |
| 31 virtual ~TestVariationsService() { | |
| 32 } | |
| 26 | 33 |
| 27 bool fetch_attempted() const { return fetch_attempted_; } | 34 bool fetch_attempted() const { return fetch_attempted_; } |
| 28 void SetFetchAttempted(bool attempted) { fetch_attempted_ = attempted; } | |
| 29 | 35 |
| 30 protected: | 36 protected: |
| 31 virtual void FetchVariationsSeed() OVERRIDE { | 37 virtual void DoActualFetch() OVERRIDE { |
| 32 fetch_attempted_ = true; | 38 fetch_attempted_ = true; |
| 33 } | 39 } |
| 34 | 40 |
| 35 private: | 41 private: |
| 36 bool fetch_attempted_; | 42 bool fetch_attempted_; |
| 37 | 43 |
| 38 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); | 44 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); |
| 39 }; | 45 }; |
| 40 | 46 |
| 41 // Override NetworkChangeNotifier to simulate connection type changes for tests. | |
| 42 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | |
| 43 public: | |
| 44 TestNetworkChangeNotifier() | |
| 45 : net::NetworkChangeNotifier(), | |
| 46 connection_type_to_return_( | |
| 47 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { | |
| 48 } | |
| 49 | |
| 50 void SimulateNetworkConnectionChange( | |
| 51 net::NetworkChangeNotifier::ConnectionType type) { | |
| 52 connection_type_to_return_ = type; | |
| 53 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
| 54 MessageLoop::current()->RunAllPending(); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { | |
| 59 return connection_type_to_return_; | |
| 60 } | |
| 61 | |
| 62 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | |
| 65 }; | |
| 66 | |
| 67 // Converts |time| to Study proto format. | 47 // Converts |time| to Study proto format. |
| 68 int64 TimeToProtoTime(const base::Time& time) { | 48 int64 TimeToProtoTime(const base::Time& time) { |
| 69 return (time - base::Time::UnixEpoch()).InSeconds(); | 49 return (time - base::Time::UnixEpoch()).InSeconds(); |
| 70 } | 50 } |
| 71 | 51 |
| 72 // Populates |seed| with simple test data. The resulting seed will contain one | 52 // Populates |seed| with simple test data. The resulting seed will contain one |
| 73 // study called "test", which contains one experiment called "abc" with | 53 // study called "test", which contains one experiment called "abc" with |
| 74 // probability weight 100. |seed|'s study field will be cleared before adding | 54 // probability weight 100. |seed|'s study field will be cleared before adding |
| 75 // the new study. | 55 // the new study. |
| 76 TrialsSeed CreateTestSeed() { | 56 TrialsSeed CreateTestSeed() { |
| 77 TrialsSeed seed; | 57 TrialsSeed seed; |
| 78 Study* study = seed.add_study(); | 58 Study* study = seed.add_study(); |
| 79 study->set_name("test"); | 59 study->set_name("test"); |
| 80 study->set_default_experiment_name("abc"); | 60 study->set_default_experiment_name("abc"); |
| 81 Study_Experiment* experiment = study->add_experiment(); | 61 Study_Experiment* experiment = study->add_experiment(); |
| 82 experiment->set_name("abc"); | 62 experiment->set_name("abc"); |
| 83 experiment->set_probability_weight(100); | 63 experiment->set_probability_weight(100); |
| 84 return seed; | 64 return seed; |
| 85 } | 65 } |
| 86 | 66 |
| 87 } // namespace | 67 } // namespace |
| 88 | 68 |
| 89 // A test fixture class for VariationsService tests that require network state | |
| 90 // simulations. | |
| 91 class VariationsServiceNetworkTest : public testing::Test { | |
| 92 public: | |
| 93 VariationsServiceNetworkTest() | |
| 94 : ui_thread(content::BrowserThread::UI, &message_loop) { } | |
| 95 ~VariationsServiceNetworkTest() { } | |
| 96 | |
| 97 void SetWasOfflineDuringLastRequestAttempt(bool offline) { | |
| 98 test_service.SetWasOfflineDuringLastRequestAttemptForTesting(offline); | |
| 99 } | |
| 100 | |
| 101 void SimulateNetworkConnectionChange( | |
| 102 net::NetworkChangeNotifier::ConnectionType type) { | |
| 103 notifier.SimulateNetworkConnectionChange(type); | |
| 104 } | |
| 105 | |
| 106 bool fetch_attempted() const { | |
| 107 return test_service.fetch_attempted(); | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 MessageLoopForUI message_loop; | |
| 112 content::TestBrowserThread ui_thread; | |
| 113 TestNetworkChangeNotifier notifier; | |
| 114 TestVariationsService test_service; | |
| 115 | |
| 116 DISALLOW_COPY_AND_ASSIGN(VariationsServiceNetworkTest); | |
| 117 }; | |
| 118 | |
| 119 TEST(VariationsServiceTest, CheckStudyChannel) { | 69 TEST(VariationsServiceTest, CheckStudyChannel) { |
| 120 const chrome::VersionInfo::Channel channels[] = { | 70 const chrome::VersionInfo::Channel channels[] = { |
| 121 chrome::VersionInfo::CHANNEL_CANARY, | 71 chrome::VersionInfo::CHANNEL_CANARY, |
| 122 chrome::VersionInfo::CHANNEL_DEV, | 72 chrome::VersionInfo::CHANNEL_DEV, |
| 123 chrome::VersionInfo::CHANNEL_BETA, | 73 chrome::VersionInfo::CHANNEL_BETA, |
| 124 chrome::VersionInfo::CHANNEL_STABLE, | 74 chrome::VersionInfo::CHANNEL_STABLE, |
| 125 }; | 75 }; |
| 126 const Study_Channel study_channels[] = { | 76 const Study_Channel study_channels[] = { |
| 127 Study_Channel_CANARY, | 77 Study_Channel_CANARY, |
| 128 Study_Channel_DEV, | 78 Study_Channel_DEV, |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 | 344 |
| 395 // Store good seed data to test if loading from prefs works. | 345 // Store good seed data to test if loading from prefs works. |
| 396 TrialsSeed seed = CreateTestSeed(); | 346 TrialsSeed seed = CreateTestSeed(); |
| 397 | 347 |
| 398 std::string serialized_seed; | 348 std::string serialized_seed; |
| 399 seed.SerializeToString(&serialized_seed); | 349 seed.SerializeToString(&serialized_seed); |
| 400 std::string base64_serialized_seed; | 350 std::string base64_serialized_seed; |
| 401 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); | 351 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); |
| 402 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); | 352 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); |
| 403 | 353 |
| 404 VariationsService variations_service; | 354 TestVariationsService variations_service(new TestRequestAllowedNotifier); |
| 405 TrialsSeed loaded_seed; | 355 TrialsSeed loaded_seed; |
| 406 EXPECT_TRUE( | 356 EXPECT_TRUE( |
| 407 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); | 357 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); |
| 408 | 358 |
| 409 std::string serialized_loaded_seed; | 359 std::string serialized_loaded_seed; |
| 410 loaded_seed.SerializeToString(&serialized_loaded_seed); | 360 loaded_seed.SerializeToString(&serialized_loaded_seed); |
| 411 // Check that the loaded data is the same as the original. | 361 // Check that the loaded data is the same as the original. |
| 412 EXPECT_EQ(serialized_seed, serialized_loaded_seed); | 362 EXPECT_EQ(serialized_seed, serialized_loaded_seed); |
| 413 // Make sure the pref hasn't been changed. | 363 // Make sure the pref hasn't been changed. |
| 414 EXPECT_FALSE( | 364 EXPECT_FALSE( |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 428 } | 378 } |
| 429 | 379 |
| 430 TEST(VariationsServiceTest, StoreSeed) { | 380 TEST(VariationsServiceTest, StoreSeed) { |
| 431 TestingPrefService pref_service; | 381 TestingPrefService pref_service; |
| 432 | 382 |
| 433 VariationsService::RegisterPrefs(&pref_service); | 383 VariationsService::RegisterPrefs(&pref_service); |
| 434 const base::Time now = base::Time::Now(); | 384 const base::Time now = base::Time::Now(); |
| 435 | 385 |
| 436 TrialsSeed seed = CreateTestSeed(); | 386 TrialsSeed seed = CreateTestSeed(); |
| 437 | 387 |
| 438 VariationsService variations_service; | 388 TestVariationsService variations_service(new TestRequestAllowedNotifier); |
| 439 std::string serialized_seed; | 389 std::string serialized_seed; |
| 440 seed.SerializeToString(&serialized_seed); | 390 seed.SerializeToString(&serialized_seed); |
| 441 EXPECT_TRUE( | 391 EXPECT_TRUE( |
| 442 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); | 392 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); |
| 443 // Make sure the pref was actually set. | 393 // Make sure the pref was actually set. |
| 444 EXPECT_FALSE( | 394 EXPECT_FALSE( |
| 445 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 395 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 446 | 396 |
| 447 std::string loaded_serialized_seed = | 397 std::string loaded_serialized_seed = |
| 448 pref_service.GetString(prefs::kVariationsSeed); | 398 pref_service.GetString(prefs::kVariationsSeed); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 &total_probability); | 477 &total_probability); |
| 528 ASSERT_TRUE(valid); | 478 ASSERT_TRUE(valid); |
| 529 Study_Experiment* repeated_group = study.add_experiment(); | 479 Study_Experiment* repeated_group = study.add_experiment(); |
| 530 repeated_group->set_name("abc"); | 480 repeated_group->set_name("abc"); |
| 531 repeated_group->set_probability_weight(1); | 481 repeated_group->set_probability_weight(1); |
| 532 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 482 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
| 533 &total_probability); | 483 &total_probability); |
| 534 EXPECT_FALSE(valid); | 484 EXPECT_FALSE(valid); |
| 535 } | 485 } |
| 536 | 486 |
| 537 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOffline) { | 487 TEST(VariationsServiceTest, ResourceRequestAllowedNotifierTest) { |
| 538 SetWasOfflineDuringLastRequestAttempt(true); | 488 MessageLoopForUI message_loop; |
| 539 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | 489 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 540 EXPECT_FALSE(fetch_attempted()); | 490 &message_loop); |
| 541 } | |
| 542 | 491 |
| 543 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOnlineToOnline) { | 492 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 544 SetWasOfflineDuringLastRequestAttempt(false); | 493 // manipulate it for this test. |
| 545 SimulateNetworkConnectionChange( | 494 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; |
| 546 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 495 TestVariationsService test_service(test_notifier); |
| 547 EXPECT_FALSE(fetch_attempted()); | 496 test_service.StartRepeatedVariationsSeedFetch(); |
| 548 } | 497 EXPECT_FALSE(test_service.fetch_attempted()); |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Can you add a comment explaining why this is expec
SteveT
2012/09/24 18:04:27
I have to push back on this because I believe that
Alexei Svitkine (slow)
2012/09/24 18:24:20
Sorry, what I actually meant was:
Can you add a s
SteveT
2012/09/24 18:46:21
Oh, yes, of course :)
| |
| 549 | 498 test_notifier->NotifyObservers(); |
| 550 TEST_F(VariationsServiceNetworkTest, FetchOnReconnect) { | 499 EXPECT_TRUE(test_service.fetch_attempted()); |
| 551 SetWasOfflineDuringLastRequestAttempt(true); | |
| 552 SimulateNetworkConnectionChange( | |
| 553 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 554 EXPECT_TRUE(fetch_attempted()); | |
| 555 } | |
| 556 | |
| 557 TEST_F(VariationsServiceNetworkTest, NoFetchOnWardriving) { | |
| 558 SetWasOfflineDuringLastRequestAttempt(false); | |
| 559 SimulateNetworkConnectionChange( | |
| 560 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 561 EXPECT_FALSE(fetch_attempted()); | |
| 562 SimulateNetworkConnectionChange( | |
| 563 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 564 EXPECT_FALSE(fetch_attempted()); | |
| 565 SimulateNetworkConnectionChange( | |
| 566 net::NetworkChangeNotifier::CONNECTION_4G); | |
| 567 EXPECT_FALSE(fetch_attempted()); | |
| 568 SimulateNetworkConnectionChange( | |
| 569 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 570 EXPECT_FALSE(fetch_attempted()); | |
| 571 } | |
| 572 | |
| 573 TEST_F(VariationsServiceNetworkTest, NoFetchOnFlakyConnection) { | |
| 574 SetWasOfflineDuringLastRequestAttempt(false); | |
| 575 SimulateNetworkConnectionChange( | |
| 576 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 577 EXPECT_FALSE(fetch_attempted()); | |
| 578 SimulateNetworkConnectionChange( | |
| 579 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 580 EXPECT_FALSE(fetch_attempted()); | |
| 581 SimulateNetworkConnectionChange( | |
| 582 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 583 EXPECT_FALSE(fetch_attempted()); | |
| 584 } | 500 } |
| 585 | 501 |
| 586 } // namespace chrome_variations | 502 } // namespace chrome_variations |
| OLD | NEW |