namespace syncer {
-const char kTestData[] = "some data";
+const char kTestData1[] = "test data 1";
+const char kTestData2[] = "test data 2";
class FakeAttachmentStoreTest : public testing::Test {
protected:
- FakeAttachmentStoreTest() : id(AttachmentId::Create()) {}
+ base::MessageLoop message_loop;
+ FakeAttachmentStore store;
+ AttachmentStore::Result result;
+ scoped_ptr<AttachmentMap> attachments;
+
+ AttachmentStore::ReadCallback read_callback;
+ AttachmentStore::WriteCallback write_callback;
+ AttachmentStore::DropCallback drop_callback;
+
+ scoped_refptr<base::RefCountedString> some_data1;
+ scoped_refptr<base::RefCountedString> some_data2;
+
+ FakeAttachmentStoreTest() : store(base::MessageLoopProxy::current()) {}
virtual void SetUp() {
Clear();
- read_callback =
- base::Bind(&FakeAttachmentStoreTest::CopyAttachmentAndResult,
- base::Unretained(this),
- &result,
- &attachment);
- write_callback = base::Bind(&FakeAttachmentStoreTest::CopyResultAndId,
- base::Unretained(this),
- &result,
- &id);
- drop_callback = base::Bind(
+ read_callback = base::Bind(&FakeAttachmentStoreTest::CopyResultAttachments,
+ base::Unretained(this),
+ &result,
+ &attachments);
+ write_callback = base::Bind(
&FakeAttachmentStoreTest::CopyResult, base::Unretained(this), &result);
+ drop_callback = write_callback;
+
+ some_data1 = new base::RefCountedString;
+ some_data1->data() = kTestData1;
+
+ some_data2 = new base::RefCountedString;
+ some_data2->data() = kTestData2;
}
virtual void ClearAndPumpLoop() {
Clear();
- message_loop_.RunUntilIdle();
+ message_loop.RunUntilIdle();
}
- AttachmentStore::Result result;
- AttachmentId id;
- scoped_ptr<Attachment> attachment;
-
- AttachmentStore::ReadCallback read_callback;
- AttachmentStore::WriteCallback write_callback;
- AttachmentStore::DropCallback drop_callback;
-
private:
void Clear() {
result = AttachmentStore::UNSPECIFIED_ERROR;
- attachment.reset();
+ attachments.reset();
}
- void CopyResult(AttachmentStore::Result* destination,
- const AttachmentStore::Result& source) {
- *destination = source;
+ void CopyResult(AttachmentStore::Result* destination_result,
+ const AttachmentStore::Result& source_result) {
+ *destination_result = source_result;
}
- void CopyResultAndId(AttachmentStore::Result* destination_result,
- AttachmentId* destination_id,
- const AttachmentStore::Result& source_result,
- const AttachmentId& source_id) {
+ void CopyResultAttachments(AttachmentStore::Result* destination_result,
+ scoped_ptr<AttachmentMap>* destination_attachments,
+ const AttachmentStore::Result& source_result,
+ scoped_ptr<AttachmentMap> source_attachments) {
CopyResult(destination_result, source_result);
- *destination_id = source_id;
+ *destination_attachments = source_attachments.Pass();
}
+};
- void CopyAttachmentAndResult(AttachmentStore::Result* destination_result,
- scoped_ptr<Attachment>* destination_attachment,
- const AttachmentStore::Result& source_result,
- scoped_ptr<Attachment> source_attachment) {
- CopyResult(destination_result, source_result);
- *destination_attachment = source_attachment.Pass();
- }
+// Verify that we do not overwrite existing attachments and that we do not treat
+// it as an error.
+TEST_F(FakeAttachmentStoreTest, Write_NoOverwriteNoError) {
+ // Create two attachments with the same id but different data.
+ Attachment attachment1 = Attachment::Create(some_data1);
+ Attachment attachment2 =
+ Attachment::CreateWithId(attachment1.GetId(), some_data2);
+
+ // Write the first one.
+ AttachmentList some_attachments;
+ some_attachments.push_back(attachment1);
+ store.Write(some_attachments, write_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
- base::MessageLoop message_loop_;
-};
+ // Write the second one.
+ some_attachments.clear();
+ some_attachments.push_back(attachment2);
+ store.Write(some_attachments, write_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
-TEST_F(FakeAttachmentStoreTest, WriteReadRoundTrip) {
- FakeAttachmentStore store(base::MessageLoopProxy::current());
- scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
- some_data->data() = kTestData;
+ // Read it back and see that it was not overwritten.
+ AttachmentIdList some_attachment_ids;
+ some_attachment_ids.push_back(attachment1.GetId());
+ store.Read(some_attachment_ids, read_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
+ EXPECT_EQ(attachments->size(), 1U);
+ AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId());
+ EXPECT_TRUE(a1 != attachments->end());
+ EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData()));
+}
- store.Write(some_data, write_callback);
+// Verify that we can write some attachments and read them back.
+TEST_F(FakeAttachmentStoreTest, Write_RoundTrip) {
+ Attachment attachment1 = Attachment::Create(some_data1);
+ Attachment attachment2 = Attachment::Create(some_data2);
+ AttachmentList some_attachments;
+ some_attachments.push_back(attachment1);
+ some_attachments.push_back(attachment2);
+
+ store.Write(some_attachments, write_callback);
ClearAndPumpLoop();
EXPECT_EQ(result, AttachmentStore::SUCCESS);
- AttachmentId id_written(id);
- store.Read(id_written, read_callback);
+ AttachmentIdList some_attachment_ids;
+ some_attachment_ids.push_back(attachment1.GetId());
+ some_attachment_ids.push_back(attachment2.GetId());
+ store.Read(some_attachment_ids, read_callback);
ClearAndPumpLoop();
EXPECT_EQ(result, AttachmentStore::SUCCESS);
- EXPECT_EQ(id_written, attachment->GetId());
- EXPECT_EQ(some_data, attachment->GetData());
+ EXPECT_EQ(attachments->size(), 2U);
+
+ AttachmentMap::const_iterator a1 = attachments->find(attachment1.GetId());
+ EXPECT_TRUE(a1 != attachments->end());
+ EXPECT_TRUE(attachment1.GetData()->Equals(a1->second.GetData()));
+
+ AttachmentMap::const_iterator a2 = attachments->find(attachment2.GetId());
+ EXPECT_TRUE(a2 != attachments->end());
+ EXPECT_TRUE(attachment2.GetData()->Equals(a2->second.GetData()));
}
-TEST_F(FakeAttachmentStoreTest, Read_NotFound) {
- FakeAttachmentStore store(base::MessageLoopProxy::current());
- scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
- scoped_ptr<Attachment> some_attachment = Attachment::Create(some_data);
- AttachmentId some_id = some_attachment->GetId();
- store.Read(some_id, read_callback);
+// Try to read two attachments when only one exists.
+TEST_F(FakeAttachmentStoreTest, Read_OneNotFound) {
+ Attachment attachment1 = Attachment::Create(some_data1);
+ Attachment attachment2 = Attachment::Create(some_data2);
+
+ AttachmentList some_attachments;
+ // Write attachment1 only.
+ some_attachments.push_back(attachment1);
+ store.Write(some_attachments, write_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
+
+ // Try to read both attachment1 and attachment2.
+ AttachmentIdList ids;
+ ids.push_back(attachment1.GetId());
+ ids.push_back(attachment2.GetId());
+ store.Read(ids, read_callback);
ClearAndPumpLoop();
- EXPECT_EQ(result, AttachmentStore::NOT_FOUND);
- EXPECT_EQ(NULL, attachment.get());
+
+ // See that only attachment1 was read.
+ EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR);
+ EXPECT_EQ(attachments->size(), 1U);
}
-TEST_F(FakeAttachmentStoreTest, Drop) {
- FakeAttachmentStore store(base::MessageLoopProxy::current());
- scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
- some_data->data() = kTestData;
- store.Write(some_data, write_callback);
+// Try to drop two attachments when only one exists. Verify that no error occurs
+// and that the existing attachment was dropped.
+TEST_F(FakeAttachmentStoreTest, Drop_DropTwoButOnlyOneExists) {
+ // First, create two attachments.
+ Attachment attachment1 = Attachment::Create(some_data1);
+ Attachment attachment2 = Attachment::Create(some_data2);
+ AttachmentList some_attachments;
+ some_attachments.push_back(attachment1);
+ some_attachments.push_back(attachment2);
+ store.Write(some_attachments, write_callback);
ClearAndPumpLoop();
EXPECT_EQ(result, AttachmentStore::SUCCESS);
- AttachmentId id_written(id);
- // First drop.
- store.Drop(id_written, drop_callback);
+ // Drop attachment1 only.
+ AttachmentIdList ids;
+ ids.push_back(attachment1.GetId());
+ store.Drop(ids, drop_callback);
ClearAndPumpLoop();
EXPECT_EQ(result, AttachmentStore::SUCCESS);
- store.Read(id_written, read_callback);
+ // See that attachment1 is gone.
+ store.Read(ids, read_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR);
+ EXPECT_EQ(attachments->size(), 0U);
+
+ // Drop both attachment1 and attachment2.
+ ids.clear();
+ ids.push_back(attachment1.GetId());
+ ids.push_back(attachment2.GetId());
+ store.Drop(ids, drop_callback);
ClearAndPumpLoop();
- EXPECT_EQ(result, AttachmentStore::NOT_FOUND);
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
+
+ // See that attachment2 is now gone.
+ ids.clear();
+ ids.push_back(attachment2.GetId());
+ store.Read(ids, read_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR);
+ EXPECT_EQ(attachments->size(), 0U);
+}
- // Second drop.
- store.Drop(id_written, drop_callback);
+// Verify that attempting to drop an attachment that does not exist is not an
+// error.
+TEST_F(FakeAttachmentStoreTest, Drop_DoesNotExist) {
+ Attachment attachment1 = Attachment::Create(some_data1);
+ AttachmentList some_attachments;
+ some_attachments.push_back(attachment1);
+ store.Write(some_attachments, write_callback);
ClearAndPumpLoop();
- EXPECT_EQ(result, AttachmentStore::NOT_FOUND);
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
- store.Read(id_written, read_callback);
+ // Drop the attachment.
+ AttachmentIdList ids;
+ ids.push_back(attachment1.GetId());
+ store.Drop(ids, drop_callback);
ClearAndPumpLoop();
- EXPECT_EQ(result, AttachmentStore::NOT_FOUND);
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
+
+ // See that it's gone.
+ store.Read(ids, read_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR);
+ EXPECT_EQ(attachments->size(), 0U);
+
+ // Drop again, see that no error occurs.
+ ids.clear();
+ ids.push_back(attachment1.GetId());
+ store.Drop(ids, drop_callback);
+ ClearAndPumpLoop();
+ EXPECT_EQ(result, AttachmentStore::SUCCESS);
}
} // namespace syncer