account_reconcilor_unittest.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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_factory.h"
9e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/signin/chrome_signin_client_factory.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_signin_manager.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
16c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "components/signin/core/browser/account_reconcilor.h"
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestEmail[] = "user@gmail.com";
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
31e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  static KeyedService* Build(content::BrowserContext* context);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  MockAccountReconcilor(ProfileOAuth2TokenService* token_service,
34c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                        SigninManagerBase* signin_manager,
35c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                        SigninClient* client);
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~MockAccountReconcilor() {}
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(StartRemoveAction, void(const std::string& account_id));
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MOCK_METHOD3(
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      FinishRemoveAction,
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      void(const std::string& account_id,
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const GoogleServiceAuthError& error,
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const std::vector<std::pair<std::string, bool> >& accounts));
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD2(PerformAddToChromeAction, void(const std::string& account_id,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              int session_index));
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochKeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) {
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  Profile* profile = Profile::FromBrowserContext(context);
53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  AccountReconcilor* reconcilor = new MockAccountReconcilor(
54c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
55c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      SigninManagerFactory::GetForProfile(profile),
56c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ChromeSigninClientFactory::GetForProfile(profile));
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->Initialize(false /* start_reconcile_if_tokens_available */);
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reconcilor;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
61c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen MurdochMockAccountReconcilor::MockAccountReconcilor(
62c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    ProfileOAuth2TokenService* token_service,
63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    SigninManagerBase* signin_manager,
64c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    SigninClient* client)
65c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    : testing::StrictMock<AccountReconcilor>(token_service,
66c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                             signin_manager,
67c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                             client) {}
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class AccountReconcilorTest : public testing::Test {
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilorTest();
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUp() OVERRIDE;
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void TearDown() OVERRIDE;
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile* profile() { return profile_.get(); }
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service() { return token_service_; }
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetFakeResponse(const std::string& url,
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::string& data,
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::HttpStatusCode code,
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::URLRequestStatus::Status status) {
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* GetMockReconcilor();
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateMergeSessionCompleted(
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      MergeSessionHelper::Observer* observer,
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const GoogleServiceAuthError& error);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateRefreshTokenFetched(
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilor* reconcilor,
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& refresh_token);
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::TestBrowserThreadBundle bundle_;
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<TestingProfile> profile_;
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager_;
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service_;
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* mock_reconcilor_;
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::FakeURLFetcherFactory url_fetcher_factory_;
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)AccountReconcilorTest::AccountReconcilorTest()
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : signin_manager_(NULL),
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      token_service_(NULL),
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      mock_reconcilor_(NULL),
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      url_fetcher_factory_(NULL) {}
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::SetUp() {
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile::Builder builder;
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
118effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                            BuildFakeProfileOAuth2TokenService);
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            FakeSigninManagerBase::Build);
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  builder.AddTestingFactory(AccountReconcilorFactory::GetInstance(),
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            MockAccountReconcilor::Build);
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_ = builder.Build();
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager_ =
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeSigninManagerForTesting*>(
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          SigninManagerFactory::GetForProfile(profile()));
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  token_service_ =
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeProfileOAuth2TokenService*>(
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
1328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::TearDown() {
1358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Destroy the profile before all threads are torn down.
1368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_.reset();
1378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!mock_reconcilor_) {
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_reconcilor_ =
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        static_cast<MockAccountReconcilor*>(
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            AccountReconcilorFactory::GetForProfile(profile()));
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return mock_reconcilor_;
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateMergeSessionCompleted(
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    MergeSessionHelper::Observer* observer,
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GoogleServiceAuthError& error) {
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  observer->MergeSessionCompleted(account_id, error);
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateRefreshTokenFetched(
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AccountReconcilor* reconcilor,
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& refresh_token) {
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->HandleRefreshTokenFetched(account_id, refresh_token);
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(AccountReconcilorTest, Basic) {
1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AccountReconcilor* reconcilor =
1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
167c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ASSERT_EQ(token_service(), reconcilor->token_service());
1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if !defined(OS_CHROMEOS)
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
1731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
1741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
1801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SignOut();
1841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, Reauth) {
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Simulate reauth.  The state of the reconcilor should not change.
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // !defined(OS_CHROMEOS)
2041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
2061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
2091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::vector<std::pair<std::string, bool> >& accounts =
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reconcilor->GetGaiaAccountsForTesting();
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, accounts.size());
234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ("user@gmail.com", accounts[0].first);
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), "",
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetGaiaAccountsForTesting().size());
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokens) {
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetValidChromeAccountsForTesting().size());
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetInvalidChromeAccountsForTesting().size());
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedUserInfo) {
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "", net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedTokenRequest) {
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueErrorForAllPendingRequests(
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoop) {
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
326a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
327a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This is test is needed until chrome changes to use gaia obfuscated id.
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The signin manager and token service use the gaia "email" property, which
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
352a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// however uses gaia "displayEmail" which does not preserve case, and then
353a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// passes the string through gaia::CanonicalizeEmail() which removes dots.  This
354a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// tests makes sure that an email like "Dot.S@hmail.com", as seen by the
355a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// token service, will be considered the same as "dots@gmail.com" as returned
356a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// by gaia::ParseListAccountsData().
357a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopWithDots) {
358a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_STREQ("dots@gmail.com",
379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               reconcilor->GetGaiaAccountsForTesting()[0].first.c_str());
380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("Dot.S@gmail.com", "access_token",
383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopMultiple) {
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(2u, reconcilor->GetGaiaAccountsForTesting().size());
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToCookie) {
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
436a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToChrome) {
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(),
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              PerformAddToChromeAction("other@gmail.com", 1));
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
463a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
464a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
465a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateRefreshTokenFetched(reconcilor, "other@gmail.com", "");
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileBadPrimary) {
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
490a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
491a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
492a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
513f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileOnlyOnce) {
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
5195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
5205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
5215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
522a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
523a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
525a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
526a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
527a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
528a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
529a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
530a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
531a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
532a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
533a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
534a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
535a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
538a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
542a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
544a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
548a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
5495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
5515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
553a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
554a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
555a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
5595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
5625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
563a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
564a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
567a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
568a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
571