account_reconcilor_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/command_line.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/run_loop.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "base/test/histogram_tester.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/time.h"
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "build/build_config.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/prefs/pref_service_syncable.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor_factory.h"
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/signin/chrome_signin_client_factory.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
16effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_signin_manager.h"
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/signin/test_signin_client_builder.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/base/testing_browser_process.h"
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/base/testing_profile_manager.h"
24c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "components/signin/core/browser/account_reconcilor.h"
25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/signin/core/browser/signin_metrics.h"
28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/signin/core/common/profile_management_switches.h"
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/common/signin_switches.h"
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestEmail[] = "user@gmail.com";
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  static KeyedService* Build(content::BrowserContext* context);
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
44c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  MockAccountReconcilor(ProfileOAuth2TokenService* token_service,
45c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                        SigninManagerBase* signin_manager,
46c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                        SigninClient* client);
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~MockAccountReconcilor() {}
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void StartFetchingExternalCcResult() OVERRIDE {
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // Don't do this in tests.
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochKeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) {
59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  Profile* profile = Profile::FromBrowserContext(context);
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  AccountReconcilor* reconcilor = new MockAccountReconcilor(
61c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
62c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      SigninManagerFactory::GetForProfile(profile),
63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ChromeSigninClientFactory::GetForProfile(profile));
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->Initialize(false /* start_reconcile_if_tokens_available */);
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reconcilor;
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
68c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen MurdochMockAccountReconcilor::MockAccountReconcilor(
69c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    ProfileOAuth2TokenService* token_service,
70c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    SigninManagerBase* signin_manager,
71c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    SigninClient* client)
72c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    : testing::StrictMock<AccountReconcilor>(token_service,
73c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                             signin_manager,
74c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                             client) {}
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class AccountReconcilorTest : public ::testing::TestWithParam<bool> {
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilorTest();
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUp() OVERRIDE;
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestingProfile* profile() { return profile_; }
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service() { return token_service_; }
8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  base::HistogramTester* histogram_tester() { return &histogram_tester_; }
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetFakeResponse(const std::string& url,
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::string& data,
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::HttpStatusCode code,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::URLRequestStatus::Status status) {
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* GetMockReconcilor();
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateMergeSessionCompleted(
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      MergeSessionHelper::Observer* observer,
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const GoogleServiceAuthError& error);
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::TestBrowserThreadBundle bundle_;
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestingProfile* profile_;
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager_;
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service_;
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* mock_reconcilor_;
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::FakeURLFetcherFactory url_fetcher_factory_;
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<TestingProfileManager> testing_profile_manager_;
11003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  base::HistogramTester histogram_tester_;
1116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AccountReconcilorTest);
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)AccountReconcilorTest::AccountReconcilorTest()
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : signin_manager_(NULL),
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      token_service_(NULL),
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      mock_reconcilor_(NULL),
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      url_fetcher_factory_(NULL) {}
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::SetUp() {
1226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // If it's a non-parameterized test, or we have a parameter of true, set flag.
1236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (!::testing::UnitTest::GetInstance()->current_test_info()->value_param() ||
1246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      GetParam()) {
1256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    CommandLine::ForCurrentProcess()->AppendSwitch(
1266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        switches::kEnableNewProfileManagement);
1276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  testing_profile_manager_.reset(
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_TRUE(testing_profile_manager_.get()->SetUp());
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestingProfile::TestingFactories factories;
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(ChromeSigninClientFactory::GetInstance(),
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      signin::BuildTestSigninClient));
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ProfileOAuth2TokenServiceFactory::GetInstance(),
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      BuildFakeProfileOAuth2TokenService));
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(SigninManagerFactory::GetInstance(),
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      FakeSigninManagerBase::Build));
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(AccountReconcilorFactory::GetInstance(),
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      MockAccountReconcilor::Build));
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  profile_ = testing_profile_manager_.get()->CreateTestingProfile("name",
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              scoped_ptr<PrefServiceSyncable>(),
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              base::UTF8ToUTF16("name"), 0, std::string(),
147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              factories);
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager_ =
1501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeSigninManagerForTesting*>(
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          SigninManagerFactory::GetForProfile(profile()));
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  token_service_ =
1541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeProfileOAuth2TokenService*>(
1551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!mock_reconcilor_) {
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_reconcilor_ =
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        static_cast<MockAccountReconcilor*>(
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            AccountReconcilorFactory::GetForProfile(profile()));
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return mock_reconcilor_;
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateMergeSessionCompleted(
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    MergeSessionHelper::Observer* observer,
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GoogleServiceAuthError& error) {
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  observer->MergeSessionCompleted(account_id, error);
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(AccountReconcilorTest, Basic) {
1768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AccountReconcilor* reconcilor =
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
179c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ASSERT_EQ(token_service(), reconcilor->token_service());
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if !defined(OS_CHROMEOS)
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This method requires the use of the |TestSigninClient| to be created from the
185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// method with an empty implementation. On MacOS, the normal implementation
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// causes the try_bots to time out.
1881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
1891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
1901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
194cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  signin_manager()->set_password("password");
1951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
198cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
199cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
200116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  signin_manager()->SignOut(signin_metrics::SIGNOUT_TEST);
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This method requires the use of the |TestSigninClient| to be created from the
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// method with an empty implementation. On MacOS, the normal implementation
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// causes the try_bots to time out.
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, Reauth) {
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  signin_manager()->set_password("password");
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Simulate reauth.  The state of the reconcilor should not change.
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // !defined(OS_CHROMEOS)
2231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
2251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
2281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction(kTestEmail));
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
250a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::vector<std::pair<std::string, bool> >& accounts =
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reconcilor->GetGaiaAccountsForTesting();
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, accounts.size());
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ("user@gmail.com", accounts[0].first);
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
263a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), "",
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileNoop) {
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_TRUE(reconcilor->is_reconcile_started_);
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
2916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
29203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectTotalCount(
2936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun", 1);
29403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
2956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
2966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::ACCOUNTS_SAME,
2976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
300a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This is test is needed until chrome changes to use gaia obfuscated id.
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The signin manager and token service use the gaia "email" property, which
302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// however uses gaia "displayEmail" which does not preserve case, and then
304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// passes the string through gaia::CanonicalizeEmail() which removes dots.  This
305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// tests makes sure that an email like "Dot.S@hmail.com", as seen by the
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// token service, will be considered the same as "dots@gmail.com" as returned
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// by gaia::ParseListAccountsData().
3086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileNoopWithDots) {
309a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
312a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
313a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
314a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
315a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
317a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
318a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
319a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
320a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
321a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
322a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
323a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
324a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
3256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
32603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
3276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
3286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::ACCOUNTS_SAME,
3296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
330a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
331a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileNoopMultiple) {
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
341a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
342a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
3506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
35103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectTotalCount(
3526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun", 1);
35303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
3546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
3556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::ACCOUNTS_SAME,
3566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileAddToCookie) {
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
3615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
3635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
3755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
3765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
3786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
37903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
3806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
3816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::ACCOUNTS_SAME,
3826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
38303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
3846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
38503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
3861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
3871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
3881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_P(AccountReconcilorTest, StartReconcileRemoveFromCookie) {
3901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
3911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
3921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
3941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
3951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
3971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
3981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
4011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  AccountReconcilor* reconcilor = GetMockReconcilor();
4021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  reconcilor->StartReconcile();
4031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
4051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::RunLoop().RunUntilIdle();
4061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
4071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                GoogleServiceAuthError::AuthErrorNone());
4081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
4101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  histogram_tester()->ExpectUniqueSample(
4111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
4121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      signin_metrics::ACCOUNTS_SAME,
4131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      1);
4141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  histogram_tester()->ExpectUniqueSample(
4151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.AddedToCookieJar.FirstRun", 0, 1);
4161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  histogram_tester()->ExpectUniqueSample(
4171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 1, 1);
4186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
4196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileAddToCookieTwice) {
4216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("third@gmail.com"));
4276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  SetFakeResponse(
4296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      GaiaUrls::GetInstance()->list_accounts_url().spec(),
4306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      net::HTTP_OK,
4326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      net::URLRequestStatus::SUCCESS);
4336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  reconcilor->StartReconcile();
4366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  SimulateMergeSessionCompleted(
4406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      reconcilor, "other@gmail.com", GoogleServiceAuthError::AuthErrorNone());
4416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
44303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
4456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::ACCOUNTS_SAME,
4466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
44703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
44903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
4516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Do another pass after I've added a third account to the token service
4536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  SetFakeResponse(
4556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      GaiaUrls::GetInstance()->list_accounts_url().spec(),
4566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
4576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      net::HTTP_OK,
4596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      net::URLRequestStatus::SUCCESS);
4606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // This will cause the reconcilor to fire.
4616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  token_service()->UpdateCredentials("third@gmail.com", "refresh_token");
4626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  SimulateMergeSessionCompleted(
4676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      reconcilor, "third@gmail.com", GoogleServiceAuthError::AuthErrorNone());
4686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
47003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
4726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::ACCOUNTS_SAME,
4736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
47403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
47603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
47803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.SubsequentRun",
4806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::ACCOUNTS_SAME,
4816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
48203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.AddedToCookieJar.SubsequentRun", 1, 1);
48403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
4851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.RemovedFromCookieJar.SubsequentRun", 0, 1);
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileBadPrimary) {
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
497a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
498a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
499a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
5065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
5115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
51403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
5156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
5166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      signin_metrics::COOKIE_AND_TOKEN_PRIMARIES_DIFFERENT,
5176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      1);
51803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
5191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.AddedToCookieJar.FirstRun", 0, 1);
52003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  histogram_tester()->ExpectUniqueSample(
5211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
5221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
523f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileOnlyOnce) {
5255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
5265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
5275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
5295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
532a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
533a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
534a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
535a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
538a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
542a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_P(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
548a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
549a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
550a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
551a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
552a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
553a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
5545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
557a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
558a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
5625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
565a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
566a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
570116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(AccountReconcilorTest, MergeSessionCompletedWithBogusAccount) {
571116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
572116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
573116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
574116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
575116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
576116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
577116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
578116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
579116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
580116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  AccountReconcilor* reconcilor =
581116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AccountReconcilorFactory::GetForProfile(profile());
582116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(reconcilor);
583116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
584116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_FALSE(reconcilor->is_reconcile_started_);
585116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  reconcilor->StartReconcile();
586116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(reconcilor->is_reconcile_started_);
587116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
588116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::RunLoop().RunUntilIdle();
589116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
590116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // If an unknown account id is sent, it should not upset the state.
591116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SimulateMergeSessionCompleted(reconcilor, "bogus@gmail.com",
592116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                GoogleServiceAuthError::AuthErrorNone());
593116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(reconcilor->is_reconcile_started_);
594116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
595116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
596116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                GoogleServiceAuthError::AuthErrorNone());
597116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_FALSE(reconcilor->is_reconcile_started_);
598116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
599116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
6006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)INSTANTIATE_TEST_CASE_P(AccountReconcilorMaybeEnabled,
6016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                        AccountReconcilorTest,
6026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                        testing::Bool());
603116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
604