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