| 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/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 "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 FakeAttachmentStoreTest : public testing::Test { |
| 22 protected: | 22 protected: |
| 23 base::MessageLoop message_loop; | 23 base::MessageLoop message_loop; |
| 24 FakeAttachmentStore store; | 24 scoped_refptr<FakeAttachmentStore> 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() : store(base::ThreadTaskRunnerHandle::Get()) {} | 36 FakeAttachmentStoreTest() |
| 37 : store(new FakeAttachmentStore(base::ThreadTaskRunnerHandle::Get())) {} |
| 37 | 38 |
| 38 virtual void SetUp() { | 39 virtual void SetUp() { |
| 39 Clear(); | 40 Clear(); |
| 40 read_callback = base::Bind(&FakeAttachmentStoreTest::CopyResultAttachments, | 41 read_callback = base::Bind(&FakeAttachmentStoreTest::CopyResultAttachments, |
| 41 base::Unretained(this), | 42 base::Unretained(this), |
| 42 &result, | 43 &result, |
| 43 &attachments, | 44 &attachments, |
| 44 &failed_attachment_ids); | 45 &failed_attachment_ids); |
| 45 write_callback = base::Bind( | 46 write_callback = base::Bind( |
| 46 &FakeAttachmentStoreTest::CopyResult, base::Unretained(this), &result); | 47 &FakeAttachmentStoreTest::CopyResult, base::Unretained(this), &result); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // it as an error. | 88 // it as an error. |
| 88 TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) { | 89 TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) { |
| 89 // Create two attachments with the same id but different data. | 90 // Create two attachments with the same id but different data. |
| 90 Attachment attachment1 = Attachment::Create(some_data1); | 91 Attachment attachment1 = Attachment::Create(some_data1); |
| 91 Attachment attachment2 = | 92 Attachment attachment2 = |
| 92 Attachment::CreateWithId(attachment1.GetId(), some_data2); | 93 Attachment::CreateWithId(attachment1.GetId(), some_data2); |
| 93 | 94 |
| 94 // Write the first one. | 95 // Write the first one. |
| 95 AttachmentList some_attachments; | 96 AttachmentList some_attachments; |
| 96 some_attachments.push_back(attachment1); | 97 some_attachments.push_back(attachment1); |
| 97 store.Write(some_attachments, write_callback); | 98 store->Write(some_attachments, write_callback); |
| 98 ClearAndPumpLoop(); | 99 ClearAndPumpLoop(); |
| 99 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 100 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 100 | 101 |
| 101 // Write the second one. | 102 // Write the second one. |
| 102 some_attachments.clear(); | 103 some_attachments.clear(); |
| 103 some_attachments.push_back(attachment2); | 104 some_attachments.push_back(attachment2); |
| 104 store.Write(some_attachments, write_callback); | 105 store->Write(some_attachments, write_callback); |
| 105 ClearAndPumpLoop(); | 106 ClearAndPumpLoop(); |
| 106 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 107 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 107 | 108 |
| 108 // Read it back and see that it was not overwritten. | 109 // Read it back and see that it was not overwritten. |
| 109 AttachmentIdList some_attachment_ids; | 110 AttachmentIdList some_attachment_ids; |
| 110 some_attachment_ids.push_back(attachment1.GetId()); | 111 some_attachment_ids.push_back(attachment1.GetId()); |
| 111 store.Read(some_attachment_ids, read_callback); | 112 store->Read(some_attachment_ids, read_callback); |
| 112 ClearAndPumpLoop(); | 113 ClearAndPumpLoop(); |
| 113 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 114 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 114 EXPECT_EQ(attachments->size(), 1U); | 115 EXPECT_EQ(attachments->size(), 1U); |
| 115 EXPECT_EQ(failed_attachment_ids->size(), 0U); | 116 EXPECT_EQ(failed_attachment_ids->size(), 0U); |
| 116 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | 117 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); |
| 117 EXPECT_TRUE(a1 != attachments->end()); | 118 EXPECT_TRUE(a1 != attachments->end()); |
| 118 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 119 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
| 119 } | 120 } |
| 120 | 121 |
| 121 // Verify that we can write some attachments and read them back. | 122 // Verify that we can write some attachments and read them back. |
| 122 TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) { | 123 TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) { |
| 123 Attachment attachment1 = Attachment::Create(some_data1); | 124 Attachment attachment1 = Attachment::Create(some_data1); |
| 124 Attachment attachment2 = Attachment::Create(some_data2); | 125 Attachment attachment2 = Attachment::Create(some_data2); |
| 125 AttachmentList some_attachments; | 126 AttachmentList some_attachments; |
| 126 some_attachments.push_back(attachment1); | 127 some_attachments.push_back(attachment1); |
| 127 some_attachments.push_back(attachment2); | 128 some_attachments.push_back(attachment2); |
| 128 | 129 |
| 129 store.Write(some_attachments, write_callback); | 130 store->Write(some_attachments, write_callback); |
| 130 ClearAndPumpLoop(); | 131 ClearAndPumpLoop(); |
| 131 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 132 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 132 | 133 |
| 133 AttachmentIdList some_attachment_ids; | 134 AttachmentIdList some_attachment_ids; |
| 134 some_attachment_ids.push_back(attachment1.GetId()); | 135 some_attachment_ids.push_back(attachment1.GetId()); |
| 135 some_attachment_ids.push_back(attachment2.GetId()); | 136 some_attachment_ids.push_back(attachment2.GetId()); |
| 136 store.Read(some_attachment_ids, read_callback); | 137 store->Read(some_attachment_ids, read_callback); |
| 137 ClearAndPumpLoop(); | 138 ClearAndPumpLoop(); |
| 138 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 139 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 139 EXPECT_EQ(attachments->size(), 2U); | 140 EXPECT_EQ(attachments->size(), 2U); |
| 140 EXPECT_EQ(failed_attachment_ids->size(), 0U); | 141 EXPECT_EQ(failed_attachment_ids->size(), 0U); |
| 141 | 142 |
| 142 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); | 143 AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId()); |
| 143 EXPECT_TRUE(a1 != attachments->end()); | 144 EXPECT_TRUE(a1 != attachments->end()); |
| 144 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); | 145 EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData())); |
| 145 | 146 |
| 146 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); | 147 AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId()); |
| 147 EXPECT_TRUE(a2 != attachments->end()); | 148 EXPECT_TRUE(a2 != attachments->end()); |
| 148 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); | 149 EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData())); |
| 149 } | 150 } |
| 150 | 151 |
| 151 // Try to read two attachments when only one exists. | 152 // Try to read two attachments when only one exists. |
| 152 TEST_F(FakeAttachmentStoreTest, Read_OneNotFound) { | 153 TEST_F(FakeAttachmentStoreTest, Read_OneNotFound) { |
| 153 Attachment attachment1 = Attachment::Create(some_data1); | 154 Attachment attachment1 = Attachment::Create(some_data1); |
| 154 Attachment attachment2 = Attachment::Create(some_data2); | 155 Attachment attachment2 = Attachment::Create(some_data2); |
| 155 | 156 |
| 156 AttachmentList some_attachments; | 157 AttachmentList some_attachments; |
| 157 // Write attachment1 only. | 158 // Write attachment1 only. |
| 158 some_attachments.push_back(attachment1); | 159 some_attachments.push_back(attachment1); |
| 159 store.Write(some_attachments, write_callback); | 160 store->Write(some_attachments, write_callback); |
| 160 ClearAndPumpLoop(); | 161 ClearAndPumpLoop(); |
| 161 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 162 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 162 | 163 |
| 163 // Try to read both attachment1 and attachment2. | 164 // Try to read both attachment1 and attachment2. |
| 164 AttachmentIdList ids; | 165 AttachmentIdList ids; |
| 165 ids.push_back(attachment1.GetId()); | 166 ids.push_back(attachment1.GetId()); |
| 166 ids.push_back(attachment2.GetId()); | 167 ids.push_back(attachment2.GetId()); |
| 167 store.Read(ids, read_callback); | 168 store->Read(ids, read_callback); |
| 168 ClearAndPumpLoop(); | 169 ClearAndPumpLoop(); |
| 169 | 170 |
| 170 // See that only attachment1 was read. | 171 // See that only attachment1 was read. |
| 171 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 172 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 172 EXPECT_EQ(attachments->size(), 1U); | 173 EXPECT_EQ(attachments->size(), 1U); |
| 173 EXPECT_EQ(failed_attachment_ids->size(), 1U); | 174 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 174 } | 175 } |
| 175 | 176 |
| 176 // Try to drop two attachments when only one exists. Verify that no error occurs | 177 // Try to drop two attachments when only one exists. Verify that no error occurs |
| 177 // and that the existing attachment was dropped. | 178 // and that the existing attachment was dropped. |
| 178 TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { | 179 TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) { |
| 179 // First, create two attachments. | 180 // First, create two attachments. |
| 180 Attachment attachment1 = Attachment::Create(some_data1); | 181 Attachment attachment1 = Attachment::Create(some_data1); |
| 181 Attachment attachment2 = Attachment::Create(some_data2); | 182 Attachment attachment2 = Attachment::Create(some_data2); |
| 182 AttachmentList some_attachments; | 183 AttachmentList some_attachments; |
| 183 some_attachments.push_back(attachment1); | 184 some_attachments.push_back(attachment1); |
| 184 some_attachments.push_back(attachment2); | 185 some_attachments.push_back(attachment2); |
| 185 store.Write(some_attachments, write_callback); | 186 store->Write(some_attachments, write_callback); |
| 186 ClearAndPumpLoop(); | 187 ClearAndPumpLoop(); |
| 187 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 188 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 188 | 189 |
| 189 // Drop attachment1 only. | 190 // Drop attachment1 only. |
| 190 AttachmentIdList ids; | 191 AttachmentIdList ids; |
| 191 ids.push_back(attachment1.GetId()); | 192 ids.push_back(attachment1.GetId()); |
| 192 store.Drop(ids, drop_callback); | 193 store->Drop(ids, drop_callback); |
| 193 ClearAndPumpLoop(); | 194 ClearAndPumpLoop(); |
| 194 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 195 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 195 | 196 |
| 196 // See that attachment1 is gone. | 197 // See that attachment1 is gone. |
| 197 store.Read(ids, read_callback); | 198 store->Read(ids, read_callback); |
| 198 ClearAndPumpLoop(); | 199 ClearAndPumpLoop(); |
| 199 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 200 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 200 EXPECT_EQ(attachments->size(), 0U); | 201 EXPECT_EQ(attachments->size(), 0U); |
| 201 EXPECT_EQ(failed_attachment_ids->size(), 1U); | 202 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 202 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); | 203 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); |
| 203 | 204 |
| 204 // Drop both attachment1 and attachment2. | 205 // Drop both attachment1 and attachment2. |
| 205 ids.clear(); | 206 ids.clear(); |
| 206 ids.push_back(attachment1.GetId()); | 207 ids.push_back(attachment1.GetId()); |
| 207 ids.push_back(attachment2.GetId()); | 208 ids.push_back(attachment2.GetId()); |
| 208 store.Drop(ids, drop_callback); | 209 store->Drop(ids, drop_callback); |
| 209 ClearAndPumpLoop(); | 210 ClearAndPumpLoop(); |
| 210 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 211 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 211 | 212 |
| 212 // See that attachment2 is now gone. | 213 // See that attachment2 is now gone. |
| 213 ids.clear(); | 214 ids.clear(); |
| 214 ids.push_back(attachment2.GetId()); | 215 ids.push_back(attachment2.GetId()); |
| 215 store.Read(ids, read_callback); | 216 store->Read(ids, read_callback); |
| 216 ClearAndPumpLoop(); | 217 ClearAndPumpLoop(); |
| 217 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 218 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 218 EXPECT_EQ(attachments->size(), 0U); | 219 EXPECT_EQ(attachments->size(), 0U); |
| 219 EXPECT_EQ(failed_attachment_ids->size(), 1U); | 220 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 220 EXPECT_EQ((*failed_attachment_ids)[0], attachment2.GetId()); | 221 EXPECT_EQ((*failed_attachment_ids)[0], attachment2.GetId()); |
| 221 } | 222 } |
| 222 | 223 |
| 223 // Verify that attempting to drop an attachment that does not exist is not an | 224 // Verify that attempting to drop an attachment that does not exist is not an |
| 224 // error. | 225 // error. |
| 225 TEST_F(FakeAttachmentStoreTest, Drop_DoesNotExist) { | 226 TEST_F(FakeAttachmentStoreTest, Drop_DoesNotExist) { |
| 226 Attachment attachment1 = Attachment::Create(some_data1); | 227 Attachment attachment1 = Attachment::Create(some_data1); |
| 227 AttachmentList some_attachments; | 228 AttachmentList some_attachments; |
| 228 some_attachments.push_back(attachment1); | 229 some_attachments.push_back(attachment1); |
| 229 store.Write(some_attachments, write_callback); | 230 store->Write(some_attachments, write_callback); |
| 230 ClearAndPumpLoop(); | 231 ClearAndPumpLoop(); |
| 231 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 232 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 232 | 233 |
| 233 // Drop the attachment. | 234 // Drop the attachment. |
| 234 AttachmentIdList ids; | 235 AttachmentIdList ids; |
| 235 ids.push_back(attachment1.GetId()); | 236 ids.push_back(attachment1.GetId()); |
| 236 store.Drop(ids, drop_callback); | 237 store->Drop(ids, drop_callback); |
| 237 ClearAndPumpLoop(); | 238 ClearAndPumpLoop(); |
| 238 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 239 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 239 | 240 |
| 240 // See that it's gone. | 241 // See that it's gone. |
| 241 store.Read(ids, read_callback); | 242 store->Read(ids, read_callback); |
| 242 ClearAndPumpLoop(); | 243 ClearAndPumpLoop(); |
| 243 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 244 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 244 EXPECT_EQ(attachments->size(), 0U); | 245 EXPECT_EQ(attachments->size(), 0U); |
| 245 EXPECT_EQ(failed_attachment_ids->size(), 1U); | 246 EXPECT_EQ(failed_attachment_ids->size(), 1U); |
| 246 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); | 247 EXPECT_EQ((*failed_attachment_ids)[0], attachment1.GetId()); |
| 247 | 248 |
| 248 // Drop again, see that no error occurs. | 249 // Drop again, see that no error occurs. |
| 249 ids.clear(); | 250 ids.clear(); |
| 250 ids.push_back(attachment1.GetId()); | 251 ids.push_back(attachment1.GetId()); |
| 251 store.Drop(ids, drop_callback); | 252 store->Drop(ids, drop_callback); |
| 252 ClearAndPumpLoop(); | 253 ClearAndPumpLoop(); |
| 253 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 254 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 254 } | 255 } |
| 255 | 256 |
| 256 } // namespace syncer | 257 } // namespace syncer |
| OLD | NEW |