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

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

Powered by Google App Engine
This is Rietveld 408576698