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/api/attachments/attachment_service_proxy.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ref_counted_memory.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/run_loop.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "base/synchronization/waitable_event.h" | |
14 #include "base/threading/non_thread_safe.h" | |
15 #include "base/threading/thread.h" | |
16 #include "sync/api/attachments/attachment.h" | |
17 #include "sync/api/attachments/attachment_service.h" | |
18 #include "sync/api/sync_data.h" | |
19 #include "sync/internal_api/public/base/model_type.h" | |
20 #include "sync/protocol/sync.pb.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 namespace syncer { | |
24 | |
25 // A stub implementation of AttachmentService that counts the number of times | |
26 // its methods are invoked. | |
27 class StubAttachmentService : public AttachmentService, | |
28 public base::NonThreadSafe { | |
29 public: | |
30 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) { | |
31 // DetachFromThread because we will be constructed in one thread and | |
32 // used/destroyed in another. | |
33 DetachFromThread(); | |
34 } | |
35 | |
36 virtual ~StubAttachmentService() {} | |
37 | |
38 virtual void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids, | |
39 const GetOrDownloadCallback& callback) | |
40 OVERRIDE { | |
41 CalledOnValidThread(); | |
42 Increment(); | |
43 scoped_ptr<AttachmentMap> attachments(new AttachmentMap()); | |
44 base::MessageLoop::current()->PostTask( | |
45 FROM_HERE, | |
46 base::Bind(callback, | |
47 AttachmentService::GET_UNSPECIFIED_ERROR, | |
48 base::Passed(&attachments))); | |
49 } | |
50 | |
51 virtual void DropAttachments(const AttachmentIdList& attachment_ids, | |
52 const DropCallback& callback) OVERRIDE { | |
53 CalledOnValidThread(); | |
54 Increment(); | |
55 base::MessageLoop::current()->PostTask( | |
56 FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS)); | |
57 } | |
58 | |
59 virtual void StoreAttachments(const AttachmentList& attachments, | |
60 const StoreCallback& callback) OVERRIDE { | |
61 CalledOnValidThread(); | |
62 Increment(); | |
63 base::MessageLoop::current()->PostTask( | |
64 FROM_HERE, base::Bind(callback, AttachmentService::STORE_SUCCESS)); | |
65 } | |
66 | |
67 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { | |
68 return weak_ptr_factory_.GetWeakPtr(); | |
69 } | |
70 | |
71 // Return the number of method invocations. | |
72 int GetCallCount() const { | |
73 base::AutoLock lock(mutex_); | |
74 return call_count_; | |
75 } | |
76 | |
77 private: | |
78 // Protects call_count_. | |
79 mutable base::Lock mutex_; | |
80 int call_count_; | |
81 | |
82 // Must be last data member. | |
83 base::WeakPtrFactory<AttachmentService> weak_ptr_factory_; | |
84 | |
85 void Increment() { | |
86 base::AutoLock lock(mutex_); | |
87 ++call_count_; | |
88 } | |
89 }; | |
90 | |
91 class AttachmentServiceProxyTest : public testing::Test, | |
92 public base::NonThreadSafe { | |
93 protected: | |
94 AttachmentServiceProxyTest() {} | |
95 | |
96 virtual void SetUp() { | |
97 CalledOnValidThread(); | |
98 stub_thread.reset(new base::Thread("attachment service stub thread")); | |
99 stub_thread->Start(); | |
100 stub.reset(new StubAttachmentService); | |
101 proxy.reset(new AttachmentServiceProxy(stub_thread->message_loop_proxy(), | |
102 stub->AsWeakPtr())); | |
103 | |
104 sync_data = | |
105 SyncData::CreateLocalData("tag", "title", sync_pb::EntitySpecifics()); | |
106 sync_data_delete = | |
107 SyncData::CreateLocalDelete("tag", syncer::PREFERENCES); | |
108 | |
109 callback_get_or_download = | |
110 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, | |
111 base::Unretained(this)); | |
112 callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop, | |
113 base::Unretained(this)); | |
114 callback_store = base::Bind(&AttachmentServiceProxyTest::IncrementStore, | |
115 base::Unretained(this)); | |
116 count_callback_get_or_download = 0; | |
117 count_callback_drop = 0; | |
118 count_callback_store = 0; | |
119 } | |
120 | |
121 virtual void TearDown() | |
122 OVERRIDE { | |
123 // We must take care to call the stub's destructor on the stub_thread | |
124 // because that's the thread to which its WeakPtrs are bound. | |
125 if (stub) { | |
126 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); | |
127 WaitForStubThread(); | |
128 } | |
129 stub_thread->Stop(); | |
130 } | |
131 | |
132 // a GetOrDownloadCallback | |
133 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, | |
134 scoped_ptr<AttachmentMap>) { | |
135 CalledOnValidThread(); | |
136 ++count_callback_get_or_download; | |
137 } | |
138 | |
139 // a DropCallback | |
140 void IncrementDrop(const AttachmentService::DropResult&) { | |
141 CalledOnValidThread(); | |
142 ++count_callback_drop; | |
143 } | |
144 | |
145 // a StoreCallback | |
146 void IncrementStore(const AttachmentService::StoreResult&) { | |
147 CalledOnValidThread(); | |
148 ++count_callback_store; | |
149 } | |
150 | |
151 void WaitForStubThread() { | |
152 base::WaitableEvent done(false, false); | |
153 stub_thread->message_loop()->PostTask( | |
154 FROM_HERE, | |
155 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
156 done.Wait(); | |
157 } | |
158 | |
159 base::MessageLoop loop; | |
160 scoped_ptr<base::Thread> stub_thread; | |
161 scoped_ptr<StubAttachmentService> stub; | |
162 scoped_ptr<AttachmentServiceProxy> proxy; | |
163 | |
164 SyncData sync_data; | |
165 SyncData sync_data_delete; | |
166 | |
167 AttachmentService::GetOrDownloadCallback callback_get_or_download; | |
168 AttachmentService::DropCallback callback_drop; | |
169 AttachmentService::StoreCallback callback_store; | |
170 | |
171 // number of times callback_get_or_download was invoked | |
172 int count_callback_get_or_download; | |
173 // number of times callback_drop was invoked | |
174 int count_callback_drop; | |
175 // number of times callback_store was invoked | |
176 int count_callback_store; | |
177 }; | |
178 | |
179 // Verify that each of AttachmentServiceProxy's callback methods (those that | |
180 // take callbacks) are invoked on the stub and that the passed callbacks are | |
181 // invoked in this thread. | |
182 TEST_F(AttachmentServiceProxyTest, MethodsWithCallbacksAreProxied) { | |
183 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | |
184 proxy->DropAttachments(AttachmentIdList(), callback_drop); | |
185 proxy->StoreAttachments(AttachmentList(), callback_store); | |
186 // Wait for the posted calls to execute in the stub thread. | |
187 WaitForStubThread(); | |
188 EXPECT_EQ(3, stub->GetCallCount()); | |
189 // At this point the stub thread has finished executed the calls. However, the | |
190 // result callbacks it has posted may not have executed yet. Wait a second | |
191 // time to ensure the stub thread has executed the posted result callbacks. | |
192 WaitForStubThread(); | |
193 | |
194 loop.RunUntilIdle(); | |
195 EXPECT_EQ(1, count_callback_get_or_download); | |
196 EXPECT_EQ(1, count_callback_drop); | |
197 EXPECT_EQ(1, count_callback_store); | |
198 } | |
199 | |
200 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped | |
201 // AttachmentService has been destroyed. | |
202 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { | |
203 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | |
204 // Wait for the posted calls to execute in the stub thread. | |
205 WaitForStubThread(); | |
206 EXPECT_EQ(1, stub->GetCallCount()); | |
207 // Wait a second time ensure the stub thread has executed the posted result | |
208 // callbacks. | |
209 WaitForStubThread(); | |
210 | |
211 loop.RunUntilIdle(); | |
212 EXPECT_EQ(1, count_callback_get_or_download); | |
213 | |
214 // Destroy the stub and call GetOrDownloadAttachments again. | |
215 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); | |
216 WaitForStubThread(); | |
217 | |
218 // Now that the wrapped object has been destroyed, call again and see that we | |
219 // don't crash and the count remains the same. | |
220 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | |
221 WaitForStubThread(); | |
222 WaitForStubThread(); | |
223 loop.RunUntilIdle(); | |
224 EXPECT_EQ(1, count_callback_get_or_download); | |
225 } | |
226 | |
227 } // namespace syncer | |
OLD | NEW |