Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/invalidation/ticl_invalidation_service.h" | 5 #include "chrome/browser/invalidation/ticl_invalidation_service.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/invalidation/invalidation_service_factory.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" | |
| 9 #include "chrome/browser/invalidation/invalidation_service_test_template.h" | 11 #include "chrome/browser/invalidation/invalidation_service_test_template.h" |
| 10 #include "chrome/browser/invalidation/invalidator_storage.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/browser/services/gcm/gcm_service.h" | 12 #include "chrome/browser/services/gcm/gcm_service.h" |
| 14 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 13 #include "google_apis/gaia/fake_identity_provider.h" |
| 15 #include "chrome/browser/signin/fake_signin_manager.h" | 14 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 16 #include "chrome/browser/signin/profile_identity_provider.h" | |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 18 #include "chrome/common/pref_names.h" | |
| 19 #include "chrome/test/base/testing_profile.h" | |
| 20 #include "components/signin/core/browser/signin_manager.h" | |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | |
| 22 #include "net/url_request/url_request_context_getter.h" | 15 #include "net/url_request/url_request_context_getter.h" |
| 23 #include "sync/notifier/fake_invalidation_handler.h" | 16 #include "sync/notifier/fake_invalidation_state_tracker.h" |
| 24 #include "sync/notifier/fake_invalidator.h" | 17 #include "sync/notifier/fake_invalidator.h" |
| 25 #include "sync/notifier/invalidation_state_tracker.h" | 18 #include "sync/notifier/invalidation_state_tracker.h" |
| 26 #include "sync/notifier/invalidation_util.h" | 19 #include "sync/notifier/invalidator.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 21 |
| 29 namespace invalidation { | 22 namespace invalidation { |
| 30 | 23 |
| 24 namespace { | |
| 25 | |
| 26 class FakeTiclInvalidationServiceSettingsProvider | |
| 27 : public TiclInvalidationServiceSettingsProvider { | |
| 28 public: | |
| 29 FakeTiclInvalidationServiceSettingsProvider(); | |
| 30 virtual ~FakeTiclInvalidationServiceSettingsProvider(); | |
| 31 | |
| 32 // TiclInvalidationServiceSettingsProvider: | |
| 33 bool UseGCMChannel() const OVERRIDE; | |
| 34 | |
| 35 private: | |
| 36 DISALLOW_COPY_AND_ASSIGN(FakeTiclInvalidationServiceSettingsProvider); | |
| 37 }; | |
| 38 | |
| 39 class FakeGCMService : public gcm::GCMService { | |
| 40 public: | |
| 41 explicit FakeGCMService(OAuth2TokenService* token_service); | |
| 42 virtual ~FakeGCMService(); | |
| 43 | |
| 44 protected: | |
| 45 // gcm::GCMService: | |
| 46 virtual bool ShouldStartAutomatically() const OVERRIDE; | |
| 47 virtual base::FilePath GetStorePath() const OVERRIDE; | |
| 48 virtual scoped_refptr<net::URLRequestContextGetter> | |
| 49 GetURLRequestContextGetter() const OVERRIDE; | |
| 50 | |
| 51 private: | |
| 52 DISALLOW_COPY_AND_ASSIGN(FakeGCMService); | |
| 53 }; | |
| 54 | |
| 55 FakeTiclInvalidationServiceSettingsProvider:: | |
|
dcheng
2014/04/24 18:26:54
These class names are really long!
Technically, Ti
bartfab (slow)
2014/04/24 19:14:48
Done.
| |
| 56 FakeTiclInvalidationServiceSettingsProvider() { | |
| 57 } | |
| 58 | |
| 59 FakeTiclInvalidationServiceSettingsProvider:: | |
| 60 ~FakeTiclInvalidationServiceSettingsProvider() { | |
| 61 } | |
| 62 | |
| 63 bool FakeTiclInvalidationServiceSettingsProvider::UseGCMChannel() const { | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 FakeGCMService::FakeGCMService(OAuth2TokenService* token_service) | |
| 68 : GCMService(scoped_ptr<IdentityProvider>( | |
| 69 new FakeIdentityProvider(token_service))) { | |
| 70 } | |
| 71 | |
| 72 FakeGCMService::~FakeGCMService() { | |
| 73 } | |
| 74 | |
| 75 bool FakeGCMService::ShouldStartAutomatically() const { | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 base::FilePath FakeGCMService::GetStorePath() const { | |
| 80 return base::FilePath(); | |
| 81 } | |
| 82 | |
| 83 scoped_refptr<net::URLRequestContextGetter> | |
| 84 FakeGCMService::GetURLRequestContextGetter() const { | |
| 85 return NULL; | |
| 86 } | |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 31 class TiclInvalidationServiceTestDelegate { | 90 class TiclInvalidationServiceTestDelegate { |
| 32 public: | 91 public: |
| 33 TiclInvalidationServiceTestDelegate() { } | 92 TiclInvalidationServiceTestDelegate() {} |
| 34 | 93 |
| 35 ~TiclInvalidationServiceTestDelegate() { | 94 ~TiclInvalidationServiceTestDelegate() { |
| 36 DestroyInvalidationService(); | 95 DestroyInvalidationService(); |
| 37 } | 96 } |
| 38 | 97 |
| 39 void CreateInvalidationService() { | 98 void CreateInvalidationService() { |
| 40 CreateUninitializedInvalidationService(); | 99 CreateUninitializedInvalidationService(); |
| 41 InitializeInvalidationService(); | 100 InitializeInvalidationService(); |
| 42 } | 101 } |
| 43 | 102 |
| 44 void CreateUninitializedInvalidationService() { | 103 void CreateUninitializedInvalidationService() { |
| 45 profile_.reset(new TestingProfile()); | 104 gcm_service_.reset(new FakeGCMService(&token_service_)); |
| 46 token_service_.reset(new FakeProfileOAuth2TokenService); | |
| 47 invalidation_service_.reset(new TiclInvalidationService( | 105 invalidation_service_.reset(new TiclInvalidationService( |
| 48 scoped_ptr<IdentityProvider>(new ProfileIdentityProvider( | 106 scoped_ptr<IdentityProvider>(new FakeIdentityProvider(&token_service_)), |
| 49 SigninManagerFactory::GetForProfile(profile_.get()), | 107 scoped_ptr<TiclInvalidationServiceSettingsProvider>( |
| 50 token_service_.get(), | 108 new FakeTiclInvalidationServiceSettingsProvider), |
| 51 NULL)), | 109 gcm_service_.get(), |
| 52 gcm::GCMProfileServiceFactory::GetForProfile(profile_.get()), | 110 NULL)); |
| 53 profile_->GetRequestContext(), | |
| 54 profile_.get())); | |
| 55 } | 111 } |
| 56 | 112 |
| 57 void InitializeInvalidationService() { | 113 void InitializeInvalidationService() { |
| 58 fake_invalidator_ = new syncer::FakeInvalidator(); | 114 fake_invalidator_ = new syncer::FakeInvalidator(); |
| 59 invalidation_service_->InitForTest( | 115 invalidation_service_->InitForTest( |
| 60 scoped_ptr<syncer::InvalidationStateTracker>( | 116 scoped_ptr<syncer::InvalidationStateTracker>( |
| 61 new InvalidatorStorage(profile_->GetPrefs())), | 117 new syncer::FakeInvalidationStateTracker), |
| 62 fake_invalidator_); | 118 fake_invalidator_); |
| 63 } | 119 } |
| 64 | 120 |
| 65 InvalidationService* GetInvalidationService() { | 121 InvalidationService* GetInvalidationService() { |
| 66 return invalidation_service_.get(); | 122 return invalidation_service_.get(); |
| 67 } | 123 } |
| 68 | 124 |
| 69 void DestroyInvalidationService() { | 125 void DestroyInvalidationService() { |
| 70 invalidation_service_->Shutdown(); | 126 invalidation_service_->Shutdown(); |
| 71 } | 127 } |
| 72 | 128 |
| 73 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { | 129 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { |
| 74 fake_invalidator_->EmitOnInvalidatorStateChange(state); | 130 fake_invalidator_->EmitOnInvalidatorStateChange(state); |
| 75 } | 131 } |
| 76 | 132 |
| 77 void TriggerOnIncomingInvalidation( | 133 void TriggerOnIncomingInvalidation( |
| 78 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 134 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 79 fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map); | 135 fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map); |
| 80 } | 136 } |
| 81 | 137 |
| 82 syncer::FakeInvalidator* fake_invalidator_; // owned by the service. | 138 FakeOAuth2TokenService token_service_; |
| 83 scoped_ptr<TestingProfile> profile_; | 139 scoped_ptr<gcm::GCMService> gcm_service_; |
| 84 scoped_ptr<FakeProfileOAuth2TokenService> token_service_; | 140 syncer::FakeInvalidator* fake_invalidator_; // Owned by the service. |
| 141 | |
| 85 scoped_ptr<TiclInvalidationService> invalidation_service_; | 142 scoped_ptr<TiclInvalidationService> invalidation_service_; |
| 86 }; | 143 }; |
| 87 | 144 |
| 88 INSTANTIATE_TYPED_TEST_CASE_P( | 145 INSTANTIATE_TYPED_TEST_CASE_P( |
| 89 TiclInvalidationServiceTest, InvalidationServiceTest, | 146 TiclInvalidationServiceTest, InvalidationServiceTest, |
| 90 TiclInvalidationServiceTestDelegate); | 147 TiclInvalidationServiceTestDelegate); |
| 91 | 148 |
| 92 class TiclInvalidationServiceChannelTest : public ::testing::Test { | |
| 93 public: | |
| 94 TiclInvalidationServiceChannelTest() {} | |
| 95 virtual ~TiclInvalidationServiceChannelTest() {} | |
| 96 | |
| 97 virtual void SetUp() OVERRIDE { | |
| 98 TestingProfile::Builder builder; | |
| 99 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | |
| 100 FakeSigninManagerBase::Build); | |
| 101 profile_ = builder.Build(); | |
| 102 fake_signin_manager_ = static_cast<SigninManagerBase*>( | |
| 103 SigninManagerFactory::GetForProfile(profile_.get())); | |
| 104 token_service_.reset(new FakeProfileOAuth2TokenService); | |
| 105 | |
| 106 scoped_ptr<IdentityProvider> identity_provider(new ProfileIdentityProvider( | |
| 107 fake_signin_manager_, token_service_.get(), NULL)); | |
| 108 invalidation_service_.reset(new TiclInvalidationService( | |
| 109 identity_provider.Pass(), | |
| 110 gcm::GCMProfileServiceFactory::GetForProfile(profile_.get()), | |
| 111 profile_->GetRequestContext(), | |
| 112 profile_.get())); | |
| 113 invalidation_service_->Init(scoped_ptr<syncer::InvalidationStateTracker>( | |
| 114 new InvalidatorStorage(profile_->GetPrefs()))); | |
| 115 } | |
| 116 | |
| 117 virtual void TearDown() OVERRIDE { | |
| 118 invalidation_service_->Shutdown(); | |
| 119 } | |
| 120 | |
| 121 TiclInvalidationService::InvalidationNetworkChannel GetNetworkChannel() { | |
| 122 return invalidation_service_->network_channel_type_; | |
| 123 } | |
| 124 | |
| 125 protected: | |
| 126 content::TestBrowserThreadBundle thread_bundle_; | |
| 127 scoped_ptr<TestingProfile> profile_; | |
| 128 SigninManagerBase* fake_signin_manager_; | |
| 129 scoped_ptr<FakeProfileOAuth2TokenService> token_service_; | |
| 130 scoped_ptr<TiclInvalidationService> invalidation_service_; | |
| 131 }; | |
| 132 | |
| 133 TEST_F(TiclInvalidationServiceChannelTest, ChannelSelectionTest) { | |
| 134 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel()); | |
| 135 | |
| 136 // If stars allign use GCM channel. | |
| 137 profile_->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); | |
| 138 profile_->GetPrefs()->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, | |
| 139 true); | |
| 140 EXPECT_EQ(TiclInvalidationService::GCM_NETWORK_CHANNEL, GetNetworkChannel()); | |
| 141 | |
| 142 // If Invalidation channel setting is not set or says false fall back to push | |
| 143 // channel. | |
| 144 profile_->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); | |
| 145 | |
| 146 profile_->GetPrefs()->ClearPref(prefs::kInvalidationServiceUseGCMChannel); | |
| 147 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel()); | |
| 148 | |
| 149 profile_->GetPrefs()->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, | |
| 150 false); | |
| 151 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel()); | |
| 152 | |
| 153 // If invalidation channel setting says use GCM but GCM is not ALWAYS_ENABLED | |
| 154 // then fall back to push channel. | |
| 155 profile_->GetPrefs()->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, | |
| 156 false); | |
| 157 | |
| 158 profile_->GetPrefs()->ClearPref(prefs::kGCMChannelEnabled); | |
| 159 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel()); | |
| 160 | |
| 161 profile_->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, false); | |
| 162 EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel()); | |
| 163 | |
| 164 // If first invalidation setting gets enabled and after that gcm setting gets | |
| 165 // enabled then should still switch to GCM channel. | |
| 166 profile_->GetPrefs()->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, | |
| 167 true); | |
| 168 profile_->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); | |
| 169 EXPECT_EQ(TiclInvalidationService::GCM_NETWORK_CHANNEL, GetNetworkChannel()); | |
| 170 } | |
| 171 | |
| 172 namespace internal { | 149 namespace internal { |
| 173 | 150 |
| 174 class FakeCallbackContainer { | 151 class FakeCallbackContainer { |
| 175 public: | 152 public: |
| 176 FakeCallbackContainer() | 153 FakeCallbackContainer() : called_(false), |
| 177 : called_(false), | 154 weak_ptr_factory_(this) {} |
| 178 weak_ptr_factory_(this) { } | |
| 179 | 155 |
| 180 void FakeCallback(const base::DictionaryValue& value) { | 156 void FakeCallback(const base::DictionaryValue& value) { |
| 181 called_ = true; | 157 called_ = true; |
| 182 } | 158 } |
| 183 | 159 |
| 184 bool called_; | 160 bool called_; |
| 185 base::WeakPtrFactory<FakeCallbackContainer> weak_ptr_factory_; | 161 base::WeakPtrFactory<FakeCallbackContainer> weak_ptr_factory_; |
| 186 }; | 162 }; |
| 163 | |
| 187 } // namespace internal | 164 } // namespace internal |
| 188 | 165 |
| 189 // Test that requesting for detailed status doesn't crash even if the | 166 // Test that requesting for detailed status doesn't crash even if the |
| 190 // underlying invalidator is not initialized. | 167 // underlying invalidator is not initialized. |
| 191 TEST(TiclInvalidationServiceLoggingTest, DetailedStatusCallbacksWork) { | 168 TEST(TiclInvalidationServiceLoggingTest, DetailedStatusCallbacksWork) { |
| 192 content::TestBrowserThreadBundle thread_bundle; | |
| 193 | |
| 194 scoped_ptr<TiclInvalidationServiceTestDelegate> delegate ( | 169 scoped_ptr<TiclInvalidationServiceTestDelegate> delegate ( |
| 195 new TiclInvalidationServiceTestDelegate()); | 170 new TiclInvalidationServiceTestDelegate()); |
| 196 | 171 |
| 197 delegate->CreateUninitializedInvalidationService(); | 172 delegate->CreateUninitializedInvalidationService(); |
| 198 invalidation::InvalidationService* const invalidator = | 173 invalidation::InvalidationService* const invalidator = |
| 199 delegate->GetInvalidationService(); | 174 delegate->GetInvalidationService(); |
| 200 | 175 |
| 201 internal::FakeCallbackContainer fake_container; | 176 internal::FakeCallbackContainer fake_container; |
| 202 invalidator->RequestDetailedStatus( | 177 invalidator->RequestDetailedStatus( |
| 203 base::Bind(&internal::FakeCallbackContainer::FakeCallback, | 178 base::Bind(&internal::FakeCallbackContainer::FakeCallback, |
| 204 fake_container.weak_ptr_factory_.GetWeakPtr())); | 179 fake_container.weak_ptr_factory_.GetWeakPtr())); |
| 205 EXPECT_FALSE(fake_container.called_); | 180 EXPECT_FALSE(fake_container.called_); |
| 206 | 181 |
| 207 delegate->InitializeInvalidationService(); | 182 delegate->InitializeInvalidationService(); |
| 208 | 183 |
| 209 invalidator->RequestDetailedStatus( | 184 invalidator->RequestDetailedStatus( |
| 210 base::Bind(&internal::FakeCallbackContainer::FakeCallback, | 185 base::Bind(&internal::FakeCallbackContainer::FakeCallback, |
| 211 fake_container.weak_ptr_factory_.GetWeakPtr())); | 186 fake_container.weak_ptr_factory_.GetWeakPtr())); |
| 212 EXPECT_TRUE(fake_container.called_); | 187 EXPECT_TRUE(fake_container.called_); |
| 213 } | 188 } |
| 189 | |
| 214 } // namespace invalidation | 190 } // namespace invalidation |
| OLD | NEW |