This source file includes following definitions.
- weak_ptr_factory_
- GetOrDownloadAttachments
- DropAttachments
- OnSyncDataAdd
- OnSyncDataDelete
- OnSyncDataUpdate
- AsWeakPtr
- GetCallCount
- Increment
- SetUp
- TearDown
- IncrementGetOrDownload
- IncrementDrop
- WaitForStubThread
- TEST_F
- TEST_F
- TEST_F
#include "sync/api/attachments/attachment_service_proxy.h"
#include "base/bind.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/non_thread_safe.h"
#include "base/threading/thread.h"
#include "sync/api/attachments/attachment.h"
#include "sync/api/attachments/attachment_service.h"
#include "sync/api/sync_data.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/protocol/sync.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
class StubAttachmentService : public AttachmentService,
public base::NonThreadSafe {
public:
StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) {
DetachFromThread();
}
virtual ~StubAttachmentService() {}
virtual void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids,
const GetOrDownloadCallback& callback)
OVERRIDE {
CalledOnValidThread();
Increment();
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(
callback, AttachmentService::GET_NOT_FOUND, AttachmentMap()));
}
virtual void DropAttachments(const AttachmentIdList& attachment_ids,
const DropCallback& callback) OVERRIDE {
CalledOnValidThread();
Increment();
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, AttachmentService::DROP_SUCCESS));
}
virtual void OnSyncDataAdd(const SyncData& sync_data) OVERRIDE {
CalledOnValidThread();
Increment();
}
virtual void OnSyncDataDelete(const SyncData& sync_data) OVERRIDE {
CalledOnValidThread();
Increment();
}
virtual void OnSyncDataUpdate(const AttachmentIdList& old_attachment_ids,
const SyncData& updated_sync_data) OVERRIDE {
CalledOnValidThread();
Increment();
}
virtual base::WeakPtr<AttachmentService> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
int GetCallCount() const {
base::AutoLock lock(mutex_);
return call_count_;
}
private:
mutable base::Lock mutex_;
int call_count_;
base::WeakPtrFactory<AttachmentService> weak_ptr_factory_;
void Increment() {
base::AutoLock lock(mutex_);
++call_count_;
}
};
class AttachmentServiceProxyTest : public testing::Test,
public base::NonThreadSafe {
protected:
AttachmentServiceProxyTest() {}
virtual void SetUp() {
CalledOnValidThread();
stub_thread.reset(new base::Thread("attachment service stub thread"));
stub_thread->Start();
stub.reset(new StubAttachmentService);
proxy.reset(new AttachmentServiceProxy(stub_thread->message_loop_proxy(),
stub->AsWeakPtr()));
sync_data =
SyncData::CreateLocalData("tag", "title", sync_pb::EntitySpecifics());
sync_data_delete =
SyncData::CreateLocalDelete("tag", syncer::PREFERENCES);
callback_get_or_download =
base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload,
base::Unretained(this));
callback_drop = base::Bind(&AttachmentServiceProxyTest::IncrementDrop,
base::Unretained(this));
count_callback_get_or_download = 0;
count_callback_drop = 0;
}
virtual void TearDown()
OVERRIDE {
if (stub) {
stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release());
WaitForStubThread();
}
stub_thread->Stop();
}
void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&,
const AttachmentMap&) {
CalledOnValidThread();
++count_callback_get_or_download;
}
void IncrementDrop(const AttachmentService::DropResult&) {
CalledOnValidThread();
++count_callback_drop;
}
void WaitForStubThread() {
base::WaitableEvent done(false, false);
stub_thread->message_loop()->PostTask(
FROM_HERE,
base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
done.Wait();
}
base::MessageLoop loop;
scoped_ptr<base::Thread> stub_thread;
scoped_ptr<StubAttachmentService> stub;
scoped_ptr<AttachmentServiceProxy> proxy;
SyncData sync_data;
SyncData sync_data_delete;
AttachmentService::GetOrDownloadCallback callback_get_or_download;
AttachmentService::DropCallback callback_drop;
int count_callback_get_or_download;
int count_callback_drop;
};
TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) {
proxy->OnSyncDataAdd(sync_data);
proxy->OnSyncDataDelete(sync_data_delete);
proxy->OnSyncDataUpdate(AttachmentIdList(), sync_data);
WaitForStubThread();
EXPECT_EQ(3, stub->GetCallCount());
}
TEST_F(AttachmentServiceProxyTest, MethodsWithCallbacksAreProxied) {
proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
proxy->DropAttachments(AttachmentIdList(), callback_drop);
WaitForStubThread();
EXPECT_EQ(2, stub->GetCallCount());
WaitForStubThread();
loop.RunUntilIdle();
EXPECT_EQ(1, count_callback_get_or_download);
EXPECT_EQ(1, count_callback_drop);
}
TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) {
proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
WaitForStubThread();
EXPECT_EQ(1, stub->GetCallCount());
WaitForStubThread();
loop.RunUntilIdle();
EXPECT_EQ(1, count_callback_get_or_download);
stub_thread->message_loop()->DeleteSoon(FROM_HERE, stub.release());
WaitForStubThread();
proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
WaitForStubThread();
WaitForStubThread();
loop.RunUntilIdle();
EXPECT_EQ(1, count_callback_get_or_download);
}
}