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