Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/policy/affiliated_invalidation_service_provide r.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "chrome/browser/chrome_notification_types.h" | |
| 10 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" | |
| 11 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
| 12 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" | |
| 13 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 14 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " | |
| 15 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 16 #include "chrome/browser/invalidation/fake_invalidation_service.h" | |
| 17 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h" | |
| 18 #include "chrome/browser/profiles/profile.h" | |
| 19 #include "chrome/test/base/testing_browser_process.h" | |
| 20 #include "chrome/test/base/testing_profile_manager.h" | |
| 21 #include "chromeos/cryptohome/system_salt_getter.h" | |
| 22 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 23 #include "components/invalidation/invalidation_service.h" | |
| 24 #include "components/invalidation/invalidator_state.h" | |
| 25 #include "components/invalidation/profile_invalidation_provider.h" | |
| 26 #include "components/invalidation/ticl_invalidation_service.h" | |
| 27 #include "components/keyed_service/core/keyed_service.h" | |
| 28 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | |
| 29 #include "content/public/browser/browser_context.h" | |
| 30 #include "content/public/browser/notification_details.h" | |
| 31 #include "content/public/browser/notification_service.h" | |
| 32 #include "content/public/test/test_browser_thread_bundle.h" | |
| 33 #include "testing/gmock/include/gmock/gmock.h" | |
| 34 #include "testing/gtest/include/gtest/gtest.h" | |
| 35 | |
| 36 using testing::_; | |
| 37 using testing::Mock; | |
| 38 using testing::Sequence; | |
| 39 | |
| 40 namespace policy { | |
| 41 | |
| 42 namespace { | |
| 43 | |
| 44 const char kAffiliatedUserID1[] = "test_1@example.com"; | |
| 45 const char kAffiliatedUserID2[] = "test_2@example.com"; | |
| 46 const char kUnaffiliatedUserID[] = "test@other_domain.test"; | |
| 47 | |
| 48 KeyedService* BuildProfileInvalidationProvider( | |
| 49 content::BrowserContext* context) { | |
| 50 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service( | |
| 51 new invalidation::FakeInvalidationService); | |
| 52 invalidation_service->SetInvalidatorState( | |
| 53 syncer::TRANSIENT_INVALIDATION_ERROR); | |
| 54 return new invalidation::ProfileInvalidationProvider( | |
| 55 invalidation_service.Pass()); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 class MockConsumer : public AffiliatedInvalidationServiceProvider::Consumer { | |
| 61 public: | |
| 62 MockConsumer(); | |
| 63 ~MockConsumer() override; | |
| 64 | |
| 65 MOCK_METHOD1(OnInvalidationServiceSet, | |
| 66 void(invalidation::InvalidationService*)); | |
| 67 MOCK_METHOD0(OnInvalidationServiceReset, void()); | |
| 68 | |
| 69 private: | |
| 70 DISALLOW_COPY_AND_ASSIGN(MockConsumer); | |
| 71 }; | |
| 72 | |
| 73 class AffiliatedInvalidationServiceProviderTest : public testing::Test { | |
| 74 public: | |
| 75 AffiliatedInvalidationServiceProviderTest(); | |
| 76 | |
| 77 // testing::Test: | |
| 78 virtual void SetUp() override; | |
| 79 virtual void TearDown() override; | |
| 80 | |
| 81 // Ownership is not passed. The Profile is owned by the global ProfileManager. | |
| 82 Profile* LogInAndReturnProfile(const std::string& user_id); | |
| 83 | |
| 84 AffiliatedInvalidationServiceProvider* GetProvider(); | |
| 85 | |
| 86 invalidation::TiclInvalidationService* GetDeviceInvalidationService(); | |
| 87 bool HasDeviceInvalidationServiceObserver() const; | |
| 88 | |
| 89 invalidation::FakeInvalidationService* GetProfileInvalidationService( | |
| 90 Profile* profile); | |
| 91 int GetProfileInvalidationServiceObserverCount() const; | |
| 92 | |
| 93 const invalidation::InvalidationService* GetInvalidationService() const; | |
| 94 | |
| 95 private: | |
| 96 content::TestBrowserThreadBundle thread_bundle_; | |
| 97 chromeos::FakeUserManager* fake_user_manager_; | |
| 98 chromeos::ScopedUserManagerEnabler user_manager_enabler_; | |
| 99 ScopedStubEnterpriseInstallAttributes install_attributes_; | |
| 100 scoped_ptr<chromeos::ScopedTestDeviceSettingsService> | |
| 101 test_device_settings_service_; | |
| 102 scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_; | |
| 103 TestingProfileManager profile_manager_; | |
| 104 | |
| 105 scoped_ptr<AffiliatedInvalidationServiceProvider> provider_; | |
| 106 }; | |
| 107 | |
| 108 MockConsumer::MockConsumer() { | |
| 109 } | |
| 110 | |
| 111 MockConsumer::~MockConsumer() { | |
| 112 } | |
| 113 | |
| 114 AffiliatedInvalidationServiceProviderTest:: | |
| 115 AffiliatedInvalidationServiceProviderTest() | |
| 116 : fake_user_manager_(new chromeos::FakeUserManager), | |
| 117 user_manager_enabler_(fake_user_manager_), | |
| 118 install_attributes_("example.com", | |
| 119 "user@example.com", | |
| 120 "device_id", | |
| 121 DEVICE_MODE_ENTERPRISE), | |
| 122 profile_manager_(TestingBrowserProcess::GetGlobal()) { | |
| 123 } | |
| 124 | |
| 125 void AffiliatedInvalidationServiceProviderTest::SetUp() { | |
| 126 chromeos::SystemSaltGetter::Initialize(); | |
| 127 chromeos::DBusThreadManager::Initialize(); | |
| 128 chromeos::DeviceOAuth2TokenServiceFactory::Initialize(); | |
| 129 ASSERT_TRUE(profile_manager_.SetUp()); | |
| 130 | |
| 131 test_device_settings_service_.reset(new | |
| 132 chromeos::ScopedTestDeviceSettingsService); | |
| 133 test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings); | |
| 134 | |
| 135 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> | |
| 136 RegisterTestingFactory(BuildProfileInvalidationProvider); | |
| 137 | |
| 138 provider_.reset(new AffiliatedInvalidationServiceProvider); | |
| 139 } | |
| 140 | |
| 141 void AffiliatedInvalidationServiceProviderTest::TearDown() { | |
| 142 provider_.reset(); | |
| 143 | |
| 144 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> | |
| 145 RegisterTestingFactory(nullptr); | |
| 146 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown(); | |
| 147 chromeos::DBusThreadManager::Shutdown(); | |
| 148 chromeos::SystemSaltGetter::Shutdown(); | |
| 149 } | |
| 150 | |
| 151 Profile* AffiliatedInvalidationServiceProviderTest::LogInAndReturnProfile( | |
| 152 const std::string& user_id) { | |
| 153 fake_user_manager_->AddUser(user_id); | |
| 154 Profile* profile = profile_manager_.CreateTestingProfile(user_id); | |
| 155 content::NotificationService::current()->Notify( | |
| 156 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
| 157 content::NotificationService::AllSources(), | |
| 158 content::Details<Profile>(profile)); | |
| 159 return profile; | |
| 160 } | |
| 161 | |
| 162 AffiliatedInvalidationServiceProvider* | |
| 163 AffiliatedInvalidationServiceProviderTest::GetProvider() { | |
| 164 return provider_.get(); | |
|
Mattias Nissler (ping if slow)
2015/01/13 09:50:16
Is there a good reason to have everything go throu
bartfab (slow)
2015/01/13 17:45:23
Just practicing good OOD encapsulation. I can make
| |
| 165 } | |
| 166 | |
| 167 invalidation::TiclInvalidationService* | |
| 168 AffiliatedInvalidationServiceProviderTest::GetDeviceInvalidationService() { | |
| 169 return provider_->GetDeviceInvalidationServiceForTest(); | |
| 170 } | |
| 171 | |
| 172 bool | |
| 173 AffiliatedInvalidationServiceProviderTest:: | |
| 174 HasDeviceInvalidationServiceObserver() const { | |
| 175 return provider_->HasDeviceInvalidationServiceObserverForTest(); | |
| 176 } | |
| 177 | |
| 178 invalidation::FakeInvalidationService* | |
| 179 AffiliatedInvalidationServiceProviderTest::GetProfileInvalidationService( | |
| 180 Profile* profile) { | |
| 181 invalidation::ProfileInvalidationProvider* invalidation_provider = | |
| 182 static_cast<invalidation::ProfileInvalidationProvider*>( | |
| 183 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> | |
| 184 GetServiceForBrowserContext(profile, false)); | |
| 185 if (!invalidation_provider) | |
| 186 return nullptr; | |
| 187 return static_cast<invalidation::FakeInvalidationService*>( | |
| 188 invalidation_provider->GetInvalidationService()); | |
| 189 } | |
| 190 | |
| 191 int AffiliatedInvalidationServiceProviderTest:: | |
| 192 GetProfileInvalidationServiceObserverCount() const { | |
| 193 return provider_->GetProfileInvalidationServiceObserverCountForTest(); | |
| 194 } | |
| 195 | |
| 196 const invalidation::InvalidationService* | |
| 197 AffiliatedInvalidationServiceProviderTest::GetInvalidationService() const { | |
| 198 return provider_->GetInvalidationServiceForTest(); | |
| 199 } | |
| 200 | |
| 201 // No consumers are registered with the AffiliatedInvalidationServiceProvider. | |
| 202 // Verifies that no device-global invalidation service is created. Further | |
| 203 // verifies that when an affiliated user's per-profile invalidation service | |
| 204 // connects, that invalidation service is ignored. | |
| 205 TEST_F(AffiliatedInvalidationServiceProviderTest, NoConsumers) { | |
| 206 // Verify that no device-global invalidation service and no per-profile | |
| 207 // invalidation service observers have been created. | |
| 208 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 209 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 210 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
|
Mattias Nissler (ping if slow)
2015/01/13 09:50:15
These tests are very strongly tied to the implemen
bartfab (slow)
2015/01/13 17:45:23
Ensuring that consumers see the right thing is one
| |
| 211 | |
| 212 // Log in as an affiliated user. | |
| 213 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 214 EXPECT_TRUE(profile); | |
| 215 | |
| 216 // Verify that a per-profile invalidation service and an observer for it have | |
| 217 // been created. | |
| 218 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 219 GetProfileInvalidationService(profile); | |
| 220 EXPECT_TRUE(profile_invalidation_service); | |
| 221 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 222 | |
| 223 // Verify that no device-global invalidation service has been created. | |
| 224 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 225 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 226 | |
| 227 // Verify that no invalidation service is being made available to consumers. | |
| 228 EXPECT_FALSE(GetInvalidationService()); | |
| 229 } | |
| 230 | |
| 231 // A consumer is registered with the AffiliatedInvalidationServiceProvider. | |
| 232 // Verifies that when no per-profile invalidation service belonging to an | |
| 233 // affiliated user is available, a device-global invalidation service is | |
| 234 // created. Further verifies that when the device-global invalidation service | |
| 235 // connects, it is made available to the consumer. | |
| 236 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 237 UseDeviceInvalidationService) { | |
| 238 // Register a consumer. Verify that the consumer is not called back | |
| 239 // immediately as no connected invalidation service exists yet. | |
| 240 MockConsumer consumer; | |
| 241 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 242 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 243 GetProvider()->RegisterConsumer(&consumer); | |
| 244 | |
| 245 // Verify that a device-global invalidation service has been created but is | |
| 246 // not being made available to consumers. | |
|
Mattias Nissler (ping if slow)
2015/01/13 09:50:15
does it matter to assert this?
bartfab (slow)
2015/01/13 17:45:23
Yes, because the call in line 260 will try to call
| |
| 247 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 248 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 249 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 250 EXPECT_FALSE(GetInvalidationService()); | |
| 251 | |
| 252 // Indicate that the device-global invalidation service has connected. Verify | |
| 253 // that the consumer is informed about this. | |
| 254 Mock::VerifyAndClearExpectations(&consumer); | |
| 255 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 256 EXPECT_CALL(consumer, | |
| 257 OnInvalidationServiceSet(GetDeviceInvalidationService())) | |
| 258 .Times(1); | |
| 259 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 260 GetDeviceInvalidationService()->OnInvalidatorStateChange( | |
| 261 syncer::INVALIDATIONS_ENABLED); | |
| 262 | |
| 263 // Verify that the device-global invalidation service still exists and is | |
| 264 // being made available to consumers. | |
|
Mattias Nissler (ping if slow)
2015/01/13 09:50:15
Didn't you test this per the consumer callback exp
bartfab (slow)
2015/01/13 17:45:23
I tested that the service is *not* made available
| |
| 265 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 266 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 267 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 268 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 269 | |
| 270 // Indicate that the device-global invalidation service has disconnected. | |
| 271 // Verify that the consumer is informed about this. | |
| 272 Mock::VerifyAndClearExpectations(&consumer); | |
| 273 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 274 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(1); | |
| 275 GetDeviceInvalidationService()->OnInvalidatorStateChange( | |
| 276 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 277 | |
| 278 // Verify that a device-global invalidation service still exists but is not | |
| 279 // being made available to consumers. | |
|
Mattias Nissler (ping if slow)
2015/01/13 09:50:15
ditto here
bartfab (slow)
2015/01/13 17:45:23
As above, in reverse - we switched back from *is*
| |
| 280 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 281 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 282 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 283 EXPECT_FALSE(GetInvalidationService()); | |
| 284 | |
| 285 // Unregister the consumer. | |
| 286 Mock::VerifyAndClearExpectations(&consumer); | |
| 287 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 288 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 289 GetProvider()->UnregisterConsumer(&consumer); | |
| 290 } | |
| 291 | |
| 292 // A consumer is registered with the AffiliatedInvalidationServiceProvider. | |
| 293 // Verifies that when a per-profile invalidation service belonging to an | |
| 294 // affiliated user connects, it is made available to the consumer. | |
| 295 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 296 UseAffiliatedProfileInvalidationService) { | |
| 297 // Register a consumer. Verify that the consumer is not called back | |
| 298 // immediately as no connected invalidation service exists yet. | |
| 299 MockConsumer consumer; | |
| 300 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 301 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 302 GetProvider()->RegisterConsumer(&consumer); | |
| 303 | |
| 304 // Verify that a device-global invalidation service has been created but is | |
| 305 // not being made available to consumers. | |
|
Mattias Nissler (ping if slow)
2015/01/13 09:50:16
ditto
bartfab (slow)
2015/01/13 17:45:23
This is a new test, so we check the starting state
| |
| 306 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 307 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 308 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 309 EXPECT_FALSE(GetInvalidationService()); | |
| 310 | |
| 311 // Log in as an affiliated user. | |
| 312 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 313 EXPECT_TRUE(profile); | |
| 314 | |
| 315 // Verify that a per-profile invalidation service and an observer for it have | |
| 316 // been created. | |
|
Mattias Nissler (ping if slow)
2015/01/13 09:50:15
ditto - do we care in the test? The consumer shoul
bartfab (slow)
2015/01/13 17:45:23
We can discuss this offline if you want. The thing
| |
| 317 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 318 GetProfileInvalidationService(profile); | |
| 319 ASSERT_TRUE(profile_invalidation_service); | |
| 320 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 321 | |
| 322 // Verify that a device-global invalidation service still exists. | |
| 323 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 324 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 325 | |
| 326 // Verify that no invalidation service is being made available to consumers. | |
| 327 EXPECT_FALSE(GetInvalidationService()); | |
| 328 | |
| 329 // Indicate that the per-profile invalidation service has connected. Verify | |
| 330 // that the consumer is informed about this. | |
| 331 Mock::VerifyAndClearExpectations(&consumer); | |
| 332 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 333 EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service)) | |
| 334 .Times(1); | |
| 335 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 336 profile_invalidation_service->SetInvalidatorState( | |
| 337 syncer::INVALIDATIONS_ENABLED); | |
| 338 | |
| 339 // Verify that the device-global invalidation service has been destroyed. | |
| 340 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 341 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 342 | |
| 343 // Verify that the per-profile invalidation service is being made available to | |
| 344 // consumers and an observer for it still exists. | |
| 345 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 346 ASSERT_TRUE(profile_invalidation_service); | |
| 347 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 348 EXPECT_EQ(profile_invalidation_service, GetInvalidationService()); | |
| 349 | |
| 350 // Indicate that the per-profile invalidation service has disconnected. Verify | |
| 351 // that the consumer is informed about this. | |
| 352 Mock::VerifyAndClearExpectations(&consumer); | |
| 353 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 354 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(1); | |
| 355 profile_invalidation_service->SetInvalidatorState( | |
| 356 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 357 | |
| 358 // Verify that a device-global invalidation service has been created. | |
| 359 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 360 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 361 | |
| 362 // Verify that the per-profile invalidation service still exists but is no | |
| 363 // longer being made available to consumers. | |
| 364 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 365 EXPECT_TRUE(profile_invalidation_service); | |
| 366 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 367 EXPECT_FALSE(GetInvalidationService()); | |
| 368 | |
| 369 // Unregister the consumer. | |
| 370 Mock::VerifyAndClearExpectations(&consumer); | |
| 371 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 372 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 373 GetProvider()->UnregisterConsumer(&consumer); | |
| 374 } | |
| 375 | |
| 376 // A consumer is registered with the AffiliatedInvalidationServiceProvider. | |
| 377 // Verifies that when a per-profile invalidation service belonging to an | |
| 378 // unaffiliated user connects, it is ignored. | |
| 379 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 380 DoNotUseUnaffiliatedProfileInvalidationService) { | |
| 381 // Register a consumer. Verify that the consumer is not called back | |
| 382 // immediately as no connected invalidation service exists yet. | |
| 383 MockConsumer consumer; | |
| 384 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 385 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 386 GetProvider()->RegisterConsumer(&consumer); | |
| 387 | |
| 388 // Verify that a device-global invalidation service has been created but is | |
| 389 // not being made available to consumers. | |
| 390 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 391 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 392 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 393 EXPECT_FALSE(GetInvalidationService()); | |
| 394 | |
| 395 // Log in as an unaffiliated user. | |
| 396 Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID); | |
| 397 EXPECT_TRUE(profile); | |
| 398 | |
| 399 // Verify that a per-profile invalidation service but no observer for it has | |
| 400 // been created. | |
| 401 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 402 GetProfileInvalidationService(profile); | |
| 403 ASSERT_TRUE(profile_invalidation_service); | |
| 404 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 405 | |
| 406 // Verify that a device-global invalidation service still exists. | |
| 407 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 408 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 409 | |
| 410 // Verify that no invalidation service is being made available to consumers. | |
| 411 EXPECT_FALSE(GetInvalidationService()); | |
| 412 | |
| 413 // Indicate that the per-profile invalidation service has connected. Verify | |
| 414 // that the consumer is not called back. | |
| 415 profile_invalidation_service->SetInvalidatorState( | |
| 416 syncer::INVALIDATIONS_ENABLED); | |
| 417 | |
| 418 // Verify that a device-global invalidation service still exists. | |
| 419 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 420 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 421 | |
| 422 // Verify that the per-profile invalidation service still exists but is not | |
| 423 // being made available to consumers. | |
| 424 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 425 EXPECT_TRUE(profile_invalidation_service); | |
| 426 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 427 EXPECT_FALSE(GetInvalidationService()); | |
| 428 | |
| 429 // Unregister the consumer. | |
| 430 GetProvider()->UnregisterConsumer(&consumer); | |
| 431 } | |
| 432 | |
| 433 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 434 // device-global invalidation service exists, is connected and is made available | |
| 435 // to the consumer. Verifies that when a per-profile invalidation service | |
| 436 // belonging to an affiliated user connects, it is made available to the | |
| 437 // consumer instead and the device-global invalidation service is destroyed. | |
| 438 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 439 SwitchToAffiliatedProfileInvalidationService) { | |
| 440 // Register a consumer. Verify that the consumer is not called back | |
| 441 // immediately as no connected invalidation service exists yet. | |
| 442 MockConsumer consumer; | |
| 443 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 444 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 445 GetProvider()->RegisterConsumer(&consumer); | |
| 446 | |
| 447 // Verify that a device-global invalidation service has been created but is | |
| 448 // not being made available to consumers. | |
| 449 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 450 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 451 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 452 EXPECT_FALSE(GetInvalidationService()); | |
| 453 | |
| 454 // Indicate that the device-global invalidation service has connected. Verify | |
| 455 // that the consumer is informed about this. | |
| 456 Mock::VerifyAndClearExpectations(&consumer); | |
| 457 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 458 EXPECT_CALL(consumer, | |
| 459 OnInvalidationServiceSet(GetDeviceInvalidationService())) | |
| 460 .Times(1); | |
| 461 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 462 GetDeviceInvalidationService()->OnInvalidatorStateChange( | |
| 463 syncer::INVALIDATIONS_ENABLED); | |
| 464 Mock::VerifyAndClearExpectations(&consumer); | |
| 465 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 466 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 467 | |
| 468 // Verify that the device-global invalidation service still exists and is | |
| 469 // being made available to consumers. | |
| 470 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 471 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 472 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 473 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 474 | |
| 475 // Log in as an affiliated user. | |
| 476 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 477 EXPECT_TRUE(profile); | |
| 478 | |
| 479 // Verify that a per-profile invalidation service and an observer for it have | |
| 480 // been created. | |
| 481 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 482 GetProfileInvalidationService(profile); | |
| 483 ASSERT_TRUE(profile_invalidation_service); | |
| 484 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 485 | |
| 486 // Verify that a device-global invalidation service still exists. | |
| 487 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 488 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 489 | |
| 490 // Verify that the device-global invalidation service is still being made | |
| 491 // available to consumers. | |
| 492 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 493 | |
| 494 // Indicate that the per-profile invalidation service has connected. Verify | |
| 495 // that the consumer is informed that the device-global invalidation service | |
| 496 // is no longer to be used and the per-profile invalidation service is to be | |
| 497 // used instead. | |
| 498 Mock::VerifyAndClearExpectations(&consumer); | |
| 499 Sequence sequence; | |
| 500 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 501 EXPECT_CALL(consumer, OnInvalidationServiceReset()) | |
| 502 .Times(1) | |
| 503 .InSequence(sequence); | |
| 504 EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service)) | |
| 505 .Times(1) | |
| 506 .InSequence(sequence); | |
| 507 profile_invalidation_service->SetInvalidatorState( | |
| 508 syncer::INVALIDATIONS_ENABLED); | |
| 509 | |
| 510 // Verify that the device-global invalidation service has been destroyed. | |
| 511 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 512 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 513 | |
| 514 // Verify that the per-profile invalidation service is being made available to | |
| 515 // consumers and an observer for it still exists. | |
| 516 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 517 ASSERT_TRUE(profile_invalidation_service); | |
| 518 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 519 EXPECT_EQ(profile_invalidation_service, GetInvalidationService()); | |
| 520 | |
| 521 // Unregister the consumer. | |
| 522 Mock::VerifyAndClearExpectations(&consumer); | |
| 523 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 524 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(1); | |
| 525 GetProvider()->UnregisterConsumer(&consumer); | |
| 526 } | |
| 527 | |
| 528 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 529 // device-global invalidation service exists, is connected and is made available | |
| 530 // to the consumer. Verifies that when a per-profile invalidation service | |
| 531 // belonging to an unaffiliated user connects, it is ignored and the | |
| 532 // device-global invalidation service continues to be made available to the | |
| 533 // consumer. | |
| 534 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 535 DoNotSwitchToUnaffiliatedProfileInvalidationService) { | |
| 536 // Register a consumer. Verify that the consumer is not called back | |
| 537 // immediately as no connected invalidation service exists yet. | |
| 538 MockConsumer consumer; | |
| 539 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 540 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 541 GetProvider()->RegisterConsumer(&consumer); | |
| 542 | |
| 543 // Verify that a device-global invalidation service has been created but is | |
| 544 // not being made available to consumers. | |
| 545 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 546 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 547 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 548 EXPECT_FALSE(GetInvalidationService()); | |
| 549 | |
| 550 // Indicate that the device-global invalidation service has connected. Verify | |
| 551 // that the consumer is informed about this. | |
| 552 Mock::VerifyAndClearExpectations(&consumer); | |
| 553 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 554 EXPECT_CALL(consumer, | |
| 555 OnInvalidationServiceSet(GetDeviceInvalidationService())) | |
| 556 .Times(1); | |
| 557 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 558 GetDeviceInvalidationService()->OnInvalidatorStateChange( | |
| 559 syncer::INVALIDATIONS_ENABLED); | |
| 560 Mock::VerifyAndClearExpectations(&consumer); | |
| 561 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 562 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 563 | |
| 564 // Verify that the device-global invalidation service still exists and is | |
| 565 // being made available to consumers. | |
| 566 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 567 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 568 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 569 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 570 | |
| 571 // Log in as an unaffiliated user. | |
| 572 Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID); | |
| 573 EXPECT_TRUE(profile); | |
| 574 | |
| 575 // Verify that a per-profile invalidation service but no observer for it has | |
| 576 // been created. | |
| 577 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 578 GetProfileInvalidationService(profile); | |
| 579 ASSERT_TRUE(profile_invalidation_service); | |
| 580 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 581 | |
| 582 // Verify that a device-global invalidation service still exists. | |
| 583 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 584 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 585 | |
| 586 // Verify that the device-global invalidation service is still being made | |
| 587 // available to consumers. | |
| 588 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 589 | |
| 590 // Indicate that the per-profile invalidation service has connected. Verify | |
| 591 // that the consumer is not called back. | |
| 592 profile_invalidation_service->SetInvalidatorState( | |
| 593 syncer::INVALIDATIONS_ENABLED); | |
| 594 | |
| 595 // Verify that the device-global invalidation service still exists and is | |
| 596 // being made available to consumers. | |
| 597 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 598 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 599 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 600 | |
| 601 // Verify that the per-profile invalidation service still exists. | |
| 602 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 603 EXPECT_TRUE(profile_invalidation_service); | |
| 604 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 605 | |
| 606 // Unregister the consumer. | |
| 607 Mock::VerifyAndClearExpectations(&consumer); | |
| 608 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 609 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(1); | |
| 610 GetProvider()->UnregisterConsumer(&consumer); | |
| 611 } | |
| 612 | |
| 613 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 614 // per-profile invalidation service belonging to an affiliated user exists, is | |
| 615 // connected and is made available to the consumer. Verifies that when the | |
| 616 // per-profile invalidation service disconnects, a device-global invalidation | |
| 617 // service is created. Further verifies that when the device-global invalidation | |
| 618 // service connects, it is made available to the consumer. | |
| 619 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 620 SwitchToDeviceInvalidationService) { | |
| 621 // Register a consumer. Verify that the consumer is not called back | |
| 622 // immediately as no connected invalidation service exists yet. | |
| 623 MockConsumer consumer; | |
| 624 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 625 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 626 GetProvider()->RegisterConsumer(&consumer); | |
| 627 | |
| 628 // Verify that a device-global invalidation service has been created but is | |
| 629 // not being made available to consumers. | |
| 630 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 631 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 632 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 633 EXPECT_FALSE(GetInvalidationService()); | |
| 634 | |
| 635 // Log in as an affiliated user. | |
| 636 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 637 EXPECT_TRUE(profile); | |
| 638 | |
| 639 // Verify that a per-profile invalidation service and an observer for it have | |
| 640 // been created. | |
| 641 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 642 GetProfileInvalidationService(profile); | |
| 643 ASSERT_TRUE(profile_invalidation_service); | |
| 644 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 645 | |
| 646 // Verify that a device-global invalidation service still exists. | |
| 647 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 648 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 649 | |
| 650 // Verify that no invalidation service is being made available to consumers. | |
| 651 EXPECT_FALSE(GetInvalidationService()); | |
| 652 | |
| 653 // Indicate that the per-profile invalidation service has connected. Verify | |
| 654 // that the consumer is informed about this. | |
| 655 Mock::VerifyAndClearExpectations(&consumer); | |
| 656 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 657 EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service)) | |
| 658 .Times(1); | |
| 659 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 660 profile_invalidation_service->SetInvalidatorState( | |
| 661 syncer::INVALIDATIONS_ENABLED); | |
| 662 | |
| 663 // Verify that the device-global invalidation service has been destroyed. | |
| 664 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 665 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 666 | |
| 667 // Verify that the per-profile invalidation service is being made available to | |
| 668 // consumers and an observer for it still exists. | |
| 669 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 670 ASSERT_TRUE(profile_invalidation_service); | |
| 671 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 672 EXPECT_EQ(profile_invalidation_service, GetInvalidationService()); | |
| 673 | |
| 674 // Indicate that the per-profile invalidation service has disconnected. Verify | |
| 675 // that the consumer is informed about this. | |
| 676 Mock::VerifyAndClearExpectations(&consumer); | |
| 677 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 678 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(1); | |
| 679 profile_invalidation_service->SetInvalidatorState( | |
| 680 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 681 | |
| 682 // Verify that a device-global invalidation service has been created. | |
| 683 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 684 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 685 | |
| 686 // Verify that the per-profile invalidation service still exists but is no | |
| 687 // longer being made available to consumers. | |
| 688 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 689 EXPECT_TRUE(profile_invalidation_service); | |
| 690 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 691 EXPECT_FALSE(GetInvalidationService()); | |
| 692 | |
| 693 // Indicate that the device-global invalidation service has connected. Verify | |
| 694 // that the consumer is informed about this. | |
| 695 Mock::VerifyAndClearExpectations(&consumer); | |
| 696 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 697 EXPECT_CALL(consumer, | |
| 698 OnInvalidationServiceSet(GetDeviceInvalidationService())) | |
| 699 .Times(1); | |
| 700 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 701 GetDeviceInvalidationService()->OnInvalidatorStateChange( | |
| 702 syncer::INVALIDATIONS_ENABLED); | |
| 703 | |
| 704 // Verify that the device-global invalidation service still exists and is | |
| 705 // being made available to consumers. | |
| 706 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 707 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 708 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 709 | |
| 710 // Verify that the per-profile invalidation service still exists. | |
| 711 profile_invalidation_service = GetProfileInvalidationService(profile); | |
| 712 EXPECT_TRUE(profile_invalidation_service); | |
| 713 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 714 | |
| 715 // Unregister the consumer. | |
| 716 Mock::VerifyAndClearExpectations(&consumer); | |
| 717 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 718 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(1); | |
| 719 GetProvider()->UnregisterConsumer(&consumer); | |
| 720 } | |
| 721 | |
| 722 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 723 // per-profile invalidation service belonging to a first affiliated user exists, | |
| 724 // is connected and is made available to the consumer. A per-profile | |
| 725 // invalidation service belonging to a second affiliated user also exists and is | |
| 726 // connected. Verifies that when the per-profile invalidation service belonging | |
| 727 // to the first user disconnects, the per-profile invalidation service belonging | |
| 728 // to the second user is made available to the consumer instead. | |
| 729 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 730 SwitchBetweenAffiliatedProfileInvalidationServices) { | |
| 731 // Register a consumer. Verify that the consumer is not called back | |
| 732 // immediately as no connected invalidation service exists yet. | |
| 733 MockConsumer consumer; | |
| 734 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 735 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 736 GetProvider()->RegisterConsumer(&consumer); | |
| 737 | |
| 738 // Verify that a device-global invalidation service has been created but is | |
| 739 // not being made available to consumers. | |
| 740 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 741 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 742 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 743 EXPECT_FALSE(GetInvalidationService()); | |
| 744 | |
| 745 // Log in as a first affiliated user. | |
| 746 Profile* profile_1 = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 747 EXPECT_TRUE(profile_1); | |
| 748 | |
| 749 // Verify that a per-profile invalidation service fr the first user and an | |
| 750 // observer for it have been created. | |
| 751 invalidation::FakeInvalidationService* profile_1_invalidation_service = | |
| 752 GetProfileInvalidationService(profile_1); | |
| 753 ASSERT_TRUE(profile_1_invalidation_service); | |
| 754 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 755 | |
| 756 // Verify that a device-global invalidation service still exists. | |
| 757 EXPECT_TRUE(GetDeviceInvalidationService()); | |
| 758 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 759 | |
| 760 // Verify that no invalidation service is being made available to consumers. | |
| 761 EXPECT_FALSE(GetInvalidationService()); | |
| 762 | |
| 763 // Indicate that the first user's per-profile invalidation service has | |
| 764 // connected. Verify that the consumer is informed about this. | |
| 765 Mock::VerifyAndClearExpectations(&consumer); | |
| 766 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 767 EXPECT_CALL(consumer, | |
| 768 OnInvalidationServiceSet(profile_1_invalidation_service)) | |
| 769 .Times(1); | |
| 770 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 771 profile_1_invalidation_service->SetInvalidatorState( | |
| 772 syncer::INVALIDATIONS_ENABLED); | |
| 773 Mock::VerifyAndClearExpectations(&consumer); | |
| 774 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 775 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(0); | |
| 776 | |
| 777 // Verify that the device-global invalidation service has been destroyed. | |
| 778 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 779 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 780 | |
| 781 // Verify that the per-profile invalidation service for the first user is | |
| 782 // being made available to consumers and an observer for it still exists. | |
| 783 profile_1_invalidation_service = GetProfileInvalidationService(profile_1); | |
| 784 EXPECT_TRUE(profile_1_invalidation_service); | |
| 785 EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount()); | |
| 786 EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService()); | |
| 787 | |
| 788 // Log in as a second affiliated user. | |
| 789 Profile* profile_2 = LogInAndReturnProfile(kAffiliatedUserID2); | |
| 790 EXPECT_TRUE(profile_2); | |
| 791 | |
| 792 // Verify that the device-global invalidation service still does not exist. | |
| 793 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 794 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 795 | |
| 796 // Verify that a per-profile invalidation service still exists for the first | |
| 797 // user and one has been created for the second user. | |
| 798 profile_1_invalidation_service = GetProfileInvalidationService(profile_1); | |
| 799 EXPECT_TRUE(profile_1_invalidation_service); | |
| 800 invalidation::FakeInvalidationService* profile_2_invalidation_service = | |
| 801 GetProfileInvalidationService(profile_2); | |
| 802 ASSERT_TRUE(profile_2_invalidation_service); | |
| 803 EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount()); | |
| 804 | |
| 805 // Verify that the per-profile invalidation service for the first user is | |
| 806 // being made available to consumers. | |
| 807 EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService()); | |
| 808 | |
| 809 // Indicate that the second user's per-profile invalidation service has | |
| 810 // connected. Verify that the consumer is not called back. | |
| 811 profile_2_invalidation_service->SetInvalidatorState( | |
| 812 syncer::INVALIDATIONS_ENABLED); | |
| 813 | |
| 814 // Verify that the device-global invalidation service still does not exist. | |
| 815 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 816 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 817 | |
| 818 // Verify that per-profile invalidation services still exist for both users. | |
| 819 profile_1_invalidation_service = GetProfileInvalidationService(profile_1); | |
| 820 ASSERT_TRUE(profile_1_invalidation_service); | |
| 821 profile_2_invalidation_service = GetProfileInvalidationService(profile_2); | |
| 822 EXPECT_TRUE(profile_2_invalidation_service); | |
| 823 EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount()); | |
| 824 | |
| 825 // Verify that the per-profile invalidation service for the first user is | |
| 826 // being made available to consumers. | |
| 827 EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService()); | |
| 828 | |
| 829 // Indicate that the first user's per-profile invalidation service has | |
| 830 // disconnected. Verify that the consumer is informed that the first user's | |
| 831 // per-profile invalidation service is no longer to be used and the second | |
| 832 // user's per-profile invalidation service is to be used instead. | |
| 833 Mock::VerifyAndClearExpectations(&consumer); | |
| 834 Sequence sequence; | |
| 835 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 836 EXPECT_CALL(consumer, OnInvalidationServiceReset()) | |
| 837 .Times(1) | |
| 838 .InSequence(sequence); | |
| 839 EXPECT_CALL(consumer, | |
| 840 OnInvalidationServiceSet(profile_2_invalidation_service)) | |
| 841 .Times(1) | |
| 842 .InSequence(sequence); | |
| 843 profile_1_invalidation_service->SetInvalidatorState( | |
| 844 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 845 | |
| 846 // Verify that the device-global invalidation service still does not exist. | |
| 847 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 848 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 849 | |
| 850 // Verify that per-profile invalidation services still exist for both users. | |
| 851 profile_1_invalidation_service = GetProfileInvalidationService(profile_1); | |
| 852 EXPECT_TRUE(profile_1_invalidation_service); | |
| 853 profile_2_invalidation_service = GetProfileInvalidationService(profile_2); | |
| 854 EXPECT_TRUE(profile_2_invalidation_service); | |
| 855 EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount()); | |
| 856 | |
| 857 // Verify that the per-profile invalidation service for the second user is | |
| 858 // being made available to consumers. | |
| 859 EXPECT_EQ(profile_2_invalidation_service, GetInvalidationService()); | |
| 860 | |
| 861 // Unregister the consumer. | |
| 862 Mock::VerifyAndClearExpectations(&consumer); | |
| 863 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 864 EXPECT_CALL(consumer, OnInvalidationServiceReset()).Times(1); | |
| 865 GetProvider()->UnregisterConsumer(&consumer); | |
| 866 } | |
| 867 | |
| 868 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 869 // device-global invalidation service exists, is connected and is made available | |
| 870 // to the consumer. Verifies that when a second consumer registers, the | |
| 871 // device-global invalidation service is made available to it as well. Further | |
| 872 // verifies that when the first consumer unregisters, the device-global | |
| 873 // invalidation service is not destroyed and remains available to the second | |
| 874 // consumer. Further verifies that when the second consumer also unregisters, | |
| 875 // the device-global invalidation service is destroyed. | |
| 876 TEST_F(AffiliatedInvalidationServiceProviderTest, MultipleConsumers) { | |
| 877 // Register a first consumer. Verify that the consumer is not called back | |
| 878 // immediately as no connected invalidation service exists yet. | |
| 879 MockConsumer consumer_1; | |
| 880 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0); | |
| 881 EXPECT_CALL(consumer_1, OnInvalidationServiceReset()).Times(0); | |
| 882 GetProvider()->RegisterConsumer(&consumer_1); | |
| 883 | |
| 884 // Verify that a device-global invalidation service has been created but is | |
| 885 // not being made available to consumers. | |
| 886 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 887 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 888 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 889 EXPECT_FALSE(GetInvalidationService()); | |
| 890 | |
| 891 // Indicate that the device-global invalidation service has connected. Verify | |
| 892 // that the first consumer is informed about this. | |
| 893 Mock::VerifyAndClearExpectations(&consumer_1); | |
| 894 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0); | |
| 895 EXPECT_CALL(consumer_1, | |
| 896 OnInvalidationServiceSet(GetDeviceInvalidationService())) | |
| 897 .Times(1); | |
| 898 EXPECT_CALL(consumer_1, OnInvalidationServiceReset()).Times(0); | |
| 899 GetDeviceInvalidationService()->OnInvalidatorStateChange( | |
| 900 syncer::INVALIDATIONS_ENABLED); | |
| 901 | |
| 902 // Verify that the device-global invalidation service still exists and is | |
| 903 // being made available to consumers. | |
| 904 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 905 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 906 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 907 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 908 | |
| 909 // Register a second consumer. Verify that the consumer is called back | |
| 910 // immediately as a connected invalidation service is available. | |
| 911 MockConsumer consumer_2; | |
| 912 EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0); | |
| 913 EXPECT_CALL(consumer_2, | |
| 914 OnInvalidationServiceSet(GetDeviceInvalidationService())) | |
| 915 .Times(1); | |
| 916 EXPECT_CALL(consumer_2, OnInvalidationServiceReset()).Times(0); | |
| 917 GetProvider()->RegisterConsumer(&consumer_2); | |
| 918 Mock::VerifyAndClearExpectations(&consumer_2); | |
| 919 EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0); | |
| 920 EXPECT_CALL(consumer_2, OnInvalidationServiceReset()).Times(0); | |
| 921 | |
| 922 // Unregister the first consumer. | |
| 923 Mock::VerifyAndClearExpectations(&consumer_1); | |
| 924 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0); | |
| 925 EXPECT_CALL(consumer_1, OnInvalidationServiceReset()).Times(1); | |
| 926 GetProvider()->UnregisterConsumer(&consumer_1); | |
| 927 Mock::VerifyAndClearExpectations(&consumer_1); | |
| 928 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0); | |
| 929 EXPECT_CALL(consumer_1, OnInvalidationServiceReset()).Times(0); | |
| 930 | |
| 931 // Verify that the device-global invalidation service still exists and is | |
| 932 // being made available to consumers. | |
| 933 ASSERT_TRUE(GetDeviceInvalidationService()); | |
| 934 EXPECT_TRUE(HasDeviceInvalidationServiceObserver()); | |
| 935 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 936 EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService()); | |
| 937 | |
| 938 // Unregister the second consumer. | |
| 939 Mock::VerifyAndClearExpectations(&consumer_2); | |
| 940 EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0); | |
| 941 EXPECT_CALL(consumer_2, OnInvalidationServiceReset()).Times(1); | |
| 942 GetProvider()->UnregisterConsumer(&consumer_2); | |
| 943 Mock::VerifyAndClearExpectations(&consumer_2); | |
| 944 EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0); | |
| 945 EXPECT_CALL(consumer_2, OnInvalidationServiceReset()).Times(0); | |
| 946 | |
| 947 // Verify that the device-global invalidation service has been destroyed. | |
| 948 EXPECT_FALSE(GetDeviceInvalidationService()); | |
| 949 EXPECT_FALSE(HasDeviceInvalidationServiceObserver()); | |
| 950 EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount()); | |
| 951 EXPECT_FALSE(GetInvalidationService()); | |
| 952 } | |
| 953 | |
| 954 } // namespace policy | |
| OLD | NEW |