| 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 FakeTiclSettingsProvider : public TiclSettingsProvider { |
| 27 public: |
| 28 FakeTiclSettingsProvider(); |
| 29 virtual ~FakeTiclSettingsProvider(); |
| 30 |
| 31 // TiclSettingsProvider: |
| 32 virtual bool UseGCMChannel() const OVERRIDE; |
| 33 |
| 34 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(FakeTiclSettingsProvider); |
| 36 }; |
| 37 |
| 38 class FakeGCMService : public gcm::GCMService { |
| 39 public: |
| 40 explicit FakeGCMService(OAuth2TokenService* token_service); |
| 41 virtual ~FakeGCMService(); |
| 42 |
| 43 protected: |
| 44 // gcm::GCMService: |
| 45 virtual bool ShouldStartAutomatically() const OVERRIDE; |
| 46 virtual base::FilePath GetStorePath() const OVERRIDE; |
| 47 virtual scoped_refptr<net::URLRequestContextGetter> |
| 48 GetURLRequestContextGetter() const OVERRIDE; |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(FakeGCMService); |
| 52 }; |
| 53 |
| 54 FakeTiclSettingsProvider::FakeTiclSettingsProvider() { |
| 55 } |
| 56 |
| 57 FakeTiclSettingsProvider::~FakeTiclSettingsProvider() { |
| 58 } |
| 59 |
| 60 bool FakeTiclSettingsProvider::UseGCMChannel() const { |
| 61 return false; |
| 62 } |
| 63 |
| 64 FakeGCMService::FakeGCMService(OAuth2TokenService* token_service) |
| 65 : GCMService(scoped_ptr<IdentityProvider>( |
| 66 new FakeIdentityProvider(token_service))) { |
| 67 } |
| 68 |
| 69 FakeGCMService::~FakeGCMService() { |
| 70 } |
| 71 |
| 72 bool FakeGCMService::ShouldStartAutomatically() const { |
| 73 return false; |
| 74 } |
| 75 |
| 76 base::FilePath FakeGCMService::GetStorePath() const { |
| 77 return base::FilePath(); |
| 78 } |
| 79 |
| 80 scoped_refptr<net::URLRequestContextGetter> |
| 81 FakeGCMService::GetURLRequestContextGetter() const { |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
| 31 class TiclInvalidationServiceTestDelegate { | 87 class TiclInvalidationServiceTestDelegate { |
| 32 public: | 88 public: |
| 33 TiclInvalidationServiceTestDelegate() { } | 89 TiclInvalidationServiceTestDelegate() {} |
| 34 | 90 |
| 35 ~TiclInvalidationServiceTestDelegate() { | 91 ~TiclInvalidationServiceTestDelegate() { |
| 36 DestroyInvalidationService(); | 92 DestroyInvalidationService(); |
| 37 } | 93 } |
| 38 | 94 |
| 39 void CreateInvalidationService() { | 95 void CreateInvalidationService() { |
| 40 CreateUninitializedInvalidationService(); | 96 CreateUninitializedInvalidationService(); |
| 41 InitializeInvalidationService(); | 97 InitializeInvalidationService(); |
| 42 } | 98 } |
| 43 | 99 |
| 44 void CreateUninitializedInvalidationService() { | 100 void CreateUninitializedInvalidationService() { |
| 45 profile_.reset(new TestingProfile()); | 101 gcm_service_.reset(new FakeGCMService(&token_service_)); |
| 46 token_service_.reset(new FakeProfileOAuth2TokenService); | |
| 47 invalidation_service_.reset(new TiclInvalidationService( | 102 invalidation_service_.reset(new TiclInvalidationService( |
| 48 scoped_ptr<IdentityProvider>(new ProfileIdentityProvider( | 103 scoped_ptr<IdentityProvider>(new FakeIdentityProvider(&token_service_)), |
| 49 SigninManagerFactory::GetForProfile(profile_.get()), | 104 scoped_ptr<TiclSettingsProvider>(new FakeTiclSettingsProvider), |
| 50 token_service_.get(), | 105 gcm_service_.get(), |
| 51 NULL)), | 106 NULL)); |
| 52 gcm::GCMProfileServiceFactory::GetForProfile(profile_.get()), | |
| 53 profile_->GetRequestContext(), | |
| 54 profile_.get())); | |
| 55 } | 107 } |
| 56 | 108 |
| 57 void InitializeInvalidationService() { | 109 void InitializeInvalidationService() { |
| 58 fake_invalidator_ = new syncer::FakeInvalidator(); | 110 fake_invalidator_ = new syncer::FakeInvalidator(); |
| 59 invalidation_service_->InitForTest( | 111 invalidation_service_->InitForTest( |
| 60 scoped_ptr<syncer::InvalidationStateTracker>( | 112 scoped_ptr<syncer::InvalidationStateTracker>( |
| 61 new InvalidatorStorage(profile_->GetPrefs())), | 113 new syncer::FakeInvalidationStateTracker), |
| 62 fake_invalidator_); | 114 fake_invalidator_); |
| 63 } | 115 } |
| 64 | 116 |
| 65 InvalidationService* GetInvalidationService() { | 117 InvalidationService* GetInvalidationService() { |
| 66 return invalidation_service_.get(); | 118 return invalidation_service_.get(); |
| 67 } | 119 } |
| 68 | 120 |
| 69 void DestroyInvalidationService() { | 121 void DestroyInvalidationService() { |
| 70 invalidation_service_->Shutdown(); | 122 invalidation_service_->Shutdown(); |
| 71 } | 123 } |
| 72 | 124 |
| 73 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { | 125 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { |
| 74 fake_invalidator_->EmitOnInvalidatorStateChange(state); | 126 fake_invalidator_->EmitOnInvalidatorStateChange(state); |
| 75 } | 127 } |
| 76 | 128 |
| 77 void TriggerOnIncomingInvalidation( | 129 void TriggerOnIncomingInvalidation( |
| 78 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 130 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 79 fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map); | 131 fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map); |
| 80 } | 132 } |
| 81 | 133 |
| 82 syncer::FakeInvalidator* fake_invalidator_; // owned by the service. | 134 FakeOAuth2TokenService token_service_; |
| 83 scoped_ptr<TestingProfile> profile_; | 135 scoped_ptr<gcm::GCMService> gcm_service_; |
| 84 scoped_ptr<FakeProfileOAuth2TokenService> token_service_; | 136 syncer::FakeInvalidator* fake_invalidator_; // Owned by the service. |
| 137 |
| 85 scoped_ptr<TiclInvalidationService> invalidation_service_; | 138 scoped_ptr<TiclInvalidationService> invalidation_service_; |
| 86 }; | 139 }; |
| 87 | 140 |
| 88 INSTANTIATE_TYPED_TEST_CASE_P( | 141 INSTANTIATE_TYPED_TEST_CASE_P( |
| 89 TiclInvalidationServiceTest, InvalidationServiceTest, | 142 TiclInvalidationServiceTest, InvalidationServiceTest, |
| 90 TiclInvalidationServiceTestDelegate); | 143 TiclInvalidationServiceTestDelegate); |
| 91 | 144 |
| 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 { | 145 namespace internal { |
| 173 | 146 |
| 174 class FakeCallbackContainer { | 147 class FakeCallbackContainer { |
| 175 public: | 148 public: |
| 176 FakeCallbackContainer() | 149 FakeCallbackContainer() : called_(false), |
| 177 : called_(false), | 150 weak_ptr_factory_(this) {} |
| 178 weak_ptr_factory_(this) { } | |
| 179 | 151 |
| 180 void FakeCallback(const base::DictionaryValue& value) { | 152 void FakeCallback(const base::DictionaryValue& value) { |
| 181 called_ = true; | 153 called_ = true; |
| 182 } | 154 } |
| 183 | 155 |
| 184 bool called_; | 156 bool called_; |
| 185 base::WeakPtrFactory<FakeCallbackContainer> weak_ptr_factory_; | 157 base::WeakPtrFactory<FakeCallbackContainer> weak_ptr_factory_; |
| 186 }; | 158 }; |
| 159 |
| 187 } // namespace internal | 160 } // namespace internal |
| 188 | 161 |
| 189 // Test that requesting for detailed status doesn't crash even if the | 162 // Test that requesting for detailed status doesn't crash even if the |
| 190 // underlying invalidator is not initialized. | 163 // underlying invalidator is not initialized. |
| 191 TEST(TiclInvalidationServiceLoggingTest, DetailedStatusCallbacksWork) { | 164 TEST(TiclInvalidationServiceLoggingTest, DetailedStatusCallbacksWork) { |
| 192 content::TestBrowserThreadBundle thread_bundle; | |
| 193 | |
| 194 scoped_ptr<TiclInvalidationServiceTestDelegate> delegate ( | 165 scoped_ptr<TiclInvalidationServiceTestDelegate> delegate ( |
| 195 new TiclInvalidationServiceTestDelegate()); | 166 new TiclInvalidationServiceTestDelegate()); |
| 196 | 167 |
| 197 delegate->CreateUninitializedInvalidationService(); | 168 delegate->CreateUninitializedInvalidationService(); |
| 198 invalidation::InvalidationService* const invalidator = | 169 invalidation::InvalidationService* const invalidator = |
| 199 delegate->GetInvalidationService(); | 170 delegate->GetInvalidationService(); |
| 200 | 171 |
| 201 internal::FakeCallbackContainer fake_container; | 172 internal::FakeCallbackContainer fake_container; |
| 202 invalidator->RequestDetailedStatus( | 173 invalidator->RequestDetailedStatus( |
| 203 base::Bind(&internal::FakeCallbackContainer::FakeCallback, | 174 base::Bind(&internal::FakeCallbackContainer::FakeCallback, |
| 204 fake_container.weak_ptr_factory_.GetWeakPtr())); | 175 fake_container.weak_ptr_factory_.GetWeakPtr())); |
| 205 EXPECT_FALSE(fake_container.called_); | 176 EXPECT_FALSE(fake_container.called_); |
| 206 | 177 |
| 207 delegate->InitializeInvalidationService(); | 178 delegate->InitializeInvalidationService(); |
| 208 | 179 |
| 209 invalidator->RequestDetailedStatus( | 180 invalidator->RequestDetailedStatus( |
| 210 base::Bind(&internal::FakeCallbackContainer::FakeCallback, | 181 base::Bind(&internal::FakeCallbackContainer::FakeCallback, |
| 211 fake_container.weak_ptr_factory_.GetWeakPtr())); | 182 fake_container.weak_ptr_factory_.GetWeakPtr())); |
| 212 EXPECT_TRUE(fake_container.called_); | 183 EXPECT_TRUE(fake_container.called_); |
| 213 } | 184 } |
| 185 |
| 214 } // namespace invalidation | 186 } // namespace invalidation |
| OLD | NEW |