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