15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/test_profile_sync_service.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
77dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/profiles/profile.h"
10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/glue/sync_backend_host.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/sync/glue/sync_backend_host_core.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_components_factory.h"
15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_components_factory_mock.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_service_factory.h"
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/test/test_http_bridge_factory.h"
19f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/invalidation/profile_invalidation_provider.h"
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/gaia_constants.h"
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_test.h"
23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "sync/internal_api/public/test/test_internal_components_factory.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/internal_api/public/user_share.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/protocol/encryption.pb.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using syncer::InternalComponentsFactory;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using syncer::TestInternalComponentsFactory;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using syncer::UserShare;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace browser_sync {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest(
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    invalidation::InvalidationService* invalidator,
3723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs,
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::Closure callback)
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : browser_sync::SyncBackendHostImpl(
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        profile->GetDebugName(), profile, invalidator,
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        sync_prefs, base::FilePath(FILE_PATH_LITERAL("test"))),
4223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      callback_(callback) {}
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {}
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SyncBackendHostForProfileSyncTest::InitCore(
47a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    scoped_ptr<DoInitializeOptions> options) {
48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  options->http_bridge_factory =
49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      scoped_ptr<syncer::HttpPostProviderFactory>(
50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          new browser_sync::TestHttpBridgeFactory());
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  options->sync_manager_factory.reset(
52a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      new syncer::SyncManagerFactoryForProfileSyncTest(callback_));
53a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  options->credentials.email = "testuser@gmail.com";
54a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  options->credentials.sync_token = "token";
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  options->credentials.scope_set.insert(GaiaConstants::kChromeSyncOAuth2Scope);
56a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  options->restored_key_for_bootstrapping = "";
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It'd be nice if we avoided creating the InternalComponentsFactory in the
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // first place, but SyncBackendHost will have created one by now so we must
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // free it. Grab the switches to pass on first.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InternalComponentsFactory::Switches factory_switches =
62a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      options->internal_components_factory->GetSwitches();
63a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  options->internal_components_factory.reset(
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      new TestInternalComponentsFactory(
656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          factory_switches, InternalComponentsFactory::STORAGE_IN_MEMORY,
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          NULL));
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SyncBackendHostImpl::InitCore(options.Pass());
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer(
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    syncer::ConfigureReason reason,
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    syncer::ModelTypeSet to_download,
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    syncer::ModelTypeSet to_purge,
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    syncer::ModelTypeSet to_journal,
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    syncer::ModelTypeSet to_unapply,
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    syncer::ModelTypeSet to_ignore,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const syncer::ModelSafeRoutingInfo& routing_info,
79b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    const base::Callback<void(syncer::ModelTypeSet,
80b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                              syncer::ModelTypeSet)>& ready_task,
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Closure& retry_callback) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  syncer::ModelTypeSet failed_configuration_types;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
84eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // The first parameter there should be the set of enabled types.  That's not
85eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // something we have access to from this strange test harness.  We'll just
86eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // send back the list of newly configured types instead and hope it doesn't
87eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // break anything.
88b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  FinishConfigureDataTypesOnFrontendLoop(
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      syncer::Difference(to_download, failed_configuration_types),
90eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      syncer::Difference(to_download, failed_configuration_types),
91b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      failed_configuration_types,
92b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      ready_task);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace browser_sync
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)syncer::TestIdFactory* TestProfileSyncService::id_factory() {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return &id_factory_;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
101a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)syncer::WeakHandle<syncer::JsEventHandler>
102a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TestProfileSyncService::GetJsEventHandler() {
103a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return syncer::WeakHandle<syncer::JsEventHandler>();
104a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
105a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestProfileSyncService::TestProfileSyncService(
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    scoped_ptr<ProfileSyncComponentsFactory> factory,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SigninManagerBase* signin,
11068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ProfileOAuth2TokenService* oauth2_token_service,
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    browser_sync::ProfileSyncServiceStartBehavior behavior)
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : ProfileSyncService(
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          factory.Pass(),
114f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          profile,
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          make_scoped_ptr(new SupervisedUserSigninManagerWrapper(profile,
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                                                 signin)),
117f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          oauth2_token_service,
118f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          behavior) {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSyncSetupCompleted();
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestProfileSyncService::~TestProfileSyncService() {
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)KeyedService* TestProfileSyncService::TestFactoryFunction(
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::BrowserContext* context) {
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Profile* profile = static_cast<Profile*>(context);
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SigninManagerBase* signin =
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SigninManagerFactory::GetForProfile(profile);
13168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ProfileOAuth2TokenService* oauth2_token_service =
13268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
1335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return new TestProfileSyncService(
1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      scoped_ptr<ProfileSyncComponentsFactory>(
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          new ProfileSyncComponentsFactoryMock()),
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      profile,
1375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      signin,
1385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      oauth2_token_service,
1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      browser_sync::AUTO_START);
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TestProfileSyncService* TestProfileSyncService::BuildAutoStartAsyncInit(
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Profile* profile, base::Closure callback) {
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TestProfileSyncService* sync_service = static_cast<TestProfileSyncService*>(
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            profile, &TestProfileSyncService::TestFactoryFunction));
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ProfileSyncComponentsFactoryMock* components =
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      sync_service->components_factory_mock();
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(tim): Convert to a fake instead of mock.
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*components,
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              CreateSyncBackendHost(testing::_,testing::_, testing::_,
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                    testing::_, testing::_)).
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      WillOnce(testing::Return(
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          new browser_sync::SyncBackendHostForProfileSyncTest(
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              profile,
157f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)              invalidation::ProfileInvalidationProviderFactory::GetForProfile(
158f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  profile)->GetInvalidationService(),
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              sync_service->sync_prefs_.AsWeakPtr(),
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              callback)));
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return sync_service;
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ProfileSyncComponentsFactoryMock*
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TestProfileSyncService::components_factory_mock() {
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // We always create a mock factory, see Build* routines.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return static_cast<ProfileSyncComponentsFactoryMock*>(factory());
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestProfileSyncService::OnConfigureDone(
1715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const sync_driver::DataTypeManager::ConfigureResult& result) {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProfileSyncService::OnConfigureDone(result);
173a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::MessageLoop::current()->Quit();
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)UserShare* TestProfileSyncService::GetUserShare() const {
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return backend_->GetUserShare();
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)bool TestProfileSyncService::NeedBackup() const {
1816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return false;
1826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
183