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/api/attachments/fake_attachment_store.h" | 5 #include "sync/api/attachments/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 "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "sync/api/attachments/attachment.h" | 12 #include "sync/api/attachments/attachment.h" |
13 #include "sync/protocol/sync.pb.h" | 13 #include "sync/protocol/sync.pb.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace syncer { | 16 namespace syncer { |
17 | 17 |
18 const char kTestData1[] = "test data 1"; | 18 const char kTestData1[] = "test data 1"; |
19 const char kTestData2[] = "test data 2"; | 19 const char kTestData2[] = "test data 2"; |
20 | 20 |
21 class FakeAttachmentStoreTest : public testing::Test { | 21 class InMemoryAttachmentStoreTest : public testing::Test { |
22 protected: | 22 protected: |
23 base::MessageLoop message_loop; | 23 base::MessageLoop message_loop; |
24 scoped_refptr<FakeAttachmentStore> store; | 24 scoped_refptr<AttachmentStore> store; |
25 AttachmentStore::Result result; | 25 AttachmentStore::Result result; |
26 scoped_ptr<AttachmentMap> attachments; | 26 scoped_ptr<AttachmentMap> attachments; |
27 scoped_ptr<AttachmentIdList> failed_attachment_ids; | 27 scoped_ptr<AttachmentIdList> failed_attachment_ids; |
28 | 28 |
29 AttachmentStore::ReadCallback read_callback; | 29 AttachmentStore::ReadCallback read_callback; |
30 AttachmentStore::WriteCallback write_callback; | 30 AttachmentStore::WriteCallback write_callback; |
31 AttachmentStore::DropCallback drop_callback; | 31 AttachmentStore::DropCallback drop_callback; |
32 | 32 |
33 scoped_refptr<base::RefCountedString> some_data1; | 33 scoped_refptr<base::RefCountedString> some_data1; |
34 scoped_refptr<base::RefCountedString> some_data2; | 34 scoped_refptr<base::RefCountedString> some_data2; |
35 | 35 |
36 FakeAttachmentStoreTest() | 36 InMemoryAttachmentStoreTest() |
37 : store(new FakeAttachmentStore(base::ThreadTaskRunnerHandle::Get())) {} | 37 : store(AttachmentStore::CreateInMemoryStore()) {} |
38 | 38 |
39 virtual void SetUp() { | 39 virtual void SetUp() { |
40 Clear(); | 40 Clear(); |
41 read_callback = base::Bind(&FakeAttachmentStoreTest::CopyResultAttachments, | 41 read_callback = |
42 base::Unretained(this), | 42 base::Bind(&InMemoryAttachmentStoreTest::CopyResultAttachments, |
43 &result, | 43 base::Unretained(this), |
44 &attachments, | 44 &result, |
45 &failed_attachment_ids); | 45 &attachments, |
46 write_callback = base::Bind( | 46 &failed_attachment_ids); |
47 &FakeAttachmentStoreTest::CopyResult, base::Unretained(this), &result); | 47 write_callback = base::Bind(&InMemoryAttachmentStoreTest::CopyResult, |
| 48 base::Unretained(this), |
| 49 &result); |
48 drop_callback = write_callback; | 50 drop_callback = write_callback; |
49 | 51 |
50 some_data1 = new base::RefCountedString; | 52 some_data1 = new base::RefCountedString; |
51 some_data1->data() = kTestData1; | 53 some_data1->data() = kTestData1; |
52 | 54 |
53 some_data2 = new base::RefCountedString; | 55 some_data2 = new base::RefCountedString; |
54 some_data2->data() = kTestData2; | 56 some_data2->data() = kTestData2; |
55 } | 57 } |
56 | 58 |
57 virtual void ClearAndPumpLoop() { | 59 virtual void ClearAndPumpLoop() { |
(...skipping 21 matching lines...) Expand all Loading... |
79 scoped_ptr<AttachmentMap> source_attachments, | 81 scoped_ptr<AttachmentMap> source_attachments, |
80 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { | 82 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { |
81 CopyResult(destination_result, source_result); | 83 CopyResult(destination_result, source_result); |
82 *destination_attachments = source_attachments.Pass(); | 84 *destination_attachments = source_attachments.Pass(); |
83 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); | 85 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); |
84 } | 86 } |
85 }; | 87 }; |
86 | 88 |
87 // Verify that we do not overwrite existing attachments and that we do not treat | 89 // Verify that we do not overwrite existing attachments and that we do not treat |
88 // it as an error. | 90 // it as an error. |
89 TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) { | 91 TEST_F(InMemoryAttachmentStoreTest, Write_NoOverwriteNoError) { |
90 // Create two attachments with the same id but different data. | 92 // Create two attachments with the same id but different data. |
91 Attachment attachment1 = Attachment::Create(some_data1); | 93 Attachment attachment1 = Attachment::Create(some_data1); |
92 Attachment attachment2 = | 94 Attachment attachment2 = |
93 Attachment::CreateWithId(attachment1.GetId(), some_data2); | 95 Attachment::CreateWithId(attachment1.GetId(), some_data2); |
94 | 96 |
95 // Write the first one. | 97 // Write the first one. |
96 AttachmentList some_attachments; | 98 AttachmentList some_attachments; |
97 some_attachments.push_back(attachment1); | 99 some_attachments.push_back(attachment1); |
98 store->Write(some_attachments, write_callback); | 100 store->Write(some_attachments, write_callback); |
99 ClearAndPumpLoop(); | 101 ClearAndPumpLoop(); |
(...skipping 13 matching lines...) Expand all Loading... |
113 ClearAndPumpLoop(); | 115 ClearAndPumpLoop(); |
114 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 116 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
115 EXPECT_EQ(attachments->size(), 1U); | 117 EXPECT_EQ(attachments->size(), 1U); |
116 EXPECT_EQ(failed_attachment_ids->size(), 0U); | 118 EXPECT_EQ(failed_attachment_ids->size(), 0U); |
117 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | 119 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); |
118 EXPECT_TRUE(a1 != attachments->end()); | 120 EXPECT_TRUE(a1 != attachments->end()); |
119 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 121 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
120 } | 122 } |
121 | 123 |
122 // Verify that we can write some attachments and read them back. | 124 // Verify that we can write some attachments and read them back. |
123 TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) { | 125 TEST_F(InMemoryAttachmentStoreTest, Write_RoundTrip) { |
124 Attachment attachment1 = Attachment::Create(some_data1); | 126 Attachment attachment1 = Attachment::Create(some_data1); |
125 Attachment attachment2 = Attachment::Create(some_data2); | 127 Attachment attachment2 = Attachment::Create(some_data2); |
126 AttachmentList some_attachments; | 128 AttachmentList some_attachments; |
127 some_attachments.push_back(attachment1); | 129 some_attachments.push_back(attachment1); |
128 some_attachments.push_back(attachment2); | 130 some_attachments.push_back(attachment2); |
129 | 131 |
130 store->Write(some_attachments, write_callback); | 132 store->Write(some_attachments, write_callback); |
131 ClearAndPumpLoop(); | 133 ClearAndPumpLoop(); |
132 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 134 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
133 | 135 |
134 AttachmentIdList some_attachment_ids; | 136 AttachmentIdList some_attachment_ids; |
135 some_attachment_ids.push_back(attachment1.GetId()); | 137 some_attachment_ids.push_back(attachment1.GetId()); |
136 some_attachment_ids.push_back(attachment2.GetId()); | 138 some_attachment_ids.push_back(attachment2.GetId()); |
137 store->Read(some_attachment_ids, read_callback); | 139 store->Read(some_attachment_ids, read_callback); |
138 ClearAndPumpLoop(); | 140 ClearAndPumpLoop(); |
139 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 141 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
140 EXPECT_EQ(attachments->size(), 2U); | 142 EXPECT_EQ(attachments->size(), 2U); |
141 EXPECT_EQ(failed_attachment_ids->size(), 0U); | 143 EXPECT_EQ(failed_attachment_ids->size(), 0U); |
142 | 144 |
143 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | 145 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); |
144 EXPECT_TRUE(a1 != attachments->end()); | 146 EXPECT_TRUE(a1 != attachments->end()); |
145 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 147 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
146 | 148 |
147 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); | 149 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); |
148 EXPECT_TRUE(a2 != attachments->end()); | 150 EXPECT_TRUE(a2 != attachments->end()); |
149 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); | 151 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); |
150 } | 152 } |
151 | 153 |
152 // Try to read two attachments when only one exists. | 154 // Try to read two attachments when only one exists. |
153 TEST_F(FakeAttachmentStoreTest, Read_OneNotFound) { | 155 TEST_F(InMemoryAttachmentStoreTest, Read_OneNotFound) { |
154 Attachment attachment1 = Attachment::Create(some_data1); | 156 Attachment attachment1 = Attachment::Create(some_data1); |
155 Attachment attachment2 = Attachment::Create(some_data2); | 157 Attachment attachment2 = Attachment::Create(some_data2); |
156 | 158 |
157 AttachmentList some_attachments; | 159 AttachmentList some_attachments; |
158 // Write attachment1 only. | 160 // Write attachment1 only. |
159 some_attachments.push_back(attachment1); | 161 some_attachments.push_back(attachment1); |
160 store->Write(some_attachments, write_callback); | 162 store->Write(some_attachments, write_callback); |
161 ClearAndPumpLoop(); | 163 ClearAndPumpLoop(); |
162 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 164 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
163 | 165 |
164 // Try to read both attachment1 and attachment2. | 166 // Try to read both attachment1 and attachment2. |
165 AttachmentIdList ids; | 167 AttachmentIdList ids; |
166 ids.push_back(attachment1.GetId()); | 168 ids.push_back(attachment1.GetId()); |
167 ids.push_back(attachment2.GetId()); | 169 ids.push_back(attachment2.GetId()); |
168 store->Read(ids, read_callback); | 170 store->Read(ids, read_callback); |
169 ClearAndPumpLoop(); | 171 ClearAndPumpLoop(); |
170 | 172 |
171 // See that only attachment1 was read. | 173 // See that only attachment1 was read. |
172 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 174 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
173 EXPECT_EQ(attachments->size(), 1U); | 175 EXPECT_EQ(attachments->size(), 1U); |
174 EXPECT_EQ(failed_attachment_ids->size(), 1U); | 176 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
175 } | 177 } |
176 | 178 |
177 // Try to drop two attachments when only one exists. Verify that no error occurs | 179 // Try to drop two attachments when only one exists. Verify that no error occurs |
178 // and that the existing attachment was dropped. | 180 // and that the existing attachment was dropped. |
179 TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { | 181 TEST_F(InMemoryAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { |
180 // First, create two attachments. | 182 // First, create two attachments. |
181 Attachment attachment1 = Attachment::Create(some_data1); | 183 Attachment attachment1 = Attachment::Create(some_data1); |
182 Attachment attachment2 = Attachment::Create(some_data2); | 184 Attachment attachment2 = Attachment::Create(some_data2); |
183 AttachmentList some_attachments; | 185 AttachmentList some_attachments; |
184 some_attachments.push_back(attachment1); | 186 some_attachments.push_back(attachment1); |
185 some_attachments.push_back(attachment2); | 187 some_attachments.push_back(attachment2); |
186 store->Write(some_attachments, write_callback); | 188 store->Write(some_attachments, write_callback); |
187 ClearAndPumpLoop(); | 189 ClearAndPumpLoop(); |
188 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 190 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
189 | 191 |
(...skipping 26 matching lines...) Expand all Loading... |
216 store->Read(ids, read_callback); | 218 store->Read(ids, read_callback); |
217 ClearAndPumpLoop(); | 219 ClearAndPumpLoop(); |
218 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 220 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
219 EXPECT_EQ(attachments->size(), 0U); | 221 EXPECT_EQ(attachments->size(), 0U); |
220 EXPECT_EQ(failed_attachment_ids->size(), 1U); | 222 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
221 EXPECT_EQ((*failed_attachment_ids)[0], attachment2.GetId()); | 223 EXPECT_EQ((*failed_attachment_ids)[0], attachment2.GetId()); |
222 } | 224 } |
223 | 225 |
224 // Verify that attempting to drop an attachment that does not exist is not an | 226 // Verify that attempting to drop an attachment that does not exist is not an |
225 // error. | 227 // error. |
226 TEST_F(FakeAttachmentStoreTest, Drop_DoesNotExist) { | 228 TEST_F(InMemoryAttachmentStoreTest, Drop_DoesNotExist) { |
227 Attachment attachment1 = Attachment::Create(some_data1); | 229 Attachment attachment1 = Attachment::Create(some_data1); |
228 AttachmentList some_attachments; | 230 AttachmentList some_attachments; |
229 some_attachments.push_back(attachment1); | 231 some_attachments.push_back(attachment1); |
230 store->Write(some_attachments, write_callback); | 232 store->Write(some_attachments, write_callback); |
231 ClearAndPumpLoop(); | 233 ClearAndPumpLoop(); |
232 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 234 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
233 | 235 |
234 // Drop the attachment. | 236 // Drop the attachment. |
235 AttachmentIdList ids; | 237 AttachmentIdList ids; |
236 ids.push_back(attachment1.GetId()); | 238 ids.push_back(attachment1.GetId()); |
(...skipping 11 matching lines...) Expand all Loading... |
248 | 250 |
249 // Drop again, see that no error occurs. | 251 // Drop again, see that no error occurs. |
250 ids.clear(); | 252 ids.clear(); |
251 ids.push_back(attachment1.GetId()); | 253 ids.push_back(attachment1.GetId()); |
252 store->Drop(ids, drop_callback); | 254 store->Drop(ids, drop_callback); |
253 ClearAndPumpLoop(); | 255 ClearAndPumpLoop(); |
254 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 256 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
255 } | 257 } |
256 | 258 |
257 } // namespace syncer | 259 } // namespace syncer |
OLD | NEW |