Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: chrome/browser/chromeos/policy/affiliated_invalidation_service_provider_unittest.cc

Issue 828953004: Add AffiliatedInvalidationServiceProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Style fixes. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/policy/affiliated_invalidation_service_provide r.h"
6
7 #include <string>
8
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
11 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
12 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
13 #include "chrome/browser/chromeos/settings/cros_settings.h"
14 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h "
15 #include "chrome/browser/chromeos/settings/device_settings_service.h"
16 #include "chrome/browser/invalidation/fake_invalidation_service.h"
17 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile_manager.h"
21 #include "chromeos/cryptohome/system_salt_getter.h"
22 #include "chromeos/dbus/dbus_thread_manager.h"
23 #include "components/invalidation/invalidation_service.h"
24 #include "components/invalidation/invalidator_state.h"
25 #include "components/invalidation/profile_invalidation_provider.h"
26 #include "components/invalidation/ticl_invalidation_service.h"
27 #include "components/keyed_service/core/keyed_service.h"
28 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
29 #include "content/public/browser/browser_context.h"
30 #include "content/public/browser/notification_details.h"
31 #include "content/public/browser/notification_service.h"
32 #include "content/public/test/test_browser_thread_bundle.h"
33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35
36 using testing::Mock;
37 using testing::StrictMock;
38
39 namespace policy {
40
41 namespace {
42
43 const char kAffiliatedUserID1[] = "test_1@example.com";
44 const char kAffiliatedUserID2[] = "test_2@example.com";
45 const char kUnaffiliatedUserID[] = "test@other_domain.test";
46
47 KeyedService* BuildProfileInvalidationProvider(
48 content::BrowserContext* context) {
49 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service(
50 new invalidation::FakeInvalidationService);
51 invalidation_service->SetInvalidatorState(
52 syncer::TRANSIENT_INVALIDATION_ERROR);
53 return new invalidation::ProfileInvalidationProvider(
54 invalidation_service.Pass());
55 }
56
57 } // namespace
58
59 class MockConsumer : public AffiliatedInvalidationServiceProvider::Consumer {
60 public:
61 MockConsumer();
62 ~MockConsumer() override;
63
64 MOCK_METHOD1(OnInvalidationServiceSet,
65 void(invalidation::InvalidationService*));
66
67 private:
68 DISALLOW_COPY_AND_ASSIGN(MockConsumer);
69 };
70
71 class AffiliatedInvalidationServiceProviderTest : public testing::Test {
72 public:
73 AffiliatedInvalidationServiceProviderTest();
74
75 // testing::Test:
76 virtual void SetUp() override;
77 virtual void TearDown() override;
78
79 // Ownership is not passed. The Profile is owned by the global ProfileManager.
80 Profile* LogInAndReturnProfile(const std::string& user_id);
81
82 // Logs in as an affiliated user and indicates that the per-profile
83 // invalidation service for this user connected. Verifies that this
84 // invalidation service is made available to the |consumer_| and the
85 // device-global invalidation service is destroyed.
86 void LogInAsAffiliatedUserAndConnectInvalidationService();
87
88 // Logs in as an unaffiliated user and indicates that the per-profile
89 // invalidation service for this user connected. Verifies that this
90 // invalidation service is ignored and the device-global invalidation service
91 // is not destroyed.
92 void LogInAsUnaffiliatedUserAndConnectInvalidationService();
93
94 // Indicates that the device-global invalidation service connected. Verifies
95 // that the |consumer_| is informed about this.
96 void ConnectDeviceGlobalInvalidationService();
97
98 // Indicates that the logged-in user's per-profile invalidation service
99 // disconnected. Verifies that the |consumer_| is informed about this and a
100 // device-global invalidation service is created.
101 void DisconnectPerProfileInvalidationService();
102
103 invalidation::FakeInvalidationService* GetProfileInvalidationService(
104 Profile* profile,
105 bool create);
106
107 protected:
108 scoped_ptr<AffiliatedInvalidationServiceProvider> provider_;
109 StrictMock<MockConsumer> consumer_;
110 invalidation::TiclInvalidationService* device_invalidation_service_;
111 invalidation::FakeInvalidationService* profile_invalidation_service_;
112
113 private:
114 content::TestBrowserThreadBundle thread_bundle_;
115 chromeos::FakeUserManager* fake_user_manager_;
116 chromeos::ScopedUserManagerEnabler user_manager_enabler_;
117 ScopedStubEnterpriseInstallAttributes install_attributes_;
118 scoped_ptr<chromeos::ScopedTestDeviceSettingsService>
119 test_device_settings_service_;
120 scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_;
121 TestingProfileManager profile_manager_;
122 };
123
124 MockConsumer::MockConsumer() {
125 }
126
127 MockConsumer::~MockConsumer() {
128 }
129
130 AffiliatedInvalidationServiceProviderTest::
131 AffiliatedInvalidationServiceProviderTest()
132 : device_invalidation_service_(nullptr),
133 profile_invalidation_service_(nullptr),
134 fake_user_manager_(new chromeos::FakeUserManager),
135 user_manager_enabler_(fake_user_manager_),
136 install_attributes_("example.com",
137 "user@example.com",
138 "device_id",
139 DEVICE_MODE_ENTERPRISE),
140 profile_manager_(TestingBrowserProcess::GetGlobal()) {
141 }
142
143 void AffiliatedInvalidationServiceProviderTest::SetUp() {
144 chromeos::SystemSaltGetter::Initialize();
145 chromeos::DBusThreadManager::Initialize();
146 chromeos::DeviceOAuth2TokenServiceFactory::Initialize();
147 ASSERT_TRUE(profile_manager_.SetUp());
148
149 test_device_settings_service_.reset(new
150 chromeos::ScopedTestDeviceSettingsService);
151 test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings);
152
153 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
154 RegisterTestingFactory(BuildProfileInvalidationProvider);
155
156 provider_.reset(new AffiliatedInvalidationServiceProvider);
157 }
158
159 void AffiliatedInvalidationServiceProviderTest::TearDown() {
160 provider_->Shutdown();
161 provider_.reset();
162
163 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
164 RegisterTestingFactory(nullptr);
165 chromeos::DeviceOAuth2TokenServiceFactory::Shutdown();
166 chromeos::DBusThreadManager::Shutdown();
167 chromeos::SystemSaltGetter::Shutdown();
168 }
169
170 Profile* AffiliatedInvalidationServiceProviderTest::LogInAndReturnProfile(
171 const std::string& user_id) {
172 fake_user_manager_->AddUser(user_id);
173 Profile* profile = profile_manager_.CreateTestingProfile(user_id);
174 content::NotificationService::current()->Notify(
175 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
176 content::NotificationService::AllSources(),
177 content::Details<Profile>(profile));
178 return profile;
179 }
180
181 void AffiliatedInvalidationServiceProviderTest::
182 LogInAsAffiliatedUserAndConnectInvalidationService() {
183 Mock::VerifyAndClearExpectations(&consumer_);
184
185 // Log in as an affiliated user.
186 Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
187 EXPECT_TRUE(profile);
188 Mock::VerifyAndClearExpectations(&consumer_);
189
190 // Verify that a per-profile invalidation service has been created.
191 profile_invalidation_service_ =
192 GetProfileInvalidationService(profile, false /* create */);
193 ASSERT_TRUE(profile_invalidation_service_);
194
195 // Verify that the device-global invalidation service still exists.
196 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
197
198 // Indicate that the per-profile invalidation service has connected. Verify
199 // that the consumer is informed about this.
200 EXPECT_CALL(consumer_,
201 OnInvalidationServiceSet(profile_invalidation_service_)).Times(1);
202 profile_invalidation_service_->SetInvalidatorState(
203 syncer::INVALIDATIONS_ENABLED);
204 Mock::VerifyAndClearExpectations(&consumer_);
205
206 // Verify that the device-global invalidation service has been destroyed.
207 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
208
209 Mock::VerifyAndClearExpectations(&consumer_);
210 }
211
212 void AffiliatedInvalidationServiceProviderTest::
213 LogInAsUnaffiliatedUserAndConnectInvalidationService() {
214 Mock::VerifyAndClearExpectations(&consumer_);
215
216 // Log in as an unaffiliated user.
217 Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID);
218 EXPECT_TRUE(profile);
219
220 // Verify that a per-profile invalidation service has been created.
221 profile_invalidation_service_ =
222 GetProfileInvalidationService(profile, false /* create */);
223 ASSERT_TRUE(profile_invalidation_service_);
224
225 // Verify that the device-global invalidation service still exists.
226 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
227
228 // Indicate that the per-profile invalidation service has connected. Verify
229 // that the consumer is not called back.
230 profile_invalidation_service_->SetInvalidatorState(
231 syncer::INVALIDATIONS_ENABLED);
232
233 // Verify that the device-global invalidation service still exists.
234 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
235
236 Mock::VerifyAndClearExpectations(&consumer_);
237 }
238
239 void AffiliatedInvalidationServiceProviderTest::
240 ConnectDeviceGlobalInvalidationService() {
241 Mock::VerifyAndClearExpectations(&consumer_);
242
243 // Verify that a device-global invalidation service has been created.
244 device_invalidation_service_ =
245 provider_->GetDeviceInvalidationServiceForTest();
246 ASSERT_TRUE(device_invalidation_service_);
247
248 // Indicate that the device-global invalidation service has connected. Verify
249 // that the consumer is informed about this.
250 EXPECT_CALL(consumer_, OnInvalidationServiceSet(device_invalidation_service_))
251 .Times(1);
252 device_invalidation_service_->OnInvalidatorStateChange(
253 syncer::INVALIDATIONS_ENABLED);
254
255 Mock::VerifyAndClearExpectations(&consumer_);
256 }
257
258 void AffiliatedInvalidationServiceProviderTest::
259 DisconnectPerProfileInvalidationService() {
260 Mock::VerifyAndClearExpectations(&consumer_);
261
262 ASSERT_TRUE(profile_invalidation_service_);
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(nullptr)).Times(1);
267 profile_invalidation_service_->SetInvalidatorState(
268 syncer::INVALIDATION_CREDENTIALS_REJECTED);
269
270 // Verify that a device-global invalidation service has been created.
271 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
272
273 Mock::VerifyAndClearExpectations(&consumer_);
274 }
275
276 invalidation::FakeInvalidationService*
277 AffiliatedInvalidationServiceProviderTest::GetProfileInvalidationService(
278 Profile* profile, bool create) {
279 invalidation::ProfileInvalidationProvider* invalidation_provider =
280 static_cast<invalidation::ProfileInvalidationProvider*>(
281 invalidation::ProfileInvalidationProviderFactory::GetInstance()->
282 GetServiceForBrowserContext(profile, create));
283 if (!invalidation_provider)
284 return nullptr;
285 return static_cast<invalidation::FakeInvalidationService*>(
286 invalidation_provider->GetInvalidationService());
287 }
288
289 // No consumers are registered with the AffiliatedInvalidationServiceProvider.
290 // Verifies that no device-global invalidation service is created, whether an
291 // affiliated user is logged in or not.
292 TEST_F(AffiliatedInvalidationServiceProviderTest, NoConsumers) {
293 // Verify that no device-global invalidation service has been created.
294 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
295
296 // Log in as an affiliated user.
297 EXPECT_TRUE(LogInAndReturnProfile(kAffiliatedUserID1));
298
299 // Verify that no device-global invalidation service has been created.
300 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
301 }
302
303 // A consumer is registered with the AffiliatedInvalidationServiceProvider.
304 // Verifies that when no per-profile invalidation service belonging to an
305 // affiliated user is available, a device-global invalidation service is
306 // created. Further verifies that when the device-global invalidation service
307 // connects, it is made available to the consumer.
308 TEST_F(AffiliatedInvalidationServiceProviderTest,
309 UseDeviceInvalidationService) {
310 // Register a consumer. Verify that the consumer is not called back
311 // immediately as no connected invalidation service exists yet.
312 provider_->RegisterConsumer(&consumer_);
313
314 // Indicate that the device-global invalidation service connected. Verify that
315 // that the consumer is informed about this.
316 ConnectDeviceGlobalInvalidationService();
317
318 // Indicate that the device-global invalidation service has disconnected.
319 // Verify that the consumer is informed about this.
320 EXPECT_CALL(consumer_, OnInvalidationServiceSet(nullptr)).Times(1);
321 device_invalidation_service_->OnInvalidatorStateChange(
322 syncer::INVALIDATION_CREDENTIALS_REJECTED);
323 Mock::VerifyAndClearExpectations(&consumer_);
324
325 // Verify that the device-global invalidation service still exists.
326 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
327
328 // Unregister the consumer.
329 provider_->UnregisterConsumer(&consumer_);
330 Mock::VerifyAndClearExpectations(&consumer_);
331 }
332
333 // A consumer is registered with the AffiliatedInvalidationServiceProvider.
334 // Verifies that when a per-profile invalidation service belonging to an
335 // affiliated user connects, it is made available to the consumer.
336 TEST_F(AffiliatedInvalidationServiceProviderTest,
337 UseAffiliatedProfileInvalidationService) {
338 // Register a consumer. Verify that the consumer is not called back
339 // immediately as no connected invalidation service exists yet.
340 provider_->RegisterConsumer(&consumer_);
341
342 // Verify that a device-global invalidation service has been created.
343 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
344
345 // Log in as an affiliated user and indicate that the per-profile invalidation
346 // service for this user connected. Verify that this invalidation service is
347 // made available to the |consumer_| and the device-global invalidation
348 // service is destroyed.
349 LogInAsAffiliatedUserAndConnectInvalidationService();
350
351 // Indicate that the logged-in user's per-profile invalidation service
352 // disconnected. Verify that the consumer is informed about this and a
353 // device-global invalidation service is created.
354 DisconnectPerProfileInvalidationService();
355
356 // Unregister the consumer.
357 provider_->UnregisterConsumer(&consumer_);
358 Mock::VerifyAndClearExpectations(&consumer_);
359 }
360
361 // A consumer is registered with the AffiliatedInvalidationServiceProvider.
362 // Verifies that when a per-profile invalidation service belonging to an
363 // unaffiliated user connects, it is ignored.
364 TEST_F(AffiliatedInvalidationServiceProviderTest,
365 DoNotUseUnaffiliatedProfileInvalidationService) {
366 // Register a consumer. Verify that the consumer is not called back
367 // immediately as no connected invalidation service exists yet.
368 provider_->RegisterConsumer(&consumer_);
369
370 // Verify that a device-global invalidation service has been created.
371 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
372
373 // Log in as an unaffiliated user and indicate that the per-profile
374 // invalidation service for this user connected. Verify that this invalidation
375 // service is ignored and the device-global invalidation service is not
376 // destroyed.
377 LogInAsUnaffiliatedUserAndConnectInvalidationService();
378
379 // Unregister the consumer.
380 provider_->UnregisterConsumer(&consumer_);
381 Mock::VerifyAndClearExpectations(&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
387 // belonging to an affiliated user connects, it is made available to the
388 // consumer instead and the device-global invalidation service is destroyed.
389 TEST_F(AffiliatedInvalidationServiceProviderTest,
390 SwitchToAffiliatedProfileInvalidationService) {
391 // Register a consumer. Verify that the consumer is not called back
392 // immediately as no connected invalidation service exists yet.
393 provider_->RegisterConsumer(&consumer_);
394
395 // Indicate that the device-global invalidation service connected. Verify that
396 // that the consumer is informed about this.
397 ConnectDeviceGlobalInvalidationService();
398
399 // Log in as an affiliated user and indicate that the per-profile invalidation
400 // service for this user connected. Verify that this invalidation service is
401 // made available to the |consumer_| and the device-global invalidation
402 // service is destroyed.
403 LogInAsAffiliatedUserAndConnectInvalidationService();
404
405 // Unregister the consumer.
406 provider_->UnregisterConsumer(&consumer_);
407 Mock::VerifyAndClearExpectations(&consumer_);
408 }
409
410 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A
411 // device-global invalidation service exists, is connected and is made available
412 // to the consumer. Verifies that when a per-profile invalidation service
413 // belonging to an unaffiliated user connects, it is ignored and the
414 // device-global invalidation service continues to be made available to the
415 // consumer.
416 TEST_F(AffiliatedInvalidationServiceProviderTest,
417 DoNotSwitchToUnaffiliatedProfileInvalidationService) {
418 // Register a consumer. Verify that the consumer is not called back
419 // immediately as no connected invalidation service exists yet.
420 provider_->RegisterConsumer(&consumer_);
421
422 // Indicate that the device-global invalidation service connected. Verify that
423 // that the consumer is informed about this.
424 ConnectDeviceGlobalInvalidationService();
425
426 // Log in as an unaffiliated user and indicate that the per-profile
427 // invalidation service for this user connected. Verify that this invalidation
428 // service is ignored and the device-global invalidation service is not
429 // destroyed.
430 LogInAsUnaffiliatedUserAndConnectInvalidationService();
431
432 // Unregister the consumer.
433 provider_->UnregisterConsumer(&consumer_);
434 Mock::VerifyAndClearExpectations(&consumer_);
435 }
436
437 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A
438 // per-profile invalidation service belonging to an affiliated user exists, is
439 // connected and is made available to the consumer. Verifies that when the
440 // per-profile invalidation service disconnects, a device-global invalidation
441 // service is created. Further verifies that when the device-global invalidation
442 // service connects, it is made available to the consumer.
443 TEST_F(AffiliatedInvalidationServiceProviderTest,
444 SwitchToDeviceInvalidationService) {
445 // Register a consumer. Verify that the consumer is not called back
446 // immediately as no connected invalidation service exists yet.
447 provider_->RegisterConsumer(&consumer_);
448
449 // Verify that a device-global invalidation service has been created.
450 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
451
452 // Log in as an affiliated user and indicate that the per-profile invalidation
453 // service for this user connected. Verify that this invalidation service is
454 // made available to the |consumer_| and the device-global invalidation
455 // service is destroyed.
456 LogInAsAffiliatedUserAndConnectInvalidationService();
457
458 // Indicate that the logged-in user's per-profile invalidation service
459 // disconnected. Verify that the consumer is informed about this and a
460 // device-global invalidation service is created.
461 DisconnectPerProfileInvalidationService();
462
463 // Indicate that the device-global invalidation service connected. Verify that
464 // that the consumer is informed about this.
465 ConnectDeviceGlobalInvalidationService();
466
467 // Unregister the consumer.
468 provider_->UnregisterConsumer(&consumer_);
469 Mock::VerifyAndClearExpectations(&consumer_);
470 }
471
472 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A
473 // per-profile invalidation service belonging to a first affiliated user exists,
474 // is connected and is made available to the consumer. A per-profile
475 // invalidation service belonging to a second affiliated user also exists and is
476 // connected. Verifies that when the per-profile invalidation service belonging
477 // to the first user disconnects, the per-profile invalidation service belonging
478 // to the second user is made available to the consumer instead.
479 TEST_F(AffiliatedInvalidationServiceProviderTest,
480 SwitchBetweenAffiliatedProfileInvalidationServices) {
481 // Register a consumer. Verify that the consumer is not called back
482 // immediately as no connected invalidation service exists yet.
483 provider_->RegisterConsumer(&consumer_);
484
485 // Verify that a device-global invalidation service has been created.
486 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
487
488 // Log in as a first affiliated user and indicate that the per-profile
489 // invalidation service for this user connected. Verify that this invalidation
490 // service is made available to the |consumer_| and the device-global
491 // invalidation service is destroyed.
492 LogInAsAffiliatedUserAndConnectInvalidationService();
493
494 // Log in as a second affiliated user.
495 Profile* second_profile = LogInAndReturnProfile(kAffiliatedUserID2);
496 EXPECT_TRUE(second_profile);
497
498 // Verify that the device-global invalidation service still does not exist.
499 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
500
501 // Verify that a per-profile invalidation service for the second user has been
502 // created.
503 invalidation::FakeInvalidationService* second_profile_invalidation_service =
504 GetProfileInvalidationService(second_profile, false /* create */);
505 ASSERT_TRUE(second_profile_invalidation_service);
506
507 // Indicate that the second user's per-profile invalidation service has
508 // connected. Verify that the consumer is not called back.
509 second_profile_invalidation_service->SetInvalidatorState(
510 syncer::INVALIDATIONS_ENABLED);
511 Mock::VerifyAndClearExpectations(&consumer_);
512
513 // Indicate that the first user's per-profile invalidation service has
514 // disconnected. Verify that the consumer is informed that the second user's
515 // per-profile invalidation service should be used instead of the first
516 // user's.
517 EXPECT_CALL(consumer_,
518 OnInvalidationServiceSet(second_profile_invalidation_service))
519 .Times(1);
520 profile_invalidation_service_->SetInvalidatorState(
521 syncer::INVALIDATION_CREDENTIALS_REJECTED);
522 Mock::VerifyAndClearExpectations(&consumer_);
523
524 // Verify that the device-global invalidation service still does not exist.
525 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
526
527 // Unregister the consumer.
528 provider_->UnregisterConsumer(&consumer_);
529 Mock::VerifyAndClearExpectations(&consumer_);
530 }
531
532 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A
533 // device-global invalidation service exists, is connected and is made available
534 // to the consumer. Verifies that when a second consumer registers, the
535 // device-global invalidation service is made available to it as well. Further
536 // verifies that when the first consumer unregisters, the device-global
537 // invalidation service is not destroyed and remains available to the second
538 // consumer. Further verifies that when the second consumer also unregisters,
539 // the device-global invalidation service is destroyed.
540 TEST_F(AffiliatedInvalidationServiceProviderTest, MultipleConsumers) {
541 // Register a first consumer. Verify that the consumer is not called back
542 // immediately as no connected invalidation service exists yet.
543 provider_->RegisterConsumer(&consumer_);
544
545 // Indicate that the device-global invalidation service connected. Verify that
546 // that the consumer is informed about this.
547 ConnectDeviceGlobalInvalidationService();
548
549 // Register a second consumer. Verify that the consumer is called back
550 // immediately as a connected invalidation service is available.
551 StrictMock<MockConsumer> second_consumer;
552 EXPECT_CALL(second_consumer,
553 OnInvalidationServiceSet(device_invalidation_service_)).Times(1);
554 provider_->RegisterConsumer(&second_consumer);
555 Mock::VerifyAndClearExpectations(&second_consumer);
556
557 // Unregister the first consumer.
558 provider_->UnregisterConsumer(&consumer_);
559
560 // Verify that the device-global invalidation service still exists.
561 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
562
563 // Unregister the second consumer.
564 provider_->UnregisterConsumer(&second_consumer);
565
566 // Verify that the device-global invalidation service has been destroyed.
567 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
568 Mock::VerifyAndClearExpectations(&consumer_);
569 Mock::VerifyAndClearExpectations(&second_consumer);
570 }
571
572 // A consumer is registered with the AffiliatedInvalidationServiceProvider. A
573 // per-profile invalidation service belonging to a first affiliated user exists,
574 // is connected and is made available to the consumer. Verifies that when the
575 // provider is shut down, the consumer is informed that no invalidation service
576 // is available for use anymore. Also verifies that no device-global
577 // invalidation service is created and a per-profile invalidation service
578 // belonging to a second affiliated user that subsequently connects is ignored.
579 TEST_F(AffiliatedInvalidationServiceProviderTest, NoServiceAfterShutdown) {
580 // Register a consumer. Verify that the consumer is not called back
581 // immediately as no connected invalidation service exists yet.
582 provider_->RegisterConsumer(&consumer_);
583
584 // Verify that a device-global invalidation service has been created.
585 EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
586
587 // Log in as a first affiliated user and indicate that the per-profile
588 // invalidation service for this user connected. Verify that this invalidation
589 // service is made available to the |consumer_| and the device-global
590 // invalidation service is destroyed.
591 LogInAsAffiliatedUserAndConnectInvalidationService();
592
593 // Shut down the |provider_|. Verify that the |consumer_| is informed that no
594 // invalidation service is available for use anymore.
595 EXPECT_CALL(consumer_, OnInvalidationServiceSet(nullptr)).Times(1);
596 provider_->Shutdown();
597 Mock::VerifyAndClearExpectations(&consumer_);
598
599 // Verify that the device-global invalidation service still does not exist.
600 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
601
602 // Log in as a second affiliated user.
603 Profile* second_profile = LogInAndReturnProfile(kAffiliatedUserID2);
604 EXPECT_TRUE(second_profile);
605
606 // Verify that the device-global invalidation service still does not exist.
607 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
608
609 // Create a per-profile invalidation service for the second user.
610 invalidation::FakeInvalidationService* second_profile_invalidation_service =
611 GetProfileInvalidationService(second_profile, true /* create */);
612 ASSERT_TRUE(second_profile_invalidation_service);
613
614 // Indicate that the second user's per-profile invalidation service has
615 // connected. Verify that the consumer is not called back.
616 second_profile_invalidation_service->SetInvalidatorState(
617 syncer::INVALIDATIONS_ENABLED);
618
619 // Verify that the device-global invalidation service still does not exist.
620 EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
621
622 // Unregister the consumer.
623 provider_->UnregisterConsumer(&consumer_);
624 Mock::VerifyAndClearExpectations(&consumer_);
625 }
626
627 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698