This source file includes following definitions.
- ACTION_P
- ACTION_P2
- sync_
- SetUp
- CreateProfile
- TearDown
- BuildService
- CreateSyncService
- IssueTestTokens
- components_factory_mock
- fake_signin
- SimulateTestUserSignin
- SetUpDataTypeManager
- SetUpSyncBackendHost
- SetUp
- BuildCrosService
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
#include "chrome/browser/signin/fake_signin_manager.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/glue/sync_backend_host_mock.h"
#include "chrome/browser/sync/managed_user_signin_manager_wrapper.h"
#include "chrome/browser/sync/profile_sync_components_factory_mock.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/sync/profile_sync_test_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/sync_driver/data_type_manager.h"
#include "components/sync_driver/data_type_manager_mock.h"
#include "components/sync_driver/pref_names.h"
#include "components/sync_driver/sync_prefs.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "google_apis/gaia/gaia_auth_consumer.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/oauth2_token_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using browser_sync::DataTypeManager;
using browser_sync::DataTypeManagerMock;
using browser_sync::SyncBackendHostMock;
using content::BrowserThread;
using testing::_;
using testing::AnyNumber;
using testing::DoAll;
using testing::InvokeArgument;
using testing::Mock;
using testing::Return;
ACTION_P(InvokeOnConfigureStart, pss) {
ProfileSyncService* service =
static_cast<ProfileSyncService*>(pss);
service->OnConfigureStart();
}
ACTION_P2(InvokeOnConfigureDone, pss, result) {
ProfileSyncService* service =
static_cast<ProfileSyncService*>(pss);
DataTypeManager::ConfigureResult configure_result =
static_cast<DataTypeManager::ConfigureResult>(result);
service->OnConfigureDone(configure_result);
}
class ProfileSyncServiceStartupTest : public testing::Test {
public:
ProfileSyncServiceStartupTest()
: thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD |
content::TestBrowserThreadBundle::REAL_FILE_THREAD |
content::TestBrowserThreadBundle::REAL_IO_THREAD),
sync_(NULL) {}
virtual ~ProfileSyncServiceStartupTest() {
}
virtual void SetUp() {
profile_ = CreateProfile();
}
virtual scoped_ptr<TestingProfile> CreateProfile() {
TestingProfile::Builder builder;
builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
FakeSigninManagerBase::Build);
builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
BuildAutoIssuingFakeProfileOAuth2TokenService);
builder.AddTestingFactory(ProfileSyncServiceFactory::GetInstance(),
BuildService);
return builder.Build();
}
virtual void TearDown() {
sync_->RemoveObserver(&observer_);
profile_.reset();
}
static KeyedService* BuildService(content::BrowserContext* browser_context) {
Profile* profile = static_cast<Profile*>(browser_context);
return new ProfileSyncService(
new ProfileSyncComponentsFactoryMock(),
profile,
new ManagedUserSigninManagerWrapper(
profile, SigninManagerFactory::GetForProfile(profile)),
ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
browser_sync::MANUAL_START);
}
void CreateSyncService() {
sync_ = static_cast<ProfileSyncService*>(
ProfileSyncServiceFactory::GetForProfile(profile_.get()));
sync_->AddObserver(&observer_);
}
void IssueTestTokens() {
ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get())
->UpdateCredentials("test_user@gmail.com", "oauth2_login_token");
}
ProfileSyncComponentsFactoryMock* components_factory_mock() {
return static_cast<ProfileSyncComponentsFactoryMock*>(sync_->factory());
}
FakeSigninManagerForTesting* fake_signin() {
return static_cast<FakeSigninManagerForTesting*>(sync_->signin());
}
protected:
void SimulateTestUserSignin() {
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
#if !defined(OS_CHROMEOS)
fake_signin()->SignIn("test_user@gmail.com", "");
#else
fake_signin()->SetAuthenticatedUsername("test_user@gmail.com");
sync_->GoogleSigninSucceeded("test_user@gmail.com", "");
#endif
}
DataTypeManagerMock* SetUpDataTypeManager() {
DataTypeManagerMock* data_type_manager = new DataTypeManagerMock();
EXPECT_CALL(*components_factory_mock(),
CreateDataTypeManager(_, _, _, _, _, _)).
WillOnce(Return(data_type_manager));
return data_type_manager;
}
browser_sync::SyncBackendHostMock* SetUpSyncBackendHost() {
browser_sync::SyncBackendHostMock* sync_backend_host =
new browser_sync::SyncBackendHostMock();
EXPECT_CALL(*components_factory_mock(),
CreateSyncBackendHost(_, _, _)).
WillOnce(Return(sync_backend_host));
return sync_backend_host;
}
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<TestingProfile> profile_;
ProfileSyncService* sync_;
ProfileSyncServiceObserverMock observer_;
};
class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest {
public:
virtual void SetUp() {
ProfileSyncServiceStartupTest::SetUp();
sync_ = static_cast<ProfileSyncService*>(
ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile_.get(), BuildCrosService));
sync_->AddObserver(&observer_);
}
static KeyedService* BuildCrosService(content::BrowserContext* context) {
Profile* profile = static_cast<Profile*>(context);
SigninManagerBase* signin =
SigninManagerFactory::GetForProfile(profile);
profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
signin->SetAuthenticatedUsername("test_user@gmail.com");
ProfileOAuth2TokenService* oauth2_token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
EXPECT_FALSE(signin->GetAuthenticatedUsername().empty());
return new ProfileSyncService(
new ProfileSyncComponentsFactoryMock(),
profile,
new ManagedUserSigninManagerWrapper(profile, signin),
oauth2_token_service,
browser_sync::AUTO_START);
}
};
TEST_F(ProfileSyncServiceStartupTest, StartFirstTime) {
profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
CreateSyncService();
SetUpSyncBackendHost();
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->Initialize();
EXPECT_EQ(
0,
profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime));
EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(
sync_driver::prefs::kSyncHasSetupCompleted));
Mock::VerifyAndClearExpectations(data_type_manager);
EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1);
EXPECT_CALL(*data_type_manager, state()).
WillOnce(Return(DataTypeManager::CONFIGURED)).
WillOnce(Return(DataTypeManager::CONFIGURED));
EXPECT_CALL(*data_type_manager, Stop()).Times(1);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->SetSetupInProgress(true);
SimulateTestUserSignin();
IssueTestTokens();
sync_->SetSetupInProgress(false);
EXPECT_TRUE(sync_->ShouldPushChanges());
}
TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartNoCredentials) {
profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
CreateSyncService();
EXPECT_CALL(*components_factory_mock(),
CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->Initialize();
EXPECT_EQ(
0,
profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime));
EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(
sync_driver::prefs::kSyncHasSetupCompleted));
sync_->SetSetupInProgress(true);
SimulateTestUserSignin();
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
token_service->LoadCredentials("test_user@gmail.com");
sync_->SetSetupInProgress(false);
EXPECT_FALSE(sync_->ShouldPushChanges());
EXPECT_EQ(GoogleServiceAuthError::USER_NOT_SIGNED_UP,
sync_->GetAuthError().state());
}
TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartInvalidCredentials) {
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
sync_->signin()->SetAuthenticatedUsername("test_user@gmail.com");
CreateSyncService();
SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost();
mock_sbh->set_fail_initial_download(true);
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->Initialize();
EXPECT_FALSE(sync_->sync_initialized());
Mock::VerifyAndClearExpectations(data_type_manager);
EXPECT_CALL(*data_type_manager, Configure(_, _));
EXPECT_CALL(*data_type_manager, state()).
WillRepeatedly(Return(DataTypeManager::CONFIGURED));
EXPECT_CALL(*data_type_manager, Stop()).Times(1);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->SetSetupInProgress(true);
SimulateTestUserSignin();
sync_->SetSetupInProgress(false);
EXPECT_TRUE(sync_->ShouldPushChanges());
}
TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) {
EXPECT_CALL(*components_factory_mock(),
CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
EXPECT_CALL(*components_factory_mock(),
CreateSyncBackendHost(_, _, _)).Times(0);
profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->Initialize();
EXPECT_FALSE(sync_->ShouldPushChanges());
sync_->SetSetupInProgress(false);
EXPECT_FALSE(sync_->ShouldPushChanges());
}
TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) {
SetUpSyncBackendHost();
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
EXPECT_CALL(*data_type_manager, Configure(_, _));
EXPECT_CALL(*data_type_manager, state()).
WillRepeatedly(Return(DataTypeManager::CONFIGURED));
EXPECT_CALL(*data_type_manager, Stop()).Times(1);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
IssueTestTokens();
sync_->Initialize();
EXPECT_TRUE(sync_->ShouldPushChanges());
}
TEST_F(ProfileSyncServiceStartupTest, StartNormal) {
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
SigninManagerFactory::GetForProfile(profile_.get())
->SetAuthenticatedUsername("test_user@gmail.com");
CreateSyncService();
sync_->SetSyncSetupCompleted();
SetUpSyncBackendHost();
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
EXPECT_CALL(*data_type_manager, Configure(_, _));
EXPECT_CALL(*data_type_manager, state()).
WillRepeatedly(Return(DataTypeManager::CONFIGURED));
EXPECT_CALL(*data_type_manager, Stop()).Times(1);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
IssueTestTokens();
sync_->Initialize();
}
TEST_F(ProfileSyncServiceStartupTest, StartRecoverDatatypePrefs) {
profile_->GetPrefs()->ClearPref(
sync_driver::prefs::kSyncKeepEverythingSynced);
syncer::ModelTypeSet user_types = syncer::UserTypes();
for (syncer::ModelTypeSet::Iterator iter = user_types.First();
iter.Good(); iter.Inc()) {
profile_->GetPrefs()->ClearPref(
sync_driver::SyncPrefs::GetPrefNameForDataType(iter.Get()));
}
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
SigninManagerFactory::GetForProfile(profile_.get())
->SetAuthenticatedUsername("test_user@gmail.com");
CreateSyncService();
sync_->SetSyncSetupCompleted();
SetUpSyncBackendHost();
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
EXPECT_CALL(*data_type_manager, Configure(_, _));
EXPECT_CALL(*data_type_manager, state()).
WillRepeatedly(Return(DataTypeManager::CONFIGURED));
EXPECT_CALL(*data_type_manager, Stop()).Times(1);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
IssueTestTokens();
sync_->Initialize();
EXPECT_TRUE(profile_->GetPrefs()->GetBoolean(
sync_driver::prefs::kSyncKeepEverythingSynced));
}
TEST_F(ProfileSyncServiceStartupTest, StartDontRecoverDatatypePrefs) {
profile_->GetPrefs()->SetBoolean(
sync_driver::prefs::kSyncKeepEverythingSynced, false);
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
SigninManagerFactory::GetForProfile(profile_.get())
->SetAuthenticatedUsername("test_user@gmail.com");
CreateSyncService();
sync_->SetSyncSetupCompleted();
SetUpSyncBackendHost();
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
EXPECT_CALL(*data_type_manager, Configure(_, _));
EXPECT_CALL(*data_type_manager, state()).
WillRepeatedly(Return(DataTypeManager::CONFIGURED));
EXPECT_CALL(*data_type_manager, Stop()).Times(1);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
IssueTestTokens();
sync_->Initialize();
EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(
sync_driver::prefs::kSyncKeepEverythingSynced));
}
TEST_F(ProfileSyncServiceStartupTest, ManagedStartup) {
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
CreateSyncService();
profile_->GetPrefs()->SetBoolean(sync_driver::prefs::kSyncManaged, true);
EXPECT_CALL(*components_factory_mock(),
CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->Initialize();
}
TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) {
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
SigninManagerFactory::GetForProfile(profile_.get())
->SetAuthenticatedUsername("test_user@gmail.com");
CreateSyncService();
sync_->SetSyncSetupCompleted();
SetUpSyncBackendHost();
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
EXPECT_CALL(*data_type_manager, Configure(_, _));
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
IssueTestTokens();
sync_->Initialize();
Mock::VerifyAndClearExpectations(data_type_manager);
EXPECT_CALL(*data_type_manager, state()).
WillOnce(Return(DataTypeManager::CONFIGURED));
EXPECT_CALL(*data_type_manager, Stop()).Times(1);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
profile_->GetPrefs()->SetBoolean(sync_driver::prefs::kSyncManaged, true);
Mock::VerifyAndClearExpectations(data_type_manager);
EXPECT_CALL(*components_factory_mock(),
CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncManaged);
}
TEST_F(ProfileSyncServiceStartupTest, StartFailure) {
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
SigninManagerFactory::GetForProfile(profile_.get())
->SetAuthenticatedUsername("test_user@gmail.com");
CreateSyncService();
sync_->SetSyncSetupCompleted();
SetUpSyncBackendHost();
DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
DataTypeManager::ConfigureStatus status = DataTypeManager::ABORTED;
syncer::SyncError error(
FROM_HERE,
syncer::SyncError::DATATYPE_ERROR,
"Association failed.",
syncer::BOOKMARKS);
std::map<syncer::ModelType, syncer::SyncError> errors;
errors[syncer::BOOKMARKS] = error;
DataTypeManager::ConfigureResult result(
status,
syncer::ModelTypeSet(),
errors,
syncer::ModelTypeSet(),
syncer::ModelTypeSet());
EXPECT_CALL(*data_type_manager, Configure(_, _)).
WillRepeatedly(
DoAll(InvokeOnConfigureStart(sync_),
InvokeOnConfigureDone(sync_, result)));
EXPECT_CALL(*data_type_manager, state()).
WillOnce(Return(DataTypeManager::STOPPED));
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
IssueTestTokens();
sync_->Initialize();
EXPECT_TRUE(sync_->HasUnrecoverableError());
}
TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) {
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
"test_user@gmail.com");
SigninManagerFactory::GetForProfile(profile_.get())
->SetAuthenticatedUsername("test_user@gmail.com");
CreateSyncService();
SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost();
mock_sbh->set_fail_initial_download(true);
profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
sync_->Initialize();
sync_->SetSetupInProgress(true);
IssueTestTokens();
sync_->SetSetupInProgress(false);
EXPECT_FALSE(sync_->sync_initialized());
}