| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/test/engine/mock_model_type_sync_worker.h" | 5 #include "sync/test/engine/mock_commit_queue.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace syncer_v2 { | 9 namespace syncer_v2 { |
| 10 | 10 |
| 11 MockModelTypeSyncWorker::MockModelTypeSyncWorker() { | 11 MockCommitQueue::MockCommitQueue() { |
| 12 } | 12 } |
| 13 | 13 |
| 14 MockModelTypeSyncWorker::~MockModelTypeSyncWorker() { | 14 MockCommitQueue::~MockCommitQueue() { |
| 15 } | 15 } |
| 16 | 16 |
| 17 void MockModelTypeSyncWorker::EnqueueForCommit( | 17 void MockCommitQueue::EnqueueForCommit( |
| 18 const CommitRequestDataList& list) { | 18 const CommitRequestDataList& list) { |
| 19 commit_request_lists_.push_back(list); | 19 commit_request_lists_.push_back(list); |
| 20 } | 20 } |
| 21 | 21 |
| 22 size_t MockModelTypeSyncWorker::GetNumCommitRequestLists() const { | 22 size_t MockCommitQueue::GetNumCommitRequestLists() const { |
| 23 return commit_request_lists_.size(); | 23 return commit_request_lists_.size(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 CommitRequestDataList MockModelTypeSyncWorker::GetNthCommitRequestList( | 26 CommitRequestDataList MockCommitQueue::GetNthCommitRequestList( |
| 27 size_t n) const { | 27 size_t n) const { |
| 28 DCHECK_LT(n, GetNumCommitRequestLists()); | 28 DCHECK_LT(n, GetNumCommitRequestLists()); |
| 29 return commit_request_lists_[n]; | 29 return commit_request_lists_[n]; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool MockModelTypeSyncWorker::HasCommitRequestForTagHash( | 32 bool MockCommitQueue::HasCommitRequestForTagHash( |
| 33 const std::string& tag_hash) const { | 33 const std::string& tag_hash) const { |
| 34 // Iterate backward through the sets of commit requests to find the most | 34 // Iterate backward through the sets of commit requests to find the most |
| 35 // recent one that applies to the specified tag_hash. | 35 // recent one that applies to the specified tag_hash. |
| 36 for (std::vector<CommitRequestDataList>::const_reverse_iterator lists_it = | 36 for (std::vector<CommitRequestDataList>::const_reverse_iterator lists_it = |
| 37 commit_request_lists_.rbegin(); | 37 commit_request_lists_.rbegin(); |
| 38 lists_it != commit_request_lists_.rend(); ++lists_it) { | 38 lists_it != commit_request_lists_.rend(); ++lists_it) { |
| 39 for (CommitRequestDataList::const_iterator it = lists_it->begin(); | 39 for (CommitRequestDataList::const_iterator it = lists_it->begin(); |
| 40 it != lists_it->end(); ++it) { | 40 it != lists_it->end(); ++it) { |
| 41 if (it->client_tag_hash == tag_hash) { | 41 if (it->client_tag_hash == tag_hash) { |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 | 49 |
| 50 CommitRequestData MockModelTypeSyncWorker::GetLatestCommitRequestForTagHash( | 50 CommitRequestData MockCommitQueue::GetLatestCommitRequestForTagHash( |
| 51 const std::string& tag_hash) const { | 51 const std::string& tag_hash) const { |
| 52 // Iterate backward through the sets of commit requests to find the most | 52 // Iterate backward through the sets of commit requests to find the most |
| 53 // recent one that applies to the specified tag_hash. | 53 // recent one that applies to the specified tag_hash. |
| 54 for (std::vector<CommitRequestDataList>::const_reverse_iterator lists_it = | 54 for (std::vector<CommitRequestDataList>::const_reverse_iterator lists_it = |
| 55 commit_request_lists_.rbegin(); | 55 commit_request_lists_.rbegin(); |
| 56 lists_it != commit_request_lists_.rend(); ++lists_it) { | 56 lists_it != commit_request_lists_.rend(); ++lists_it) { |
| 57 for (CommitRequestDataList::const_iterator it = lists_it->begin(); | 57 for (CommitRequestDataList::const_iterator it = lists_it->begin(); |
| 58 it != lists_it->end(); ++it) { | 58 it != lists_it->end(); ++it) { |
| 59 if (it->client_tag_hash == tag_hash) { | 59 if (it->client_tag_hash == tag_hash) { |
| 60 return *it; | 60 return *it; |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 NOTREACHED() << "Could not find commit for tag hash " << tag_hash << "."; | 65 NOTREACHED() << "Could not find commit for tag hash " << tag_hash << "."; |
| 66 return CommitRequestData(); | 66 return CommitRequestData(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 UpdateResponseData MockModelTypeSyncWorker::UpdateFromServer( | 69 UpdateResponseData MockCommitQueue::UpdateFromServer( |
| 70 int64 version_offset, | 70 int64 version_offset, |
| 71 const std::string& tag_hash, | 71 const std::string& tag_hash, |
| 72 const sync_pb::EntitySpecifics& specifics) { | 72 const sync_pb::EntitySpecifics& specifics) { |
| 73 // Overwrite the existing server version if this is the new highest version. | 73 // Overwrite the existing server version if this is the new highest version. |
| 74 int64 old_version = GetServerVersion(tag_hash); | 74 int64 old_version = GetServerVersion(tag_hash); |
| 75 int64 version = old_version + version_offset; | 75 int64 version = old_version + version_offset; |
| 76 if (version > old_version) { | 76 if (version > old_version) { |
| 77 SetServerVersion(tag_hash, version); | 77 SetServerVersion(tag_hash, version); |
| 78 } | 78 } |
| 79 | 79 |
| 80 UpdateResponseData data; | 80 UpdateResponseData data; |
| 81 data.id = GenerateId(tag_hash); | 81 data.id = GenerateId(tag_hash); |
| 82 data.client_tag_hash = tag_hash; | 82 data.client_tag_hash = tag_hash; |
| 83 data.response_version = version; | 83 data.response_version = version; |
| 84 data.deleted = false; | 84 data.deleted = false; |
| 85 data.specifics = specifics; | 85 data.specifics = specifics; |
| 86 | 86 |
| 87 // These elements should have no effect on behavior, but we set them anyway | 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. | 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); | 89 data.ctime = base::Time::UnixEpoch() + base::TimeDelta::FromDays(1); |
| 90 data.mtime = data.ctime + base::TimeDelta::FromSeconds(version); | 90 data.mtime = data.ctime + base::TimeDelta::FromSeconds(version); |
| 91 data.non_unique_name = specifics.preference().name(); | 91 data.non_unique_name = specifics.preference().name(); |
| 92 | 92 |
| 93 data.encryption_key_name = server_encryption_key_name_; | 93 data.encryption_key_name = server_encryption_key_name_; |
| 94 | 94 |
| 95 return data; | 95 return data; |
| 96 } | 96 } |
| 97 | 97 |
| 98 UpdateResponseData MockModelTypeSyncWorker::TombstoneFromServer( | 98 UpdateResponseData MockCommitQueue::TombstoneFromServer( |
| 99 int64 version_offset, | 99 int64 version_offset, |
| 100 const std::string& tag_hash) { | 100 const std::string& tag_hash) { |
| 101 int64 old_version = GetServerVersion(tag_hash); | 101 int64 old_version = GetServerVersion(tag_hash); |
| 102 int64 version = old_version + version_offset; | 102 int64 version = old_version + version_offset; |
| 103 if (version > old_version) { | 103 if (version > old_version) { |
| 104 SetServerVersion(tag_hash, version); | 104 SetServerVersion(tag_hash, version); |
| 105 } | 105 } |
| 106 | 106 |
| 107 UpdateResponseData data; | 107 UpdateResponseData data; |
| 108 data.id = GenerateId(tag_hash); | 108 data.id = GenerateId(tag_hash); |
| 109 data.client_tag_hash = tag_hash; | 109 data.client_tag_hash = tag_hash; |
| 110 data.response_version = version; | 110 data.response_version = version; |
| 111 data.deleted = true; | 111 data.deleted = true; |
| 112 | 112 |
| 113 // These elements should have no effect on behavior, but we set them anyway | 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. | 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); | 115 data.ctime = base::Time::UnixEpoch() + base::TimeDelta::FromDays(1); |
| 116 data.mtime = data.ctime + base::TimeDelta::FromSeconds(version); | 116 data.mtime = data.ctime + base::TimeDelta::FromSeconds(version); |
| 117 data.non_unique_name = "Name Non Unique"; | 117 data.non_unique_name = "Name Non Unique"; |
| 118 | 118 |
| 119 data.encryption_key_name = server_encryption_key_name_; | 119 data.encryption_key_name = server_encryption_key_name_; |
| 120 | 120 |
| 121 return data; | 121 return data; |
| 122 } | 122 } |
| 123 | 123 |
| 124 CommitResponseData MockModelTypeSyncWorker::SuccessfulCommitResponse( | 124 CommitResponseData MockCommitQueue::SuccessfulCommitResponse( |
| 125 const CommitRequestData& request_data) { | 125 const CommitRequestData& request_data) { |
| 126 const std::string& client_tag_hash = request_data.client_tag_hash; | 126 const std::string& client_tag_hash = request_data.client_tag_hash; |
| 127 | 127 |
| 128 CommitResponseData response_data; | 128 CommitResponseData response_data; |
| 129 | 129 |
| 130 if (request_data.base_version == 0) { | 130 if (request_data.base_version == 0) { |
| 131 // Server assigns new ID to newly committed items. | 131 // Server assigns new ID to newly committed items. |
| 132 DCHECK(request_data.id.empty()); | 132 DCHECK(request_data.id.empty()); |
| 133 response_data.id = request_data.id; | 133 response_data.id = request_data.id; |
| 134 } else { | 134 } else { |
| 135 // Otherwise we reuse the ID from the request. | 135 // Otherwise we reuse the ID from the request. |
| 136 response_data.id = GenerateId(client_tag_hash); | 136 response_data.id = GenerateId(client_tag_hash); |
| 137 } | 137 } |
| 138 | 138 |
| 139 response_data.client_tag_hash = client_tag_hash; | 139 response_data.client_tag_hash = client_tag_hash; |
| 140 response_data.sequence_number = request_data.sequence_number; | 140 response_data.sequence_number = request_data.sequence_number; |
| 141 | 141 |
| 142 // Increment the server version on successful commit. | 142 // Increment the server version on successful commit. |
| 143 int64 version = GetServerVersion(client_tag_hash); | 143 int64 version = GetServerVersion(client_tag_hash); |
| 144 version++; | 144 version++; |
| 145 SetServerVersion(client_tag_hash, version); | 145 SetServerVersion(client_tag_hash, version); |
| 146 | 146 |
| 147 response_data.response_version = version; | 147 response_data.response_version = version; |
| 148 | 148 |
| 149 return response_data; | 149 return response_data; |
| 150 } | 150 } |
| 151 | 151 |
| 152 void MockModelTypeSyncWorker::SetServerEncryptionKey( | 152 void MockCommitQueue::SetServerEncryptionKey( |
| 153 const std::string& key_name) { | 153 const std::string& key_name) { |
| 154 server_encryption_key_name_ = key_name; | 154 server_encryption_key_name_ = key_name; |
| 155 } | 155 } |
| 156 | 156 |
| 157 std::string MockModelTypeSyncWorker::GenerateId(const std::string& tag_hash) { | 157 std::string MockCommitQueue::GenerateId(const std::string& tag_hash) { |
| 158 return "FakeId:" + tag_hash; | 158 return "FakeId:" + tag_hash; |
| 159 } | 159 } |
| 160 | 160 |
| 161 int64 MockModelTypeSyncWorker::GetServerVersion(const std::string& tag_hash) { | 161 int64 MockCommitQueue::GetServerVersion(const std::string& tag_hash) { |
| 162 std::map<const std::string, int64>::const_iterator it; | 162 std::map<const std::string, int64>::const_iterator it; |
| 163 it = server_versions_.find(tag_hash); | 163 it = server_versions_.find(tag_hash); |
| 164 if (it == server_versions_.end()) { | 164 if (it == server_versions_.end()) { |
| 165 return 0; | 165 return 0; |
| 166 } else { | 166 } else { |
| 167 return it->second; | 167 return it->second; |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 void MockModelTypeSyncWorker::SetServerVersion(const std::string& tag_hash, | 171 void MockCommitQueue::SetServerVersion(const std::string& tag_hash, |
| 172 int64 version) { | 172 int64 version) { |
| 173 server_versions_[tag_hash] = version; | 173 server_versions_[tag_hash] = version; |
| 174 } | 174 } |
| 175 | 175 |
| 176 } // namespace syncer | 176 } // namespace syncer |
| OLD | NEW |