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.
5 #include "sync/internal_api/public/attachments/attachment_store_frontend.h"
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "sync/api/attachments/attachment.h"
15 #include "sync/api/attachments/attachment_id.h"
16 #include "sync/api/attachments/attachment_store_backend.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 class MockAttachmentStore
: public AttachmentStoreBackend
{
25 MockAttachmentStore(const base::Closure
& init_called
,
26 const base::Closure
& read_called
,
27 const base::Closure
& write_called
,
28 const base::Closure
& set_reference_called
,
29 const base::Closure
& drop_reference_called
,
30 const base::Closure
& read_metadata_by_id_called
,
31 const base::Closure
& read_metadata_called
,
32 const base::Closure
& dtor_called
)
33 : AttachmentStoreBackend(nullptr),
34 init_called_(init_called
),
35 read_called_(read_called
),
36 write_called_(write_called
),
37 set_reference_called_(set_reference_called
),
38 drop_reference_called_(drop_reference_called
),
39 read_metadata_by_id_called_(read_metadata_by_id_called
),
40 read_metadata_called_(read_metadata_called
),
41 dtor_called_(dtor_called
) {}
43 ~MockAttachmentStore() override
{ dtor_called_
.Run(); }
45 void Init(const AttachmentStore::InitCallback
& callback
) override
{
49 void Read(AttachmentStore::Component component
,
50 const AttachmentIdList
& ids
,
51 const AttachmentStore::ReadCallback
& callback
) override
{
55 void Write(AttachmentStore::Component component
,
56 const AttachmentList
& attachments
,
57 const AttachmentStore::WriteCallback
& callback
) override
{
61 void SetReference(AttachmentStore::Component component
,
62 const AttachmentIdList
& ids
) override
{
63 set_reference_called_
.Run();
66 void DropReference(AttachmentStore::Component component
,
67 const AttachmentIdList
& ids
,
68 const AttachmentStore::DropCallback
& callback
) override
{
69 drop_reference_called_
.Run();
72 void ReadMetadataById(
73 AttachmentStore::Component component
,
74 const AttachmentIdList
& ids
,
75 const AttachmentStore::ReadMetadataCallback
& callback
) override
{
76 read_metadata_by_id_called_
.Run();
80 AttachmentStore::Component component
,
81 const AttachmentStore::ReadMetadataCallback
& callback
) override
{
82 read_metadata_called_
.Run();
85 base::Closure init_called_
;
86 base::Closure read_called_
;
87 base::Closure write_called_
;
88 base::Closure set_reference_called_
;
89 base::Closure drop_reference_called_
;
90 base::Closure read_metadata_by_id_called_
;
91 base::Closure read_metadata_called_
;
92 base::Closure dtor_called_
;
97 class AttachmentStoreFrontendTest
: public testing::Test
{
99 AttachmentStoreFrontendTest()
100 : init_call_count_(0),
102 write_call_count_(0),
103 add_component_call_count_(0),
105 read_metadata_by_id_call_count_(0),
106 read_metadata_call_count_(0),
107 dtor_call_count_(0) {}
109 void SetUp() override
{
110 scoped_ptr
<AttachmentStoreBackend
> backend(new MockAttachmentStore(
111 base::Bind(&AttachmentStoreFrontendTest::InitCalled
,
112 base::Unretained(this)),
113 base::Bind(&AttachmentStoreFrontendTest::ReadCalled
,
114 base::Unretained(this)),
115 base::Bind(&AttachmentStoreFrontendTest::WriteCalled
,
116 base::Unretained(this)),
117 base::Bind(&AttachmentStoreFrontendTest::SetReferenceCalled
,
118 base::Unretained(this)),
119 base::Bind(&AttachmentStoreFrontendTest::DropReferenceCalled
,
120 base::Unretained(this)),
121 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataByIdCalled
,
122 base::Unretained(this)),
123 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataCalled
,
124 base::Unretained(this)),
125 base::Bind(&AttachmentStoreFrontendTest::DtorCalled
,
126 base::Unretained(this))));
127 attachment_store_frontend_
= new AttachmentStoreFrontend(
128 backend
.Pass(), base::ThreadTaskRunnerHandle::Get());
131 static void DoneWithResult(const AttachmentStore::Result
& result
) {
135 static void ReadDone(const AttachmentStore::Result
& result
,
136 scoped_ptr
<AttachmentMap
> attachments
,
137 scoped_ptr
<AttachmentIdList
> unavailable_attachments
) {
141 static void ReadMetadataDone(const AttachmentStore::Result
& result
,
142 scoped_ptr
<AttachmentMetadataList
> metadata
) {
146 void InitCalled() { ++init_call_count_
; }
148 void ReadCalled() { ++read_call_count_
; }
150 void WriteCalled() { ++write_call_count_
; }
152 void SetReferenceCalled() { ++add_component_call_count_
; }
154 void DropReferenceCalled() { ++drop_call_count_
; }
156 void ReadMetadataByIdCalled() { ++read_metadata_by_id_call_count_
; }
158 void ReadMetadataCalled() { ++read_metadata_call_count_
; }
160 void DtorCalled() { ++dtor_call_count_
; }
162 void RunMessageLoop() {
163 base::RunLoop run_loop
;
164 run_loop
.RunUntilIdle();
167 base::MessageLoop message_loop_
;
168 scoped_refptr
<AttachmentStoreFrontend
> attachment_store_frontend_
;
169 int init_call_count_
;
170 int read_call_count_
;
171 int write_call_count_
;
172 int add_component_call_count_
;
173 int drop_call_count_
;
174 int read_metadata_by_id_call_count_
;
175 int read_metadata_call_count_
;
176 int dtor_call_count_
;
179 // Test that method calls are forwarded to backend loop
180 TEST_F(AttachmentStoreFrontendTest
, MethodsCalled
) {
181 AttachmentIdList id_list
;
182 AttachmentList attachments
;
184 attachment_store_frontend_
->Init(
185 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult
));
186 EXPECT_EQ(init_call_count_
, 0);
188 EXPECT_EQ(init_call_count_
, 1);
190 attachment_store_frontend_
->Read(
191 AttachmentStore::SYNC
, id_list
,
192 base::Bind(&AttachmentStoreFrontendTest::ReadDone
));
193 EXPECT_EQ(read_call_count_
, 0);
195 EXPECT_EQ(read_call_count_
, 1);
197 attachment_store_frontend_
->Write(
198 AttachmentStore::SYNC
, attachments
,
199 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult
));
200 EXPECT_EQ(write_call_count_
, 0);
202 EXPECT_EQ(write_call_count_
, 1);
204 attachment_store_frontend_
->SetReference(AttachmentStore::SYNC
, id_list
);
206 attachment_store_frontend_
->DropReference(
207 AttachmentStore::SYNC
, id_list
,
208 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult
));
209 EXPECT_EQ(drop_call_count_
, 0);
211 EXPECT_EQ(drop_call_count_
, 1);
213 attachment_store_frontend_
->ReadMetadataById(
214 AttachmentStore::SYNC
, id_list
,
215 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone
));
216 EXPECT_EQ(read_metadata_by_id_call_count_
, 0);
218 EXPECT_EQ(read_metadata_by_id_call_count_
, 1);
220 attachment_store_frontend_
->ReadMetadata(
221 AttachmentStore::SYNC
,
222 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone
));
223 EXPECT_EQ(read_metadata_call_count_
, 0);
225 EXPECT_EQ(read_metadata_call_count_
, 1);
227 // Releasing referehce to AttachmentStoreFrontend should result in
228 // MockAttachmentStore being deleted on backend loop.
229 attachment_store_frontend_
= NULL
;
230 EXPECT_EQ(dtor_call_count_
, 0);
232 EXPECT_EQ(dtor_call_count_
, 1);
235 } // namespace syncer