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_commit_queue.h"
7 #include "base/logging.h"
11 MockCommitQueue::MockCommitQueue() {
14 MockCommitQueue::~MockCommitQueue() {
17 void MockCommitQueue::EnqueueForCommit(
18 const CommitRequestDataList
& list
) {
19 commit_request_lists_
.push_back(list
);
22 size_t MockCommitQueue::GetNumCommitRequestLists() const {
23 return commit_request_lists_
.size();
26 CommitRequestDataList
MockCommitQueue::GetNthCommitRequestList(
28 DCHECK_LT(n
, GetNumCommitRequestLists());
29 return commit_request_lists_
[n
];
32 bool MockCommitQueue::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(); ++lists_it
) {
39 for (CommitRequestDataList::const_iterator it
= lists_it
->begin();
40 it
!= lists_it
->end(); ++it
) {
41 if (it
->client_tag_hash
== tag_hash
) {
50 CommitRequestData
MockCommitQueue::GetLatestCommitRequestForTagHash(
51 const std::string
& tag_hash
) const {
52 // Iterate backward through the sets of commit requests to find the most
53 // recent one that applies to the specified tag_hash.
54 for (std::vector
<CommitRequestDataList
>::const_reverse_iterator lists_it
=
55 commit_request_lists_
.rbegin();
56 lists_it
!= commit_request_lists_
.rend(); ++lists_it
) {
57 for (CommitRequestDataList::const_iterator it
= lists_it
->begin();
58 it
!= lists_it
->end(); ++it
) {
59 if (it
->client_tag_hash
== tag_hash
) {
65 NOTREACHED() << "Could not find commit for tag hash " << tag_hash
<< ".";
66 return CommitRequestData();
69 UpdateResponseData
MockCommitQueue::UpdateFromServer(
71 const std::string
& tag_hash
,
72 const sync_pb::EntitySpecifics
& specifics
) {
73 // Overwrite the existing server version if this is the new highest version.
74 int64 old_version
= GetServerVersion(tag_hash
);
75 int64 version
= old_version
+ version_offset
;
76 if (version
> old_version
) {
77 SetServerVersion(tag_hash
, version
);
80 UpdateResponseData data
;
81 data
.id
= GenerateId(tag_hash
);
82 data
.client_tag_hash
= tag_hash
;
83 data
.response_version
= version
;
85 data
.specifics
= specifics
;
87 // These elements should have no effect on behavior, but we set them anyway
88 // so we can test they are properly copied around the system if we want to.
89 data
.ctime
= base::Time::UnixEpoch() + base::TimeDelta::FromDays(1);
90 data
.mtime
= data
.ctime
+ base::TimeDelta::FromSeconds(version
);
91 data
.non_unique_name
= specifics
.preference().name();
93 data
.encryption_key_name
= server_encryption_key_name_
;
98 UpdateResponseData
MockCommitQueue::TombstoneFromServer(
100 const std::string
& tag_hash
) {
101 int64 old_version
= GetServerVersion(tag_hash
);
102 int64 version
= old_version
+ version_offset
;
103 if (version
> old_version
) {
104 SetServerVersion(tag_hash
, version
);
107 UpdateResponseData data
;
108 data
.id
= GenerateId(tag_hash
);
109 data
.client_tag_hash
= tag_hash
;
110 data
.response_version
= version
;
113 // These elements should have no effect on behavior, but we set them anyway
114 // so we can test they are properly copied around the system if we want to.
115 data
.ctime
= base::Time::UnixEpoch() + base::TimeDelta::FromDays(1);
116 data
.mtime
= data
.ctime
+ base::TimeDelta::FromSeconds(version
);
117 data
.non_unique_name
= "Name Non Unique";
119 data
.encryption_key_name
= server_encryption_key_name_
;
124 CommitResponseData
MockCommitQueue::SuccessfulCommitResponse(
125 const CommitRequestData
& request_data
) {
126 const std::string
& client_tag_hash
= request_data
.client_tag_hash
;
128 CommitResponseData response_data
;
130 if (request_data
.base_version
== 0) {
131 // Server assigns new ID to newly committed items.
132 DCHECK(request_data
.id
.empty());
133 response_data
.id
= request_data
.id
;
135 // Otherwise we reuse the ID from the request.
136 response_data
.id
= GenerateId(client_tag_hash
);
139 response_data
.client_tag_hash
= client_tag_hash
;
140 response_data
.sequence_number
= request_data
.sequence_number
;
142 // Increment the server version on successful commit.
143 int64 version
= GetServerVersion(client_tag_hash
);
145 SetServerVersion(client_tag_hash
, version
);
147 response_data
.response_version
= version
;
149 return response_data
;
152 void MockCommitQueue::SetServerEncryptionKey(
153 const std::string
& key_name
) {
154 server_encryption_key_name_
= key_name
;
157 std::string
MockCommitQueue::GenerateId(const std::string
& tag_hash
) {
158 return "FakeId:" + tag_hash
;
161 int64
MockCommitQueue::GetServerVersion(const std::string
& tag_hash
) {
162 std::map
<const std::string
, int64
>::const_iterator it
;
163 it
= server_versions_
.find(tag_hash
);
164 if (it
== server_versions_
.end()) {
171 void MockCommitQueue::SetServerVersion(const std::string
& tag_hash
,
173 server_versions_
[tag_hash
] = version
;
176 } // namespace syncer