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 #ifndef COMPONENTS_SYNC_ENGINE_FAKE_SYNC_MANAGER_H_
6 #define COMPONENTS_SYNC_ENGINE_FAKE_SYNC_MANAGER_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include "base/callback_forward.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/observer_list.h"
17 #include "components/sync/base/model_type.h"
18 #include "components/sync/engine/fake_model_type_connector.h"
19 #include "components/sync/engine/sync_manager.h"
20 #include "components/sync/test/fake_sync_encryption_handler.h"
21 
22 namespace base {
23 class SequencedTaskRunner;
24 }
25 
26 namespace syncer {
27 
28 class FakeSyncEncryptionHandler;
29 
30 class FakeSyncManager : public SyncManager {
31  public:
32   // |initial_sync_ended_types|: The set of types that have initial_sync_ended
33   // set to true. This value will be used by InitialSyncEndedTypes() until the
34   // next configuration is performed.
35   //
36   // |progress_marker_types|: The set of types that have valid progress
37   // markers. This will be used by GetTypesWithEmptyProgressMarkerToken() until
38   // the next configuration is performed.
39   //
40   // |configure_fail_types|: The set of types that will fail
41   // configuration. Once ConfigureSyncer is called, the
42   // |initial_sync_ended_types_| and |progress_marker_types_| will be updated
43   // to include those types that didn't fail.
44   FakeSyncManager(ModelTypeSet initial_sync_ended_types,
45                   ModelTypeSet progress_marker_types,
46                   ModelTypeSet configure_fail_types,
47                   bool should_fail_on_init);
48   ~FakeSyncManager() override;
49 
50   // Returns those types that have been downloaded since the last call to
51   // GetAndResetDownloadedTypes(), or since startup if never called.
52   ModelTypeSet GetAndResetDownloadedTypes();
53 
54   // Returns the types that have most recently received a refresh request.
55   ModelTypeSet GetLastRefreshRequestTypes();
56 
57   // Returns the most recent configuration reason since the last call to
58   // GetAndResetConfigureReason, or since startup if never called.
59   ConfigureReason GetAndResetConfigureReason();
60 
61   // Returns the number of invalidations received for type since startup.
62   int GetInvalidationCount(ModelType type) const;
63 
64   // Block until the sync thread has finished processing any pending messages.
65   void WaitForSyncThread();
66 
67   // SyncManager implementation.
68   // Note: we treat whatever message loop this is called from as the sync
69   // loop for purposes of callbacks.
70   void Init(InitArgs* args) override;
71   ModelTypeSet InitialSyncEndedTypes() override;
72   ModelTypeSet GetEnabledTypes() override;
73   void UpdateCredentials(const SyncCredentials& credentials) override;
74   void InvalidateCredentials() override;
75   void StartSyncingNormally(base::Time last_poll_time) override;
76   void StartConfiguration() override;
77   void ConfigureSyncer(ConfigureReason reason,
78                        ModelTypeSet to_download,
79                        SyncFeatureState sync_feature_state,
80                        base::OnceClosure ready_task) override;
81   void OnIncomingInvalidation(
82       ModelType type,
83       std::unique_ptr<InvalidationInterface> interface) override;
84   void SetInvalidatorEnabled(bool invalidator_enabled) override;
85   void AddObserver(Observer* observer) override;
86   void RemoveObserver(Observer* observer) override;
87   void ShutdownOnSyncThread() override;
88   ModelTypeConnector* GetModelTypeConnector() override;
89   std::unique_ptr<ModelTypeConnector> GetModelTypeConnectorProxy() override;
90   std::string cache_guid() override;
91   std::string birthday() override;
92   std::string bag_of_chips() override;
93   bool HasUnsyncedItemsForTest() override;
94   SyncEncryptionHandler* GetEncryptionHandler() override;
95   std::vector<std::unique_ptr<ProtocolEvent>> GetBufferedProtocolEvents()
96       override;
97   void RefreshTypes(ModelTypeSet types) override;
98   void OnCookieJarChanged(bool account_mismatch, bool empty_jar) override;
99   void UpdateInvalidationClientId(const std::string&) override;
100   void UpdateSingleClientStatus(bool single_client) override;
101 
102  private:
103   scoped_refptr<base::SequencedTaskRunner> sync_task_runner_;
104 
105   base::ObserverList<SyncManager::Observer>::Unchecked observers_;
106 
107   bool should_fail_on_init_;
108   // Faked data state.
109   ModelTypeSet initial_sync_ended_types_;
110   ModelTypeSet progress_marker_types_;
111 
112   // Test specific state.
113   // The types that should fail configuration attempts. These types will not
114   // have their progress markers or initial_sync_ended bits set.
115   ModelTypeSet configure_fail_types_;
116 
117   // The set of types that have been downloaded.
118   ModelTypeSet downloaded_types_;
119 
120   // The types for which a refresh was most recently requested.
121   ModelTypeSet last_refresh_request_types_;
122 
123   // The most recent configure reason.
124   ConfigureReason last_configure_reason_;
125 
126   FakeSyncEncryptionHandler fake_encryption_handler_;
127 
128   FakeModelTypeConnector fake_model_type_connector_;
129 
130   // Number of invalidations received per type since startup.
131   std::map<ModelType, int> num_invalidations_received_;
132 
133   DISALLOW_COPY_AND_ASSIGN(FakeSyncManager);
134 };
135 
136 }  // namespace syncer
137 
138 #endif  // COMPONENTS_SYNC_ENGINE_FAKE_SYNC_MANAGER_H_
139