| 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(); |
| 64 failed_attachment_ids.reset(); |
| 62 } | 65 } |
| 63 | 66 |
| 64 void CopyResult(AttachmentStore::Result* destination_result, | 67 void CopyResult(AttachmentStore::Result* destination_result, |
| 65 const AttachmentStore::Result& source_result) { | 68 const AttachmentStore::Result& source_result) { |
| 66 *destination_result = source_result; | 69 *destination_result = source_result; |
| 67 } | 70 } |
| 68 | 71 |
| 69 void CopyResultAttachments(AttachmentStore::Result* destination_result, | 72 void CopyResultAttachments( |
| 70 scoped_ptr<AttachmentMap>* destination_attachments, | 73 AttachmentStore::Result* destination_result, |
| 71 const AttachmentStore::Result& source_result, | 74 scoped_ptr<AttachmentMap>* destination_attachments, |
| 72 scoped_ptr<AttachmentMap> source_attachments) { | 75 scoped_ptr<AttachmentIdList>* destination_failed_attachment_ids, |
| 76 const AttachmentStore::Result& source_result, |
| 77 scoped_ptr<AttachmentMap> source_attachments, |
| 78 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { |
| 73 CopyResult(destination_result, source_result); | 79 CopyResult(destination_result, source_result); |
| 74 *destination_attachments = source_attachments.Pass(); | 80 *destination_attachments = source_attachments.Pass(); |
| 81 *destination_failed_attachment_ids = source_failed_attachment_ids.Pass(); |
| 75 } | 82 } |
| 76 }; | 83 }; |
| 77 | 84 |
| 78 // Verify that we do not overwrite existing attachments and that we do not treat | 85 // Verify that we do not overwrite existing attachments and that we do not treat |
| 79 // it as an error. | 86 // it as an error. |
| 80 TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) { | 87 TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) { |
| 81 // Create two attachments with the same id but different data. | 88 // Create two attachments with the same id but different data. |
| 82 Attachment attachment1 = Attachment::Create(some_data1); | 89 Attachment attachment1 = Attachment::Create(some_data1); |
| 83 Attachment attachment2 = | 90 Attachment attachment2 = |
| 84 Attachment::CreateWithId(attachment1.GetId(), some_data2); | 91 Attachment::CreateWithId(attachment1.GetId(), some_data2); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 97 ClearAndPumpLoop(); | 104 ClearAndPumpLoop(); |
| 98 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 105 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 99 | 106 |
| 100 // Read it back and see that it was not overwritten. | 107 // Read it back and see that it was not overwritten. |
| 101 AttachmentIdList some_attachment_ids; | 108 AttachmentIdList some_attachment_ids; |
| 102 some_attachment_ids.push_back(attachment1.GetId()); | 109 some_attachment_ids.push_back(attachment1.GetId()); |
| 103 store.Read(some_attachment_ids, read_callback); | 110 store.Read(some_attachment_ids, read_callback); |
| 104 ClearAndPumpLoop(); | 111 ClearAndPumpLoop(); |
| 105 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 112 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 106 EXPECT_EQ(attachments->size(), 1U); | 113 EXPECT_EQ(attachments->size(), 1U); |
| 114 EXPECT_EQ(failed_attachment_ids->size(), 0U); |
| 107 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | 115 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); |
| 108 EXPECT_TRUE(a1 != attachments->end()); | 116 EXPECT_TRUE(a1 != attachments->end()); |
| 109 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 117 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
| 110 } | 118 } |
| 111 | 119 |
| 112 // Verify that we can write some attachments and read them back. | 120 // Verify that we can write some attachments and read them back. |
| 113 TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) { | 121 TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) { |
| 114 Attachment attachment1 = Attachment::Create(some_data1); | 122 Attachment attachment1 = Attachment::Create(some_data1); |
| 115 Attachment attachment2 = Attachment::Create(some_data2); | 123 Attachment attachment2 = Attachment::Create(some_data2); |
| 116 AttachmentList some_attachments; | 124 AttachmentList some_attachments; |
| 117 some_attachments.push_back(attachment1); | 125 some_attachments.push_back(attachment1); |
| 118 some_attachments.push_back(attachment2); | 126 some_attachments.push_back(attachment2); |
| 119 | 127 |
| 120 store.Write(some_attachments, write_callback); | 128 store.Write(some_attachments, write_callback); |
| 121 ClearAndPumpLoop(); | 129 ClearAndPumpLoop(); |
| 122 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 130 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 123 | 131 |
| 124 AttachmentIdList some_attachment_ids; | 132 AttachmentIdList some_attachment_ids; |
| 125 some_attachment_ids.push_back(attachment1.GetId()); | 133 some_attachment_ids.push_back(attachment1.GetId()); |
| 126 some_attachment_ids.push_back(attachment2.GetId()); | 134 some_attachment_ids.push_back(attachment2.GetId()); |
| 127 store.Read(some_attachment_ids, read_callback); | 135 store.Read(some_attachment_ids, read_callback); |
| 128 ClearAndPumpLoop(); | 136 ClearAndPumpLoop(); |
| 129 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 137 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 130 EXPECT_EQ(attachments->size(), 2U); | 138 EXPECT_EQ(attachments->size(), 2U); |
| 139 EXPECT_EQ(failed_attachment_ids->size(), 0U); |
| 131 | 140 |
| 132 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | 141 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); |
| 133 EXPECT_TRUE(a1 != attachments->end()); | 142 EXPECT_TRUE(a1 != attachments->end()); |
| 134 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 143 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
| 135 | 144 |
| 136 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); | 145 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); |
| 137 EXPECT_TRUE(a2 != attachments->end()); | 146 EXPECT_TRUE(a2 != attachments->end()); |
| 138 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); | 147 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); |
| 139 } | 148 } |
| 140 | 149 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 153 // Try to read both attachment1 and attachment2. | 162 // Try to read both attachment1 and attachment2. |
| 154 AttachmentIdList ids; | 163 AttachmentIdList ids; |
| 155 ids.push_back(attachment1.GetId()); | 164 ids.push_back(attachment1.GetId()); |
| 156 ids.push_back(attachment2.GetId()); | 165 ids.push_back(attachment2.GetId()); |
| 157 store.Read(ids, read_callback); | 166 store.Read(ids, read_callback); |
| 158 ClearAndPumpLoop(); | 167 ClearAndPumpLoop(); |
| 159 | 168 |
| 160 // See that only attachment1 was read. | 169 // See that only attachment1 was read. |
| 161 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 170 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 162 EXPECT_EQ(attachments->size(), 1U); | 171 EXPECT_EQ(attachments->size(), 1U); |
| 172 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 163 } | 173 } |
| 164 | 174 |
| 165 // Try to drop two attachments when only one exists. Verify that no error occurs | 175 // Try to drop two attachments when only one exists. Verify that no error occurs |
| 166 // and that the existing attachment was dropped. | 176 // and that the existing attachment was dropped. |
| 167 TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { | 177 TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { |
| 168 // First, create two attachments. | 178 // First, create two attachments. |
| 169 Attachment attachment1 = Attachment::Create(some_data1); | 179 Attachment attachment1 = Attachment::Create(some_data1); |
| 170 Attachment attachment2 = Attachment::Create(some_data2); | 180 Attachment attachment2 = Attachment::Create(some_data2); |
| 171 AttachmentList some_attachments; | 181 AttachmentList some_attachments; |
| 172 some_attachments.push_back(attachment1); | 182 some_attachments.push_back(attachment1); |
| 173 some_attachments.push_back(attachment2); | 183 some_attachments.push_back(attachment2); |
| 174 store.Write(some_attachments, write_callback); | 184 store.Write(some_attachments, write_callback); |
| 175 ClearAndPumpLoop(); | 185 ClearAndPumpLoop(); |
| 176 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 186 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 177 | 187 |
| 178 // Drop attachment1 only. | 188 // Drop attachment1 only. |
| 179 AttachmentIdList ids; | 189 AttachmentIdList ids; |
| 180 ids.push_back(attachment1.GetId()); | 190 ids.push_back(attachment1.GetId()); |
| 181 store.Drop(ids, drop_callback); | 191 store.Drop(ids, drop_callback); |
| 182 ClearAndPumpLoop(); | 192 ClearAndPumpLoop(); |
| 183 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 193 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 184 | 194 |
| 185 // See that attachment1 is gone. | 195 // See that attachment1 is gone. |
| 186 store.Read(ids, read_callback); | 196 store.Read(ids, read_callback); |
| 187 ClearAndPumpLoop(); | 197 ClearAndPumpLoop(); |
| 188 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 198 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 189 EXPECT_EQ(attachments->size(), 0U); | 199 EXPECT_EQ(attachments->size(), 0U); |
| 200 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 201 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); |
| 190 | 202 |
| 191 // Drop both attachment1 and attachment2. | 203 // Drop both attachment1 and attachment2. |
| 192 ids.clear(); | 204 ids.clear(); |
| 193 ids.push_back(attachment1.GetId()); | 205 ids.push_back(attachment1.GetId()); |
| 194 ids.push_back(attachment2.GetId()); | 206 ids.push_back(attachment2.GetId()); |
| 195 store.Drop(ids, drop_callback); | 207 store.Drop(ids, drop_callback); |
| 196 ClearAndPumpLoop(); | 208 ClearAndPumpLoop(); |
| 197 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 209 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 198 | 210 |
| 199 // See that attachment2 is now gone. | 211 // See that attachment2 is now gone. |
| 200 ids.clear(); | 212 ids.clear(); |
| 201 ids.push_back(attachment2.GetId()); | 213 ids.push_back(attachment2.GetId()); |
| 202 store.Read(ids, read_callback); | 214 store.Read(ids, read_callback); |
| 203 ClearAndPumpLoop(); | 215 ClearAndPumpLoop(); |
| 204 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 216 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 205 EXPECT_EQ(attachments->size(), 0U); | 217 EXPECT_EQ(attachments->size(), 0U); |
| 218 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 219 EXPECT_EQ((*failed_attachment_ids)[0], attachment2.GetId()); |
| 206 } | 220 } |
| 207 | 221 |
| 208 // Verify that attempting to drop an attachment that does not exist is not an | 222 // Verify that attempting to drop an attachment that does not exist is not an |
| 209 // error. | 223 // error. |
| 210 TEST_F(FakeAttachmentStoreTest, Drop_DoesNotExist) { | 224 TEST_F(FakeAttachmentStoreTest, Drop_DoesNotExist) { |
| 211 Attachment attachment1 = Attachment::Create(some_data1); | 225 Attachment attachment1 = Attachment::Create(some_data1); |
| 212 AttachmentList some_attachments; | 226 AttachmentList some_attachments; |
| 213 some_attachments.push_back(attachment1); | 227 some_attachments.push_back(attachment1); |
| 214 store.Write(some_attachments, write_callback); | 228 store.Write(some_attachments, write_callback); |
| 215 ClearAndPumpLoop(); | 229 ClearAndPumpLoop(); |
| 216 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 230 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 217 | 231 |
| 218 // Drop the attachment. | 232 // Drop the attachment. |
| 219 AttachmentIdList ids; | 233 AttachmentIdList ids; |
| 220 ids.push_back(attachment1.GetId()); | 234 ids.push_back(attachment1.GetId()); |
| 221 store.Drop(ids, drop_callback); | 235 store.Drop(ids, drop_callback); |
| 222 ClearAndPumpLoop(); | 236 ClearAndPumpLoop(); |
| 223 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 237 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 224 | 238 |
| 225 // See that it's gone. | 239 // See that it's gone. |
| 226 store.Read(ids, read_callback); | 240 store.Read(ids, read_callback); |
| 227 ClearAndPumpLoop(); | 241 ClearAndPumpLoop(); |
| 228 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 242 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 229 EXPECT_EQ(attachments->size(), 0U); | 243 EXPECT_EQ(attachments->size(), 0U); |
| 244 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 245 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); |
| 230 | 246 |
| 231 // Drop again, see that no error occurs. | 247 // Drop again, see that no error occurs. |
| 232 ids.clear(); | 248 ids.clear(); |
| 233 ids.push_back(attachment1.GetId()); | 249 ids.push_back(attachment1.GetId()); |
| 234 store.Drop(ids, drop_callback); | 250 store.Drop(ids, drop_callback); |
| 235 ClearAndPumpLoop(); | 251 ClearAndPumpLoop(); |
| 236 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 252 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 237 } | 253 } |
| 238 | 254 |
| 239 } // namespace syncer | 255 } // namespace syncer |
| OLD | NEW |