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 "sync/util/time.h"
8
9 using google::protobuf::RepeatedPtrField;
10
11 namespace syncer {
12
SingleTypeMockServer(syncer::ModelType type)13 SingleTypeMockServer::SingleTypeMockServer(syncer::ModelType type)
14 : type_(type), type_root_id_(ModelTypeToRootTag(type)) {
15 }
16
~SingleTypeMockServer()17 SingleTypeMockServer::~SingleTypeMockServer() {
18 }
19
TypeRootUpdate()20 sync_pb::SyncEntity SingleTypeMockServer::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
UpdateFromServer(int64 version_offset,const std::string & tag_hash,const sync_pb::EntitySpecifics & specifics)35 sync_pb::SyncEntity SingleTypeMockServer::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
TombstoneFromServer(int64 version_offset,const std::string & tag_hash)64 sync_pb::SyncEntity SingleTypeMockServer::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
DoSuccessfulCommit(const sync_pb::ClientToServerMessage & message)92 sync_pb::ClientToServerResponse SingleTypeMockServer::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
GetNumCommitMessages() const127 size_t SingleTypeMockServer::GetNumCommitMessages() const {
128 return commit_messages_.size();
129 }
130
GetNthCommitMessage(size_t n) const131 sync_pb::ClientToServerMessage SingleTypeMockServer::GetNthCommitMessage(
132 size_t n) const {
133 DCHECK_LT(n, GetNumCommitMessages());
134 return commit_messages_[n];
135 }
136
HasCommitEntity(const std::string & tag_hash) const137 bool SingleTypeMockServer::HasCommitEntity(const std::string& tag_hash) const {
138 return committed_items_.find(tag_hash) != committed_items_.end();
139 }
140
GetLastCommittedEntity(const std::string & tag_hash) const141 sync_pb::SyncEntity SingleTypeMockServer::GetLastCommittedEntity(
142 const std::string& tag_hash) const {
143 DCHECK(HasCommitEntity(tag_hash));
144 return committed_items_.find(tag_hash)->second;
145 }
146
GetProgress() const147 sync_pb::DataTypeProgressMarker SingleTypeMockServer::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
GetContext() const154 sync_pb::DataTypeContext SingleTypeMockServer::GetContext() const {
155 return sync_pb::DataTypeContext();
156 }
157
GenerateId(const std::string & tag_hash)158 std::string SingleTypeMockServer::GenerateId(const std::string& tag_hash) {
159 return "FakeId:" + tag_hash;
160 }
161
GetServerVersion(const std::string & tag_hash) const162 int64 SingleTypeMockServer::GetServerVersion(
163 const std::string& tag_hash) const {
164 std::map<const std::string, int64>::const_iterator it;
165 it = server_versions_.find(tag_hash);
166 // Server versions do not necessarily start at 1 or 0.
167 if (it == server_versions_.end()) {
168 return 2048;
169 } else {
170 return it->second;
171 }
172 }
173
SetServerVersion(const std::string & tag_hash,int64 version)174 void SingleTypeMockServer::SetServerVersion(const std::string& tag_hash,
175 int64 version) {
176 server_versions_[tag_hash] = version;
177 }
178
179 } // namespace syncer
180