1 // Copyright 2013 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 "mojo/system/core_test_base.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "mojo/system/constants.h"
13 #include "mojo/system/core_impl.h"
14 #include "mojo/system/dispatcher.h"
15 #include "mojo/system/memory.h"
23 // MockDispatcher --------------------------------------------------------------
25 class MockDispatcher
: public Dispatcher
{
27 explicit MockDispatcher(CoreTestBase::MockHandleInfo
* info
)
30 info_
->IncrementCtorCallCount();
33 virtual Type
GetType() const OVERRIDE
{
38 friend class base::RefCountedThreadSafe
<MockDispatcher
>;
39 virtual ~MockDispatcher() {
40 info_
->IncrementDtorCallCount();
43 // |Dispatcher| implementation/overrides:
44 virtual void CloseImplNoLock() OVERRIDE
{
45 info_
->IncrementCloseCallCount();
46 lock().AssertAcquired();
49 virtual MojoResult
WriteMessageImplNoLock(
52 std::vector
<DispatcherTransport
>* transports
,
53 MojoWriteMessageFlags
/*flags*/) OVERRIDE
{
54 info_
->IncrementWriteMessageCallCount();
55 lock().AssertAcquired();
57 if (!VerifyUserPointer
<void>(bytes
, num_bytes
))
58 return MOJO_RESULT_INVALID_ARGUMENT
;
59 if (num_bytes
> kMaxMessageNumBytes
)
60 return MOJO_RESULT_RESOURCE_EXHAUSTED
;
63 return MOJO_RESULT_UNIMPLEMENTED
;
65 return MOJO_RESULT_OK
;
68 virtual MojoResult
ReadMessageImplNoLock(
71 std::vector
<scoped_refptr
<Dispatcher
> >* /*dispatchers*/,
72 uint32_t* /*num_dispatchers*/,
73 MojoReadMessageFlags
/*flags*/) OVERRIDE
{
74 info_
->IncrementReadMessageCallCount();
75 lock().AssertAcquired();
77 if (num_bytes
&& !VerifyUserPointer
<void>(bytes
, *num_bytes
))
78 return MOJO_RESULT_INVALID_ARGUMENT
;
80 return MOJO_RESULT_OK
;
83 virtual MojoResult
WriteDataImplNoLock(
84 const void* /*elements*/,
85 uint32_t* /*num_bytes*/,
86 MojoWriteDataFlags
/*flags*/) OVERRIDE
{
87 info_
->IncrementWriteDataCallCount();
88 lock().AssertAcquired();
89 return MOJO_RESULT_UNIMPLEMENTED
;
92 virtual MojoResult
BeginWriteDataImplNoLock(
94 uint32_t* /*buffer_num_bytes*/,
95 MojoWriteDataFlags
/*flags*/) OVERRIDE
{
96 info_
->IncrementBeginWriteDataCallCount();
97 lock().AssertAcquired();
98 return MOJO_RESULT_UNIMPLEMENTED
;
101 virtual MojoResult
EndWriteDataImplNoLock(
102 uint32_t /*num_bytes_written*/) OVERRIDE
{
103 info_
->IncrementEndWriteDataCallCount();
104 lock().AssertAcquired();
105 return MOJO_RESULT_UNIMPLEMENTED
;
108 virtual MojoResult
ReadDataImplNoLock(void* /*elements*/,
109 uint32_t* /*num_bytes*/,
110 MojoReadDataFlags
/*flags*/) OVERRIDE
{
111 info_
->IncrementReadDataCallCount();
112 lock().AssertAcquired();
113 return MOJO_RESULT_UNIMPLEMENTED
;
116 virtual MojoResult
BeginReadDataImplNoLock(
117 const void** /*buffer*/,
118 uint32_t* /*buffer_num_bytes*/,
119 MojoReadDataFlags
/*flags*/) OVERRIDE
{
120 info_
->IncrementBeginReadDataCallCount();
121 lock().AssertAcquired();
122 return MOJO_RESULT_UNIMPLEMENTED
;
125 virtual MojoResult
EndReadDataImplNoLock(
126 uint32_t /*num_bytes_read*/) OVERRIDE
{
127 info_
->IncrementEndReadDataCallCount();
128 lock().AssertAcquired();
129 return MOJO_RESULT_UNIMPLEMENTED
;
132 virtual MojoResult
AddWaiterImplNoLock(Waiter
* /*waiter*/,
133 MojoWaitFlags
/*flags*/,
134 MojoResult
/*wake_result*/) OVERRIDE
{
135 info_
->IncrementAddWaiterCallCount();
136 lock().AssertAcquired();
137 return MOJO_RESULT_FAILED_PRECONDITION
;
140 virtual void RemoveWaiterImplNoLock(Waiter
* /*waiter*/) OVERRIDE
{
141 info_
->IncrementRemoveWaiterCallCount();
142 lock().AssertAcquired();
145 virtual void CancelAllWaitersNoLock() OVERRIDE
{
146 info_
->IncrementCancelAllWaitersCallCount();
147 lock().AssertAcquired();
150 virtual scoped_refptr
<Dispatcher
>
151 CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE
{
152 return scoped_refptr
<Dispatcher
>(new MockDispatcher(info_
));
155 CoreTestBase::MockHandleInfo
* const info_
;
157 DISALLOW_COPY_AND_ASSIGN(MockDispatcher
);
162 // CoreTestBase ----------------------------------------------------------------
164 CoreTestBase::CoreTestBase() {
167 CoreTestBase::~CoreTestBase() {
170 void CoreTestBase::SetUp() {
171 core_
= new CoreImpl();
174 void CoreTestBase::TearDown() {
179 MojoHandle
CoreTestBase::CreateMockHandle(CoreTestBase::MockHandleInfo
* info
) {
181 scoped_refptr
<MockDispatcher
> dispatcher(new MockDispatcher(info
));
182 return core_
->AddDispatcher(dispatcher
);
185 // CoreTestBase_MockHandleInfo -------------------------------------------------
187 CoreTestBase_MockHandleInfo::CoreTestBase_MockHandleInfo()
188 : ctor_call_count_(0),
190 close_call_count_(0),
191 write_message_call_count_(0),
192 read_message_call_count_(0),
193 write_data_call_count_(0),
194 begin_write_data_call_count_(0),
195 end_write_data_call_count_(0),
196 read_data_call_count_(0),
197 begin_read_data_call_count_(0),
198 end_read_data_call_count_(0),
199 add_waiter_call_count_(0),
200 remove_waiter_call_count_(0),
201 cancel_all_waiters_call_count_(0) {
204 CoreTestBase_MockHandleInfo::~CoreTestBase_MockHandleInfo() {
207 unsigned CoreTestBase_MockHandleInfo::GetCtorCallCount() const {
208 base::AutoLock
locker(lock_
);
209 return ctor_call_count_
;
212 unsigned CoreTestBase_MockHandleInfo::GetDtorCallCount() const {
213 base::AutoLock
locker(lock_
);
214 return dtor_call_count_
;
217 unsigned CoreTestBase_MockHandleInfo::GetCloseCallCount() const {
218 base::AutoLock
locker(lock_
);
219 return close_call_count_
;
222 unsigned CoreTestBase_MockHandleInfo::GetWriteMessageCallCount() const {
223 base::AutoLock
locker(lock_
);
224 return write_message_call_count_
;
227 unsigned CoreTestBase_MockHandleInfo::GetReadMessageCallCount() const {
228 base::AutoLock
locker(lock_
);
229 return read_message_call_count_
;
232 unsigned CoreTestBase_MockHandleInfo::GetWriteDataCallCount() const {
233 base::AutoLock
locker(lock_
);
234 return write_data_call_count_
;
237 unsigned CoreTestBase_MockHandleInfo::GetBeginWriteDataCallCount() const {
238 base::AutoLock
locker(lock_
);
239 return begin_write_data_call_count_
;
242 unsigned CoreTestBase_MockHandleInfo::GetEndWriteDataCallCount() const {
243 base::AutoLock
locker(lock_
);
244 return end_write_data_call_count_
;
247 unsigned CoreTestBase_MockHandleInfo::GetReadDataCallCount() const {
248 base::AutoLock
locker(lock_
);
249 return read_data_call_count_
;
252 unsigned CoreTestBase_MockHandleInfo::GetBeginReadDataCallCount() const {
253 base::AutoLock
locker(lock_
);
254 return begin_read_data_call_count_
;
257 unsigned CoreTestBase_MockHandleInfo::GetEndReadDataCallCount() const {
258 base::AutoLock
locker(lock_
);
259 return end_read_data_call_count_
;
262 unsigned CoreTestBase_MockHandleInfo::GetAddWaiterCallCount() const {
263 base::AutoLock
locker(lock_
);
264 return add_waiter_call_count_
;
267 unsigned CoreTestBase_MockHandleInfo::GetRemoveWaiterCallCount() const {
268 base::AutoLock
locker(lock_
);
269 return remove_waiter_call_count_
;
272 unsigned CoreTestBase_MockHandleInfo::GetCancelAllWaitersCallCount() const {
273 base::AutoLock
locker(lock_
);
274 return cancel_all_waiters_call_count_
;
277 void CoreTestBase_MockHandleInfo::IncrementCtorCallCount() {
278 base::AutoLock
locker(lock_
);
282 void CoreTestBase_MockHandleInfo::IncrementDtorCallCount() {
283 base::AutoLock
locker(lock_
);
287 void CoreTestBase_MockHandleInfo::IncrementCloseCallCount() {
288 base::AutoLock
locker(lock_
);
292 void CoreTestBase_MockHandleInfo::IncrementWriteMessageCallCount() {
293 base::AutoLock
locker(lock_
);
294 write_message_call_count_
++;
297 void CoreTestBase_MockHandleInfo::IncrementReadMessageCallCount() {
298 base::AutoLock
locker(lock_
);
299 read_message_call_count_
++;
302 void CoreTestBase_MockHandleInfo::IncrementWriteDataCallCount() {
303 base::AutoLock
locker(lock_
);
304 write_data_call_count_
++;
307 void CoreTestBase_MockHandleInfo::IncrementBeginWriteDataCallCount() {
308 base::AutoLock
locker(lock_
);
309 begin_write_data_call_count_
++;
312 void CoreTestBase_MockHandleInfo::IncrementEndWriteDataCallCount() {
313 base::AutoLock
locker(lock_
);
314 end_write_data_call_count_
++;
317 void CoreTestBase_MockHandleInfo::IncrementReadDataCallCount() {
318 base::AutoLock
locker(lock_
);
319 read_data_call_count_
++;
322 void CoreTestBase_MockHandleInfo::IncrementBeginReadDataCallCount() {
323 base::AutoLock
locker(lock_
);
324 begin_read_data_call_count_
++;
327 void CoreTestBase_MockHandleInfo::IncrementEndReadDataCallCount() {
328 base::AutoLock
locker(lock_
);
329 end_read_data_call_count_
++;
332 void CoreTestBase_MockHandleInfo::IncrementAddWaiterCallCount() {
333 base::AutoLock
locker(lock_
);
334 add_waiter_call_count_
++;
337 void CoreTestBase_MockHandleInfo::IncrementRemoveWaiterCallCount() {
338 base::AutoLock
locker(lock_
);
339 remove_waiter_call_count_
++;
342 void CoreTestBase_MockHandleInfo::IncrementCancelAllWaitersCallCount() {
343 base::AutoLock
locker(lock_
);
344 cancel_all_waiters_call_count_
++;
348 } // namespace system