1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/memory/scoped_ptr.h"
6#include "chrome/browser/signin/account_reconcilor.h"
7#include "chrome/browser/signin/account_reconcilor_factory.h"
8#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
9#include "chrome/browser/signin/fake_signin_manager.h"
10#include "chrome/browser/signin/profile_oauth2_token_service.h"
11#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12#include "chrome/browser/signin/signin_manager.h"
13#include "chrome/browser/signin/signin_manager_factory.h"
14#include "chrome/test/base/testing_profile.h"
15#include "content/public/test/test_browser_thread_bundle.h"
16#include "testing/gmock/include/gmock/gmock.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace {
20
21#if defined(OS_CHROMEOS)
22typedef FakeSigninManagerBase FakeSigninManagerForTesting;
23#else
24typedef FakeSigninManager FakeSigninManagerForTesting;
25#endif
26
27const char kTestEmail[] = "user@gmail.com";
28
29class AccountReconcilorTest : public testing::Test {
30 public:
31  AccountReconcilorTest();
32  virtual void SetUp() OVERRIDE;
33  virtual void TearDown() OVERRIDE;
34
35  TestingProfile* profile() { return profile_.get(); }
36  FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
37  FakeProfileOAuth2TokenService* token_service() { return token_service_; }
38
39private:
40  content::TestBrowserThreadBundle bundle_;
41  scoped_ptr<TestingProfile> profile_;
42  FakeSigninManagerForTesting* signin_manager_;
43  FakeProfileOAuth2TokenService* token_service_;
44};
45
46AccountReconcilorTest::AccountReconcilorTest()
47    : signin_manager_(NULL), token_service_(NULL) {}
48
49void AccountReconcilorTest::SetUp() {
50  TestingProfile::Builder builder;
51  builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
52                            FakeProfileOAuth2TokenService::Build);
53  builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
54                            FakeSigninManagerForTesting::Build);
55  profile_ = builder.Build();
56
57  signin_manager_ =
58      static_cast<FakeSigninManagerForTesting*>(
59          SigninManagerFactory::GetForProfile(profile()));
60  signin_manager_->Initialize(profile(), NULL);
61
62  token_service_ =
63      static_cast<FakeProfileOAuth2TokenService*>(
64          ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
65}
66
67void AccountReconcilorTest::TearDown() {
68  // Destroy the profile before all threads are torn down.
69  profile_.reset();
70}
71
72}  // namespace
73
74TEST_F(AccountReconcilorTest, Basic) {
75  AccountReconcilor* reconcilor =
76      AccountReconcilorFactory::GetForProfile(profile());
77  ASSERT_TRUE(NULL != reconcilor);
78  ASSERT_EQ(profile(), reconcilor->profile());
79}
80
81#if !defined(OS_CHROMEOS)
82TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
83  AccountReconcilor* reconcilor =
84      AccountReconcilorFactory::GetForProfile(profile());
85  ASSERT_TRUE(NULL != reconcilor);
86  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
87
88  signin_manager()->OnExternalSigninCompleted(kTestEmail);
89  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
90
91  signin_manager()->SignOut();
92  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
93}
94#endif
95
96TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
97  signin_manager()->SetAuthenticatedUsername(kTestEmail);
98
99  AccountReconcilor* reconcilor =
100      AccountReconcilorFactory::GetForProfile(profile());
101  ASSERT_TRUE(NULL != reconcilor);
102  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
103}
104
105