| 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 <string.h> | 5 #include <string.h> |
| 6 #include <type_traits> | 6 #include <type_traits> |
| 7 | 7 |
| 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
| 9 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 9 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 10 #include "mojo/public/cpp/environment/environment.h" | 10 #include "mojo/public/cpp/environment/environment.h" |
| 11 #include "mojo/public/cpp/system/message_pipe.h" | 11 #include "mojo/public/cpp/system/message_pipe.h" |
| 12 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" | 12 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 namespace test { | 16 namespace test { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 static_assert(std::is_same<std::underlying_type<ScopedConstants::EType>::type, | 19 static_assert(std::is_same<std::underlying_type<ScopedConstants::EType>::type, |
| 20 int32_t>::value, | 20 int32_t>::value, |
| 21 "The underlying type of mojom generated enums must be int32_t."); | 21 "The underlying type of mojom generated enums must be int32_t."); |
| 22 | 22 |
| 23 RectPtr MakeRect(int32_t factor = 1) { | 23 RectPtr MakeRect(int32_t factor = 1) { |
| 24 RectPtr rect(Rect::New()); | 24 RectPtr rect(Rect::New()); |
| 25 rect->x = 1 * factor; | 25 rect->x = 1 * factor; |
| 26 rect->y = 2 * factor; | 26 rect->y = 2 * factor; |
| 27 rect->width = 10 * factor; | 27 rect->width = 10 * factor; |
| 28 rect->height = 20 * factor; | 28 rect->height = 20 * factor; |
| 29 return rect.Pass(); | 29 return rect; |
| 30 } | 30 } |
| 31 | 31 |
| 32 void CheckRect(const Rect& rect, int32_t factor = 1) { | 32 void CheckRect(const Rect& rect, int32_t factor = 1) { |
| 33 EXPECT_EQ(1 * factor, rect.x); | 33 EXPECT_EQ(1 * factor, rect.x); |
| 34 EXPECT_EQ(2 * factor, rect.y); | 34 EXPECT_EQ(2 * factor, rect.y); |
| 35 EXPECT_EQ(10 * factor, rect.width); | 35 EXPECT_EQ(10 * factor, rect.width); |
| 36 EXPECT_EQ(20 * factor, rect.height); | 36 EXPECT_EQ(20 * factor, rect.height); |
| 37 } | 37 } |
| 38 | 38 |
| 39 MultiVersionStructPtr MakeMultiVersionStruct() { | 39 MultiVersionStructPtr MakeMultiVersionStruct() { |
| 40 MultiVersionStructPtr output(MultiVersionStruct::New()); | 40 MultiVersionStructPtr output(MultiVersionStruct::New()); |
| 41 output->f_int32 = 123; | 41 output->f_int32 = 123; |
| 42 output->f_rect = MakeRect(5); | 42 output->f_rect = MakeRect(5); |
| 43 output->f_string = "hello"; | 43 output->f_string = "hello"; |
| 44 output->f_array = Array<int8_t>::New(3); | 44 output->f_array = Array<int8_t>::New(3); |
| 45 output->f_array[0] = 10; | 45 output->f_array[0] = 10; |
| 46 output->f_array[1] = 9; | 46 output->f_array[1] = 9; |
| 47 output->f_array[2] = 8; | 47 output->f_array[2] = 8; |
| 48 MessagePipe pipe; | 48 MessagePipe pipe; |
| 49 output->f_message_pipe = pipe.handle0.Pass(); | 49 output->f_message_pipe = pipe.handle0.Pass(); |
| 50 output->f_int16 = 42; | 50 output->f_int16 = 42; |
| 51 | 51 |
| 52 return output.Pass(); | 52 return output; |
| 53 } | 53 } |
| 54 | 54 |
| 55 template <typename U, typename T> | 55 template <typename U, typename T> |
| 56 U SerializeAndDeserialize(T input) { | 56 U SerializeAndDeserialize(T input) { |
| 57 typedef typename mojo::internal::WrapperTraits<T>::DataType InputDataType; | 57 typedef typename mojo::internal::WrapperTraits<T>::DataType InputDataType; |
| 58 typedef typename mojo::internal::WrapperTraits<U>::DataType OutputDataType; | 58 typedef typename mojo::internal::WrapperTraits<U>::DataType OutputDataType; |
| 59 | 59 |
| 60 size_t size = GetSerializedSize_(*input); | 60 size_t size = GetSerializedSize_(*input); |
| 61 mojo::internal::FixedBufferForTesting buf(size + 32); | 61 mojo::internal::FixedBufferForTesting buf(size + 32); |
| 62 InputDataType data; | 62 InputDataType data; |
| 63 EXPECT_EQ(mojo::internal::ValidationError::NONE, | 63 EXPECT_EQ(mojo::internal::ValidationError::NONE, |
| 64 Serialize_(input.get(), &buf, &data)); | 64 Serialize_(input.get(), &buf, &data)); |
| 65 | 65 |
| 66 std::vector<Handle> handles; | 66 std::vector<Handle> handles; |
| 67 data->EncodePointersAndHandles(&handles); | 67 data->EncodePointersAndHandles(&handles); |
| 68 | 68 |
| 69 // Set the subsequent area to a special value, so that we can find out if we | 69 // Set the subsequent area to a special value, so that we can find out if we |
| 70 // mistakenly access the area. | 70 // mistakenly access the area. |
| 71 void* subsequent_area = buf.Allocate(32); | 71 void* subsequent_area = buf.Allocate(32); |
| 72 memset(subsequent_area, 0xAA, 32); | 72 memset(subsequent_area, 0xAA, 32); |
| 73 | 73 |
| 74 OutputDataType output_data = reinterpret_cast<OutputDataType>(data); | 74 OutputDataType output_data = reinterpret_cast<OutputDataType>(data); |
| 75 output_data->DecodePointersAndHandles(&handles); | 75 output_data->DecodePointersAndHandles(&handles); |
| 76 | 76 |
| 77 using RawUType = typename mojo::internal::RemoveStructPtr<U>::type; | 77 using RawUType = typename mojo::internal::RemoveStructPtr<U>::type; |
| 78 U output(RawUType::New()); | 78 U output(RawUType::New()); |
| 79 Deserialize_(output_data, output.get()); | 79 Deserialize_(output_data, output.get()); |
| 80 return output.Pass(); | 80 return output; |
| 81 } | 81 } |
| 82 | 82 |
| 83 class StructTest : public testing::Test { | 83 class StructTest : public testing::Test { |
| 84 public: | 84 public: |
| 85 ~StructTest() override {} | 85 ~StructTest() override {} |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 Environment env_; | 88 Environment env_; |
| 89 }; | 89 }; |
| 90 | 90 |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 | 454 |
| 455 MultiVersionStructV0Ptr output = | 455 MultiVersionStructV0Ptr output = |
| 456 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass()); | 456 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass()); |
| 457 EXPECT_TRUE(output); | 457 EXPECT_TRUE(output); |
| 458 EXPECT_TRUE(output->Equals(*expected_output)); | 458 EXPECT_TRUE(output->Equals(*expected_output)); |
| 459 } | 459 } |
| 460 } | 460 } |
| 461 | 461 |
| 462 } // namespace test | 462 } // namespace test |
| 463 } // namespace mojo | 463 } // namespace mojo |
| OLD | NEW |