| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "mojo/public/cpp/bindings/array.h" | 12 #include "mojo/public/cpp/bindings/array.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 14 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 15 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 15 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
| 16 #include "mojo/public/cpp/bindings/lib/serialization.h" | 16 #include "mojo/public/cpp/bindings/lib/serialization.h" |
| 17 #include "mojo/public/cpp/bindings/lib/validation_context.h" | 17 #include "mojo/public/cpp/bindings/lib/validation_context.h" |
| 18 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 18 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 19 #include "mojo/public/cpp/bindings/string.h" | |
| 20 #include "mojo/public/cpp/test_support/test_utils.h" | 19 #include "mojo/public/cpp/test_support/test_utils.h" |
| 21 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" | 20 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" |
| 22 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" | 21 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 23 |
| 25 namespace mojo { | 24 namespace mojo { |
| 26 namespace test { | 25 namespace test { |
| 27 | 26 |
| 28 TEST(UnionTest, PlainOldDataGetterSetter) { | 27 TEST(UnionTest, PlainOldDataGetterSetter) { |
| 29 PodUnionPtr pod(PodUnion::New()); | 28 PodUnionPtr pod(PodUnion::New()); |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 mojo::internal::ValidationContext validation_context( | 307 mojo::internal::ValidationContext validation_context( |
| 309 data, static_cast<uint32_t>(size), 0); | 308 data, static_cast<uint32_t>(size), 0); |
| 310 EXPECT_TRUE( | 309 EXPECT_TRUE( |
| 311 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); | 310 internal::PodUnion_Data::Validate(raw_buf, &validation_context, false)); |
| 312 free(raw_buf); | 311 free(raw_buf); |
| 313 } | 312 } |
| 314 | 313 |
| 315 TEST(UnionTest, StringGetterSetter) { | 314 TEST(UnionTest, StringGetterSetter) { |
| 316 ObjectUnionPtr pod(ObjectUnion::New()); | 315 ObjectUnionPtr pod(ObjectUnion::New()); |
| 317 | 316 |
| 318 String hello("hello world"); | 317 std::string hello("hello world"); |
| 319 pod->set_f_string(hello); | 318 pod->set_f_string(hello); |
| 320 EXPECT_EQ(hello, pod->get_f_string()); | 319 EXPECT_EQ(hello, pod->get_f_string()); |
| 321 EXPECT_TRUE(pod->is_f_string()); | 320 EXPECT_TRUE(pod->is_f_string()); |
| 322 EXPECT_EQ(pod->which(), ObjectUnion::Tag::F_STRING); | 321 EXPECT_EQ(pod->which(), ObjectUnion::Tag::F_STRING); |
| 323 } | 322 } |
| 324 | 323 |
| 325 TEST(UnionTest, StringEquals) { | 324 TEST(UnionTest, StringEquals) { |
| 326 ObjectUnionPtr pod1(ObjectUnion::New()); | 325 ObjectUnionPtr pod1(ObjectUnion::New()); |
| 327 ObjectUnionPtr pod2(ObjectUnion::New()); | 326 ObjectUnionPtr pod2(ObjectUnion::New()); |
| 328 | 327 |
| 329 pod1->set_f_string("hello world"); | 328 pod1->set_f_string("hello world"); |
| 330 pod2->set_f_string("hello world"); | 329 pod2->set_f_string("hello world"); |
| 331 EXPECT_TRUE(pod1.Equals(pod2)); | 330 EXPECT_TRUE(pod1.Equals(pod2)); |
| 332 | 331 |
| 333 pod2->set_f_string("hello universe"); | 332 pod2->set_f_string("hello universe"); |
| 334 EXPECT_FALSE(pod1.Equals(pod2)); | 333 EXPECT_FALSE(pod1.Equals(pod2)); |
| 335 } | 334 } |
| 336 | 335 |
| 337 TEST(UnionTest, StringClone) { | 336 TEST(UnionTest, StringClone) { |
| 338 ObjectUnionPtr pod(ObjectUnion::New()); | 337 ObjectUnionPtr pod(ObjectUnion::New()); |
| 339 | 338 |
| 340 String hello("hello world"); | 339 std::string hello("hello world"); |
| 341 pod->set_f_string(hello); | 340 pod->set_f_string(hello); |
| 342 ObjectUnionPtr pod_clone = pod.Clone(); | 341 ObjectUnionPtr pod_clone = pod.Clone(); |
| 343 EXPECT_EQ(hello, pod_clone->get_f_string()); | 342 EXPECT_EQ(hello, pod_clone->get_f_string()); |
| 344 EXPECT_TRUE(pod_clone->is_f_string()); | 343 EXPECT_TRUE(pod_clone->is_f_string()); |
| 345 EXPECT_EQ(pod_clone->which(), ObjectUnion::Tag::F_STRING); | 344 EXPECT_EQ(pod_clone->which(), ObjectUnion::Tag::F_STRING); |
| 346 } | 345 } |
| 347 | 346 |
| 348 TEST(UnionTest, StringSerialization) { | 347 TEST(UnionTest, StringSerialization) { |
| 349 ObjectUnionPtr pod1(ObjectUnion::New()); | 348 ObjectUnionPtr pod1(ObjectUnion::New()); |
| 350 | 349 |
| 351 String hello("hello world"); | 350 std::string hello("hello world"); |
| 352 pod1->set_f_string(hello); | 351 pod1->set_f_string(hello); |
| 353 | 352 |
| 354 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( | 353 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( |
| 355 pod1, false, nullptr); | 354 pod1, false, nullptr); |
| 356 mojo::internal::FixedBufferForTesting buf(size); | 355 mojo::internal::FixedBufferForTesting buf(size); |
| 357 internal::ObjectUnion_Data* data = nullptr; | 356 internal::ObjectUnion_Data* data = nullptr; |
| 358 mojo::internal::Serialize<ObjectUnionDataView>(pod1, &buf, &data, false, | 357 mojo::internal::Serialize<ObjectUnionDataView>(pod1, &buf, &data, false, |
| 359 nullptr); | 358 nullptr); |
| 360 | 359 |
| 361 ObjectUnionPtr pod2; | 360 ObjectUnionPtr pod2; |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 data, static_cast<uint32_t>(size), 0); | 519 data, static_cast<uint32_t>(size), 0); |
| 521 ASSERT_TRUE(mojo::internal::Array_Data<internal::ObjectUnion_Data>::Validate( | 520 ASSERT_TRUE(mojo::internal::Array_Data<internal::ObjectUnion_Data>::Validate( |
| 522 data, &validation_context, &validate_params)); | 521 data, &validation_context, &validate_params)); |
| 523 | 522 |
| 524 Array<ObjectUnionPtr> array2; | 523 Array<ObjectUnionPtr> array2; |
| 525 mojo::internal::Deserialize<ArrayDataView<ObjectUnionDataView>>(data, &array2, | 524 mojo::internal::Deserialize<ArrayDataView<ObjectUnionDataView>>(data, &array2, |
| 526 nullptr); | 525 nullptr); |
| 527 | 526 |
| 528 EXPECT_EQ(2U, array2.size()); | 527 EXPECT_EQ(2U, array2.size()); |
| 529 | 528 |
| 530 EXPECT_EQ(String("hello"), array2[0]->get_f_string()); | 529 EXPECT_EQ("hello", array2[0]->get_f_string()); |
| 531 EXPECT_EQ(String("world"), array2[1]->get_f_string()); | 530 EXPECT_EQ("world", array2[1]->get_f_string()); |
| 532 } | 531 } |
| 533 | 532 |
| 534 // TODO(azani): Move back in struct_unittest.cc when possible. | 533 // TODO(azani): Move back in struct_unittest.cc when possible. |
| 535 // Struct tests | 534 // Struct tests |
| 536 TEST(UnionTest, Clone_Union) { | 535 TEST(UnionTest, Clone_Union) { |
| 537 SmallStructPtr small_struct(SmallStruct::New()); | 536 SmallStructPtr small_struct(SmallStruct::New()); |
| 538 small_struct->pod_union = PodUnion::New(); | 537 small_struct->pod_union = PodUnion::New(); |
| 539 small_struct->pod_union->set_f_int8(10); | 538 small_struct->pod_union->set_f_int8(10); |
| 540 | 539 |
| 541 SmallStructPtr clone = small_struct.Clone(); | 540 SmallStructPtr clone = small_struct.Clone(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 561 mojo::internal::Deserialize<SmallStructDataView>(data, &deserialized, | 560 mojo::internal::Deserialize<SmallStructDataView>(data, &deserialized, |
| 562 &context); | 561 &context); |
| 563 | 562 |
| 564 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); | 563 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); |
| 565 } | 564 } |
| 566 | 565 |
| 567 // Serialization test of a struct with a union of structs. | 566 // Serialization test of a struct with a union of structs. |
| 568 TEST(UnionTest, Serialization_UnionOfObjects) { | 567 TEST(UnionTest, Serialization_UnionOfObjects) { |
| 569 SmallObjStructPtr obj_struct(SmallObjStruct::New()); | 568 SmallObjStructPtr obj_struct(SmallObjStruct::New()); |
| 570 obj_struct->obj_union = ObjectUnion::New(); | 569 obj_struct->obj_union = ObjectUnion::New(); |
| 571 String hello("hello world"); | 570 std::string hello("hello world"); |
| 572 obj_struct->obj_union->set_f_string(hello); | 571 obj_struct->obj_union->set_f_string(hello); |
| 573 | 572 |
| 574 size_t size = mojo::internal::PrepareToSerialize<SmallObjStructDataView>( | 573 size_t size = mojo::internal::PrepareToSerialize<SmallObjStructDataView>( |
| 575 obj_struct, nullptr); | 574 obj_struct, nullptr); |
| 576 | 575 |
| 577 mojo::internal::FixedBufferForTesting buf(size); | 576 mojo::internal::FixedBufferForTesting buf(size); |
| 578 internal::SmallObjStruct_Data* data = nullptr; | 577 internal::SmallObjStruct_Data* data = nullptr; |
| 579 mojo::internal::Serialize<SmallObjStructDataView>(obj_struct, &buf, &data, | 578 mojo::internal::Serialize<SmallObjStructDataView>(obj_struct, &buf, &data, |
| 580 nullptr); | 579 nullptr); |
| 581 | 580 |
| (...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1239 | 1238 |
| 1240 PodUnionPtr pod(PodUnion::New()); | 1239 PodUnionPtr pod(PodUnion::New()); |
| 1241 pod->set_f_int16(16); | 1240 pod->set_f_int16(16); |
| 1242 | 1241 |
| 1243 ptr->Echo(std::move(pod), base::Bind(&ExpectInt16, 16)); | 1242 ptr->Echo(std::move(pod), base::Bind(&ExpectInt16, 16)); |
| 1244 base::RunLoop().RunUntilIdle(); | 1243 base::RunLoop().RunUntilIdle(); |
| 1245 } | 1244 } |
| 1246 | 1245 |
| 1247 } // namespace test | 1246 } // namespace test |
| 1248 } // namespace mojo | 1247 } // namespace mojo |
| OLD | NEW |