[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / invalidation / ticl_profile_settings_provider_unittest.cc
blob179760b8c50c62c5d4702df89f4c1e460329fc13
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/invalidation/ticl_profile_settings_provider.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/invalidation/ticl_invalidation_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/services/gcm/gcm_profile_service.h"
12 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/invalidation/ticl_settings_provider.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "google_apis/gaia/fake_identity_provider.h"
18 #include "google_apis/gaia/fake_oauth2_token_service.h"
19 #include "google_apis/gaia/identity_provider.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "sync/notifier/fake_invalidation_state_tracker.h"
22 #include "sync/notifier/invalidation_state_tracker.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace invalidation {
27 class TiclProfileSettingsProviderTest : public testing::Test {
28 protected:
29 TiclProfileSettingsProviderTest();
30 virtual ~TiclProfileSettingsProviderTest();
32 // testing::Test:
33 virtual void SetUp() OVERRIDE;
34 virtual void TearDown() OVERRIDE;
36 TiclInvalidationService::InvalidationNetworkChannel GetNetworkChannel();
38 content::TestBrowserThreadBundle thread_bundle_;
39 TestingProfile profile_;
40 FakeOAuth2TokenService token_service_;
42 scoped_ptr<TiclInvalidationService> invalidation_service_;
44 private:
45 DISALLOW_COPY_AND_ASSIGN(TiclProfileSettingsProviderTest);
48 TiclProfileSettingsProviderTest::TiclProfileSettingsProviderTest() {
51 TiclProfileSettingsProviderTest::~TiclProfileSettingsProviderTest() {
54 void TiclProfileSettingsProviderTest::SetUp() {
55 invalidation_service_.reset(new TiclInvalidationService(
56 scoped_ptr<IdentityProvider>(new FakeIdentityProvider(&token_service_)),
57 scoped_ptr<TiclSettingsProvider>(
58 new TiclProfileSettingsProvider(&profile_)),
59 gcm::GCMProfileServiceFactory::GetForProfile(&profile_),
60 profile_.GetRequestContext()));
61 invalidation_service_->Init(scoped_ptr<syncer::InvalidationStateTracker>(
62 new syncer::FakeInvalidationStateTracker));
65 void TiclProfileSettingsProviderTest::TearDown() {
66 invalidation_service_->Shutdown();
69 TiclInvalidationService::InvalidationNetworkChannel
70 TiclProfileSettingsProviderTest::GetNetworkChannel() {
71 return invalidation_service_->network_channel_type_;
74 TEST_F(TiclProfileSettingsProviderTest, ChannelSelectionTest) {
75 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
76 PrefService* prefs = profile_.GetPrefs();
78 // If stars align, use GCM channel.
79 prefs->SetBoolean(prefs::kGCMChannelEnabled, true);
80 prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, true);
81 EXPECT_EQ(TiclInvalidationService::GCM_NETWORK_CHANNEL, GetNetworkChannel());
83 // If invalidation channel setting is not set or says false, fall back to push
84 // channel.
85 prefs->SetBoolean(prefs::kGCMChannelEnabled, true);
87 prefs->ClearPref(prefs::kInvalidationServiceUseGCMChannel);
88 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
90 prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, false);
91 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
93 // If invalidation channel setting says use GCM but GCM is not ALWAYS_ENABLED,
94 // fall back to push channel.
95 prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, false);
97 prefs->ClearPref(prefs::kGCMChannelEnabled);
98 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
100 prefs->SetBoolean(prefs::kGCMChannelEnabled, false);
101 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
103 // If invalidation channel setting is enabled first and the GCM setting is
104 // enabled after that, switch to GCM channel.
105 prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, true);
106 prefs->SetBoolean(prefs::kGCMChannelEnabled, true);
107 EXPECT_EQ(TiclInvalidationService::GCM_NETWORK_CHANNEL, GetNetworkChannel());
110 } // namespace invalidation