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