OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/public/attachments/attachment_store_handle.h" | 5 #include "sync/internal_api/public/attachments/attachment_store_frontend.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "sync/api/attachments/attachment.h" | 14 #include "sync/api/attachments/attachment.h" |
15 #include "sync/api/attachments/attachment_id.h" | 15 #include "sync/api/attachments/attachment_id.h" |
| 16 #include "sync/api/attachments/attachment_store_backend.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace syncer { | 19 namespace syncer { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 class MockAttachmentStore : public AttachmentStoreBackend { | 23 class MockAttachmentStore : public AttachmentStoreBackend { |
23 public: | 24 public: |
24 MockAttachmentStore(const base::Closure& init_called, | 25 MockAttachmentStore(const base::Closure& init_called, |
25 const base::Closure& read_called, | 26 const base::Closure& read_called, |
(...skipping 15 matching lines...) Expand all Loading... |
41 | 42 |
42 void Init(const AttachmentStore::InitCallback& callback) override { | 43 void Init(const AttachmentStore::InitCallback& callback) override { |
43 init_called_.Run(); | 44 init_called_.Run(); |
44 } | 45 } |
45 | 46 |
46 void Read(const AttachmentIdList& ids, | 47 void Read(const AttachmentIdList& ids, |
47 const AttachmentStore::ReadCallback& callback) override { | 48 const AttachmentStore::ReadCallback& callback) override { |
48 read_called_.Run(); | 49 read_called_.Run(); |
49 } | 50 } |
50 | 51 |
51 void Write(const AttachmentList& attachments, | 52 void Write(AttachmentStore::AttachmentReferrer referrer, |
| 53 const AttachmentList& attachments, |
52 const AttachmentStore::WriteCallback& callback) override { | 54 const AttachmentStore::WriteCallback& callback) override { |
53 write_called_.Run(); | 55 write_called_.Run(); |
54 } | 56 } |
55 | 57 |
56 void Drop(const AttachmentIdList& ids, | 58 void Drop(AttachmentStore::AttachmentReferrer referrer, |
| 59 const AttachmentIdList& ids, |
57 const AttachmentStore::DropCallback& callback) override { | 60 const AttachmentStore::DropCallback& callback) override { |
58 drop_called_.Run(); | 61 drop_called_.Run(); |
59 } | 62 } |
60 | 63 |
61 void ReadMetadata( | 64 void ReadMetadata( |
62 const AttachmentIdList& ids, | 65 const AttachmentIdList& ids, |
63 const AttachmentStore::ReadMetadataCallback& callback) override { | 66 const AttachmentStore::ReadMetadataCallback& callback) override { |
64 read_metadata_called_.Run(); | 67 read_metadata_called_.Run(); |
65 } | 68 } |
66 | 69 |
67 void ReadAllMetadata( | 70 void ReadAllMetadata( |
| 71 AttachmentStore::AttachmentReferrer referrer, |
68 const AttachmentStore::ReadMetadataCallback& callback) override { | 72 const AttachmentStore::ReadMetadataCallback& callback) override { |
69 read_all_metadata_called_.Run(); | 73 read_all_metadata_called_.Run(); |
70 } | 74 } |
71 | 75 |
72 base::Closure init_called_; | 76 base::Closure init_called_; |
73 base::Closure read_called_; | 77 base::Closure read_called_; |
74 base::Closure write_called_; | 78 base::Closure write_called_; |
75 base::Closure drop_called_; | 79 base::Closure drop_called_; |
76 base::Closure read_metadata_called_; | 80 base::Closure read_metadata_called_; |
77 base::Closure read_all_metadata_called_; | 81 base::Closure read_all_metadata_called_; |
78 base::Closure dtor_called_; | 82 base::Closure dtor_called_; |
79 }; | 83 }; |
80 | 84 |
81 } // namespace | 85 } // namespace |
82 | 86 |
83 class AttachmentStoreHandleTest : public testing::Test { | 87 class AttachmentStoreFrontendTest : public testing::Test { |
84 protected: | 88 protected: |
85 AttachmentStoreHandleTest() | 89 AttachmentStoreFrontendTest() |
86 : init_call_count_(0), | 90 : init_call_count_(0), |
87 read_call_count_(0), | 91 read_call_count_(0), |
88 write_call_count_(0), | 92 write_call_count_(0), |
89 drop_call_count_(0), | 93 drop_call_count_(0), |
90 read_metadata_call_count_(0), | 94 read_metadata_call_count_(0), |
91 read_all_metadata_call_count_(0), | 95 read_all_metadata_call_count_(0), |
92 dtor_call_count_(0) {} | 96 dtor_call_count_(0) {} |
93 | 97 |
94 void SetUp() override { | 98 void SetUp() override { |
95 scoped_ptr<AttachmentStoreBackend> backend(new MockAttachmentStore( | 99 scoped_ptr<AttachmentStoreBackend> backend(new MockAttachmentStore( |
96 base::Bind(&AttachmentStoreHandleTest::InitCalled, | 100 base::Bind(&AttachmentStoreFrontendTest::InitCalled, |
97 base::Unretained(this)), | 101 base::Unretained(this)), |
98 base::Bind(&AttachmentStoreHandleTest::ReadCalled, | 102 base::Bind(&AttachmentStoreFrontendTest::ReadCalled, |
99 base::Unretained(this)), | 103 base::Unretained(this)), |
100 base::Bind(&AttachmentStoreHandleTest::WriteCalled, | 104 base::Bind(&AttachmentStoreFrontendTest::WriteCalled, |
101 base::Unretained(this)), | 105 base::Unretained(this)), |
102 base::Bind(&AttachmentStoreHandleTest::DropCalled, | 106 base::Bind(&AttachmentStoreFrontendTest::DropCalled, |
103 base::Unretained(this)), | 107 base::Unretained(this)), |
104 base::Bind(&AttachmentStoreHandleTest::ReadMetadataCalled, | 108 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataCalled, |
105 base::Unretained(this)), | 109 base::Unretained(this)), |
106 base::Bind(&AttachmentStoreHandleTest::ReadAllMetadataCalled, | 110 base::Bind(&AttachmentStoreFrontendTest::ReadAllMetadataCalled, |
107 base::Unretained(this)), | 111 base::Unretained(this)), |
108 base::Bind(&AttachmentStoreHandleTest::DtorCalled, | 112 base::Bind(&AttachmentStoreFrontendTest::DtorCalled, |
109 base::Unretained(this)))); | 113 base::Unretained(this)))); |
110 attachment_store_handle_ = new AttachmentStoreHandle( | 114 attachment_store_frontend_ = new AttachmentStoreFrontend( |
111 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); | 115 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); |
112 } | 116 } |
113 | 117 |
114 static void DoneWithResult(const AttachmentStore::Result& result) { | 118 static void DoneWithResult(const AttachmentStore::Result& result) { |
115 NOTREACHED(); | 119 NOTREACHED(); |
116 } | 120 } |
117 | 121 |
118 static void ReadDone(const AttachmentStore::Result& result, | 122 static void ReadDone(const AttachmentStore::Result& result, |
119 scoped_ptr<AttachmentMap> attachments, | 123 scoped_ptr<AttachmentMap> attachments, |
120 scoped_ptr<AttachmentIdList> unavailable_attachments) { | 124 scoped_ptr<AttachmentIdList> unavailable_attachments) { |
(...skipping 18 matching lines...) Expand all Loading... |
139 void ReadAllMetadataCalled() { ++read_all_metadata_call_count_; } | 143 void ReadAllMetadataCalled() { ++read_all_metadata_call_count_; } |
140 | 144 |
141 void DtorCalled() { ++dtor_call_count_; } | 145 void DtorCalled() { ++dtor_call_count_; } |
142 | 146 |
143 void RunMessageLoop() { | 147 void RunMessageLoop() { |
144 base::RunLoop run_loop; | 148 base::RunLoop run_loop; |
145 run_loop.RunUntilIdle(); | 149 run_loop.RunUntilIdle(); |
146 } | 150 } |
147 | 151 |
148 base::MessageLoop message_loop_; | 152 base::MessageLoop message_loop_; |
149 scoped_refptr<AttachmentStoreHandle> attachment_store_handle_; | 153 scoped_refptr<AttachmentStoreFrontend> attachment_store_frontend_; |
150 int init_call_count_; | 154 int init_call_count_; |
151 int read_call_count_; | 155 int read_call_count_; |
152 int write_call_count_; | 156 int write_call_count_; |
153 int drop_call_count_; | 157 int drop_call_count_; |
154 int read_metadata_call_count_; | 158 int read_metadata_call_count_; |
155 int read_all_metadata_call_count_; | 159 int read_all_metadata_call_count_; |
156 int dtor_call_count_; | 160 int dtor_call_count_; |
157 }; | 161 }; |
158 | 162 |
159 // Test that method calls are forwarded to backend loop | 163 // Test that method calls are forwarded to backend loop |
160 TEST_F(AttachmentStoreHandleTest, MethodsCalled) { | 164 TEST_F(AttachmentStoreFrontendTest, MethodsCalled) { |
161 AttachmentIdList ids; | 165 AttachmentIdList ids; |
162 AttachmentList attachments; | 166 AttachmentList attachments; |
163 | 167 |
164 attachment_store_handle_->Init( | 168 attachment_store_frontend_->Init( |
165 base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); | 169 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); |
166 EXPECT_EQ(init_call_count_, 0); | 170 EXPECT_EQ(init_call_count_, 0); |
167 RunMessageLoop(); | 171 RunMessageLoop(); |
168 EXPECT_EQ(init_call_count_, 1); | 172 EXPECT_EQ(init_call_count_, 1); |
169 | 173 |
170 attachment_store_handle_->Read( | 174 attachment_store_frontend_->Read( |
171 ids, base::Bind(&AttachmentStoreHandleTest::ReadDone)); | 175 ids, base::Bind(&AttachmentStoreFrontendTest::ReadDone)); |
172 EXPECT_EQ(read_call_count_, 0); | 176 EXPECT_EQ(read_call_count_, 0); |
173 RunMessageLoop(); | 177 RunMessageLoop(); |
174 EXPECT_EQ(read_call_count_, 1); | 178 EXPECT_EQ(read_call_count_, 1); |
175 | 179 |
176 attachment_store_handle_->Write( | 180 attachment_store_frontend_->Write( |
177 attachments, base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); | 181 AttachmentStore::SYNC, attachments, |
| 182 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); |
178 EXPECT_EQ(write_call_count_, 0); | 183 EXPECT_EQ(write_call_count_, 0); |
179 RunMessageLoop(); | 184 RunMessageLoop(); |
180 EXPECT_EQ(write_call_count_, 1); | 185 EXPECT_EQ(write_call_count_, 1); |
181 | 186 |
182 attachment_store_handle_->Drop( | 187 attachment_store_frontend_->Drop( |
183 ids, base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); | 188 AttachmentStore::SYNC, ids, |
| 189 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); |
184 EXPECT_EQ(drop_call_count_, 0); | 190 EXPECT_EQ(drop_call_count_, 0); |
185 RunMessageLoop(); | 191 RunMessageLoop(); |
186 EXPECT_EQ(drop_call_count_, 1); | 192 EXPECT_EQ(drop_call_count_, 1); |
187 | 193 |
188 attachment_store_handle_->ReadMetadata( | 194 attachment_store_frontend_->ReadMetadata( |
189 ids, base::Bind(&AttachmentStoreHandleTest::ReadMetadataDone)); | 195 ids, base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone)); |
190 EXPECT_EQ(read_metadata_call_count_, 0); | 196 EXPECT_EQ(read_metadata_call_count_, 0); |
191 RunMessageLoop(); | 197 RunMessageLoop(); |
192 EXPECT_EQ(read_metadata_call_count_, 1); | 198 EXPECT_EQ(read_metadata_call_count_, 1); |
193 | 199 |
194 attachment_store_handle_->ReadAllMetadata( | 200 attachment_store_frontend_->ReadAllMetadata( |
195 base::Bind(&AttachmentStoreHandleTest::ReadMetadataDone)); | 201 AttachmentStore::SYNC, |
| 202 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone)); |
196 EXPECT_EQ(read_all_metadata_call_count_, 0); | 203 EXPECT_EQ(read_all_metadata_call_count_, 0); |
197 RunMessageLoop(); | 204 RunMessageLoop(); |
198 EXPECT_EQ(read_all_metadata_call_count_, 1); | 205 EXPECT_EQ(read_all_metadata_call_count_, 1); |
199 | 206 |
200 // Releasing referehce to AttachmentStoreHandle should result in | 207 // Releasing referehce to AttachmentStoreFrontend should result in |
201 // MockAttachmentStore being deleted on backend loop. | 208 // MockAttachmentStore being deleted on backend loop. |
202 attachment_store_handle_ = NULL; | 209 attachment_store_frontend_ = NULL; |
203 EXPECT_EQ(dtor_call_count_, 0); | 210 EXPECT_EQ(dtor_call_count_, 0); |
204 RunMessageLoop(); | 211 RunMessageLoop(); |
205 EXPECT_EQ(dtor_call_count_, 1); | 212 EXPECT_EQ(dtor_call_count_, 1); |
206 } | 213 } |
207 | 214 |
208 } // namespace syncer | 215 } // namespace syncer |
OLD | NEW |