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.
5 #include "sync/internal_api/public/test/fake_sync_manager.h"
10 #include "base/bind_helpers.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/run_loop.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "sync/internal_api/public/http_post_provider_factory.h"
18 #include "sync/internal_api/public/internal_components_factory.h"
19 #include "sync/internal_api/public/util/weak_handle.h"
20 #include "sync/notifier/invalidator.h"
21 #include "sync/notifier/invalidator_state.h"
22 #include "sync/notifier/object_id_invalidation_map.h"
23 #include "sync/syncable/directory.h"
24 #include "sync/test/fake_sync_encryption_handler.h"
28 FakeSyncManager::FakeSyncManager(ModelTypeSet initial_sync_ended_types
,
29 ModelTypeSet progress_marker_types
,
30 ModelTypeSet configure_fail_types
) :
31 initial_sync_ended_types_(initial_sync_ended_types
),
32 progress_marker_types_(progress_marker_types
),
33 configure_fail_types_(configure_fail_types
),
34 last_configure_reason_(CONFIGURE_REASON_UNKNOWN
) {
35 fake_encryption_handler_
.reset(new FakeSyncEncryptionHandler());
38 FakeSyncManager::~FakeSyncManager() {}
40 ModelTypeSet
FakeSyncManager::GetAndResetCleanedTypes() {
41 ModelTypeSet cleaned_types
= cleaned_types_
;
42 cleaned_types_
.Clear();
46 ModelTypeSet
FakeSyncManager::GetAndResetDownloadedTypes() {
47 ModelTypeSet downloaded_types
= downloaded_types_
;
48 downloaded_types_
.Clear();
49 return downloaded_types
;
52 ModelTypeSet
FakeSyncManager::GetAndResetEnabledTypes() {
53 ModelTypeSet enabled_types
= enabled_types_
;
54 enabled_types_
.Clear();
58 ConfigureReason
FakeSyncManager::GetAndResetConfigureReason() {
59 ConfigureReason reason
= last_configure_reason_
;
60 last_configure_reason_
= CONFIGURE_REASON_UNKNOWN
;
64 void FakeSyncManager::WaitForSyncThread() {
65 // Post a task to |sync_task_runner_| and block until it runs.
66 base::RunLoop run_loop
;
67 if (!sync_task_runner_
->PostTaskAndReply(
69 base::Bind(&base::DoNothing
),
70 run_loop
.QuitClosure())) {
76 void FakeSyncManager::Init(
77 const base::FilePath
& database_location
,
78 const WeakHandle
<JsEventHandler
>& event_handler
,
79 const std::string
& sync_server_and_path
,
82 scoped_ptr
<HttpPostProviderFactory
> post_factory
,
83 const std::vector
<scoped_refptr
<ModelSafeWorker
> >& workers
,
84 ExtensionsActivity
* extensions_activity
,
85 ChangeDelegate
* change_delegate
,
86 const SyncCredentials
& credentials
,
87 const std::string
& invalidator_client_id
,
88 const std::string
& restored_key_for_bootstrapping
,
89 const std::string
& restored_keystore_key_for_bootstrapping
,
90 InternalComponentsFactory
* internal_components_factory
,
92 scoped_ptr
<UnrecoverableErrorHandler
> unrecoverable_error_handler
,
93 ReportUnrecoverableErrorFunction report_unrecoverable_error_function
,
94 CancelationSignal
* cancelation_signal
) {
95 sync_task_runner_
= base::ThreadTaskRunnerHandle::Get();
96 PurgePartiallySyncedTypes();
98 test_user_share_
.SetUp();
99 UserShare
* share
= test_user_share_
.user_share();
100 for (ModelTypeSet::Iterator it
= initial_sync_ended_types_
.First();
101 it
.Good(); it
.Inc()) {
102 TestUserShare::CreateRoot(it
.Get(), share
);
105 FOR_EACH_OBSERVER(SyncManager::Observer
, observers_
,
106 OnInitializationComplete(
107 WeakHandle
<JsBackend
>(),
108 WeakHandle
<DataTypeDebugInfoListener
>(),
109 true, initial_sync_ended_types_
));
112 void FakeSyncManager::ThrowUnrecoverableError() {
116 ModelTypeSet
FakeSyncManager::InitialSyncEndedTypes() {
117 return initial_sync_ended_types_
;
120 ModelTypeSet
FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
121 ModelTypeSet types
) {
122 ModelTypeSet empty_types
= types
;
123 empty_types
.RemoveAll(progress_marker_types_
);
127 bool FakeSyncManager::PurgePartiallySyncedTypes() {
128 ModelTypeSet partial_types
;
129 for (ModelTypeSet::Iterator i
= progress_marker_types_
.First();
131 if (!initial_sync_ended_types_
.Has(i
.Get()))
132 partial_types
.Put(i
.Get());
134 progress_marker_types_
.RemoveAll(partial_types
);
135 cleaned_types_
.PutAll(partial_types
);
139 void FakeSyncManager::UpdateCredentials(const SyncCredentials
& credentials
) {
143 void FakeSyncManager::StartSyncingNormally(
144 const ModelSafeRoutingInfo
& routing_info
) {
148 void FakeSyncManager::ConfigureSyncer(
149 ConfigureReason reason
,
150 ModelTypeSet to_download
,
151 ModelTypeSet to_purge
,
152 ModelTypeSet to_journal
,
153 ModelTypeSet to_unapply
,
154 const ModelSafeRoutingInfo
& new_routing_info
,
155 const base::Closure
& ready_task
,
156 const base::Closure
& retry_task
) {
157 last_configure_reason_
= reason
;
158 enabled_types_
= GetRoutingInfoTypes(new_routing_info
);
159 ModelTypeSet success_types
= to_download
;
160 success_types
.RemoveAll(configure_fail_types_
);
162 DVLOG(1) << "Faking configuration. Downloading: "
163 << ModelTypeSetToString(success_types
) << ". Cleaning: "
164 << ModelTypeSetToString(to_purge
);
166 // Update our fake directory by clearing and fake-downloading as necessary.
167 UserShare
* share
= GetUserShare();
168 share
->directory
->PurgeEntriesWithTypeIn(to_purge
,
171 for (ModelTypeSet::Iterator it
= success_types
.First(); it
.Good(); it
.Inc()) {
172 // We must be careful to not create the same root node twice.
173 if (!initial_sync_ended_types_
.Has(it
.Get())) {
174 TestUserShare::CreateRoot(it
.Get(), share
);
178 // Simulate cleaning up disabled types.
179 // TODO(sync): consider only cleaning those types that were recently disabled,
180 // if this isn't the first cleanup, which more accurately reflects the
181 // behavior of the real cleanup logic.
182 initial_sync_ended_types_
.RemoveAll(to_purge
);
183 progress_marker_types_
.RemoveAll(to_purge
);
184 cleaned_types_
.PutAll(to_purge
);
186 // Now simulate the actual configuration for those types that successfully
188 progress_marker_types_
.PutAll(success_types
);
189 initial_sync_ended_types_
.PutAll(success_types
);
190 downloaded_types_
.PutAll(success_types
);
195 void FakeSyncManager::AddObserver(Observer
* observer
) {
196 observers_
.AddObserver(observer
);
199 void FakeSyncManager::RemoveObserver(Observer
* observer
) {
200 observers_
.RemoveObserver(observer
);
203 SyncStatus
FakeSyncManager::GetDetailedStatus() const {
208 void FakeSyncManager::SaveChanges() {
212 void FakeSyncManager::ShutdownOnSyncThread() {
213 DCHECK(sync_task_runner_
->RunsTasksOnCurrentThread());
214 test_user_share_
.TearDown();
217 UserShare
* FakeSyncManager::GetUserShare() {
218 return test_user_share_
.user_share();
221 syncer::SyncCoreProxy
* FakeSyncManager::GetSyncCoreProxy() {
222 return &null_sync_core_proxy_
;
225 const std::string
FakeSyncManager::cache_guid() {
226 return test_user_share_
.user_share()->directory
->cache_guid();
229 bool FakeSyncManager::ReceivedExperiment(Experiments
* experiments
) {
233 bool FakeSyncManager::HasUnsyncedItems() {
238 SyncEncryptionHandler
* FakeSyncManager::GetEncryptionHandler() {
239 return fake_encryption_handler_
.get();
242 ScopedVector
<syncer::ProtocolEvent
>
243 FakeSyncManager::GetBufferedProtocolEvents() {
244 return ScopedVector
<syncer::ProtocolEvent
>();
247 scoped_ptr
<base::ListValue
> FakeSyncManager::GetAllNodesForType(
248 syncer::ModelType type
) {
249 return scoped_ptr
<base::ListValue
>(new base::ListValue());
252 void FakeSyncManager::RefreshTypes(ModelTypeSet types
) {
253 last_refresh_request_types_
= types
;
256 void FakeSyncManager::OnIncomingInvalidation(
257 const ObjectIdInvalidationMap
& invalidation_map
) {
261 ModelTypeSet
FakeSyncManager::GetLastRefreshRequestTypes() {
262 return last_refresh_request_types_
;
265 void FakeSyncManager::OnInvalidatorStateChange(InvalidatorState state
) {
269 std::string
FakeSyncManager::GetOwnerName() const { return "FakeSyncManager"; }
271 } // namespace syncer