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/edk/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/edk/embedder/simple_platform_support.h"
13 #include "mojo/edk/system/configuration.h"
14 #include "mojo/edk/system/core.h"
15 #include "mojo/edk/system/dispatcher.h"
16 #include "mojo/edk/system/memory.h"
24 // MockDispatcher --------------------------------------------------------------
26 class MockDispatcher
: public Dispatcher
{
28 explicit MockDispatcher(CoreTestBase::MockHandleInfo
* info
) : info_(info
) {
30 info_
->IncrementCtorCallCount();
33 // |Dispatcher| private methods:
34 Type
GetType() const override
{ return kTypeUnknown
; }
37 ~MockDispatcher() override
{ info_
->IncrementDtorCallCount(); }
39 // |Dispatcher| protected methods:
40 void CloseImplNoLock() override
{
41 info_
->IncrementCloseCallCount();
42 lock().AssertAcquired();
45 MojoResult
WriteMessageImplNoLock(
46 UserPointer
<const void> bytes
,
48 std::vector
<DispatcherTransport
>* transports
,
49 MojoWriteMessageFlags
/*flags*/) override
{
50 info_
->IncrementWriteMessageCallCount();
51 lock().AssertAcquired();
53 if (num_bytes
> GetConfiguration().max_message_num_bytes
)
54 return MOJO_RESULT_RESOURCE_EXHAUSTED
;
57 return MOJO_RESULT_UNIMPLEMENTED
;
59 return MOJO_RESULT_OK
;
62 MojoResult
ReadMessageImplNoLock(UserPointer
<void> bytes
,
63 UserPointer
<uint32_t> num_bytes
,
64 DispatcherVector
* dispatchers
,
65 uint32_t* num_dispatchers
,
66 MojoReadMessageFlags
/*flags*/) override
{
67 info_
->IncrementReadMessageCallCount();
68 lock().AssertAcquired();
70 if (num_dispatchers
) {
73 // Okay to leave an invalid dispatcher.
74 dispatchers
->resize(1);
78 return MOJO_RESULT_OK
;
81 MojoResult
WriteDataImplNoLock(UserPointer
<const void> /*elements*/,
82 UserPointer
<uint32_t> /*num_bytes*/,
83 MojoWriteDataFlags
/*flags*/) override
{
84 info_
->IncrementWriteDataCallCount();
85 lock().AssertAcquired();
86 return MOJO_RESULT_UNIMPLEMENTED
;
89 MojoResult
BeginWriteDataImplNoLock(
90 UserPointer
<void*> /*buffer*/,
91 UserPointer
<uint32_t> /*buffer_num_bytes*/,
92 MojoWriteDataFlags
/*flags*/) override
{
93 info_
->IncrementBeginWriteDataCallCount();
94 lock().AssertAcquired();
95 return MOJO_RESULT_UNIMPLEMENTED
;
98 MojoResult
EndWriteDataImplNoLock(uint32_t /*num_bytes_written*/) override
{
99 info_
->IncrementEndWriteDataCallCount();
100 lock().AssertAcquired();
101 return MOJO_RESULT_UNIMPLEMENTED
;
104 MojoResult
ReadDataImplNoLock(UserPointer
<void> /*elements*/,
105 UserPointer
<uint32_t> /*num_bytes*/,
106 MojoReadDataFlags
/*flags*/) override
{
107 info_
->IncrementReadDataCallCount();
108 lock().AssertAcquired();
109 return MOJO_RESULT_UNIMPLEMENTED
;
112 MojoResult
BeginReadDataImplNoLock(UserPointer
<const void*> /*buffer*/,
113 UserPointer
<uint32_t> /*buffer_num_bytes*/,
114 MojoReadDataFlags
/*flags*/) override
{
115 info_
->IncrementBeginReadDataCallCount();
116 lock().AssertAcquired();
117 return MOJO_RESULT_UNIMPLEMENTED
;
120 MojoResult
EndReadDataImplNoLock(uint32_t /*num_bytes_read*/) override
{
121 info_
->IncrementEndReadDataCallCount();
122 lock().AssertAcquired();
123 return MOJO_RESULT_UNIMPLEMENTED
;
126 MojoResult
AddAwakableImplNoLock(Awakable
* /*awakable*/,
127 MojoHandleSignals
/*signals*/,
128 uint32_t /*context*/,
129 HandleSignalsState
* signals_state
) override
{
130 info_
->IncrementAddAwakableCallCount();
131 lock().AssertAcquired();
133 *signals_state
= HandleSignalsState();
134 return MOJO_RESULT_FAILED_PRECONDITION
;
137 void RemoveAwakableImplNoLock(Awakable
* /*awakable*/,
138 HandleSignalsState
* signals_state
) override
{
139 info_
->IncrementRemoveAwakableCallCount();
140 lock().AssertAcquired();
142 *signals_state
= HandleSignalsState();
145 void CancelAllAwakablesNoLock() override
{
146 info_
->IncrementCancelAllAwakablesCallCount();
147 lock().AssertAcquired();
150 scoped_refptr
<Dispatcher
> CreateEquivalentDispatcherAndCloseImplNoLock()
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 Core(make_scoped_ptr(new embedder::SimplePlatformSupport()));
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_awakable_call_count_(0),
200 remove_awakable_call_count_(0),
201 cancel_all_awakables_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::GetAddAwakableCallCount() const {
263 base::AutoLock
locker(lock_
);
264 return add_awakable_call_count_
;
267 unsigned CoreTestBase_MockHandleInfo::GetRemoveAwakableCallCount() const {
268 base::AutoLock
locker(lock_
);
269 return remove_awakable_call_count_
;
272 unsigned CoreTestBase_MockHandleInfo::GetCancelAllAwakablesCallCount() const {
273 base::AutoLock
locker(lock_
);
274 return cancel_all_awakables_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::IncrementAddAwakableCallCount() {
333 base::AutoLock
locker(lock_
);
334 add_awakable_call_count_
++;
337 void CoreTestBase_MockHandleInfo::IncrementRemoveAwakableCallCount() {
338 base::AutoLock
locker(lock_
);
339 remove_awakable_call_count_
++;
342 void CoreTestBase_MockHandleInfo::IncrementCancelAllAwakablesCallCount() {
343 base::AutoLock
locker(lock_
);
344 cancel_all_awakables_call_count_
++;
348 } // namespace system