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