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/api/attachments/attachment_service_proxy.h" | 5 #include "sync/api/attachments/attachment_service_proxy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 } | 49 } |
50 | 50 |
51 virtual void DropAttachments(const AttachmentIdList& attachment_ids, | 51 virtual void DropAttachments(const AttachmentIdList& attachment_ids, |
52 const DropCallback& callback) OVERRIDE { | 52 const DropCallback& callback) OVERRIDE { |
53 CalledOnValidThread(); | 53 CalledOnValidThread(); |
54 Increment(); | 54 Increment(); |
55 base::MessageLoop::current()->PostTask( | 55 base::MessageLoop::current()->PostTask( |
56 FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS)); | 56 FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS)); |
57 } | 57 } |
58 | 58 |
59 virtual void StoreAttachments(const AttachmentList& attachments, | 59 virtual void OnSyncDataAdd(const SyncData& sync_data) OVERRIDE { |
60 const StoreCallback& callback) OVERRIDE { | |
61 CalledOnValidThread(); | 60 CalledOnValidThread(); |
62 Increment(); | 61 Increment(); |
63 base::MessageLoop::current()->PostTask( | |
64 FROM_HERE, base::Bind(callback, AttachmentService::STORE_SUCCESS)); | |
65 } | 62 } |
66 | 63 |
67 virtual void OnSyncDataDelete(const SyncData& sync_data) OVERRIDE { | 64 virtual void OnSyncDataDelete(const SyncData& sync_data) OVERRIDE { |
68 CalledOnValidThread(); | 65 CalledOnValidThread(); |
69 Increment(); | 66 Increment(); |
70 } | 67 } |
71 | 68 |
72 virtual void OnSyncDataUpdate(const AttachmentIdList& old_attachment_ids, | 69 virtual void OnSyncDataUpdate(const AttachmentIdList& old_attachment_ids, |
73 const SyncData& updated_sync_data) OVERRIDE { | 70 const SyncData& updated_sync_data) OVERRIDE { |
74 CalledOnValidThread(); | 71 CalledOnValidThread(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 sync_data = | 112 sync_data = |
116 SyncData::CreateLocalData("tag", "title", sync_pb::EntitySpecifics()); | 113 SyncData::CreateLocalData("tag", "title", sync_pb::EntitySpecifics()); |
117 sync_data_delete = | 114 sync_data_delete = |
118 SyncData::CreateLocalDelete("tag", syncer::PREFERENCES); | 115 SyncData::CreateLocalDelete("tag", syncer::PREFERENCES); |
119 | 116 |
120 callback_get_or_download = | 117 callback_get_or_download = |
121 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, | 118 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, |
122 base::Unretained(this)); | 119 base::Unretained(this)); |
123 callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop, | 120 callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop, |
124 base::Unretained(this)); | 121 base::Unretained(this)); |
125 callback_store = base::Bind(&AttachmentServiceProxyTest::IncrementStore, | |
126 base::Unretained(this)); | |
127 count_callback_get_or_download = 0; | 122 count_callback_get_or_download = 0; |
128 count_callback_drop = 0; | 123 count_callback_drop = 0; |
129 count_callback_store = 0; | |
130 } | 124 } |
131 | 125 |
132 virtual void TearDown() | 126 virtual void TearDown() |
133 OVERRIDE { | 127 OVERRIDE { |
134 // We must take care to call the stub's destructor on the stub_thread | 128 // We must take care to call the stub's destructor on the stub_thread |
135 // because that's the thread to which its WeakPtrs are bound. | 129 // because that's the thread to which its WeakPtrs are bound. |
136 if (stub) { | 130 if (stub) { |
137 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); | 131 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); |
138 WaitForStubThread(); | 132 WaitForStubThread(); |
139 } | 133 } |
140 stub_thread->Stop(); | 134 stub_thread->Stop(); |
141 } | 135 } |
142 | 136 |
143 // a GetOrDownloadCallback | 137 // a GetOrDownloadCallback |
144 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, | 138 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, |
145 scoped_ptr<AttachmentMap>) { | 139 scoped_ptr<AttachmentMap>) { |
146 CalledOnValidThread(); | 140 CalledOnValidThread(); |
147 ++count_callback_get_or_download; | 141 ++count_callback_get_or_download; |
148 } | 142 } |
149 | 143 |
150 // a DropCallback | 144 // a DropCallback |
151 void IncrementDrop(const AttachmentService::DropResult&) { | 145 void IncrementDrop(const AttachmentService::DropResult&) { |
152 CalledOnValidThread(); | 146 CalledOnValidThread(); |
153 ++count_callback_drop; | 147 ++count_callback_drop; |
154 } | 148 } |
155 | 149 |
156 // a StoreCallback | |
157 void IncrementStore(const AttachmentService::StoreResult&) { | |
158 CalledOnValidThread(); | |
159 ++count_callback_store; | |
160 } | |
161 | |
162 void WaitForStubThread() { | 150 void WaitForStubThread() { |
163 base::WaitableEvent done(false, false); | 151 base::WaitableEvent done(false, false); |
164 stub_thread->message_loop()->PostTask( | 152 stub_thread->message_loop()->PostTask( |
165 FROM_HERE, | 153 FROM_HERE, |
166 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | 154 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
167 done.Wait(); | 155 done.Wait(); |
168 } | 156 } |
169 | 157 |
170 base::MessageLoop loop; | 158 base::MessageLoop loop; |
171 scoped_ptr<base::Thread> stub_thread; | 159 scoped_ptr<base::Thread> stub_thread; |
172 scoped_ptr<StubAttachmentService> stub; | 160 scoped_ptr<StubAttachmentService> stub; |
173 scoped_ptr<AttachmentServiceProxy> proxy; | 161 scoped_ptr<AttachmentServiceProxy> proxy; |
174 | 162 |
175 SyncData sync_data; | 163 SyncData sync_data; |
176 SyncData sync_data_delete; | 164 SyncData sync_data_delete; |
177 | 165 |
178 AttachmentService::GetOrDownloadCallback callback_get_or_download; | 166 AttachmentService::GetOrDownloadCallback callback_get_or_download; |
179 AttachmentService::DropCallback callback_drop; | 167 AttachmentService::DropCallback callback_drop; |
180 AttachmentService::StoreCallback callback_store; | |
181 | 168 |
182 // number of times callback_get_or_download was invoked | 169 // number of times callback_get_or_download was invoked |
183 int count_callback_get_or_download; | 170 int count_callback_get_or_download; |
184 // number of times callback_drop was invoked | 171 // number of times callback_drop was invoked |
185 int count_callback_drop; | 172 int count_callback_drop; |
186 // number of times callback_store was invoked | |
187 int count_callback_store; | |
188 }; | 173 }; |
189 | 174 |
190 // Verify that each of AttachmentServiceProxy's regular methods (those that | 175 // Verify that each of AttachmentServiceProxy's regular methods (those that |
191 // don't take callbacks) are invoked on the stub. | 176 // don't take callbacks) are invoked on the stub. |
192 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { | 177 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { |
| 178 proxy->OnSyncDataAdd(sync_data); |
193 proxy->OnSyncDataDelete(sync_data_delete); | 179 proxy->OnSyncDataDelete(sync_data_delete); |
194 proxy->OnSyncDataUpdate(AttachmentIdList(), sync_data); | 180 proxy->OnSyncDataUpdate(AttachmentIdList(), sync_data); |
195 WaitForStubThread(); | 181 WaitForStubThread(); |
196 EXPECT_EQ(2, stub->GetCallCount()); | 182 EXPECT_EQ(3, stub->GetCallCount()); |
197 } | 183 } |
198 | 184 |
199 // Verify that each of AttachmentServiceProxy's callback methods (those that | 185 // Verify that each of AttachmentServiceProxy's callback methods (those that |
200 // take callbacks) are invoked on the stub and that the passed callbacks are | 186 // take callbacks) are invoked on the stub and that the passed callbacks are |
201 // invoked in this thread. | 187 // invoked in this thread. |
202 TEST_F(AttachmentServiceProxyTest, MethodsWithCallbacksAreProxied) { | 188 TEST_F(AttachmentServiceProxyTest, MethodsWithCallbacksAreProxied) { |
203 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 189 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
204 proxy->DropAttachments(AttachmentIdList(), callback_drop); | 190 proxy->DropAttachments(AttachmentIdList(), callback_drop); |
205 proxy->StoreAttachments(AttachmentList(), callback_store); | |
206 // Wait for the posted calls to execute in the stub thread. | 191 // Wait for the posted calls to execute in the stub thread. |
207 WaitForStubThread(); | 192 WaitForStubThread(); |
208 EXPECT_EQ(3, stub->GetCallCount()); | 193 EXPECT_EQ(2, stub->GetCallCount()); |
209 // At this point the stub thread has finished executed the calls. However, the | 194 // At this point the stub thread has finished executed the calls. However, the |
210 // result callbacks it has posted may not have executed yet. Wait a second | 195 // result callbacks it has posted may not have executed yet. Wait a second |
211 // time to ensure the stub thread has executed the posted result callbacks. | 196 // time to ensure the stub thread has executed the posted result callbacks. |
212 WaitForStubThread(); | 197 WaitForStubThread(); |
213 | 198 |
214 loop.RunUntilIdle(); | 199 loop.RunUntilIdle(); |
215 EXPECT_EQ(1, count_callback_get_or_download); | 200 EXPECT_EQ(1, count_callback_get_or_download); |
216 EXPECT_EQ(1, count_callback_drop); | 201 EXPECT_EQ(1, count_callback_drop); |
217 EXPECT_EQ(1, count_callback_store); | |
218 } | 202 } |
219 | 203 |
220 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped | 204 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped |
221 // AttachmentService has been destroyed. | 205 // AttachmentService has been destroyed. |
222 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { | 206 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { |
223 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 207 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
224 // Wait for the posted calls to execute in the stub thread. | 208 // Wait for the posted calls to execute in the stub thread. |
225 WaitForStubThread(); | 209 WaitForStubThread(); |
226 EXPECT_EQ(1, stub->GetCallCount()); | 210 EXPECT_EQ(1, stub->GetCallCount()); |
227 // Wait a second time ensure the stub thread has executed the posted result | 211 // Wait a second time ensure the stub thread has executed the posted result |
(...skipping 10 matching lines...) Expand all Loading... |
238 // Now that the wrapped object has been destroyed, call again and see that we | 222 // Now that the wrapped object has been destroyed, call again and see that we |
239 // don't crash and the count remains the same. | 223 // don't crash and the count remains the same. |
240 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 224 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
241 WaitForStubThread(); | 225 WaitForStubThread(); |
242 WaitForStubThread(); | 226 WaitForStubThread(); |
243 loop.RunUntilIdle(); | 227 loop.RunUntilIdle(); |
244 EXPECT_EQ(1, count_callback_get_or_download); | 228 EXPECT_EQ(1, count_callback_get_or_download); |
245 } | 229 } |
246 | 230 |
247 } // namespace syncer | 231 } // namespace syncer |
OLD | NEW |