Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / attachments / attachment_store_handle_unittest.cc
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/internal_api/public/attachments/attachment_store_handle.h"
6
7 #include "base/bind.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"
17
18 namespace syncer {
19
20 namespace {
21
22 class MockAttachmentStore : public AttachmentStoreBase {
23  public:
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) {}
32
33   ~MockAttachmentStore() override { dtor_called_.Run(); }
34
35   void Read(const AttachmentIdList& ids,
36             const ReadCallback& callback) override {
37     read_called_.Run();
38   }
39
40   void Write(const AttachmentList& attachments,
41              const WriteCallback& callback) override {
42     write_called_.Run();
43   }
44
45   void Drop(const AttachmentIdList& ids,
46             const DropCallback& callback) override {
47     drop_called_.Run();
48   }
49
50   base::Closure read_called_;
51   base::Closure write_called_;
52   base::Closure drop_called_;
53   base::Closure dtor_called_;
54 };
55
56 }  // namespace
57
58 class AttachmentStoreHandleTest : public testing::Test {
59  protected:
60   AttachmentStoreHandleTest()
61       : read_call_count_(0),
62         write_call_count_(0),
63         drop_call_count_(0),
64         dtor_call_count_(0) {}
65
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());
78   }
79
80   static void DoneWithResult(const AttachmentStore::Result& result) {
81     NOTREACHED();
82   }
83
84   static void ReadDone(const AttachmentStore::Result& result,
85                        scoped_ptr<AttachmentMap> attachments,
86                        scoped_ptr<AttachmentIdList> unavailable_attachments) {
87     NOTREACHED();
88   }
89
90   void ReadCalled() { ++read_call_count_; }
91
92   void WriteCalled() { ++write_call_count_; }
93
94   void DropCalled() { ++drop_call_count_; }
95
96   void DtorCalled() { ++dtor_call_count_; }
97
98   void RunMessageLoop() {
99     base::RunLoop run_loop;
100     run_loop.RunUntilIdle();
101   }
102
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_;
109 };
110
111 // Test that method calls are forwarded to backend loop
112 TEST_F(AttachmentStoreHandleTest, MethodsCalled) {
113   AttachmentIdList ids;
114   AttachmentList attachments;
115
116   attachment_store_handle_->Read(
117       ids, base::Bind(&AttachmentStoreHandleTest::ReadDone));
118   EXPECT_EQ(read_call_count_, 0);
119   RunMessageLoop();
120   EXPECT_EQ(read_call_count_, 1);
121
122   attachment_store_handle_->Write(
123       attachments, base::Bind(&AttachmentStoreHandleTest::DoneWithResult));
124   EXPECT_EQ(write_call_count_, 0);
125   RunMessageLoop();
126   EXPECT_EQ(write_call_count_, 1);
127
128   attachment_store_handle_->Drop(
129       ids, base::Bind(&AttachmentStoreHandleTest::DoneWithResult));
130   EXPECT_EQ(drop_call_count_, 0);
131   RunMessageLoop();
132   EXPECT_EQ(drop_call_count_, 1);
133
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);
138   RunMessageLoop();
139   EXPECT_EQ(dtor_call_count_, 1);
140 }
141
142 }  // namespace syncer