| 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 #ifndef SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_ | 5 #ifndef SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_ |
| 6 #define SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_ | 6 #define SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_ |
| 7 | 7 |
| 8 #include "sync/api/attachments/attachment_store.h" | 8 #include "sync/api/attachments/attachment_store.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "sync/api/attachments/attachment.h" | 16 #include "sync/api/attachments/attachment.h" |
| 17 #include "sync/internal_api/public/attachments/attachment_util.h" |
| 17 #include "sync/protocol/sync.pb.h" | 18 #include "sync/protocol/sync.pb.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 // AttachmentStoreTest defines tests for AttachmentStore. To instantiate these | 21 // AttachmentStoreTest defines tests for AttachmentStore. To instantiate these |
| 21 // tests for a particular implementation you need to: | 22 // tests for a particular implementation you need to: |
| 22 // - Include this file in test. | 23 // - Include this file in test. |
| 23 // - Create factory class for attachment store that implements factory method. | 24 // - Create factory class for attachment store that implements factory method. |
| 24 // - add INSTANTIATE_TYPED_TEST_CASE_P statement. | 25 // - add INSTANTIATE_TYPED_TEST_CASE_P statement. |
| 25 // Here is how to do it for MyAttachmentStore: | 26 // Here is how to do it for MyAttachmentStore: |
| 26 // | 27 // |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); | 109 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); |
| 109 } | 110 } |
| 110 }; | 111 }; |
| 111 | 112 |
| 112 TYPED_TEST_CASE_P(AttachmentStoreTest); | 113 TYPED_TEST_CASE_P(AttachmentStoreTest); |
| 113 | 114 |
| 114 // Verify that we do not overwrite existing attachments and that we do not treat | 115 // Verify that we do not overwrite existing attachments and that we do not treat |
| 115 // it as an error. | 116 // it as an error. |
| 116 TYPED_TEST_P(AttachmentStoreTest, Write_NoOverwriteNoError) { | 117 TYPED_TEST_P(AttachmentStoreTest, Write_NoOverwriteNoError) { |
| 117 // Create two attachments with the same id but different data. | 118 // Create two attachments with the same id but different data. |
| 118 Attachment attachment1 = Attachment::Create(this->some_data1); | 119 Attachment attachment1 = Attachment::CreateNew(this->some_data1); |
| 120 uint32_t crc = ComputeCrc32c(this->some_data2); |
| 119 Attachment attachment2 = | 121 Attachment attachment2 = |
| 120 Attachment::CreateWithId(attachment1.GetId(), this->some_data2); | 122 Attachment::RestoreExisting(attachment1.GetId(), this->some_data2, crc); |
| 121 | 123 |
| 122 // Write the first one. | 124 // Write the first one. |
| 123 AttachmentList some_attachments; | 125 AttachmentList some_attachments; |
| 124 some_attachments.push_back(attachment1); | 126 some_attachments.push_back(attachment1); |
| 125 this->store->Write(some_attachments, this->write_callback); | 127 this->store->Write(some_attachments, this->write_callback); |
| 126 this->ClearAndPumpLoop(); | 128 this->ClearAndPumpLoop(); |
| 127 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | 129 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); |
| 128 | 130 |
| 129 // Write the second one. | 131 // Write the second one. |
| 130 some_attachments.clear(); | 132 some_attachments.clear(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 142 EXPECT_EQ(this->attachments->size(), 1U); | 144 EXPECT_EQ(this->attachments->size(), 1U); |
| 143 EXPECT_EQ(this->failed_attachment_ids->size(), 0U); | 145 EXPECT_EQ(this->failed_attachment_ids->size(), 0U); |
| 144 AttachmentMap::const_iterator a1 = | 146 AttachmentMap::const_iterator a1 = |
| 145 this->attachments->find(attachment1.GetId()); | 147 this->attachments->find(attachment1.GetId()); |
| 146 EXPECT_TRUE(a1 != this->attachments->end()); | 148 EXPECT_TRUE(a1 != this->attachments->end()); |
| 147 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 149 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
| 148 } | 150 } |
| 149 | 151 |
| 150 // Verify that we can write some attachments and read them back. | 152 // Verify that we can write some attachments and read them back. |
| 151 TYPED_TEST_P(AttachmentStoreTest, Write_RoundTrip) { | 153 TYPED_TEST_P(AttachmentStoreTest, Write_RoundTrip) { |
| 152 Attachment attachment1 = Attachment::Create(this->some_data1); | 154 Attachment attachment1 = Attachment::CreateNew(this->some_data1); |
| 153 Attachment attachment2 = Attachment::Create(this->some_data2); | 155 Attachment attachment2 = Attachment::CreateNew(this->some_data2); |
| 154 AttachmentList some_attachments; | 156 AttachmentList some_attachments; |
| 155 some_attachments.push_back(attachment1); | 157 some_attachments.push_back(attachment1); |
| 156 some_attachments.push_back(attachment2); | 158 some_attachments.push_back(attachment2); |
| 157 | 159 |
| 158 this->store->Write(some_attachments, this->write_callback); | 160 this->store->Write(some_attachments, this->write_callback); |
| 159 this->ClearAndPumpLoop(); | 161 this->ClearAndPumpLoop(); |
| 160 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | 162 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); |
| 161 | 163 |
| 162 AttachmentIdList some_attachment_ids; | 164 AttachmentIdList some_attachment_ids; |
| 163 some_attachment_ids.push_back(attachment1.GetId()); | 165 some_attachment_ids.push_back(attachment1.GetId()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 174 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 176 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
| 175 | 177 |
| 176 AttachmentMap::const_iterator a2 = | 178 AttachmentMap::const_iterator a2 = |
| 177 this->attachments->find(attachment2.GetId()); | 179 this->attachments->find(attachment2.GetId()); |
| 178 EXPECT_TRUE(a2 != this->attachments->end()); | 180 EXPECT_TRUE(a2 != this->attachments->end()); |
| 179 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); | 181 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); |
| 180 } | 182 } |
| 181 | 183 |
| 182 // Try to read two attachments when only one exists. | 184 // Try to read two attachments when only one exists. |
| 183 TYPED_TEST_P(AttachmentStoreTest, Read_OneNotFound) { | 185 TYPED_TEST_P(AttachmentStoreTest, Read_OneNotFound) { |
| 184 Attachment attachment1 = Attachment::Create(this->some_data1); | 186 Attachment attachment1 = Attachment::CreateNew(this->some_data1); |
| 185 Attachment attachment2 = Attachment::Create(this->some_data2); | 187 Attachment attachment2 = Attachment::CreateNew(this->some_data2); |
| 186 | 188 |
| 187 AttachmentList some_attachments; | 189 AttachmentList some_attachments; |
| 188 // Write attachment1 only. | 190 // Write attachment1 only. |
| 189 some_attachments.push_back(attachment1); | 191 some_attachments.push_back(attachment1); |
| 190 this->store->Write(some_attachments, this->write_callback); | 192 this->store->Write(some_attachments, this->write_callback); |
| 191 this->ClearAndPumpLoop(); | 193 this->ClearAndPumpLoop(); |
| 192 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | 194 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); |
| 193 | 195 |
| 194 // Try to read both attachment1 and attachment2. | 196 // Try to read both attachment1 and attachment2. |
| 195 AttachmentIdList ids; | 197 AttachmentIdList ids; |
| 196 ids.push_back(attachment1.GetId()); | 198 ids.push_back(attachment1.GetId()); |
| 197 ids.push_back(attachment2.GetId()); | 199 ids.push_back(attachment2.GetId()); |
| 198 this->store->Read(ids, this->read_callback); | 200 this->store->Read(ids, this->read_callback); |
| 199 this->ClearAndPumpLoop(); | 201 this->ClearAndPumpLoop(); |
| 200 | 202 |
| 201 // See that only attachment1 was read. | 203 // See that only attachment1 was read. |
| 202 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); | 204 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); |
| 203 EXPECT_EQ(this->attachments->size(), 1U); | 205 EXPECT_EQ(this->attachments->size(), 1U); |
| 204 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); | 206 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); |
| 205 } | 207 } |
| 206 | 208 |
| 207 // Try to drop two attachments when only one exists. Verify that no error occurs | 209 // Try to drop two attachments when only one exists. Verify that no error occurs |
| 208 // and that the existing attachment was dropped. | 210 // and that the existing attachment was dropped. |
| 209 TYPED_TEST_P(AttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { | 211 TYPED_TEST_P(AttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { |
| 210 // First, create two attachments. | 212 // First, create two attachments. |
| 211 Attachment attachment1 = Attachment::Create(this->some_data1); | 213 Attachment attachment1 = Attachment::CreateNew(this->some_data1); |
| 212 Attachment attachment2 = Attachment::Create(this->some_data2); | 214 Attachment attachment2 = Attachment::CreateNew(this->some_data2); |
| 213 AttachmentList some_attachments; | 215 AttachmentList some_attachments; |
| 214 some_attachments.push_back(attachment1); | 216 some_attachments.push_back(attachment1); |
| 215 some_attachments.push_back(attachment2); | 217 some_attachments.push_back(attachment2); |
| 216 this->store->Write(some_attachments, this->write_callback); | 218 this->store->Write(some_attachments, this->write_callback); |
| 217 this->ClearAndPumpLoop(); | 219 this->ClearAndPumpLoop(); |
| 218 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | 220 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); |
| 219 | 221 |
| 220 // Drop attachment1 only. | 222 // Drop attachment1 only. |
| 221 AttachmentIdList ids; | 223 AttachmentIdList ids; |
| 222 ids.push_back(attachment1.GetId()); | 224 ids.push_back(attachment1.GetId()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 247 this->ClearAndPumpLoop(); | 249 this->ClearAndPumpLoop(); |
| 248 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); | 250 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); |
| 249 EXPECT_EQ(this->attachments->size(), 0U); | 251 EXPECT_EQ(this->attachments->size(), 0U); |
| 250 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); | 252 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); |
| 251 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment2.GetId()); | 253 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment2.GetId()); |
| 252 } | 254 } |
| 253 | 255 |
| 254 // Verify that attempting to drop an attachment that does not exist is not an | 256 // Verify that attempting to drop an attachment that does not exist is not an |
| 255 // error. | 257 // error. |
| 256 TYPED_TEST_P(AttachmentStoreTest, Drop_DoesNotExist) { | 258 TYPED_TEST_P(AttachmentStoreTest, Drop_DoesNotExist) { |
| 257 Attachment attachment1 = Attachment::Create(this->some_data1); | 259 Attachment attachment1 = Attachment::CreateNew(this->some_data1); |
| 258 AttachmentList some_attachments; | 260 AttachmentList some_attachments; |
| 259 some_attachments.push_back(attachment1); | 261 some_attachments.push_back(attachment1); |
| 260 this->store->Write(some_attachments, this->write_callback); | 262 this->store->Write(some_attachments, this->write_callback); |
| 261 this->ClearAndPumpLoop(); | 263 this->ClearAndPumpLoop(); |
| 262 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | 264 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); |
| 263 | 265 |
| 264 // Drop the attachment. | 266 // Drop the attachment. |
| 265 AttachmentIdList ids; | 267 AttachmentIdList ids; |
| 266 ids.push_back(attachment1.GetId()); | 268 ids.push_back(attachment1.GetId()); |
| 267 this->store->Drop(ids, this->drop_callback); | 269 this->store->Drop(ids, this->drop_callback); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 287 REGISTER_TYPED_TEST_CASE_P(AttachmentStoreTest, | 289 REGISTER_TYPED_TEST_CASE_P(AttachmentStoreTest, |
| 288 Write_NoOverwriteNoError, | 290 Write_NoOverwriteNoError, |
| 289 Write_RoundTrip, | 291 Write_RoundTrip, |
| 290 Read_OneNotFound, | 292 Read_OneNotFound, |
| 291 Drop_DropTwoButOnlyOneExists, | 293 Drop_DropTwoButOnlyOneExists, |
| 292 Drop_DoesNotExist); | 294 Drop_DoesNotExist); |
| 293 | 295 |
| 294 } // namespace syncer | 296 } // namespace syncer |
| 295 | 297 |
| 296 #endif // SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_ | 298 #endif // SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_ |
| OLD | NEW |