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 // Note that |test_notifier| may be NULL if the test does not care to perform |
| 23 fetch_attempted_(false) { | 25 // mock operations on the notifier. |
|
Alexei Svitkine (slow)
2012/09/21 15:52:05
Is the comment about the test_notifier true? I not
SteveT
2012/09/24 15:38:38
Sorry - I had tried this earlier before taking you
| |
| 24 } | 26 explicit TestVariationsService(TestRequestAllowedNotifier* test_notifier); |
| 25 virtual ~TestVariationsService() {} | 27 virtual ~TestVariationsService(); |
| 26 | 28 |
| 27 bool fetch_attempted() const { return fetch_attempted_; } | 29 bool fetch_attempted() const { return fetch_attempted_; } |
| 28 void SetFetchAttempted(bool attempted) { fetch_attempted_ = attempted; } | 30 |
| 31 void SetRequestsAllowed(bool allowed); | |
|
Alexei Svitkine (slow)
2012/09/21 15:52:05
Remove.
SteveT
2012/09/24 15:38:38
Done.
| |
| 32 | |
| 33 void NotifyObservers(); | |
|
Alexei Svitkine (slow)
2012/09/21 15:52:05
Remove.
SteveT
2012/09/24 15:38:38
Done.
| |
| 29 | 34 |
| 30 protected: | 35 protected: |
| 31 virtual void FetchVariationsSeed() OVERRIDE { | 36 virtual void DoActualFetch() OVERRIDE { |
| 32 fetch_attempted_ = true; | 37 fetch_attempted_ = true; |
| 33 } | 38 } |
| 34 | 39 |
| 35 private: | 40 private: |
| 36 bool fetch_attempted_; | 41 bool fetch_attempted_; |
| 37 | 42 |
| 43 // Weak pointer to the base class' copy of the notifier. | |
| 44 TestRequestAllowedNotifier* test_notifier_; | |
|
Alexei Svitkine (slow)
2012/09/21 15:52:05
Remove.
SteveT
2012/09/24 15:38:38
Done.
| |
| 45 | |
| 38 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); | 46 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); |
| 39 }; | 47 }; |
| 40 | 48 |
| 41 // Override NetworkChangeNotifier to simulate connection type changes for tests. | 49 TestVariationsService::TestVariationsService( |
| 42 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | 50 TestRequestAllowedNotifier* test_notifier) |
| 43 public: | 51 : VariationsService(test_notifier), |
| 44 TestNetworkChangeNotifier() | 52 fetch_attempted_(false) { |
| 45 : net::NetworkChangeNotifier(), | 53 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. |
| 46 connection_type_to_return_( | 54 SetCreateTrialsFromSeedCalledForTesting(true); |
| 47 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { | 55 } |
|
Alexei Svitkine (slow)
2012/09/21 15:52:05
Nit: Inline this into the class declaration above.
SteveT
2012/09/24 15:38:38
Done.
| |
| 48 } | |
| 49 | 56 |
| 50 void SimulateNetworkConnectionChange( | 57 TestVariationsService::~TestVariationsService() { |
|
Alexei Svitkine (slow)
2012/09/21 15:52:05
Nit: Inline this into the class declaration above.
SteveT
2012/09/24 15:38:38
Done.
| |
| 51 net::NetworkChangeNotifier::ConnectionType type) { | 58 } |
| 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 | 59 |
| 67 // Converts |time| to Study proto format. | 60 // Converts |time| to Study proto format. |
| 68 int64 TimeToProtoTime(const base::Time& time) { | 61 int64 TimeToProtoTime(const base::Time& time) { |
| 69 return (time - base::Time::UnixEpoch()).InSeconds(); | 62 return (time - base::Time::UnixEpoch()).InSeconds(); |
| 70 } | 63 } |
| 71 | 64 |
| 72 // Populates |seed| with simple test data. The resulting seed will contain one | 65 // Populates |seed| with simple test data. The resulting seed will contain one |
| 73 // study called "test", which contains one experiment called "abc" with | 66 // study called "test", which contains one experiment called "abc" with |
| 74 // probability weight 100. |seed|'s study field will be cleared before adding | 67 // probability weight 100. |seed|'s study field will be cleared before adding |
| 75 // the new study. | 68 // the new study. |
| 76 TrialsSeed CreateTestSeed() { | 69 TrialsSeed CreateTestSeed() { |
| 77 TrialsSeed seed; | 70 TrialsSeed seed; |
| 78 Study* study = seed.add_study(); | 71 Study* study = seed.add_study(); |
| 79 study->set_name("test"); | 72 study->set_name("test"); |
| 80 study->set_default_experiment_name("abc"); | 73 study->set_default_experiment_name("abc"); |
| 81 Study_Experiment* experiment = study->add_experiment(); | 74 Study_Experiment* experiment = study->add_experiment(); |
| 82 experiment->set_name("abc"); | 75 experiment->set_name("abc"); |
| 83 experiment->set_probability_weight(100); | 76 experiment->set_probability_weight(100); |
| 84 return seed; | 77 return seed; |
| 85 } | 78 } |
| 86 | 79 |
| 87 } // namespace | 80 } // namespace |
| 88 | 81 |
| 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) { | 82 TEST(VariationsServiceTest, CheckStudyChannel) { |
| 120 const chrome::VersionInfo::Channel channels[] = { | 83 const chrome::VersionInfo::Channel channels[] = { |
| 121 chrome::VersionInfo::CHANNEL_CANARY, | 84 chrome::VersionInfo::CHANNEL_CANARY, |
| 122 chrome::VersionInfo::CHANNEL_DEV, | 85 chrome::VersionInfo::CHANNEL_DEV, |
| 123 chrome::VersionInfo::CHANNEL_BETA, | 86 chrome::VersionInfo::CHANNEL_BETA, |
| 124 chrome::VersionInfo::CHANNEL_STABLE, | 87 chrome::VersionInfo::CHANNEL_STABLE, |
| 125 }; | 88 }; |
| 126 const Study_Channel study_channels[] = { | 89 const Study_Channel study_channels[] = { |
| 127 Study_Channel_CANARY, | 90 Study_Channel_CANARY, |
| 128 Study_Channel_DEV, | 91 Study_Channel_DEV, |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 | 357 |
| 395 // Store good seed data to test if loading from prefs works. | 358 // Store good seed data to test if loading from prefs works. |
| 396 TrialsSeed seed = CreateTestSeed(); | 359 TrialsSeed seed = CreateTestSeed(); |
| 397 | 360 |
| 398 std::string serialized_seed; | 361 std::string serialized_seed; |
| 399 seed.SerializeToString(&serialized_seed); | 362 seed.SerializeToString(&serialized_seed); |
| 400 std::string base64_serialized_seed; | 363 std::string base64_serialized_seed; |
| 401 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); | 364 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); |
| 402 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); | 365 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); |
| 403 | 366 |
| 404 VariationsService variations_service; | 367 TestVariationsService variations_service(new TestRequestAllowedNotifier); |
| 405 TrialsSeed loaded_seed; | 368 TrialsSeed loaded_seed; |
| 406 EXPECT_TRUE( | 369 EXPECT_TRUE( |
| 407 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); | 370 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); |
| 408 | 371 |
| 409 std::string serialized_loaded_seed; | 372 std::string serialized_loaded_seed; |
| 410 loaded_seed.SerializeToString(&serialized_loaded_seed); | 373 loaded_seed.SerializeToString(&serialized_loaded_seed); |
| 411 // Check that the loaded data is the same as the original. | 374 // Check that the loaded data is the same as the original. |
| 412 EXPECT_EQ(serialized_seed, serialized_loaded_seed); | 375 EXPECT_EQ(serialized_seed, serialized_loaded_seed); |
| 413 // Make sure the pref hasn't been changed. | 376 // Make sure the pref hasn't been changed. |
| 414 EXPECT_FALSE( | 377 EXPECT_FALSE( |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 428 } | 391 } |
| 429 | 392 |
| 430 TEST(VariationsServiceTest, StoreSeed) { | 393 TEST(VariationsServiceTest, StoreSeed) { |
| 431 TestingPrefService pref_service; | 394 TestingPrefService pref_service; |
| 432 | 395 |
| 433 VariationsService::RegisterPrefs(&pref_service); | 396 VariationsService::RegisterPrefs(&pref_service); |
| 434 const base::Time now = base::Time::Now(); | 397 const base::Time now = base::Time::Now(); |
| 435 | 398 |
| 436 TrialsSeed seed = CreateTestSeed(); | 399 TrialsSeed seed = CreateTestSeed(); |
| 437 | 400 |
| 438 VariationsService variations_service; | 401 TestVariationsService variations_service(new TestRequestAllowedNotifier); |
| 439 std::string serialized_seed; | 402 std::string serialized_seed; |
| 440 seed.SerializeToString(&serialized_seed); | 403 seed.SerializeToString(&serialized_seed); |
| 441 EXPECT_TRUE( | 404 EXPECT_TRUE( |
| 442 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); | 405 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); |
| 443 // Make sure the pref was actually set. | 406 // Make sure the pref was actually set. |
| 444 EXPECT_FALSE( | 407 EXPECT_FALSE( |
| 445 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 408 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 446 | 409 |
| 447 std::string loaded_serialized_seed = | 410 std::string loaded_serialized_seed = |
| 448 pref_service.GetString(prefs::kVariationsSeed); | 411 pref_service.GetString(prefs::kVariationsSeed); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 &total_probability); | 490 &total_probability); |
| 528 ASSERT_TRUE(valid); | 491 ASSERT_TRUE(valid); |
| 529 Study_Experiment* repeated_group = study.add_experiment(); | 492 Study_Experiment* repeated_group = study.add_experiment(); |
| 530 repeated_group->set_name("abc"); | 493 repeated_group->set_name("abc"); |
| 531 repeated_group->set_probability_weight(1); | 494 repeated_group->set_probability_weight(1); |
| 532 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 495 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
| 533 &total_probability); | 496 &total_probability); |
| 534 EXPECT_FALSE(valid); | 497 EXPECT_FALSE(valid); |
| 535 } | 498 } |
| 536 | 499 |
| 537 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOffline) { | 500 TEST(VariationsServiceTest, ResourceRequestAllowedNotifierTest) { |
| 538 SetWasOfflineDuringLastRequestAttempt(true); | 501 MessageLoopForUI message_loop; |
| 539 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | 502 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 540 EXPECT_FALSE(fetch_attempted()); | 503 &message_loop); |
| 541 } | |
| 542 | 504 |
| 543 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOnlineToOnline) { | 505 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 544 SetWasOfflineDuringLastRequestAttempt(false); | 506 // manipulate it for this test. |
| 545 SimulateNetworkConnectionChange( | 507 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; |
| 546 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 508 TestVariationsService test_service(test_notifier); |
| 547 EXPECT_FALSE(fetch_attempted()); | 509 test_notifier->SetRequestsAllowed(false); |
| 548 } | 510 test_service.StartRepeatedVariationsSeedFetch(); |
| 549 | 511 EXPECT_FALSE(test_service.fetch_attempted()); |
| 550 TEST_F(VariationsServiceNetworkTest, FetchOnReconnect) { | 512 test_notifier->NotifyObservers(); |
| 551 SetWasOfflineDuringLastRequestAttempt(true); | 513 EXPECT_TRUE(test_service.fetch_attempted()); |
| 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 } | 514 } |
| 585 | 515 |
| 586 } // namespace chrome_variations | 516 } // namespace chrome_variations |
| OLD | NEW |