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_service_proxy.h" | 5 #include "sync/internal_api/public/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 16 matching lines...) Expand all Loading... |
27 public base::NonThreadSafe { | 27 public base::NonThreadSafe { |
28 public: | 28 public: |
29 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) { | 29 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) { |
30 // DetachFromThread because we will be constructed in one thread and | 30 // DetachFromThread because we will be constructed in one thread and |
31 // used/destroyed in another. | 31 // used/destroyed in another. |
32 DetachFromThread(); | 32 DetachFromThread(); |
33 } | 33 } |
34 | 34 |
35 virtual ~StubAttachmentService() {} | 35 virtual ~StubAttachmentService() {} |
36 | 36 |
| 37 virtual AttachmentStore* GetStore() OVERRIDE { return NULL; } |
| 38 |
37 virtual void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids, | 39 virtual void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids, |
38 const GetOrDownloadCallback& callback) | 40 const GetOrDownloadCallback& callback) |
39 OVERRIDE { | 41 OVERRIDE { |
40 CalledOnValidThread(); | 42 CalledOnValidThread(); |
41 Increment(); | 43 Increment(); |
42 scoped_ptr<AttachmentMap> attachments(new AttachmentMap()); | 44 scoped_ptr<AttachmentMap> attachments(new AttachmentMap()); |
43 base::MessageLoop::current()->PostTask( | 45 base::MessageLoop::current()->PostTask( |
44 FROM_HERE, | 46 FROM_HERE, |
45 base::Bind(callback, | 47 base::Bind(callback, |
46 AttachmentService::GET_UNSPECIFIED_ERROR, | 48 AttachmentService::GET_UNSPECIFIED_ERROR, |
47 base::Passed(&attachments))); | 49 base::Passed(&attachments))); |
48 } | 50 } |
49 | 51 |
50 virtual void DropAttachments(const AttachmentIdList& attachment_ids, | 52 virtual void DropAttachments(const AttachmentIdList& attachment_ids, |
51 const DropCallback& callback) OVERRIDE { | 53 const DropCallback& callback) OVERRIDE { |
52 CalledOnValidThread(); | 54 CalledOnValidThread(); |
53 Increment(); | 55 Increment(); |
54 base::MessageLoop::current()->PostTask( | 56 base::MessageLoop::current()->PostTask( |
55 FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS)); | 57 FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS)); |
56 } | 58 } |
57 | 59 |
58 virtual void StoreAttachments(const AttachmentList& attachments, | 60 virtual void UploadAttachments( |
59 const StoreCallback& callback) OVERRIDE { | 61 const AttachmentIdSet& attachments_ids) OVERRIDE { |
60 CalledOnValidThread(); | 62 CalledOnValidThread(); |
61 Increment(); | 63 Increment(); |
62 base::MessageLoop::current()->PostTask( | |
63 FROM_HERE, base::Bind(callback, AttachmentService::STORE_SUCCESS)); | |
64 } | 64 } |
65 | 65 |
66 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { | 66 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { |
67 return weak_ptr_factory_.GetWeakPtr(); | 67 return weak_ptr_factory_.GetWeakPtr(); |
68 } | 68 } |
69 | 69 |
70 // Return the number of method invocations. | 70 // Return the number of method invocations. |
71 int GetCallCount() const { | 71 int GetCallCount() const { |
72 base::AutoLock lock(mutex_); | 72 base::AutoLock lock(mutex_); |
73 return call_count_; | 73 return call_count_; |
(...skipping 24 matching lines...) Expand all Loading... |
98 stub_thread->Start(); | 98 stub_thread->Start(); |
99 stub.reset(new StubAttachmentService); | 99 stub.reset(new StubAttachmentService); |
100 proxy.reset(new AttachmentServiceProxy(stub_thread->message_loop_proxy(), | 100 proxy.reset(new AttachmentServiceProxy(stub_thread->message_loop_proxy(), |
101 stub->AsWeakPtr())); | 101 stub->AsWeakPtr())); |
102 | 102 |
103 callback_get_or_download = | 103 callback_get_or_download = |
104 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, | 104 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, |
105 base::Unretained(this)); | 105 base::Unretained(this)); |
106 callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop, | 106 callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop, |
107 base::Unretained(this)); | 107 base::Unretained(this)); |
108 callback_store = base::Bind(&AttachmentServiceProxyTest::IncrementStore, | |
109 base::Unretained(this)); | |
110 count_callback_get_or_download = 0; | 108 count_callback_get_or_download = 0; |
111 count_callback_drop = 0; | 109 count_callback_drop = 0; |
112 count_callback_store = 0; | |
113 } | 110 } |
114 | 111 |
115 virtual void TearDown() | 112 virtual void TearDown() |
116 OVERRIDE { | 113 OVERRIDE { |
117 // We must take care to call the stub's destructor on the stub_thread | 114 // We must take care to call the stub's destructor on the stub_thread |
118 // because that's the thread to which its WeakPtrs are bound. | 115 // because that's the thread to which its WeakPtrs are bound. |
119 if (stub) { | 116 if (stub) { |
120 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); | 117 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release()); |
121 WaitForStubThread(); | 118 WaitForStubThread(); |
122 } | 119 } |
123 stub_thread->Stop(); | 120 stub_thread->Stop(); |
124 } | 121 } |
125 | 122 |
126 // a GetOrDownloadCallback | 123 // a GetOrDownloadCallback |
127 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, | 124 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, |
128 scoped_ptr<AttachmentMap>) { | 125 scoped_ptr<AttachmentMap>) { |
129 CalledOnValidThread(); | 126 CalledOnValidThread(); |
130 ++count_callback_get_or_download; | 127 ++count_callback_get_or_download; |
131 } | 128 } |
132 | 129 |
133 // a DropCallback | 130 // a DropCallback |
134 void IncrementDrop(const AttachmentService::DropResult&) { | 131 void IncrementDrop(const AttachmentService::DropResult&) { |
135 CalledOnValidThread(); | 132 CalledOnValidThread(); |
136 ++count_callback_drop; | 133 ++count_callback_drop; |
137 } | 134 } |
138 | 135 |
139 // a StoreCallback | |
140 void IncrementStore(const AttachmentService::StoreResult&) { | |
141 CalledOnValidThread(); | |
142 ++count_callback_store; | |
143 } | |
144 | |
145 void WaitForStubThread() { | 136 void WaitForStubThread() { |
146 base::WaitableEvent done(false, false); | 137 base::WaitableEvent done(false, false); |
147 stub_thread->message_loop()->PostTask( | 138 stub_thread->message_loop()->PostTask( |
148 FROM_HERE, | 139 FROM_HERE, |
149 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | 140 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
150 done.Wait(); | 141 done.Wait(); |
151 } | 142 } |
152 | 143 |
153 base::MessageLoop loop; | 144 base::MessageLoop loop; |
154 scoped_ptr<base::Thread> stub_thread; | 145 scoped_ptr<base::Thread> stub_thread; |
155 scoped_ptr<StubAttachmentService> stub; | 146 scoped_ptr<StubAttachmentService> stub; |
156 scoped_ptr<AttachmentServiceProxy> proxy; | 147 scoped_ptr<AttachmentServiceProxy> proxy; |
157 | 148 |
158 AttachmentService::GetOrDownloadCallback callback_get_or_download; | 149 AttachmentService::GetOrDownloadCallback callback_get_or_download; |
159 AttachmentService::DropCallback callback_drop; | 150 AttachmentService::DropCallback callback_drop; |
160 AttachmentService::StoreCallback callback_store; | |
161 | 151 |
162 // number of times callback_get_or_download was invoked | 152 // number of times callback_get_or_download was invoked |
163 int count_callback_get_or_download; | 153 int count_callback_get_or_download; |
164 // number of times callback_drop was invoked | 154 // number of times callback_drop was invoked |
165 int count_callback_drop; | 155 int count_callback_drop; |
166 // number of times callback_store was invoked | |
167 int count_callback_store; | |
168 }; | 156 }; |
169 | 157 |
170 // Verify that each of AttachmentServiceProxy's callback methods (those that | 158 TEST_F(AttachmentServiceProxyTest, GetStore) { |
171 // take callbacks) are invoked on the stub and that the passed callbacks are | 159 EXPECT_EQ(NULL, proxy->GetStore()); |
172 // invoked in this thread. | 160 } |
173 TEST_F(AttachmentServiceProxyTest, MethodsWithCallbacksAreProxied) { | 161 |
| 162 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub. |
| 163 // Verify that the methods that take callbacks invoke passed callbacks on this |
| 164 // thread. |
| 165 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { |
174 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 166 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
175 proxy->DropAttachments(AttachmentIdList(), callback_drop); | 167 proxy->DropAttachments(AttachmentIdList(), callback_drop); |
176 proxy->StoreAttachments(AttachmentList(), callback_store); | 168 proxy->UploadAttachments(AttachmentIdSet()); |
177 // Wait for the posted calls to execute in the stub thread. | 169 // Wait for the posted calls to execute in the stub thread. |
178 WaitForStubThread(); | 170 WaitForStubThread(); |
179 EXPECT_EQ(3, stub->GetCallCount()); | 171 EXPECT_EQ(3, stub->GetCallCount()); |
180 // At this point the stub thread has finished executed the calls. However, the | 172 // At this point the stub thread has finished executed the calls. However, the |
181 // result callbacks it has posted may not have executed yet. Wait a second | 173 // result callbacks it has posted may not have executed yet. Wait a second |
182 // time to ensure the stub thread has executed the posted result callbacks. | 174 // time to ensure the stub thread has executed the posted result callbacks. |
183 WaitForStubThread(); | 175 WaitForStubThread(); |
184 | 176 |
185 loop.RunUntilIdle(); | 177 loop.RunUntilIdle(); |
186 EXPECT_EQ(1, count_callback_get_or_download); | 178 EXPECT_EQ(1, count_callback_get_or_download); |
187 EXPECT_EQ(1, count_callback_drop); | 179 EXPECT_EQ(1, count_callback_drop); |
188 EXPECT_EQ(1, count_callback_store); | |
189 } | 180 } |
190 | 181 |
191 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped | 182 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped |
192 // AttachmentService has been destroyed. | 183 // AttachmentService has been destroyed. |
193 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { | 184 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { |
194 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 185 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
195 // Wait for the posted calls to execute in the stub thread. | 186 // Wait for the posted calls to execute in the stub thread. |
196 WaitForStubThread(); | 187 WaitForStubThread(); |
197 EXPECT_EQ(1, stub->GetCallCount()); | 188 EXPECT_EQ(1, stub->GetCallCount()); |
198 // Wait a second time ensure the stub thread has executed the posted result | 189 // Wait a second time ensure the stub thread has executed the posted result |
(...skipping 10 matching lines...) Expand all Loading... |
209 // Now that the wrapped object has been destroyed, call again and see that we | 200 // Now that the wrapped object has been destroyed, call again and see that we |
210 // don't crash and the count remains the same. | 201 // don't crash and the count remains the same. |
211 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 202 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); |
212 WaitForStubThread(); | 203 WaitForStubThread(); |
213 WaitForStubThread(); | 204 WaitForStubThread(); |
214 loop.RunUntilIdle(); | 205 loop.RunUntilIdle(); |
215 EXPECT_EQ(1, count_callback_get_or_download); | 206 EXPECT_EQ(1, count_callback_get_or_download); |
216 } | 207 } |
217 | 208 |
218 } // namespace syncer | 209 } // namespace syncer |
OLD | NEW |