Chromium Code Reviews| Index: chrome/browser/metrics/variations/variations_service_unittest.cc |
| diff --git a/chrome/browser/metrics/variations/variations_service_unittest.cc b/chrome/browser/metrics/variations/variations_service_unittest.cc |
| index 0de0800342379ff326e0489f17d10e0fd2ac7ad0..eb136308b2d9ce4d7e04a03dc4b10b33d1af0912 100644 |
| --- a/chrome/browser/metrics/variations/variations_service_unittest.cc |
| +++ b/chrome/browser/metrics/variations/variations_service_unittest.cc |
| @@ -5,9 +5,11 @@ |
| #include "base/base64.h" |
| #include "base/string_split.h" |
| #include "chrome/browser/metrics/proto/study.pb.h" |
| +#include "chrome/browser/metrics/variations/resource_request_allowed_notifier_test_util.h" |
| #include "chrome/browser/metrics/variations/variations_service.h" |
| #include "chrome/common/chrome_version_info.h" |
| #include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/testing_browser_process.h" |
| #include "chrome/test/base/testing_pref_service.h" |
| #include "content/public/test/test_browser_thread.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -19,50 +21,50 @@ namespace { |
| // A test class used to validate expected functionality in VariationsService. |
| class TestVariationsService : public VariationsService { |
| public: |
| - TestVariationsService() : VariationsService(), |
| - fetch_attempted_(false) { |
| - } |
| - virtual ~TestVariationsService() {} |
| + explicit TestVariationsService(); |
| + virtual ~TestVariationsService(); |
| bool fetch_attempted() const { return fetch_attempted_; } |
| - void SetFetchAttempted(bool attempted) { fetch_attempted_ = attempted; } |
| + |
| + void SetRequestsAllowed(bool allowed); |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
It seems both SetRequestsAllowed() and NotifyObser
SteveT
2012/09/21 15:16:17
Thanks for that suggestion - made things a lot nic
|
| + |
| + void NotifyObservers(); |
| protected: |
| - virtual void FetchVariationsSeed() OVERRIDE { |
| + virtual void DoActualFetch() OVERRIDE { |
| fetch_attempted_ = true; |
| } |
| private: |
| bool fetch_attempted_; |
| + // Weak pointer to the base class' copy of the notifier. |
| + TestRequestAllowedNotifier* test_notifier_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(TestVariationsService); |
| }; |
| -// Override NetworkChangeNotifier to simulate connection type changes for tests. |
| -class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| - public: |
| - TestNetworkChangeNotifier() |
| - : net::NetworkChangeNotifier(), |
| - connection_type_to_return_( |
| - net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { |
| - } |
| - |
| - void SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::ConnectionType type) { |
| - connection_type_to_return_ = type; |
| - net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); |
| - MessageLoop::current()->RunAllPending(); |
| - } |
| +TestVariationsService::TestVariationsService() |
| + : VariationsService(new TestRequestAllowedNotifier), |
| + fetch_attempted_(false), |
| + // This is known to return a TestRequestAllowedNotifier* since it was |
| + // created just above. |
| + test_notifier_(static_cast<TestRequestAllowedNotifier*>( |
| + GetResourceRequestAllowedNotifierForTesting())) { |
| + // Set this so StartRepeatedVariationsSeedFetch can be called in tests. |
| + SetCreateTrialsFromSeedCalledForTesting(true); |
| +} |
| - private: |
| - virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { |
| - return connection_type_to_return_; |
| - } |
| +TestVariationsService::~TestVariationsService() { |
| +} |
| - net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; |
| +void TestVariationsService::SetRequestsAllowed(bool allowed) { |
| + test_notifier_->SetRequestsAllowed(allowed); |
| +} |
| - DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| -}; |
| +void TestVariationsService::NotifyObservers() { |
| + test_notifier_->NotifyObservers(); |
| +} |
| // Converts |time| to Study proto format. |
| int64 TimeToProtoTime(const base::Time& time) { |
| @@ -86,36 +88,6 @@ TrialsSeed CreateTestSeed() { |
| } // namespace |
| -// A test fixture class for VariationsService tests that require network state |
| -// simulations. |
| -class VariationsServiceNetworkTest : public testing::Test { |
| - public: |
| - VariationsServiceNetworkTest() |
| - : ui_thread(content::BrowserThread::UI, &message_loop) { } |
| - ~VariationsServiceNetworkTest() { } |
| - |
| - void SetWasOfflineDuringLastRequestAttempt(bool offline) { |
| - test_service.SetWasOfflineDuringLastRequestAttemptForTesting(offline); |
| - } |
| - |
| - void SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::ConnectionType type) { |
| - notifier.SimulateNetworkConnectionChange(type); |
| - } |
| - |
| - bool fetch_attempted() const { |
| - return test_service.fetch_attempted(); |
| - } |
| - |
| - private: |
| - MessageLoopForUI message_loop; |
| - content::TestBrowserThread ui_thread; |
| - TestNetworkChangeNotifier notifier; |
| - TestVariationsService test_service; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(VariationsServiceNetworkTest); |
| -}; |
| - |
| TEST(VariationsServiceTest, CheckStudyChannel) { |
| const chrome::VersionInfo::Channel channels[] = { |
| chrome::VersionInfo::CHANNEL_CANARY, |
| @@ -401,7 +373,7 @@ TEST(VariationsServiceTest, LoadSeed) { |
| ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); |
| pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); |
| - VariationsService variations_service; |
| + TestVariationsService variations_service; |
| TrialsSeed loaded_seed; |
| EXPECT_TRUE( |
| variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); |
| @@ -435,7 +407,7 @@ TEST(VariationsServiceTest, StoreSeed) { |
| TrialsSeed seed = CreateTestSeed(); |
| - VariationsService variations_service; |
| + TestVariationsService variations_service; |
| std::string serialized_seed; |
| seed.SerializeToString(&serialized_seed); |
| EXPECT_TRUE( |
| @@ -534,53 +506,19 @@ TEST(VariationsServiceTest, ValidateStudy) { |
| EXPECT_FALSE(valid); |
| } |
| -TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOffline) { |
| - SetWasOfflineDuringLastRequestAttempt(true); |
| - SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); |
| - EXPECT_FALSE(fetch_attempted()); |
| -} |
| - |
| -TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOnlineToOnline) { |
| - SetWasOfflineDuringLastRequestAttempt(false); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| - EXPECT_FALSE(fetch_attempted()); |
| -} |
| - |
| -TEST_F(VariationsServiceNetworkTest, FetchOnReconnect) { |
| - SetWasOfflineDuringLastRequestAttempt(true); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| - EXPECT_TRUE(fetch_attempted()); |
| -} |
| - |
| -TEST_F(VariationsServiceNetworkTest, NoFetchOnWardriving) { |
| - SetWasOfflineDuringLastRequestAttempt(false); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_WIFI); |
| - EXPECT_FALSE(fetch_attempted()); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_3G); |
| - EXPECT_FALSE(fetch_attempted()); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_4G); |
| - EXPECT_FALSE(fetch_attempted()); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_WIFI); |
| - EXPECT_FALSE(fetch_attempted()); |
| -} |
| +TEST(VariationsServiceTest, ResourceRequestAllowedNotifierTest) { |
| + MessageLoopForUI message_loop; |
| + content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| + &message_loop); |
| -TEST_F(VariationsServiceNetworkTest, NoFetchOnFlakyConnection) { |
| - SetWasOfflineDuringLastRequestAttempt(false); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_WIFI); |
| - EXPECT_FALSE(fetch_attempted()); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_NONE); |
| - EXPECT_FALSE(fetch_attempted()); |
| - SimulateNetworkConnectionChange( |
| - net::NetworkChangeNotifier::CONNECTION_WIFI); |
| - EXPECT_FALSE(fetch_attempted()); |
| + // Pass ownership to TestVariationsService, but keep a weak pointer to |
| + // manipulate it for this test. |
| + TestVariationsService test_service; |
| + test_service.SetRequestsAllowed(false); |
| + test_service.StartRepeatedVariationsSeedFetch(); |
| + EXPECT_FALSE(test_service.fetch_attempted()); |
| + test_service.NotifyObservers(); |
| + EXPECT_TRUE(test_service.fetch_attempted()); |
| } |
| } // namespace chrome_variations |