fake_attachment_store_unittest.cc revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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 3// found in the LICENSE file. 4 5#include "sync/api/attachments/fake_attachment_store.h" 6 7#include "base/bind.h" 8#include "base/memory/ref_counted_memory.h" 9#include "base/memory/scoped_ptr.h" 10#include "base/message_loop/message_loop.h" 11#include "sync/api/attachments/attachment.h" 12#include "sync/protocol/sync.pb.h" 13#include "testing/gtest/include/gtest/gtest.h" 14 15namespace syncer { 16 17const char kTestData[] = "some data"; 18 19class FakeAttachmentStoreTest : public testing::Test { 20 protected: 21 FakeAttachmentStoreTest() : id(AttachmentId::Create()) {} 22 23 virtual void SetUp() { 24 Clear(); 25 read_callback = 26 base::Bind(&FakeAttachmentStoreTest::CopyAttachmentAndResult, 27 base::Unretained(this), 28 &result, 29 &attachment); 30 write_callback = base::Bind(&FakeAttachmentStoreTest::CopyResultAndId, 31 base::Unretained(this), 32 &result, 33 &id); 34 drop_callback = base::Bind( 35 &FakeAttachmentStoreTest::CopyResult, base::Unretained(this), &result); 36 } 37 38 virtual void ClearAndPumpLoop() { 39 Clear(); 40 message_loop_.RunUntilIdle(); 41 } 42 43 AttachmentStore::Result result; 44 AttachmentId id; 45 scoped_ptr<Attachment> attachment; 46 47 AttachmentStore::ReadCallback read_callback; 48 AttachmentStore::WriteCallback write_callback; 49 AttachmentStore::DropCallback drop_callback; 50 51 private: 52 void Clear() { 53 result = AttachmentStore::UNSPECIFIED_ERROR; 54 attachment.reset(); 55 } 56 57 void CopyResult(AttachmentStore::Result* destination, 58 const AttachmentStore::Result& source) { 59 *destination = source; 60 } 61 62 void CopyResultAndId(AttachmentStore::Result* destination_result, 63 AttachmentId* destination_id, 64 const AttachmentStore::Result& source_result, 65 const AttachmentId& source_id) { 66 CopyResult(destination_result, source_result); 67 *destination_id = source_id; 68 } 69 70 void CopyAttachmentAndResult(AttachmentStore::Result* destination_result, 71 scoped_ptr<Attachment>* destination_attachment, 72 const AttachmentStore::Result& source_result, 73 scoped_ptr<Attachment> source_attachment) { 74 CopyResult(destination_result, source_result); 75 *destination_attachment = source_attachment.Pass(); 76 } 77 78 base::MessageLoop message_loop_; 79}; 80 81TEST_F(FakeAttachmentStoreTest, WriteReadRoundTrip) { 82 FakeAttachmentStore store(base::MessageLoopProxy::current()); 83 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); 84 some_data->data() = kTestData; 85 86 store.Write(some_data, write_callback); 87 ClearAndPumpLoop(); 88 EXPECT_EQ(result, AttachmentStore::SUCCESS); 89 AttachmentId id_written(id); 90 91 store.Read(id_written, read_callback); 92 ClearAndPumpLoop(); 93 EXPECT_EQ(result, AttachmentStore::SUCCESS); 94 EXPECT_EQ(id_written, attachment->GetId()); 95 EXPECT_EQ(some_data, attachment->GetData()); 96} 97 98TEST_F(FakeAttachmentStoreTest, Read_NotFound) { 99 FakeAttachmentStore store(base::MessageLoopProxy::current()); 100 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); 101 Attachment some_attachment = Attachment::Create(some_data); 102 AttachmentId some_id = some_attachment.GetId(); 103 store.Read(some_id, read_callback); 104 ClearAndPumpLoop(); 105 EXPECT_EQ(result, AttachmentStore::NOT_FOUND); 106 EXPECT_EQ(NULL, attachment.get()); 107} 108 109TEST_F(FakeAttachmentStoreTest, Drop) { 110 FakeAttachmentStore store(base::MessageLoopProxy::current()); 111 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); 112 some_data->data() = kTestData; 113 store.Write(some_data, write_callback); 114 ClearAndPumpLoop(); 115 EXPECT_EQ(result, AttachmentStore::SUCCESS); 116 AttachmentId id_written(id); 117 118 // First drop. 119 store.Drop(id_written, drop_callback); 120 ClearAndPumpLoop(); 121 EXPECT_EQ(result, AttachmentStore::SUCCESS); 122 123 store.Read(id_written, read_callback); 124 ClearAndPumpLoop(); 125 EXPECT_EQ(result, AttachmentStore::NOT_FOUND); 126 127 // Second drop. 128 store.Drop(id_written, drop_callback); 129 ClearAndPumpLoop(); 130 EXPECT_EQ(result, AttachmentStore::NOT_FOUND); 131 132 store.Read(id_written, read_callback); 133 ClearAndPumpLoop(); 134 EXPECT_EQ(result, AttachmentStore::NOT_FOUND); 135} 136 137} // namespace syncer 138