1 // Copyright 2018 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 "components/sync/driver/test_sync_service.h"
6 
7 #include <utility>
8 #include <vector>
9 
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "components/sync/base/progress_marker_map.h"
13 #include "components/sync/driver/sync_token_status.h"
14 #include "components/sync/engine/cycle/model_neutral_state.h"
15 #include "components/sync/model/type_entities_count.h"
16 
17 namespace syncer {
18 
19 namespace {
20 
MakeDefaultCycleSnapshot()21 SyncCycleSnapshot MakeDefaultCycleSnapshot() {
22   return SyncCycleSnapshot(
23       /*birthday=*/"", /*bag_of_chips=*/"", ModelNeutralState(),
24       ProgressMarkerMap(), /*is_silenced-*/ false,
25       /*num_encryption_conflicts=*/5, /*num_hierarchy_conflicts=*/2,
26       /*num_server_conflicts=*/7, /*notifications_enabled=*/false,
27       /*num_entries=*/0, /*sync_start_time=*/base::Time::Now(),
28       /*poll_finish_time=*/base::Time::Now(),
29       /*num_entries_by_type=*/std::vector<int>(ModelType::NUM_ENTRIES, 0),
30       /*num_to_delete_entries_by_type=*/
31       std::vector<int>(ModelType::NUM_ENTRIES, 0),
32       /*get_updates_origin=*/sync_pb::SyncEnums::UNKNOWN_ORIGIN,
33       /*poll_interval=*/base::TimeDelta::FromMinutes(30),
34       /*has_remaining_local_changes=*/false);
35 }
36 
37 }  // namespace
38 
TestSyncService()39 TestSyncService::TestSyncService()
40     : user_settings_(this),
41       preferred_data_types_(ModelTypeSet::All()),
42       active_data_types_(ModelTypeSet::All()),
43       last_cycle_snapshot_(MakeDefaultCycleSnapshot()) {}
44 
45 TestSyncService::~TestSyncService() = default;
46 
SetDisableReasons(DisableReasonSet disable_reasons)47 void TestSyncService::SetDisableReasons(DisableReasonSet disable_reasons) {
48   disable_reasons_ = disable_reasons;
49 }
50 
SetTransportState(TransportState transport_state)51 void TestSyncService::SetTransportState(TransportState transport_state) {
52   transport_state_ = transport_state;
53 }
54 
SetLocalSyncEnabled(bool local_sync_enabled)55 void TestSyncService::SetLocalSyncEnabled(bool local_sync_enabled) {
56   local_sync_enabled_ = local_sync_enabled;
57 }
58 
SetAuthenticatedAccountInfo(const CoreAccountInfo & account_info)59 void TestSyncService::SetAuthenticatedAccountInfo(
60     const CoreAccountInfo& account_info) {
61   account_info_ = account_info;
62 }
63 
SetSetupInProgress(bool in_progress)64 void TestSyncService::SetSetupInProgress(bool in_progress) {
65   setup_in_progress_ = in_progress;
66 }
67 
SetIsAuthenticatedAccountPrimary(bool is_primary)68 void TestSyncService::SetIsAuthenticatedAccountPrimary(bool is_primary) {
69   account_is_primary_ = is_primary;
70 }
71 
SetAuthError(const GoogleServiceAuthError & auth_error)72 void TestSyncService::SetAuthError(const GoogleServiceAuthError& auth_error) {
73   auth_error_ = auth_error;
74 }
75 
SetFirstSetupComplete(bool first_setup_complete)76 void TestSyncService::SetFirstSetupComplete(bool first_setup_complete) {
77   if (first_setup_complete)
78     user_settings_.SetFirstSetupComplete();
79   else
80     user_settings_.ClearFirstSetupComplete();
81 }
82 
SetPreferredDataTypes(const ModelTypeSet & types)83 void TestSyncService::SetPreferredDataTypes(const ModelTypeSet& types) {
84   preferred_data_types_ = types;
85 }
86 
SetActiveDataTypes(const ModelTypeSet & types)87 void TestSyncService::SetActiveDataTypes(const ModelTypeSet& types) {
88   active_data_types_ = types;
89 }
90 
SetBackedOffDataTypes(const ModelTypeSet & types)91 void TestSyncService::SetBackedOffDataTypes(const ModelTypeSet& types) {
92   backed_off_data_types_ = types;
93 }
94 
SetLastCycleSnapshot(const SyncCycleSnapshot & snapshot)95 void TestSyncService::SetLastCycleSnapshot(const SyncCycleSnapshot& snapshot) {
96   last_cycle_snapshot_ = snapshot;
97 }
98 
SetEmptyLastCycleSnapshot()99 void TestSyncService::SetEmptyLastCycleSnapshot() {
100   SetLastCycleSnapshot(SyncCycleSnapshot());
101 }
102 
SetNonEmptyLastCycleSnapshot()103 void TestSyncService::SetNonEmptyLastCycleSnapshot() {
104   SetLastCycleSnapshot(MakeDefaultCycleSnapshot());
105 }
106 
SetDetailedSyncStatus(bool engine_available,SyncStatus status)107 void TestSyncService::SetDetailedSyncStatus(bool engine_available,
108                                             SyncStatus status) {
109   detailed_sync_status_engine_available_ = engine_available;
110   detailed_sync_status_ = status;
111 }
112 
SetPassphraseRequired(bool required)113 void TestSyncService::SetPassphraseRequired(bool required) {
114   user_settings_.SetPassphraseRequired(required);
115 }
116 
SetPassphraseRequiredForPreferredDataTypes(bool required)117 void TestSyncService::SetPassphraseRequiredForPreferredDataTypes(
118     bool required) {
119   user_settings_.SetPassphraseRequiredForPreferredDataTypes(required);
120 }
121 
SetTrustedVaultKeyRequired(bool required)122 void TestSyncService::SetTrustedVaultKeyRequired(bool required) {
123   user_settings_.SetTrustedVaultKeyRequired(required);
124 }
125 
SetTrustedVaultKeyRequiredForPreferredDataTypes(bool required)126 void TestSyncService::SetTrustedVaultKeyRequiredForPreferredDataTypes(
127     bool required) {
128   user_settings_.SetTrustedVaultKeyRequiredForPreferredDataTypes(required);
129 }
130 
SetTrustedVaultRecoverabilityDegraded(bool degraded)131 void TestSyncService::SetTrustedVaultRecoverabilityDegraded(bool degraded) {
132   user_settings_.SetTrustedVaultRecoverabilityDegraded(degraded);
133 }
134 
SetIsUsingSecondaryPassphrase(bool enabled)135 void TestSyncService::SetIsUsingSecondaryPassphrase(bool enabled) {
136   user_settings_.SetIsUsingSecondaryPassphrase(enabled);
137 }
138 
FireStateChanged()139 void TestSyncService::FireStateChanged() {
140   for (auto& observer : observers_)
141     observer.OnStateChanged(this);
142 }
143 
FireSyncCycleCompleted()144 void TestSyncService::FireSyncCycleCompleted() {
145   for (auto& observer : observers_)
146     observer.OnSyncCycleCompleted(this);
147 }
148 
GetUserSettings()149 SyncUserSettings* TestSyncService::GetUserSettings() {
150   return &user_settings_;
151 }
152 
GetUserSettings() const153 const SyncUserSettings* TestSyncService::GetUserSettings() const {
154   return &user_settings_;
155 }
156 
GetDisableReasons() const157 SyncService::DisableReasonSet TestSyncService::GetDisableReasons() const {
158   return disable_reasons_;
159 }
160 
GetTransportState() const161 SyncService::TransportState TestSyncService::GetTransportState() const {
162   return transport_state_;
163 }
164 
IsLocalSyncEnabled() const165 bool TestSyncService::IsLocalSyncEnabled() const {
166   return local_sync_enabled_;
167 }
168 
GetAuthenticatedAccountInfo() const169 CoreAccountInfo TestSyncService::GetAuthenticatedAccountInfo() const {
170   return account_info_;
171 }
172 
IsAuthenticatedAccountPrimary() const173 bool TestSyncService::IsAuthenticatedAccountPrimary() const {
174   return account_is_primary_;
175 }
176 
GetAuthError() const177 GoogleServiceAuthError TestSyncService::GetAuthError() const {
178   return auth_error_;
179 }
180 
GetAuthErrorTime() const181 base::Time TestSyncService::GetAuthErrorTime() const {
182   return base::Time();
183 }
184 
RequiresClientUpgrade() const185 bool TestSyncService::RequiresClientUpgrade() const {
186   return detailed_sync_status_.sync_protocol_error.action ==
187          syncer::UPGRADE_CLIENT;
188 }
189 
190 std::unique_ptr<SyncSetupInProgressHandle>
GetSetupInProgressHandle()191 TestSyncService::GetSetupInProgressHandle() {
192   return nullptr;
193 }
194 
IsSetupInProgress() const195 bool TestSyncService::IsSetupInProgress() const {
196   return setup_in_progress_;
197 }
198 
GetPreferredDataTypes() const199 ModelTypeSet TestSyncService::GetPreferredDataTypes() const {
200   return preferred_data_types_;
201 }
202 
GetActiveDataTypes() const203 ModelTypeSet TestSyncService::GetActiveDataTypes() const {
204   return active_data_types_;
205 }
206 
GetBackedOffDataTypes() const207 ModelTypeSet TestSyncService::GetBackedOffDataTypes() const {
208   return backed_off_data_types_;
209 }
210 
StopAndClear()211 void TestSyncService::StopAndClear() {}
212 
OnDataTypeRequestsSyncStartup(ModelType type)213 void TestSyncService::OnDataTypeRequestsSyncStartup(ModelType type) {}
214 
TriggerRefresh(const ModelTypeSet & types)215 void TestSyncService::TriggerRefresh(const ModelTypeSet& types) {}
216 
DataTypePreconditionChanged(ModelType type)217 void TestSyncService::DataTypePreconditionChanged(ModelType type) {}
218 
AddObserver(SyncServiceObserver * observer)219 void TestSyncService::AddObserver(SyncServiceObserver* observer) {
220   observers_.AddObserver(observer);
221 }
222 
RemoveObserver(SyncServiceObserver * observer)223 void TestSyncService::RemoveObserver(SyncServiceObserver* observer) {
224   observers_.RemoveObserver(observer);
225 }
226 
HasObserver(const SyncServiceObserver * observer) const227 bool TestSyncService::HasObserver(const SyncServiceObserver* observer) const {
228   return observers_.HasObserver(observer);
229 }
230 
GetSyncTokenStatusForDebugging() const231 SyncTokenStatus TestSyncService::GetSyncTokenStatusForDebugging() const {
232   SyncTokenStatus token;
233 
234   if (GetAuthError().state() != GoogleServiceAuthError::NONE) {
235     token.connection_status = ConnectionStatus::CONNECTION_AUTH_ERROR;
236     token.last_get_token_error =
237         GoogleServiceAuthError::FromServiceError("error");
238   }
239 
240   return token;
241 }
242 
QueryDetailedSyncStatusForDebugging(SyncStatus * result) const243 bool TestSyncService::QueryDetailedSyncStatusForDebugging(
244     SyncStatus* result) const {
245   *result = detailed_sync_status_;
246   return detailed_sync_status_engine_available_;
247 }
248 
GetLastSyncedTimeForDebugging() const249 base::Time TestSyncService::GetLastSyncedTimeForDebugging() const {
250   return base::Time();
251 }
252 
GetLastCycleSnapshotForDebugging() const253 SyncCycleSnapshot TestSyncService::GetLastCycleSnapshotForDebugging() const {
254   return last_cycle_snapshot_;
255 }
256 
GetTypeStatusMapForDebugging()257 std::unique_ptr<base::Value> TestSyncService::GetTypeStatusMapForDebugging() {
258   return std::make_unique<base::ListValue>();
259 }
260 
GetEntityCountsForDebugging(base::OnceCallback<void (const std::vector<TypeEntitiesCount> &)> callback) const261 void TestSyncService::GetEntityCountsForDebugging(
262     base::OnceCallback<void(const std::vector<TypeEntitiesCount>&)> callback)
263     const {
264   std::move(callback).Run({});
265 }
266 
GetSyncServiceUrlForDebugging() const267 const GURL& TestSyncService::GetSyncServiceUrlForDebugging() const {
268   return sync_service_url_;
269 }
270 
GetUnrecoverableErrorMessageForDebugging() const271 std::string TestSyncService::GetUnrecoverableErrorMessageForDebugging() const {
272   return std::string();
273 }
274 
GetUnrecoverableErrorLocationForDebugging() const275 base::Location TestSyncService::GetUnrecoverableErrorLocationForDebugging()
276     const {
277   return base::Location();
278 }
279 
AddProtocolEventObserver(ProtocolEventObserver * observer)280 void TestSyncService::AddProtocolEventObserver(
281     ProtocolEventObserver* observer) {}
282 
RemoveProtocolEventObserver(ProtocolEventObserver * observer)283 void TestSyncService::RemoveProtocolEventObserver(
284     ProtocolEventObserver* observer) {}
285 
GetJsController()286 base::WeakPtr<JsController> TestSyncService::GetJsController() {
287   return base::WeakPtr<JsController>();
288 }
289 
GetAllNodesForDebugging(base::OnceCallback<void (std::unique_ptr<base::ListValue>)> callback)290 void TestSyncService::GetAllNodesForDebugging(
291     base::OnceCallback<void(std::unique_ptr<base::ListValue>)> callback) {}
292 
SetInvalidationsForSessionsEnabled(bool enabled)293 void TestSyncService::SetInvalidationsForSessionsEnabled(bool enabled) {}
294 
AddTrustedVaultDecryptionKeysFromWeb(const std::string & gaia_id,const std::vector<std::vector<uint8_t>> & keys,int last_key_version)295 void TestSyncService::AddTrustedVaultDecryptionKeysFromWeb(
296     const std::string& gaia_id,
297     const std::vector<std::vector<uint8_t>>& keys,
298     int last_key_version) {}
299 
AddTrustedVaultRecoveryMethodFromWeb(const std::string & gaia_id,const std::vector<uint8_t> & public_key,base::OnceClosure callback)300 void TestSyncService::AddTrustedVaultRecoveryMethodFromWeb(
301     const std::string& gaia_id,
302     const std::vector<uint8_t>& public_key,
303     base::OnceClosure callback) {}
304 
Shutdown()305 void TestSyncService::Shutdown() {
306   for (auto& observer : observers_)
307     observer.OnSyncShutdown(this);
308 }
309 
310 }  // namespace syncer
311