OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | |
6 #include "mojo/public/cpp/environment/environment.h" | |
7 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace mojo { | |
11 namespace test { | |
12 namespace { | |
13 | |
14 RectPtr MakeRect(int32_t factor = 1) { | |
15 RectPtr rect(Rect::New()); | |
16 rect->x = 1 * factor; | |
17 rect->y = 2 * factor; | |
18 rect->width = 10 * factor; | |
19 rect->height = 20 * factor; | |
20 return rect.Pass(); | |
21 } | |
22 | |
23 void CheckRect(const Rect& rect, int32_t factor = 1) { | |
24 EXPECT_EQ(1 * factor, rect.x); | |
25 EXPECT_EQ(2 * factor, rect.y); | |
26 EXPECT_EQ(10 * factor, rect.width); | |
27 EXPECT_EQ(20 * factor, rect.height); | |
28 } | |
29 | |
30 class StructTest : public testing::Test { | |
31 public: | |
32 ~StructTest() override {} | |
33 | |
34 private: | |
35 Environment env_; | |
36 }; | |
37 | |
38 } // namespace | |
39 | |
40 TEST_F(StructTest, Rect) { | |
41 RectPtr rect; | |
42 EXPECT_TRUE(rect.is_null()); | |
43 EXPECT_TRUE(!rect); | |
44 EXPECT_FALSE(rect); | |
45 | |
46 rect = nullptr; | |
47 EXPECT_TRUE(rect.is_null()); | |
48 EXPECT_TRUE(!rect); | |
49 EXPECT_FALSE(rect); | |
50 | |
51 rect = MakeRect(); | |
52 EXPECT_FALSE(rect.is_null()); | |
53 EXPECT_FALSE(!rect); | |
54 EXPECT_TRUE(rect); | |
55 | |
56 RectPtr null_rect = nullptr; | |
57 EXPECT_TRUE(null_rect.is_null()); | |
58 EXPECT_TRUE(!null_rect); | |
59 EXPECT_FALSE(null_rect); | |
60 | |
61 CheckRect(*rect); | |
62 } | |
63 | |
64 TEST_F(StructTest, Clone) { | |
65 NamedRegionPtr region; | |
66 | |
67 NamedRegionPtr clone_region = region.Clone(); | |
68 EXPECT_TRUE(clone_region.is_null()); | |
69 | |
70 region = NamedRegion::New(); | |
71 clone_region = region.Clone(); | |
72 EXPECT_TRUE(clone_region->name.is_null()); | |
73 EXPECT_TRUE(clone_region->rects.is_null()); | |
74 | |
75 region->name = "hello world"; | |
76 clone_region = region.Clone(); | |
77 EXPECT_EQ(region->name, clone_region->name); | |
78 | |
79 region->rects = Array<RectPtr>(2); | |
80 region->rects[1] = MakeRect(); | |
81 clone_region = region.Clone(); | |
82 EXPECT_EQ(2u, clone_region->rects.size()); | |
83 EXPECT_TRUE(clone_region->rects[0].is_null()); | |
84 CheckRect(*clone_region->rects[1]); | |
85 | |
86 // NoDefaultFieldValues contains handles, so Clone() is not available, but | |
87 // NoDefaultFieldValuesPtr should still compile. | |
88 NoDefaultFieldValuesPtr no_default_field_values(NoDefaultFieldValues::New()); | |
89 EXPECT_FALSE(no_default_field_values->f13.is_valid()); | |
90 } | |
91 | |
92 // Serialization test of a struct with no pointer or handle members. | |
93 TEST_F(StructTest, Serialization_Basic) { | |
94 RectPtr rect(MakeRect()); | |
95 | |
96 size_t size = GetSerializedSize_(rect); | |
97 EXPECT_EQ(8U + 16U, size); | |
98 | |
99 mojo::internal::FixedBuffer buf(size); | |
100 internal::Rect_Data* data; | |
101 Serialize_(rect.Pass(), &buf, &data); | |
102 | |
103 RectPtr rect2; | |
104 Deserialize_(data, &rect2); | |
105 | |
106 CheckRect(*rect2); | |
107 } | |
108 | |
109 // Construction of a struct with struct pointers from null. | |
110 TEST_F(StructTest, Construction_StructPointers) { | |
111 RectPairPtr pair; | |
112 EXPECT_TRUE(pair.is_null()); | |
113 | |
114 pair = RectPair::New(); | |
115 EXPECT_FALSE(pair.is_null()); | |
116 EXPECT_TRUE(pair->first.is_null()); | |
117 EXPECT_TRUE(pair->first.is_null()); | |
118 | |
119 pair = nullptr; | |
120 EXPECT_TRUE(pair.is_null()); | |
121 } | |
122 | |
123 // Serialization test of a struct with struct pointers. | |
124 TEST_F(StructTest, Serialization_StructPointers) { | |
125 RectPairPtr pair(RectPair::New()); | |
126 pair->first = MakeRect(); | |
127 pair->second = MakeRect(); | |
128 | |
129 size_t size = GetSerializedSize_(pair); | |
130 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), size); | |
131 | |
132 mojo::internal::FixedBuffer buf(size); | |
133 internal::RectPair_Data* data; | |
134 Serialize_(pair.Pass(), &buf, &data); | |
135 | |
136 RectPairPtr pair2; | |
137 Deserialize_(data, &pair2); | |
138 | |
139 CheckRect(*pair2->first); | |
140 CheckRect(*pair2->second); | |
141 } | |
142 | |
143 // Serialization test of a struct with an array member. | |
144 TEST_F(StructTest, Serialization_ArrayPointers) { | |
145 NamedRegionPtr region(NamedRegion::New()); | |
146 region->name = "region"; | |
147 region->rects = Array<RectPtr>::New(4); | |
148 for (size_t i = 0; i < region->rects.size(); ++i) | |
149 region->rects[i] = MakeRect(static_cast<int32_t>(i) + 1); | |
150 | |
151 size_t size = GetSerializedSize_(region); | |
152 EXPECT_EQ(8U + // header | |
153 8U + // name pointer | |
154 8U + // rects pointer | |
155 8U + // name header | |
156 8U + // name payload (rounded up) | |
157 8U + // rects header | |
158 4 * 8U + // rects payload (four pointers) | |
159 4 * (8U + // rect header | |
160 16U), // rect payload (four ints) | |
161 size); | |
162 | |
163 mojo::internal::FixedBuffer buf(size); | |
164 internal::NamedRegion_Data* data; | |
165 Serialize_(region.Pass(), &buf, &data); | |
166 | |
167 NamedRegionPtr region2; | |
168 Deserialize_(data, ®ion2); | |
169 | |
170 EXPECT_EQ(String("region"), region2->name); | |
171 | |
172 EXPECT_EQ(4U, region2->rects.size()); | |
173 for (size_t i = 0; i < region2->rects.size(); ++i) | |
174 CheckRect(*region2->rects[i], static_cast<int32_t>(i) + 1); | |
175 } | |
176 | |
177 // Serialization test of a struct with null array pointers. | |
178 TEST_F(StructTest, Serialization_NullArrayPointers) { | |
179 NamedRegionPtr region(NamedRegion::New()); | |
180 EXPECT_TRUE(region->name.is_null()); | |
181 EXPECT_TRUE(region->rects.is_null()); | |
182 | |
183 size_t size = GetSerializedSize_(region); | |
184 EXPECT_EQ(8U + // header | |
185 8U + // name pointer | |
186 8U, // rects pointer | |
187 size); | |
188 | |
189 mojo::internal::FixedBuffer buf(size); | |
190 internal::NamedRegion_Data* data; | |
191 Serialize_(region.Pass(), &buf, &data); | |
192 | |
193 NamedRegionPtr region2; | |
194 Deserialize_(data, ®ion2); | |
195 | |
196 EXPECT_TRUE(region2->name.is_null()); | |
197 EXPECT_TRUE(region2->rects.is_null()); | |
198 } | |
199 | |
200 } // namespace test | |
201 } // namespace mojo | |
OLD | NEW |