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_store_handle.h"
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "sync/api/attachments/attachment.h"
15 #include "sync/api/attachments/attachment_id.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 class MockAttachmentStore : public AttachmentStoreBase {
24 MockAttachmentStore(const base::Closure& read_called,
25 const base::Closure& write_called,
26 const base::Closure& drop_called,
27 const base::Closure& dtor_called)
28 : read_called_(read_called),
29 write_called_(write_called),
30 drop_called_(drop_called),
31 dtor_called_(dtor_called) {}
33 ~MockAttachmentStore() override { dtor_called_.Run(); }
35 void Read(const AttachmentIdList& ids,
36 const ReadCallback& callback) override {
40 void Write(const AttachmentList& attachments,
41 const WriteCallback& callback) override {
45 void Drop(const AttachmentIdList& ids,
46 const DropCallback& callback) override {
50 base::Closure read_called_;
51 base::Closure write_called_;
52 base::Closure drop_called_;
53 base::Closure dtor_called_;
58 class AttachmentStoreHandleTest : public testing::Test {
60 AttachmentStoreHandleTest()
61 : read_call_count_(0),
64 dtor_call_count_(0) {}
66 virtual void SetUp() {
67 scoped_ptr<AttachmentStoreBase> backend(new MockAttachmentStore(
68 base::Bind(&AttachmentStoreHandleTest::ReadCalled,
69 base::Unretained(this)),
70 base::Bind(&AttachmentStoreHandleTest::WriteCalled,
71 base::Unretained(this)),
72 base::Bind(&AttachmentStoreHandleTest::DropCalled,
73 base::Unretained(this)),
74 base::Bind(&AttachmentStoreHandleTest::DtorCalled,
75 base::Unretained(this))));
76 attachment_store_handle_ = new AttachmentStoreHandle(
77 backend.Pass(), base::ThreadTaskRunnerHandle::Get());
80 static void DoneWithResult(const AttachmentStore::Result& result) {
84 static void ReadDone(const AttachmentStore::Result& result,
85 scoped_ptr<AttachmentMap> attachments,
86 scoped_ptr<AttachmentIdList> unavailable_attachments) {
90 void ReadCalled() { ++read_call_count_; }
92 void WriteCalled() { ++write_call_count_; }
94 void DropCalled() { ++drop_call_count_; }
96 void DtorCalled() { ++dtor_call_count_; }
98 void RunMessageLoop() {
99 base::RunLoop run_loop;
100 run_loop.RunUntilIdle();
103 base::MessageLoop message_loop_;
104 scoped_refptr<AttachmentStoreHandle> attachment_store_handle_;
105 int read_call_count_;
106 int write_call_count_;
107 int drop_call_count_;
108 int dtor_call_count_;
111 // Test that method calls are forwarded to backend loop
112 TEST_F(AttachmentStoreHandleTest, MethodsCalled) {
113 AttachmentIdList ids;
114 AttachmentList attachments;
116 attachment_store_handle_->Read(
117 ids, base::Bind(&AttachmentStoreHandleTest::ReadDone));
118 EXPECT_EQ(read_call_count_, 0);
120 EXPECT_EQ(read_call_count_, 1);
122 attachment_store_handle_->Write(
123 attachments, base::Bind(&AttachmentStoreHandleTest::DoneWithResult));
124 EXPECT_EQ(write_call_count_, 0);
126 EXPECT_EQ(write_call_count_, 1);
128 attachment_store_handle_->Drop(
129 ids, base::Bind(&AttachmentStoreHandleTest::DoneWithResult));
130 EXPECT_EQ(drop_call_count_, 0);
132 EXPECT_EQ(drop_call_count_, 1);
134 // Releasing referehce to AttachmentStoreHandle should result in
135 // MockAttachmentStore being deleted on backend loop.
136 attachment_store_handle_ = NULL;
137 EXPECT_EQ(dtor_call_count_, 0);
139 EXPECT_EQ(dtor_call_count_, 1);
142 } // namespace syncer