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