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 #ifndef COMPONENTS_SYNC_DRIVER_TEST_SYNC_SERVICE_H_
6 #define COMPONENTS_SYNC_DRIVER_TEST_SYNC_SERVICE_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/macros.h"
13 #include "base/observer_list.h"
14 #include "components/signin/public/identity_manager/account_info.h"
15 #include "components/sync/driver/sync_service.h"
16 #include "components/sync/driver/test_sync_user_settings.h"
17 #include "components/sync/engine/cycle/sync_cycle_snapshot.h"
18 #include "components/sync/engine/sync_status.h"
19 #include "google_apis/gaia/google_service_auth_error.h"
20 #include "url/gurl.h"
21 
22 namespace syncer {
23 
24 // A simple test implementation of SyncService that allows direct control over
25 // the returned state. By default, everything returns "enabled"/"active".
26 class TestSyncService : public SyncService {
27  public:
28   TestSyncService();
29   ~TestSyncService() override;
30 
31   void SetDisableReasons(DisableReasonSet disable_reasons);
32   void SetTransportState(TransportState transport_state);
33   void SetLocalSyncEnabled(bool local_sync_enabled);
34   void SetAuthenticatedAccountInfo(const CoreAccountInfo& account_info);
35   void SetIsAuthenticatedAccountPrimary(bool is_primary);
36   void SetSetupInProgress(bool in_progress);
37   void SetAuthError(const GoogleServiceAuthError& auth_error);
38   void SetFirstSetupComplete(bool first_setup_complete);
39   void SetPreferredDataTypes(const ModelTypeSet& types);
40   void SetActiveDataTypes(const ModelTypeSet& types);
41   void SetBackedOffDataTypes(const ModelTypeSet& types);
42   void SetLastCycleSnapshot(const SyncCycleSnapshot& snapshot);
43   // Convenience versions of the above, for when the caller doesn't care about
44   // the particular values in the snapshot, just whether there is one.
45   void SetEmptyLastCycleSnapshot();
46   void SetNonEmptyLastCycleSnapshot();
47   void SetDetailedSyncStatus(bool engine_available, SyncStatus status);
48   void SetPassphraseRequired(bool required);
49   void SetPassphraseRequiredForPreferredDataTypes(bool required);
50   void SetTrustedVaultKeyRequired(bool required);
51   void SetTrustedVaultKeyRequiredForPreferredDataTypes(bool required);
52   void SetTrustedVaultRecoverabilityDegraded(bool degraded);
53   void SetIsUsingSecondaryPassphrase(bool enabled);
54 
55   void FireStateChanged();
56   void FireSyncCycleCompleted();
57 
58   // SyncService implementation.
59   syncer::SyncUserSettings* GetUserSettings() override;
60   const syncer::SyncUserSettings* GetUserSettings() const override;
61   DisableReasonSet GetDisableReasons() const override;
62   TransportState GetTransportState() const override;
63   bool IsLocalSyncEnabled() const override;
64   CoreAccountInfo GetAuthenticatedAccountInfo() const override;
65   bool IsAuthenticatedAccountPrimary() const override;
66   GoogleServiceAuthError GetAuthError() const override;
67   base::Time GetAuthErrorTime() const override;
68   bool RequiresClientUpgrade() const override;
69 
70   std::unique_ptr<SyncSetupInProgressHandle> GetSetupInProgressHandle()
71       override;
72   bool IsSetupInProgress() const override;
73 
74   ModelTypeSet GetPreferredDataTypes() const override;
75   ModelTypeSet GetActiveDataTypes() const override;
76   ModelTypeSet GetBackedOffDataTypes() const override;
77 
78   void StopAndClear() override;
79   void OnDataTypeRequestsSyncStartup(ModelType type) override;
80   void TriggerRefresh(const ModelTypeSet& types) override;
81   void DataTypePreconditionChanged(syncer::ModelType type) override;
82 
83   void AddObserver(SyncServiceObserver* observer) override;
84   void RemoveObserver(SyncServiceObserver* observer) override;
85   bool HasObserver(const SyncServiceObserver* observer) const override;
86 
87   SyncTokenStatus GetSyncTokenStatusForDebugging() const override;
88   bool QueryDetailedSyncStatusForDebugging(SyncStatus* result) const override;
89   base::Time GetLastSyncedTimeForDebugging() const override;
90   SyncCycleSnapshot GetLastCycleSnapshotForDebugging() const override;
91   std::unique_ptr<base::Value> GetTypeStatusMapForDebugging() override;
92   void GetEntityCountsForDebugging(
93       base::OnceCallback<void(const std::vector<TypeEntitiesCount>&)> callback)
94       const override;
95   const GURL& GetSyncServiceUrlForDebugging() const override;
96   std::string GetUnrecoverableErrorMessageForDebugging() const override;
97   base::Location GetUnrecoverableErrorLocationForDebugging() const override;
98   void AddProtocolEventObserver(ProtocolEventObserver* observer) override;
99   void RemoveProtocolEventObserver(ProtocolEventObserver* observer) override;
100   base::WeakPtr<JsController> GetJsController() override;
101   void GetAllNodesForDebugging(
102       base::OnceCallback<void(std::unique_ptr<base::ListValue>)> callback)
103       override;
104   void SetInvalidationsForSessionsEnabled(bool enabled) override;
105   void AddTrustedVaultDecryptionKeysFromWeb(
106       const std::string& gaia_id,
107       const std::vector<std::vector<uint8_t>>& keys,
108       int last_key_version) override;
109   void AddTrustedVaultRecoveryMethodFromWeb(
110       const std::string& gaia_id,
111       const std::vector<uint8_t>& public_key,
112       base::OnceClosure callback) override;
113 
114   // KeyedService implementation.
115   void Shutdown() override;
116 
117  private:
118   TestSyncUserSettings user_settings_;
119 
120   DisableReasonSet disable_reasons_;
121   TransportState transport_state_ = TransportState::ACTIVE;
122   bool local_sync_enabled_ = false;
123   CoreAccountInfo account_info_;
124   bool account_is_primary_ = true;
125   bool setup_in_progress_ = false;
126   GoogleServiceAuthError auth_error_;
127 
128   ModelTypeSet preferred_data_types_;
129   ModelTypeSet active_data_types_;
130   ModelTypeSet backed_off_data_types_;
131 
132   bool detailed_sync_status_engine_available_ = false;
133   SyncStatus detailed_sync_status_;
134 
135   SyncCycleSnapshot last_cycle_snapshot_;
136 
137   base::ObserverList<syncer::SyncServiceObserver>::Unchecked observers_;
138 
139   GURL sync_service_url_;
140 
141   DISALLOW_COPY_AND_ASSIGN(TestSyncService);
142 };
143 
144 }  // namespace syncer
145 
146 #endif  // COMPONENTS_SYNC_DRIVER_TEST_SYNC_SERVICE_H_
147