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/variations_service.h" | 8 #include "chrome/browser/metrics/variations/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_browser_process.h" | |
| 11 #include "chrome/test/base/testing_pref_service.h" | 12 #include "chrome/test/base/testing_pref_service.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace chrome_variations { | 16 namespace chrome_variations { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // A test class used to validate expected functionality in VariationsService. | |
| 20 class TestVariationsService : public VariationsService { | |
| 21 public: | |
| 22 TestVariationsService() : VariationsService(), | |
| 23 fetch_attempted_(false) { | |
| 24 } | |
| 25 virtual ~TestVariationsService() {} | |
| 26 | |
| 27 bool fetch_attempted() const { return fetch_attempted_; } | |
| 28 void SetFetchAttempted(bool attempted) { fetch_attempted_ = attempted; } | |
| 29 | |
| 30 protected: | |
| 31 virtual void FetchVariationsSeed() OVERRIDE { | |
| 32 fetch_attempted_ = true; | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 bool fetch_attempted_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); | |
| 39 }; | |
| 40 | |
| 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. | 20 // Converts |time| to Study proto format. |
| 68 int64 TimeToProtoTime(const base::Time& time) { | 21 int64 TimeToProtoTime(const base::Time& time) { |
| 69 return (time - base::Time::UnixEpoch()).InSeconds(); | 22 return (time - base::Time::UnixEpoch()).InSeconds(); |
| 70 } | 23 } |
| 71 | 24 |
| 72 // Populates |seed| with simple test data. The resulting seed will contain one | 25 // Populates |seed| with simple test data. The resulting seed will contain one |
| 73 // study called "test", which contains one experiment called "abc" with | 26 // study called "test", which contains one experiment called "abc" with |
| 74 // probability weight 100. |seed|'s study field will be cleared before adding | 27 // probability weight 100. |seed|'s study field will be cleared before adding |
| 75 // the new study. | 28 // the new study. |
| 76 TrialsSeed CreateTestSeed() { | 29 TrialsSeed CreateTestSeed() { |
| 77 TrialsSeed seed; | 30 TrialsSeed seed; |
| 78 Study* study = seed.add_study(); | 31 Study* study = seed.add_study(); |
| 79 study->set_name("test"); | 32 study->set_name("test"); |
| 80 study->set_default_experiment_name("abc"); | 33 study->set_default_experiment_name("abc"); |
| 81 Study_Experiment* experiment = study->add_experiment(); | 34 Study_Experiment* experiment = study->add_experiment(); |
| 82 experiment->set_name("abc"); | 35 experiment->set_name("abc"); |
| 83 experiment->set_probability_weight(100); | 36 experiment->set_probability_weight(100); |
| 84 return seed; | 37 return seed; |
| 85 } | 38 } |
| 86 | 39 |
| 87 } // namespace | 40 } // namespace |
| 88 | 41 |
| 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) { | 42 TEST(VariationsServiceTest, CheckStudyChannel) { |
| 120 const chrome::VersionInfo::Channel channels[] = { | 43 const chrome::VersionInfo::Channel channels[] = { |
| 121 chrome::VersionInfo::CHANNEL_CANARY, | 44 chrome::VersionInfo::CHANNEL_CANARY, |
| 122 chrome::VersionInfo::CHANNEL_DEV, | 45 chrome::VersionInfo::CHANNEL_DEV, |
| 123 chrome::VersionInfo::CHANNEL_BETA, | 46 chrome::VersionInfo::CHANNEL_BETA, |
| 124 chrome::VersionInfo::CHANNEL_STABLE, | 47 chrome::VersionInfo::CHANNEL_STABLE, |
| 125 }; | 48 }; |
| 126 const Study_Channel study_channels[] = { | 49 const Study_Channel study_channels[] = { |
| 127 Study_Channel_CANARY, | 50 Study_Channel_CANARY, |
| 128 Study_Channel_DEV, | 51 Study_Channel_DEV, |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 526 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 449 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
| 527 &total_probability); | 450 &total_probability); |
| 528 ASSERT_TRUE(valid); | 451 ASSERT_TRUE(valid); |
| 529 Study_Experiment* repeated_group = study.add_experiment(); | 452 Study_Experiment* repeated_group = study.add_experiment(); |
| 530 repeated_group->set_name("abc"); | 453 repeated_group->set_name("abc"); |
| 531 repeated_group->set_probability_weight(1); | 454 repeated_group->set_probability_weight(1); |
| 532 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 455 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
| 533 &total_probability); | 456 &total_probability); |
| 534 EXPECT_FALSE(valid); | 457 EXPECT_FALSE(valid); |
| 535 } | 458 } |
| 536 | 459 |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Can you add a test that mocks the ResourceRequestA
SteveT
2012/09/14 20:25:09
Not done yet...
| |
| 537 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOffline) { | |
| 538 SetWasOfflineDuringLastRequestAttempt(true); | |
| 539 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 540 EXPECT_FALSE(fetch_attempted()); | |
| 541 } | |
| 542 | |
| 543 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOnlineToOnline) { | |
| 544 SetWasOfflineDuringLastRequestAttempt(false); | |
| 545 SimulateNetworkConnectionChange( | |
| 546 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 547 EXPECT_FALSE(fetch_attempted()); | |
| 548 } | |
| 549 | |
| 550 TEST_F(VariationsServiceNetworkTest, FetchOnReconnect) { | |
| 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 } | |
| 585 | |
| 586 } // namespace chrome_variations | 460 } // namespace chrome_variations |
| OLD | NEW |