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_called
,
31 const base::Closure
& read_all_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_called_(read_metadata_called
),
40 read_all_metadata_called_(read_all_metadata_called
),
41 dtor_called_(dtor_called
) {}
43 ~MockAttachmentStore() override
{ dtor_called_
.Run(); }
45 void Init(const AttachmentStore::InitCallback
& callback
) override
{
49 void Read(const AttachmentIdList
& ids
,
50 const AttachmentStore::ReadCallback
& callback
) override
{
54 void Write(AttachmentStore::Component component
,
55 const AttachmentList
& attachments
,
56 const AttachmentStore::WriteCallback
& callback
) override
{
60 void SetReference(AttachmentStore::Component component
,
61 const AttachmentIdList
& ids
) override
{
62 set_reference_called_
.Run();
65 void DropReference(AttachmentStore::Component component
,
66 const AttachmentIdList
& ids
,
67 const AttachmentStore::DropCallback
& callback
) override
{
68 drop_reference_called_
.Run();
72 const AttachmentIdList
& ids
,
73 const AttachmentStore::ReadMetadataCallback
& callback
) override
{
74 read_metadata_called_
.Run();
78 AttachmentStore::Component component
,
79 const AttachmentStore::ReadMetadataCallback
& callback
) override
{
80 read_all_metadata_called_
.Run();
83 base::Closure init_called_
;
84 base::Closure read_called_
;
85 base::Closure write_called_
;
86 base::Closure set_reference_called_
;
87 base::Closure drop_reference_called_
;
88 base::Closure read_metadata_called_
;
89 base::Closure read_all_metadata_called_
;
90 base::Closure dtor_called_
;
95 class AttachmentStoreFrontendTest
: public testing::Test
{
97 AttachmentStoreFrontendTest()
98 : init_call_count_(0),
100 write_call_count_(0),
101 add_component_call_count_(0),
103 read_metadata_call_count_(0),
104 read_all_metadata_call_count_(0),
105 dtor_call_count_(0) {}
107 void SetUp() override
{
108 scoped_ptr
<AttachmentStoreBackend
> backend(new MockAttachmentStore(
109 base::Bind(&AttachmentStoreFrontendTest::InitCalled
,
110 base::Unretained(this)),
111 base::Bind(&AttachmentStoreFrontendTest::ReadCalled
,
112 base::Unretained(this)),
113 base::Bind(&AttachmentStoreFrontendTest::WriteCalled
,
114 base::Unretained(this)),
115 base::Bind(&AttachmentStoreFrontendTest::SetReferenceCalled
,
116 base::Unretained(this)),
117 base::Bind(&AttachmentStoreFrontendTest::DropReferenceCalled
,
118 base::Unretained(this)),
119 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataCalled
,
120 base::Unretained(this)),
121 base::Bind(&AttachmentStoreFrontendTest::ReadAllMetadataCalled
,
122 base::Unretained(this)),
123 base::Bind(&AttachmentStoreFrontendTest::DtorCalled
,
124 base::Unretained(this))));
125 attachment_store_frontend_
= new AttachmentStoreFrontend(
126 backend
.Pass(), base::ThreadTaskRunnerHandle::Get());
129 static void DoneWithResult(const AttachmentStore::Result
& result
) {
133 static void ReadDone(const AttachmentStore::Result
& result
,
134 scoped_ptr
<AttachmentMap
> attachments
,
135 scoped_ptr
<AttachmentIdList
> unavailable_attachments
) {
139 static void ReadMetadataDone(const AttachmentStore::Result
& result
,
140 scoped_ptr
<AttachmentMetadataList
> metadata
) {
144 void InitCalled() { ++init_call_count_
; }
146 void ReadCalled() { ++read_call_count_
; }
148 void WriteCalled() { ++write_call_count_
; }
150 void SetReferenceCalled() { ++add_component_call_count_
; }
152 void DropReferenceCalled() { ++drop_call_count_
; }
154 void ReadMetadataCalled() { ++read_metadata_call_count_
; }
156 void ReadAllMetadataCalled() { ++read_all_metadata_call_count_
; }
158 void DtorCalled() { ++dtor_call_count_
; }
160 void RunMessageLoop() {
161 base::RunLoop run_loop
;
162 run_loop
.RunUntilIdle();
165 base::MessageLoop message_loop_
;
166 scoped_refptr
<AttachmentStoreFrontend
> attachment_store_frontend_
;
167 int init_call_count_
;
168 int read_call_count_
;
169 int write_call_count_
;
170 int add_component_call_count_
;
171 int drop_call_count_
;
172 int read_metadata_call_count_
;
173 int read_all_metadata_call_count_
;
174 int dtor_call_count_
;
177 // Test that method calls are forwarded to backend loop
178 TEST_F(AttachmentStoreFrontendTest
, MethodsCalled
) {
179 AttachmentIdList id_list
;
180 AttachmentList attachments
;
182 attachment_store_frontend_
->Init(
183 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult
));
184 EXPECT_EQ(init_call_count_
, 0);
186 EXPECT_EQ(init_call_count_
, 1);
188 attachment_store_frontend_
->Read(
189 id_list
, base::Bind(&AttachmentStoreFrontendTest::ReadDone
));
190 EXPECT_EQ(read_call_count_
, 0);
192 EXPECT_EQ(read_call_count_
, 1);
194 attachment_store_frontend_
->Write(
195 AttachmentStore::SYNC
, attachments
,
196 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult
));
197 EXPECT_EQ(write_call_count_
, 0);
199 EXPECT_EQ(write_call_count_
, 1);
201 attachment_store_frontend_
->SetReference(AttachmentStore::SYNC
, id_list
);
203 attachment_store_frontend_
->DropReference(
204 AttachmentStore::SYNC
, id_list
,
205 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult
));
206 EXPECT_EQ(drop_call_count_
, 0);
208 EXPECT_EQ(drop_call_count_
, 1);
210 attachment_store_frontend_
->ReadMetadata(
211 id_list
, base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone
));
212 EXPECT_EQ(read_metadata_call_count_
, 0);
214 EXPECT_EQ(read_metadata_call_count_
, 1);
216 attachment_store_frontend_
->ReadAllMetadata(
217 AttachmentStore::SYNC
,
218 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone
));
219 EXPECT_EQ(read_all_metadata_call_count_
, 0);
221 EXPECT_EQ(read_all_metadata_call_count_
, 1);
223 // Releasing referehce to AttachmentStoreFrontend should result in
224 // MockAttachmentStore being deleted on backend loop.
225 attachment_store_frontend_
= NULL
;
226 EXPECT_EQ(dtor_call_count_
, 0);
228 EXPECT_EQ(dtor_call_count_
, 1);
231 } // namespace syncer