| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <vector> | 5 #include <vector> |
| 6 #include "mojo/public/cpp/bindings/array.h" | 6 #include "mojo/public/cpp/bindings/array.h" |
| 7 #include "mojo/public/cpp/bindings/binding.h" | 7 #include "mojo/public/cpp/bindings/binding.h" |
| 8 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 8 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
| 10 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 10 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 void* raw_buf = buf.Leak(); | 376 void* raw_buf = buf.Leak(); |
| 377 EXPECT_FALSE( | 377 EXPECT_FALSE( |
| 378 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 378 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
| 379 free(raw_buf); | 379 free(raw_buf); |
| 380 } | 380 } |
| 381 | 381 |
| 382 // TODO(azani): Move back in array_unittest.cc when possible. | 382 // TODO(azani): Move back in array_unittest.cc when possible. |
| 383 // Array tests | 383 // Array tests |
| 384 TEST(UnionTest, PodUnionInArray) { | 384 TEST(UnionTest, PodUnionInArray) { |
| 385 SmallStructPtr small_struct(SmallStruct::New()); | 385 SmallStructPtr small_struct(SmallStruct::New()); |
| 386 small_struct->pod_union_array = Array<PodUnionPtr>(2); | 386 small_struct->pod_union_array = Array<PodUnionPtr>::New(2); |
| 387 small_struct->pod_union_array[0] = PodUnion::New(); | 387 small_struct->pod_union_array[0] = PodUnion::New(); |
| 388 small_struct->pod_union_array[1] = PodUnion::New(); | 388 small_struct->pod_union_array[1] = PodUnion::New(); |
| 389 | 389 |
| 390 small_struct->pod_union_array[0]->set_f_int8(10); | 390 small_struct->pod_union_array[0]->set_f_int8(10); |
| 391 small_struct->pod_union_array[1]->set_f_int16(12); | 391 small_struct->pod_union_array[1]->set_f_int16(12); |
| 392 | 392 |
| 393 EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8()); | 393 EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8()); |
| 394 EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16()); | 394 EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16()); |
| 395 } | 395 } |
| 396 | 396 |
| 397 TEST(UnionTest, PodUnionInArraySerialization) { | 397 TEST(UnionTest, PodUnionInArraySerialization) { |
| 398 Environment environment; | 398 Environment environment; |
| 399 Array<PodUnionPtr> array(2); | 399 auto array = Array<PodUnionPtr>::New(2); |
| 400 array[0] = PodUnion::New(); | 400 array[0] = PodUnion::New(); |
| 401 array[1] = PodUnion::New(); | 401 array[1] = PodUnion::New(); |
| 402 | 402 |
| 403 array[0]->set_f_int8(10); | 403 array[0]->set_f_int8(10); |
| 404 array[1]->set_f_int16(12); | 404 array[1]->set_f_int16(12); |
| 405 EXPECT_EQ(2U, array.size()); | 405 EXPECT_EQ(2U, array.size()); |
| 406 | 406 |
| 407 size_t size = GetSerializedSize_(array); | 407 size_t size = GetSerializedSize_(array); |
| 408 EXPECT_EQ(40U, size); | 408 EXPECT_EQ(40U, size); |
| 409 | 409 |
| 410 mojo::internal::FixedBufferForTesting buf(size); | 410 mojo::internal::FixedBufferForTesting buf(size); |
| 411 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | 411 mojo::internal::Array_Data<internal::PodUnion_Data>* data; |
| 412 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); | 412 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); |
| 413 SerializeArray_(&array, &buf, &data, &validate_params); | 413 SerializeArray_(&array, &buf, &data, &validate_params); |
| 414 | 414 |
| 415 Array<PodUnionPtr> array2; | 415 Array<PodUnionPtr> array2; |
| 416 Deserialize_(data, &array2); | 416 Deserialize_(data, &array2); |
| 417 | 417 |
| 418 EXPECT_EQ(2U, array2.size()); | 418 EXPECT_EQ(2U, array2.size()); |
| 419 | 419 |
| 420 EXPECT_EQ(10, array2[0]->get_f_int8()); | 420 EXPECT_EQ(10, array2[0]->get_f_int8()); |
| 421 EXPECT_EQ(12, array2[1]->get_f_int16()); | 421 EXPECT_EQ(12, array2[1]->get_f_int16()); |
| 422 } | 422 } |
| 423 | 423 |
| 424 TEST(UnionTest, PodUnionInArrayValidation) { | 424 TEST(UnionTest, PodUnionInArrayValidation) { |
| 425 Environment environment; | 425 Environment environment; |
| 426 Array<PodUnionPtr> array(2); | 426 auto array = Array<PodUnionPtr>::New(2); |
| 427 array[0] = PodUnion::New(); | 427 array[0] = PodUnion::New(); |
| 428 array[1] = PodUnion::New(); | 428 array[1] = PodUnion::New(); |
| 429 | 429 |
| 430 array[0]->set_f_int8(10); | 430 array[0]->set_f_int8(10); |
| 431 array[1]->set_f_int16(12); | 431 array[1]->set_f_int16(12); |
| 432 | 432 |
| 433 size_t size = GetSerializedSize_(array); | 433 size_t size = GetSerializedSize_(array); |
| 434 | 434 |
| 435 mojo::internal::FixedBufferForTesting buf(size); | 435 mojo::internal::FixedBufferForTesting buf(size); |
| 436 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | 436 mojo::internal::Array_Data<internal::PodUnion_Data>* data; |
| 437 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); | 437 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); |
| 438 SerializeArray_(&array, &buf, &data, &validate_params); | 438 SerializeArray_(&array, &buf, &data, &validate_params); |
| 439 | 439 |
| 440 void* raw_buf = buf.Leak(); | 440 void* raw_buf = buf.Leak(); |
| 441 mojo::internal::BoundsChecker bounds_checker(data, | 441 mojo::internal::BoundsChecker bounds_checker(data, |
| 442 static_cast<uint32_t>(size), 1); | 442 static_cast<uint32_t>(size), 1); |
| 443 | 443 |
| 444 EXPECT_TRUE(Array<PodUnionPtr>::Data_::Validate(raw_buf, &bounds_checker, | 444 EXPECT_TRUE(Array<PodUnionPtr>::Data_::Validate(raw_buf, &bounds_checker, |
| 445 &validate_params)); | 445 &validate_params)); |
| 446 free(raw_buf); | 446 free(raw_buf); |
| 447 } | 447 } |
| 448 TEST(UnionTest, PodUnionInArraySerializationWithNull) { | 448 TEST(UnionTest, PodUnionInArraySerializationWithNull) { |
| 449 Environment environment; | 449 Environment environment; |
| 450 Array<PodUnionPtr> array(2); | 450 auto array = Array<PodUnionPtr>::New(2); |
| 451 array[0] = PodUnion::New(); | 451 array[0] = PodUnion::New(); |
| 452 | 452 |
| 453 array[0]->set_f_int8(10); | 453 array[0]->set_f_int8(10); |
| 454 EXPECT_EQ(2U, array.size()); | 454 EXPECT_EQ(2U, array.size()); |
| 455 | 455 |
| 456 size_t size = GetSerializedSize_(array); | 456 size_t size = GetSerializedSize_(array); |
| 457 EXPECT_EQ(40U, size); | 457 EXPECT_EQ(40U, size); |
| 458 | 458 |
| 459 mojo::internal::FixedBufferForTesting buf(size); | 459 mojo::internal::FixedBufferForTesting buf(size); |
| 460 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | 460 mojo::internal::Array_Data<internal::PodUnion_Data>* data; |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 mojo::internal::BoundsChecker bounds_checker(data, | 762 mojo::internal::BoundsChecker bounds_checker(data, |
| 763 static_cast<uint32_t>(size), 0); | 763 static_cast<uint32_t>(size), 0); |
| 764 EXPECT_TRUE( | 764 EXPECT_TRUE( |
| 765 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | 765 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); |
| 766 free(raw_buf); | 766 free(raw_buf); |
| 767 } | 767 } |
| 768 | 768 |
| 769 TEST(UnionTest, ArrayInUnionGetterSetter) { | 769 TEST(UnionTest, ArrayInUnionGetterSetter) { |
| 770 Environment environment; | 770 Environment environment; |
| 771 | 771 |
| 772 Array<int8_t> array(2); | 772 auto array = Array<int8_t>::New(2); |
| 773 array[0] = 8; | 773 array[0] = 8; |
| 774 array[1] = 9; | 774 array[1] = 9; |
| 775 | 775 |
| 776 ObjectUnionPtr obj(ObjectUnion::New()); | 776 ObjectUnionPtr obj(ObjectUnion::New()); |
| 777 obj->set_f_array_int8(array.Pass()); | 777 obj->set_f_array_int8(array.Pass()); |
| 778 | 778 |
| 779 EXPECT_EQ(8, obj->get_f_array_int8()[0]); | 779 EXPECT_EQ(8, obj->get_f_array_int8()[0]); |
| 780 EXPECT_EQ(9, obj->get_f_array_int8()[1]); | 780 EXPECT_EQ(9, obj->get_f_array_int8()[1]); |
| 781 } | 781 } |
| 782 | 782 |
| 783 TEST(UnionTest, ArrayInUnionSerialization) { | 783 TEST(UnionTest, ArrayInUnionSerialization) { |
| 784 Environment environment; | 784 Environment environment; |
| 785 | 785 |
| 786 Array<int8_t> array(2); | 786 auto array = Array<int8_t>::New(2); |
| 787 array[0] = 8; | 787 array[0] = 8; |
| 788 array[1] = 9; | 788 array[1] = 9; |
| 789 | 789 |
| 790 ObjectUnionPtr obj(ObjectUnion::New()); | 790 ObjectUnionPtr obj(ObjectUnion::New()); |
| 791 obj->set_f_array_int8(array.Pass()); | 791 obj->set_f_array_int8(array.Pass()); |
| 792 | 792 |
| 793 size_t size = GetSerializedSize_(obj, false); | 793 size_t size = GetSerializedSize_(obj, false); |
| 794 EXPECT_EQ(32U, size); | 794 EXPECT_EQ(32U, size); |
| 795 | 795 |
| 796 mojo::internal::FixedBufferForTesting buf(size); | 796 mojo::internal::FixedBufferForTesting buf(size); |
| 797 internal::ObjectUnion_Data* data = nullptr; | 797 internal::ObjectUnion_Data* data = nullptr; |
| 798 SerializeUnion_(obj.get(), &buf, &data, false); | 798 SerializeUnion_(obj.get(), &buf, &data, false); |
| 799 | 799 |
| 800 std::vector<Handle> handles; | 800 std::vector<Handle> handles; |
| 801 data->EncodePointersAndHandles(&handles); | 801 data->EncodePointersAndHandles(&handles); |
| 802 data->DecodePointersAndHandles(&handles); | 802 data->DecodePointersAndHandles(&handles); |
| 803 | 803 |
| 804 ObjectUnionPtr obj2 = ObjectUnion::New(); | 804 ObjectUnionPtr obj2 = ObjectUnion::New(); |
| 805 Deserialize_(data, obj2.get()); | 805 Deserialize_(data, obj2.get()); |
| 806 | 806 |
| 807 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); | 807 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); |
| 808 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); | 808 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); |
| 809 } | 809 } |
| 810 | 810 |
| 811 TEST(UnionTest, ArrayInUnionValidation) { | 811 TEST(UnionTest, ArrayInUnionValidation) { |
| 812 Environment environment; | 812 Environment environment; |
| 813 | 813 |
| 814 Array<int8_t> array(2); | 814 auto array = Array<int8_t>::New(2); |
| 815 array[0] = 8; | 815 array[0] = 8; |
| 816 array[1] = 9; | 816 array[1] = 9; |
| 817 | 817 |
| 818 ObjectUnionPtr obj(ObjectUnion::New()); | 818 ObjectUnionPtr obj(ObjectUnion::New()); |
| 819 obj->set_f_array_int8(array.Pass()); | 819 obj->set_f_array_int8(array.Pass()); |
| 820 | 820 |
| 821 size_t size = GetSerializedSize_(obj, false); | 821 size_t size = GetSerializedSize_(obj, false); |
| 822 mojo::internal::FixedBufferForTesting buf(size); | 822 mojo::internal::FixedBufferForTesting buf(size); |
| 823 internal::ObjectUnion_Data* data = nullptr; | 823 internal::ObjectUnion_Data* data = nullptr; |
| 824 SerializeUnion_(obj.get(), &buf, &data, false); | 824 SerializeUnion_(obj.get(), &buf, &data, false); |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1169 PodUnionPtr pod(PodUnion::New()); | 1169 PodUnionPtr pod(PodUnion::New()); |
| 1170 pod->set_f_int16(16); | 1170 pod->set_f_int16(16); |
| 1171 | 1171 |
| 1172 ptr->Echo(pod.Pass(), | 1172 ptr->Echo(pod.Pass(), |
| 1173 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); | 1173 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); |
| 1174 run_loop.RunUntilIdle(); | 1174 run_loop.RunUntilIdle(); |
| 1175 } | 1175 } |
| 1176 | 1176 |
| 1177 } // namespace test | 1177 } // namespace test |
| 1178 } // namespace mojo | 1178 } // namespace mojo |
| OLD | NEW |