Chromium Code Reviews| 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::_; | |
| 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 | |
| 68 private: | |
| 69 DISALLOW_COPY_AND_ASSIGN(MockConsumer); | |
| 70 }; | |
| 71 | |
| 72 class AffiliatedInvalidationServiceProviderTest : public testing::Test { | |
| 73 public: | |
| 74 AffiliatedInvalidationServiceProviderTest(); | |
| 75 | |
| 76 // testing::Test: | |
| 77 virtual void SetUp() override; | |
| 78 virtual void TearDown() override; | |
| 79 | |
| 80 // Ownership is not passed. The Profile is owned by the global ProfileManager. | |
| 81 Profile* LogInAndReturnProfile(const std::string& user_id); | |
| 82 | |
| 83 invalidation::FakeInvalidationService* GetProfileInvalidationService( | |
| 84 Profile* profile); | |
| 85 | |
| 86 protected: | |
| 87 scoped_ptr<AffiliatedInvalidationServiceProvider> provider_; | |
| 88 | |
| 89 private: | |
| 90 content::TestBrowserThreadBundle thread_bundle_; | |
| 91 chromeos::FakeUserManager* fake_user_manager_; | |
| 92 chromeos::ScopedUserManagerEnabler user_manager_enabler_; | |
| 93 ScopedStubEnterpriseInstallAttributes install_attributes_; | |
| 94 scoped_ptr<chromeos::ScopedTestDeviceSettingsService> | |
| 95 test_device_settings_service_; | |
| 96 scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_; | |
| 97 TestingProfileManager profile_manager_; | |
| 98 }; | |
| 99 | |
| 100 MockConsumer::MockConsumer() { | |
| 101 } | |
| 102 | |
| 103 MockConsumer::~MockConsumer() { | |
| 104 } | |
| 105 | |
| 106 AffiliatedInvalidationServiceProviderTest:: | |
| 107 AffiliatedInvalidationServiceProviderTest() | |
| 108 : fake_user_manager_(new chromeos::FakeUserManager), | |
| 109 user_manager_enabler_(fake_user_manager_), | |
| 110 install_attributes_("example.com", | |
| 111 "user@example.com", | |
| 112 "device_id", | |
| 113 DEVICE_MODE_ENTERPRISE), | |
| 114 profile_manager_(TestingBrowserProcess::GetGlobal()) { | |
| 115 } | |
| 116 | |
| 117 void AffiliatedInvalidationServiceProviderTest::SetUp() { | |
| 118 chromeos::SystemSaltGetter::Initialize(); | |
| 119 chromeos::DBusThreadManager::Initialize(); | |
| 120 chromeos::DeviceOAuth2TokenServiceFactory::Initialize(); | |
| 121 ASSERT_TRUE(profile_manager_.SetUp()); | |
| 122 | |
| 123 test_device_settings_service_.reset(new | |
| 124 chromeos::ScopedTestDeviceSettingsService); | |
| 125 test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings); | |
| 126 | |
| 127 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> | |
| 128 RegisterTestingFactory(BuildProfileInvalidationProvider); | |
| 129 | |
| 130 provider_.reset(new AffiliatedInvalidationServiceProvider); | |
| 131 } | |
| 132 | |
| 133 void AffiliatedInvalidationServiceProviderTest::TearDown() { | |
| 134 provider_.reset(); | |
| 135 | |
| 136 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> | |
| 137 RegisterTestingFactory(nullptr); | |
| 138 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown(); | |
| 139 chromeos::DBusThreadManager::Shutdown(); | |
| 140 chromeos::SystemSaltGetter::Shutdown(); | |
| 141 } | |
| 142 | |
| 143 Profile* AffiliatedInvalidationServiceProviderTest::LogInAndReturnProfile( | |
| 144 const std::string& user_id) { | |
| 145 fake_user_manager_->AddUser(user_id); | |
| 146 Profile* profile = profile_manager_.CreateTestingProfile(user_id); | |
| 147 content::NotificationService::current()->Notify( | |
| 148 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
| 149 content::NotificationService::AllSources(), | |
| 150 content::Details<Profile>(profile)); | |
| 151 return profile; | |
| 152 } | |
| 153 | |
| 154 invalidation::FakeInvalidationService* | |
| 155 AffiliatedInvalidationServiceProviderTest::GetProfileInvalidationService( | |
| 156 Profile* profile) { | |
| 157 invalidation::ProfileInvalidationProvider* invalidation_provider = | |
| 158 static_cast<invalidation::ProfileInvalidationProvider*>( | |
| 159 invalidation::ProfileInvalidationProviderFactory::GetInstance()-> | |
| 160 GetServiceForBrowserContext(profile, false)); | |
| 161 if (!invalidation_provider) | |
| 162 return nullptr; | |
| 163 return static_cast<invalidation::FakeInvalidationService*>( | |
| 164 invalidation_provider->GetInvalidationService()); | |
| 165 } | |
| 166 | |
| 167 // No consumers are registered with the AffiliatedInvalidationServiceProvider. | |
| 168 // Verifies that no device-global invalidation service is created, whether an | |
| 169 // affiliated user is logged in or not. | |
| 170 TEST_F(AffiliatedInvalidationServiceProviderTest, NoConsumers) { | |
| 171 // Verify that no device-global invalidation service has been created. | |
| 172 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 173 | |
| 174 // Log in as an affiliated user. | |
| 175 EXPECT_TRUE(LogInAndReturnProfile(kAffiliatedUserID1)); | |
| 176 | |
| 177 // Verify that no device-global invalidation service has been created. | |
| 178 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 179 } | |
| 180 | |
| 181 // A consumer is registered with the AffiliatedInvalidationServiceProvider. | |
| 182 // Verifies that when no per-profile invalidation service belonging to an | |
| 183 // affiliated user is available, a device-global invalidation service is | |
| 184 // created. Further verifies that when the device-global invalidation service | |
| 185 // connects, it is made available to the consumer. | |
| 186 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 187 UseDeviceInvalidationService) { | |
| 188 // Register a consumer. Verify that the consumer is not called back | |
| 189 // immediately as no connected invalidation service exists yet. | |
| 190 MockConsumer consumer; | |
| 191 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 192 provider_->RegisterConsumer(&consumer); | |
| 193 Mock::VerifyAndClearExpectations(&consumer); | |
| 194 | |
| 195 // Verify that a device-global invalidation service has been created. | |
| 196 invalidation::TiclInvalidationService* device_invalidation_service = | |
| 197 provider_->GetDeviceInvalidationServiceForTest(); | |
| 198 ASSERT_TRUE(device_invalidation_service); | |
| 199 | |
| 200 // Indicate that the device-global invalidation service has connected. Verify | |
| 201 // that the consumer is informed about this. | |
| 202 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
Are these expectations actually necessary, i.e. do
bartfab (slow)
2015/01/20 13:22:47
Regular Mock just prints a warning that there was
| |
| 203 EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service)) | |
| 204 .Times(1); | |
| 205 device_invalidation_service->OnInvalidatorStateChange( | |
| 206 syncer::INVALIDATIONS_ENABLED); | |
| 207 Mock::VerifyAndClearExpectations(&consumer); | |
| 208 | |
| 209 // Indicate that the device-global invalidation service has disconnected. | |
| 210 // Verify that the consumer is informed about this. | |
| 211 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 212 EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)).Times(1); | |
| 213 device_invalidation_service->OnInvalidatorStateChange( | |
| 214 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 215 Mock::VerifyAndClearExpectations(&consumer); | |
| 216 | |
| 217 // Verify that the device-global invalidation service still exists. | |
| 218 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 219 | |
| 220 // Unregister the consumer. | |
| 221 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 222 provider_->UnregisterConsumer(&consumer); | |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
Let's put a Mock::VerifyAndClearExpectations here
bartfab (slow)
2015/01/20 13:22:47
Done.
| |
| 223 } | |
| 224 | |
| 225 // A consumer is registered with the AffiliatedInvalidationServiceProvider. | |
| 226 // Verifies that when a per-profile invalidation service belonging to an | |
| 227 // affiliated user connects, it is made available to the consumer. | |
| 228 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 229 UseAffiliatedProfileInvalidationService) { | |
| 230 // Register a consumer. Verify that the consumer is not called back | |
| 231 // immediately as no connected invalidation service exists yet. | |
| 232 MockConsumer consumer; | |
| 233 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 234 provider_->RegisterConsumer(&consumer); | |
| 235 | |
| 236 // Verify that a device-global invalidation service has been created. | |
| 237 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 238 | |
| 239 // Log in as an affiliated user. | |
| 240 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 241 EXPECT_TRUE(profile); | |
| 242 Mock::VerifyAndClearExpectations(&consumer); | |
| 243 | |
| 244 // Verify that a per-profile invalidation service has been created. | |
| 245 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 246 GetProfileInvalidationService(profile); | |
| 247 ASSERT_TRUE(profile_invalidation_service); | |
| 248 | |
| 249 // Verify that the device-global invalidation service still exists. | |
| 250 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 251 | |
| 252 // Indicate that the per-profile invalidation service has connected. Verify | |
| 253 // that the consumer is informed about this. | |
| 254 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 255 EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service)) | |
| 256 .Times(1); | |
| 257 profile_invalidation_service->SetInvalidatorState( | |
| 258 syncer::INVALIDATIONS_ENABLED); | |
| 259 Mock::VerifyAndClearExpectations(&consumer); | |
| 260 | |
| 261 // Verify that the device-global invalidation service has been destroyed. | |
| 262 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 263 | |
| 264 // Indicate that the per-profile invalidation service has disconnected. Verify | |
| 265 // that the consumer is informed about this. | |
| 266 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 267 EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)).Times(1); | |
| 268 profile_invalidation_service->SetInvalidatorState( | |
| 269 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 270 Mock::VerifyAndClearExpectations(&consumer); | |
| 271 | |
| 272 // Verify that a device-global invalidation service has been created. | |
| 273 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 274 | |
| 275 // Unregister the consumer. | |
| 276 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 277 provider_->UnregisterConsumer(&consumer); | |
| 278 } | |
| 279 | |
| 280 // A consumer is registered with the AffiliatedInvalidationServiceProvider. | |
| 281 // Verifies that when a per-profile invalidation service belonging to an | |
| 282 // unaffiliated user connects, it is ignored. | |
| 283 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 284 DoNotUseUnaffiliatedProfileInvalidationService) { | |
| 285 // Register a consumer. Verify that the consumer is not called back | |
| 286 // immediately as no connected invalidation service exists yet. | |
| 287 MockConsumer consumer; | |
| 288 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 289 provider_->RegisterConsumer(&consumer); | |
| 290 | |
| 291 // Verify that a device-global invalidation service has been created. | |
| 292 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 293 | |
| 294 // Log in as an unaffiliated user. | |
| 295 Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID); | |
| 296 EXPECT_TRUE(profile); | |
| 297 | |
| 298 // Verify that a per-profile invalidation service has been created. | |
| 299 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 300 GetProfileInvalidationService(profile); | |
| 301 ASSERT_TRUE(profile_invalidation_service); | |
| 302 | |
| 303 // Verify that the device-global invalidation service still exists. | |
| 304 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 305 | |
| 306 // Indicate that the per-profile invalidation service has connected. Verify | |
| 307 // that the consumer is not called back. | |
| 308 profile_invalidation_service->SetInvalidatorState( | |
| 309 syncer::INVALIDATIONS_ENABLED); | |
| 310 | |
| 311 // Verify that the device-global invalidation service still exists. | |
| 312 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 313 | |
| 314 // Unregister the consumer. | |
| 315 provider_->UnregisterConsumer(&consumer); | |
| 316 } | |
| 317 | |
| 318 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 319 // device-global invalidation service exists, is connected and is made available | |
| 320 // to the consumer. Verifies that when a per-profile invalidation service | |
| 321 // belonging to an affiliated user connects, it is made available to the | |
| 322 // consumer instead and the device-global invalidation service is destroyed. | |
| 323 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 324 SwitchToAffiliatedProfileInvalidationService) { | |
| 325 // Register a consumer. Verify that the consumer is not called back | |
| 326 // immediately as no connected invalidation service exists yet. | |
| 327 MockConsumer consumer; | |
| 328 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 329 provider_->RegisterConsumer(&consumer); | |
| 330 Mock::VerifyAndClearExpectations(&consumer); | |
| 331 | |
| 332 // Verify that a device-global invalidation service has been created. | |
| 333 invalidation::TiclInvalidationService* device_invalidation_service = | |
| 334 provider_->GetDeviceInvalidationServiceForTest(); | |
| 335 ASSERT_TRUE(device_invalidation_service); | |
| 336 | |
| 337 // Indicate that the device-global invalidation service has connected. Verify | |
| 338 // that the consumer is informed about this. | |
| 339 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 340 EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service)) | |
| 341 .Times(1); | |
| 342 device_invalidation_service->OnInvalidatorStateChange( | |
| 343 syncer::INVALIDATIONS_ENABLED); | |
| 344 Mock::VerifyAndClearExpectations(&consumer); | |
| 345 | |
| 346 // Log in as an affiliated user. | |
| 347 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 348 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 349 EXPECT_TRUE(profile); | |
| 350 Mock::VerifyAndClearExpectations(&consumer); | |
| 351 | |
| 352 // Verify that a per-profile invalidation service has been created. | |
| 353 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 354 GetProfileInvalidationService(profile); | |
| 355 ASSERT_TRUE(profile_invalidation_service); | |
| 356 | |
| 357 // Verify that the device-global invalidation service still exists. | |
| 358 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 359 | |
| 360 // Indicate that the per-profile invalidation service has connected. Verify | |
| 361 // that the consumer is informed that the device-global invalidation service | |
| 362 // is no longer to be used and the per-profile invalidation service is to be | |
| 363 // used instead. | |
| 364 Sequence sequence; | |
| 365 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 366 EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)) | |
| 367 .Times(1) | |
| 368 .InSequence(sequence); | |
| 369 EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service)) | |
| 370 .Times(1) | |
| 371 .InSequence(sequence); | |
| 372 profile_invalidation_service->SetInvalidatorState( | |
| 373 syncer::INVALIDATIONS_ENABLED); | |
| 374 Mock::VerifyAndClearExpectations(&consumer); | |
| 375 | |
| 376 // Verify that the device-global invalidation service has been destroyed. | |
| 377 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 378 | |
| 379 // Unregister the consumer. | |
| 380 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 381 provider_->UnregisterConsumer(&consumer); | |
| 382 } | |
| 383 | |
| 384 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 385 // device-global invalidation service exists, is connected and is made available | |
| 386 // to the consumer. Verifies that when a per-profile invalidation service | |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
This and all tests below have a common setup seque
bartfab (slow)
2015/01/20 13:22:47
Done.
| |
| 387 // belonging to an unaffiliated user connects, it is ignored and the | |
| 388 // device-global invalidation service continues to be made available to the | |
| 389 // consumer. | |
| 390 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 391 DoNotSwitchToUnaffiliatedProfileInvalidationService) { | |
| 392 // Register a consumer. Verify that the consumer is not called back | |
| 393 // immediately as no connected invalidation service exists yet. | |
| 394 MockConsumer consumer; | |
| 395 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 396 provider_->RegisterConsumer(&consumer); | |
| 397 Mock::VerifyAndClearExpectations(&consumer); | |
| 398 | |
| 399 // Verify that a device-global invalidation service has been created. | |
| 400 invalidation::TiclInvalidationService* device_invalidation_service = | |
| 401 provider_->GetDeviceInvalidationServiceForTest(); | |
| 402 ASSERT_TRUE(device_invalidation_service); | |
| 403 | |
| 404 // Indicate that the device-global invalidation service has connected. Verify | |
| 405 // that the consumer is informed about this. | |
| 406 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 407 EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service)) | |
| 408 .Times(1); | |
| 409 device_invalidation_service->OnInvalidatorStateChange( | |
| 410 syncer::INVALIDATIONS_ENABLED); | |
| 411 Mock::VerifyAndClearExpectations(&consumer); | |
| 412 | |
| 413 // Log in as an unaffiliated user. | |
| 414 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 415 Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID); | |
| 416 EXPECT_TRUE(profile); | |
| 417 | |
| 418 // Verify that a per-profile invalidation service has been created. | |
| 419 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 420 GetProfileInvalidationService(profile); | |
| 421 ASSERT_TRUE(profile_invalidation_service); | |
| 422 | |
| 423 // Verify that the device-global invalidation service still exists. | |
| 424 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 425 | |
| 426 // Indicate that the per-profile invalidation service has connected. Verify | |
| 427 // that the consumer is not called back. | |
| 428 profile_invalidation_service->SetInvalidatorState( | |
| 429 syncer::INVALIDATIONS_ENABLED); | |
| 430 | |
| 431 // Verify that the device-global invalidation service still exists. | |
| 432 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 433 | |
| 434 // Unregister the consumer. | |
| 435 provider_->UnregisterConsumer(&consumer); | |
| 436 } | |
| 437 | |
| 438 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 439 // per-profile invalidation service belonging to an affiliated user exists, is | |
| 440 // connected and is made available to the consumer. Verifies that when the | |
| 441 // per-profile invalidation service disconnects, a device-global invalidation | |
| 442 // service is created. Further verifies that when the device-global invalidation | |
| 443 // service connects, it is made available to the consumer. | |
| 444 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 445 SwitchToDeviceInvalidationService) { | |
| 446 // Register a consumer. Verify that the consumer is not called back | |
| 447 // immediately as no connected invalidation service exists yet. | |
| 448 MockConsumer consumer; | |
| 449 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 450 provider_->RegisterConsumer(&consumer); | |
| 451 | |
| 452 // Verify that a device-global invalidation service has been created. | |
| 453 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 454 | |
| 455 // Log in as an affiliated user. | |
| 456 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 457 EXPECT_TRUE(profile); | |
| 458 Mock::VerifyAndClearExpectations(&consumer); | |
| 459 | |
| 460 // Verify that a per-profile invalidation service has been created. | |
| 461 invalidation::FakeInvalidationService* profile_invalidation_service = | |
| 462 GetProfileInvalidationService(profile); | |
| 463 ASSERT_TRUE(profile_invalidation_service); | |
| 464 | |
| 465 // Verify that the device-global invalidation service still exists. | |
| 466 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 467 | |
| 468 // Indicate that the per-profile invalidation service has connected. Verify | |
| 469 // that the consumer is informed about this. | |
| 470 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 471 EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service)) | |
| 472 .Times(1); | |
| 473 profile_invalidation_service->SetInvalidatorState( | |
| 474 syncer::INVALIDATIONS_ENABLED); | |
| 475 Mock::VerifyAndClearExpectations(&consumer); | |
| 476 | |
| 477 // Verify that the device-global invalidation service has been destroyed. | |
| 478 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 479 | |
| 480 // Indicate that the per-profile invalidation service has disconnected. Verify | |
| 481 // that the consumer is informed about this. | |
| 482 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 483 EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)).Times(1); | |
| 484 profile_invalidation_service->SetInvalidatorState( | |
| 485 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 486 Mock::VerifyAndClearExpectations(&consumer); | |
| 487 | |
| 488 // Verify that a device-global invalidation service has been created. | |
| 489 invalidation::TiclInvalidationService* device_invalidation_service = | |
| 490 provider_->GetDeviceInvalidationServiceForTest(); | |
| 491 ASSERT_TRUE(device_invalidation_service); | |
| 492 | |
| 493 // Indicate that the device-global invalidation service has connected. Verify | |
| 494 // that the consumer is informed about this. | |
| 495 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 496 EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service)) | |
| 497 .Times(1); | |
| 498 device_invalidation_service->OnInvalidatorStateChange( | |
| 499 syncer::INVALIDATIONS_ENABLED); | |
| 500 Mock::VerifyAndClearExpectations(&consumer); | |
| 501 | |
| 502 // Unregister the consumer. | |
| 503 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 504 provider_->UnregisterConsumer(&consumer); | |
| 505 } | |
| 506 | |
| 507 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 508 // per-profile invalidation service belonging to a first affiliated user exists, | |
| 509 // is connected and is made available to the consumer. A per-profile | |
| 510 // invalidation service belonging to a second affiliated user also exists and is | |
| 511 // connected. Verifies that when the per-profile invalidation service belonging | |
| 512 // to the first user disconnects, the per-profile invalidation service belonging | |
| 513 // to the second user is made available to the consumer instead. | |
| 514 TEST_F(AffiliatedInvalidationServiceProviderTest, | |
| 515 SwitchBetweenAffiliatedProfileInvalidationServices) { | |
| 516 // Register a consumer. Verify that the consumer is not called back | |
| 517 // immediately as no connected invalidation service exists yet. | |
| 518 MockConsumer consumer; | |
| 519 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 520 provider_->RegisterConsumer(&consumer); | |
| 521 | |
| 522 // Verify that a device-global invalidation service has been created. | |
| 523 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 524 | |
| 525 // Log in as a first affiliated user. | |
| 526 Profile* profile_1 = LogInAndReturnProfile(kAffiliatedUserID1); | |
| 527 EXPECT_TRUE(profile_1); | |
| 528 Mock::VerifyAndClearExpectations(&consumer); | |
| 529 | |
| 530 // Verify that a per-profile invalidation service for the first user has been | |
| 531 // created. | |
| 532 invalidation::FakeInvalidationService* profile_1_invalidation_service = | |
| 533 GetProfileInvalidationService(profile_1); | |
| 534 ASSERT_TRUE(profile_1_invalidation_service); | |
| 535 | |
| 536 // Verify that the device-global invalidation service still exists. | |
| 537 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 538 | |
| 539 // Indicate that the first user's per-profile invalidation service has | |
| 540 // connected. Verify that the consumer is informed about this. | |
| 541 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 542 EXPECT_CALL(consumer, | |
| 543 OnInvalidationServiceSet(profile_1_invalidation_service)) | |
| 544 .Times(1); | |
| 545 profile_1_invalidation_service->SetInvalidatorState( | |
| 546 syncer::INVALIDATIONS_ENABLED); | |
| 547 Mock::VerifyAndClearExpectations(&consumer); | |
| 548 | |
| 549 // Verify that the device-global invalidation service has been destroyed. | |
| 550 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 551 | |
| 552 // Log in as a second affiliated user. | |
| 553 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 554 Profile* profile_2 = LogInAndReturnProfile(kAffiliatedUserID2); | |
| 555 EXPECT_TRUE(profile_2); | |
| 556 | |
| 557 // Verify that the device-global invalidation service still does not exist. | |
| 558 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 559 | |
| 560 // Verify that a per-profile invalidation service for the second user has been | |
| 561 // created. | |
| 562 invalidation::FakeInvalidationService* profile_2_invalidation_service = | |
| 563 GetProfileInvalidationService(profile_2); | |
| 564 ASSERT_TRUE(profile_2_invalidation_service); | |
| 565 | |
| 566 // Indicate that the second user's per-profile invalidation service has | |
| 567 // connected. Verify that the consumer is not called back. | |
| 568 profile_2_invalidation_service->SetInvalidatorState( | |
| 569 syncer::INVALIDATIONS_ENABLED); | |
| 570 Mock::VerifyAndClearExpectations(&consumer); | |
| 571 | |
| 572 // Indicate that the first user's per-profile invalidation service has | |
| 573 // disconnected. Verify that the consumer is informed that the first user's | |
| 574 // per-profile invalidation service is no longer to be used and the second | |
| 575 // user's per-profile invalidation service is to be used instead. | |
| 576 Sequence sequence; | |
| 577 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 578 EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)) | |
| 579 .Times(1) | |
| 580 .InSequence(sequence); | |
| 581 EXPECT_CALL(consumer, | |
| 582 OnInvalidationServiceSet(profile_2_invalidation_service)) | |
| 583 .Times(1) | |
| 584 .InSequence(sequence); | |
| 585 profile_1_invalidation_service->SetInvalidatorState( | |
| 586 syncer::INVALIDATION_CREDENTIALS_REJECTED); | |
| 587 Mock::VerifyAndClearExpectations(&consumer); | |
| 588 | |
| 589 // Verify that the device-global invalidation service still does not exist. | |
| 590 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 591 | |
| 592 // Unregister the consumer. | |
| 593 EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0); | |
| 594 provider_->UnregisterConsumer(&consumer); | |
| 595 } | |
| 596 | |
| 597 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A | |
| 598 // device-global invalidation service exists, is connected and is made available | |
| 599 // to the consumer. Verifies that when a second consumer registers, the | |
| 600 // device-global invalidation service is made available to it as well. Further | |
| 601 // verifies that when the first consumer unregisters, the device-global | |
| 602 // invalidation service is not destroyed and remains available to the second | |
| 603 // consumer. Further verifies that when the second consumer also unregisters, | |
| 604 // the device-global invalidation service is destroyed. | |
| 605 TEST_F(AffiliatedInvalidationServiceProviderTest, MultipleConsumers) { | |
| 606 // Register a first consumer. Verify that the consumer is not called back | |
| 607 // immediately as no connected invalidation service exists yet. | |
| 608 MockConsumer consumer_1; | |
| 609 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0); | |
| 610 provider_->RegisterConsumer(&consumer_1); | |
| 611 Mock::VerifyAndClearExpectations(&consumer_1); | |
| 612 | |
| 613 // Verify that a device-global invalidation service has been created. | |
| 614 invalidation::TiclInvalidationService* device_invalidation_service = | |
| 615 provider_->GetDeviceInvalidationServiceForTest(); | |
| 616 ASSERT_TRUE(device_invalidation_service); | |
| 617 | |
| 618 // Indicate that the device-global invalidation service has connected. Verify | |
| 619 // that the first consumer is informed about this. | |
| 620 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0); | |
| 621 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(device_invalidation_service)) | |
| 622 .Times(1); | |
| 623 device_invalidation_service->OnInvalidatorStateChange( | |
| 624 syncer::INVALIDATIONS_ENABLED); | |
| 625 Mock::VerifyAndClearExpectations(&consumer_1); | |
| 626 EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0); | |
| 627 | |
| 628 // Register a second consumer. Verify that the consumer is called back | |
| 629 // immediately as a connected invalidation service is available. | |
| 630 MockConsumer consumer_2; | |
| 631 EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0); | |
| 632 EXPECT_CALL(consumer_2, OnInvalidationServiceSet(device_invalidation_service)) | |
| 633 .Times(1); | |
| 634 provider_->RegisterConsumer(&consumer_2); | |
| 635 Mock::VerifyAndClearExpectations(&consumer_2); | |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
nit: Put a blank line here, the expectation in the
bartfab (slow)
2015/01/20 13:22:47
Done.
| |
| 636 EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0); | |
| 637 | |
| 638 // Unregister the first consumer. | |
| 639 provider_->UnregisterConsumer(&consumer_1); | |
| 640 | |
| 641 // Verify that the device-global invalidation service still exists. | |
| 642 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 643 | |
| 644 // Unregister the second consumer. | |
| 645 provider_->UnregisterConsumer(&consumer_2); | |
| 646 | |
| 647 // Verify that the device-global invalidation service has been destroyed. | |
| 648 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest()); | |
| 649 } | |
| 650 | |
| 651 } // namespace policy | |
| OLD | NEW |