account_reconcilor_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
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"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor.h"
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor_factory.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_signin_manager.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if defined(OS_CHROMEOS)
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)typedef FakeSigninManagerBase FakeSigninManagerForTesting;
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#else
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)typedef FakeSigninManager FakeSigninManagerForTesting;
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestEmail[] = "user@gmail.com";
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class AccountReconcilorTest : public testing::Test {
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilorTest();
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUp() OVERRIDE;
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void TearDown() OVERRIDE;
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile* profile() { return profile_.get(); }
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service() { return token_service_; }
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)private:
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::TestBrowserThreadBundle bundle_;
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<TestingProfile> profile_;
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager_;
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service_;
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)AccountReconcilorTest::AccountReconcilorTest()
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    : signin_manager_(NULL), token_service_(NULL) {}
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::SetUp() {
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile::Builder builder;
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                            FakeProfileOAuth2TokenService::Build);
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                            FakeSigninManagerForTesting::Build);
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_ = builder.Build();
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager_ =
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeSigninManagerForTesting*>(
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          SigninManagerFactory::GetForProfile(profile()));
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager_->Initialize(profile(), NULL);
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  token_service_ =
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeProfileOAuth2TokenService*>(
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::TearDown() {
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Destroy the profile before all threads are torn down.
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_.reset();
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(AccountReconcilorTest, Basic) {
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AccountReconcilor* reconcilor =
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(NULL != reconcilor);
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_EQ(profile(), reconcilor->profile());
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if !defined(OS_CHROMEOS)
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(NULL != reconcilor);
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SignOut();
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(NULL != reconcilor);
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(AccountReconcilorTest, ParseListAccountsData) {
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::vector<std::string> accounts;
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData("");
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(0u, accounts.size());
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData("1");
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(0u, accounts.size());
112f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData("[]");
114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(0u, accounts.size());
115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData("[\"foo\", \"bar\"]");
117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(0u, accounts.size());
118f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData("[\"foo\", []]");
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(0u, accounts.size());
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData(
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "[\"foo\", [[\"bar\", 0, \"name\", 0, \"photo\", 0, 0, 0]]]");
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(0u, accounts.size());
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData(
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "[\"foo\", [[\"bar\", 0, \"name\", \"email\", \"photo\", 0, 0, 0]]]");
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(1u, accounts.size());
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ("email", accounts[0]);
130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
131f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  accounts = AccountReconcilor::ParseListAccountsData(
132f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "[\"foo\", [[\"bar1\", 0, \"name1\", \"email1\", \"photo1\", 0, 0, 0], "
133f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                 "[\"bar2\", 0, \"name2\", \"email2\", \"photo2\", 0, 0, 0]]]");
134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ(2u, accounts.size());
135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ("email1", accounts[0]);
136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_EQ("email2", accounts[1]);
137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
138