| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "sync/test/engine/single_type_mock_server.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "sync/util/time.h" | |
| 11 | |
| 12 using google::protobuf::RepeatedPtrField; | |
| 13 | |
| 14 namespace syncer { | |
| 15 | |
| 16 SingleTypeMockServer::SingleTypeMockServer(syncer::ModelType type) | |
| 17 : type_(type), type_root_id_(ModelTypeToRootTag(type)) { | |
| 18 } | |
| 19 | |
| 20 SingleTypeMockServer::~SingleTypeMockServer() { | |
| 21 } | |
| 22 | |
| 23 sync_pb::SyncEntity SingleTypeMockServer::TypeRootUpdate() { | |
| 24 sync_pb::SyncEntity entity; | |
| 25 | |
| 26 entity.set_id_string(type_root_id_); | |
| 27 entity.set_parent_id_string("r"); | |
| 28 entity.set_version(1000); | |
| 29 entity.set_ctime(TimeToProtoTime(base::Time::UnixEpoch())); | |
| 30 entity.set_mtime(TimeToProtoTime(base::Time::UnixEpoch())); | |
| 31 entity.set_server_defined_unique_tag(ModelTypeToRootTag(type_)); | |
| 32 entity.set_deleted(false); | |
| 33 AddDefaultFieldValue(type_, entity.mutable_specifics()); | |
| 34 | |
| 35 return entity; | |
| 36 } | |
| 37 | |
| 38 sync_pb::SyncEntity SingleTypeMockServer::UpdateFromServer( | |
| 39 int64_t version_offset, | |
| 40 const std::string& tag_hash, | |
| 41 const sync_pb::EntitySpecifics& specifics) { | |
| 42 int64_t old_version = GetServerVersion(tag_hash); | |
| 43 int64_t version = old_version + version_offset; | |
| 44 if (version > old_version) { | |
| 45 SetServerVersion(tag_hash, version); | |
| 46 } | |
| 47 | |
| 48 sync_pb::SyncEntity entity; | |
| 49 | |
| 50 entity.set_id_string(GenerateId(tag_hash)); | |
| 51 entity.set_parent_id_string(type_root_id_); | |
| 52 entity.set_version(version); | |
| 53 entity.set_client_defined_unique_tag(tag_hash); | |
| 54 entity.set_deleted(false); | |
| 55 entity.mutable_specifics()->CopyFrom(specifics); | |
| 56 | |
| 57 // Unimportant fields, set for completeness only. | |
| 58 base::Time ctime = base::Time::UnixEpoch() + base::TimeDelta::FromDays(1); | |
| 59 base::Time mtime = ctime + base::TimeDelta::FromSeconds(version); | |
| 60 entity.set_ctime(TimeToProtoTime(ctime)); | |
| 61 entity.set_mtime(TimeToProtoTime(mtime)); | |
| 62 entity.set_name("Name: " + tag_hash); | |
| 63 | |
| 64 return entity; | |
| 65 } | |
| 66 | |
| 67 sync_pb::SyncEntity SingleTypeMockServer::TombstoneFromServer( | |
| 68 int64_t version_offset, | |
| 69 const std::string& tag_hash) { | |
| 70 int64_t old_version = GetServerVersion(tag_hash); | |
| 71 int64_t version = old_version + version_offset; | |
| 72 if (version > old_version) { | |
| 73 SetServerVersion(tag_hash, version); | |
| 74 } | |
| 75 | |
| 76 sync_pb::SyncEntity entity; | |
| 77 | |
| 78 entity.set_id_string(GenerateId(tag_hash)); | |
| 79 entity.set_parent_id_string(type_root_id_); | |
| 80 entity.set_version(version); | |
| 81 entity.set_client_defined_unique_tag(tag_hash); | |
| 82 entity.set_deleted(false); | |
| 83 AddDefaultFieldValue(type_, entity.mutable_specifics()); | |
| 84 | |
| 85 // Unimportant fields, set for completeness only. | |
| 86 base::Time ctime = base::Time::UnixEpoch() + base::TimeDelta::FromDays(1); | |
| 87 base::Time mtime = ctime + base::TimeDelta::FromSeconds(version); | |
| 88 entity.set_ctime(TimeToProtoTime(ctime)); | |
| 89 entity.set_mtime(TimeToProtoTime(mtime)); | |
| 90 entity.set_name("Tombstone"); | |
| 91 | |
| 92 return entity; | |
| 93 } | |
| 94 | |
| 95 sync_pb::ClientToServerResponse SingleTypeMockServer::DoSuccessfulCommit( | |
| 96 const sync_pb::ClientToServerMessage& message) { | |
| 97 commit_messages_.push_back(message); | |
| 98 | |
| 99 sync_pb::ClientToServerResponse response; | |
| 100 sync_pb::CommitResponse* commit_response = response.mutable_commit(); | |
| 101 | |
| 102 const RepeatedPtrField<sync_pb::SyncEntity>& entries = | |
| 103 message.commit().entries(); | |
| 104 for (RepeatedPtrField<sync_pb::SyncEntity>::const_iterator it = | |
| 105 entries.begin(); | |
| 106 it != entries.end(); | |
| 107 ++it) { | |
| 108 const std::string tag_hash = it->client_defined_unique_tag(); | |
| 109 | |
| 110 committed_items_[tag_hash] = *it; | |
| 111 | |
| 112 // Every commit increments the version number. | |
| 113 int64_t version = GetServerVersion(tag_hash); | |
| 114 version++; | |
| 115 SetServerVersion(tag_hash, version); | |
| 116 | |
| 117 sync_pb::CommitResponse_EntryResponse* entryresponse = | |
| 118 commit_response->add_entryresponse(); | |
| 119 entryresponse->set_response_type(sync_pb::CommitResponse::SUCCESS); | |
| 120 entryresponse->set_id_string(GenerateId(tag_hash)); | |
| 121 entryresponse->set_parent_id_string(it->parent_id_string()); | |
| 122 entryresponse->set_version(version); | |
| 123 entryresponse->set_name(it->name()); | |
| 124 entryresponse->set_mtime(it->mtime()); | |
| 125 } | |
| 126 | |
| 127 return response; | |
| 128 } | |
| 129 | |
| 130 size_t SingleTypeMockServer::GetNumCommitMessages() const { | |
| 131 return commit_messages_.size(); | |
| 132 } | |
| 133 | |
| 134 sync_pb::ClientToServerMessage SingleTypeMockServer::GetNthCommitMessage( | |
| 135 size_t n) const { | |
| 136 DCHECK_LT(n, GetNumCommitMessages()); | |
| 137 return commit_messages_[n]; | |
| 138 } | |
| 139 | |
| 140 bool SingleTypeMockServer::HasCommitEntity(const std::string& tag_hash) const { | |
| 141 return committed_items_.find(tag_hash) != committed_items_.end(); | |
| 142 } | |
| 143 | |
| 144 sync_pb::SyncEntity SingleTypeMockServer::GetLastCommittedEntity( | |
| 145 const std::string& tag_hash) const { | |
| 146 DCHECK(HasCommitEntity(tag_hash)); | |
| 147 return committed_items_.find(tag_hash)->second; | |
| 148 } | |
| 149 | |
| 150 sync_pb::DataTypeProgressMarker SingleTypeMockServer::GetProgress() const { | |
| 151 sync_pb::DataTypeProgressMarker progress; | |
| 152 progress.set_data_type_id(type_); | |
| 153 progress.set_token("non_null_progress_token"); | |
| 154 return progress; | |
| 155 } | |
| 156 | |
| 157 sync_pb::DataTypeContext SingleTypeMockServer::GetContext() const { | |
| 158 return sync_pb::DataTypeContext(); | |
| 159 } | |
| 160 | |
| 161 std::string SingleTypeMockServer::GenerateId(const std::string& tag_hash) { | |
| 162 return "FakeId:" + tag_hash; | |
| 163 } | |
| 164 | |
| 165 int64_t SingleTypeMockServer::GetServerVersion( | |
| 166 const std::string& tag_hash) const { | |
| 167 std::map<const std::string, int64_t>::const_iterator it; | |
| 168 it = server_versions_.find(tag_hash); | |
| 169 // Server versions do not necessarily start at 1 or 0. | |
| 170 if (it == server_versions_.end()) { | |
| 171 return 2048; | |
| 172 } else { | |
| 173 return it->second; | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 void SingleTypeMockServer::SetServerVersion(const std::string& tag_hash, | |
| 178 int64_t version) { | |
| 179 server_versions_[tag_hash] = version; | |
| 180 } | |
| 181 | |
| 182 } // namespace syncer | |
| OLD | NEW |