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