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 void GetOrDownloadAttachments(
37 const AttachmentIdList
& attachment_ids
,
38 const GetOrDownloadCallback
& callback
) override
{
39 CalledOnValidThread();
41 scoped_ptr
<AttachmentMap
> attachments(new AttachmentMap());
42 base::MessageLoop::current()->PostTask(
45 AttachmentService::GET_UNSPECIFIED_ERROR
,
46 base::Passed(&attachments
)));
49 void UploadAttachments(const AttachmentIdList
& attachments_ids
) override
{
50 CalledOnValidThread();
54 virtual base::WeakPtr
<AttachmentService
> AsWeakPtr() {
55 return weak_ptr_factory_
.GetWeakPtr();
58 // Return the number of method invocations.
59 int GetCallCount() const {
60 base::AutoLock
lock(mutex_
);
65 // Protects call_count_.
66 mutable base::Lock mutex_
;
69 // Must be last data member.
70 base::WeakPtrFactory
<AttachmentService
> weak_ptr_factory_
;
73 base::AutoLock
lock(mutex_
);
78 class AttachmentServiceProxyTest
: public testing::Test
,
79 public base::NonThreadSafe
{
81 AttachmentServiceProxyTest() {}
83 void SetUp() override
{
84 CalledOnValidThread();
85 stub_thread
.reset(new base::Thread("attachment service stub thread"));
87 stub
.reset(new StubAttachmentService
);
88 proxy
.reset(new AttachmentServiceProxy(stub_thread
->message_loop_proxy(),
91 callback_get_or_download
=
92 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload
,
93 base::Unretained(this));
94 count_callback_get_or_download
= 0;
97 void TearDown() override
{
98 // We must take care to call the stub's destructor on the stub_thread
99 // because that's the thread to which its WeakPtrs are bound.
101 stub_thread
->message_loop()->DeleteSoon(FROM_HERE
, stub
.release());
107 // a GetOrDownloadCallback
108 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult
&,
109 scoped_ptr
<AttachmentMap
>) {
110 CalledOnValidThread();
111 ++count_callback_get_or_download
;
114 void WaitForStubThread() {
115 base::WaitableEvent
done(false, false);
116 stub_thread
->message_loop()->PostTask(
118 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&done
)));
122 base::MessageLoop loop
;
123 scoped_ptr
<base::Thread
> stub_thread
;
124 scoped_ptr
<StubAttachmentService
> stub
;
125 scoped_ptr
<AttachmentServiceProxy
> proxy
;
127 AttachmentService::GetOrDownloadCallback callback_get_or_download
;
129 // number of times callback_get_or_download was invoked
130 int count_callback_get_or_download
;
133 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub.
134 // Verify that the methods that take callbacks invoke passed callbacks on this
136 TEST_F(AttachmentServiceProxyTest
, MethodsAreProxied
) {
137 proxy
->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download
);
138 proxy
->UploadAttachments(AttachmentIdList());
139 // Wait for the posted calls to execute in the stub thread.
141 EXPECT_EQ(2, stub
->GetCallCount());
142 // At this point the stub thread has finished executed the calls. However, the
143 // result callbacks it has posted may not have executed yet. Wait a second
144 // time to ensure the stub thread has executed the posted result callbacks.
147 base::RunLoop().RunUntilIdle();
148 EXPECT_EQ(1, count_callback_get_or_download
);
151 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped
152 // AttachmentService has been destroyed.
153 TEST_F(AttachmentServiceProxyTest
, WrappedIsDestroyed
) {
154 proxy
->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download
);
155 // Wait for the posted calls to execute in the stub thread.
157 EXPECT_EQ(1, stub
->GetCallCount());
158 // Wait a second time ensure the stub thread has executed the posted result
162 base::RunLoop().RunUntilIdle();
163 EXPECT_EQ(1, count_callback_get_or_download
);
165 // Destroy the stub and call GetOrDownloadAttachments again.
166 stub_thread
->message_loop()->DeleteSoon(FROM_HERE
, stub
.release());
169 // Now that the wrapped object has been destroyed, call again and see that we
170 // don't crash and the count remains the same.
171 proxy
->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download
);
174 base::RunLoop().RunUntilIdle();
175 EXPECT_EQ(1, count_callback_get_or_download
);
178 } // namespace syncer