OLD | NEW |
| (Empty) |
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 virtual ~MockAttachmentStore() { | |
34 dtor_called_.Run(); | |
35 } | |
36 | |
37 virtual void Read(const AttachmentIdList& ids, | |
38 const ReadCallback& callback) OVERRIDE { | |
39 read_called_.Run(); | |
40 } | |
41 | |
42 virtual void Write(const AttachmentList& attachments, | |
43 const WriteCallback& callback) OVERRIDE { | |
44 write_called_.Run(); | |
45 } | |
46 | |
47 virtual void Drop(const AttachmentIdList& ids, | |
48 const DropCallback& callback) OVERRIDE { | |
49 drop_called_.Run(); | |
50 } | |
51 | |
52 base::Closure read_called_; | |
53 base::Closure write_called_; | |
54 base::Closure drop_called_; | |
55 base::Closure dtor_called_; | |
56 }; | |
57 | |
58 } // namespace | |
59 | |
60 class AttachmentStoreHandleTest : public testing::Test { | |
61 protected: | |
62 AttachmentStoreHandleTest() | |
63 : read_call_count_(0), | |
64 write_call_count_(0), | |
65 drop_call_count_(0), | |
66 dtor_call_count_(0) {} | |
67 | |
68 virtual void SetUp() { | |
69 scoped_ptr<AttachmentStoreBase> backend(new MockAttachmentStore( | |
70 base::Bind(&AttachmentStoreHandleTest::ReadCalled, | |
71 base::Unretained(this)), | |
72 base::Bind(&AttachmentStoreHandleTest::WriteCalled, | |
73 base::Unretained(this)), | |
74 base::Bind(&AttachmentStoreHandleTest::DropCalled, | |
75 base::Unretained(this)), | |
76 base::Bind(&AttachmentStoreHandleTest::DtorCalled, | |
77 base::Unretained(this)))); | |
78 attachment_store_handle_ = new AttachmentStoreHandle( | |
79 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); | |
80 } | |
81 | |
82 static void ReadDone(const AttachmentStore::Result& result, | |
83 scoped_ptr<AttachmentMap> attachments, | |
84 scoped_ptr<AttachmentIdList> unavailable_attachments) { | |
85 NOTREACHED(); | |
86 } | |
87 | |
88 static void WriteDone(const AttachmentStore::Result& result) { | |
89 NOTREACHED(); | |
90 } | |
91 | |
92 static void DropDone(const AttachmentStore::Result& result) { | |
93 NOTREACHED(); | |
94 } | |
95 | |
96 void ReadCalled() { ++read_call_count_; } | |
97 | |
98 void WriteCalled() { ++write_call_count_; } | |
99 | |
100 void DropCalled() { ++drop_call_count_; } | |
101 | |
102 void DtorCalled() { ++dtor_call_count_; } | |
103 | |
104 void RunMessageLoop() { | |
105 base::RunLoop run_loop; | |
106 run_loop.RunUntilIdle(); | |
107 } | |
108 | |
109 base::MessageLoop message_loop_; | |
110 scoped_refptr<AttachmentStoreHandle> attachment_store_handle_; | |
111 int read_call_count_; | |
112 int write_call_count_; | |
113 int drop_call_count_; | |
114 int dtor_call_count_; | |
115 }; | |
116 | |
117 // Test that method calls are forwarded to backend loop | |
118 TEST_F(AttachmentStoreHandleTest, MethodsCalled) { | |
119 AttachmentIdList ids; | |
120 AttachmentList attachments; | |
121 | |
122 attachment_store_handle_->Read( | |
123 ids, base::Bind(&AttachmentStoreHandleTest::ReadDone)); | |
124 EXPECT_EQ(read_call_count_, 0); | |
125 RunMessageLoop(); | |
126 EXPECT_EQ(read_call_count_, 1); | |
127 | |
128 attachment_store_handle_->Write( | |
129 attachments, base::Bind(&AttachmentStoreHandleTest::WriteDone)); | |
130 EXPECT_EQ(write_call_count_, 0); | |
131 RunMessageLoop(); | |
132 EXPECT_EQ(write_call_count_, 1); | |
133 | |
134 attachment_store_handle_->Drop( | |
135 ids, base::Bind(&AttachmentStoreHandleTest::DropDone)); | |
136 EXPECT_EQ(drop_call_count_, 0); | |
137 RunMessageLoop(); | |
138 EXPECT_EQ(drop_call_count_, 1); | |
139 | |
140 // Releasing referehce to AttachmentStoreHandle should result in | |
141 // MockAttachmentStore being deleted on backend loop. | |
142 attachment_store_handle_ = NULL; | |
143 EXPECT_EQ(dtor_call_count_, 0); | |
144 RunMessageLoop(); | |
145 EXPECT_EQ(dtor_call_count_, 1); | |
146 } | |
147 | |
148 } // namespace syncer | |
OLD | NEW |