OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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::Mock; |
| 37 using testing::StrictMock; |
| 38 |
| 39 namespace policy { |
| 40 |
| 41 namespace { |
| 42 |
| 43 const char kAffiliatedUserID1[] = "test_1@example.com"; |
| 44 const char kAffiliatedUserID2[] = "test_2@example.com"; |
| 45 const char kUnaffiliatedUserID[] = "test@other_domain.test"; |
| 46 |
| 47 KeyedService* BuildProfileInvalidationProvider( |
| 48 content::BrowserContext* context) { |
| 49 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service( |
| 50 new invalidation::FakeInvalidationService); |
| 51 invalidation_service->SetInvalidatorState( |
| 52 syncer::TRANSIENT_INVALIDATION_ERROR); |
| 53 return new invalidation::ProfileInvalidationProvider( |
| 54 invalidation_service.Pass()); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 class MockConsumer : public AffiliatedInvalidationServiceProvider::Consumer { |
| 60 public: |
| 61 MockConsumer(); |
| 62 ~MockConsumer() override; |
| 63 |
| 64 MOCK_METHOD1(OnInvalidationServiceSet, |
| 65 void(invalidation::InvalidationService*)); |
| 66 |
| 67 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(MockConsumer); |
| 69 }; |
| 70 |
| 71 class AffiliatedInvalidationServiceProviderTest : public testing::Test { |
| 72 public: |
| 73 AffiliatedInvalidationServiceProviderTest(); |
| 74 |
| 75 // testing::Test: |
| 76 virtual void SetUp() override; |
| 77 virtual void TearDown() override; |
| 78 |
| 79 // Ownership is not passed. The Profile is owned by the global ProfileManager. |
| 80 Profile* LogInAndReturnProfile(const std::string& user_id); |
| 81 |
| 82 // Logs in as an affiliated user and indicates that the per-profile |
| 83 // invalidation service for this user connected. Verifies that this |
| 84 // invalidation service is made available to the |consumer_| and the |
| 85 // device-global invalidation service is destroyed. |
| 86 void LogInAsAffiliatedUserAndConnectInvalidationService(); |
| 87 |
| 88 // Logs in as an unaffiliated user and indicates that the per-profile |
| 89 // invalidation service for this user connected. Verifies that this |
| 90 // invalidation service is ignored and the device-global invalidation service |
| 91 // is not destroyed. |
| 92 void LogInAsUnaffiliatedUserAndConnectInvalidationService(); |
| 93 |
| 94 // Indicates that the device-global invalidation service connected. Verifies |
| 95 // that the |consumer_| is informed about this. |
| 96 void ConnectDeviceGlobalInvalidationService(); |
| 97 |
| 98 // Indicates that the logged-in user's per-profile invalidation service |
| 99 // disconnected. Verifies that the |consumer_| is informed about this and a |
| 100 // device-global invalidation service is created. |
| 101 void DisconnectPerProfileInvalidationService(); |
| 102 |
| 103 invalidation::FakeInvalidationService* GetProfileInvalidationService( |
| 104 Profile* profile); |
| 105 |
| 106 protected: |
| 107 scoped_ptr<AffiliatedInvalidationServiceProvider> provider_; |
| 108 StrictMock<MockConsumer> consumer_; |
| 109 invalidation::TiclInvalidationService* device_invalidation_service_; |
| 110 invalidation::FakeInvalidationService* profile_invalidation_service_; |
| 111 |
| 112 private: |
| 113 content::TestBrowserThreadBundle thread_bundle_; |
| 114 chromeos::FakeUserManager* fake_user_manager_; |
| 115 chromeos::ScopedUserManagerEnabler user_manager_enabler_; |
| 116 ScopedStubEnterpriseInstallAttributes install_attributes_; |
| 117 scoped_ptr<chromeos::ScopedTestDeviceSettingsService> |
| 118 test_device_settings_service_; |
| 119 scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_; |
| 120 TestingProfileManager profile_manager_; |
| 121 }; |
| 122 |
| 123 MockConsumer::MockConsumer() { |
| 124 } |
| 125 |
| 126 MockConsumer::~MockConsumer() { |
| 127 } |
| 128 |
| 129 AffiliatedInvalidationServiceProviderTest:: |
| 130 AffiliatedInvalidationServiceProviderTest() |
| 131 : device_invalidation_service_(nullptr), |
| 132 profile_invalidation_service_(nullptr), |
| 133 fake_user_manager_(new chromeos::FakeUserManager), |
| 134 user_manager_enabler_(fake_user_manager_), |
| 135 install_attributes_("example.com", |
| 136 "user@example.com", |
| 137 "device_id", |
| 138 DEVICE_MODE_ENTERPRISE), |
| 139 profile_manager_(TestingBrowserProcess::GetGlobal()) { |
| 140 } |
| 141 |
| 142 void AffiliatedInvalidationServiceProviderTest::SetUp() { |
| 143 chromeos::SystemSaltGetter::Initialize(); |
| 144 chromeos::DBusThreadManager::Initialize(); |
| 145 chromeos::DeviceOAuth2TokenServiceFactory::Initialize(); |
| 146 ASSERT_TRUE(profile_manager_.SetUp()); |
| 147 |
| 148 test_device_settings_service_.reset(new |
| 149 chromeos::ScopedTestDeviceSettingsService); |
| 150 test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings); |
| 151 |
| 152 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> |
| 153 RegisterTestingFactory(BuildProfileInvalidationProvider); |
| 154 |
| 155 provider_.reset(new AffiliatedInvalidationServiceProvider); |
| 156 } |
| 157 |
| 158 void AffiliatedInvalidationServiceProviderTest::TearDown() { |
| 159 provider_.reset(); |
| 160 |
| 161 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> |
| 162 RegisterTestingFactory(nullptr); |
| 163 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown(); |
| 164 chromeos::DBusThreadManager::Shutdown(); |
| 165 chromeos::SystemSaltGetter::Shutdown(); |
| 166 } |
| 167 |
| 168 Profile* AffiliatedInvalidationServiceProviderTest::LogInAndReturnProfile( |
| 169 const std::string& user_id) { |
| 170 fake_user_manager_->AddUser(user_id); |
| 171 Profile* profile = profile_manager_.CreateTestingProfile(user_id); |
| 172 content::NotificationService::current()->Notify( |
| 173 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
| 174 content::NotificationService::AllSources(), |
| 175 content::Details<Profile>(profile)); |
| 176 return profile; |
| 177 } |
| 178 |
| 179 void AffiliatedInvalidationServiceProviderTest:: |
| 180 LogInAsAffiliatedUserAndConnectInvalidationService() { |
| 181 Mock::VerifyAndClearExpectations(&consumer_); |
| 182 |
| 183 // Log in as an affiliated user. |
| 184 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); |
| 185 EXPECT_TRUE(profile); |
| 186 Mock::VerifyAndClearExpectations(&consumer_); |
| 187 |
| 188 // Verify that a per-profile invalidation service has been created. |
| 189 profile_invalidation_service_ = GetProfileInvalidationService(profile); |
| 190 ASSERT_TRUE(profile_invalidation_service_); |
| 191 |
| 192 // Verify that the device-global invalidation service still exists. |
| 193 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 194 |
| 195 // Indicate that the per-profile invalidation service has connected. Verify |
| 196 // that the consumer is informed about this. |
| 197 EXPECT_CALL(consumer_, |
| 198 OnInvalidationServiceSet(profile_invalidation_service_)).Times(1); |
| 199 profile_invalidation_service_->SetInvalidatorState( |
| 200 syncer::INVALIDATIONS_ENABLED); |
| 201 Mock::VerifyAndClearExpectations(&consumer_); |
| 202 |
| 203 // Verify that the device-global invalidation service has been destroyed. |
| 204 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); |
| 205 |
| 206 Mock::VerifyAndClearExpectations(&consumer_); |
| 207 } |
| 208 |
| 209 void AffiliatedInvalidationServiceProviderTest:: |
| 210 LogInAsUnaffiliatedUserAndConnectInvalidationService() { |
| 211 Mock::VerifyAndClearExpectations(&consumer_); |
| 212 |
| 213 // Log in as an unaffiliated user. |
| 214 Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID); |
| 215 EXPECT_TRUE(profile); |
| 216 |
| 217 // Verify that a per-profile invalidation service has been created. |
| 218 profile_invalidation_service_ = GetProfileInvalidationService(profile); |
| 219 ASSERT_TRUE(profile_invalidation_service_); |
| 220 |
| 221 // Verify that the device-global invalidation service still exists. |
| 222 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 223 |
| 224 // Indicate that the per-profile invalidation service has connected. Verify |
| 225 // that the consumer is not called back. |
| 226 profile_invalidation_service_->SetInvalidatorState( |
| 227 syncer::INVALIDATIONS_ENABLED); |
| 228 |
| 229 // Verify that the device-global invalidation service still exists. |
| 230 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 231 |
| 232 Mock::VerifyAndClearExpectations(&consumer_); |
| 233 } |
| 234 |
| 235 void AffiliatedInvalidationServiceProviderTest:: |
| 236 ConnectDeviceGlobalInvalidationService() { |
| 237 Mock::VerifyAndClearExpectations(&consumer_); |
| 238 |
| 239 // Verify that a device-global invalidation service has been created. |
| 240 device_invalidation_service_ = |
| 241 provider_->GetDeviceInvalidationServiceForTest(); |
| 242 ASSERT_TRUE(device_invalidation_service_); |
| 243 |
| 244 // Indicate that the device-global invalidation service has connected. Verify |
| 245 // that the consumer is informed about this. |
| 246 EXPECT_CALL(consumer_, OnInvalidationServiceSet(device_invalidation_service_)) |
| 247 .Times(1); |
| 248 device_invalidation_service_->OnInvalidatorStateChange( |
| 249 syncer::INVALIDATIONS_ENABLED); |
| 250 |
| 251 Mock::VerifyAndClearExpectations(&consumer_); |
| 252 } |
| 253 |
| 254 void AffiliatedInvalidationServiceProviderTest:: |
| 255 DisconnectPerProfileInvalidationService() { |
| 256 Mock::VerifyAndClearExpectations(&consumer_); |
| 257 |
| 258 ASSERT_TRUE(profile_invalidation_service_); |
| 259 |
| 260 // Indicate that the per-profile invalidation service has disconnected. Verify |
| 261 // that the consumer is informed about this. |
| 262 EXPECT_CALL(consumer_, OnInvalidationServiceSet(nullptr)).Times(1); |
| 263 profile_invalidation_service_->SetInvalidatorState( |
| 264 syncer::INVALIDATION_CREDENTIALS_REJECTED); |
| 265 |
| 266 // Verify that a device-global invalidation service has been created. |
| 267 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 268 |
| 269 Mock::VerifyAndClearExpectations(&consumer_); |
| 270 } |
| 271 |
| 272 invalidation::FakeInvalidationService* |
| 273 AffiliatedInvalidationServiceProviderTest::GetProfileInvalidationService( |
| 274 Profile* profile) { |
| 275 invalidation::ProfileInvalidationProvider* invalidation_provider = |
| 276 static_cast<invalidation::ProfileInvalidationProvider*>( |
| 277 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> |
| 278 GetServiceForBrowserContext(profile, false)); |
| 279 if (!invalidation_provider) |
| 280 return nullptr; |
| 281 return static_cast<invalidation::FakeInvalidationService*>( |
| 282 invalidation_provider->GetInvalidationService()); |
| 283 } |
| 284 |
| 285 // No consumers are registered with the AffiliatedInvalidationServiceProvider. |
| 286 // Verifies that no device-global invalidation service is created, whether an |
| 287 // affiliated user is logged in or not. |
| 288 TEST_F(AffiliatedInvalidationServiceProviderTest, NoConsumers) { |
| 289 // Verify that no device-global invalidation service has been created. |
| 290 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); |
| 291 |
| 292 // Log in as an affiliated user. |
| 293 EXPECT_TRUE(LogInAndReturnProfile(kAffiliatedUserID1)); |
| 294 |
| 295 // Verify that no device-global invalidation service has been created. |
| 296 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); |
| 297 } |
| 298 |
| 299 // A consumer is registered with the AffiliatedInvalidationServiceProvider. |
| 300 // Verifies that when no per-profile invalidation service belonging to an |
| 301 // affiliated user is available, a device-global invalidation service is |
| 302 // created. Further verifies that when the device-global invalidation service |
| 303 // connects, it is made available to the consumer. |
| 304 TEST_F(AffiliatedInvalidationServiceProviderTest, |
| 305 UseDeviceInvalidationService) { |
| 306 // Register a consumer. Verify that the consumer is not called back |
| 307 // immediately as no connected invalidation service exists yet. |
| 308 provider_->RegisterConsumer(&consumer_); |
| 309 |
| 310 // Indicate that the device-global invalidation service connected. Verify that |
| 311 // that the consumer is informed about this. |
| 312 ConnectDeviceGlobalInvalidationService(); |
| 313 |
| 314 // Indicate that the device-global invalidation service has disconnected. |
| 315 // Verify that the consumer is informed about this. |
| 316 EXPECT_CALL(consumer_, OnInvalidationServiceSet(nullptr)).Times(1); |
| 317 device_invalidation_service_->OnInvalidatorStateChange( |
| 318 syncer::INVALIDATION_CREDENTIALS_REJECTED); |
| 319 Mock::VerifyAndClearExpectations(&consumer_); |
| 320 |
| 321 // Verify that the device-global invalidation service still exists. |
| 322 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 323 |
| 324 // Unregister the consumer. |
| 325 provider_->UnregisterConsumer(&consumer_); |
| 326 Mock::VerifyAndClearExpectations(&consumer_); |
| 327 } |
| 328 |
| 329 // A consumer is registered with the AffiliatedInvalidationServiceProvider. |
| 330 // Verifies that when a per-profile invalidation service belonging to an |
| 331 // affiliated user connects, it is made available to the consumer. |
| 332 TEST_F(AffiliatedInvalidationServiceProviderTest, |
| 333 UseAffiliatedProfileInvalidationService) { |
| 334 // Register a consumer. Verify that the consumer is not called back |
| 335 // immediately as no connected invalidation service exists yet. |
| 336 provider_->RegisterConsumer(&consumer_); |
| 337 |
| 338 // Verify that a device-global invalidation service has been created. |
| 339 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 340 |
| 341 // Log in as an affiliated user and indicate that the per-profile invalidation |
| 342 // service for this user connected. Verify that this invalidation service is |
| 343 // made available to the |consumer_| and the device-global invalidation |
| 344 // service is destroyed. |
| 345 LogInAsAffiliatedUserAndConnectInvalidationService(); |
| 346 |
| 347 // Indicate that the logged-in user's per-profile invalidation service |
| 348 // disconnected. Verify that the consumer is informed about this and a |
| 349 // device-global invalidation service is created. |
| 350 DisconnectPerProfileInvalidationService(); |
| 351 |
| 352 // Unregister the consumer. |
| 353 provider_->UnregisterConsumer(&consumer_); |
| 354 Mock::VerifyAndClearExpectations(&consumer_); |
| 355 } |
| 356 |
| 357 // A consumer is registered with the AffiliatedInvalidationServiceProvider. |
| 358 // Verifies that when a per-profile invalidation service belonging to an |
| 359 // unaffiliated user connects, it is ignored. |
| 360 TEST_F(AffiliatedInvalidationServiceProviderTest, |
| 361 DoNotUseUnaffiliatedProfileInvalidationService) { |
| 362 // Register a consumer. Verify that the consumer is not called back |
| 363 // immediately as no connected invalidation service exists yet. |
| 364 provider_->RegisterConsumer(&consumer_); |
| 365 |
| 366 // Verify that a device-global invalidation service has been created. |
| 367 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 368 |
| 369 // Log in as an unaffiliated user and indicate that the per-profile |
| 370 // invalidation service for this user connected. Verify that this invalidation |
| 371 // service is ignored and the device-global invalidation service is not |
| 372 // destroyed. |
| 373 LogInAsUnaffiliatedUserAndConnectInvalidationService(); |
| 374 |
| 375 // Unregister the consumer. |
| 376 provider_->UnregisterConsumer(&consumer_); |
| 377 Mock::VerifyAndClearExpectations(&consumer_); |
| 378 } |
| 379 |
| 380 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A |
| 381 // device-global invalidation service exists, is connected and is made available |
| 382 // to the consumer. Verifies that when a per-profile invalidation service |
| 383 // belonging to an affiliated user connects, it is made available to the |
| 384 // consumer instead and the device-global invalidation service is destroyed. |
| 385 TEST_F(AffiliatedInvalidationServiceProviderTest, |
| 386 SwitchToAffiliatedProfileInvalidationService) { |
| 387 // Register a consumer. Verify that the consumer is not called back |
| 388 // immediately as no connected invalidation service exists yet. |
| 389 provider_->RegisterConsumer(&consumer_); |
| 390 |
| 391 // Indicate that the device-global invalidation service connected. Verify that |
| 392 // that the consumer is informed about this. |
| 393 ConnectDeviceGlobalInvalidationService(); |
| 394 |
| 395 // Log in as an affiliated user and indicate that the per-profile invalidation |
| 396 // service for this user connected. Verify that this invalidation service is |
| 397 // made available to the |consumer_| and the device-global invalidation |
| 398 // service is destroyed. |
| 399 LogInAsAffiliatedUserAndConnectInvalidationService(); |
| 400 |
| 401 // Unregister the consumer. |
| 402 provider_->UnregisterConsumer(&consumer_); |
| 403 Mock::VerifyAndClearExpectations(&consumer_); |
| 404 } |
| 405 |
| 406 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A |
| 407 // device-global invalidation service exists, is connected and is made available |
| 408 // to the consumer. Verifies that when a per-profile invalidation service |
| 409 // belonging to an unaffiliated user connects, it is ignored and the |
| 410 // device-global invalidation service continues to be made available to the |
| 411 // consumer. |
| 412 TEST_F(AffiliatedInvalidationServiceProviderTest, |
| 413 DoNotSwitchToUnaffiliatedProfileInvalidationService) { |
| 414 // Register a consumer. Verify that the consumer is not called back |
| 415 // immediately as no connected invalidation service exists yet. |
| 416 provider_->RegisterConsumer(&consumer_); |
| 417 |
| 418 // Indicate that the device-global invalidation service connected. Verify that |
| 419 // that the consumer is informed about this. |
| 420 ConnectDeviceGlobalInvalidationService(); |
| 421 |
| 422 // Log in as an unaffiliated user and indicate that the per-profile |
| 423 // invalidation service for this user connected. Verify that this invalidation |
| 424 // service is ignored and the device-global invalidation service is not |
| 425 // destroyed. |
| 426 LogInAsUnaffiliatedUserAndConnectInvalidationService(); |
| 427 |
| 428 // Unregister the consumer. |
| 429 provider_->UnregisterConsumer(&consumer_); |
| 430 Mock::VerifyAndClearExpectations(&consumer_); |
| 431 } |
| 432 |
| 433 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A |
| 434 // per-profile invalidation service belonging to an affiliated user exists, is |
| 435 // connected and is made available to the consumer. Verifies that when the |
| 436 // per-profile invalidation service disconnects, a device-global invalidation |
| 437 // service is created. Further verifies that when the device-global invalidation |
| 438 // service connects, it is made available to the consumer. |
| 439 TEST_F(AffiliatedInvalidationServiceProviderTest, |
| 440 SwitchToDeviceInvalidationService) { |
| 441 // Register a consumer. Verify that the consumer is not called back |
| 442 // immediately as no connected invalidation service exists yet. |
| 443 provider_->RegisterConsumer(&consumer_); |
| 444 |
| 445 // Verify that a device-global invalidation service has been created. |
| 446 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 447 |
| 448 // Log in as an affiliated user and indicate that the per-profile invalidation |
| 449 // service for this user connected. Verify that this invalidation service is |
| 450 // made available to the |consumer_| and the device-global invalidation |
| 451 // service is destroyed. |
| 452 LogInAsAffiliatedUserAndConnectInvalidationService(); |
| 453 |
| 454 // Indicate that the logged-in user's per-profile invalidation service |
| 455 // disconnected. Verify that the consumer is informed about this and a |
| 456 // device-global invalidation service is created. |
| 457 DisconnectPerProfileInvalidationService(); |
| 458 |
| 459 // Indicate that the device-global invalidation service connected. Verify that |
| 460 // that the consumer is informed about this. |
| 461 ConnectDeviceGlobalInvalidationService(); |
| 462 |
| 463 // Unregister the consumer. |
| 464 provider_->UnregisterConsumer(&consumer_); |
| 465 Mock::VerifyAndClearExpectations(&consumer_); |
| 466 } |
| 467 |
| 468 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A |
| 469 // per-profile invalidation service belonging to a first affiliated user exists, |
| 470 // is connected and is made available to the consumer. A per-profile |
| 471 // invalidation service belonging to a second affiliated user also exists and is |
| 472 // connected. Verifies that when the per-profile invalidation service belonging |
| 473 // to the first user disconnects, the per-profile invalidation service belonging |
| 474 // to the second user is made available to the consumer instead. |
| 475 TEST_F(AffiliatedInvalidationServiceProviderTest, |
| 476 SwitchBetweenAffiliatedProfileInvalidationServices) { |
| 477 // Register a consumer. Verify that the consumer is not called back |
| 478 // immediately as no connected invalidation service exists yet. |
| 479 provider_->RegisterConsumer(&consumer_); |
| 480 |
| 481 // Verify that a device-global invalidation service has been created. |
| 482 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 483 |
| 484 // Log in as a first affiliated user and indicate that the per-profile |
| 485 // invalidation service for this user connected. Verify that this invalidation |
| 486 // service is made available to the |consumer_| and the device-global |
| 487 // invalidation service is destroyed. |
| 488 LogInAsAffiliatedUserAndConnectInvalidationService(); |
| 489 |
| 490 // Log in as a second affiliated user. |
| 491 Profile* second_profile = LogInAndReturnProfile(kAffiliatedUserID2); |
| 492 EXPECT_TRUE(second_profile); |
| 493 |
| 494 // Verify that the device-global invalidation service still does not exist. |
| 495 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); |
| 496 |
| 497 // Verify that a per-profile invalidation service for the second user has been |
| 498 // created. |
| 499 invalidation::FakeInvalidationService* second_profile_invalidation_service = |
| 500 GetProfileInvalidationService(second_profile); |
| 501 ASSERT_TRUE(second_profile_invalidation_service); |
| 502 |
| 503 // Indicate that the second user's per-profile invalidation service has |
| 504 // connected. Verify that the consumer is not called back. |
| 505 second_profile_invalidation_service->SetInvalidatorState( |
| 506 syncer::INVALIDATIONS_ENABLED); |
| 507 Mock::VerifyAndClearExpectations(&consumer_); |
| 508 |
| 509 // Indicate that the first user's per-profile invalidation service has |
| 510 // disconnected. Verify that the consumer is informed that the second user's |
| 511 // per-profile invalidation service should be used instead of the first |
| 512 // user's. |
| 513 EXPECT_CALL(consumer_, |
| 514 OnInvalidationServiceSet(second_profile_invalidation_service)) |
| 515 .Times(1); |
| 516 profile_invalidation_service_->SetInvalidatorState( |
| 517 syncer::INVALIDATION_CREDENTIALS_REJECTED); |
| 518 Mock::VerifyAndClearExpectations(&consumer_); |
| 519 |
| 520 // Verify that the device-global invalidation service still does not exist. |
| 521 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); |
| 522 |
| 523 // Unregister the consumer. |
| 524 provider_->UnregisterConsumer(&consumer_); |
| 525 Mock::VerifyAndClearExpectations(&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 second consumer registers, the |
| 531 // device-global invalidation service is made available to it as well. Further |
| 532 // verifies that when the first consumer unregisters, the device-global |
| 533 // invalidation service is not destroyed and remains available to the second |
| 534 // consumer. Further verifies that when the second consumer also unregisters, |
| 535 // the device-global invalidation service is destroyed. |
| 536 TEST_F(AffiliatedInvalidationServiceProviderTest, MultipleConsumers) { |
| 537 // Register a first consumer. Verify that the consumer is not called back |
| 538 // immediately as no connected invalidation service exists yet. |
| 539 provider_->RegisterConsumer(&consumer_); |
| 540 |
| 541 // Indicate that the device-global invalidation service connected. Verify that |
| 542 // that the consumer is informed about this. |
| 543 ConnectDeviceGlobalInvalidationService(); |
| 544 |
| 545 // Register a second consumer. Verify that the consumer is called back |
| 546 // immediately as a connected invalidation service is available. |
| 547 StrictMock<MockConsumer> second_consumer; |
| 548 EXPECT_CALL(second_consumer, |
| 549 OnInvalidationServiceSet(device_invalidation_service_)).Times(1); |
| 550 provider_->RegisterConsumer(&second_consumer); |
| 551 Mock::VerifyAndClearExpectations(&second_consumer); |
| 552 |
| 553 // Unregister the first consumer. |
| 554 provider_->UnregisterConsumer(&consumer_); |
| 555 |
| 556 // Verify that the device-global invalidation service still exists. |
| 557 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); |
| 558 |
| 559 // Unregister the second consumer. |
| 560 provider_->UnregisterConsumer(&second_consumer); |
| 561 |
| 562 // Verify that the device-global invalidation service has been destroyed. |
| 563 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); |
| 564 Mock::VerifyAndClearExpectations(&consumer_); |
| 565 Mock::VerifyAndClearExpectations(&second_consumer); |
| 566 } |
| 567 |
| 568 } // namespace policy |
OLD | NEW |