| 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 <string> | 5 #include <string> |
| 6 #include <utility> |
| 6 | 7 |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/callback.h" | 9 #include "base/callback.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 12 #include "mojo/public/cpp/bindings/binding_set.h" | 13 #include "mojo/public/cpp/bindings/binding_set.h" |
| 13 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/interface_request.h" |
| 14 #include "mojo/public/cpp/bindings/tests/pickled_types_blink.h" | 15 #include "mojo/public/cpp/bindings/tests/pickled_types_blink.h" |
| 15 #include "mojo/public/cpp/bindings/tests/pickled_types_chromium.h" | 16 #include "mojo/public/cpp/bindings/tests/pickled_types_chromium.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 template <typename T> | 68 template <typename T> |
| 68 base::Callback<void(T)> EnumFail(const std::string& reason) { | 69 base::Callback<void(T)> EnumFail(const std::string& reason) { |
| 69 return base::Bind(&DoEnumFail<T>, reason); | 70 return base::Bind(&DoEnumFail<T>, reason); |
| 70 } | 71 } |
| 71 | 72 |
| 72 template <typename T> | 73 template <typename T> |
| 73 void ExpectError(InterfacePtr<T>* proxy, const base::Closure& callback) { | 74 void ExpectError(InterfacePtr<T>* proxy, const base::Closure& callback) { |
| 74 proxy->set_connection_error_handler(callback); | 75 proxy->set_connection_error_handler(callback); |
| 75 } | 76 } |
| 76 | 77 |
| 78 template <typename Func, typename Arg> |
| 79 void RunSimpleLambda(Func func, Arg arg) { func(std::move(arg)); } |
| 80 |
| 81 template <typename Arg, typename Func> |
| 82 base::Callback<void(Arg)> BindSimpleLambda(Func func) { |
| 83 return base::Bind(&RunSimpleLambda<Func, Arg>, func); |
| 84 } |
| 85 |
| 77 // This implements the generated Chromium variant of PicklePasser. | 86 // This implements the generated Chromium variant of PicklePasser. |
| 78 class ChromiumPicklePasserImpl : public PicklePasser { | 87 class ChromiumPicklePasserImpl : public PicklePasser { |
| 79 public: | 88 public: |
| 80 ChromiumPicklePasserImpl() {} | 89 ChromiumPicklePasserImpl() {} |
| 81 | 90 |
| 82 // mojo::test::PicklePasser: | 91 // mojo::test::PicklePasser: |
| 83 void PassPickledStruct(const PickledStructChromium& pickle, | 92 void PassPickledStruct(const PickledStructChromium& pickle, |
| 84 const PassPickledStructCallback& callback) override { | 93 const PassPickledStructCallback& callback) override { |
| 85 callback.Run(pickle); | 94 callback.Run(pickle); |
| 86 } | 95 } |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 pickles[1].set_foo(3); | 306 pickles[1].set_foo(3); |
| 298 pickles[1].set_bar(4); | 307 pickles[1].set_bar(4); |
| 299 pickles[1].set_baz(100); | 308 pickles[1].set_baz(100); |
| 300 { | 309 { |
| 301 base::RunLoop run_loop; | 310 base::RunLoop run_loop; |
| 302 // Verify that the array of pickled structs can be serialized and | 311 // Verify that the array of pickled structs can be serialized and |
| 303 // deserialized intact. This ensures that the ParamTraits are actually used | 312 // deserialized intact. This ensures that the ParamTraits are actually used |
| 304 // rather than doing a byte-for-byte copy of the element data, beacuse the | 313 // rather than doing a byte-for-byte copy of the element data, beacuse the |
| 305 // |baz| field should never be serialized. | 314 // |baz| field should never be serialized. |
| 306 proxy->PassPickles(std::move(pickles), | 315 proxy->PassPickles(std::move(pickles), |
| 307 [&](Array<PickledStructChromium> passed) { | 316 BindSimpleLambda<Array<PickledStructChromium>>( |
| 308 ASSERT_FALSE(passed.is_null()); | 317 [&](Array<PickledStructChromium> passed) { |
| 309 ASSERT_EQ(2u, passed.size()); | 318 ASSERT_FALSE(passed.is_null()); |
| 310 EXPECT_EQ(1, passed[0].foo()); | 319 ASSERT_EQ(2u, passed.size()); |
| 311 EXPECT_EQ(2, passed[0].bar()); | 320 EXPECT_EQ(1, passed[0].foo()); |
| 312 EXPECT_EQ(0, passed[0].baz()); | 321 EXPECT_EQ(2, passed[0].bar()); |
| 313 EXPECT_EQ(3, passed[1].foo()); | 322 EXPECT_EQ(0, passed[0].baz()); |
| 314 EXPECT_EQ(4, passed[1].bar()); | 323 EXPECT_EQ(3, passed[1].foo()); |
| 315 EXPECT_EQ(0, passed[1].baz()); | 324 EXPECT_EQ(4, passed[1].bar()); |
| 316 run_loop.Quit(); | 325 EXPECT_EQ(0, passed[1].baz()); |
| 317 }); | 326 run_loop.Quit(); |
| 327 })); |
| 318 run_loop.Run(); | 328 run_loop.Run(); |
| 319 } | 329 } |
| 320 } | 330 } |
| 321 | 331 |
| 322 TEST_F(PickleTest, PickleArrayArray) { | 332 TEST_F(PickleTest, PickleArrayArray) { |
| 323 auto proxy = ConnectToChromiumService(); | 333 auto proxy = ConnectToChromiumService(); |
| 324 auto pickle_arrays = Array<Array<PickledStructChromium>>::New(2); | 334 auto pickle_arrays = Array<Array<PickledStructChromium>>::New(2); |
| 325 for (size_t i = 0; i < 2; ++i) | 335 for (size_t i = 0; i < 2; ++i) |
| 326 pickle_arrays[i] = Array<PickledStructChromium>::New(2); | 336 pickle_arrays[i] = Array<PickledStructChromium>::New(2); |
| 327 | 337 |
| 328 pickle_arrays[0][0].set_foo(1); | 338 pickle_arrays[0][0].set_foo(1); |
| 329 pickle_arrays[0][0].set_bar(2); | 339 pickle_arrays[0][0].set_bar(2); |
| 330 pickle_arrays[0][0].set_baz(100); | 340 pickle_arrays[0][0].set_baz(100); |
| 331 pickle_arrays[0][1].set_foo(3); | 341 pickle_arrays[0][1].set_foo(3); |
| 332 pickle_arrays[0][1].set_bar(4); | 342 pickle_arrays[0][1].set_bar(4); |
| 333 pickle_arrays[0][1].set_baz(100); | 343 pickle_arrays[0][1].set_baz(100); |
| 334 pickle_arrays[1][0].set_foo(5); | 344 pickle_arrays[1][0].set_foo(5); |
| 335 pickle_arrays[1][0].set_bar(6); | 345 pickle_arrays[1][0].set_bar(6); |
| 336 pickle_arrays[1][0].set_baz(100); | 346 pickle_arrays[1][0].set_baz(100); |
| 337 pickle_arrays[1][1].set_foo(7); | 347 pickle_arrays[1][1].set_foo(7); |
| 338 pickle_arrays[1][1].set_bar(8); | 348 pickle_arrays[1][1].set_bar(8); |
| 339 pickle_arrays[1][1].set_baz(100); | 349 pickle_arrays[1][1].set_baz(100); |
| 340 { | 350 { |
| 341 base::RunLoop run_loop; | 351 base::RunLoop run_loop; |
| 342 // Verify that the array-of-arrays serializes and deserializes properly. | 352 // Verify that the array-of-arrays serializes and deserializes properly. |
| 343 proxy->PassPickleArrays(std::move(pickle_arrays), | 353 proxy->PassPickleArrays( |
| 344 [&](Array<Array<PickledStructChromium>> passed) { | 354 std::move(pickle_arrays), |
| 345 ASSERT_FALSE(passed.is_null()); | 355 BindSimpleLambda<Array<Array<PickledStructChromium>>>( |
| 346 ASSERT_EQ(2u, passed.size()); | 356 [&](Array<Array<PickledStructChromium>> passed) { |
| 347 ASSERT_EQ(2u, passed[0].size()); | 357 ASSERT_FALSE(passed.is_null()); |
| 348 ASSERT_EQ(2u, passed[1].size()); | 358 ASSERT_EQ(2u, passed.size()); |
| 349 EXPECT_EQ(1, passed[0][0].foo()); | 359 ASSERT_EQ(2u, passed[0].size()); |
| 350 EXPECT_EQ(2, passed[0][0].bar()); | 360 ASSERT_EQ(2u, passed[1].size()); |
| 351 EXPECT_EQ(0, passed[0][0].baz()); | 361 EXPECT_EQ(1, passed[0][0].foo()); |
| 352 EXPECT_EQ(3, passed[0][1].foo()); | 362 EXPECT_EQ(2, passed[0][0].bar()); |
| 353 EXPECT_EQ(4, passed[0][1].bar()); | 363 EXPECT_EQ(0, passed[0][0].baz()); |
| 354 EXPECT_EQ(0, passed[0][1].baz()); | 364 EXPECT_EQ(3, passed[0][1].foo()); |
| 355 EXPECT_EQ(5, passed[1][0].foo()); | 365 EXPECT_EQ(4, passed[0][1].bar()); |
| 356 EXPECT_EQ(6, passed[1][0].bar()); | 366 EXPECT_EQ(0, passed[0][1].baz()); |
| 357 EXPECT_EQ(0, passed[1][0].baz()); | 367 EXPECT_EQ(5, passed[1][0].foo()); |
| 358 EXPECT_EQ(7, passed[1][1].foo()); | 368 EXPECT_EQ(6, passed[1][0].bar()); |
| 359 EXPECT_EQ(8, passed[1][1].bar()); | 369 EXPECT_EQ(0, passed[1][0].baz()); |
| 360 EXPECT_EQ(0, passed[1][1].baz()); | 370 EXPECT_EQ(7, passed[1][1].foo()); |
| 361 run_loop.Quit(); | 371 EXPECT_EQ(8, passed[1][1].bar()); |
| 362 }); | 372 EXPECT_EQ(0, passed[1][1].baz()); |
| 373 run_loop.Quit(); |
| 374 })); |
| 363 run_loop.Run(); | 375 run_loop.Run(); |
| 364 } | 376 } |
| 365 } | 377 } |
| 366 | 378 |
| 367 TEST_F(PickleTest, PickleContainer) { | 379 TEST_F(PickleTest, PickleContainer) { |
| 368 auto proxy = ConnectToChromiumService(); | 380 auto proxy = ConnectToChromiumService(); |
| 369 PickleContainerPtr pickle_container = PickleContainer::New(); | 381 PickleContainerPtr pickle_container = PickleContainer::New(); |
| 370 pickle_container->f_struct.set_foo(42); | 382 pickle_container->f_struct.set_foo(42); |
| 371 pickle_container->f_struct.set_bar(43); | 383 pickle_container->f_struct.set_bar(43); |
| 372 pickle_container->f_struct.set_baz(44); | 384 pickle_container->f_struct.set_baz(44); |
| 373 pickle_container->f_enum = PickledEnumChromium::VALUE_1; | 385 pickle_container->f_enum = PickledEnumChromium::VALUE_1; |
| 374 EXPECT_TRUE(pickle_container.Equals(pickle_container)); | 386 EXPECT_TRUE(pickle_container.Equals(pickle_container)); |
| 375 EXPECT_FALSE(pickle_container.Equals(PickleContainer::New())); | 387 EXPECT_FALSE(pickle_container.Equals(PickleContainer::New())); |
| 376 { | 388 { |
| 377 base::RunLoop run_loop; | 389 base::RunLoop run_loop; |
| 378 proxy->PassPickleContainer(std::move(pickle_container), | 390 proxy->PassPickleContainer(std::move(pickle_container), |
| 379 [&](PickleContainerPtr passed) { | 391 BindSimpleLambda<PickleContainerPtr>( |
| 380 ASSERT_FALSE(passed.is_null()); | 392 [&](PickleContainerPtr passed) { |
| 381 EXPECT_EQ(42, passed->f_struct.foo()); | 393 ASSERT_FALSE(passed.is_null()); |
| 382 EXPECT_EQ(43, passed->f_struct.bar()); | 394 EXPECT_EQ(42, passed->f_struct.foo()); |
| 383 EXPECT_EQ(0, passed->f_struct.baz()); | 395 EXPECT_EQ(43, passed->f_struct.bar()); |
| 384 EXPECT_EQ(PickledEnumChromium::VALUE_1, | 396 EXPECT_EQ(0, passed->f_struct.baz()); |
| 385 passed->f_enum); | 397 EXPECT_EQ(PickledEnumChromium::VALUE_1, |
| 386 run_loop.Quit(); | 398 passed->f_enum); |
| 387 }); | 399 run_loop.Quit(); |
| 400 })); |
| 388 run_loop.Run(); | 401 run_loop.Run(); |
| 389 } | 402 } |
| 390 } | 403 } |
| 391 | 404 |
| 392 } // namespace test | 405 } // namespace test |
| 393 } // namespace mojo | 406 } // namespace mojo |
| OLD | NEW |