Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / sync / test / engine / mock_non_blocking_type_processor_core.cc
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/mock_non_blocking_type_processor_core.h"
6
7 #include "base/logging.h"
8
9 namespace syncer {
10
11 MockNonBlockingTypeProcessorCore::MockNonBlockingTypeProcessorCore() {
12 }
13
14 MockNonBlockingTypeProcessorCore::~MockNonBlockingTypeProcessorCore() {
15 }
16
17 void MockNonBlockingTypeProcessorCore::RequestCommits(
18     const CommitRequestDataList& list) {
19   commit_request_lists_.push_back(list);
20 }
21
22 size_t MockNonBlockingTypeProcessorCore::GetNumCommitRequestLists() const {
23   return commit_request_lists_.size();
24 }
25
26 CommitRequestDataList MockNonBlockingTypeProcessorCore::GetNthCommitRequestList(
27     size_t n) const {
28   DCHECK_LT(n, GetNumCommitRequestLists());
29   return commit_request_lists_[n];
30 }
31
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();
39        ++lists_it) {
40     for (CommitRequestDataList::const_iterator it = lists_it->begin();
41          it != lists_it->end();
42          ++it) {
43       if (it->client_tag_hash == tag_hash) {
44         return true;
45       }
46     }
47   }
48
49   return false;
50 }
51
52 CommitRequestData
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();
60        ++lists_it) {
61     for (CommitRequestDataList::const_iterator it = lists_it->begin();
62          it != lists_it->end();
63          ++it) {
64       if (it->client_tag_hash == tag_hash) {
65         return *it;
66       }
67     }
68   }
69
70   NOTREACHED() << "Could not find commit for tag hash " << tag_hash << ".";
71   return CommitRequestData();
72 }
73
74 UpdateResponseData MockNonBlockingTypeProcessorCore::UpdateFromServer(
75     int64 version_offset,
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);
83   }
84
85   UpdateResponseData data;
86   data.id = GenerateId(tag_hash);
87   data.client_tag_hash = tag_hash;
88   data.response_version = version;
89   data.deleted = false;
90   data.specifics = specifics;
91
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();
97
98   return data;
99 }
100
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);
108   }
109
110   UpdateResponseData data;
111   data.id = GenerateId(tag_hash);
112   data.client_tag_hash = tag_hash;
113   data.response_version = version;
114   data.deleted = true;
115
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";
121
122   return data;
123 }
124
125 CommitResponseData MockNonBlockingTypeProcessorCore::SuccessfulCommitResponse(
126     const CommitRequestData& request_data) {
127   const std::string& client_tag_hash = request_data.client_tag_hash;
128
129   CommitResponseData response_data;
130
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;
135   } else {
136     // Otherwise we reuse the ID from the request.
137     response_data.id = GenerateId(client_tag_hash);
138   }
139
140   response_data.client_tag_hash = client_tag_hash;
141   response_data.sequence_number = request_data.sequence_number;
142
143   // Increment the server version on successful commit.
144   int64 version = GetServerVersion(client_tag_hash);
145   version++;
146   SetServerVersion(client_tag_hash, version);
147
148   response_data.response_version = version;
149
150   return response_data;
151 }
152
153 std::string MockNonBlockingTypeProcessorCore::GenerateId(
154     const std::string& tag_hash) {
155   return "FakeId:" + tag_hash;
156 }
157
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()) {
163     return 0;
164   } else {
165     return it->second;
166   }
167 }
168
169 void MockNonBlockingTypeProcessorCore::SetServerVersion(
170     const std::string& tag_hash,
171     int64 version) {
172   server_versions_[tag_hash] = version;
173 }
174
175 }  // namespace syncer