1 // Copyright (c) 2012 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 "chrome/browser/sync/test_profile_sync_service.h"
6
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
11 #include "chrome/browser/signin/signin_manager_factory.h"
12 #include "chrome/browser/sync/glue/sync_backend_host.h"
13 #include "chrome/browser/sync/glue/sync_backend_host_core.h"
14 #include "chrome/browser/sync/managed_user_signin_manager_wrapper.h"
15 #include "chrome/browser/sync/profile_sync_components_factory.h"
16 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #include "chrome/browser/sync/test/test_http_bridge_factory.h"
19 #include "components/invalidation/profile_invalidation_provider.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_test.h"
22 #include "sync/internal_api/public/test/test_internal_components_factory.h"
23 #include "sync/internal_api/public/user_share.h"
24 #include "sync/protocol/encryption.pb.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26
27 using syncer::InternalComponentsFactory;
28 using syncer::TestInternalComponentsFactory;
29 using syncer::UserShare;
30
31 namespace browser_sync {
32
SyncBackendHostForProfileSyncTest(Profile * profile,invalidation::InvalidationService * invalidator,const base::WeakPtr<sync_driver::SyncPrefs> & sync_prefs,base::Closure callback)33 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest(
34 Profile* profile,
35 invalidation::InvalidationService* invalidator,
36 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs,
37 base::Closure callback)
38 : browser_sync::SyncBackendHostImpl(
39 profile->GetDebugName(), profile, invalidator,
40 sync_prefs, base::FilePath(FILE_PATH_LITERAL("test"))),
41 callback_(callback) {}
42
~SyncBackendHostForProfileSyncTest()43 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {}
44
InitCore(scoped_ptr<DoInitializeOptions> options)45 void SyncBackendHostForProfileSyncTest::InitCore(
46 scoped_ptr<DoInitializeOptions> options) {
47 options->http_bridge_factory =
48 scoped_ptr<syncer::HttpPostProviderFactory>(
49 new browser_sync::TestHttpBridgeFactory());
50 options->sync_manager_factory.reset(
51 new syncer::SyncManagerFactoryForProfileSyncTest(callback_));
52 options->credentials.email = "testuser@gmail.com";
53 options->credentials.sync_token = "token";
54 options->restored_key_for_bootstrapping = "";
55
56 // It'd be nice if we avoided creating the InternalComponentsFactory in the
57 // first place, but SyncBackendHost will have created one by now so we must
58 // free it. Grab the switches to pass on first.
59 InternalComponentsFactory::Switches factory_switches =
60 options->internal_components_factory->GetSwitches();
61 options->internal_components_factory.reset(
62 new TestInternalComponentsFactory(factory_switches,
63 syncer::STORAGE_IN_MEMORY));
64
65 SyncBackendHostImpl::InitCore(options.Pass());
66 }
67
RequestConfigureSyncer(syncer::ConfigureReason reason,syncer::ModelTypeSet to_download,syncer::ModelTypeSet to_purge,syncer::ModelTypeSet to_journal,syncer::ModelTypeSet to_unapply,syncer::ModelTypeSet to_ignore,const syncer::ModelSafeRoutingInfo & routing_info,const base::Callback<void (syncer::ModelTypeSet,syncer::ModelTypeSet)> & ready_task,const base::Closure & retry_callback)68 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer(
69 syncer::ConfigureReason reason,
70 syncer::ModelTypeSet to_download,
71 syncer::ModelTypeSet to_purge,
72 syncer::ModelTypeSet to_journal,
73 syncer::ModelTypeSet to_unapply,
74 syncer::ModelTypeSet to_ignore,
75 const syncer::ModelSafeRoutingInfo& routing_info,
76 const base::Callback<void(syncer::ModelTypeSet,
77 syncer::ModelTypeSet)>& ready_task,
78 const base::Closure& retry_callback) {
79 syncer::ModelTypeSet failed_configuration_types;
80
81 // The first parameter there should be the set of enabled types. That's not
82 // something we have access to from this strange test harness. We'll just
83 // send back the list of newly configured types instead and hope it doesn't
84 // break anything.
85 FinishConfigureDataTypesOnFrontendLoop(
86 syncer::Difference(to_download, failed_configuration_types),
87 syncer::Difference(to_download, failed_configuration_types),
88 failed_configuration_types,
89 ready_task);
90 }
91
92 } // namespace browser_sync
93
id_factory()94 syncer::TestIdFactory* TestProfileSyncService::id_factory() {
95 return &id_factory_;
96 }
97
98 syncer::WeakHandle<syncer::JsEventHandler>
GetJsEventHandler()99 TestProfileSyncService::GetJsEventHandler() {
100 return syncer::WeakHandle<syncer::JsEventHandler>();
101 }
102
TestProfileSyncService(ProfileSyncComponentsFactory * factory,Profile * profile,SigninManagerBase * signin,ProfileOAuth2TokenService * oauth2_token_service,browser_sync::ProfileSyncServiceStartBehavior behavior)103 TestProfileSyncService::TestProfileSyncService(
104 ProfileSyncComponentsFactory* factory,
105 Profile* profile,
106 SigninManagerBase* signin,
107 ProfileOAuth2TokenService* oauth2_token_service,
108 browser_sync::ProfileSyncServiceStartBehavior behavior)
109 : ProfileSyncService(
110 factory,
111 profile,
112 make_scoped_ptr(new ManagedUserSigninManagerWrapper(profile, signin)),
113 oauth2_token_service,
114 behavior) {
115 SetSyncSetupCompleted();
116 }
117
~TestProfileSyncService()118 TestProfileSyncService::~TestProfileSyncService() {
119 }
120
121 // static
TestFactoryFunction(content::BrowserContext * context)122 KeyedService* TestProfileSyncService::TestFactoryFunction(
123 content::BrowserContext* context) {
124 Profile* profile = static_cast<Profile*>(context);
125 SigninManagerBase* signin =
126 SigninManagerFactory::GetForProfile(profile);
127 ProfileOAuth2TokenService* oauth2_token_service =
128 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
129 ProfileSyncComponentsFactoryMock* factory =
130 new ProfileSyncComponentsFactoryMock();
131 return new TestProfileSyncService(factory,
132 profile,
133 signin,
134 oauth2_token_service,
135 browser_sync::AUTO_START);
136 }
137
138 // static
BuildAutoStartAsyncInit(Profile * profile,base::Closure callback)139 TestProfileSyncService* TestProfileSyncService::BuildAutoStartAsyncInit(
140 Profile* profile, base::Closure callback) {
141 TestProfileSyncService* sync_service = static_cast<TestProfileSyncService*>(
142 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
143 profile, &TestProfileSyncService::TestFactoryFunction));
144 ProfileSyncComponentsFactoryMock* components =
145 sync_service->components_factory_mock();
146 // TODO(tim): Convert to a fake instead of mock.
147 EXPECT_CALL(*components,
148 CreateSyncBackendHost(testing::_,testing::_, testing::_,
149 testing::_, testing::_)).
150 WillOnce(testing::Return(
151 new browser_sync::SyncBackendHostForProfileSyncTest(
152 profile,
153 invalidation::ProfileInvalidationProviderFactory::GetForProfile(
154 profile)->GetInvalidationService(),
155 sync_service->sync_prefs_.AsWeakPtr(),
156 callback)));
157 return sync_service;
158 }
159
160 ProfileSyncComponentsFactoryMock*
components_factory_mock()161 TestProfileSyncService::components_factory_mock() {
162 // We always create a mock factory, see Build* routines.
163 return static_cast<ProfileSyncComponentsFactoryMock*>(factory());
164 }
165
OnConfigureDone(const browser_sync::DataTypeManager::ConfigureResult & result)166 void TestProfileSyncService::OnConfigureDone(
167 const browser_sync::DataTypeManager::ConfigureResult& result) {
168 ProfileSyncService::OnConfigureDone(result);
169 base::MessageLoop::current()->Quit();
170 }
171
GetUserShare() const172 UserShare* TestProfileSyncService::GetUserShare() const {
173 return backend_->GetUserShare();
174 }
175