test_sync_service.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include "components/sync/driver/test_sync_service.h"
  5. #include <utility>
  6. #include <vector>
  7. #include "base/time/time.h"
  8. #include "base/values.h"
  9. #include "components/sync/base/progress_marker_map.h"
  10. #include "components/sync/driver/sync_token_status.h"
  11. #include "components/sync/engine/cycle/model_neutral_state.h"
  12. #include "components/sync/model/type_entities_count.h"
  13. namespace syncer {
  14. namespace {
  15. SyncCycleSnapshot MakeDefaultCycleSnapshot() {
  16. return SyncCycleSnapshot(
  17. /*birthday=*/"", /*bag_of_chips=*/"", ModelNeutralState(),
  18. ProgressMarkerMap(), /*is_silenced-*/ false,
  19. /*num_server_conflicts=*/7, /*notifications_enabled=*/false,
  20. /*sync_start_time=*/base::Time::Now(),
  21. /*poll_finish_time=*/base::Time::Now(),
  22. /*get_updates_origin=*/sync_pb::SyncEnums::UNKNOWN_ORIGIN,
  23. /*poll_interval=*/base::Minutes(30),
  24. /*has_remaining_local_changes=*/false);
  25. }
  26. } // namespace
  27. TestSyncService::TestSyncService()
  28. : user_settings_(this),
  29. preferred_data_types_(ModelTypeSet::All()),
  30. active_data_types_(ModelTypeSet::All()),
  31. last_cycle_snapshot_(MakeDefaultCycleSnapshot()) {}
  32. TestSyncService::~TestSyncService() = default;
  33. void TestSyncService::SetDisableReasons(DisableReasonSet disable_reasons) {
  34. disable_reasons_ = disable_reasons;
  35. }
  36. void TestSyncService::SetTransportState(TransportState transport_state) {
  37. transport_state_ = transport_state;
  38. }
  39. void TestSyncService::SetLocalSyncEnabled(bool local_sync_enabled) {
  40. local_sync_enabled_ = local_sync_enabled;
  41. }
  42. void TestSyncService::SetAccountInfo(const CoreAccountInfo& account_info) {
  43. account_info_ = account_info;
  44. }
  45. void TestSyncService::SetSetupInProgress(bool in_progress) {
  46. setup_in_progress_ = in_progress;
  47. }
  48. void TestSyncService::SetHasSyncConsent(bool has_sync_consent) {
  49. has_sync_consent_ = has_sync_consent;
  50. }
  51. void TestSyncService::SetAuthError(const GoogleServiceAuthError& auth_error) {
  52. auth_error_ = auth_error;
  53. }
  54. void TestSyncService::SetFirstSetupComplete(bool first_setup_complete) {
  55. if (first_setup_complete)
  56. user_settings_.SetFirstSetupComplete();
  57. else
  58. user_settings_.ClearFirstSetupComplete();
  59. }
  60. void TestSyncService::SetPreferredDataTypes(const ModelTypeSet& types) {
  61. preferred_data_types_ = types;
  62. }
  63. void TestSyncService::SetActiveDataTypes(const ModelTypeSet& types) {
  64. active_data_types_ = types;
  65. }
  66. void TestSyncService::SetLastCycleSnapshot(const SyncCycleSnapshot& snapshot) {
  67. last_cycle_snapshot_ = snapshot;
  68. }
  69. void TestSyncService::SetEmptyLastCycleSnapshot() {
  70. SetLastCycleSnapshot(SyncCycleSnapshot());
  71. }
  72. void TestSyncService::SetNonEmptyLastCycleSnapshot() {
  73. SetLastCycleSnapshot(MakeDefaultCycleSnapshot());
  74. }
  75. void TestSyncService::SetDetailedSyncStatus(bool engine_available,
  76. SyncStatus status) {
  77. detailed_sync_status_engine_available_ = engine_available;
  78. detailed_sync_status_ = status;
  79. }
  80. void TestSyncService::SetPassphraseRequired(bool required) {
  81. user_settings_.SetPassphraseRequired(required);
  82. }
  83. void TestSyncService::SetPassphraseRequiredForPreferredDataTypes(
  84. bool required) {
  85. user_settings_.SetPassphraseRequiredForPreferredDataTypes(required);
  86. }
  87. void TestSyncService::SetTrustedVaultKeyRequired(bool required) {
  88. user_settings_.SetTrustedVaultKeyRequired(required);
  89. }
  90. void TestSyncService::SetTrustedVaultKeyRequiredForPreferredDataTypes(
  91. bool required) {
  92. user_settings_.SetTrustedVaultKeyRequiredForPreferredDataTypes(required);
  93. }
  94. void TestSyncService::SetTrustedVaultRecoverabilityDegraded(bool degraded) {
  95. user_settings_.SetTrustedVaultRecoverabilityDegraded(degraded);
  96. }
  97. void TestSyncService::SetIsUsingExplicitPassphrase(bool enabled) {
  98. user_settings_.SetIsUsingExplicitPassphrase(enabled);
  99. }
  100. void TestSyncService::FireStateChanged() {
  101. for (SyncServiceObserver& observer : observers_)
  102. observer.OnStateChanged(this);
  103. }
  104. void TestSyncService::FireSyncCycleCompleted() {
  105. for (SyncServiceObserver& observer : observers_)
  106. observer.OnSyncCycleCompleted(this);
  107. }
  108. SyncUserSettings* TestSyncService::GetUserSettings() {
  109. return &user_settings_;
  110. }
  111. const SyncUserSettings* TestSyncService::GetUserSettings() const {
  112. return &user_settings_;
  113. }
  114. SyncService::DisableReasonSet TestSyncService::GetDisableReasons() const {
  115. return disable_reasons_;
  116. }
  117. SyncService::TransportState TestSyncService::GetTransportState() const {
  118. return transport_state_;
  119. }
  120. bool TestSyncService::IsLocalSyncEnabled() const {
  121. return local_sync_enabled_;
  122. }
  123. CoreAccountInfo TestSyncService::GetAccountInfo() const {
  124. return account_info_;
  125. }
  126. bool TestSyncService::HasSyncConsent() const {
  127. return has_sync_consent_;
  128. }
  129. GoogleServiceAuthError TestSyncService::GetAuthError() const {
  130. return auth_error_;
  131. }
  132. base::Time TestSyncService::GetAuthErrorTime() const {
  133. return base::Time();
  134. }
  135. bool TestSyncService::RequiresClientUpgrade() const {
  136. return detailed_sync_status_.sync_protocol_error.action ==
  137. syncer::UPGRADE_CLIENT;
  138. }
  139. std::unique_ptr<SyncSetupInProgressHandle>
  140. TestSyncService::GetSetupInProgressHandle() {
  141. return nullptr;
  142. }
  143. bool TestSyncService::IsSetupInProgress() const {
  144. return setup_in_progress_;
  145. }
  146. ModelTypeSet TestSyncService::GetPreferredDataTypes() const {
  147. return preferred_data_types_;
  148. }
  149. ModelTypeSet TestSyncService::GetActiveDataTypes() const {
  150. return active_data_types_;
  151. }
  152. void TestSyncService::StopAndClear() {}
  153. void TestSyncService::OnDataTypeRequestsSyncStartup(ModelType type) {}
  154. void TestSyncService::TriggerRefresh(const ModelTypeSet& types) {}
  155. void TestSyncService::DataTypePreconditionChanged(ModelType type) {}
  156. void TestSyncService::AddObserver(SyncServiceObserver* observer) {
  157. observers_.AddObserver(observer);
  158. }
  159. void TestSyncService::RemoveObserver(SyncServiceObserver* observer) {
  160. observers_.RemoveObserver(observer);
  161. }
  162. bool TestSyncService::HasObserver(const SyncServiceObserver* observer) const {
  163. return observers_.HasObserver(observer);
  164. }
  165. SyncTokenStatus TestSyncService::GetSyncTokenStatusForDebugging() const {
  166. SyncTokenStatus token;
  167. if (GetAuthError().state() != GoogleServiceAuthError::NONE) {
  168. token.connection_status = ConnectionStatus::CONNECTION_AUTH_ERROR;
  169. token.last_get_token_error =
  170. GoogleServiceAuthError::FromServiceError("error");
  171. }
  172. return token;
  173. }
  174. bool TestSyncService::QueryDetailedSyncStatusForDebugging(
  175. SyncStatus* result) const {
  176. *result = detailed_sync_status_;
  177. return detailed_sync_status_engine_available_;
  178. }
  179. base::Time TestSyncService::GetLastSyncedTimeForDebugging() const {
  180. return base::Time();
  181. }
  182. SyncCycleSnapshot TestSyncService::GetLastCycleSnapshotForDebugging() const {
  183. return last_cycle_snapshot_;
  184. }
  185. std::unique_ptr<base::Value> TestSyncService::GetTypeStatusMapForDebugging()
  186. const {
  187. return std::make_unique<base::ListValue>();
  188. }
  189. void TestSyncService::GetEntityCountsForDebugging(
  190. base::OnceCallback<void(const std::vector<TypeEntitiesCount>&)> callback)
  191. const {
  192. std::move(callback).Run({});
  193. }
  194. const GURL& TestSyncService::GetSyncServiceUrlForDebugging() const {
  195. return sync_service_url_;
  196. }
  197. std::string TestSyncService::GetUnrecoverableErrorMessageForDebugging() const {
  198. return std::string();
  199. }
  200. base::Location TestSyncService::GetUnrecoverableErrorLocationForDebugging()
  201. const {
  202. return base::Location();
  203. }
  204. void TestSyncService::AddProtocolEventObserver(
  205. ProtocolEventObserver* observer) {}
  206. void TestSyncService::RemoveProtocolEventObserver(
  207. ProtocolEventObserver* observer) {}
  208. void TestSyncService::GetAllNodesForDebugging(
  209. base::OnceCallback<void(base::Value::List)> callback) {}
  210. void TestSyncService::SetInvalidationsForSessionsEnabled(bool enabled) {}
  211. void TestSyncService::AddTrustedVaultDecryptionKeysFromWeb(
  212. const std::string& gaia_id,
  213. const std::vector<std::vector<uint8_t>>& keys,
  214. int last_key_version) {}
  215. void TestSyncService::AddTrustedVaultRecoveryMethodFromWeb(
  216. const std::string& gaia_id,
  217. const std::vector<uint8_t>& public_key,
  218. int method_type_hint,
  219. base::OnceClosure callback) {}
  220. void TestSyncService::Shutdown() {
  221. for (SyncServiceObserver& observer : observers_)
  222. observer.OnSyncShutdown(this);
  223. }
  224. } // namespace syncer