• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core/core_test_base.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <vector>
11 
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "mojo/core/configuration.h"
16 #include "mojo/core/core.h"
17 #include "mojo/core/dispatcher.h"
18 #include "mojo/core/user_message_impl.h"
19 
20 namespace mojo {
21 namespace core {
22 namespace test {
23 
24 namespace {
25 
26 // MockDispatcher --------------------------------------------------------------
27 
28 class MockDispatcher : public Dispatcher {
29  public:
Create(CoreTestBase::MockHandleInfo * info)30   static scoped_refptr<MockDispatcher> Create(
31       CoreTestBase::MockHandleInfo* info) {
32     return base::WrapRefCounted(new MockDispatcher(info));
33   }
34 
35   // Dispatcher:
GetType() const36   Type GetType() const override { return Type::UNKNOWN; }
37 
Close()38   MojoResult Close() override {
39     info_->IncrementCloseCallCount();
40     return MOJO_RESULT_OK;
41   }
42 
WriteMessage(std::unique_ptr<ports::UserMessageEvent> message_event)43   MojoResult WriteMessage(
44       std::unique_ptr<ports::UserMessageEvent> message_event) override {
45     info_->IncrementWriteMessageCallCount();
46     return MOJO_RESULT_OK;
47   }
48 
ReadMessage(std::unique_ptr<ports::UserMessageEvent> * message_event)49   MojoResult ReadMessage(
50       std::unique_ptr<ports::UserMessageEvent>* message_event) override {
51     info_->IncrementReadMessageCallCount();
52     return MOJO_RESULT_OK;
53   }
54 
WriteData(const void * elements,uint32_t * num_bytes,const MojoWriteDataOptions & options)55   MojoResult WriteData(const void* elements,
56                        uint32_t* num_bytes,
57                        const MojoWriteDataOptions& options) override {
58     info_->IncrementWriteDataCallCount();
59     return MOJO_RESULT_UNIMPLEMENTED;
60   }
61 
BeginWriteData(void ** buffer,uint32_t * buffer_num_bytes)62   MojoResult BeginWriteData(void** buffer,
63                             uint32_t* buffer_num_bytes) override {
64     info_->IncrementBeginWriteDataCallCount();
65     return MOJO_RESULT_UNIMPLEMENTED;
66   }
67 
EndWriteData(uint32_t num_bytes_written)68   MojoResult EndWriteData(uint32_t num_bytes_written) override {
69     info_->IncrementEndWriteDataCallCount();
70     return MOJO_RESULT_UNIMPLEMENTED;
71   }
72 
ReadData(const MojoReadDataOptions & options,void * elements,uint32_t * num_bytes)73   MojoResult ReadData(const MojoReadDataOptions& options,
74                       void* elements,
75                       uint32_t* num_bytes) override {
76     info_->IncrementReadDataCallCount();
77     return MOJO_RESULT_UNIMPLEMENTED;
78   }
79 
BeginReadData(const void ** buffer,uint32_t * buffer_num_bytes)80   MojoResult BeginReadData(const void** buffer,
81                            uint32_t* buffer_num_bytes) override {
82     info_->IncrementBeginReadDataCallCount();
83     return MOJO_RESULT_UNIMPLEMENTED;
84   }
85 
EndReadData(uint32_t num_bytes_read)86   MojoResult EndReadData(uint32_t num_bytes_read) override {
87     info_->IncrementEndReadDataCallCount();
88     return MOJO_RESULT_UNIMPLEMENTED;
89   }
90 
91  private:
MockDispatcher(CoreTestBase::MockHandleInfo * info)92   explicit MockDispatcher(CoreTestBase::MockHandleInfo* info) : info_(info) {
93     CHECK(info_);
94     info_->IncrementCtorCallCount();
95   }
96 
~MockDispatcher()97   ~MockDispatcher() override { info_->IncrementDtorCallCount(); }
98 
99   CoreTestBase::MockHandleInfo* const info_;
100 
101   DISALLOW_COPY_AND_ASSIGN(MockDispatcher);
102 };
103 
104 }  // namespace
105 
106 // CoreTestBase ----------------------------------------------------------------
107 
CoreTestBase()108 CoreTestBase::CoreTestBase() {}
109 
~CoreTestBase()110 CoreTestBase::~CoreTestBase() {}
111 
CreateMockHandle(CoreTestBase::MockHandleInfo * info)112 MojoHandle CoreTestBase::CreateMockHandle(CoreTestBase::MockHandleInfo* info) {
113   scoped_refptr<MockDispatcher> dispatcher = MockDispatcher::Create(info);
114   return core()->AddDispatcher(dispatcher);
115 }
116 
core()117 Core* CoreTestBase::core() {
118   return Core::Get();
119 }
120 
121 // CoreTestBase_MockHandleInfo -------------------------------------------------
122 
CoreTestBase_MockHandleInfo()123 CoreTestBase_MockHandleInfo::CoreTestBase_MockHandleInfo()
124     : ctor_call_count_(0),
125       dtor_call_count_(0),
126       close_call_count_(0),
127       write_message_call_count_(0),
128       read_message_call_count_(0),
129       write_data_call_count_(0),
130       begin_write_data_call_count_(0),
131       end_write_data_call_count_(0),
132       read_data_call_count_(0),
133       begin_read_data_call_count_(0),
134       end_read_data_call_count_(0) {}
135 
~CoreTestBase_MockHandleInfo()136 CoreTestBase_MockHandleInfo::~CoreTestBase_MockHandleInfo() {}
137 
GetCtorCallCount() const138 unsigned CoreTestBase_MockHandleInfo::GetCtorCallCount() const {
139   base::AutoLock locker(lock_);
140   return ctor_call_count_;
141 }
142 
GetDtorCallCount() const143 unsigned CoreTestBase_MockHandleInfo::GetDtorCallCount() const {
144   base::AutoLock locker(lock_);
145   return dtor_call_count_;
146 }
147 
GetCloseCallCount() const148 unsigned CoreTestBase_MockHandleInfo::GetCloseCallCount() const {
149   base::AutoLock locker(lock_);
150   return close_call_count_;
151 }
152 
GetWriteMessageCallCount() const153 unsigned CoreTestBase_MockHandleInfo::GetWriteMessageCallCount() const {
154   base::AutoLock locker(lock_);
155   return write_message_call_count_;
156 }
157 
GetReadMessageCallCount() const158 unsigned CoreTestBase_MockHandleInfo::GetReadMessageCallCount() const {
159   base::AutoLock locker(lock_);
160   return read_message_call_count_;
161 }
162 
GetWriteDataCallCount() const163 unsigned CoreTestBase_MockHandleInfo::GetWriteDataCallCount() const {
164   base::AutoLock locker(lock_);
165   return write_data_call_count_;
166 }
167 
GetBeginWriteDataCallCount() const168 unsigned CoreTestBase_MockHandleInfo::GetBeginWriteDataCallCount() const {
169   base::AutoLock locker(lock_);
170   return begin_write_data_call_count_;
171 }
172 
GetEndWriteDataCallCount() const173 unsigned CoreTestBase_MockHandleInfo::GetEndWriteDataCallCount() const {
174   base::AutoLock locker(lock_);
175   return end_write_data_call_count_;
176 }
177 
GetReadDataCallCount() const178 unsigned CoreTestBase_MockHandleInfo::GetReadDataCallCount() const {
179   base::AutoLock locker(lock_);
180   return read_data_call_count_;
181 }
182 
GetBeginReadDataCallCount() const183 unsigned CoreTestBase_MockHandleInfo::GetBeginReadDataCallCount() const {
184   base::AutoLock locker(lock_);
185   return begin_read_data_call_count_;
186 }
187 
GetEndReadDataCallCount() const188 unsigned CoreTestBase_MockHandleInfo::GetEndReadDataCallCount() const {
189   base::AutoLock locker(lock_);
190   return end_read_data_call_count_;
191 }
192 
IncrementCtorCallCount()193 void CoreTestBase_MockHandleInfo::IncrementCtorCallCount() {
194   base::AutoLock locker(lock_);
195   ctor_call_count_++;
196 }
197 
IncrementDtorCallCount()198 void CoreTestBase_MockHandleInfo::IncrementDtorCallCount() {
199   base::AutoLock locker(lock_);
200   dtor_call_count_++;
201 }
202 
IncrementCloseCallCount()203 void CoreTestBase_MockHandleInfo::IncrementCloseCallCount() {
204   base::AutoLock locker(lock_);
205   close_call_count_++;
206 }
207 
IncrementWriteMessageCallCount()208 void CoreTestBase_MockHandleInfo::IncrementWriteMessageCallCount() {
209   base::AutoLock locker(lock_);
210   write_message_call_count_++;
211 }
212 
IncrementReadMessageCallCount()213 void CoreTestBase_MockHandleInfo::IncrementReadMessageCallCount() {
214   base::AutoLock locker(lock_);
215   read_message_call_count_++;
216 }
217 
IncrementWriteDataCallCount()218 void CoreTestBase_MockHandleInfo::IncrementWriteDataCallCount() {
219   base::AutoLock locker(lock_);
220   write_data_call_count_++;
221 }
222 
IncrementBeginWriteDataCallCount()223 void CoreTestBase_MockHandleInfo::IncrementBeginWriteDataCallCount() {
224   base::AutoLock locker(lock_);
225   begin_write_data_call_count_++;
226 }
227 
IncrementEndWriteDataCallCount()228 void CoreTestBase_MockHandleInfo::IncrementEndWriteDataCallCount() {
229   base::AutoLock locker(lock_);
230   end_write_data_call_count_++;
231 }
232 
IncrementReadDataCallCount()233 void CoreTestBase_MockHandleInfo::IncrementReadDataCallCount() {
234   base::AutoLock locker(lock_);
235   read_data_call_count_++;
236 }
237 
IncrementBeginReadDataCallCount()238 void CoreTestBase_MockHandleInfo::IncrementBeginReadDataCallCount() {
239   base::AutoLock locker(lock_);
240   begin_read_data_call_count_++;
241 }
242 
IncrementEndReadDataCallCount()243 void CoreTestBase_MockHandleInfo::IncrementEndReadDataCallCount() {
244   base::AutoLock locker(lock_);
245   end_read_data_call_count_++;
246 }
247 
248 }  // namespace test
249 }  // namespace core
250 }  // namespace mojo
251