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