OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <algorithm> | 5 #include <algorithm> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/prefs/pref_registry_simple.h" | 8 #include "base/prefs/pref_registry_simple.h" |
9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
10 #include "base/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 ? testing::AssertionSuccess() | 238 ? testing::AssertionSuccess() |
239 : (testing::AssertionFailure() | 239 : (testing::AssertionFailure() |
240 << "Expected " << maybe_newline << Str(events) << ", " | 240 << "Expected " << maybe_newline << Str(events) << ", " |
241 << maybe_newline << "Got " << maybe_newline << Str(events_))); | 241 << maybe_newline << "Got " << maybe_newline << Str(events_))); |
242 events_.clear(); | 242 events_.clear(); |
243 return result; | 243 return result; |
244 } | 244 } |
245 | 245 |
246 } // namespace | 246 } // namespace |
247 | 247 |
| 248 class InfoMutatingFakeAccountFetcherService : public FakeAccountFetcherService { |
| 249 public: |
| 250 InfoMutatingFakeAccountFetcherService(AccountInfo new_info) |
| 251 : info_(new_info) {} |
| 252 |
| 253 void StartFetchingUserInfo(const std::string& account_id) override { |
| 254 // It would make no sense to return an AccountInfo with a different ID. |
| 255 EXPECT_EQ(info_.account_id, account_id); |
| 256 // Immediately fake a response with the provided |info_|. |
| 257 FakeUserInfoFetchSuccess(account_id, |
| 258 info_.email, |
| 259 info_.gaia, |
| 260 info_.hosted_domain, |
| 261 info_.full_name, |
| 262 info_.given_name, |
| 263 info_.locale, |
| 264 info_.picture_url); |
| 265 } |
| 266 |
| 267 private: |
| 268 AccountInfo info_; |
| 269 }; |
| 270 |
248 class AccountTrackerServiceTest : public testing::Test { | 271 class AccountTrackerServiceTest : public testing::Test { |
249 public: | 272 public: |
250 AccountTrackerServiceTest() {} | 273 AccountTrackerServiceTest() {} |
251 | 274 |
252 ~AccountTrackerServiceTest() override {} | 275 ~AccountTrackerServiceTest() override {} |
253 | 276 |
254 void SetUp() override { | 277 void SetUp() override { |
255 fake_oauth2_token_service_.reset(new FakeOAuth2TokenService()); | 278 fake_oauth2_token_service_.reset(new FakeOAuth2TokenService()); |
256 | 279 |
257 pref_service_.registry()->RegisterListPref( | 280 pref_service_.registry()->RegisterListPref( |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 | 342 |
320 net::TestURLFetcherFactory* test_fetcher_factory() { | 343 net::TestURLFetcherFactory* test_fetcher_factory() { |
321 return &test_fetcher_factory_; | 344 return &test_fetcher_factory_; |
322 } | 345 } |
323 AccountFetcherService* account_fetcher() { return account_fetcher_.get(); } | 346 AccountFetcherService* account_fetcher() { return account_fetcher_.get(); } |
324 AccountTrackerService* account_tracker() { return account_tracker_.get(); } | 347 AccountTrackerService* account_tracker() { return account_tracker_.get(); } |
325 OAuth2TokenService* token_service() { | 348 OAuth2TokenService* token_service() { |
326 return fake_oauth2_token_service_.get(); | 349 return fake_oauth2_token_service_.get(); |
327 } | 350 } |
328 SigninClient* signin_client() { return signin_client_.get(); } | 351 SigninClient* signin_client() { return signin_client_.get(); } |
| 352 PrefService* pref_service() { return &pref_service_; } |
329 | 353 |
330 private: | 354 private: |
331 void ReturnOAuthUrlFetchResults(int fetcher_id, | 355 void ReturnOAuthUrlFetchResults(int fetcher_id, |
332 net::HttpStatusCode response_code, | 356 net::HttpStatusCode response_code, |
333 const std::string& response_string); | 357 const std::string& response_string); |
334 | 358 |
335 base::MessageLoopForIO message_loop_; | 359 base::MessageLoopForIO message_loop_; |
336 net::TestURLFetcherFactory test_fetcher_factory_; | 360 net::TestURLFetcherFactory test_fetcher_factory_; |
337 scoped_ptr<FakeOAuth2TokenService> fake_oauth2_token_service_; | 361 scoped_ptr<FakeOAuth2TokenService> fake_oauth2_token_service_; |
338 TestingPrefServiceSimple pref_service_; | 362 TestingPrefServiceSimple pref_service_; |
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1164 ASSERT_FALSE(info.is_child_account); | 1188 ASSERT_FALSE(info.is_child_account); |
1165 ASSERT_TRUE(observer.CheckEvents(TrackingEvent(UPDATED, child_id))); | 1189 ASSERT_TRUE(observer.CheckEvents(TrackingEvent(UPDATED, child_id))); |
1166 | 1190 |
1167 SimulateTokenRevoked(child_id); | 1191 SimulateTokenRevoked(child_id); |
1168 ASSERT_TRUE(observer.CheckEvents(TrackingEvent(REMOVED, child_id))); | 1192 ASSERT_TRUE(observer.CheckEvents(TrackingEvent(REMOVED, child_id))); |
1169 | 1193 |
1170 tracker.RemoveObserver(&observer); | 1194 tracker.RemoveObserver(&observer); |
1171 fetcher.Shutdown(); | 1195 fetcher.Shutdown(); |
1172 tracker.Shutdown(); | 1196 tracker.Shutdown(); |
1173 } | 1197 } |
| 1198 |
| 1199 TEST_F(AccountTrackerServiceTest, RefreshOnTokensLoadedWhenNeeded) { |
| 1200 std::string account_id("account_id"); |
| 1201 AccountInfo info; |
| 1202 { |
| 1203 AccountTrackerService tracker; |
| 1204 tracker.Initialize(signin_client()); |
| 1205 FakeAccountFetcherService fetcher; |
| 1206 fetcher.Initialize(signin_client(), token_service(), &tracker, nullptr); |
| 1207 fetcher.EnableNetworkFetches(); |
| 1208 fetcher.FakeUserInfoFetchSuccess(account_id, |
| 1209 "original@example.com", |
| 1210 account_id, |
| 1211 "", |
| 1212 "first", |
| 1213 "fi", |
| 1214 "en-US", |
| 1215 ""); |
| 1216 info = tracker.GetAccountInfo(account_id); |
| 1217 EXPECT_TRUE(info.IsValid()); |
| 1218 EXPECT_EQ("original@example.com", info.email); |
| 1219 } |
| 1220 |
| 1221 // Set the last update to a long time ago to trigger a refresh when the AFS |
| 1222 // starts. |
| 1223 pref_service()->SetInt64(AccountFetcherService::kLastUpdatePref, 0); |
| 1224 |
| 1225 { |
| 1226 // Prepare the new account info that will be fetched for |account_id|. |
| 1227 info.email = "mutated@example.com"; |
| 1228 AccountTrackerService tracker; |
| 1229 tracker.Initialize(signin_client()); |
| 1230 InfoMutatingFakeAccountFetcherService fetcher(info); |
| 1231 fetcher.Initialize(signin_client(), token_service(), &tracker, nullptr); |
| 1232 fetcher.EnableNetworkFetches(); |
| 1233 |
| 1234 info = tracker.GetAccountInfo(account_id); |
| 1235 // The persisted info should be returned since the refresh tokens aren't |
| 1236 // loaded yet. |
| 1237 EXPECT_TRUE(info.IsValid()); |
| 1238 EXPECT_EQ("original@example.com", info.email); |
| 1239 |
| 1240 // Trigger a OnRefreshTokensLoaded() notification. |
| 1241 static_cast<FakeOAuth2TokenService*>(token_service())-> |
| 1242 GetFakeOAuth2TokenServiceDelegate()->LoadCredentials(account_id); |
| 1243 |
| 1244 info = tracker.GetAccountInfo(account_id); |
| 1245 EXPECT_TRUE(info.IsValid()); |
| 1246 EXPECT_EQ("mutated@example.com", info.email); |
| 1247 } |
| 1248 } |
| 1249 |
OLD | NEW |