Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / affiliated_invalidation_service_provider_impl_unittest.cc
blobdedc6b4398dbb61aeef02e91ec57897ff42c8281
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"
7 #include <string>
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/chromeos/login/users/fake_chrome_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/fake_invalidation_handler.h"
24 #include "components/invalidation/invalidation_service.h"
25 #include "components/invalidation/invalidator_state.h"
26 #include "components/invalidation/profile_invalidation_provider.h"
27 #include "components/invalidation/ticl_invalidation_service.h"
28 #include "components/keyed_service/core/keyed_service.h"
29 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
30 #include "content/public/browser/browser_context.h"
31 #include "content/public/browser/notification_details.h"
32 #include "content/public/browser/notification_service.h"
33 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 namespace policy {
38 namespace {
40 const char kAffiliatedUserID1[] = "test_1@example.com";
41 const char kAffiliatedUserID2[] = "test_2@example.com";
42 const char kUnaffiliatedUserID[] = "test@other_domain.test";
44 KeyedService* BuildProfileInvalidationProvider(
45 content::BrowserContext* context) {
46 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service(
47 new invalidation::FakeInvalidationService);
48 invalidation_service->SetInvalidatorState(
49 syncer::TRANSIENT_INVALIDATION_ERROR);
50 return new invalidation::ProfileInvalidationProvider(
51 invalidation_service.Pass());
54 } // namespace
56 // A simple AffiliatedInvalidationServiceProvider::Consumer that registers a
57 // syncer::FakeInvalidationHandler with the invalidation::InvalidationService
58 // that is currently being made available.
59 class FakeConsumer : public AffiliatedInvalidationServiceProvider::Consumer {
60 public:
61 explicit FakeConsumer(AffiliatedInvalidationServiceProviderImpl* provider);
62 ~FakeConsumer() override;
64 // AffiliatedInvalidationServiceProvider::Consumer:
65 void OnInvalidationServiceSet(
66 invalidation::InvalidationService* invalidation_service) override;
68 int GetAndClearInvalidationServiceSetCount();
69 const invalidation::InvalidationService* GetInvalidationService() const;
71 private:
72 AffiliatedInvalidationServiceProviderImpl* provider_;
73 syncer::FakeInvalidationHandler invalidation_handler_;
75 int invalidation_service_set_count_ = 0;
76 invalidation::InvalidationService* invalidation_service_ = nullptr;
78 DISALLOW_COPY_AND_ASSIGN(FakeConsumer);
81 class AffiliatedInvalidationServiceProviderImplTest : public testing::Test {
82 public:
83 AffiliatedInvalidationServiceProviderImplTest();
85 // testing::Test:
86 void SetUp() override;
87 void TearDown() override;
89 // Ownership is not passed. The Profile is owned by the global ProfileManager.
90 Profile* LogInAndReturnProfile(const std::string& user_id);
92 // Logs in as an affiliated user and indicates that the per-profile
93 // invalidation service for this user connected. Verifies that this
94 // invalidation service is made available to the |consumer_| and the
95 // device-global invalidation service is destroyed.
96 void LogInAsAffiliatedUserAndConnectInvalidationService();
98 // Logs in as an unaffiliated user and indicates that the per-profile
99 // invalidation service for this user connected. Verifies that this
100 // invalidation service is ignored and the device-global invalidation service
101 // is not destroyed.
102 void LogInAsUnaffiliatedUserAndConnectInvalidationService();
104 // Indicates that the device-global invalidation service connected. Verifies
105 // that the |consumer_| is informed about this.
106 void ConnectDeviceGlobalInvalidationService();
108 // Indicates that the logged-in user's per-profile invalidation service
109 // disconnected. Verifies that the |consumer_| is informed about this and a
110 // device-global invalidation service is created.
111 void DisconnectPerProfileInvalidationService();
113 invalidation::FakeInvalidationService* GetProfileInvalidationService(
114 Profile* profile,
115 bool create);
117 protected:
118 scoped_ptr<AffiliatedInvalidationServiceProviderImpl> provider_;
119 scoped_ptr<FakeConsumer> consumer_;
120 invalidation::TiclInvalidationService* device_invalidation_service_;
121 invalidation::FakeInvalidationService* profile_invalidation_service_;
123 private:
124 content::TestBrowserThreadBundle thread_bundle_;
125 chromeos::FakeChromeUserManager* fake_user_manager_;
126 chromeos::ScopedUserManagerEnabler user_manager_enabler_;
127 ScopedStubEnterpriseInstallAttributes install_attributes_;
128 scoped_ptr<chromeos::ScopedTestDeviceSettingsService>
129 test_device_settings_service_;
130 scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_;
131 TestingProfileManager profile_manager_;
134 FakeConsumer::FakeConsumer(AffiliatedInvalidationServiceProviderImpl* provider)
135 : provider_(provider) {
136 provider_->RegisterConsumer(this);
139 FakeConsumer::~FakeConsumer() {
140 if (invalidation_service_) {
141 invalidation_service_->UnregisterInvalidationHandler(
142 &invalidation_handler_);
144 provider_->UnregisterConsumer(this);
146 EXPECT_EQ(0, invalidation_service_set_count_);
149 void FakeConsumer::OnInvalidationServiceSet(
150 invalidation::InvalidationService* invalidation_service) {
151 ++invalidation_service_set_count_;
153 if (invalidation_service_) {
154 invalidation_service_->UnregisterInvalidationHandler(
155 &invalidation_handler_);
158 invalidation_service_ = invalidation_service;
160 if (invalidation_service_) {
161 // Regression test for http://crbug.com/455504: The |invalidation_service|
162 // was sometimes destroyed without notifying consumers and giving them a
163 // chance to unregister their invalidation handlers. Register an
164 // invalidation handler so that |invalidation_service| CHECK()s in its
165 // destructor if this regresses.
166 invalidation_service_->RegisterInvalidationHandler(&invalidation_handler_);
170 int FakeConsumer::GetAndClearInvalidationServiceSetCount() {
171 const int invalidation_service_set_count = invalidation_service_set_count_;
172 invalidation_service_set_count_ = 0;
173 return invalidation_service_set_count;
176 const invalidation::InvalidationService*
177 FakeConsumer::GetInvalidationService() const {
178 return invalidation_service_;
181 AffiliatedInvalidationServiceProviderImplTest::
182 AffiliatedInvalidationServiceProviderImplTest()
183 : device_invalidation_service_(nullptr),
184 profile_invalidation_service_(nullptr),
185 fake_user_manager_(new chromeos::FakeChromeUserManager),
186 user_manager_enabler_(fake_user_manager_),
187 install_attributes_("example.com",
188 "user@example.com",
189 "device_id",
190 DEVICE_MODE_ENTERPRISE),
191 profile_manager_(TestingBrowserProcess::GetGlobal()) {
194 void AffiliatedInvalidationServiceProviderImplTest::SetUp() {
195 chromeos::SystemSaltGetter::Initialize();
196 chromeos::DBusThreadManager::Initialize();
197 ASSERT_TRUE(profile_manager_.SetUp());
199 test_device_settings_service_.reset(new
200 chromeos::ScopedTestDeviceSettingsService);
201 test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings);
202 chromeos::DeviceOAuth2TokenServiceFactory::Initialize();
204 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
205 RegisterTestingFactory(BuildProfileInvalidationProvider);
207 provider_.reset(new AffiliatedInvalidationServiceProviderImpl);
210 void AffiliatedInvalidationServiceProviderImplTest::TearDown() {
211 consumer_.reset();
212 provider_->Shutdown();
213 provider_.reset();
215 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
216 RegisterTestingFactory(nullptr);
217 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown();
218 chromeos::DBusThreadManager::Shutdown();
219 chromeos::SystemSaltGetter::Shutdown();
222 Profile* AffiliatedInvalidationServiceProviderImplTest::LogInAndReturnProfile(
223 const std::string& user_id) {
224 fake_user_manager_->AddUser(user_id);
225 Profile* profile = profile_manager_.CreateTestingProfile(user_id);
226 content::NotificationService::current()->Notify(
227 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
228 content::NotificationService::AllSources(),
229 content::Details<Profile>(profile));
230 return profile;
233 void AffiliatedInvalidationServiceProviderImplTest::
234 LogInAsAffiliatedUserAndConnectInvalidationService() {
235 // Log in as an affiliated user.
236 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
237 EXPECT_TRUE(profile);
239 // Verify that a per-profile invalidation service has been created.
240 profile_invalidation_service_ =
241 GetProfileInvalidationService(profile, false /* create */);
242 ASSERT_TRUE(profile_invalidation_service_);
244 // Verify that the device-global invalidation service still exists.
245 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
247 // Indicate that the per-profile invalidation service has connected. Verify
248 // that the consumer is informed about this.
249 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
250 profile_invalidation_service_->SetInvalidatorState(
251 syncer::INVALIDATIONS_ENABLED);
252 EXPECT_EQ(1, consumer_->GetAndClearInvalidationServiceSetCount());
253 EXPECT_EQ(profile_invalidation_service_, consumer_->GetInvalidationService());
255 // Verify that the device-global invalidation service has been destroyed.
256 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
259 void AffiliatedInvalidationServiceProviderImplTest::
260 LogInAsUnaffiliatedUserAndConnectInvalidationService() {
261 // Log in as an unaffiliated user.
262 Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID);
263 EXPECT_TRUE(profile);
265 // Verify that a per-profile invalidation service has been created.
266 profile_invalidation_service_ =
267 GetProfileInvalidationService(profile, false /* create */);
268 ASSERT_TRUE(profile_invalidation_service_);
270 // Verify that the device-global invalidation service still exists.
271 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
273 // Indicate that the per-profile invalidation service has connected. Verify
274 // that the consumer is not called back.
275 profile_invalidation_service_->SetInvalidatorState(
276 syncer::INVALIDATIONS_ENABLED);
277 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
279 // Verify that the device-global invalidation service still exists.
280 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
283 void AffiliatedInvalidationServiceProviderImplTest::
284 ConnectDeviceGlobalInvalidationService() {
285 // Verify that a device-global invalidation service has been created.
286 device_invalidation_service_ =
287 provider_->GetDeviceInvalidationServiceForTest();
288 ASSERT_TRUE(device_invalidation_service_);
290 // Indicate that the device-global invalidation service has connected. Verify
291 // that the consumer is informed about this.
292 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
293 device_invalidation_service_->OnInvalidatorStateChange(
294 syncer::INVALIDATIONS_ENABLED);
295 EXPECT_EQ(1, consumer_->GetAndClearInvalidationServiceSetCount());
296 EXPECT_EQ(device_invalidation_service_, consumer_->GetInvalidationService());
299 void AffiliatedInvalidationServiceProviderImplTest::
300 DisconnectPerProfileInvalidationService() {
301 ASSERT_TRUE(profile_invalidation_service_);
303 // Indicate that the per-profile invalidation service has disconnected. Verify
304 // that the consumer is informed about this.
305 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
306 profile_invalidation_service_->SetInvalidatorState(
307 syncer::INVALIDATION_CREDENTIALS_REJECTED);
308 EXPECT_EQ(1, consumer_->GetAndClearInvalidationServiceSetCount());
309 EXPECT_EQ(nullptr, consumer_->GetInvalidationService());
311 // Verify that a device-global invalidation service has been created.
312 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
315 invalidation::FakeInvalidationService*
316 AffiliatedInvalidationServiceProviderImplTest::GetProfileInvalidationService(
317 Profile* profile, bool create) {
318 invalidation::ProfileInvalidationProvider* invalidation_provider =
319 static_cast<invalidation::ProfileInvalidationProvider*>(
320 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
321 GetServiceForBrowserContext(profile, create));
322 if (!invalidation_provider)
323 return nullptr;
324 return static_cast<invalidation::FakeInvalidationService*>(
325 invalidation_provider->GetInvalidationService());
328 // No consumers are registered with the
329 // AffiliatedInvalidationServiceProviderImpl. Verifies that no device-global
330 // invalidation service is created, whether an affiliated user is logged in or
331 // not.
332 TEST_F(AffiliatedInvalidationServiceProviderImplTest, NoConsumers) {
333 // Verify that no device-global invalidation service has been created.
334 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
336 // Log in as an affiliated user.
337 EXPECT_TRUE(LogInAndReturnProfile(kAffiliatedUserID1));
339 // Verify that no device-global invalidation service has been created.
340 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
343 // Verifies that when no connected invalidation service is available for use,
344 // none is made available to consumers.
345 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
346 NoInvalidationServiceAvailable) {
347 // Register a consumer. Verify that the consumer is not called back
348 // immediately as no connected invalidation service exists yet.
349 consumer_.reset(new FakeConsumer(provider_.get()));
350 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
353 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
354 // Verifies that when no per-profile invalidation service belonging to an
355 // affiliated user is available, a device-global invalidation service is
356 // created. Further verifies that when the device-global invalidation service
357 // connects, it is made available to the consumer.
358 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
359 UseDeviceInvalidationService) {
360 consumer_.reset(new FakeConsumer(provider_.get()));
362 // Indicate that the device-global invalidation service connected. Verify that
363 // that the consumer is informed about this.
364 ConnectDeviceGlobalInvalidationService();
366 // Indicate that the device-global invalidation service has disconnected.
367 // Verify that the consumer is informed about this.
368 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
369 device_invalidation_service_->OnInvalidatorStateChange(
370 syncer::INVALIDATION_CREDENTIALS_REJECTED);
371 EXPECT_EQ(1, consumer_->GetAndClearInvalidationServiceSetCount());
372 EXPECT_EQ(nullptr, consumer_->GetInvalidationService());
374 // Verify that the device-global invalidation service still exists.
375 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
378 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
379 // Verifies that when a per-profile invalidation service belonging to an
380 // affiliated user connects, it is made available to the consumer.
381 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
382 UseAffiliatedProfileInvalidationService) {
383 consumer_.reset(new FakeConsumer(provider_.get()));
385 // Verify that a device-global invalidation service has been created.
386 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
388 // Log in as an affiliated user and indicate that the per-profile invalidation
389 // service for this user connected. Verify that this invalidation service is
390 // made available to the |consumer_| and the device-global invalidation
391 // service is destroyed.
392 LogInAsAffiliatedUserAndConnectInvalidationService();
394 // Indicate that the logged-in user's per-profile invalidation service
395 // disconnected. Verify that the consumer is informed about this and a
396 // device-global invalidation service is created.
397 DisconnectPerProfileInvalidationService();
400 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
401 // Verifies that when a per-profile invalidation service belonging to an
402 // unaffiliated user connects, it is ignored.
403 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
404 DoNotUseUnaffiliatedProfileInvalidationService) {
405 consumer_.reset(new FakeConsumer(provider_.get()));
407 // Verify that a device-global invalidation service has been created.
408 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
410 // Log in as an unaffiliated user and indicate that the per-profile
411 // invalidation service for this user connected. Verify that this invalidation
412 // service is ignored and the device-global invalidation service is not
413 // destroyed.
414 LogInAsUnaffiliatedUserAndConnectInvalidationService();
417 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
418 // A device-global invalidation service exists, is connected and is made
419 // available to the consumer. Verifies that when a per-profile invalidation
420 // service belonging to an affiliated user connects, it is made available to the
421 // consumer instead and the device-global invalidation service is destroyed.
422 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
423 SwitchToAffiliatedProfileInvalidationService) {
424 consumer_.reset(new FakeConsumer(provider_.get()));
426 // Indicate that the device-global invalidation service connected. Verify that
427 // that the consumer is informed about this.
428 ConnectDeviceGlobalInvalidationService();
430 // Log in as an affiliated user and indicate that the per-profile invalidation
431 // service for this user connected. Verify that this invalidation service is
432 // made available to the |consumer_| and the device-global invalidation
433 // service is destroyed.
434 LogInAsAffiliatedUserAndConnectInvalidationService();
437 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
438 // A device-global invalidation service exists, is connected and is made
439 // available to the consumer. Verifies that when a per-profile invalidation
440 // service belonging to an unaffiliated user connects, it is ignored and the
441 // device-global invalidation service continues to be made available to the
442 // consumer.
443 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
444 DoNotSwitchToUnaffiliatedProfileInvalidationService) {
445 consumer_.reset(new FakeConsumer(provider_.get()));
447 // Indicate that the device-global invalidation service connected. Verify that
448 // that the consumer is informed about this.
449 ConnectDeviceGlobalInvalidationService();
451 // Log in as an unaffiliated user and indicate that the per-profile
452 // invalidation service for this user connected. Verify that this invalidation
453 // service is ignored and the device-global invalidation service is not
454 // destroyed.
455 LogInAsUnaffiliatedUserAndConnectInvalidationService();
458 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
459 // A per-profile invalidation service belonging to an affiliated user exists, is
460 // connected and is made available to the consumer. Verifies that when the
461 // per-profile invalidation service disconnects, a device-global invalidation
462 // service is created. Further verifies that when the device-global invalidation
463 // service connects, it is made available to the consumer.
464 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
465 SwitchToDeviceInvalidationService) {
466 consumer_.reset(new FakeConsumer(provider_.get()));
468 // Verify that a device-global invalidation service has been created.
469 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
471 // Log in as an affiliated user and indicate that the per-profile invalidation
472 // service for this user connected. Verify that this invalidation service is
473 // made available to the |consumer_| and the device-global invalidation
474 // service is destroyed.
475 LogInAsAffiliatedUserAndConnectInvalidationService();
477 // Indicate that the logged-in user's per-profile invalidation service
478 // disconnected. Verify that the consumer is informed about this and a
479 // device-global invalidation service is created.
480 DisconnectPerProfileInvalidationService();
482 // Indicate that the device-global invalidation service connected. Verify that
483 // that the consumer is informed about this.
484 ConnectDeviceGlobalInvalidationService();
487 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
488 // A per-profile invalidation service belonging to a first affiliated user
489 // exists, is connected and is made available to the consumer. A per-profile
490 // invalidation service belonging to a second affiliated user also exists and is
491 // connected. Verifies that when the per-profile invalidation service belonging
492 // to the first user disconnects, the per-profile invalidation service belonging
493 // to the second user is made available to the consumer instead.
494 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
495 SwitchBetweenAffiliatedProfileInvalidationServices) {
496 consumer_.reset(new FakeConsumer(provider_.get()));
498 // Verify that a device-global invalidation service has been created.
499 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
501 // Log in as a first affiliated user and indicate that the per-profile
502 // invalidation service for this user connected. Verify that this invalidation
503 // service is made available to the |consumer_| and the device-global
504 // invalidation service is destroyed.
505 LogInAsAffiliatedUserAndConnectInvalidationService();
507 // Log in as a second affiliated user.
508 Profile* second_profile = LogInAndReturnProfile(kAffiliatedUserID2);
509 EXPECT_TRUE(second_profile);
511 // Verify that the device-global invalidation service still does not exist.
512 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
514 // Verify that a per-profile invalidation service for the second user has been
515 // created.
516 invalidation::FakeInvalidationService* second_profile_invalidation_service =
517 GetProfileInvalidationService(second_profile, false /* create */);
518 ASSERT_TRUE(second_profile_invalidation_service);
520 // Indicate that the second user's per-profile invalidation service has
521 // connected. Verify that the consumer is not called back.
522 second_profile_invalidation_service->SetInvalidatorState(
523 syncer::INVALIDATIONS_ENABLED);
524 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
526 // Indicate that the first user's per-profile invalidation service has
527 // disconnected. Verify that the consumer is informed that the second user's
528 // per-profile invalidation service should be used instead of the first
529 // user's.
530 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
531 profile_invalidation_service_->SetInvalidatorState(
532 syncer::INVALIDATION_CREDENTIALS_REJECTED);
533 EXPECT_EQ(1, consumer_->GetAndClearInvalidationServiceSetCount());
534 EXPECT_EQ(second_profile_invalidation_service,
535 consumer_->GetInvalidationService());
537 // Verify that the device-global invalidation service still does not exist.
538 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
541 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
542 // A device-global invalidation service exists, is connected and is made
543 // available to the consumer. Verifies that when a second consumer registers,
544 // the device-global invalidation service is made available to it as well.
545 // Further verifies that when the first consumer unregisters, the device-global
546 // invalidation service is not destroyed and remains available to the second
547 // consumer. Further verifies that when the second consumer also unregisters,
548 // the device-global invalidation service is destroyed.
549 TEST_F(AffiliatedInvalidationServiceProviderImplTest, MultipleConsumers) {
550 consumer_.reset(new FakeConsumer(provider_.get()));
552 // Indicate that the device-global invalidation service connected. Verify that
553 // that the consumer is informed about this.
554 ConnectDeviceGlobalInvalidationService();
556 // Register a second consumer. Verify that the consumer is called back
557 // immediately as a connected invalidation service is available.
558 scoped_ptr<FakeConsumer> second_consumer(new FakeConsumer(provider_.get()));
559 EXPECT_EQ(1, second_consumer->GetAndClearInvalidationServiceSetCount());
560 EXPECT_EQ(device_invalidation_service_,
561 second_consumer->GetInvalidationService());
563 // Unregister the first consumer.
564 consumer_.reset();
566 // Verify that the device-global invalidation service still exists.
567 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
569 // Unregister the second consumer.
570 second_consumer.reset();
572 // Verify that the device-global invalidation service has been destroyed.
573 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
576 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
577 // A per-profile invalidation service belonging to a first affiliated user
578 // exists, is connected and is made available to the consumer. Verifies that
579 // when the provider is shut down, the consumer is informed that no
580 // invalidation service is available for use anymore. Also verifies that no
581 // device-global invalidation service is created and a per-profile invalidation
582 // service belonging to a second affiliated user that subsequently connects is
583 // ignored.
584 TEST_F(AffiliatedInvalidationServiceProviderImplTest, NoServiceAfterShutdown) {
585 consumer_.reset(new FakeConsumer(provider_.get()));
587 // Verify that a device-global invalidation service has been created.
588 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
590 // Log in as a first affiliated user and indicate that the per-profile
591 // invalidation service for this user connected. Verify that this invalidation
592 // service is made available to the |consumer_| and the device-global
593 // invalidation service is destroyed.
594 LogInAsAffiliatedUserAndConnectInvalidationService();
596 // Shut down the |provider_|. Verify that the |consumer_| is informed that no
597 // invalidation service is available for use anymore.
598 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
599 provider_->Shutdown();
600 EXPECT_EQ(1, consumer_->GetAndClearInvalidationServiceSetCount());
601 EXPECT_EQ(nullptr, consumer_->GetInvalidationService());
603 // Verify that the device-global invalidation service still does not exist.
604 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
606 // Log in as a second affiliated user.
607 Profile* second_profile = LogInAndReturnProfile(kAffiliatedUserID2);
608 EXPECT_TRUE(second_profile);
610 // Verify that the device-global invalidation service still does not exist.
611 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
613 // Create a per-profile invalidation service for the second user.
614 invalidation::FakeInvalidationService* second_profile_invalidation_service =
615 GetProfileInvalidationService(second_profile, true /* create */);
616 ASSERT_TRUE(second_profile_invalidation_service);
618 // Indicate that the second user's per-profile invalidation service has
619 // connected. Verify that the consumer is not called back.
620 second_profile_invalidation_service->SetInvalidatorState(
621 syncer::INVALIDATIONS_ENABLED);
622 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
624 // Verify that the device-global invalidation service still does not exist.
625 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
628 // A consumer is registered with the AffiliatedInvalidationServiceProviderImpl.
629 // A device-global invalidation service exists, is connected and is made
630 // available to the consumer. Verifies that when the provider is shut down, the
631 // consumer is informed that no invalidation service is available for use
632 // anymore before the device-global invalidation service is destroyed.
633 // This is a regression test for http://crbug.com/455504.
634 TEST_F(AffiliatedInvalidationServiceProviderImplTest,
635 ConnectedDeviceGlobalInvalidationServiceOnShutdown) {
636 consumer_.reset(new FakeConsumer(provider_.get()));
638 // Verify that a device-global invalidation service has been created.
639 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
641 // Indicate that the device-global invalidation service connected. Verify that
642 // that the consumer is informed about this.
643 ConnectDeviceGlobalInvalidationService();
645 // Shut down the |provider_|. Verify that the |consumer_| is informed that no
646 // invalidation service is available for use anymore. This also serves as a
647 // regression test which verifies that the invalidation service is not
648 // destroyed until the |consumer_| has been informed: If the invalidation
649 // service was destroyed too early, the |consumer_| would still be registered
650 // as an observer and the invalidation service's destructor would DCHECK().
651 EXPECT_EQ(0, consumer_->GetAndClearInvalidationServiceSetCount());
652 provider_->Shutdown();
653 EXPECT_EQ(1, consumer_->GetAndClearInvalidationServiceSetCount());
654 EXPECT_EQ(nullptr, consumer_->GetInvalidationService());
656 // Verify that the device-global invalidation service has been destroyed.
657 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
660 } // namespace policy