| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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_API_FAKE_MODEL_TYPE_SERVICE_H_ | |
| 6 #define COMPONENTS_SYNC_API_FAKE_MODEL_TYPE_SERVICE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "components/sync/api/entity_data.h" | |
| 13 #include "components/sync/api/metadata_batch.h" | |
| 14 #include "components/sync/api/model_type_service.h" | |
| 15 #include "components/sync/core/non_blocking_sync_common.h" | |
| 16 #include "components/sync/protocol/entity_metadata.pb.h" | |
| 17 #include "components/sync/protocol/model_type_state.pb.h" | |
| 18 | |
| 19 namespace syncer { | |
| 20 | |
| 21 // A basic, functional implementation of ModelTypeService for testing purposes. | |
| 22 // It uses the PREFERENCES type to provide a simple key/value interface, and | |
| 23 // uses its own simple in-memory Store class. | |
| 24 class FakeModelTypeService : public ModelTypeService { | |
| 25 public: | |
| 26 // Generate a client tag with the given key. | |
| 27 static std::string ClientTagFromKey(const std::string& key); | |
| 28 | |
| 29 // Generates the tag hash for a given key. | |
| 30 static std::string TagHashFromKey(const std::string& key); | |
| 31 | |
| 32 // Generates entity specifics for the given key and value. | |
| 33 static sync_pb::EntitySpecifics GenerateSpecifics(const std::string& key, | |
| 34 const std::string& value); | |
| 35 | |
| 36 // Generates an EntityData for the given key and value. | |
| 37 static std::unique_ptr<EntityData> GenerateEntityData( | |
| 38 const std::string& key, | |
| 39 const std::string& value); | |
| 40 | |
| 41 // A basic in-memory storage mechanism for data and metadata. This makes it | |
| 42 // easier to test more complex behaviors involving when entities are written, | |
| 43 // committed, etc. Having a separate class helps keep the main one cleaner. | |
| 44 class Store { | |
| 45 public: | |
| 46 Store(); | |
| 47 virtual ~Store(); | |
| 48 | |
| 49 void PutData(const std::string& key, const EntityData& data); | |
| 50 void PutMetadata(const std::string& key, | |
| 51 const sync_pb::EntityMetadata& metadata); | |
| 52 void RemoveData(const std::string& key); | |
| 53 void RemoveMetadata(const std::string& key); | |
| 54 bool HasData(const std::string& key) const; | |
| 55 bool HasMetadata(const std::string& key) const; | |
| 56 const EntityData& GetData(const std::string& key) const; | |
| 57 const std::string& GetValue(const std::string& key) const; | |
| 58 const sync_pb::EntityMetadata& GetMetadata(const std::string& key) const; | |
| 59 | |
| 60 const std::map<std::string, std::unique_ptr<EntityData>>& all_data() const { | |
| 61 return data_store_; | |
| 62 } | |
| 63 | |
| 64 size_t data_count() const { return data_store_.size(); } | |
| 65 size_t metadata_count() const { return metadata_store_.size(); } | |
| 66 size_t data_change_count() const { return data_change_count_; } | |
| 67 size_t metadata_change_count() const { return metadata_change_count_; } | |
| 68 | |
| 69 const sync_pb::ModelTypeState& model_type_state() const { | |
| 70 return model_type_state_; | |
| 71 } | |
| 72 | |
| 73 void set_model_type_state(const sync_pb::ModelTypeState& model_type_state) { | |
| 74 model_type_state_ = model_type_state; | |
| 75 } | |
| 76 | |
| 77 std::unique_ptr<MetadataBatch> CreateMetadataBatch() const; | |
| 78 void Reset(); | |
| 79 | |
| 80 private: | |
| 81 size_t data_change_count_ = 0; | |
| 82 size_t metadata_change_count_ = 0; | |
| 83 std::map<std::string, std::unique_ptr<EntityData>> data_store_; | |
| 84 std::map<std::string, sync_pb::EntityMetadata> metadata_store_; | |
| 85 sync_pb::ModelTypeState model_type_state_; | |
| 86 }; | |
| 87 | |
| 88 explicit FakeModelTypeService( | |
| 89 const ChangeProcessorFactory& change_processor_factory); | |
| 90 ~FakeModelTypeService() override; | |
| 91 | |
| 92 // Local data modification. Emulates signals from the model thread. | |
| 93 sync_pb::EntitySpecifics WriteItem(const std::string& key, | |
| 94 const std::string& value); | |
| 95 | |
| 96 // Overloaded form to allow passing of custom entity data. | |
| 97 void WriteItem(const std::string& key, | |
| 98 std::unique_ptr<EntityData> entity_data); | |
| 99 | |
| 100 // Local data deletion. | |
| 101 void DeleteItem(const std::string& key); | |
| 102 | |
| 103 // ModelTypeService implementation | |
| 104 std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() override; | |
| 105 SyncError MergeSyncData( | |
| 106 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
| 107 EntityDataMap entity_data_map) override; | |
| 108 SyncError ApplySyncChanges( | |
| 109 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
| 110 EntityChangeList entity_changes) override; | |
| 111 void GetData(StorageKeyList storage_keys, DataCallback callback) override; | |
| 112 void GetAllData(DataCallback callback) override; | |
| 113 std::string GetClientTag(const EntityData& entity_data) override; | |
| 114 std::string GetStorageKey(const EntityData& entity_data) override; | |
| 115 void OnChangeProcessorSet() override; | |
| 116 ConflictResolution ResolveConflict( | |
| 117 const EntityData& local_data, | |
| 118 const EntityData& remote_data) const override; | |
| 119 | |
| 120 // Store a resolution for the next call to ResolveConflict. Note that if this | |
| 121 // is a USE_NEW resolution, the data will only exist for one resolve call. | |
| 122 void SetConflictResolution(ConflictResolution resolution); | |
| 123 | |
| 124 // Sets the error that the next fallible call to the service will generate. | |
| 125 void SetServiceError(SyncError::ErrorType error_type); | |
| 126 | |
| 127 const Store& db() const { return db_; } | |
| 128 | |
| 129 protected: | |
| 130 // Used to verify conditions upon destruction. | |
| 131 virtual void CheckPostConditions(); | |
| 132 | |
| 133 // Contains all of the data and metadata state. | |
| 134 Store db_; | |
| 135 | |
| 136 private: | |
| 137 // Applies |change_list| to the metadata store. | |
| 138 void ApplyMetadataChangeList(std::unique_ptr<MetadataChangeList> change_list); | |
| 139 | |
| 140 // The conflict resolution to use for calls to ResolveConflict. | |
| 141 std::unique_ptr<ConflictResolution> conflict_resolution_; | |
| 142 | |
| 143 // The error to produce on the next service call. | |
| 144 SyncError service_error_; | |
| 145 }; | |
| 146 | |
| 147 } // namespace syncer | |
| 148 | |
| 149 #endif // COMPONENTS_SYNC_API_FAKE_MODEL_TYPE_SERVICE_H_ | |
| OLD | NEW |