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.
5 #include "chrome/browser/chromeos/policy/affiliated_invalidation_service_provider_impl.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
12 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
13 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
14 #include "chrome/browser/chromeos/settings/cros_settings.h"
15 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
16 #include "chrome/browser/chromeos/settings/device_settings_service.h"
17 #include "chrome/browser/invalidation/fake_invalidation_service.h"
18 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_profile_manager.h"
22 #include "chromeos/cryptohome/system_salt_getter.h"
23 #include "chromeos/dbus/dbus_thread_manager.h"
24 #include "components/invalidation/impl/fake_invalidation_handler.h"
25 #include "components/invalidation/impl/profile_invalidation_provider.h"
26 #include "components/invalidation/impl/ticl_invalidation_service.h"
27 #include "components/invalidation/public/invalidation_service.h"
28 #include "components/invalidation/public/invalidator_state.h"
29 #include "components/keyed_service/core/keyed_service.h"
30 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
31 #include "content/public/browser/browser_context.h"
32 #include "content/public/browser/notification_details.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "testing/gtest/include/gtest/gtest.h"
41 const char kAffiliatedUserID1
[] = "test_1@example.com";
42 const char kAffiliatedUserID2
[] = "test_2@example.com";
43 const char kUnaffiliatedUserID
[] = "test@other_domain.test";
45 scoped_ptr
<KeyedService
> BuildProfileInvalidationProvider(
46 content::BrowserContext
* context
) {
47 scoped_ptr
<invalidation::FakeInvalidationService
> invalidation_service(
48 new invalidation::FakeInvalidationService
);
49 invalidation_service
->SetInvalidatorState(
50 syncer::TRANSIENT_INVALIDATION_ERROR
);
51 return make_scoped_ptr(new invalidation::ProfileInvalidationProvider(
52 invalidation_service
.Pass()));
57 // A simple AffiliatedInvalidationServiceProvider::Consumer that registers a
58 // syncer::FakeInvalidationHandler with the invalidation::InvalidationService
59 // that is currently being made available.
60 class FakeConsumer
: public AffiliatedInvalidationServiceProvider::Consumer
{
62 explicit FakeConsumer(AffiliatedInvalidationServiceProviderImpl
* provider
);
63 ~FakeConsumer() override
;
65 // AffiliatedInvalidationServiceProvider::Consumer:
66 void OnInvalidationServiceSet(
67 invalidation::InvalidationService
* invalidation_service
) override
;
69 int GetAndClearInvalidationServiceSetCount();
70 const invalidation::InvalidationService
* GetInvalidationService() const;
73 AffiliatedInvalidationServiceProviderImpl
* provider_
;
74 syncer::FakeInvalidationHandler invalidation_handler_
;
76 int invalidation_service_set_count_
= 0;
77 invalidation::InvalidationService
* invalidation_service_
= nullptr;
79 DISALLOW_COPY_AND_ASSIGN(FakeConsumer
);
82 class AffiliatedInvalidationServiceProviderImplTest
: public testing::Test
{
84 AffiliatedInvalidationServiceProviderImplTest();
87 void SetUp() override
;
88 void TearDown() override
;
90 // Ownership is not passed. The Profile is owned by the global ProfileManager.
91 Profile
* LogInAndReturnProfile(const std::string
& user_id
);
93 // Logs in as an affiliated user and indicates that the per-profile
94 // invalidation service for this user connected. Verifies that this
95 // invalidation service is made available to the |consumer_| and the
96 // device-global invalidation service is destroyed.
97 void LogInAsAffiliatedUserAndConnectInvalidationService();
99 // Logs in as an unaffiliated user and indicates that the per-profile
100 // invalidation service for this user connected. Verifies that this
101 // invalidation service is ignored and the device-global invalidation service
103 void LogInAsUnaffiliatedUserAndConnectInvalidationService();
105 // Indicates that the device-global invalidation service connected. Verifies
106 // that the |consumer_| is informed about this.
107 void ConnectDeviceGlobalInvalidationService();
109 // Indicates that the logged-in user's per-profile invalidation service
110 // disconnected. Verifies that the |consumer_| is informed about this and a
111 // device-global invalidation service is created.
112 void DisconnectPerProfileInvalidationService();
114 invalidation::FakeInvalidationService
* GetProfileInvalidationService(
119 scoped_ptr
<AffiliatedInvalidationServiceProviderImpl
> provider_
;
120 scoped_ptr
<FakeConsumer
> consumer_
;
121 invalidation::TiclInvalidationService
* device_invalidation_service_
;
122 invalidation::FakeInvalidationService
* profile_invalidation_service_
;
125 content::TestBrowserThreadBundle thread_bundle_
;
126 chromeos::FakeChromeUserManager
* fake_user_manager_
;
127 chromeos::ScopedUserManagerEnabler user_manager_enabler_
;
128 ScopedStubEnterpriseInstallAttributes install_attributes_
;
129 scoped_ptr
<chromeos::ScopedTestDeviceSettingsService
>
130 test_device_settings_service_
;
131 scoped_ptr
<chromeos::ScopedTestCrosSettings
> test_cros_settings_
;
132 TestingProfileManager profile_manager_
;
135 FakeConsumer::FakeConsumer(AffiliatedInvalidationServiceProviderImpl
* provider
)
136 : provider_(provider
) {
137 provider_
->RegisterConsumer(this);
140 FakeConsumer::~FakeConsumer() {
141 if (invalidation_service_
) {
142 invalidation_service_
->UnregisterInvalidationHandler(
143 &invalidation_handler_
);
145 provider_
->UnregisterConsumer(this);
147 EXPECT_EQ(0, invalidation_service_set_count_
);
150 void FakeConsumer::OnInvalidationServiceSet(
151 invalidation::InvalidationService
* invalidation_service
) {
152 ++invalidation_service_set_count_
;
154 if (invalidation_service_
) {
155 invalidation_service_
->UnregisterInvalidationHandler(
156 &invalidation_handler_
);
159 invalidation_service_
= invalidation_service
;
161 if (invalidation_service_
) {
162 // Regression test for http://crbug.com/455504: The |invalidation_service|
163 // was sometimes destroyed without notifying consumers and giving them a
164 // chance to unregister their invalidation handlers. Register an
165 // invalidation handler so that |invalidation_service| CHECK()s in its
166 // destructor if this regresses.
167 invalidation_service_
->RegisterInvalidationHandler(&invalidation_handler_
);
171 int FakeConsumer::GetAndClearInvalidationServiceSetCount() {
172 const int invalidation_service_set_count
= invalidation_service_set_count_
;
173 invalidation_service_set_count_
= 0;
174 return invalidation_service_set_count
;
177 const invalidation::InvalidationService
*
178 FakeConsumer::GetInvalidationService() const {
179 return invalidation_service_
;
182 AffiliatedInvalidationServiceProviderImplTest::
183 AffiliatedInvalidationServiceProviderImplTest()
184 : device_invalidation_service_(nullptr),
185 profile_invalidation_service_(nullptr),
186 fake_user_manager_(new chromeos::FakeChromeUserManager
),
187 user_manager_enabler_(fake_user_manager_
),
188 install_attributes_("example.com",
191 DEVICE_MODE_ENTERPRISE
),
192 profile_manager_(TestingBrowserProcess::GetGlobal()) {
195 void AffiliatedInvalidationServiceProviderImplTest::SetUp() {
196 chromeos::SystemSaltGetter::Initialize();
197 chromeos::DBusThreadManager::Initialize();
198 ASSERT_TRUE(profile_manager_
.SetUp());
200 test_device_settings_service_
.reset(new
201 chromeos::ScopedTestDeviceSettingsService
);
202 test_cros_settings_
.reset(new chromeos::ScopedTestCrosSettings
);
203 chromeos::DeviceOAuth2TokenServiceFactory::Initialize();
205 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
206 RegisterTestingFactory(BuildProfileInvalidationProvider
);
208 provider_
.reset(new AffiliatedInvalidationServiceProviderImpl
);
211 void AffiliatedInvalidationServiceProviderImplTest::TearDown() {
213 provider_
->Shutdown();
216 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
217 RegisterTestingFactory(nullptr);
218 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown();
219 chromeos::DBusThreadManager::Shutdown();
220 chromeos::SystemSaltGetter::Shutdown();
223 Profile
* AffiliatedInvalidationServiceProviderImplTest::LogInAndReturnProfile(
224 const std::string
& user_id
) {
225 fake_user_manager_
->AddUser(user_id
);
226 Profile
* profile
= profile_manager_
.CreateTestingProfile(user_id
);
227 content::NotificationService::current()->Notify(
228 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED
,
229 content::NotificationService::AllSources(),
230 content::Details
<Profile
>(profile
));
234 void AffiliatedInvalidationServiceProviderImplTest::
235 LogInAsAffiliatedUserAndConnectInvalidationService() {
236 // Log in as an affiliated user.
237 Profile
* profile
= LogInAndReturnProfile(kAffiliatedUserID1
);
238 EXPECT_TRUE(profile
);
240 // Verify that a per-profile invalidation service has been created.
241 profile_invalidation_service_
=
242 GetProfileInvalidationService(profile
, false /* create */);
243 ASSERT_TRUE(profile_invalidation_service_
);
245 // Verify that the device-global invalidation service still exists.
246 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
248 // Indicate that the per-profile invalidation service has connected. Verify
249 // that the consumer is informed about this.
250 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
251 profile_invalidation_service_
->SetInvalidatorState(
252 syncer::INVALIDATIONS_ENABLED
);
253 EXPECT_EQ(1, consumer_
->GetAndClearInvalidationServiceSetCount());
254 EXPECT_EQ(profile_invalidation_service_
, consumer_
->GetInvalidationService());
256 // Verify that the device-global invalidation service has been destroyed.
257 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
260 void AffiliatedInvalidationServiceProviderImplTest::
261 LogInAsUnaffiliatedUserAndConnectInvalidationService() {
262 // Log in as an unaffiliated user.
263 Profile
* profile
= LogInAndReturnProfile(kUnaffiliatedUserID
);
264 EXPECT_TRUE(profile
);
266 // Verify that a per-profile invalidation service has been created.
267 profile_invalidation_service_
=
268 GetProfileInvalidationService(profile
, false /* create */);
269 ASSERT_TRUE(profile_invalidation_service_
);
271 // Verify that the device-global invalidation service still exists.
272 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
274 // Indicate that the per-profile invalidation service has connected. Verify
275 // that the consumer is not called back.
276 profile_invalidation_service_
->SetInvalidatorState(
277 syncer::INVALIDATIONS_ENABLED
);
278 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
280 // Verify that the device-global invalidation service still exists.
281 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
284 void AffiliatedInvalidationServiceProviderImplTest::
285 ConnectDeviceGlobalInvalidationService() {
286 // Verify that a device-global invalidation service has been created.
287 device_invalidation_service_
=
288 provider_
->GetDeviceInvalidationServiceForTest();
289 ASSERT_TRUE(device_invalidation_service_
);
291 // Indicate that the device-global invalidation service has connected. Verify
292 // that the consumer is informed about this.
293 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
294 device_invalidation_service_
->OnInvalidatorStateChange(
295 syncer::INVALIDATIONS_ENABLED
);
296 EXPECT_EQ(1, consumer_
->GetAndClearInvalidationServiceSetCount());
297 EXPECT_EQ(device_invalidation_service_
, consumer_
->GetInvalidationService());
300 void AffiliatedInvalidationServiceProviderImplTest::
301 DisconnectPerProfileInvalidationService() {
302 ASSERT_TRUE(profile_invalidation_service_
);
304 // Indicate that the per-profile invalidation service has disconnected. Verify
305 // that the consumer is informed about this.
306 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
307 profile_invalidation_service_
->SetInvalidatorState(
308 syncer::INVALIDATION_CREDENTIALS_REJECTED
);
309 EXPECT_EQ(1, consumer_
->GetAndClearInvalidationServiceSetCount());
310 EXPECT_EQ(nullptr, consumer_
->GetInvalidationService());
312 // Verify that a device-global invalidation service has been created.
313 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
316 invalidation::FakeInvalidationService
*
317 AffiliatedInvalidationServiceProviderImplTest::GetProfileInvalidationService(
318 Profile
* profile
, bool create
) {
319 invalidation::ProfileInvalidationProvider
* invalidation_provider
=
320 static_cast<invalidation::ProfileInvalidationProvider
*>(
321 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
322 GetServiceForBrowserContext(profile
, create
));
323 if (!invalidation_provider
)
325 return static_cast<invalidation::FakeInvalidationService
*>(
326 invalidation_provider
->GetInvalidationService());
329 // No consumers are registered with the
330 // AffiliatedInvalidationServiceProviderImpl. Verifies that no device-global
331 // invalidation service is created, whether an affiliated user is logged in or
333 TEST_F(AffiliatedInvalidationServiceProviderImplTest
, NoConsumers
) {
334 // Verify that no device-global invalidation service has been created.
335 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
337 // Log in as an affiliated user.
338 EXPECT_TRUE(LogInAndReturnProfile(kAffiliatedUserID1
));
340 // Verify that no device-global invalidation service has been created.
341 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
344 // Verifies that when no connected invalidation service is available for use,
345 // none is made available to consumers.
346 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
347 NoInvalidationServiceAvailable
) {
348 // Register a consumer. Verify that the consumer is not called back
349 // immediately as no connected invalidation service exists yet.
350 consumer_
.reset(new FakeConsumer(provider_
.get()));
351 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
354 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
355 // Verifies that when no per-profile invalidation service belonging to an
356 // affiliated user is available, a device-global invalidation service is
357 // created. Further verifies that when the device-global invalidation service
358 // connects, it is made available to the consumer.
359 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
360 UseDeviceInvalidationService
) {
361 consumer_
.reset(new FakeConsumer(provider_
.get()));
363 // Indicate that the device-global invalidation service connected. Verify that
364 // that the consumer is informed about this.
365 ConnectDeviceGlobalInvalidationService();
367 // Indicate that the device-global invalidation service has disconnected.
368 // Verify that the consumer is informed about this.
369 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
370 device_invalidation_service_
->OnInvalidatorStateChange(
371 syncer::INVALIDATION_CREDENTIALS_REJECTED
);
372 EXPECT_EQ(1, consumer_
->GetAndClearInvalidationServiceSetCount());
373 EXPECT_EQ(nullptr, consumer_
->GetInvalidationService());
375 // Verify that the device-global invalidation service still exists.
376 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
379 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
380 // Verifies that when a per-profile invalidation service belonging to an
381 // affiliated user connects, it is made available to the consumer.
382 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
383 UseAffiliatedProfileInvalidationService
) {
384 consumer_
.reset(new FakeConsumer(provider_
.get()));
386 // Verify that a device-global invalidation service has been created.
387 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
389 // Log in as an affiliated user and indicate that the per-profile invalidation
390 // service for this user connected. Verify that this invalidation service is
391 // made available to the |consumer_| and the device-global invalidation
392 // service is destroyed.
393 LogInAsAffiliatedUserAndConnectInvalidationService();
395 // Indicate that the logged-in user's per-profile invalidation service
396 // disconnected. Verify that the consumer is informed about this and a
397 // device-global invalidation service is created.
398 DisconnectPerProfileInvalidationService();
401 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
402 // Verifies that when a per-profile invalidation service belonging to an
403 // unaffiliated user connects, it is ignored.
404 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
405 DoNotUseUnaffiliatedProfileInvalidationService
) {
406 consumer_
.reset(new FakeConsumer(provider_
.get()));
408 // Verify that a device-global invalidation service has been created.
409 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
411 // Log in as an unaffiliated user and indicate that the per-profile
412 // invalidation service for this user connected. Verify that this invalidation
413 // service is ignored and the device-global invalidation service is not
415 LogInAsUnaffiliatedUserAndConnectInvalidationService();
418 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
419 // A device-global invalidation service exists, is connected and is made
420 // available to the consumer. Verifies that when a per-profile invalidation
421 // service belonging to an affiliated user connects, it is made available to the
422 // consumer instead and the device-global invalidation service is destroyed.
423 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
424 SwitchToAffiliatedProfileInvalidationService
) {
425 consumer_
.reset(new FakeConsumer(provider_
.get()));
427 // Indicate that the device-global invalidation service connected. Verify that
428 // that the consumer is informed about this.
429 ConnectDeviceGlobalInvalidationService();
431 // Log in as an affiliated user and indicate that the per-profile invalidation
432 // service for this user connected. Verify that this invalidation service is
433 // made available to the |consumer_| and the device-global invalidation
434 // service is destroyed.
435 LogInAsAffiliatedUserAndConnectInvalidationService();
438 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
439 // A device-global invalidation service exists, is connected and is made
440 // available to the consumer. Verifies that when a per-profile invalidation
441 // service belonging to an unaffiliated user connects, it is ignored and the
442 // device-global invalidation service continues to be made available to the
444 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
445 DoNotSwitchToUnaffiliatedProfileInvalidationService
) {
446 consumer_
.reset(new FakeConsumer(provider_
.get()));
448 // Indicate that the device-global invalidation service connected. Verify that
449 // that the consumer is informed about this.
450 ConnectDeviceGlobalInvalidationService();
452 // Log in as an unaffiliated user and indicate that the per-profile
453 // invalidation service for this user connected. Verify that this invalidation
454 // service is ignored and the device-global invalidation service is not
456 LogInAsUnaffiliatedUserAndConnectInvalidationService();
459 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
460 // A per-profile invalidation service belonging to an affiliated user exists, is
461 // connected and is made available to the consumer. Verifies that when the
462 // per-profile invalidation service disconnects, a device-global invalidation
463 // service is created. Further verifies that when the device-global invalidation
464 // service connects, it is made available to the consumer.
465 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
466 SwitchToDeviceInvalidationService
) {
467 consumer_
.reset(new FakeConsumer(provider_
.get()));
469 // Verify that a device-global invalidation service has been created.
470 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
472 // Log in as an affiliated user and indicate that the per-profile invalidation
473 // service for this user connected. Verify that this invalidation service is
474 // made available to the |consumer_| and the device-global invalidation
475 // service is destroyed.
476 LogInAsAffiliatedUserAndConnectInvalidationService();
478 // Indicate that the logged-in user's per-profile invalidation service
479 // disconnected. Verify that the consumer is informed about this and a
480 // device-global invalidation service is created.
481 DisconnectPerProfileInvalidationService();
483 // Indicate that the device-global invalidation service connected. Verify that
484 // that the consumer is informed about this.
485 ConnectDeviceGlobalInvalidationService();
488 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
489 // A per-profile invalidation service belonging to a first affiliated user
490 // exists, is connected and is made available to the consumer. A per-profile
491 // invalidation service belonging to a second affiliated user also exists and is
492 // connected. Verifies that when the per-profile invalidation service belonging
493 // to the first user disconnects, the per-profile invalidation service belonging
494 // to the second user is made available to the consumer instead.
495 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
496 SwitchBetweenAffiliatedProfileInvalidationServices
) {
497 consumer_
.reset(new FakeConsumer(provider_
.get()));
499 // Verify that a device-global invalidation service has been created.
500 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
502 // Log in as a first affiliated user and indicate that the per-profile
503 // invalidation service for this user connected. Verify that this invalidation
504 // service is made available to the |consumer_| and the device-global
505 // invalidation service is destroyed.
506 LogInAsAffiliatedUserAndConnectInvalidationService();
508 // Log in as a second affiliated user.
509 Profile
* second_profile
= LogInAndReturnProfile(kAffiliatedUserID2
);
510 EXPECT_TRUE(second_profile
);
512 // Verify that the device-global invalidation service still does not exist.
513 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
515 // Verify that a per-profile invalidation service for the second user has been
517 invalidation::FakeInvalidationService
* second_profile_invalidation_service
=
518 GetProfileInvalidationService(second_profile
, false /* create */);
519 ASSERT_TRUE(second_profile_invalidation_service
);
521 // Indicate that the second user's per-profile invalidation service has
522 // connected. Verify that the consumer is not called back.
523 second_profile_invalidation_service
->SetInvalidatorState(
524 syncer::INVALIDATIONS_ENABLED
);
525 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
527 // Indicate that the first user's per-profile invalidation service has
528 // disconnected. Verify that the consumer is informed that the second user's
529 // per-profile invalidation service should be used instead of the first
531 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
532 profile_invalidation_service_
->SetInvalidatorState(
533 syncer::INVALIDATION_CREDENTIALS_REJECTED
);
534 EXPECT_EQ(1, consumer_
->GetAndClearInvalidationServiceSetCount());
535 EXPECT_EQ(second_profile_invalidation_service
,
536 consumer_
->GetInvalidationService());
538 // Verify that the device-global invalidation service still does not exist.
539 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
542 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
543 // A device-global invalidation service exists, is connected and is made
544 // available to the consumer. Verifies that when a second consumer registers,
545 // the device-global invalidation service is made available to it as well.
546 // Further verifies that when the first consumer unregisters, the device-global
547 // invalidation service is not destroyed and remains available to the second
548 // consumer. Further verifies that when the second consumer also unregisters,
549 // the device-global invalidation service is destroyed.
550 TEST_F(AffiliatedInvalidationServiceProviderImplTest
, MultipleConsumers
) {
551 consumer_
.reset(new FakeConsumer(provider_
.get()));
553 // Indicate that the device-global invalidation service connected. Verify that
554 // that the consumer is informed about this.
555 ConnectDeviceGlobalInvalidationService();
557 // Register a second consumer. Verify that the consumer is called back
558 // immediately as a connected invalidation service is available.
559 scoped_ptr
<FakeConsumer
> second_consumer(new FakeConsumer(provider_
.get()));
560 EXPECT_EQ(1, second_consumer
->GetAndClearInvalidationServiceSetCount());
561 EXPECT_EQ(device_invalidation_service_
,
562 second_consumer
->GetInvalidationService());
564 // Unregister the first consumer.
567 // Verify that the device-global invalidation service still exists.
568 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
570 // Unregister the second consumer.
571 second_consumer
.reset();
573 // Verify that the device-global invalidation service has been destroyed.
574 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
577 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
578 // A per-profile invalidation service belonging to a first affiliated user
579 // exists, is connected and is made available to the consumer. Verifies that
580 // when the provider is shut down, the consumer is informed that no
581 // invalidation service is available for use anymore. Also verifies that no
582 // device-global invalidation service is created and a per-profile invalidation
583 // service belonging to a second affiliated user that subsequently connects is
585 TEST_F(AffiliatedInvalidationServiceProviderImplTest
, NoServiceAfterShutdown
) {
586 consumer_
.reset(new FakeConsumer(provider_
.get()));
588 // Verify that a device-global invalidation service has been created.
589 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
591 // Log in as a first affiliated user and indicate that the per-profile
592 // invalidation service for this user connected. Verify that this invalidation
593 // service is made available to the |consumer_| and the device-global
594 // invalidation service is destroyed.
595 LogInAsAffiliatedUserAndConnectInvalidationService();
597 // Shut down the |provider_|. Verify that the |consumer_| is informed that no
598 // invalidation service is available for use anymore.
599 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
600 provider_
->Shutdown();
601 EXPECT_EQ(1, consumer_
->GetAndClearInvalidationServiceSetCount());
602 EXPECT_EQ(nullptr, consumer_
->GetInvalidationService());
604 // Verify that the device-global invalidation service still does not exist.
605 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
607 // Log in as a second affiliated user.
608 Profile
* second_profile
= LogInAndReturnProfile(kAffiliatedUserID2
);
609 EXPECT_TRUE(second_profile
);
611 // Verify that the device-global invalidation service still does not exist.
612 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
614 // Create a per-profile invalidation service for the second user.
615 invalidation::FakeInvalidationService
* second_profile_invalidation_service
=
616 GetProfileInvalidationService(second_profile
, true /* create */);
617 ASSERT_TRUE(second_profile_invalidation_service
);
619 // Indicate that the second user's per-profile invalidation service has
620 // connected. Verify that the consumer is not called back.
621 second_profile_invalidation_service
->SetInvalidatorState(
622 syncer::INVALIDATIONS_ENABLED
);
623 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
625 // Verify that the device-global invalidation service still does not exist.
626 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
629 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
630 // A device-global invalidation service exists, is connected and is made
631 // available to the consumer. Verifies that when the provider is shut down, the
632 // consumer is informed that no invalidation service is available for use
633 // anymore before the device-global invalidation service is destroyed.
634 // This is a regression test for http://crbug.com/455504.
635 TEST_F(AffiliatedInvalidationServiceProviderImplTest
,
636 ConnectedDeviceGlobalInvalidationServiceOnShutdown
) {
637 consumer_
.reset(new FakeConsumer(provider_
.get()));
639 // Verify that a device-global invalidation service has been created.
640 EXPECT_TRUE(provider_
->GetDeviceInvalidationServiceForTest());
642 // Indicate that the device-global invalidation service connected. Verify that
643 // that the consumer is informed about this.
644 ConnectDeviceGlobalInvalidationService();
646 // Shut down the |provider_|. Verify that the |consumer_| is informed that no
647 // invalidation service is available for use anymore. This also serves as a
648 // regression test which verifies that the invalidation service is not
649 // destroyed until the |consumer_| has been informed: If the invalidation
650 // service was destroyed too early, the |consumer_| would still be registered
651 // as an observer and the invalidation service's destructor would DCHECK().
652 EXPECT_EQ(0, consumer_
->GetAndClearInvalidationServiceSetCount());
653 provider_
->Shutdown();
654 EXPECT_EQ(1, consumer_
->GetAndClearInvalidationServiceSetCount());
655 EXPECT_EQ(nullptr, consumer_
->GetInvalidationService());
657 // Verify that the device-global invalidation service has been destroyed.
658 EXPECT_FALSE(provider_
->GetDeviceInvalidationServiceForTest());
661 } // namespace policy