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/syncable/directory.h"
21 #include "sync/test/fake_sync_encryption_handler.h"
27 FakeSyncManager::FakeSyncManager(ModelTypeSet initial_sync_ended_types
,
28 ModelTypeSet progress_marker_types
,
29 ModelTypeSet configure_fail_types
) :
30 initial_sync_ended_types_(initial_sync_ended_types
),
31 progress_marker_types_(progress_marker_types
),
32 configure_fail_types_(configure_fail_types
),
33 last_configure_reason_(CONFIGURE_REASON_UNKNOWN
) {
34 fake_encryption_handler_
.reset(new FakeSyncEncryptionHandler());
37 FakeSyncManager::~FakeSyncManager() {}
39 ModelTypeSet
FakeSyncManager::GetAndResetCleanedTypes() {
40 ModelTypeSet cleaned_types
= cleaned_types_
;
41 cleaned_types_
.Clear();
45 ModelTypeSet
FakeSyncManager::GetAndResetDownloadedTypes() {
46 ModelTypeSet downloaded_types
= downloaded_types_
;
47 downloaded_types_
.Clear();
48 return downloaded_types
;
51 ModelTypeSet
FakeSyncManager::GetAndResetEnabledTypes() {
52 ModelTypeSet enabled_types
= enabled_types_
;
53 enabled_types_
.Clear();
57 ConfigureReason
FakeSyncManager::GetAndResetConfigureReason() {
58 ConfigureReason reason
= last_configure_reason_
;
59 last_configure_reason_
= CONFIGURE_REASON_UNKNOWN
;
63 void FakeSyncManager::WaitForSyncThread() {
64 // Post a task to |sync_task_runner_| and block until it runs.
65 base::RunLoop run_loop
;
66 if (!sync_task_runner_
->PostTaskAndReply(
68 base::Bind(&base::DoNothing
),
69 run_loop
.QuitClosure())) {
75 void FakeSyncManager::Init(InitArgs
* args
) {
76 sync_task_runner_
= base::ThreadTaskRunnerHandle::Get();
77 PurgePartiallySyncedTypes();
79 test_user_share_
.SetUp();
80 UserShare
* share
= test_user_share_
.user_share();
81 for (ModelTypeSet::Iterator it
= initial_sync_ended_types_
.First();
82 it
.Good(); it
.Inc()) {
83 TestUserShare::CreateRoot(it
.Get(), share
);
86 FOR_EACH_OBSERVER(SyncManager::Observer
, observers_
,
87 OnInitializationComplete(
88 WeakHandle
<JsBackend
>(),
89 WeakHandle
<DataTypeDebugInfoListener
>(),
90 true, initial_sync_ended_types_
));
93 ModelTypeSet
FakeSyncManager::InitialSyncEndedTypes() {
94 return initial_sync_ended_types_
;
97 ModelTypeSet
FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
99 ModelTypeSet empty_types
= types
;
100 empty_types
.RemoveAll(progress_marker_types_
);
104 bool FakeSyncManager::PurgePartiallySyncedTypes() {
105 ModelTypeSet partial_types
;
106 for (ModelTypeSet::Iterator i
= progress_marker_types_
.First();
108 if (!initial_sync_ended_types_
.Has(i
.Get()))
109 partial_types
.Put(i
.Get());
111 progress_marker_types_
.RemoveAll(partial_types
);
112 cleaned_types_
.PutAll(partial_types
);
116 void FakeSyncManager::UpdateCredentials(const SyncCredentials
& credentials
) {
120 void FakeSyncManager::StartSyncingNormally(
121 const ModelSafeRoutingInfo
& routing_info
) {
125 void FakeSyncManager::ConfigureSyncer(
126 ConfigureReason reason
,
127 ModelTypeSet to_download
,
128 ModelTypeSet to_purge
,
129 ModelTypeSet to_journal
,
130 ModelTypeSet to_unapply
,
131 const ModelSafeRoutingInfo
& new_routing_info
,
132 const base::Closure
& ready_task
,
133 const base::Closure
& retry_task
) {
134 last_configure_reason_
= reason
;
135 enabled_types_
= GetRoutingInfoTypes(new_routing_info
);
136 ModelTypeSet success_types
= to_download
;
137 success_types
.RemoveAll(configure_fail_types_
);
139 DVLOG(1) << "Faking configuration. Downloading: "
140 << ModelTypeSetToString(success_types
) << ". Cleaning: "
141 << ModelTypeSetToString(to_purge
);
143 // Update our fake directory by clearing and fake-downloading as necessary.
144 UserShare
* share
= GetUserShare();
145 share
->directory
->PurgeEntriesWithTypeIn(to_purge
,
148 for (ModelTypeSet::Iterator it
= success_types
.First(); it
.Good(); it
.Inc()) {
149 // We must be careful to not create the same root node twice.
150 if (!initial_sync_ended_types_
.Has(it
.Get())) {
151 TestUserShare::CreateRoot(it
.Get(), share
);
155 // Simulate cleaning up disabled types.
156 // TODO(sync): consider only cleaning those types that were recently disabled,
157 // if this isn't the first cleanup, which more accurately reflects the
158 // behavior of the real cleanup logic.
159 initial_sync_ended_types_
.RemoveAll(to_purge
);
160 progress_marker_types_
.RemoveAll(to_purge
);
161 cleaned_types_
.PutAll(to_purge
);
163 // Now simulate the actual configuration for those types that successfully
165 progress_marker_types_
.PutAll(success_types
);
166 initial_sync_ended_types_
.PutAll(success_types
);
167 downloaded_types_
.PutAll(success_types
);
172 void FakeSyncManager::AddObserver(Observer
* observer
) {
173 observers_
.AddObserver(observer
);
176 void FakeSyncManager::RemoveObserver(Observer
* observer
) {
177 observers_
.RemoveObserver(observer
);
180 SyncStatus
FakeSyncManager::GetDetailedStatus() const {
185 void FakeSyncManager::SaveChanges() {
189 void FakeSyncManager::ShutdownOnSyncThread(ShutdownReason reason
) {
190 DCHECK(sync_task_runner_
->RunsTasksOnCurrentThread());
191 test_user_share_
.TearDown();
194 UserShare
* FakeSyncManager::GetUserShare() {
195 return test_user_share_
.user_share();
198 syncer::SyncContextProxy
* FakeSyncManager::GetSyncContextProxy() {
199 return &null_sync_context_proxy_
;
202 const std::string
FakeSyncManager::cache_guid() {
203 return test_user_share_
.user_share()->directory
->cache_guid();
206 bool FakeSyncManager::ReceivedExperiment(Experiments
* experiments
) {
210 bool FakeSyncManager::HasUnsyncedItems() {
215 SyncEncryptionHandler
* FakeSyncManager::GetEncryptionHandler() {
216 return fake_encryption_handler_
.get();
219 ScopedVector
<syncer::ProtocolEvent
>
220 FakeSyncManager::GetBufferedProtocolEvents() {
221 return ScopedVector
<syncer::ProtocolEvent
>();
224 scoped_ptr
<base::ListValue
> FakeSyncManager::GetAllNodesForType(
225 syncer::ModelType type
) {
226 return scoped_ptr
<base::ListValue
>(new base::ListValue());
229 void FakeSyncManager::RefreshTypes(ModelTypeSet types
) {
230 last_refresh_request_types_
= types
;
233 void FakeSyncManager::RegisterDirectoryTypeDebugInfoObserver(
234 syncer::TypeDebugInfoObserver
* observer
) {}
236 void FakeSyncManager::UnregisterDirectoryTypeDebugInfoObserver(
237 syncer::TypeDebugInfoObserver
* observer
) {}
239 bool FakeSyncManager::HasDirectoryTypeDebugInfoObserver(
240 syncer::TypeDebugInfoObserver
* observer
) {
244 void FakeSyncManager::RequestEmitDebugInfo() {}
246 void FakeSyncManager::OnIncomingInvalidation(
247 syncer::ModelType type
,
248 scoped_ptr
<InvalidationInterface
> invalidation
) {
252 ModelTypeSet
FakeSyncManager::GetLastRefreshRequestTypes() {
253 return last_refresh_request_types_
;
256 void FakeSyncManager::SetInvalidatorEnabled(bool invalidator_enabled
) {
260 } // namespace syncer