OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/test/mojo_test_base.h" | 5 #include "mojo/edk/test/mojo_test_base.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "mojo/edk/embedder/embedder.h" | 9 #include "mojo/edk/embedder/embedder.h" |
10 #include "mojo/edk/system/handle_signals_state.h" | 10 #include "mojo/edk/system/handle_signals_state.h" |
11 #include "mojo/public/c/system/buffer.h" | 11 #include "mojo/public/c/system/buffer.h" |
| 12 #include "mojo/public/c/system/data_pipe.h" |
12 #include "mojo/public/c/system/functions.h" | 13 #include "mojo/public/c/system/functions.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace mojo { | 16 namespace mojo { |
16 namespace edk { | 17 namespace edk { |
17 namespace test { | 18 namespace test { |
18 | 19 |
19 MojoTestBase::MojoTestBase() { | 20 MojoTestBase::MojoTestBase() { |
20 } | 21 } |
21 | 22 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 size_t offset, | 215 size_t offset, |
215 const base::StringPiece& s) { | 216 const base::StringPiece& s) { |
216 char* data; | 217 char* data; |
217 EXPECT_EQ(MOJO_RESULT_OK, | 218 EXPECT_EQ(MOJO_RESULT_OK, |
218 MojoMapBuffer(h, offset, s.size(), reinterpret_cast<void**>(&data), | 219 MojoMapBuffer(h, offset, s.size(), reinterpret_cast<void**>(&data), |
219 MOJO_MAP_BUFFER_FLAG_NONE)); | 220 MOJO_MAP_BUFFER_FLAG_NONE)); |
220 EXPECT_EQ(s, base::StringPiece(data, s.size())); | 221 EXPECT_EQ(s, base::StringPiece(data, s.size())); |
221 EXPECT_EQ(MOJO_RESULT_OK, MojoUnmapBuffer(static_cast<void*>(data))); | 222 EXPECT_EQ(MOJO_RESULT_OK, MojoUnmapBuffer(static_cast<void*>(data))); |
222 } | 223 } |
223 | 224 |
| 225 // static |
| 226 void MojoTestBase::CreateDataPipe(MojoHandle *p0, |
| 227 MojoHandle* p1, |
| 228 size_t capacity) { |
| 229 MojoCreateDataPipeOptions options; |
| 230 options.struct_size = static_cast<uint32_t>(sizeof(options)); |
| 231 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 232 options.element_num_bytes = 1; |
| 233 options.capacity_num_bytes = static_cast<uint32_t>(capacity); |
| 234 |
| 235 MojoCreateDataPipe(&options, p0, p1); |
| 236 CHECK_NE(*p0, MOJO_HANDLE_INVALID); |
| 237 CHECK_NE(*p1, MOJO_HANDLE_INVALID); |
| 238 } |
| 239 |
| 240 // static |
| 241 void MojoTestBase::WriteData(MojoHandle producer, const std::string& data) { |
| 242 CHECK_EQ(MojoWait(producer, MOJO_HANDLE_SIGNAL_WRITABLE, |
| 243 MOJO_DEADLINE_INDEFINITE, nullptr), |
| 244 MOJO_RESULT_OK); |
| 245 uint32_t num_bytes = static_cast<uint32_t>(data.size()); |
| 246 CHECK_EQ(MojoWriteData(producer, data.data(), &num_bytes, |
| 247 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), |
| 248 MOJO_RESULT_OK); |
| 249 CHECK_EQ(num_bytes, static_cast<uint32_t>(data.size())); |
| 250 } |
| 251 |
| 252 // static |
| 253 std::string MojoTestBase::ReadData(MojoHandle consumer, size_t size) { |
| 254 CHECK_EQ(MojoWait(consumer, MOJO_HANDLE_SIGNAL_READABLE, |
| 255 MOJO_DEADLINE_INDEFINITE, nullptr), |
| 256 MOJO_RESULT_OK); |
| 257 std::vector<char> buffer(size); |
| 258 uint32_t num_bytes = static_cast<uint32_t>(size); |
| 259 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes, |
| 260 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), |
| 261 MOJO_RESULT_OK); |
| 262 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); |
| 263 |
| 264 return std::string(buffer.data(), buffer.size()); |
| 265 } |
| 266 |
224 } // namespace test | 267 } // namespace test |
225 } // namespace edk | 268 } // namespace edk |
226 } // namespace mojo | 269 } // namespace mojo |
OLD | NEW |