Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: mojo/public/cpp/bindings/tests/sample_service_unittest.cc

Issue 2136733002: Mojo C++ bindings: add a new mode to generator to use native STL/WTF types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@67_new
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <algorithm> 7 #include <algorithm>
8 #include <ostream> 8 #include <ostream>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 18 matching lines...) Expand all
29 namespace { 29 namespace {
30 30
31 // Set this variable to true to print the message in hex. 31 // Set this variable to true to print the message in hex.
32 bool g_dump_message_as_hex = false; 32 bool g_dump_message_as_hex = false;
33 33
34 // Set this variable to true to print the message in human readable form. 34 // Set this variable to true to print the message in human readable form.
35 bool g_dump_message_as_text = false; 35 bool g_dump_message_as_text = false;
36 36
37 // Make a sample |Foo|. 37 // Make a sample |Foo|.
38 FooPtr MakeFoo() { 38 FooPtr MakeFoo() {
39 mojo::String name("foopy"); 39 std::string name("foopy");
40 40
41 BarPtr bar(Bar::New()); 41 BarPtr bar(Bar::New());
42 bar->alpha = 20; 42 bar->alpha = 20;
43 bar->beta = 40; 43 bar->beta = 40;
44 bar->gamma = 60; 44 bar->gamma = 60;
45 bar->type = Bar::Type::VERTICAL; 45 bar->type = Bar::Type::VERTICAL;
46 46
47 mojo::Array<BarPtr> extra_bars(3); 47 std::vector<BarPtr> extra_bars(3);
48 for (size_t i = 0; i < extra_bars.size(); ++i) { 48 for (size_t i = 0; i < extra_bars.size(); ++i) {
49 Bar::Type type = i % 2 == 0 ? Bar::Type::VERTICAL : Bar::Type::HORIZONTAL; 49 Bar::Type type = i % 2 == 0 ? Bar::Type::VERTICAL : Bar::Type::HORIZONTAL;
50 BarPtr bar(Bar::New()); 50 BarPtr bar(Bar::New());
51 uint8_t base = static_cast<uint8_t>(i * 100); 51 uint8_t base = static_cast<uint8_t>(i * 100);
52 bar->alpha = base; 52 bar->alpha = base;
53 bar->beta = base + 20; 53 bar->beta = base + 20;
54 bar->gamma = base + 40; 54 bar->gamma = base + 40;
55 bar->type = type; 55 bar->type = type;
56 extra_bars[i] = std::move(bar); 56 extra_bars[i] = std::move(bar);
57 } 57 }
58 58
59 mojo::Array<uint8_t> data(10); 59 std::vector<uint8_t> data(10);
60 for (size_t i = 0; i < data.size(); ++i) 60 for (size_t i = 0; i < data.size(); ++i)
61 data[i] = static_cast<uint8_t>(data.size() - i); 61 data[i] = static_cast<uint8_t>(data.size() - i);
62 62
63 mojo::Array<mojo::ScopedDataPipeConsumerHandle> input_streams(2); 63 std::vector<mojo::ScopedDataPipeConsumerHandle> input_streams(2);
64 mojo::Array<mojo::ScopedDataPipeProducerHandle> output_streams(2); 64 std::vector<mojo::ScopedDataPipeProducerHandle> output_streams(2);
65 for (size_t i = 0; i < input_streams.size(); ++i) { 65 for (size_t i = 0; i < input_streams.size(); ++i) {
66 MojoCreateDataPipeOptions options; 66 MojoCreateDataPipeOptions options;
67 options.struct_size = sizeof(MojoCreateDataPipeOptions); 67 options.struct_size = sizeof(MojoCreateDataPipeOptions);
68 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; 68 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
69 options.element_num_bytes = 1; 69 options.element_num_bytes = 1;
70 options.capacity_num_bytes = 1024; 70 options.capacity_num_bytes = 1024;
71 mojo::ScopedDataPipeProducerHandle producer; 71 mojo::ScopedDataPipeProducerHandle producer;
72 mojo::ScopedDataPipeConsumerHandle consumer; 72 mojo::ScopedDataPipeConsumerHandle consumer;
73 mojo::CreateDataPipe(&options, &producer, &consumer); 73 mojo::CreateDataPipe(&options, &producer, &consumer);
74 input_streams[i] = std::move(consumer); 74 input_streams[i] = std::move(consumer);
75 output_streams[i] = std::move(producer); 75 output_streams[i] = std::move(producer);
76 } 76 }
77 77
78 mojo::Array<mojo::Array<bool>> array_of_array_of_bools(2); 78 std::vector<std::vector<bool>> array_of_array_of_bools(2);
79 for (size_t i = 0; i < 2; ++i) { 79 for (size_t i = 0; i < 2; ++i) {
80 mojo::Array<bool> array_of_bools(2); 80 std::vector<bool> array_of_bools(2);
81 for (size_t j = 0; j < 2; ++j) 81 for (size_t j = 0; j < 2; ++j)
82 array_of_bools[j] = j; 82 array_of_bools[j] = j;
83 array_of_array_of_bools[i] = std::move(array_of_bools); 83 array_of_array_of_bools[i] = std::move(array_of_bools);
84 } 84 }
85 85
86 mojo::MessagePipe pipe; 86 mojo::MessagePipe pipe;
87 FooPtr foo(Foo::New()); 87 FooPtr foo(Foo::New());
88 foo->name = name; 88 foo->name = name;
89 foo->x = 1; 89 foo->x = 1;
90 foo->y = 2; 90 foo->y = 2;
91 foo->a = false; 91 foo->a = false;
92 foo->b = true; 92 foo->b = true;
93 foo->c = false; 93 foo->c = false;
94 foo->bar = std::move(bar); 94 foo->bar = std::move(bar);
95 foo->extra_bars = std::move(extra_bars); 95 foo->extra_bars = std::move(extra_bars);
96 foo->data = std::move(data); 96 foo->data = std::move(data);
97 foo->source = std::move(pipe.handle1); 97 foo->source = std::move(pipe.handle1);
98 foo->input_streams = std::move(input_streams); 98 foo->input_streams = std::move(input_streams);
99 foo->output_streams = std::move(output_streams); 99 foo->output_streams = std::move(output_streams);
100 foo->array_of_array_of_bools = std::move(array_of_array_of_bools); 100 foo->array_of_array_of_bools = std::move(array_of_array_of_bools);
101 101
102 return foo; 102 return foo;
103 } 103 }
104 104
105 // Check that the given |Foo| is identical to the one made by |MakeFoo()|. 105 // Check that the given |Foo| is identical to the one made by |MakeFoo()|.
106 void CheckFoo(const Foo& foo) { 106 void CheckFoo(const Foo& foo) {
107 const std::string kName("foopy"); 107 const std::string kName("foopy");
108 ASSERT_FALSE(foo.name.is_null());
109 EXPECT_EQ(kName.size(), foo.name.size()); 108 EXPECT_EQ(kName.size(), foo.name.size());
110 for (size_t i = 0; i < std::min(kName.size(), foo.name.size()); i++) { 109 for (size_t i = 0; i < std::min(kName.size(), foo.name.size()); i++) {
111 // Test both |operator[]| and |at|. 110 // Test both |operator[]| and |at|.
112 EXPECT_EQ(kName[i], foo.name.at(i)) << i; 111 EXPECT_EQ(kName[i], foo.name.at(i)) << i;
113 EXPECT_EQ(kName[i], foo.name[i]) << i; 112 EXPECT_EQ(kName[i], foo.name[i]) << i;
114 } 113 }
115 EXPECT_EQ(kName, foo.name.get()); 114 EXPECT_EQ(kName, foo.name);
116 115
117 EXPECT_EQ(1, foo.x); 116 EXPECT_EQ(1, foo.x);
118 EXPECT_EQ(2, foo.y); 117 EXPECT_EQ(2, foo.y);
119 EXPECT_FALSE(foo.a); 118 EXPECT_FALSE(foo.a);
120 EXPECT_TRUE(foo.b); 119 EXPECT_TRUE(foo.b);
121 EXPECT_FALSE(foo.c); 120 EXPECT_FALSE(foo.c);
122 121
123 EXPECT_EQ(20, foo.bar->alpha); 122 EXPECT_EQ(20, foo.bar->alpha);
124 EXPECT_EQ(40, foo.bar->beta); 123 EXPECT_EQ(40, foo.bar->beta);
125 EXPECT_EQ(60, foo.bar->gamma); 124 EXPECT_EQ(60, foo.bar->gamma);
126 EXPECT_EQ(Bar::Type::VERTICAL, foo.bar->type); 125 EXPECT_EQ(Bar::Type::VERTICAL, foo.bar->type);
127 126
128 EXPECT_EQ(3u, foo.extra_bars.size()); 127 EXPECT_EQ(3u, foo.extra_bars->size());
129 for (size_t i = 0; i < foo.extra_bars.size(); i++) { 128 for (size_t i = 0; i < foo.extra_bars->size(); i++) {
130 uint8_t base = static_cast<uint8_t>(i * 100); 129 uint8_t base = static_cast<uint8_t>(i * 100);
131 Bar::Type type = i % 2 == 0 ? Bar::Type::VERTICAL : Bar::Type::HORIZONTAL; 130 Bar::Type type = i % 2 == 0 ? Bar::Type::VERTICAL : Bar::Type::HORIZONTAL;
132 EXPECT_EQ(base, foo.extra_bars[i]->alpha) << i; 131 EXPECT_EQ(base, (*foo.extra_bars)[i]->alpha) << i;
133 EXPECT_EQ(base + 20, foo.extra_bars[i]->beta) << i; 132 EXPECT_EQ(base + 20, (*foo.extra_bars)[i]->beta) << i;
134 EXPECT_EQ(base + 40, foo.extra_bars[i]->gamma) << i; 133 EXPECT_EQ(base + 40, (*foo.extra_bars)[i]->gamma) << i;
135 EXPECT_EQ(type, foo.extra_bars[i]->type) << i; 134 EXPECT_EQ(type, (*foo.extra_bars)[i]->type) << i;
136 } 135 }
137 136
138 EXPECT_EQ(10u, foo.data.size()); 137 EXPECT_EQ(10u, foo.data->size());
139 for (size_t i = 0; i < foo.data.size(); ++i) { 138 for (size_t i = 0; i < foo.data->size(); ++i) {
140 EXPECT_EQ(static_cast<uint8_t>(foo.data.size() - i), foo.data[i]) << i; 139 EXPECT_EQ(static_cast<uint8_t>(foo.data->size() - i), (*foo.data)[i]) << i;
141 } 140 }
142 141
143 EXPECT_FALSE(foo.input_streams.is_null()); 142 EXPECT_TRUE(foo.input_streams);
144 EXPECT_EQ(2u, foo.input_streams.size()); 143 EXPECT_EQ(2u, foo.input_streams->size());
145 144
146 EXPECT_FALSE(foo.output_streams.is_null()); 145 EXPECT_TRUE(foo.output_streams);
147 EXPECT_EQ(2u, foo.output_streams.size()); 146 EXPECT_EQ(2u, foo.output_streams->size());
148 147
149 EXPECT_EQ(2u, foo.array_of_array_of_bools.size()); 148 EXPECT_EQ(2u, foo.array_of_array_of_bools->size());
150 for (size_t i = 0; i < foo.array_of_array_of_bools.size(); ++i) { 149 for (size_t i = 0; i < foo.array_of_array_of_bools->size(); ++i) {
151 EXPECT_EQ(2u, foo.array_of_array_of_bools[i].size()); 150 EXPECT_EQ(2u, (*foo.array_of_array_of_bools)[i].size());
152 for (size_t j = 0; j < foo.array_of_array_of_bools[i].size(); ++j) { 151 for (size_t j = 0; j < (*foo.array_of_array_of_bools)[i].size(); ++j) {
153 EXPECT_EQ(bool(j), foo.array_of_array_of_bools[i][j]); 152 EXPECT_EQ(bool(j), (*foo.array_of_array_of_bools)[i][j]);
154 } 153 }
155 } 154 }
156 } 155 }
157 156
158 void PrintSpacer(int depth) { 157 void PrintSpacer(int depth) {
159 for (int i = 0; i < depth; ++i) 158 for (int i = 0; i < depth; ++i)
160 std::cout << " "; 159 std::cout << " ";
161 } 160 }
162 161
163 void Print(int depth, const char* name, bool value) { 162 void Print(int depth, const char* name, bool value) {
(...skipping 12 matching lines...) Expand all
176 } 175 }
177 176
178 template <typename H> 177 template <typename H>
179 void Print(int depth, 178 void Print(int depth,
180 const char* name, 179 const char* name,
181 const mojo::ScopedHandleBase<H>& value) { 180 const mojo::ScopedHandleBase<H>& value) {
182 PrintSpacer(depth); 181 PrintSpacer(depth);
183 std::cout << name << ": 0x" << std::hex << value.get().value() << std::endl; 182 std::cout << name << ": 0x" << std::hex << value.get().value() << std::endl;
184 } 183 }
185 184
186 void Print(int depth, const char* name, const mojo::String& str) { 185 void Print(int depth, const char* name, const std::string& str) {
187 PrintSpacer(depth); 186 PrintSpacer(depth);
188 std::cout << name << ": \"" << str.get() << "\"" << std::endl; 187 std::cout << name << ": \"" << str << "\"" << std::endl;
189 } 188 }
190 189
191 void Print(int depth, const char* name, const BarPtr& bar) { 190 void Print(int depth, const char* name, const BarPtr& bar) {
192 PrintSpacer(depth); 191 PrintSpacer(depth);
193 std::cout << name << ":" << std::endl; 192 std::cout << name << ":" << std::endl;
194 if (!bar.is_null()) { 193 if (!bar.is_null()) {
195 ++depth; 194 ++depth;
196 Print(depth, "alpha", bar->alpha); 195 Print(depth, "alpha", bar->alpha);
197 Print(depth, "beta", bar->beta); 196 Print(depth, "beta", bar->beta);
198 Print(depth, "gamma", bar->gamma); 197 Print(depth, "gamma", bar->gamma);
199 Print(depth, "packed", bar.To<int32_t>()); 198 Print(depth, "packed", bar.To<int32_t>());
200 --depth; 199 --depth;
201 } 200 }
202 } 201 }
203 202
204 template <typename T> 203 template <typename T>
205 void Print(int depth, const char* name, const mojo::Array<T>& array) { 204 void Print(int depth, const char* name, const std::vector<T>& array) {
206 PrintSpacer(depth); 205 PrintSpacer(depth);
207 std::cout << name << ":" << std::endl; 206 std::cout << name << ":" << std::endl;
208 if (!array.is_null()) { 207 ++depth;
209 ++depth; 208 for (size_t i = 0; i < array.size(); ++i) {
210 for (size_t i = 0; i < array.size(); ++i) { 209 std::stringstream buf;
211 std::stringstream buf; 210 buf << i;
212 buf << i; 211 Print(depth, buf.str().data(), array.at(i));
213 Print(depth, buf.str().data(), array.at(i));
214 }
215 --depth;
216 } 212 }
213 --depth;
214 }
215
216 template <typename T>
217 void Print(int depth,
218 const char* name,
219 const base::Optional<std::vector<T>>& array) {
220 if (array)
221 Print(depth, name, *array);
222 else
223 Print(depth, name, std::vector<T>());
217 } 224 }
218 225
219 void Print(int depth, const char* name, const FooPtr& foo) { 226 void Print(int depth, const char* name, const FooPtr& foo) {
220 PrintSpacer(depth); 227 PrintSpacer(depth);
221 std::cout << name << ":" << std::endl; 228 std::cout << name << ":" << std::endl;
222 if (!foo.is_null()) { 229 if (!foo.is_null()) {
223 ++depth; 230 ++depth;
224 Print(depth, "name", foo->name); 231 Print(depth, "name", foo->name);
225 Print(depth, "x", foo->x); 232 Print(depth, "x", foo->x);
226 Print(depth, "y", foo->y); 233 Print(depth, "y", foo->y);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 EXPECT_EQ(0x12345, defaults->a8); 358 EXPECT_EQ(0x12345, defaults->a8);
352 EXPECT_EQ(-0x12345, defaults->a9); 359 EXPECT_EQ(-0x12345, defaults->a9);
353 EXPECT_EQ(1234, defaults->a10); 360 EXPECT_EQ(1234, defaults->a10);
354 EXPECT_TRUE(defaults->a11); 361 EXPECT_TRUE(defaults->a11);
355 EXPECT_FALSE(defaults->a12); 362 EXPECT_FALSE(defaults->a12);
356 EXPECT_FLOAT_EQ(123.25f, defaults->a13); 363 EXPECT_FLOAT_EQ(123.25f, defaults->a13);
357 EXPECT_DOUBLE_EQ(1234567890.123, defaults->a14); 364 EXPECT_DOUBLE_EQ(1234567890.123, defaults->a14);
358 EXPECT_DOUBLE_EQ(1E10, defaults->a15); 365 EXPECT_DOUBLE_EQ(1E10, defaults->a15);
359 EXPECT_DOUBLE_EQ(-1.2E+20, defaults->a16); 366 EXPECT_DOUBLE_EQ(-1.2E+20, defaults->a16);
360 EXPECT_DOUBLE_EQ(1.23E-20, defaults->a17); 367 EXPECT_DOUBLE_EQ(1.23E-20, defaults->a17);
361 EXPECT_TRUE(defaults->a18.is_null()); 368 EXPECT_TRUE(defaults->a18.empty());
362 EXPECT_TRUE(defaults->a19.is_null()); 369 EXPECT_TRUE(defaults->a19.empty());
363 EXPECT_EQ(Bar::Type::BOTH, defaults->a20); 370 EXPECT_EQ(Bar::Type::BOTH, defaults->a20);
364 EXPECT_TRUE(defaults->a21.is_null()); 371 EXPECT_TRUE(defaults->a21.is_null());
365 ASSERT_FALSE(defaults->a22.is_null()); 372 ASSERT_FALSE(defaults->a22.is_null());
366 EXPECT_EQ(imported::Shape::RECTANGLE, defaults->a22->shape); 373 EXPECT_EQ(imported::Shape::RECTANGLE, defaults->a22->shape);
367 EXPECT_EQ(imported::Color::BLACK, defaults->a22->color); 374 EXPECT_EQ(imported::Color::BLACK, defaults->a22->color);
368 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, defaults->a23); 375 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, defaults->a23);
369 EXPECT_EQ(0x123456789, defaults->a24); 376 EXPECT_EQ(0x123456789, defaults->a24);
370 EXPECT_EQ(-0x123456789, defaults->a25); 377 EXPECT_EQ(-0x123456789, defaults->a25);
371 } 378 }
372 379
373 } // namespace 380 } // namespace
374 } // namespace sample 381 } // namespace sample
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698