account_reconcilor_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/run_loop.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/time.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor_factory.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_signin_manager.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestEmail[] = "user@gmail.com";
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static KeyedService* Build(content::BrowserContext* profile);
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit MockAccountReconcilor(Profile* profile);
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~MockAccountReconcilor() {}
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(StartRemoveAction, void(const std::string& account_id));
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MOCK_METHOD3(
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      FinishRemoveAction,
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      void(const std::string& account_id,
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const GoogleServiceAuthError& error,
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const std::vector<std::pair<std::string, bool> >& accounts));
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD2(PerformAddToChromeAction, void(const std::string& account_id,
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              int session_index));
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)KeyedService* MockAccountReconcilor::Build(content::BrowserContext* profile) {
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new MockAccountReconcilor(static_cast<Profile*>(profile));
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->Initialize(false /* start_reconcile_if_tokens_available */);
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reconcilor;
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MockAccountReconcilor::MockAccountReconcilor(Profile* profile)
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : testing::StrictMock<AccountReconcilor>(profile) {}
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class AccountReconcilorTest : public testing::Test {
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilorTest();
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUp() OVERRIDE;
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void TearDown() OVERRIDE;
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile* profile() { return profile_.get(); }
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service() { return token_service_; }
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetFakeResponse(const std::string& url,
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::string& data,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::HttpStatusCode code,
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::URLRequestStatus::Status status) {
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* GetMockReconcilor();
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateMergeSessionCompleted(
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      MergeSessionHelper::Observer* observer,
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const GoogleServiceAuthError& error);
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateRefreshTokenFetched(
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilor* reconcilor,
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& refresh_token);
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::TestBrowserThreadBundle bundle_;
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<TestingProfile> profile_;
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager_;
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service_;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* mock_reconcilor_;
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::FakeURLFetcherFactory url_fetcher_factory_;
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)AccountReconcilorTest::AccountReconcilorTest()
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : signin_manager_(NULL),
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      token_service_(NULL),
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      mock_reconcilor_(NULL),
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      url_fetcher_factory_(NULL) {}
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::SetUp() {
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile::Builder builder;
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            FakeProfileOAuth2TokenServiceWrapper::Build);
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            FakeSigninManagerBase::Build);
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  builder.AddTestingFactory(AccountReconcilorFactory::GetInstance(),
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            MockAccountReconcilor::Build);
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_ = builder.Build();
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager_ =
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeSigninManagerForTesting*>(
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          SigninManagerFactory::GetForProfile(profile()));
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  token_service_ =
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeProfileOAuth2TokenService*>(
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::TearDown() {
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Destroy the profile before all threads are torn down.
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_.reset();
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!mock_reconcilor_) {
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_reconcilor_ =
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        static_cast<MockAccountReconcilor*>(
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            AccountReconcilorFactory::GetForProfile(profile()));
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return mock_reconcilor_;
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateMergeSessionCompleted(
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    MergeSessionHelper::Observer* observer,
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GoogleServiceAuthError& error) {
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  observer->MergeSessionCompleted(account_id, error);
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateRefreshTokenFetched(
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AccountReconcilor* reconcilor,
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& refresh_token) {
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->HandleRefreshTokenFetched(account_id, refresh_token);
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(AccountReconcilorTest, Basic) {
1538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AccountReconcilor* reconcilor =
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_EQ(profile(), reconcilor->profile());
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if !defined(OS_CHROMEOS)
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
1631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SignOut();
1731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, Reauth) {
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Simulate reauth.  The state of the reconcilor should not change.
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // !defined(OS_CHROMEOS)
1931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
1951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
1961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
1981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::vector<std::pair<std::string, bool> >& accounts =
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reconcilor->GetGaiaAccountsForTesting();
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, accounts.size());
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ("user@gmail.com", accounts[0].first);
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), "",
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetGaiaAccountsForTesting().size());
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokens) {
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetValidChromeAccountsForTesting().size());
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetInvalidChromeAccountsForTesting().size());
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedUserInfo) {
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "", net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedTokenRequest) {
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueErrorForAllPendingRequests(
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoop) {
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
338a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This is test is needed until chrome changes to use gaia obfuscated id.
339a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The signin manager and token service use the gaia "email" property, which
340a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
341a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// however uses gaia "displayEmail" which does not preserve case, and then
342a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// passes the string through gaia::CanonicalizeEmail() which removes dots.  This
343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// tests makes sure that an email like "Dot.S@hmail.com", as seen by the
344a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// token service, will be considered the same as "dots@gmail.com" as returned
345a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// by gaia::ParseListAccountsData().
346a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopWithDots) {
347a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
348a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
352a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
353a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
354a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
355a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
356a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
357a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
358a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_STREQ("dots@gmail.com",
368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               reconcilor->GetGaiaAccountsForTesting()[0].first.c_str());
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("Dot.S@gmail.com", "access_token",
372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopMultiple) {
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
3815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(2u, reconcilor->GetGaiaAccountsForTesting().size());
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToCookie) {
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToChrome) {
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(),
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              PerformAddToChromeAction("other@gmail.com", 1));
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
452a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
453a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
454a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateRefreshTokenFetched(reconcilor, "other@gmail.com", "");
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileBadPrimary) {
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
479a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
480a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
481a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
502f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileOnlyOnce) {
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
5065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
5095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
513a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
516a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
517a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
518a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
519a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
520a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
521a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
522a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
523a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
525a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
526a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
527a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
528a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
529a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
530a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
531a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
532a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
533a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
534a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
535a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
5385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
5405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
542a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
544a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
5485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
5515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
552a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
553a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
557a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
560