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.
5 #include "sync/internal_api/public/attachments/attachment_service_proxy.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/synchronization/lock.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/threading/thread.h"
15 #include "sync/api/attachments/attachment.h"
16 #include "sync/internal_api/public/attachments/attachment_service.h"
17 #include "sync/internal_api/public/base/model_type.h"
18 #include "sync/protocol/sync.pb.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 // A stub implementation of AttachmentService that counts the number of times
24 // its methods are invoked.
25 class StubAttachmentService
: public AttachmentService
,
26 public base::NonThreadSafe
{
28 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) {
29 // DetachFromThread because we will be constructed in one thread and
30 // used/destroyed in another.
34 ~StubAttachmentService() override
{}
36 AttachmentStore
* GetStore() override
{ return NULL
; }
38 void GetOrDownloadAttachments(
39 const AttachmentIdList
& attachment_ids
,
40 const GetOrDownloadCallback
& callback
) override
{
41 CalledOnValidThread();
43 scoped_ptr
<AttachmentMap
> attachments(new AttachmentMap());
44 base::MessageLoop::current()->PostTask(
47 AttachmentService::GET_UNSPECIFIED_ERROR
,
48 base::Passed(&attachments
)));
51 void DropAttachments(const AttachmentIdList
& attachment_ids
,
52 const DropCallback
& callback
) override
{
53 CalledOnValidThread();
55 base::MessageLoop::current()->PostTask(
56 FROM_HERE
, base::Bind(callback
, AttachmentService::DROP_SUCCESS
));
59 void UploadAttachments(const AttachmentIdSet
& attachments_ids
) override
{
60 CalledOnValidThread();
64 virtual base::WeakPtr
<AttachmentService
> AsWeakPtr() {
65 return weak_ptr_factory_
.GetWeakPtr();
68 // Return the number of method invocations.
69 int GetCallCount() const {
70 base::AutoLock
lock(mutex_
);
75 // Protects call_count_.
76 mutable base::Lock mutex_
;
79 // Must be last data member.
80 base::WeakPtrFactory
<AttachmentService
> weak_ptr_factory_
;
83 base::AutoLock
lock(mutex_
);
88 class AttachmentServiceProxyTest
: public testing::Test
,
89 public base::NonThreadSafe
{
91 AttachmentServiceProxyTest() {}
93 void SetUp() override
{
94 CalledOnValidThread();
95 stub_thread
.reset(new base::Thread("attachment service stub thread"));
97 stub
.reset(new StubAttachmentService
);
98 proxy
.reset(new AttachmentServiceProxy(stub_thread
->message_loop_proxy(),
101 callback_get_or_download
=
102 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload
,
103 base::Unretained(this));
104 callback_drop
= base::Bind(&AttachmentServiceProxyTest::IncrementDrop
,
105 base::Unretained(this));
106 count_callback_get_or_download
= 0;
107 count_callback_drop
= 0;
110 void TearDown() override
{
111 // We must take care to call the stub's destructor on the stub_thread
112 // because that's the thread to which its WeakPtrs are bound.
114 stub_thread
->message_loop()->DeleteSoon(FROM_HERE
, stub
.release());
120 // a GetOrDownloadCallback
121 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult
&,
122 scoped_ptr
<AttachmentMap
>) {
123 CalledOnValidThread();
124 ++count_callback_get_or_download
;
128 void IncrementDrop(const AttachmentService::DropResult
&) {
129 CalledOnValidThread();
130 ++count_callback_drop
;
133 void WaitForStubThread() {
134 base::WaitableEvent
done(false, false);
135 stub_thread
->message_loop()->PostTask(
137 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&done
)));
141 base::MessageLoop loop
;
142 scoped_ptr
<base::Thread
> stub_thread
;
143 scoped_ptr
<StubAttachmentService
> stub
;
144 scoped_ptr
<AttachmentServiceProxy
> proxy
;
146 AttachmentService::GetOrDownloadCallback callback_get_or_download
;
147 AttachmentService::DropCallback callback_drop
;
149 // number of times callback_get_or_download was invoked
150 int count_callback_get_or_download
;
151 // number of times callback_drop was invoked
152 int count_callback_drop
;
155 TEST_F(AttachmentServiceProxyTest
, GetStore
) {
156 EXPECT_EQ(NULL
, proxy
->GetStore());
159 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub.
160 // Verify that the methods that take callbacks invoke passed callbacks on this
162 TEST_F(AttachmentServiceProxyTest
, MethodsAreProxied
) {
163 proxy
->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download
);
164 proxy
->DropAttachments(AttachmentIdList(), callback_drop
);
165 proxy
->UploadAttachments(AttachmentIdSet());
166 // Wait for the posted calls to execute in the stub thread.
168 EXPECT_EQ(3, stub
->GetCallCount());
169 // At this point the stub thread has finished executed the calls. However, the
170 // result callbacks it has posted may not have executed yet. Wait a second
171 // time to ensure the stub thread has executed the posted result callbacks.
174 base::RunLoop().RunUntilIdle();
175 EXPECT_EQ(1, count_callback_get_or_download
);
176 EXPECT_EQ(1, count_callback_drop
);
179 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped
180 // AttachmentService has been destroyed.
181 TEST_F(AttachmentServiceProxyTest
, WrappedIsDestroyed
) {
182 proxy
->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download
);
183 // Wait for the posted calls to execute in the stub thread.
185 EXPECT_EQ(1, stub
->GetCallCount());
186 // Wait a second time ensure the stub thread has executed the posted result
190 base::RunLoop().RunUntilIdle();
191 EXPECT_EQ(1, count_callback_get_or_download
);
193 // Destroy the stub and call GetOrDownloadAttachments again.
194 stub_thread
->message_loop()->DeleteSoon(FROM_HERE
, stub
.release());
197 // Now that the wrapped object has been destroyed, call again and see that we
198 // don't crash and the count remains the same.
199 proxy
->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download
);
202 base::RunLoop().RunUntilIdle();
203 EXPECT_EQ(1, count_callback_get_or_download
);
206 } // namespace syncer