Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: sync/api/attachments/attachment_service_proxy_unittest.cc

Issue 187303006: Update sync API to support attachments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@attachmentapi
Patch Set: Rename GetAttachments to GetLocalAttachmentsForUpload. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 base::MessageLoop::current()->PostTask(
44 FROM_HERE,
45 base::Bind(
46 callback, AttachmentService::GET_NOT_FOUND, AttachmentMap()));
47 }
48
49 virtual void DropAttachments(const AttachmentIdList& attachment_ids,
50 const DropCallback& callback) OVERRIDE {
51 CalledOnValidThread();
52 Increment();
53 base::MessageLoop::current()->PostTask(
54 FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS));
55 }
56
57 virtual void OnSyncDataAdd(const SyncData& sync_data) OVERRIDE {
58 CalledOnValidThread();
59 Increment();
60 }
61
62 virtual void OnSyncDataDelete(const SyncData& sync_data) OVERRIDE {
63 CalledOnValidThread();
64 Increment();
65 }
66
67 virtual void OnSyncDataUpdate(const AttachmentIdList& old_attachment_ids,
68 const SyncData& updated_sync_data) OVERRIDE {
69 CalledOnValidThread();
70 Increment();
71 }
72
73 virtual base::WeakPtr<AttachmentService> AsWeakPtr() {
74 return weak_ptr_factory_.GetWeakPtr();
75 }
76
77 // Return the number of method invocations.
78 int GetCallCount() const {
79 base::AutoLock lock(mutex_);
80 return call_count_;
81 }
82
83 private:
84 // Protects call_count_.
85 mutable base::Lock mutex_;
86 int call_count_;
87
88 // Must be last data member.
89 base::WeakPtrFactory<AttachmentService> weak_ptr_factory_;
90
91 void Increment() {
92 base::AutoLock lock(mutex_);
93 ++call_count_;
94 }
95 };
96
97 class AttachmentServiceProxyTest : public testing::Test,
98 public base::NonThreadSafe {
99 protected:
100 AttachmentServiceProxyTest() {}
101
102 virtual void SetUp() {
103 CalledOnValidThread();
104 stub_thread.reset(new base::Thread("attachment service stub thread"));
105 stub_thread->Start();
106 stub.reset(new StubAttachmentService);
107 proxy.reset(new AttachmentServiceProxy(stub_thread->message_loop_proxy(),
108 stub->AsWeakPtr()));
109
110 sync_data =
111 SyncData::CreateLocalData("tag", "title", sync_pb::EntitySpecifics());
112 sync_data_delete =
113 SyncData::CreateLocalDelete("tag", syncer::PREFERENCES);
114
115 callback_get_or_download =
116 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload,
117 base::Unretained(this));
118 callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop,
119 base::Unretained(this));
120 count_callback_get_or_download = 0;
121 count_callback_drop = 0;
122 }
123
124 virtual void TearDown()
125 OVERRIDE {
126 // We must take care to call the stub's destructor on the stub_thread
127 // because that's the thread to which its WeakPtrs are bound.
128 if (stub) {
129 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release());
130 WaitForStubThread();
131 }
132 stub_thread->Stop();
133 }
134
135 // a GetOrDownloadCallback
136 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&,
137 const AttachmentMap&) {
138 CalledOnValidThread();
139 ++count_callback_get_or_download;
140 }
141
142 // a DropCallback
143 void IncrementDrop(const AttachmentService::DropResult&) {
144 CalledOnValidThread();
145 ++count_callback_drop;
146 }
147
148 void WaitForStubThread() {
149 base::WaitableEvent done(false, false);
150 stub_thread->message_loop()->PostTask(
151 FROM_HERE,
152 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
153 done.Wait();
154 }
155
156 base::MessageLoop loop;
157 scoped_ptr<base::Thread> stub_thread;
158 scoped_ptr<StubAttachmentService> stub;
159 scoped_ptr<AttachmentServiceProxy> proxy;
160
161 SyncData sync_data;
162 SyncData sync_data_delete;
163
164 AttachmentService::GetOrDownloadCallback callback_get_or_download;
165 AttachmentService::DropCallback callback_drop;
166
167 // number of times callback_get_or_download was invoked
168 int count_callback_get_or_download;
169 // number of times callback_drop was invoked
170 int count_callback_drop;
171 };
172
173 // Verify that each of AttachmentServiceProxy's regular methods (those that
174 // don't take callbacks) are invoked on the stub.
175 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) {
176 proxy->OnSyncDataAdd(sync_data);
177 proxy->OnSyncDataDelete(sync_data_delete);
178 proxy->OnSyncDataUpdate(AttachmentIdList(), sync_data);
179 WaitForStubThread();
180 EXPECT_EQ(3, stub->GetCallCount());
181 }
182
183 // Verify that each of AttachmentServiceProxy's callback methods (those that
184 // take callbacks) are invoked on the stub and that the passed callbacks are
185 // invoked in this thread.
186 TEST_F(AttachmentServiceProxyTest, MethodsWithCallbacksAreProxied) {
187 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
188 proxy->DropAttachments(AttachmentIdList(), callback_drop);
189 // Wait for the posted calls to execute in the stub thread.
190 WaitForStubThread();
191 EXPECT_EQ(2, stub->GetCallCount());
192 // At this point the stub thread has finished executed the calls. However, the
193 // result callbacks it has posted may not have executed yet. Wait a second
194 // time to ensure the stub thread has executed the posted result callbacks.
195 WaitForStubThread();
196
197 loop.RunUntilIdle();
198 EXPECT_EQ(1, count_callback_get_or_download);
199 EXPECT_EQ(1, count_callback_drop);
200 }
201
202 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped
203 // AttachmentService has been destroyed.
204 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) {
205 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
206 // Wait for the posted calls to execute in the stub thread.
207 WaitForStubThread();
208 EXPECT_EQ(1, stub->GetCallCount());
209 // Wait a second time ensure the stub thread has executed the posted result
210 // callbacks.
211 WaitForStubThread();
212
213 loop.RunUntilIdle();
214 EXPECT_EQ(1, count_callback_get_or_download);
215
216 // Destroy the stub and call GetOrDownloadAttachments again.
217 stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release());
218 WaitForStubThread();
219
220 // Now that the wrapped object has been destroyed, call again and see that we
221 // don't crash and the count remains the same.
222 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
223 WaitForStubThread();
224 WaitForStubThread();
225 loop.RunUntilIdle();
226 EXPECT_EQ(1, count_callback_get_or_download);
227 }
228
229 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/api/attachments/attachment_service_proxy.cc ('k') | sync/api/attachments/attachment_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698