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 "sync/internal_api/public/test/fake_sync_manager.h"
6
7 #include <cstddef>
8
9 #include "base/bind.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"
22
23 class GURL;
24
25 namespace syncer {
26
FakeSyncManager(ModelTypeSet initial_sync_ended_types,ModelTypeSet progress_marker_types,ModelTypeSet configure_fail_types)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());
35 }
36
~FakeSyncManager()37 FakeSyncManager::~FakeSyncManager() {}
38
GetAndResetCleanedTypes()39 ModelTypeSet FakeSyncManager::GetAndResetCleanedTypes() {
40 ModelTypeSet cleaned_types = cleaned_types_;
41 cleaned_types_.Clear();
42 return cleaned_types;
43 }
44
GetAndResetDownloadedTypes()45 ModelTypeSet FakeSyncManager::GetAndResetDownloadedTypes() {
46 ModelTypeSet downloaded_types = downloaded_types_;
47 downloaded_types_.Clear();
48 return downloaded_types;
49 }
50
GetAndResetEnabledTypes()51 ModelTypeSet FakeSyncManager::GetAndResetEnabledTypes() {
52 ModelTypeSet enabled_types = enabled_types_;
53 enabled_types_.Clear();
54 return enabled_types;
55 }
56
GetAndResetConfigureReason()57 ConfigureReason FakeSyncManager::GetAndResetConfigureReason() {
58 ConfigureReason reason = last_configure_reason_;
59 last_configure_reason_ = CONFIGURE_REASON_UNKNOWN;
60 return reason;
61 }
62
WaitForSyncThread()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(
67 FROM_HERE,
68 base::Bind(&base::DoNothing),
69 run_loop.QuitClosure())) {
70 NOTREACHED();
71 }
72 run_loop.Run();
73 }
74
Init(InitArgs * args)75 void FakeSyncManager::Init(InitArgs* args) {
76 sync_task_runner_ = base::ThreadTaskRunnerHandle::Get();
77 PurgePartiallySyncedTypes();
78
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);
84 }
85
86 FOR_EACH_OBSERVER(SyncManager::Observer, observers_,
87 OnInitializationComplete(
88 WeakHandle<JsBackend>(),
89 WeakHandle<DataTypeDebugInfoListener>(),
90 true, initial_sync_ended_types_));
91 }
92
InitialSyncEndedTypes()93 ModelTypeSet FakeSyncManager::InitialSyncEndedTypes() {
94 return initial_sync_ended_types_;
95 }
96
GetTypesWithEmptyProgressMarkerToken(ModelTypeSet types)97 ModelTypeSet FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
98 ModelTypeSet types) {
99 ModelTypeSet empty_types = types;
100 empty_types.RemoveAll(progress_marker_types_);
101 return empty_types;
102 }
103
PurgePartiallySyncedTypes()104 bool FakeSyncManager::PurgePartiallySyncedTypes() {
105 ModelTypeSet partial_types;
106 for (ModelTypeSet::Iterator i = progress_marker_types_.First();
107 i.Good(); i.Inc()) {
108 if (!initial_sync_ended_types_.Has(i.Get()))
109 partial_types.Put(i.Get());
110 }
111 progress_marker_types_.RemoveAll(partial_types);
112 cleaned_types_.PutAll(partial_types);
113 return true;
114 }
115
UpdateCredentials(const SyncCredentials & credentials)116 void FakeSyncManager::UpdateCredentials(const SyncCredentials& credentials) {
117 NOTIMPLEMENTED();
118 }
119
StartSyncingNormally(const ModelSafeRoutingInfo & routing_info)120 void FakeSyncManager::StartSyncingNormally(
121 const ModelSafeRoutingInfo& routing_info) {
122 // Do nothing.
123 }
124
ConfigureSyncer(ConfigureReason reason,ModelTypeSet to_download,ModelTypeSet to_purge,ModelTypeSet to_journal,ModelTypeSet to_unapply,const ModelSafeRoutingInfo & new_routing_info,const base::Closure & ready_task,const base::Closure & retry_task)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_);
138
139 DVLOG(1) << "Faking configuration. Downloading: "
140 << ModelTypeSetToString(success_types) << ". Cleaning: "
141 << ModelTypeSetToString(to_purge);
142
143 // Update our fake directory by clearing and fake-downloading as necessary.
144 UserShare* share = GetUserShare();
145 share->directory->PurgeEntriesWithTypeIn(to_purge,
146 to_journal,
147 to_unapply);
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);
152 }
153 }
154
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);
162
163 // Now simulate the actual configuration for those types that successfully
164 // download + apply.
165 progress_marker_types_.PutAll(success_types);
166 initial_sync_ended_types_.PutAll(success_types);
167 downloaded_types_.PutAll(success_types);
168
169 ready_task.Run();
170 }
171
AddObserver(Observer * observer)172 void FakeSyncManager::AddObserver(Observer* observer) {
173 observers_.AddObserver(observer);
174 }
175
RemoveObserver(Observer * observer)176 void FakeSyncManager::RemoveObserver(Observer* observer) {
177 observers_.RemoveObserver(observer);
178 }
179
GetDetailedStatus() const180 SyncStatus FakeSyncManager::GetDetailedStatus() const {
181 NOTIMPLEMENTED();
182 return SyncStatus();
183 }
184
SaveChanges()185 void FakeSyncManager::SaveChanges() {
186 // Do nothing.
187 }
188
ShutdownOnSyncThread(ShutdownReason reason)189 void FakeSyncManager::ShutdownOnSyncThread(ShutdownReason reason) {
190 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
191 test_user_share_.TearDown();
192 }
193
GetUserShare()194 UserShare* FakeSyncManager::GetUserShare() {
195 return test_user_share_.user_share();
196 }
197
GetSyncContextProxy()198 syncer::SyncContextProxy* FakeSyncManager::GetSyncContextProxy() {
199 return &null_sync_context_proxy_;
200 }
201
cache_guid()202 const std::string FakeSyncManager::cache_guid() {
203 return test_user_share_.user_share()->directory->cache_guid();
204 }
205
ReceivedExperiment(Experiments * experiments)206 bool FakeSyncManager::ReceivedExperiment(Experiments* experiments) {
207 return false;
208 }
209
HasUnsyncedItems()210 bool FakeSyncManager::HasUnsyncedItems() {
211 NOTIMPLEMENTED();
212 return false;
213 }
214
GetEncryptionHandler()215 SyncEncryptionHandler* FakeSyncManager::GetEncryptionHandler() {
216 return fake_encryption_handler_.get();
217 }
218
219 ScopedVector<syncer::ProtocolEvent>
GetBufferedProtocolEvents()220 FakeSyncManager::GetBufferedProtocolEvents() {
221 return ScopedVector<syncer::ProtocolEvent>();
222 }
223
GetAllNodesForType(syncer::ModelType type)224 scoped_ptr<base::ListValue> FakeSyncManager::GetAllNodesForType(
225 syncer::ModelType type) {
226 return scoped_ptr<base::ListValue>(new base::ListValue());
227 }
228
RefreshTypes(ModelTypeSet types)229 void FakeSyncManager::RefreshTypes(ModelTypeSet types) {
230 last_refresh_request_types_ = types;
231 }
232
RegisterDirectoryTypeDebugInfoObserver(syncer::TypeDebugInfoObserver * observer)233 void FakeSyncManager::RegisterDirectoryTypeDebugInfoObserver(
234 syncer::TypeDebugInfoObserver* observer) {}
235
UnregisterDirectoryTypeDebugInfoObserver(syncer::TypeDebugInfoObserver * observer)236 void FakeSyncManager::UnregisterDirectoryTypeDebugInfoObserver(
237 syncer::TypeDebugInfoObserver* observer) {}
238
HasDirectoryTypeDebugInfoObserver(syncer::TypeDebugInfoObserver * observer)239 bool FakeSyncManager::HasDirectoryTypeDebugInfoObserver(
240 syncer::TypeDebugInfoObserver* observer) {
241 return false;
242 }
243
RequestEmitDebugInfo()244 void FakeSyncManager::RequestEmitDebugInfo() {}
245
OnIncomingInvalidation(syncer::ModelType type,scoped_ptr<InvalidationInterface> invalidation)246 void FakeSyncManager::OnIncomingInvalidation(
247 syncer::ModelType type,
248 scoped_ptr<InvalidationInterface> invalidation) {
249 // Do nothing.
250 }
251
GetLastRefreshRequestTypes()252 ModelTypeSet FakeSyncManager::GetLastRefreshRequestTypes() {
253 return last_refresh_request_types_;
254 }
255
SetInvalidatorEnabled(bool invalidator_enabled)256 void FakeSyncManager::SetInvalidatorEnabled(bool invalidator_enabled) {
257 // Do nothing.
258 }
259
260 } // namespace syncer
261