OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 | 7 |
8 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" | 8 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 } | 37 } |
38 | 38 |
39 return true; | 39 return true; |
40 } | 40 } |
41 | 41 |
42 } // namespace | 42 } // namespace |
43 | 43 |
44 template <> | 44 template <> |
45 struct TypeConverter<test::RectPtr, RedmondRect> { | 45 struct TypeConverter<test::RectPtr, RedmondRect> { |
46 static test::RectPtr Convert(const RedmondRect& input) { | 46 static test::RectPtr Convert(const RedmondRect& input) { |
47 test::RectPtr rect(test::Rect::New()); | 47 return test::Rect::New(input.left, input.top, input.right - input.left, |
48 rect->x = input.left; | 48 input.bottom - input.top); |
49 rect->y = input.top; | |
50 rect->width = input.right - input.left; | |
51 rect->height = input.bottom - input.top; | |
52 return rect; | |
53 } | 49 } |
54 }; | 50 }; |
55 | 51 |
56 template <> | 52 template <> |
57 struct TypeConverter<RedmondRect, test::RectPtr> { | 53 struct TypeConverter<RedmondRect, test::RectPtr> { |
58 static RedmondRect Convert(const test::RectPtr& input) { | 54 static RedmondRect Convert(const test::RectPtr& input) { |
59 RedmondRect rect; | 55 RedmondRect rect; |
60 rect.left = input->x; | 56 rect.left = input->x; |
61 rect.top = input->y; | 57 rect.top = input->y; |
62 rect.right = input->x + input->width; | 58 rect.right = input->x + input->width; |
63 rect.bottom = input->y + input->height; | 59 rect.bottom = input->y + input->height; |
64 return rect; | 60 return rect; |
65 } | 61 } |
66 }; | 62 }; |
67 | 63 |
68 template <> | 64 template <> |
69 struct TypeConverter<test::NamedRegionPtr, RedmondNamedRegion> { | 65 struct TypeConverter<test::NamedRegionPtr, RedmondNamedRegion> { |
70 static test::NamedRegionPtr Convert(const RedmondNamedRegion& input) { | 66 static test::NamedRegionPtr Convert(const RedmondNamedRegion& input) { |
71 test::NamedRegionPtr region(test::NamedRegion::New()); | 67 return test::NamedRegion::New( |
72 region->name.emplace(input.name); | 68 input.name, ConvertTo<std::vector<test::RectPtr>>(input.rects)); |
73 region->rects = ConvertTo<std::vector<test::RectPtr>>(input.rects); | |
74 return region; | |
75 } | 69 } |
76 }; | 70 }; |
77 | 71 |
78 template <> | 72 template <> |
79 struct TypeConverter<RedmondNamedRegion, test::NamedRegionPtr> { | 73 struct TypeConverter<RedmondNamedRegion, test::NamedRegionPtr> { |
80 static RedmondNamedRegion Convert(const test::NamedRegionPtr& input) { | 74 static RedmondNamedRegion Convert(const test::NamedRegionPtr& input) { |
81 RedmondNamedRegion region; | 75 RedmondNamedRegion region; |
82 if (input->name) | 76 if (input->name) |
83 region.name = input->name.value(); | 77 region.name = input->name.value(); |
84 if (input->rects) { | 78 if (input->rects) { |
85 region.rects.reserve(input->rects->size()); | 79 region.rects.reserve(input->rects->size()); |
86 for (const auto& element : *input->rects) | 80 for (const auto& element : *input->rects) |
87 region.rects.push_back(element.To<RedmondRect>()); | 81 region.rects.push_back(element.To<RedmondRect>()); |
88 } | 82 } |
89 return region; | 83 return region; |
90 } | 84 } |
91 }; | 85 }; |
92 | 86 |
93 namespace test { | 87 namespace test { |
94 namespace { | 88 namespace { |
95 | 89 |
96 TEST(TypeConversionTest, CustomTypeConverter) { | 90 TEST(TypeConversionTest, CustomTypeConverter) { |
97 RectPtr rect(Rect::New()); | 91 RectPtr rect(Rect::New(10, 20, 50, 45)); |
98 rect->x = 10; | |
99 rect->y = 20; | |
100 rect->width = 50; | |
101 rect->height = 45; | |
102 | 92 |
103 RedmondRect rr = rect.To<RedmondRect>(); | 93 RedmondRect rr = rect.To<RedmondRect>(); |
104 EXPECT_EQ(10, rr.left); | 94 EXPECT_EQ(10, rr.left); |
105 EXPECT_EQ(20, rr.top); | 95 EXPECT_EQ(20, rr.top); |
106 EXPECT_EQ(60, rr.right); | 96 EXPECT_EQ(60, rr.right); |
107 EXPECT_EQ(65, rr.bottom); | 97 EXPECT_EQ(65, rr.bottom); |
108 | 98 |
109 RectPtr rect2(Rect::From(rr)); | 99 RectPtr rect2(Rect::From(rr)); |
110 EXPECT_EQ(rect->x, rect2->x); | 100 EXPECT_EQ(rect->x, rect2->x); |
111 EXPECT_EQ(rect->y, rect2->y); | 101 EXPECT_EQ(rect->y, rect2->y); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 EXPECT_EQ(redmond_region.rects[i].left, redmond_region2.rects[i].left); | 152 EXPECT_EQ(redmond_region.rects[i].left, redmond_region2.rects[i].left); |
163 EXPECT_EQ(redmond_region.rects[i].top, redmond_region2.rects[i].top); | 153 EXPECT_EQ(redmond_region.rects[i].top, redmond_region2.rects[i].top); |
164 EXPECT_EQ(redmond_region.rects[i].right, redmond_region2.rects[i].right); | 154 EXPECT_EQ(redmond_region.rects[i].right, redmond_region2.rects[i].right); |
165 EXPECT_EQ(redmond_region.rects[i].bottom, redmond_region2.rects[i].bottom); | 155 EXPECT_EQ(redmond_region.rects[i].bottom, redmond_region2.rects[i].bottom); |
166 } | 156 } |
167 } | 157 } |
168 | 158 |
169 } // namespace | 159 } // namespace |
170 } // namespace test | 160 } // namespace test |
171 } // namespace mojo | 161 } // namespace mojo |
OLD | NEW |