Index: components/signin/core/browser/account_tracker_service_unittest.cc |
diff --git a/components/signin/core/browser/account_tracker_service_unittest.cc b/components/signin/core/browser/account_tracker_service_unittest.cc |
index eb6751cb46cea84a464a142692c53630c918978e..8cc4e920771e1b4bd34ee7c6866fa486862df1a3 100644 |
--- a/components/signin/core/browser/account_tracker_service_unittest.cc |
+++ b/components/signin/core/browser/account_tracker_service_unittest.cc |
@@ -245,6 +245,29 @@ testing::AssertionResult AccountTrackerObserver::CheckEvents( |
} // namespace |
+class InfoMutatingFakeAccountFetcherService : public FakeAccountFetcherService { |
+ public: |
+ InfoMutatingFakeAccountFetcherService(AccountInfo new_info) |
+ : info_(new_info) {} |
+ |
+ void StartFetchingUserInfo(const std::string& account_id) override { |
+ // It would make no sense to return an AccountInfo with a different ID. |
+ EXPECT_EQ(info_.account_id, account_id); |
+ // Immediately fake a response with the provided |info_|. |
+ FakeUserInfoFetchSuccess(account_id, |
+ info_.email, |
+ info_.gaia, |
+ info_.hosted_domain, |
+ info_.full_name, |
+ info_.given_name, |
+ info_.locale, |
+ info_.picture_url); |
+ } |
+ |
+ private: |
+ AccountInfo info_; |
+}; |
+ |
class AccountTrackerServiceTest : public testing::Test { |
public: |
AccountTrackerServiceTest() {} |
@@ -326,6 +349,7 @@ class AccountTrackerServiceTest : public testing::Test { |
return fake_oauth2_token_service_.get(); |
} |
SigninClient* signin_client() { return signin_client_.get(); } |
+ PrefService* pref_service() { return &pref_service_; } |
private: |
void ReturnOAuthUrlFetchResults(int fetcher_id, |
@@ -1171,3 +1195,55 @@ TEST_F(AccountTrackerServiceTest, ChildAccountGraduation) { |
fetcher.Shutdown(); |
tracker.Shutdown(); |
} |
+ |
+TEST_F(AccountTrackerServiceTest, RefreshOnTokensLoadedWhenNeeded) { |
+ std::string account_id("account_id"); |
+ AccountInfo info; |
+ { |
+ AccountTrackerService tracker; |
+ tracker.Initialize(signin_client()); |
+ FakeAccountFetcherService fetcher; |
+ fetcher.Initialize(signin_client(), token_service(), &tracker, nullptr); |
+ fetcher.EnableNetworkFetches(); |
+ fetcher.FakeUserInfoFetchSuccess(account_id, |
+ "original@example.com", |
+ account_id, |
+ "", |
+ "first", |
+ "fi", |
+ "en-US", |
+ ""); |
+ info = tracker.GetAccountInfo(account_id); |
+ EXPECT_TRUE(info.IsValid()); |
+ EXPECT_EQ("original@example.com", info.email); |
+ } |
+ |
+ // Set the last update to a long time ago to trigger a refresh when the AFS |
+ // starts. |
+ pref_service()->SetInt64(AccountFetcherService::kLastUpdatePref, 0); |
+ |
+ { |
+ // Prepare the new account info that will be fetched for |account_id|. |
+ info.email = "mutated@example.com"; |
+ AccountTrackerService tracker; |
+ tracker.Initialize(signin_client()); |
+ InfoMutatingFakeAccountFetcherService fetcher(info); |
+ fetcher.Initialize(signin_client(), token_service(), &tracker, nullptr); |
+ fetcher.EnableNetworkFetches(); |
+ |
+ info = tracker.GetAccountInfo(account_id); |
+ // The persisted info should be returned since the refresh tokens aren't |
+ // loaded yet. |
+ EXPECT_TRUE(info.IsValid()); |
+ EXPECT_EQ("original@example.com", info.email); |
+ |
+ // Trigger a OnRefreshTokensLoaded() notification. |
+ static_cast<FakeOAuth2TokenService*>(token_service())-> |
+ GetFakeOAuth2TokenServiceDelegate()->LoadCredentials(account_id); |
+ |
+ info = tracker.GetAccountInfo(account_id); |
+ EXPECT_TRUE(info.IsValid()); |
+ EXPECT_EQ("mutated@example.com", info.email); |
+ } |
+} |
+ |