OLD | NEW |
| (Empty) |
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/internal_api/public/attachments/attachment_store_handle.h" | |
6 | |
7 #include "base/bind.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" | |
17 | |
18 namespace syncer { | |
19 | |
20 namespace { | |
21 | |
22 class MockAttachmentStore : public AttachmentStoreBackend { | |
23 public: | |
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 : AttachmentStoreBackend(nullptr), | |
32 init_called_(init_called), | |
33 read_called_(read_called), | |
34 write_called_(write_called), | |
35 drop_called_(drop_called), | |
36 read_metadata_called_(read_metadata_called), | |
37 read_all_metadata_called_(read_all_metadata_called), | |
38 dtor_called_(dtor_called) {} | |
39 | |
40 ~MockAttachmentStore() override { dtor_called_.Run(); } | |
41 | |
42 void Init(const AttachmentStore::InitCallback& callback) override { | |
43 init_called_.Run(); | |
44 } | |
45 | |
46 void Read(const AttachmentIdList& ids, | |
47 const AttachmentStore::ReadCallback& callback) override { | |
48 read_called_.Run(); | |
49 } | |
50 | |
51 void Write(const AttachmentList& attachments, | |
52 const AttachmentStore::WriteCallback& callback) override { | |
53 write_called_.Run(); | |
54 } | |
55 | |
56 void Drop(const AttachmentIdList& ids, | |
57 const AttachmentStore::DropCallback& callback) override { | |
58 drop_called_.Run(); | |
59 } | |
60 | |
61 void ReadMetadata( | |
62 const AttachmentIdList& ids, | |
63 const AttachmentStore::ReadMetadataCallback& callback) override { | |
64 read_metadata_called_.Run(); | |
65 } | |
66 | |
67 void ReadAllMetadata( | |
68 const AttachmentStore::ReadMetadataCallback& callback) override { | |
69 read_all_metadata_called_.Run(); | |
70 } | |
71 | |
72 base::Closure init_called_; | |
73 base::Closure read_called_; | |
74 base::Closure write_called_; | |
75 base::Closure drop_called_; | |
76 base::Closure read_metadata_called_; | |
77 base::Closure read_all_metadata_called_; | |
78 base::Closure dtor_called_; | |
79 }; | |
80 | |
81 } // namespace | |
82 | |
83 class AttachmentStoreHandleTest : public testing::Test { | |
84 protected: | |
85 AttachmentStoreHandleTest() | |
86 : init_call_count_(0), | |
87 read_call_count_(0), | |
88 write_call_count_(0), | |
89 drop_call_count_(0), | |
90 read_metadata_call_count_(0), | |
91 read_all_metadata_call_count_(0), | |
92 dtor_call_count_(0) {} | |
93 | |
94 void SetUp() override { | |
95 scoped_ptr<AttachmentStoreBackend> backend(new MockAttachmentStore( | |
96 base::Bind(&AttachmentStoreHandleTest::InitCalled, | |
97 base::Unretained(this)), | |
98 base::Bind(&AttachmentStoreHandleTest::ReadCalled, | |
99 base::Unretained(this)), | |
100 base::Bind(&AttachmentStoreHandleTest::WriteCalled, | |
101 base::Unretained(this)), | |
102 base::Bind(&AttachmentStoreHandleTest::DropCalled, | |
103 base::Unretained(this)), | |
104 base::Bind(&AttachmentStoreHandleTest::ReadMetadataCalled, | |
105 base::Unretained(this)), | |
106 base::Bind(&AttachmentStoreHandleTest::ReadAllMetadataCalled, | |
107 base::Unretained(this)), | |
108 base::Bind(&AttachmentStoreHandleTest::DtorCalled, | |
109 base::Unretained(this)))); | |
110 attachment_store_handle_ = new AttachmentStoreHandle( | |
111 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); | |
112 } | |
113 | |
114 static void DoneWithResult(const AttachmentStore::Result& result) { | |
115 NOTREACHED(); | |
116 } | |
117 | |
118 static void ReadDone(const AttachmentStore::Result& result, | |
119 scoped_ptr<AttachmentMap> attachments, | |
120 scoped_ptr<AttachmentIdList> unavailable_attachments) { | |
121 NOTREACHED(); | |
122 } | |
123 | |
124 static void ReadMetadataDone(const AttachmentStore::Result& result, | |
125 scoped_ptr<AttachmentMetadataList> metadata) { | |
126 NOTREACHED(); | |
127 } | |
128 | |
129 void InitCalled() { ++init_call_count_; } | |
130 | |
131 void ReadCalled() { ++read_call_count_; } | |
132 | |
133 void WriteCalled() { ++write_call_count_; } | |
134 | |
135 void DropCalled() { ++drop_call_count_; } | |
136 | |
137 void ReadMetadataCalled() { ++read_metadata_call_count_; } | |
138 | |
139 void ReadAllMetadataCalled() { ++read_all_metadata_call_count_; } | |
140 | |
141 void DtorCalled() { ++dtor_call_count_; } | |
142 | |
143 void RunMessageLoop() { | |
144 base::RunLoop run_loop; | |
145 run_loop.RunUntilIdle(); | |
146 } | |
147 | |
148 base::MessageLoop message_loop_; | |
149 scoped_refptr<AttachmentStoreHandle> attachment_store_handle_; | |
150 int init_call_count_; | |
151 int read_call_count_; | |
152 int write_call_count_; | |
153 int drop_call_count_; | |
154 int read_metadata_call_count_; | |
155 int read_all_metadata_call_count_; | |
156 int dtor_call_count_; | |
157 }; | |
158 | |
159 // Test that method calls are forwarded to backend loop | |
160 TEST_F(AttachmentStoreHandleTest, MethodsCalled) { | |
161 AttachmentIdList ids; | |
162 AttachmentList attachments; | |
163 | |
164 attachment_store_handle_->Init( | |
165 base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); | |
166 EXPECT_EQ(init_call_count_, 0); | |
167 RunMessageLoop(); | |
168 EXPECT_EQ(init_call_count_, 1); | |
169 | |
170 attachment_store_handle_->Read( | |
171 ids, base::Bind(&AttachmentStoreHandleTest::ReadDone)); | |
172 EXPECT_EQ(read_call_count_, 0); | |
173 RunMessageLoop(); | |
174 EXPECT_EQ(read_call_count_, 1); | |
175 | |
176 attachment_store_handle_->Write( | |
177 attachments, base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); | |
178 EXPECT_EQ(write_call_count_, 0); | |
179 RunMessageLoop(); | |
180 EXPECT_EQ(write_call_count_, 1); | |
181 | |
182 attachment_store_handle_->Drop( | |
183 ids, base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); | |
184 EXPECT_EQ(drop_call_count_, 0); | |
185 RunMessageLoop(); | |
186 EXPECT_EQ(drop_call_count_, 1); | |
187 | |
188 attachment_store_handle_->ReadMetadata( | |
189 ids, base::Bind(&AttachmentStoreHandleTest::ReadMetadataDone)); | |
190 EXPECT_EQ(read_metadata_call_count_, 0); | |
191 RunMessageLoop(); | |
192 EXPECT_EQ(read_metadata_call_count_, 1); | |
193 | |
194 attachment_store_handle_->ReadAllMetadata( | |
195 base::Bind(&AttachmentStoreHandleTest::ReadMetadataDone)); | |
196 EXPECT_EQ(read_all_metadata_call_count_, 0); | |
197 RunMessageLoop(); | |
198 EXPECT_EQ(read_all_metadata_call_count_, 1); | |
199 | |
200 // Releasing referehce to AttachmentStoreHandle should result in | |
201 // MockAttachmentStore being deleted on backend loop. | |
202 attachment_store_handle_ = NULL; | |
203 EXPECT_EQ(dtor_call_count_, 0); | |
204 RunMessageLoop(); | |
205 EXPECT_EQ(dtor_call_count_, 1); | |
206 } | |
207 | |
208 } // namespace syncer | |
OLD | NEW |