OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "mojo/public/cpp/bindings/binding_set.h" | 10 #include "mojo/public/cpp/bindings/binding_set.h" |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 { | 213 { |
214 base::RunLoop loop; | 214 base::RunLoop loop; |
215 blink_proxy->AddRect(RectBlink(1, 1, 4, 5)); | 215 blink_proxy->AddRect(RectBlink(1, 1, 4, 5)); |
216 blink_proxy->AddRect(RectBlink(10, 10, 2, 2)); | 216 blink_proxy->AddRect(RectBlink(10, 10, 2, 2)); |
217 blink_proxy->GetLargestRect( | 217 blink_proxy->GetLargestRect( |
218 ExpectResult(RectBlink(1, 1, 4, 5), loop.QuitClosure())); | 218 ExpectResult(RectBlink(1, 1, 4, 5), loop.QuitClosure())); |
219 loop.Run(); | 219 loop.Run(); |
220 } | 220 } |
221 } | 221 } |
222 | 222 |
| 223 void ExpectStructWithTraits(const StructWithTraitsImpl& expected, |
| 224 const base::Closure& closure, |
| 225 const StructWithTraitsImpl& passed) { |
| 226 EXPECT_EQ(expected.get_enum(), passed.get_enum()); |
| 227 EXPECT_EQ(expected.get_bool(), passed.get_bool()); |
| 228 EXPECT_EQ(expected.get_uint32(), passed.get_uint32()); |
| 229 EXPECT_EQ(expected.get_uint64(), passed.get_uint64()); |
| 230 EXPECT_EQ(expected.get_string(), passed.get_string()); |
| 231 EXPECT_EQ(expected.get_string_array(), passed.get_string_array()); |
| 232 EXPECT_EQ(expected.get_struct(), passed.get_struct()); |
| 233 EXPECT_EQ(expected.get_struct_array(), passed.get_struct_array()); |
| 234 EXPECT_EQ(expected.get_struct_map(), passed.get_struct_map()); |
| 235 closure.Run(); |
| 236 } |
| 237 |
223 TEST_F(StructTraitsTest, EchoStructWithTraits) { | 238 TEST_F(StructTraitsTest, EchoStructWithTraits) { |
224 StructWithTraitsImpl input; | 239 StructWithTraitsImpl input; |
225 input.set_enum(EnumWithTraitsImpl::CUSTOM_VALUE_1); | 240 input.set_enum(EnumWithTraitsImpl::CUSTOM_VALUE_1); |
226 input.set_bool(true); | 241 input.set_bool(true); |
227 input.set_uint32(7); | 242 input.set_uint32(7); |
228 input.set_uint64(42); | 243 input.set_uint64(42); |
229 input.set_string("hello world!"); | 244 input.set_string("hello world!"); |
230 input.get_mutable_string_array().assign({"hello", "world!"}); | 245 input.get_mutable_string_array().assign({"hello", "world!"}); |
231 input.get_mutable_struct().value = 42; | 246 input.get_mutable_struct().value = 42; |
232 input.get_mutable_struct_array().resize(2); | 247 input.get_mutable_struct_array().resize(2); |
233 input.get_mutable_struct_array()[0].value = 1; | 248 input.get_mutable_struct_array()[0].value = 1; |
234 input.get_mutable_struct_array()[1].value = 2; | 249 input.get_mutable_struct_array()[1].value = 2; |
235 input.get_mutable_struct_map()["hello"] = NestedStructWithTraitsImpl(1024); | 250 input.get_mutable_struct_map()["hello"] = NestedStructWithTraitsImpl(1024); |
236 input.get_mutable_struct_map()["world"] = NestedStructWithTraitsImpl(2048); | 251 input.get_mutable_struct_map()["world"] = NestedStructWithTraitsImpl(2048); |
237 | 252 |
238 base::RunLoop loop; | 253 base::RunLoop loop; |
239 TraitsTestServicePtr proxy = GetTraitsTestProxy(); | 254 TraitsTestServicePtr proxy = GetTraitsTestProxy(); |
240 | 255 |
241 proxy->EchoStructWithTraits(input, [&](const StructWithTraitsImpl& passed) { | 256 proxy->EchoStructWithTraits( |
242 EXPECT_EQ(input.get_enum(), passed.get_enum()); | 257 input, |
243 EXPECT_EQ(input.get_bool(), passed.get_bool()); | 258 base::Bind(&ExpectStructWithTraits, input, loop.QuitClosure())); |
244 EXPECT_EQ(input.get_uint32(), passed.get_uint32()); | |
245 EXPECT_EQ(input.get_uint64(), passed.get_uint64()); | |
246 EXPECT_EQ(input.get_string(), passed.get_string()); | |
247 EXPECT_EQ(input.get_string_array(), passed.get_string_array()); | |
248 EXPECT_EQ(input.get_struct(), passed.get_struct()); | |
249 EXPECT_EQ(input.get_struct_array(), passed.get_struct_array()); | |
250 EXPECT_EQ(input.get_struct_map(), passed.get_struct_map()); | |
251 loop.Quit(); | |
252 }); | |
253 loop.Run(); | 259 loop.Run(); |
254 } | 260 } |
255 | 261 |
256 TEST_F(StructTraitsTest, CloneStructWithTraitsContainer) { | 262 TEST_F(StructTraitsTest, CloneStructWithTraitsContainer) { |
257 StructWithTraitsContainerPtr container = StructWithTraitsContainer::New(); | 263 StructWithTraitsContainerPtr container = StructWithTraitsContainer::New(); |
258 container->f_struct.set_uint32(7); | 264 container->f_struct.set_uint32(7); |
259 container->f_struct.set_uint64(42); | 265 container->f_struct.set_uint64(42); |
260 StructWithTraitsContainerPtr cloned_container = container.Clone(); | 266 StructWithTraitsContainerPtr cloned_container = container.Clone(); |
261 EXPECT_EQ(7u, cloned_container->f_struct.get_uint32()); | 267 EXPECT_EQ(7u, cloned_container->f_struct.get_uint32()); |
262 EXPECT_EQ(42u, cloned_container->f_struct.get_uint64()); | 268 EXPECT_EQ(42u, cloned_container->f_struct.get_uint64()); |
263 } | 269 } |
264 | 270 |
| 271 void CaptureMessagePipe(ScopedMessagePipeHandle* storage, |
| 272 const base::Closure& closure, |
| 273 PassByValueStructWithTraitsImpl passed) { |
| 274 storage->reset(MessagePipeHandle( |
| 275 passed.get_mutable_handle().release().value())); |
| 276 closure.Run(); |
| 277 } |
| 278 |
265 TEST_F(StructTraitsTest, EchoPassByValueStructWithTraits) { | 279 TEST_F(StructTraitsTest, EchoPassByValueStructWithTraits) { |
266 MessagePipe mp; | 280 MessagePipe mp; |
267 PassByValueStructWithTraitsImpl input; | 281 PassByValueStructWithTraitsImpl input; |
268 input.get_mutable_handle().reset(mp.handle0.release()); | 282 input.get_mutable_handle().reset(mp.handle0.release()); |
269 | 283 |
270 base::RunLoop loop; | 284 base::RunLoop loop; |
271 TraitsTestServicePtr proxy = GetTraitsTestProxy(); | 285 TraitsTestServicePtr proxy = GetTraitsTestProxy(); |
272 | 286 |
273 ScopedMessagePipeHandle received; | 287 ScopedMessagePipeHandle received; |
274 proxy->EchoPassByValueStructWithTraits( | 288 proxy->EchoPassByValueStructWithTraits( |
275 std::move(input), [&](PassByValueStructWithTraitsImpl passed) { | 289 std::move(input), |
276 received.reset( | 290 base::Bind(&CaptureMessagePipe, &received, loop.QuitClosure())); |
277 MessagePipeHandle(passed.get_mutable_handle().release().value())); | |
278 loop.Quit(); | |
279 }); | |
280 loop.Run(); | 291 loop.Run(); |
281 | 292 |
282 ASSERT_TRUE(received.is_valid()); | 293 ASSERT_TRUE(received.is_valid()); |
283 | 294 |
284 // Verify that the message pipe handle is correctly passed. | 295 // Verify that the message pipe handle is correctly passed. |
285 const char kHello[] = "hello"; | 296 const char kHello[] = "hello"; |
286 const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); | 297 const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); |
287 EXPECT_EQ(MOJO_RESULT_OK, | 298 EXPECT_EQ(MOJO_RESULT_OK, |
288 WriteMessageRaw(mp.handle1.get(), kHello, kHelloSize, nullptr, 0, | 299 WriteMessageRaw(mp.handle1.get(), kHello, kHelloSize, nullptr, 0, |
289 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 300 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
290 | 301 |
291 EXPECT_EQ(MOJO_RESULT_OK, Wait(received.get(), MOJO_HANDLE_SIGNAL_READABLE, | 302 EXPECT_EQ(MOJO_RESULT_OK, Wait(received.get(), MOJO_HANDLE_SIGNAL_READABLE, |
292 MOJO_DEADLINE_INDEFINITE, nullptr)); | 303 MOJO_DEADLINE_INDEFINITE, nullptr)); |
293 | 304 |
294 char buffer[10] = {0}; | 305 char buffer[10] = {0}; |
295 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 306 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
296 EXPECT_EQ(MOJO_RESULT_OK, | 307 EXPECT_EQ(MOJO_RESULT_OK, |
297 ReadMessageRaw(received.get(), buffer, &buffer_size, nullptr, | 308 ReadMessageRaw(received.get(), buffer, &buffer_size, nullptr, |
298 nullptr, MOJO_READ_MESSAGE_FLAG_NONE)); | 309 nullptr, MOJO_READ_MESSAGE_FLAG_NONE)); |
299 EXPECT_EQ(kHelloSize, buffer_size); | 310 EXPECT_EQ(kHelloSize, buffer_size); |
300 EXPECT_STREQ(kHello, buffer); | 311 EXPECT_STREQ(kHello, buffer); |
301 } | 312 } |
302 | 313 |
| 314 void ExpectEnumWithTraits(EnumWithTraitsImpl expected_value, |
| 315 const base::Closure& closure, |
| 316 EnumWithTraitsImpl value) { |
| 317 EXPECT_EQ(expected_value, value); |
| 318 closure.Run(); |
| 319 } |
| 320 |
303 TEST_F(StructTraitsTest, EchoEnumWithTraits) { | 321 TEST_F(StructTraitsTest, EchoEnumWithTraits) { |
304 base::RunLoop loop; | 322 base::RunLoop loop; |
305 TraitsTestServicePtr proxy = GetTraitsTestProxy(); | 323 TraitsTestServicePtr proxy = GetTraitsTestProxy(); |
306 | 324 |
307 proxy->EchoEnumWithTraits( | 325 proxy->EchoEnumWithTraits( |
308 EnumWithTraitsImpl::CUSTOM_VALUE_1, [&](EnumWithTraitsImpl passed) { | 326 EnumWithTraitsImpl::CUSTOM_VALUE_1, |
309 EXPECT_EQ(EnumWithTraitsImpl::CUSTOM_VALUE_1, passed); | 327 base::Bind(&ExpectEnumWithTraits, EnumWithTraitsImpl::CUSTOM_VALUE_1, |
310 loop.Quit(); | 328 loop.QuitClosure())); |
311 }); | |
312 loop.Run(); | 329 loop.Run(); |
313 } | 330 } |
314 | 331 |
315 TEST_F(StructTraitsTest, SerializeStructWithTraits) { | 332 TEST_F(StructTraitsTest, SerializeStructWithTraits) { |
316 StructWithTraitsImpl input; | 333 StructWithTraitsImpl input; |
317 input.set_enum(EnumWithTraitsImpl::CUSTOM_VALUE_1); | 334 input.set_enum(EnumWithTraitsImpl::CUSTOM_VALUE_1); |
318 input.set_bool(true); | 335 input.set_bool(true); |
319 input.set_uint32(7); | 336 input.set_uint32(7); |
320 input.set_uint64(42); | 337 input.set_uint64(42); |
321 input.set_string("hello world!"); | 338 input.set_string("hello world!"); |
(...skipping 15 matching lines...) Expand all Loading... |
337 EXPECT_EQ(input.get_uint64(), output.get_uint64()); | 354 EXPECT_EQ(input.get_uint64(), output.get_uint64()); |
338 EXPECT_EQ(input.get_string(), output.get_string()); | 355 EXPECT_EQ(input.get_string(), output.get_string()); |
339 EXPECT_EQ(input.get_string_array(), output.get_string_array()); | 356 EXPECT_EQ(input.get_string_array(), output.get_string_array()); |
340 EXPECT_EQ(input.get_struct(), output.get_struct()); | 357 EXPECT_EQ(input.get_struct(), output.get_struct()); |
341 EXPECT_EQ(input.get_struct_array(), output.get_struct_array()); | 358 EXPECT_EQ(input.get_struct_array(), output.get_struct_array()); |
342 EXPECT_EQ(input.get_struct_map(), output.get_struct_map()); | 359 EXPECT_EQ(input.get_struct_map(), output.get_struct_map()); |
343 } | 360 } |
344 | 361 |
345 } // namespace test | 362 } // namespace test |
346 } // namespace mojo | 363 } // namespace mojo |
OLD | NEW |