OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/edk/system/core_test_base.h" | |
6 | |
7 #include <vector> | |
8 | |
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/constants.h" | |
14 #include "mojo/edk/system/core.h" | |
15 #include "mojo/edk/system/dispatcher.h" | |
16 #include "mojo/edk/system/memory.h" | |
17 | |
18 namespace mojo { | |
19 namespace system { | |
20 namespace test { | |
21 | |
22 namespace { | |
23 | |
24 // MockDispatcher -------------------------------------------------------------- | |
25 | |
26 class MockDispatcher : public Dispatcher { | |
27 public: | |
28 explicit MockDispatcher(CoreTestBase::MockHandleInfo* info) : info_(info) { | |
29 CHECK(info_); | |
30 info_->IncrementCtorCallCount(); | |
31 } | |
32 | |
33 // |Dispatcher| private methods: | |
34 virtual Type GetType() const override { return kTypeUnknown; } | |
35 | |
36 private: | |
37 virtual ~MockDispatcher() { info_->IncrementDtorCallCount(); } | |
38 | |
39 // |Dispatcher| protected methods: | |
40 virtual void CloseImplNoLock() override { | |
41 info_->IncrementCloseCallCount(); | |
42 lock().AssertAcquired(); | |
43 } | |
44 | |
45 virtual MojoResult WriteMessageImplNoLock( | |
46 UserPointer<const void> bytes, | |
47 uint32_t num_bytes, | |
48 std::vector<DispatcherTransport>* transports, | |
49 MojoWriteMessageFlags /*flags*/) override { | |
50 info_->IncrementWriteMessageCallCount(); | |
51 lock().AssertAcquired(); | |
52 | |
53 if (num_bytes > kMaxMessageNumBytes) | |
54 return MOJO_RESULT_RESOURCE_EXHAUSTED; | |
55 | |
56 if (transports) | |
57 return MOJO_RESULT_UNIMPLEMENTED; | |
58 | |
59 return MOJO_RESULT_OK; | |
60 } | |
61 | |
62 virtual MojoResult ReadMessageImplNoLock( | |
63 UserPointer<void> bytes, | |
64 UserPointer<uint32_t> num_bytes, | |
65 DispatcherVector* dispatchers, | |
66 uint32_t* num_dispatchers, | |
67 MojoReadMessageFlags /*flags*/) override { | |
68 info_->IncrementReadMessageCallCount(); | |
69 lock().AssertAcquired(); | |
70 | |
71 if (num_dispatchers) { | |
72 *num_dispatchers = 1; | |
73 if (dispatchers) { | |
74 // Okay to leave an invalid dispatcher. | |
75 dispatchers->resize(1); | |
76 } | |
77 } | |
78 | |
79 return MOJO_RESULT_OK; | |
80 } | |
81 | |
82 virtual MojoResult WriteDataImplNoLock( | |
83 UserPointer<const void> /*elements*/, | |
84 UserPointer<uint32_t> /*num_bytes*/, | |
85 MojoWriteDataFlags /*flags*/) override { | |
86 info_->IncrementWriteDataCallCount(); | |
87 lock().AssertAcquired(); | |
88 return MOJO_RESULT_UNIMPLEMENTED; | |
89 } | |
90 | |
91 virtual MojoResult BeginWriteDataImplNoLock( | |
92 UserPointer<void*> /*buffer*/, | |
93 UserPointer<uint32_t> /*buffer_num_bytes*/, | |
94 MojoWriteDataFlags /*flags*/) override { | |
95 info_->IncrementBeginWriteDataCallCount(); | |
96 lock().AssertAcquired(); | |
97 return MOJO_RESULT_UNIMPLEMENTED; | |
98 } | |
99 | |
100 virtual MojoResult EndWriteDataImplNoLock( | |
101 uint32_t /*num_bytes_written*/) override { | |
102 info_->IncrementEndWriteDataCallCount(); | |
103 lock().AssertAcquired(); | |
104 return MOJO_RESULT_UNIMPLEMENTED; | |
105 } | |
106 | |
107 virtual MojoResult ReadDataImplNoLock(UserPointer<void> /*elements*/, | |
108 UserPointer<uint32_t> /*num_bytes*/, | |
109 MojoReadDataFlags /*flags*/) override { | |
110 info_->IncrementReadDataCallCount(); | |
111 lock().AssertAcquired(); | |
112 return MOJO_RESULT_UNIMPLEMENTED; | |
113 } | |
114 | |
115 virtual MojoResult BeginReadDataImplNoLock( | |
116 UserPointer<const void*> /*buffer*/, | |
117 UserPointer<uint32_t> /*buffer_num_bytes*/, | |
118 MojoReadDataFlags /*flags*/) override { | |
119 info_->IncrementBeginReadDataCallCount(); | |
120 lock().AssertAcquired(); | |
121 return MOJO_RESULT_UNIMPLEMENTED; | |
122 } | |
123 | |
124 virtual MojoResult EndReadDataImplNoLock( | |
125 uint32_t /*num_bytes_read*/) override { | |
126 info_->IncrementEndReadDataCallCount(); | |
127 lock().AssertAcquired(); | |
128 return MOJO_RESULT_UNIMPLEMENTED; | |
129 } | |
130 | |
131 virtual MojoResult AddWaiterImplNoLock( | |
132 Waiter* /*waiter*/, | |
133 MojoHandleSignals /*signals*/, | |
134 uint32_t /*context*/, | |
135 HandleSignalsState* signals_state) override { | |
136 info_->IncrementAddWaiterCallCount(); | |
137 lock().AssertAcquired(); | |
138 if (signals_state) | |
139 *signals_state = HandleSignalsState(); | |
140 return MOJO_RESULT_FAILED_PRECONDITION; | |
141 } | |
142 | |
143 virtual void RemoveWaiterImplNoLock( | |
144 Waiter* /*waiter*/, | |
145 HandleSignalsState* signals_state) override { | |
146 info_->IncrementRemoveWaiterCallCount(); | |
147 lock().AssertAcquired(); | |
148 if (signals_state) | |
149 *signals_state = HandleSignalsState(); | |
150 } | |
151 | |
152 virtual void CancelAllWaitersNoLock() override { | |
153 info_->IncrementCancelAllWaitersCallCount(); | |
154 lock().AssertAcquired(); | |
155 } | |
156 | |
157 virtual scoped_refptr<Dispatcher> | |
158 CreateEquivalentDispatcherAndCloseImplNoLock() override { | |
159 return scoped_refptr<Dispatcher>(new MockDispatcher(info_)); | |
160 } | |
161 | |
162 CoreTestBase::MockHandleInfo* const info_; | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); | |
165 }; | |
166 | |
167 } // namespace | |
168 | |
169 // CoreTestBase ---------------------------------------------------------------- | |
170 | |
171 CoreTestBase::CoreTestBase() { | |
172 } | |
173 | |
174 CoreTestBase::~CoreTestBase() { | |
175 } | |
176 | |
177 void CoreTestBase::SetUp() { | |
178 core_ = new Core(make_scoped_ptr(new embedder::SimplePlatformSupport())); | |
179 } | |
180 | |
181 void CoreTestBase::TearDown() { | |
182 delete core_; | |
183 core_ = nullptr; | |
184 } | |
185 | |
186 MojoHandle CoreTestBase::CreateMockHandle(CoreTestBase::MockHandleInfo* info) { | |
187 CHECK(core_); | |
188 scoped_refptr<MockDispatcher> dispatcher(new MockDispatcher(info)); | |
189 return core_->AddDispatcher(dispatcher); | |
190 } | |
191 | |
192 // CoreTestBase_MockHandleInfo ------------------------------------------------- | |
193 | |
194 CoreTestBase_MockHandleInfo::CoreTestBase_MockHandleInfo() | |
195 : ctor_call_count_(0), | |
196 dtor_call_count_(0), | |
197 close_call_count_(0), | |
198 write_message_call_count_(0), | |
199 read_message_call_count_(0), | |
200 write_data_call_count_(0), | |
201 begin_write_data_call_count_(0), | |
202 end_write_data_call_count_(0), | |
203 read_data_call_count_(0), | |
204 begin_read_data_call_count_(0), | |
205 end_read_data_call_count_(0), | |
206 add_waiter_call_count_(0), | |
207 remove_waiter_call_count_(0), | |
208 cancel_all_waiters_call_count_(0) { | |
209 } | |
210 | |
211 CoreTestBase_MockHandleInfo::~CoreTestBase_MockHandleInfo() { | |
212 } | |
213 | |
214 unsigned CoreTestBase_MockHandleInfo::GetCtorCallCount() const { | |
215 base::AutoLock locker(lock_); | |
216 return ctor_call_count_; | |
217 } | |
218 | |
219 unsigned CoreTestBase_MockHandleInfo::GetDtorCallCount() const { | |
220 base::AutoLock locker(lock_); | |
221 return dtor_call_count_; | |
222 } | |
223 | |
224 unsigned CoreTestBase_MockHandleInfo::GetCloseCallCount() const { | |
225 base::AutoLock locker(lock_); | |
226 return close_call_count_; | |
227 } | |
228 | |
229 unsigned CoreTestBase_MockHandleInfo::GetWriteMessageCallCount() const { | |
230 base::AutoLock locker(lock_); | |
231 return write_message_call_count_; | |
232 } | |
233 | |
234 unsigned CoreTestBase_MockHandleInfo::GetReadMessageCallCount() const { | |
235 base::AutoLock locker(lock_); | |
236 return read_message_call_count_; | |
237 } | |
238 | |
239 unsigned CoreTestBase_MockHandleInfo::GetWriteDataCallCount() const { | |
240 base::AutoLock locker(lock_); | |
241 return write_data_call_count_; | |
242 } | |
243 | |
244 unsigned CoreTestBase_MockHandleInfo::GetBeginWriteDataCallCount() const { | |
245 base::AutoLock locker(lock_); | |
246 return begin_write_data_call_count_; | |
247 } | |
248 | |
249 unsigned CoreTestBase_MockHandleInfo::GetEndWriteDataCallCount() const { | |
250 base::AutoLock locker(lock_); | |
251 return end_write_data_call_count_; | |
252 } | |
253 | |
254 unsigned CoreTestBase_MockHandleInfo::GetReadDataCallCount() const { | |
255 base::AutoLock locker(lock_); | |
256 return read_data_call_count_; | |
257 } | |
258 | |
259 unsigned CoreTestBase_MockHandleInfo::GetBeginReadDataCallCount() const { | |
260 base::AutoLock locker(lock_); | |
261 return begin_read_data_call_count_; | |
262 } | |
263 | |
264 unsigned CoreTestBase_MockHandleInfo::GetEndReadDataCallCount() const { | |
265 base::AutoLock locker(lock_); | |
266 return end_read_data_call_count_; | |
267 } | |
268 | |
269 unsigned CoreTestBase_MockHandleInfo::GetAddWaiterCallCount() const { | |
270 base::AutoLock locker(lock_); | |
271 return add_waiter_call_count_; | |
272 } | |
273 | |
274 unsigned CoreTestBase_MockHandleInfo::GetRemoveWaiterCallCount() const { | |
275 base::AutoLock locker(lock_); | |
276 return remove_waiter_call_count_; | |
277 } | |
278 | |
279 unsigned CoreTestBase_MockHandleInfo::GetCancelAllWaitersCallCount() const { | |
280 base::AutoLock locker(lock_); | |
281 return cancel_all_waiters_call_count_; | |
282 } | |
283 | |
284 void CoreTestBase_MockHandleInfo::IncrementCtorCallCount() { | |
285 base::AutoLock locker(lock_); | |
286 ctor_call_count_++; | |
287 } | |
288 | |
289 void CoreTestBase_MockHandleInfo::IncrementDtorCallCount() { | |
290 base::AutoLock locker(lock_); | |
291 dtor_call_count_++; | |
292 } | |
293 | |
294 void CoreTestBase_MockHandleInfo::IncrementCloseCallCount() { | |
295 base::AutoLock locker(lock_); | |
296 close_call_count_++; | |
297 } | |
298 | |
299 void CoreTestBase_MockHandleInfo::IncrementWriteMessageCallCount() { | |
300 base::AutoLock locker(lock_); | |
301 write_message_call_count_++; | |
302 } | |
303 | |
304 void CoreTestBase_MockHandleInfo::IncrementReadMessageCallCount() { | |
305 base::AutoLock locker(lock_); | |
306 read_message_call_count_++; | |
307 } | |
308 | |
309 void CoreTestBase_MockHandleInfo::IncrementWriteDataCallCount() { | |
310 base::AutoLock locker(lock_); | |
311 write_data_call_count_++; | |
312 } | |
313 | |
314 void CoreTestBase_MockHandleInfo::IncrementBeginWriteDataCallCount() { | |
315 base::AutoLock locker(lock_); | |
316 begin_write_data_call_count_++; | |
317 } | |
318 | |
319 void CoreTestBase_MockHandleInfo::IncrementEndWriteDataCallCount() { | |
320 base::AutoLock locker(lock_); | |
321 end_write_data_call_count_++; | |
322 } | |
323 | |
324 void CoreTestBase_MockHandleInfo::IncrementReadDataCallCount() { | |
325 base::AutoLock locker(lock_); | |
326 read_data_call_count_++; | |
327 } | |
328 | |
329 void CoreTestBase_MockHandleInfo::IncrementBeginReadDataCallCount() { | |
330 base::AutoLock locker(lock_); | |
331 begin_read_data_call_count_++; | |
332 } | |
333 | |
334 void CoreTestBase_MockHandleInfo::IncrementEndReadDataCallCount() { | |
335 base::AutoLock locker(lock_); | |
336 end_read_data_call_count_++; | |
337 } | |
338 | |
339 void CoreTestBase_MockHandleInfo::IncrementAddWaiterCallCount() { | |
340 base::AutoLock locker(lock_); | |
341 add_waiter_call_count_++; | |
342 } | |
343 | |
344 void CoreTestBase_MockHandleInfo::IncrementRemoveWaiterCallCount() { | |
345 base::AutoLock locker(lock_); | |
346 remove_waiter_call_count_++; | |
347 } | |
348 | |
349 void CoreTestBase_MockHandleInfo::IncrementCancelAllWaitersCallCount() { | |
350 base::AutoLock locker(lock_); | |
351 cancel_all_waiters_call_count_++; | |
352 } | |
353 | |
354 } // namespace test | |
355 } // namespace system | |
356 } // namespace mojo | |
OLD | NEW |