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 num_invalidations_received_(0) {
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 int FakeSyncManager::GetInvalidationCount() const {
65 return num_invalidations_received_
;
68 void FakeSyncManager::WaitForSyncThread() {
69 // Post a task to |sync_task_runner_| and block until it runs.
70 base::RunLoop run_loop
;
71 if (!sync_task_runner_
->PostTaskAndReply(
73 base::Bind(&base::DoNothing
),
74 run_loop
.QuitClosure())) {
80 void FakeSyncManager::Init(InitArgs
* args
) {
81 sync_task_runner_
= base::ThreadTaskRunnerHandle::Get();
82 PurgePartiallySyncedTypes();
84 test_user_share_
.SetUp();
85 UserShare
* share
= test_user_share_
.user_share();
86 for (ModelTypeSet::Iterator it
= initial_sync_ended_types_
.First();
87 it
.Good(); it
.Inc()) {
88 TestUserShare::CreateRoot(it
.Get(), share
);
91 FOR_EACH_OBSERVER(SyncManager::Observer
, observers_
,
92 OnInitializationComplete(
93 WeakHandle
<JsBackend
>(),
94 WeakHandle
<DataTypeDebugInfoListener
>(),
95 true, initial_sync_ended_types_
));
98 ModelTypeSet
FakeSyncManager::InitialSyncEndedTypes() {
99 return initial_sync_ended_types_
;
102 ModelTypeSet
FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
103 ModelTypeSet types
) {
104 ModelTypeSet empty_types
= types
;
105 empty_types
.RemoveAll(progress_marker_types_
);
109 bool FakeSyncManager::PurgePartiallySyncedTypes() {
110 ModelTypeSet partial_types
;
111 for (ModelTypeSet::Iterator i
= progress_marker_types_
.First();
113 if (!initial_sync_ended_types_
.Has(i
.Get()))
114 partial_types
.Put(i
.Get());
116 progress_marker_types_
.RemoveAll(partial_types
);
117 cleaned_types_
.PutAll(partial_types
);
121 void FakeSyncManager::UpdateCredentials(const SyncCredentials
& credentials
) {
125 void FakeSyncManager::StartSyncingNormally(
126 const ModelSafeRoutingInfo
& routing_info
, base::Time last_poll_time
) {
130 void FakeSyncManager::ConfigureSyncer(
131 ConfigureReason reason
,
132 ModelTypeSet to_download
,
133 ModelTypeSet to_purge
,
134 ModelTypeSet to_journal
,
135 ModelTypeSet to_unapply
,
136 const ModelSafeRoutingInfo
& new_routing_info
,
137 const base::Closure
& ready_task
,
138 const base::Closure
& retry_task
) {
139 last_configure_reason_
= reason
;
140 enabled_types_
= GetRoutingInfoTypes(new_routing_info
);
141 ModelTypeSet success_types
= to_download
;
142 success_types
.RemoveAll(configure_fail_types_
);
144 DVLOG(1) << "Faking configuration. Downloading: "
145 << ModelTypeSetToString(success_types
) << ". Cleaning: "
146 << ModelTypeSetToString(to_purge
);
148 // Update our fake directory by clearing and fake-downloading as necessary.
149 UserShare
* share
= GetUserShare();
150 share
->directory
->PurgeEntriesWithTypeIn(to_purge
,
153 for (ModelTypeSet::Iterator it
= success_types
.First(); it
.Good(); it
.Inc()) {
154 // We must be careful to not create the same root node twice.
155 if (!initial_sync_ended_types_
.Has(it
.Get())) {
156 TestUserShare::CreateRoot(it
.Get(), share
);
160 // Simulate cleaning up disabled types.
161 // TODO(sync): consider only cleaning those types that were recently disabled,
162 // if this isn't the first cleanup, which more accurately reflects the
163 // behavior of the real cleanup logic.
164 initial_sync_ended_types_
.RemoveAll(to_purge
);
165 progress_marker_types_
.RemoveAll(to_purge
);
166 cleaned_types_
.PutAll(to_purge
);
168 // Now simulate the actual configuration for those types that successfully
170 progress_marker_types_
.PutAll(success_types
);
171 initial_sync_ended_types_
.PutAll(success_types
);
172 downloaded_types_
.PutAll(success_types
);
177 void FakeSyncManager::AddObserver(Observer
* observer
) {
178 observers_
.AddObserver(observer
);
181 void FakeSyncManager::RemoveObserver(Observer
* observer
) {
182 observers_
.RemoveObserver(observer
);
185 SyncStatus
FakeSyncManager::GetDetailedStatus() const {
190 void FakeSyncManager::SaveChanges() {
194 void FakeSyncManager::ShutdownOnSyncThread(ShutdownReason reason
) {
195 DCHECK(sync_task_runner_
->RunsTasksOnCurrentThread());
196 test_user_share_
.TearDown();
199 UserShare
* FakeSyncManager::GetUserShare() {
200 return test_user_share_
.user_share();
203 syncer_v2::SyncContextProxy
* FakeSyncManager::GetSyncContextProxy() {
204 return &null_sync_context_proxy_
;
207 const std::string
FakeSyncManager::cache_guid() {
208 return test_user_share_
.user_share()->directory
->cache_guid();
211 bool FakeSyncManager::ReceivedExperiment(Experiments
* experiments
) {
215 bool FakeSyncManager::HasUnsyncedItems() {
220 SyncEncryptionHandler
* FakeSyncManager::GetEncryptionHandler() {
221 return fake_encryption_handler_
.get();
224 ScopedVector
<syncer::ProtocolEvent
>
225 FakeSyncManager::GetBufferedProtocolEvents() {
226 return ScopedVector
<syncer::ProtocolEvent
>();
229 scoped_ptr
<base::ListValue
> FakeSyncManager::GetAllNodesForType(
230 syncer::ModelType type
) {
231 return scoped_ptr
<base::ListValue
>(new base::ListValue());
234 void FakeSyncManager::RefreshTypes(ModelTypeSet types
) {
235 last_refresh_request_types_
= types
;
238 void FakeSyncManager::RegisterDirectoryTypeDebugInfoObserver(
239 syncer::TypeDebugInfoObserver
* observer
) {}
241 void FakeSyncManager::UnregisterDirectoryTypeDebugInfoObserver(
242 syncer::TypeDebugInfoObserver
* observer
) {}
244 bool FakeSyncManager::HasDirectoryTypeDebugInfoObserver(
245 syncer::TypeDebugInfoObserver
* observer
) {
249 void FakeSyncManager::RequestEmitDebugInfo() {}
251 void FakeSyncManager::OnIncomingInvalidation(
252 syncer::ModelType type
,
253 scoped_ptr
<InvalidationInterface
> invalidation
) {
254 num_invalidations_received_
++;
257 ModelTypeSet
FakeSyncManager::GetLastRefreshRequestTypes() {
258 return last_refresh_request_types_
;
261 void FakeSyncManager::SetInvalidatorEnabled(bool invalidator_enabled
) {
265 void FakeSyncManager::ClearServerData(const ClearServerDataCallback
& callback
) {
269 } // namespace syncer