OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
maniscalco
2014/10/14 21:03:18
Too bad the diff came out this like. It's not eas
pavely
2014/10/16 22:13:29
Done.
| |
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 "sync/api/attachments/attachment_store.h" | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/files/scoped_temp_dir.h" | |
12 #include "base/memory/ref_counted_memory.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/thread_task_runner_handle.h" | |
16 #include "sync/api/attachments/attachment.h" | |
17 #include "sync/protocol/sync.pb.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 // AttachmentStoreTest defines tests for AttachmentStore. To instantiate these | |
21 // tests for a particular implementation you need to: | |
22 // - Include this file in test. | |
23 // - Create factory class for attachment store that implements factory method. | |
24 // - add INSTANTIATE_TYPED_TEST_CASE_P statement. | |
25 // Here is how to do it for MyAttachmentStore: | |
26 // | |
27 // class MyAttachmentStoreFactory { | |
28 // public: | |
29 // scoped_refptr<AttachmentStore> CreateAttachmentStore() { | |
30 // return new MyAttachmentStore(); | |
31 // } | |
32 // }; | |
33 // | |
34 // INSTANTIATE_TYPED_TEST_CASE_P(My, | |
35 // AttachmentStoreTest, | |
36 // MyAttachmentStoreFactory); | |
37 | |
38 namespace syncer { | |
39 | |
40 const char kTestData1[] = "test data 1"; | |
41 const char kTestData2[] = "test data 2"; | |
42 | |
43 template <typename AttachmentStoreFactory> | |
44 class AttachmentStoreTest : public testing::Test { | |
45 protected: | |
46 AttachmentStoreFactory attachment_store_factory; | |
47 base::MessageLoop message_loop; | |
48 scoped_refptr<AttachmentStore> store; | |
49 AttachmentStore::Result result; | |
50 scoped_ptr<AttachmentMap> attachments; | |
51 scoped_ptr<AttachmentIdList> failed_attachment_ids; | |
52 | |
53 AttachmentStore::LoadCallback load_callback; | |
54 AttachmentStore::ReadCallback read_callback; | |
55 AttachmentStore::WriteCallback write_callback; | |
56 AttachmentStore::DropCallback drop_callback; | |
57 | |
58 scoped_refptr<base::RefCountedString> some_data1; | |
59 scoped_refptr<base::RefCountedString> some_data2; | |
60 | |
61 AttachmentStoreTest() {} | |
62 | |
63 virtual void SetUp() { | |
64 store = attachment_store_factory.CreateAttachmentStore(); | |
65 | |
66 Clear(); | |
67 load_callback = base::Bind( | |
68 &AttachmentStoreTest::CopyResult, base::Unretained(this), &result); | |
69 read_callback = base::Bind(&AttachmentStoreTest::CopyResultAttachments, | |
70 base::Unretained(this), | |
71 &result, | |
72 &attachments, | |
73 &failed_attachment_ids); | |
74 write_callback = load_callback; | |
75 drop_callback = load_callback; | |
76 | |
77 store->Load(load_callback); | |
78 message_loop.RunUntilIdle(); | |
79 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
80 | |
81 some_data1 = new base::RefCountedString; | |
82 some_data1->data() = kTestData1; | |
83 | |
84 some_data2 = new base::RefCountedString; | |
85 some_data2->data() = kTestData2; | |
86 } | |
87 | |
88 virtual void ClearAndPumpLoop() { | |
89 Clear(); | |
90 message_loop.RunUntilIdle(); | |
91 } | |
92 | |
93 private: | |
94 void Clear() { | |
95 result = AttachmentStore::UNSPECIFIED_ERROR; | |
96 attachments.reset(); | |
97 failed_attachment_ids.reset(); | |
98 } | |
99 | |
100 void CopyResult(AttachmentStore::Result* destination_result, | |
101 const AttachmentStore::Result& source_result) { | |
102 *destination_result = source_result; | |
103 } | |
104 | |
105 void CopyResultAttachments( | |
106 AttachmentStore::Result* destination_result, | |
107 scoped_ptr<AttachmentMap>* destination_attachments, | |
108 scoped_ptr<AttachmentIdList>* destination_failed_attachment_ids, | |
109 const AttachmentStore::Result& source_result, | |
110 scoped_ptr<AttachmentMap> source_attachments, | |
111 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { | |
112 CopyResult(destination_result, source_result); | |
113 *destination_attachments = source_attachments.Pass(); | |
114 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); | |
115 } | |
116 }; | |
117 | |
118 TYPED_TEST_CASE_P(AttachmentStoreTest); | |
119 | |
120 // Verify that we do not overwrite existing attachments and that we do not treat | |
121 // it as an error. | |
122 TYPED_TEST_P(AttachmentStoreTest, Write_NoOverwriteNoError) { | |
123 // Create two attachments with the same id but different data. | |
124 Attachment attachment1 = Attachment::Create(this->some_data1); | |
125 Attachment attachment2 = | |
126 Attachment::CreateWithId(attachment1.GetId(), this->some_data2); | |
127 | |
128 // Write the first one. | |
129 AttachmentList some_attachments; | |
130 some_attachments.push_back(attachment1); | |
131 this->store->Write(some_attachments, this->write_callback); | |
132 this->ClearAndPumpLoop(); | |
133 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
134 | |
135 // Write the second one. | |
136 some_attachments.clear(); | |
137 some_attachments.push_back(attachment2); | |
138 this->store->Write(some_attachments, this->write_callback); | |
139 this->ClearAndPumpLoop(); | |
140 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
141 | |
142 // Read it back and see that it was not overwritten. | |
143 AttachmentIdList some_attachment_ids; | |
144 some_attachment_ids.push_back(attachment1.GetId()); | |
145 this->store->Read(some_attachment_ids, this->read_callback); | |
146 this->ClearAndPumpLoop(); | |
147 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
148 EXPECT_EQ(this->attachments->size(), 1U); | |
149 EXPECT_EQ(this->failed_attachment_ids->size(), 0U); | |
150 AttachmentMap::const_iterator a1 = | |
151 this->attachments->find(attachment1.GetId()); | |
152 EXPECT_TRUE(a1 != this->attachments->end()); | |
153 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | |
154 } | |
155 | |
156 // Verify that we can write some attachments and read them back. | |
157 TYPED_TEST_P(AttachmentStoreTest, Write_RoundTrip) { | |
158 Attachment attachment1 = Attachment::Create(this->some_data1); | |
159 Attachment attachment2 = Attachment::Create(this->some_data2); | |
160 AttachmentList some_attachments; | |
161 some_attachments.push_back(attachment1); | |
162 some_attachments.push_back(attachment2); | |
163 | |
164 this->store->Write(some_attachments, this->write_callback); | |
165 this->ClearAndPumpLoop(); | |
166 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
167 | |
168 AttachmentIdList some_attachment_ids; | |
169 some_attachment_ids.push_back(attachment1.GetId()); | |
170 some_attachment_ids.push_back(attachment2.GetId()); | |
171 this->store->Read(some_attachment_ids, this->read_callback); | |
172 this->ClearAndPumpLoop(); | |
173 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
174 EXPECT_EQ(this->attachments->size(), 2U); | |
175 EXPECT_EQ(this->failed_attachment_ids->size(), 0U); | |
176 | |
177 AttachmentMap::const_iterator a1 = | |
178 this->attachments->find(attachment1.GetId()); | |
179 EXPECT_TRUE(a1 != this->attachments->end()); | |
180 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | |
181 | |
182 AttachmentMap::const_iterator a2 = | |
183 this->attachments->find(attachment2.GetId()); | |
184 EXPECT_TRUE(a2 != this->attachments->end()); | |
185 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); | |
186 } | |
187 | |
188 // Try to read two attachments when only one exists. | |
189 TYPED_TEST_P(AttachmentStoreTest, Read_OneNotFound) { | |
190 Attachment attachment1 = Attachment::Create(this->some_data1); | |
191 Attachment attachment2 = Attachment::Create(this->some_data2); | |
192 | |
193 AttachmentList some_attachments; | |
194 // Write attachment1 only. | |
195 some_attachments.push_back(attachment1); | |
196 this->store->Write(some_attachments, this->write_callback); | |
197 this->ClearAndPumpLoop(); | |
198 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
199 | |
200 // Try to read both attachment1 and attachment2. | |
201 AttachmentIdList ids; | |
202 ids.push_back(attachment1.GetId()); | |
203 ids.push_back(attachment2.GetId()); | |
204 this->store->Read(ids, this->read_callback); | |
205 this->ClearAndPumpLoop(); | |
206 | |
207 // See that only attachment1 was read. | |
208 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); | |
209 EXPECT_EQ(this->attachments->size(), 1U); | |
210 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); | |
211 } | |
212 | |
213 // Try to drop two attachments when only one exists. Verify that no error occurs | |
214 // and that the existing attachment was dropped. | |
215 TYPED_TEST_P(AttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { | |
216 // First, create two attachments. | |
217 Attachment attachment1 = Attachment::Create(this->some_data1); | |
218 Attachment attachment2 = Attachment::Create(this->some_data2); | |
219 AttachmentList some_attachments; | |
220 some_attachments.push_back(attachment1); | |
221 some_attachments.push_back(attachment2); | |
222 this->store->Write(some_attachments, this->write_callback); | |
223 this->ClearAndPumpLoop(); | |
224 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
225 | |
226 // Drop attachment1 only. | |
227 AttachmentIdList ids; | |
228 ids.push_back(attachment1.GetId()); | |
229 this->store->Drop(ids, this->drop_callback); | |
230 this->ClearAndPumpLoop(); | |
231 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
232 | |
233 // See that attachment1 is gone. | |
234 this->store->Read(ids, this->read_callback); | |
235 this->ClearAndPumpLoop(); | |
236 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); | |
237 EXPECT_EQ(this->attachments->size(), 0U); | |
238 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); | |
239 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment1.GetId()); | |
240 | |
241 // Drop both attachment1 and attachment2. | |
242 ids.clear(); | |
243 ids.push_back(attachment1.GetId()); | |
244 ids.push_back(attachment2.GetId()); | |
245 this->store->Drop(ids, this->drop_callback); | |
246 this->ClearAndPumpLoop(); | |
247 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
248 | |
249 // See that attachment2 is now gone. | |
250 ids.clear(); | |
251 ids.push_back(attachment2.GetId()); | |
252 this->store->Read(ids, this->read_callback); | |
253 this->ClearAndPumpLoop(); | |
254 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); | |
255 EXPECT_EQ(this->attachments->size(), 0U); | |
256 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); | |
257 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment2.GetId()); | |
258 } | |
259 | |
260 // Verify that attempting to drop an attachment that does not exist is not an | |
261 // error. | |
262 TYPED_TEST_P(AttachmentStoreTest, Drop_DoesNotExist) { | |
263 Attachment attachment1 = Attachment::Create(this->some_data1); | |
264 AttachmentList some_attachments; | |
265 some_attachments.push_back(attachment1); | |
266 this->store->Write(some_attachments, this->write_callback); | |
267 this->ClearAndPumpLoop(); | |
268 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
269 | |
270 // Drop the attachment. | |
271 AttachmentIdList ids; | |
272 ids.push_back(attachment1.GetId()); | |
273 this->store->Drop(ids, this->drop_callback); | |
274 this->ClearAndPumpLoop(); | |
275 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
276 | |
277 // See that it's gone. | |
278 this->store->Read(ids, this->read_callback); | |
279 this->ClearAndPumpLoop(); | |
280 EXPECT_EQ(this->result, AttachmentStore::UNSPECIFIED_ERROR); | |
281 EXPECT_EQ(this->attachments->size(), 0U); | |
282 EXPECT_EQ(this->failed_attachment_ids->size(), 1U); | |
283 EXPECT_EQ((*this->failed_attachment_ids)[0], attachment1.GetId()); | |
284 | |
285 // Drop again, see that no error occurs. | |
286 ids.clear(); | |
287 ids.push_back(attachment1.GetId()); | |
288 this->store->Drop(ids, this->drop_callback); | |
289 this->ClearAndPumpLoop(); | |
290 EXPECT_EQ(this->result, AttachmentStore::SUCCESS); | |
291 } | |
292 | |
293 REGISTER_TYPED_TEST_CASE_P(AttachmentStoreTest, | |
294 Write_NoOverwriteNoError, | |
295 Write_RoundTrip, | |
296 Read_OneNotFound, | |
297 Drop_DropTwoButOnlyOneExists, | |
298 Drop_DoesNotExist); | |
299 | |
300 } // namespace syncer | |
301 | |
302 #endif // SYNC_INTERNAL_API_ATTACHMENTS_ATTACHMENT_STORE_TEST_TEMPLATE_H_ | |
OLD | NEW |