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

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

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

Powered by Google App Engine
This is Rietveld 408576698