OLD | NEW |
| (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 #include "sync/api/attachments/attachment_store.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ref_counted_memory.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "sync/api/attachments/attachment.h" | |
13 #include "sync/protocol/sync.pb.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace syncer { | |
17 | |
18 const char kTestData1[] = "test data 1"; | |
19 const char kTestData2[] = "test data 2"; | |
20 | |
21 class InMemoryAttachmentStoreTest : public testing::Test { | |
22 protected: | |
23 base::MessageLoop message_loop; | |
24 scoped_refptr<AttachmentStore> store; | |
25 AttachmentStore::Result result; | |
26 scoped_ptr<AttachmentMap> attachments; | |
27 scoped_ptr<AttachmentIdList> failed_attachment_ids; | |
28 | |
29 AttachmentStore::ReadCallback read_callback; | |
30 AttachmentStore::WriteCallback write_callback; | |
31 AttachmentStore::DropCallback drop_callback; | |
32 | |
33 scoped_refptr<base::RefCountedString> some_data1; | |
34 scoped_refptr<base::RefCountedString> some_data2; | |
35 | |
36 InMemoryAttachmentStoreTest() | |
37 : store(AttachmentStore::CreateInMemoryStore()) {} | |
38 | |
39 virtual void SetUp() { | |
40 Clear(); | |
41 read_callback = | |
42 base::Bind(&InMemoryAttachmentStoreTest::CopyResultAttachments, | |
43 base::Unretained(this), | |
44 &result, | |
45 &attachments, | |
46 &failed_attachment_ids); | |
47 write_callback = base::Bind(&InMemoryAttachmentStoreTest::CopyResult, | |
48 base::Unretained(this), | |
49 &result); | |
50 drop_callback = write_callback; | |
51 | |
52 some_data1 = new base::RefCountedString; | |
53 some_data1->data() = kTestData1; | |
54 | |
55 some_data2 = new base::RefCountedString; | |
56 some_data2->data() = kTestData2; | |
57 } | |
58 | |
59 virtual void ClearAndPumpLoop() { | |
60 Clear(); | |
61 message_loop.RunUntilIdle(); | |
62 } | |
63 | |
64 private: | |
65 void Clear() { | |
66 result = AttachmentStore::UNSPECIFIED_ERROR; | |
67 attachments.reset(); | |
68 failed_attachment_ids.reset(); | |
69 } | |
70 | |
71 void CopyResult(AttachmentStore::Result* destination_result, | |
72 const AttachmentStore::Result& source_result) { | |
73 *destination_result = source_result; | |
74 } | |
75 | |
76 void CopyResultAttachments( | |
77 AttachmentStore::Result* destination_result, | |
78 scoped_ptr<AttachmentMap>* destination_attachments, | |
79 scoped_ptr<AttachmentIdList>* destination_failed_attachment_ids, | |
80 const AttachmentStore::Result& source_result, | |
81 scoped_ptr<AttachmentMap> source_attachments, | |
82 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { | |
83 CopyResult(destination_result, source_result); | |
84 *destination_attachments = source_attachments.Pass(); | |
85 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); | |
86 } | |
87 }; | |
88 | |
89 // Verify that we do not overwrite existing attachments and that we do not treat | |
90 // it as an error. | |
91 TEST_F(InMemoryAttachmentStoreTest, Write_NoOverwriteNoError) { | |
92 // Create two attachments with the same id but different data. | |
93 Attachment attachment1 = Attachment::Create(some_data1); | |
94 Attachment attachment2 = | |
95 Attachment::CreateWithId(attachment1.GetId(), some_data2); | |
96 | |
97 // Write the first one. | |
98 AttachmentList some_attachments; | |
99 some_attachments.push_back(attachment1); | |
100 store->Write(some_attachments, write_callback); | |
101 ClearAndPumpLoop(); | |
102 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
103 | |
104 // Write the second one. | |
105 some_attachments.clear(); | |
106 some_attachments.push_back(attachment2); | |
107 store->Write(some_attachments, write_callback); | |
108 ClearAndPumpLoop(); | |
109 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
110 | |
111 // Read it back and see that it was not overwritten. | |
112 AttachmentIdList some_attachment_ids; | |
113 some_attachment_ids.push_back(attachment1.GetId()); | |
114 store->Read(some_attachment_ids, read_callback); | |
115 ClearAndPumpLoop(); | |
116 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
117 EXPECT_EQ(attachments->size(), 1U); | |
118 EXPECT_EQ(failed_attachment_ids->size(), 0U); | |
119 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | |
120 EXPECT_TRUE(a1 != attachments->end()); | |
121 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | |
122 } | |
123 | |
124 // Verify that we can write some attachments and read them back. | |
125 TEST_F(InMemoryAttachmentStoreTest, Write_RoundTrip) { | |
126 Attachment attachment1 = Attachment::Create(some_data1); | |
127 Attachment attachment2 = Attachment::Create(some_data2); | |
128 AttachmentList some_attachments; | |
129 some_attachments.push_back(attachment1); | |
130 some_attachments.push_back(attachment2); | |
131 | |
132 store->Write(some_attachments, write_callback); | |
133 ClearAndPumpLoop(); | |
134 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
135 | |
136 AttachmentIdList some_attachment_ids; | |
137 some_attachment_ids.push_back(attachment1.GetId()); | |
138 some_attachment_ids.push_back(attachment2.GetId()); | |
139 store->Read(some_attachment_ids, read_callback); | |
140 ClearAndPumpLoop(); | |
141 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
142 EXPECT_EQ(attachments->size(), 2U); | |
143 EXPECT_EQ(failed_attachment_ids->size(), 0U); | |
144 | |
145 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | |
146 EXPECT_TRUE(a1 != attachments->end()); | |
147 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | |
148 | |
149 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); | |
150 EXPECT_TRUE(a2 != attachments->end()); | |
151 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); | |
152 } | |
153 | |
154 // Try to read two attachments when only one exists. | |
155 TEST_F(InMemoryAttachmentStoreTest, Read_OneNotFound) { | |
156 Attachment attachment1 = Attachment::Create(some_data1); | |
157 Attachment attachment2 = Attachment::Create(some_data2); | |
158 | |
159 AttachmentList some_attachments; | |
160 // Write attachment1 only. | |
161 some_attachments.push_back(attachment1); | |
162 store->Write(some_attachments, write_callback); | |
163 ClearAndPumpLoop(); | |
164 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
165 | |
166 // Try to read both attachment1 and attachment2. | |
167 AttachmentIdList ids; | |
168 ids.push_back(attachment1.GetId()); | |
169 ids.push_back(attachment2.GetId()); | |
170 store->Read(ids, read_callback); | |
171 ClearAndPumpLoop(); | |
172 | |
173 // See that only attachment1 was read. | |
174 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | |
175 EXPECT_EQ(attachments->size(), 1U); | |
176 EXPECT_EQ(failed_attachment_ids->size(), 1U); | |
177 } | |
178 | |
179 // Try to drop two attachments when only one exists. Verify that no error occurs | |
180 // and that the existing attachment was dropped. | |
181 TEST_F(InMemoryAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { | |
182 // First, create two attachments. | |
183 Attachment attachment1 = Attachment::Create(some_data1); | |
184 Attachment attachment2 = Attachment::Create(some_data2); | |
185 AttachmentList some_attachments; | |
186 some_attachments.push_back(attachment1); | |
187 some_attachments.push_back(attachment2); | |
188 store->Write(some_attachments, write_callback); | |
189 ClearAndPumpLoop(); | |
190 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
191 | |
192 // Drop attachment1 only. | |
193 AttachmentIdList ids; | |
194 ids.push_back(attachment1.GetId()); | |
195 store->Drop(ids, drop_callback); | |
196 ClearAndPumpLoop(); | |
197 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
198 | |
199 // See that attachment1 is gone. | |
200 store->Read(ids, read_callback); | |
201 ClearAndPumpLoop(); | |
202 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | |
203 EXPECT_EQ(attachments->size(), 0U); | |
204 EXPECT_EQ(failed_attachment_ids->size(), 1U); | |
205 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); | |
206 | |
207 // Drop both attachment1 and attachment2. | |
208 ids.clear(); | |
209 ids.push_back(attachment1.GetId()); | |
210 ids.push_back(attachment2.GetId()); | |
211 store->Drop(ids, drop_callback); | |
212 ClearAndPumpLoop(); | |
213 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
214 | |
215 // See that attachment2 is now gone. | |
216 ids.clear(); | |
217 ids.push_back(attachment2.GetId()); | |
218 store->Read(ids, read_callback); | |
219 ClearAndPumpLoop(); | |
220 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | |
221 EXPECT_EQ(attachments->size(), 0U); | |
222 EXPECT_EQ(failed_attachment_ids->size(), 1U); | |
223 EXPECT_EQ((*failed_attachment_ids)[0], attachment2.GetId()); | |
224 } | |
225 | |
226 // Verify that attempting to drop an attachment that does not exist is not an | |
227 // error. | |
228 TEST_F(InMemoryAttachmentStoreTest, Drop_DoesNotExist) { | |
229 Attachment attachment1 = Attachment::Create(some_data1); | |
230 AttachmentList some_attachments; | |
231 some_attachments.push_back(attachment1); | |
232 store->Write(some_attachments, write_callback); | |
233 ClearAndPumpLoop(); | |
234 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
235 | |
236 // Drop the attachment. | |
237 AttachmentIdList ids; | |
238 ids.push_back(attachment1.GetId()); | |
239 store->Drop(ids, drop_callback); | |
240 ClearAndPumpLoop(); | |
241 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
242 | |
243 // See that it's gone. | |
244 store->Read(ids, read_callback); | |
245 ClearAndPumpLoop(); | |
246 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | |
247 EXPECT_EQ(attachments->size(), 0U); | |
248 EXPECT_EQ(failed_attachment_ids->size(), 1U); | |
249 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); | |
250 | |
251 // Drop again, see that no error occurs. | |
252 ids.clear(); | |
253 ids.push_back(attachment1.GetId()); | |
254 store->Drop(ids, drop_callback); | |
255 ClearAndPumpLoop(); | |
256 EXPECT_EQ(result, AttachmentStore::SUCCESS); | |
257 } | |
258 | |
259 } // namespace syncer | |
OLD | NEW |