Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: sync/internal_api/attachments/attachment_store_test_template.h

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #ifndef SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_
6 #define SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_
7
8 #include <memory>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/run_loop.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "sync/api/attachments/attachment.h"
17 #include "sync/api/attachments/attachment_store.h"
18 #include "sync/internal_api/public/attachments/attachment_util.h"
19 #include "sync/protocol/sync.pb.h"
20 #include "testing/gmock/include/gmock/gmock-matchers.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 // AttachmentStoreTest defines tests for AttachmentStore. To instantiate these
24 // tests for a particular implementation you need to:
25 // - Include this file in test.
26 // - Create factory class for attachment store that implements factory method.
27 // - add INSTANTIATE_TYPED_TEST_CASE_P statement.
28 // Here is how to do it for MyAttachmentStore:
29 //
30 // class MyAttachmentStoreFactory {
31 // public:
32 // scoped_refptr<AttachmentStore> CreateAttachmentStore() {
33 // return new MyAttachmentStore();
34 // }
35 // };
36 //
37 // INSTANTIATE_TYPED_TEST_CASE_P(My,
38 // AttachmentStoreTest,
39 // MyAttachmentStoreFactory);
40
41 namespace syncer {
42
43 const char kTestData1[] = "test data 1";
44 const char kTestData2[] = "test data 2";
45
46 template <typename AttachmentStoreFactory>
47 class AttachmentStoreTest : public testing::Test {
48 protected:
49 AttachmentStoreFactory attachment_store_factory;
50 base::MessageLoop message_loop;
51 std::unique_ptr<AttachmentStore> store;
52 std::unique_ptr<AttachmentStoreForSync> store_for_sync;
53 AttachmentStore::Result result;
54 std::unique_ptr<AttachmentMap> attachments;
55 std::unique_ptr<AttachmentIdList> failed_attachment_ids;
56 std::unique_ptr<AttachmentMetadataList> attachment_metadata;
57
58 AttachmentStore::ReadCallback read_callback;
59 AttachmentStore::WriteCallback write_callback;
60 AttachmentStore::DropCallback drop_callback;
61 AttachmentStore::ReadMetadataCallback read_metadata_callback;
62
63 scoped_refptr<base::RefCountedString> some_data1;
64 scoped_refptr<base::RefCountedString> some_data2;
65
66 AttachmentStoreTest() {}
67
68 void SetUp() override {
69 store = attachment_store_factory.CreateAttachmentStore();
70 store_for_sync = store->CreateAttachmentStoreForSync();
71
72 Clear();
73 read_callback = base::Bind(&AttachmentStoreTest::CopyResultAttachments,
74 base::Unretained(this),
75 &result,
76 &attachments,
77 &failed_attachment_ids);
78 write_callback = base::Bind(
79 &AttachmentStoreTest::CopyResult, base::Unretained(this), &result);
80 drop_callback = write_callback;
81 read_metadata_callback =
82 base::Bind(&AttachmentStoreTest::CopyResultMetadata,
83 base::Unretained(this), &result, &attachment_metadata);
84
85 some_data1 = new base::RefCountedString;
86 some_data1->data() = kTestData1;
87
88 some_data2 = new base::RefCountedString;
89 some_data2->data() = kTestData2;
90 }
91
92 void ClearAndPumpLoop() {
93 Clear();
94 base::RunLoop().RunUntilIdle();
95 }
96
97 private:
98 void Clear() {
99 result = AttachmentStore::UNSPECIFIED_ERROR;
100 attachments.reset();
101 failed_attachment_ids.reset();
102 attachment_metadata.reset();
103 }
104
105 void CopyResult(AttachmentStore::Result* destination_result,
106 const AttachmentStore::Result& source_result) {
107 *destination_result = source_result;
108 }
109
110 void CopyResultAttachments(
111 AttachmentStore::Result* destination_result,
112 std::unique_ptr<AttachmentMap>* destination_attachments,
113 std::unique_ptr<AttachmentIdList>* destination_failed_attachment_ids,
114 const AttachmentStore::Result& source_result,
115 std::unique_ptr<AttachmentMap> source_attachments,
116 std::unique_ptr<AttachmentIdList> source_failed_attachment_ids) {
117 CopyResult(destination_result, source_result);
118 *destination_attachments = std::move(source_attachments);
119 *destination_failed_attachment_ids =
120 std::move(source_failed_attachment_ids);
121 }
122
123 void CopyResultMetadata(
124 AttachmentStore::Result* destination_result,
125 std::unique_ptr<AttachmentMetadataList>* destination_metadata,
126 const AttachmentStore::Result& source_result,
127 std::unique_ptr<AttachmentMetadataList> source_metadata) {
128 CopyResult(destination_result, source_result);
129 *destination_metadata = std::move(source_metadata);
130 }
131 };
132
133 TYPED_TEST_CASE_P(AttachmentStoreTest);
134
135 // Verify that CreateAttachmentStoreForSync() creates valid object.
136 TYPED_TEST_P(AttachmentStoreTest, CreateAttachmentStoreForSync) {
137 std::unique_ptr<AttachmentStoreForSync> attachment_store_for_sync =
138 this->store->CreateAttachmentStoreForSync();
139 EXPECT_NE(nullptr, attachment_store_for_sync);
140 }
141
142 // Verify that we do not overwrite existing attachments and that we do not treat
143 // it as an error.
144 TYPED_TEST_P(AttachmentStoreTest, Write_NoOverwriteNoError) {
145 // Create two attachments with the same id but different data.
146 Attachment attachment1 = Attachment::Create(this->some_data1);
147 Attachment attachment2 =
148 Attachment::CreateFromParts(attachment1.GetId(), this->some_data2);
149
150 // Write the first one.
151 AttachmentList some_attachments;
152 some_attachments.push_back(attachment1);
153 this->store->Write(some_attachments, this->write_callback);
154 this->ClearAndPumpLoop();
155 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
156
157 // Write the second one.
158 some_attachments.clear();
159 some_attachments.push_back(attachment2);
160 this->store->Write(some_attachments, this->write_callback);
161 this->ClearAndPumpLoop();
162 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
163
164 // Read it back and see that it was not overwritten.
165 AttachmentIdList some_attachment_ids;
166 some_attachment_ids.push_back(attachment1.GetId());
167 this->store->Read(some_attachment_ids, this->read_callback);
168 this->ClearAndPumpLoop();
169 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
170 EXPECT_EQ(1U, this->attachments->size());
171 EXPECT_EQ(0U, this->failed_attachment_ids->size());
172 AttachmentMap::const_iterator a1 =
173 this->attachments->find(attachment1.GetId());
174 EXPECT_TRUE(a1 != this->attachments->end());
175 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData()));
176 }
177
178 // Verify that we can write some attachments and read them back.
179 TYPED_TEST_P(AttachmentStoreTest, Write_RoundTrip) {
180 Attachment attachment1 = Attachment::Create(this->some_data1);
181 Attachment attachment2 = Attachment::Create(this->some_data2);
182 AttachmentList some_attachments;
183 some_attachments.push_back(attachment1);
184 some_attachments.push_back(attachment2);
185
186 this->store->Write(some_attachments, this->write_callback);
187 this->ClearAndPumpLoop();
188 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
189
190 AttachmentIdList some_attachment_ids;
191 some_attachment_ids.push_back(attachment1.GetId());
192 some_attachment_ids.push_back(attachment2.GetId());
193 this->store->Read(some_attachment_ids, this->read_callback);
194 this->ClearAndPumpLoop();
195 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
196 EXPECT_EQ(2U, this->attachments->size());
197 EXPECT_EQ(0U, this->failed_attachment_ids->size());
198
199 AttachmentMap::const_iterator a1 =
200 this->attachments->find(attachment1.GetId());
201 EXPECT_TRUE(a1 != this->attachments->end());
202 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData()));
203
204 AttachmentMap::const_iterator a2 =
205 this->attachments->find(attachment2.GetId());
206 EXPECT_TRUE(a2 != this->attachments->end());
207 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData()));
208 }
209
210 // Try to read two attachments when only one exists.
211 TYPED_TEST_P(AttachmentStoreTest, Read_OneNotFound) {
212 Attachment attachment1 = Attachment::Create(this->some_data1);
213 Attachment attachment2 = Attachment::Create(this->some_data2);
214
215 AttachmentList some_attachments;
216 // Write attachment1 only.
217 some_attachments.push_back(attachment1);
218 this->store->Write(some_attachments, this->write_callback);
219 this->ClearAndPumpLoop();
220 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
221
222 // Try to read both attachment1 and attachment2.
223 AttachmentIdList ids;
224 ids.push_back(attachment1.GetId());
225 ids.push_back(attachment2.GetId());
226 this->store->Read(ids, this->read_callback);
227 this->ClearAndPumpLoop();
228
229 // See that only attachment1 was read.
230 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, this->result);
231 EXPECT_EQ(1U, this->attachments->size());
232 EXPECT_EQ(1U, this->failed_attachment_ids->size());
233 }
234
235 // Try to drop two attachments when only one exists. Verify that no error occurs
236 // and that the existing attachment was dropped.
237 TYPED_TEST_P(AttachmentStoreTest, Drop_DropTwoButOnlyOneExists) {
238 // First, create two attachments.
239 Attachment attachment1 = Attachment::Create(this->some_data1);
240 Attachment attachment2 = Attachment::Create(this->some_data2);
241 AttachmentList some_attachments;
242 some_attachments.push_back(attachment1);
243 some_attachments.push_back(attachment2);
244 this->store->Write(some_attachments, this->write_callback);
245 this->ClearAndPumpLoop();
246 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
247
248 // Drop attachment1 only.
249 AttachmentIdList ids;
250 ids.push_back(attachment1.GetId());
251 this->store->Drop(ids, this->drop_callback);
252 this->ClearAndPumpLoop();
253 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
254
255 // See that attachment1 is gone.
256 this->store->Read(ids, this->read_callback);
257 this->ClearAndPumpLoop();
258 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, this->result);
259 EXPECT_EQ(0U, this->attachments->size());
260 EXPECT_EQ(1U, this->failed_attachment_ids->size());
261 EXPECT_EQ(attachment1.GetId(), (*this->failed_attachment_ids)[0]);
262
263 // Drop both attachment1 and attachment2.
264 ids.clear();
265 ids.push_back(attachment1.GetId());
266 ids.push_back(attachment2.GetId());
267 this->store->Drop(ids, this->drop_callback);
268 this->ClearAndPumpLoop();
269 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
270
271 // See that attachment2 is now gone.
272 ids.clear();
273 ids.push_back(attachment2.GetId());
274 this->store->Read(ids, this->read_callback);
275 this->ClearAndPumpLoop();
276 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, this->result);
277 EXPECT_EQ(0U, this->attachments->size());
278 EXPECT_EQ(1U, this->failed_attachment_ids->size());
279 EXPECT_EQ(attachment2.GetId(), (*this->failed_attachment_ids)[0]);
280 }
281
282 // Verify that attempting to drop an attachment that does not exist is not an
283 // error.
284 TYPED_TEST_P(AttachmentStoreTest, Drop_DoesNotExist) {
285 Attachment attachment1 = Attachment::Create(this->some_data1);
286 AttachmentList some_attachments;
287 some_attachments.push_back(attachment1);
288 this->store->Write(some_attachments, this->write_callback);
289 this->ClearAndPumpLoop();
290 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
291
292 // Drop the attachment.
293 AttachmentIdList ids;
294 ids.push_back(attachment1.GetId());
295 this->store->Drop(ids, this->drop_callback);
296 this->ClearAndPumpLoop();
297 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
298
299 // See that it's gone.
300 this->store->Read(ids, this->read_callback);
301 this->ClearAndPumpLoop();
302 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, this->result);
303 EXPECT_EQ(0U, this->attachments->size());
304 EXPECT_EQ(1U, this->failed_attachment_ids->size());
305 EXPECT_EQ(attachment1.GetId(), (*this->failed_attachment_ids)[0]);
306
307 // Drop again, see that no error occurs.
308 ids.clear();
309 ids.push_back(attachment1.GetId());
310 this->store->Drop(ids, this->drop_callback);
311 this->ClearAndPumpLoop();
312 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
313 }
314
315 // Verify getting metadata for specific attachments.
316 TYPED_TEST_P(AttachmentStoreTest, ReadMetadataById) {
317 Attachment attachment1 = Attachment::Create(this->some_data1);
318 Attachment attachment2 = Attachment::Create(this->some_data2);
319
320 AttachmentList some_attachments;
321 // Write attachment1 only.
322 some_attachments.push_back(attachment1);
323 this->store->Write(some_attachments, this->write_callback);
324 this->ClearAndPumpLoop();
325 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
326
327 // Try to read metadata for both attachment1 and attachment2.
328 AttachmentIdList ids;
329 ids.push_back(attachment1.GetId());
330 ids.push_back(attachment2.GetId());
331 this->store->ReadMetadataById(ids, this->read_metadata_callback);
332 this->ClearAndPumpLoop();
333
334 // See that only one entry was read.
335 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, this->result);
336 EXPECT_EQ(1U, this->attachment_metadata->size());
337
338 // Now write attachment2.
339 some_attachments[0] = attachment2;
340 this->store->Write(some_attachments, this->write_callback);
341 this->ClearAndPumpLoop();
342 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
343
344 // Try to read metadata for both attachment1 and attachment2 again.
345 this->store->ReadMetadataById(ids, this->read_metadata_callback);
346 this->ClearAndPumpLoop();
347 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
348 EXPECT_EQ(2U, this->attachment_metadata->size());
349
350 // Verify that we've got both entries back in the right order.
351 AttachmentMetadataList::const_iterator iter =
352 this->attachment_metadata->begin();
353 EXPECT_EQ(attachment1.GetId(), iter->GetId());
354 ++iter;
355 EXPECT_EQ(attachment2.GetId(), iter->GetId());
356 }
357
358 // Verify that ReadMetadata/ReadMetadataForSync returns metadata for correct
359 // set of attachments.
360 TYPED_TEST_P(AttachmentStoreTest, ReadMetadata) {
361 // Try to read all metadata from an empty store.
362 this->store->ReadMetadata(this->read_metadata_callback);
363 this->ClearAndPumpLoop();
364 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
365 EXPECT_EQ(0U, this->attachment_metadata->size());
366
367 // Create and write attachments with different set of references.
368 Attachment attachment_mt = Attachment::Create(this->some_data1);
369 Attachment attachment_sync = Attachment::Create(this->some_data1);
370 Attachment attachment_both = Attachment::Create(this->some_data1);
371
372 AttachmentList attachments;
373 attachments.push_back(attachment_mt);
374 attachments.push_back(attachment_sync);
375 attachments.push_back(attachment_both);
376 this->store->Write(attachments, this->write_callback);
377 this->ClearAndPumpLoop();
378 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
379
380 AttachmentIdList ids;
381 ids.push_back(attachment_sync.GetId());
382 ids.push_back(attachment_both.GetId());
383 this->store_for_sync->SetSyncReference(ids);
384
385 ids.clear();
386 ids.push_back(attachment_sync.GetId());
387 this->store->Drop(ids, this->drop_callback);
388 this->ClearAndPumpLoop();
389
390 // Calling ReadMetadataById for above three attachments should only return
391 // attachments with model type reference.
392 ids.clear();
393 ids.push_back(attachment_mt.GetId());
394 ids.push_back(attachment_sync.GetId());
395 ids.push_back(attachment_both.GetId());
396 this->store->ReadMetadataById(ids, this->read_metadata_callback);
397 this->ClearAndPumpLoop();
398 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, this->result);
399 EXPECT_EQ(2U, this->attachment_metadata->size());
400 AttachmentIdSet model_type_id_set;
401 model_type_id_set.insert(attachment_mt.GetId());
402 model_type_id_set.insert(attachment_both.GetId());
403 EXPECT_THAT(model_type_id_set,
404 testing::Contains((*this->attachment_metadata)[0].GetId()));
405 EXPECT_THAT(model_type_id_set,
406 testing::Contains((*this->attachment_metadata)[1].GetId()));
407
408 // Call to ReadMetadata() should only return attachments with model type
409 // reference.
410 this->store->ReadMetadata(this->read_metadata_callback);
411 this->ClearAndPumpLoop();
412 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
413 EXPECT_EQ(2U, this->attachment_metadata->size());
414
415 // Verify that we get all attachments back (the order is undefined).
416 EXPECT_THAT(model_type_id_set,
417 testing::Contains((*this->attachment_metadata)[0].GetId()));
418 EXPECT_THAT(model_type_id_set,
419 testing::Contains((*this->attachment_metadata)[1].GetId()));
420
421 // Call to ReadMetadataForSync() should only return attachments with sync
422 // reference.
423 this->store_for_sync->ReadMetadataForSync(this->read_metadata_callback);
424 this->ClearAndPumpLoop();
425 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
426 EXPECT_EQ(2U, this->attachment_metadata->size());
427
428 AttachmentIdSet sync_id_set;
429 sync_id_set.insert(attachment_sync.GetId());
430 sync_id_set.insert(attachment_both.GetId());
431 EXPECT_THAT(sync_id_set,
432 testing::Contains((*this->attachment_metadata)[0].GetId()));
433 EXPECT_THAT(sync_id_set,
434 testing::Contains((*this->attachment_metadata)[1].GetId()));
435 }
436
437 // Verify that setting/droping references gets reflected in ReadMetadata and
438 // that attachment is only deleted after last reference is droped.
439 TYPED_TEST_P(AttachmentStoreTest, SetSyncReference_DropSyncReference) {
440 Attachment attachment = Attachment::Create(this->some_data1);
441 AttachmentList attachments;
442 attachments.push_back(attachment);
443 AttachmentIdList ids;
444 ids.push_back(attachment.GetId());
445
446 // When writing attachment to store only model type reference should be set.
447 this->store->Write(attachments, this->write_callback);
448
449 this->store->ReadMetadata(this->read_metadata_callback);
450 this->ClearAndPumpLoop();
451 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
452 EXPECT_EQ(1U, this->attachment_metadata->size());
453 EXPECT_EQ(attachment.GetId(), this->attachment_metadata->begin()->GetId());
454
455 this->store_for_sync->ReadMetadataForSync(this->read_metadata_callback);
456 this->ClearAndPumpLoop();
457 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
458 EXPECT_EQ(0U, this->attachment_metadata->size());
459
460 // After call to SetSyncReference() ReadMetadataForSync should start returning
461 // attachment.
462 this->store_for_sync->SetSyncReference(ids);
463
464 this->store_for_sync->ReadMetadataForSync(this->read_metadata_callback);
465 this->ClearAndPumpLoop();
466 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
467 EXPECT_EQ(1U, this->attachment_metadata->size());
468
469 // Call SetSyncReference() to verify it is idempotent.
470 this->store_for_sync->SetSyncReference(ids);
471 this->ClearAndPumpLoop();
472
473 // Droping attachment should remove model type reference, but there is still
474 // sync reference.
475 this->store->Drop(ids, this->drop_callback);
476 this->ClearAndPumpLoop();
477 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
478
479 this->store->ReadMetadata(this->read_metadata_callback);
480 this->ClearAndPumpLoop();
481 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
482 EXPECT_EQ(0U, this->attachment_metadata->size());
483
484 this->store_for_sync->ReadMetadataForSync(this->read_metadata_callback);
485 this->ClearAndPumpLoop();
486 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
487 EXPECT_EQ(1U, this->attachment_metadata->size());
488
489 // ReadMetadataById should return UNSPECIFIED_ERROR, attachment doesn't have
490 // model type reference.
491 this->store->ReadMetadataById(ids, this->read_metadata_callback);
492 this->ClearAndPumpLoop();
493 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, this->result);
494 EXPECT_EQ(0U, this->attachment_metadata->size());
495
496 // Call Drop() again to ensure it doesn't fail.
497 this->store->Drop(ids, this->drop_callback);
498 this->ClearAndPumpLoop();
499 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
500
501 // After droping sync reference attachment should be deleted from store.
502 // ReadMetadataForSync should return empty result.
503 this->store_for_sync->DropSyncReference(ids);
504
505 this->store_for_sync->ReadMetadataForSync(this->read_metadata_callback);
506 this->ClearAndPumpLoop();
507 EXPECT_EQ(AttachmentStore::SUCCESS, this->result);
508 EXPECT_EQ(0U, this->attachment_metadata->size());
509 }
510
511 REGISTER_TYPED_TEST_CASE_P(AttachmentStoreTest,
512 CreateAttachmentStoreForSync,
513 Write_NoOverwriteNoError,
514 Write_RoundTrip,
515 Read_OneNotFound,
516 Drop_DropTwoButOnlyOneExists,
517 Drop_DoesNotExist,
518 ReadMetadataById,
519 ReadMetadata,
520 SetSyncReference_DropSyncReference);
521
522 } // namespace syncer
523
524 #endif // SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698