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_handle.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 "testing/gtest/include/gtest/gtest.h"
22 class MockAttachmentStore
: public AttachmentStoreBase
{
24 MockAttachmentStore(const base::Closure
& init_called
,
25 const base::Closure
& read_called
,
26 const base::Closure
& write_called
,
27 const base::Closure
& drop_called
,
28 const base::Closure
& read_metadata_called
,
29 const base::Closure
& read_all_metadata_called
,
30 const base::Closure
& dtor_called
)
31 : init_called_(init_called
),
32 read_called_(read_called
),
33 write_called_(write_called
),
34 drop_called_(drop_called
),
35 read_metadata_called_(read_metadata_called
),
36 read_all_metadata_called_(read_all_metadata_called
),
37 dtor_called_(dtor_called
) {}
39 ~MockAttachmentStore() override
{ dtor_called_
.Run(); }
41 void Init(const InitCallback
& callback
) override
{
45 void Read(const AttachmentIdList
& ids
,
46 const ReadCallback
& callback
) override
{
50 void Write(const AttachmentList
& attachments
,
51 const WriteCallback
& callback
) override
{
55 void Drop(const AttachmentIdList
& ids
,
56 const DropCallback
& callback
) override
{
60 void ReadMetadata(const AttachmentIdList
& ids
,
61 const ReadMetadataCallback
& callback
) override
{
62 read_metadata_called_
.Run();
65 void ReadAllMetadata(const ReadMetadataCallback
& callback
) override
{
66 read_all_metadata_called_
.Run();
69 base::Closure init_called_
;
70 base::Closure read_called_
;
71 base::Closure write_called_
;
72 base::Closure drop_called_
;
73 base::Closure read_metadata_called_
;
74 base::Closure read_all_metadata_called_
;
75 base::Closure dtor_called_
;
80 class AttachmentStoreHandleTest
: public testing::Test
{
82 AttachmentStoreHandleTest()
83 : init_call_count_(0),
87 read_metadata_call_count_(0),
88 read_all_metadata_call_count_(0),
89 dtor_call_count_(0) {}
91 void SetUp() override
{
92 scoped_ptr
<AttachmentStoreBase
> backend(new MockAttachmentStore(
93 base::Bind(&AttachmentStoreHandleTest::InitCalled
,
94 base::Unretained(this)),
95 base::Bind(&AttachmentStoreHandleTest::ReadCalled
,
96 base::Unretained(this)),
97 base::Bind(&AttachmentStoreHandleTest::WriteCalled
,
98 base::Unretained(this)),
99 base::Bind(&AttachmentStoreHandleTest::DropCalled
,
100 base::Unretained(this)),
101 base::Bind(&AttachmentStoreHandleTest::ReadMetadataCalled
,
102 base::Unretained(this)),
103 base::Bind(&AttachmentStoreHandleTest::ReadAllMetadataCalled
,
104 base::Unretained(this)),
105 base::Bind(&AttachmentStoreHandleTest::DtorCalled
,
106 base::Unretained(this))));
107 attachment_store_handle_
= new AttachmentStoreHandle(
108 backend
.Pass(), base::ThreadTaskRunnerHandle::Get());
111 static void DoneWithResult(const AttachmentStore::Result
& result
) {
115 static void ReadDone(const AttachmentStore::Result
& result
,
116 scoped_ptr
<AttachmentMap
> attachments
,
117 scoped_ptr
<AttachmentIdList
> unavailable_attachments
) {
121 static void ReadMetadataDone(const AttachmentStore::Result
& result
,
122 scoped_ptr
<AttachmentMetadataList
> metadata
) {
126 void InitCalled() { ++init_call_count_
; }
128 void ReadCalled() { ++read_call_count_
; }
130 void WriteCalled() { ++write_call_count_
; }
132 void DropCalled() { ++drop_call_count_
; }
134 void ReadMetadataCalled() { ++read_metadata_call_count_
; }
136 void ReadAllMetadataCalled() { ++read_all_metadata_call_count_
; }
138 void DtorCalled() { ++dtor_call_count_
; }
140 void RunMessageLoop() {
141 base::RunLoop run_loop
;
142 run_loop
.RunUntilIdle();
145 base::MessageLoop message_loop_
;
146 scoped_refptr
<AttachmentStoreHandle
> attachment_store_handle_
;
147 int init_call_count_
;
148 int read_call_count_
;
149 int write_call_count_
;
150 int drop_call_count_
;
151 int read_metadata_call_count_
;
152 int read_all_metadata_call_count_
;
153 int dtor_call_count_
;
156 // Test that method calls are forwarded to backend loop
157 TEST_F(AttachmentStoreHandleTest
, MethodsCalled
) {
158 AttachmentIdList ids
;
159 AttachmentList attachments
;
161 attachment_store_handle_
->Init(
162 base::Bind(&AttachmentStoreHandleTest::DoneWithResult
));
163 EXPECT_EQ(init_call_count_
, 0);
165 EXPECT_EQ(init_call_count_
, 1);
167 attachment_store_handle_
->Read(
168 ids
, base::Bind(&AttachmentStoreHandleTest::ReadDone
));
169 EXPECT_EQ(read_call_count_
, 0);
171 EXPECT_EQ(read_call_count_
, 1);
173 attachment_store_handle_
->Write(
174 attachments
, base::Bind(&AttachmentStoreHandleTest::DoneWithResult
));
175 EXPECT_EQ(write_call_count_
, 0);
177 EXPECT_EQ(write_call_count_
, 1);
179 attachment_store_handle_
->Drop(
180 ids
, base::Bind(&AttachmentStoreHandleTest::DoneWithResult
));
181 EXPECT_EQ(drop_call_count_
, 0);
183 EXPECT_EQ(drop_call_count_
, 1);
185 attachment_store_handle_
->ReadMetadata(
186 ids
, base::Bind(&AttachmentStoreHandleTest::ReadMetadataDone
));
187 EXPECT_EQ(read_metadata_call_count_
, 0);
189 EXPECT_EQ(read_metadata_call_count_
, 1);
191 attachment_store_handle_
->ReadAllMetadata(
192 base::Bind(&AttachmentStoreHandleTest::ReadMetadataDone
));
193 EXPECT_EQ(read_all_metadata_call_count_
, 0);
195 EXPECT_EQ(read_all_metadata_call_count_
, 1);
197 // Releasing referehce to AttachmentStoreHandle should result in
198 // MockAttachmentStore being deleted on backend loop.
199 attachment_store_handle_
= NULL
;
200 EXPECT_EQ(dtor_call_count_
, 0);
202 EXPECT_EQ(dtor_call_count_
, 1);
205 } // namespace syncer