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.
5 #include "sync/test/engine/mock_non_blocking_type_processor_core.h"
7 #include "base/logging.h"
11 MockNonBlockingTypeProcessorCore::MockNonBlockingTypeProcessorCore() {
14 MockNonBlockingTypeProcessorCore::~MockNonBlockingTypeProcessorCore() {
17 void MockNonBlockingTypeProcessorCore::RequestCommits(
18 const CommitRequestDataList& list) {
19 commit_request_lists_.push_back(list);
22 size_t MockNonBlockingTypeProcessorCore::GetNumCommitRequestLists() const {
23 return commit_request_lists_.size();
26 CommitRequestDataList MockNonBlockingTypeProcessorCore::GetNthCommitRequestList(
28 DCHECK_LT(n, GetNumCommitRequestLists());
29 return commit_request_lists_[n];
32 bool MockNonBlockingTypeProcessorCore::HasCommitRequestForTagHash(
33 const std::string& tag_hash) const {
34 // Iterate backward through the sets of commit requests to find the most
35 // recent one that applies to the specified tag_hash.
36 for (std::vector<CommitRequestDataList>::const_reverse_iterator lists_it =
37 commit_request_lists_.rbegin();
38 lists_it != commit_request_lists_.rend();
40 for (CommitRequestDataList::const_iterator it = lists_it->begin();
41 it != lists_it->end();
43 if (it->client_tag_hash == tag_hash) {
53 MockNonBlockingTypeProcessorCore::GetLatestCommitRequestForTagHash(
54 const std::string& tag_hash) const {
55 // Iterate backward through the sets of commit requests to find the most
56 // recent one that applies to the specified tag_hash.
57 for (std::vector<CommitRequestDataList>::const_reverse_iterator lists_it =
58 commit_request_lists_.rbegin();
59 lists_it != commit_request_lists_.rend();
61 for (CommitRequestDataList::const_iterator it = lists_it->begin();
62 it != lists_it->end();
64 if (it->client_tag_hash == tag_hash) {
70 NOTREACHED() << "Could not find commit for tag hash " << tag_hash << ".";
71 return CommitRequestData();
74 UpdateResponseData MockNonBlockingTypeProcessorCore::UpdateFromServer(
76 const std::string& tag_hash,
77 const sync_pb::EntitySpecifics& specifics) {
78 // Overwrite the existing server version if this is the new highest version.
79 int64 old_version = GetServerVersion(tag_hash);
80 int64 version = old_version + version_offset;
81 if (version > old_version) {
82 SetServerVersion(tag_hash, version);
85 UpdateResponseData data;
86 data.id = GenerateId(tag_hash);
87 data.client_tag_hash = tag_hash;
88 data.response_version = version;
90 data.specifics = specifics;
92 // These elements should have no effect on behavior, but we set them anyway
93 // so we can test they are properly copied around the system if we want to.
94 data.ctime = base::Time::UnixEpoch() + base::TimeDelta::FromDays(1);
95 data.mtime = data.ctime + base::TimeDelta::FromSeconds(version);
96 data.non_unique_name = specifics.preference().name();
101 UpdateResponseData MockNonBlockingTypeProcessorCore::TombstoneFromServer(
102 int64 version_offset,
103 const std::string& tag_hash) {
104 int64 old_version = GetServerVersion(tag_hash);
105 int64 version = old_version + version_offset;
106 if (version > old_version) {
107 SetServerVersion(tag_hash, version);
110 UpdateResponseData data;
111 data.id = GenerateId(tag_hash);
112 data.client_tag_hash = tag_hash;
113 data.response_version = version;
116 // These elements should have no effect on behavior, but we set them anyway
117 // so we can test they are properly copied around the system if we want to.
118 data.ctime = base::Time::UnixEpoch() + base::TimeDelta::FromDays(1);
119 data.mtime = data.ctime + base::TimeDelta::FromSeconds(version);
120 data.non_unique_name = "Name Non Unique";
125 CommitResponseData MockNonBlockingTypeProcessorCore::SuccessfulCommitResponse(
126 const CommitRequestData& request_data) {
127 const std::string& client_tag_hash = request_data.client_tag_hash;
129 CommitResponseData response_data;
131 if (request_data.base_version == 0) {
132 // Server assigns new ID to newly committed items.
133 DCHECK(request_data.id.empty());
134 response_data.id = request_data.id;
136 // Otherwise we reuse the ID from the request.
137 response_data.id = GenerateId(client_tag_hash);
140 response_data.client_tag_hash = client_tag_hash;
141 response_data.sequence_number = request_data.sequence_number;
143 // Increment the server version on successful commit.
144 int64 version = GetServerVersion(client_tag_hash);
146 SetServerVersion(client_tag_hash, version);
148 response_data.response_version = version;
150 return response_data;
153 std::string MockNonBlockingTypeProcessorCore::GenerateId(
154 const std::string& tag_hash) {
155 return "FakeId:" + tag_hash;
158 int64 MockNonBlockingTypeProcessorCore::GetServerVersion(
159 const std::string& tag_hash) {
160 std::map<const std::string, int64>::const_iterator it;
161 it = server_versions_.find(tag_hash);
162 if (it == server_versions_.end()) {
169 void MockNonBlockingTypeProcessorCore::SetServerVersion(
170 const std::string& tag_hash,
172 server_versions_[tag_hash] = version;
175 } // namespace syncer