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 <algorithm>
6 #include <ostream>
7 #include <string>
8
9 #include "mojo/public/cpp/environment/environment.h"
10 #include "mojo/public/cpp/system/macros.h"
11 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace mojo {
15
16 template <>
17 struct TypeConverter<int32_t, sample::BarPtr> {
Convertmojo::TypeConverter18 static int32_t Convert(const sample::BarPtr& bar) {
19 return static_cast<int32_t>(bar->alpha) << 16 |
20 static_cast<int32_t>(bar->beta) << 8 |
21 static_cast<int32_t>(bar->gamma);
22 }
23 };
24
25 } // namespace mojo
26
27 namespace sample {
28 namespace {
29
30 // Set this variable to true to print the message in hex.
31 bool g_dump_message_as_hex = false;
32
33 // Set this variable to true to print the message in human readable form.
34 bool g_dump_message_as_text = false;
35
36 // Make a sample |Foo|.
MakeFoo()37 FooPtr MakeFoo() {
38 mojo::String name("foopy");
39
40 BarPtr bar(Bar::New());
41 bar->alpha = 20;
42 bar->beta = 40;
43 bar->gamma = 60;
44 bar->type = Bar::TYPE_VERTICAL;
45
46 mojo::Array<BarPtr> extra_bars(3);
47 for (size_t i = 0; i < extra_bars.size(); ++i) {
48 Bar::Type type = i % 2 == 0 ? Bar::TYPE_VERTICAL : Bar::TYPE_HORIZONTAL;
49 BarPtr bar(Bar::New());
50 uint8_t base = static_cast<uint8_t>(i * 100);
51 bar->alpha = base;
52 bar->beta = base + 20;
53 bar->gamma = base + 40;
54 bar->type = type;
55 extra_bars[i] = bar.Pass();
56 }
57
58 mojo::Array<uint8_t> data(10);
59 for (size_t i = 0; i < data.size(); ++i)
60 data[i] = static_cast<uint8_t>(data.size() - i);
61
62 mojo::Array<mojo::ScopedDataPipeConsumerHandle> input_streams(2);
63 mojo::Array<mojo::ScopedDataPipeProducerHandle> output_streams(2);
64 for (size_t i = 0; i < input_streams.size(); ++i) {
65 MojoCreateDataPipeOptions options;
66 options.struct_size = sizeof(MojoCreateDataPipeOptions);
67 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
68 options.element_num_bytes = 1;
69 options.capacity_num_bytes = 1024;
70 mojo::ScopedDataPipeProducerHandle producer;
71 mojo::ScopedDataPipeConsumerHandle consumer;
72 mojo::CreateDataPipe(&options, &producer, &consumer);
73 input_streams[i] = consumer.Pass();
74 output_streams[i] = producer.Pass();
75 }
76
77 mojo::Array<mojo::Array<bool> > array_of_array_of_bools(2);
78 for (size_t i = 0; i < 2; ++i) {
79 mojo::Array<bool> array_of_bools(2);
80 for (size_t j = 0; j < 2; ++j)
81 array_of_bools[j] = j;
82 array_of_array_of_bools[i] = array_of_bools.Pass();
83 }
84
85 mojo::MessagePipe pipe;
86 FooPtr foo(Foo::New());
87 foo->name = name;
88 foo->x = 1;
89 foo->y = 2;
90 foo->a = false;
91 foo->b = true;
92 foo->c = false;
93 foo->bar = bar.Pass();
94 foo->extra_bars = extra_bars.Pass();
95 foo->data = data.Pass();
96 foo->source = pipe.handle1.Pass();
97 foo->input_streams = input_streams.Pass();
98 foo->output_streams = output_streams.Pass();
99 foo->array_of_array_of_bools = array_of_array_of_bools.Pass();
100
101 return foo.Pass();
102 }
103
104 // Check that the given |Foo| is identical to the one made by |MakeFoo()|.
CheckFoo(const Foo & foo)105 void CheckFoo(const Foo& foo) {
106 const std::string kName("foopy");
107 ASSERT_FALSE(foo.name.is_null());
108 EXPECT_EQ(kName.size(), foo.name.size());
109 for (size_t i = 0; i < std::min(kName.size(), foo.name.size()); i++) {
110 // Test both |operator[]| and |at|.
111 EXPECT_EQ(kName[i], foo.name.at(i)) << i;
112 EXPECT_EQ(kName[i], foo.name[i]) << i;
113 }
114 EXPECT_EQ(kName, foo.name.get());
115
116 EXPECT_EQ(1, foo.x);
117 EXPECT_EQ(2, foo.y);
118 EXPECT_FALSE(foo.a);
119 EXPECT_TRUE(foo.b);
120 EXPECT_FALSE(foo.c);
121
122 EXPECT_EQ(20, foo.bar->alpha);
123 EXPECT_EQ(40, foo.bar->beta);
124 EXPECT_EQ(60, foo.bar->gamma);
125 EXPECT_EQ(Bar::TYPE_VERTICAL, foo.bar->type);
126
127 EXPECT_EQ(3u, foo.extra_bars.size());
128 for (size_t i = 0; i < foo.extra_bars.size(); i++) {
129 uint8_t base = static_cast<uint8_t>(i * 100);
130 Bar::Type type = i % 2 == 0 ? Bar::TYPE_VERTICAL : Bar::TYPE_HORIZONTAL;
131 EXPECT_EQ(base, foo.extra_bars[i]->alpha) << i;
132 EXPECT_EQ(base + 20, foo.extra_bars[i]->beta) << i;
133 EXPECT_EQ(base + 40, foo.extra_bars[i]->gamma) << i;
134 EXPECT_EQ(type, foo.extra_bars[i]->type) << i;
135 }
136
137 EXPECT_EQ(10u, foo.data.size());
138 for (size_t i = 0; i < foo.data.size(); ++i) {
139 EXPECT_EQ(static_cast<uint8_t>(foo.data.size() - i), foo.data[i]) << i;
140 }
141
142 EXPECT_FALSE(foo.input_streams.is_null());
143 EXPECT_EQ(2u, foo.input_streams.size());
144
145 EXPECT_FALSE(foo.output_streams.is_null());
146 EXPECT_EQ(2u, foo.output_streams.size());
147
148 EXPECT_EQ(2u, foo.array_of_array_of_bools.size());
149 for (size_t i = 0; i < foo.array_of_array_of_bools.size(); ++i) {
150 EXPECT_EQ(2u, foo.array_of_array_of_bools[i].size());
151 for (size_t j = 0; j < foo.array_of_array_of_bools[i].size(); ++j) {
152 EXPECT_EQ(bool(j), foo.array_of_array_of_bools[i][j]);
153 }
154 }
155 }
156
PrintSpacer(int depth)157 void PrintSpacer(int depth) {
158 for (int i = 0; i < depth; ++i)
159 std::cout << " ";
160 }
161
Print(int depth,const char * name,bool value)162 void Print(int depth, const char* name, bool value) {
163 PrintSpacer(depth);
164 std::cout << name << ": " << (value ? "true" : "false") << std::endl;
165 }
166
Print(int depth,const char * name,int32_t value)167 void Print(int depth, const char* name, int32_t value) {
168 PrintSpacer(depth);
169 std::cout << name << ": " << value << std::endl;
170 }
171
Print(int depth,const char * name,uint8_t value)172 void Print(int depth, const char* name, uint8_t value) {
173 PrintSpacer(depth);
174 std::cout << name << ": " << uint32_t(value) << std::endl;
175 }
176
177 template <typename H>
Print(int depth,const char * name,const mojo::ScopedHandleBase<H> & value)178 void Print(int depth, const char* name,
179 const mojo::ScopedHandleBase<H>& value) {
180 PrintSpacer(depth);
181 std::cout << name << ": 0x" << std::hex << value.get().value() << std::endl;
182 }
183
Print(int depth,const char * name,const mojo::String & str)184 void Print(int depth, const char* name, const mojo::String& str) {
185 PrintSpacer(depth);
186 std::cout << name << ": \"" << str.get() << "\"" << std::endl;
187 }
188
Print(int depth,const char * name,const BarPtr & bar)189 void Print(int depth, const char* name, const BarPtr& bar) {
190 PrintSpacer(depth);
191 std::cout << name << ":" << std::endl;
192 if (!bar.is_null()) {
193 ++depth;
194 Print(depth, "alpha", bar->alpha);
195 Print(depth, "beta", bar->beta);
196 Print(depth, "gamma", bar->gamma);
197 Print(depth, "packed", bar.To<int32_t>());
198 --depth;
199 }
200 }
201
202 template <typename T>
Print(int depth,const char * name,const mojo::Array<T> & array)203 void Print(int depth, const char* name, const mojo::Array<T>& array) {
204 PrintSpacer(depth);
205 std::cout << name << ":" << std::endl;
206 if (!array.is_null()) {
207 ++depth;
208 for (size_t i = 0; i < array.size(); ++i) {
209 std::stringstream buf;
210 buf << i;
211 Print(depth, buf.str().data(), array.at(i));
212 }
213 --depth;
214 }
215 }
216
Print(int depth,const char * name,const FooPtr & foo)217 void Print(int depth, const char* name, const FooPtr& foo) {
218 PrintSpacer(depth);
219 std::cout << name << ":" << std::endl;
220 if (!foo.is_null()) {
221 ++depth;
222 Print(depth, "name", foo->name);
223 Print(depth, "x", foo->x);
224 Print(depth, "y", foo->y);
225 Print(depth, "a", foo->a);
226 Print(depth, "b", foo->b);
227 Print(depth, "c", foo->c);
228 Print(depth, "bar", foo->bar);
229 Print(depth, "extra_bars", foo->extra_bars);
230 Print(depth, "data", foo->data);
231 Print(depth, "source", foo->source);
232 Print(depth, "input_streams", foo->input_streams);
233 Print(depth, "output_streams", foo->output_streams);
234 Print(depth, "array_of_array_of_bools", foo->array_of_array_of_bools);
235 --depth;
236 }
237 }
238
DumpHex(const uint8_t * bytes,uint32_t num_bytes)239 void DumpHex(const uint8_t* bytes, uint32_t num_bytes) {
240 for (uint32_t i = 0; i < num_bytes; ++i) {
241 std::cout << std::setw(2) << std::setfill('0') << std::hex <<
242 uint32_t(bytes[i]);
243
244 if (i % 16 == 15) {
245 std::cout << std::endl;
246 continue;
247 }
248
249 if (i % 2 == 1)
250 std::cout << " ";
251 if (i % 8 == 7)
252 std::cout << " ";
253 }
254 }
255
256 class ServiceImpl : public Service {
257 public:
Frobinate(FooPtr foo,BazOptions baz,PortPtr port)258 virtual void Frobinate(FooPtr foo, BazOptions baz, PortPtr port)
259 MOJO_OVERRIDE {
260 // Users code goes here to handle the incoming Frobinate message.
261
262 // We mainly check that we're given the expected arguments.
263 EXPECT_FALSE(foo.is_null());
264 if (!foo.is_null())
265 CheckFoo(*foo);
266 EXPECT_EQ(BAZ_OPTIONS_EXTRA, baz);
267
268 if (g_dump_message_as_text) {
269 // Also dump the Foo structure and all of its members.
270 std::cout << "Frobinate:" << std::endl;
271 int depth = 1;
272 Print(depth, "foo", foo);
273 Print(depth, "baz", baz);
274 Print(depth, "port", port.get());
275 }
276 }
277
GetPort(mojo::InterfaceRequest<Port> port_request)278 virtual void GetPort(mojo::InterfaceRequest<Port> port_request)
279 MOJO_OVERRIDE {
280 }
281 };
282
283 class ServiceProxyImpl : public ServiceProxy {
284 public:
ServiceProxyImpl(mojo::MessageReceiverWithResponder * receiver)285 explicit ServiceProxyImpl(mojo::MessageReceiverWithResponder* receiver)
286 : ServiceProxy(receiver) {
287 }
288 };
289
290 class SimpleMessageReceiver : public mojo::MessageReceiverWithResponder {
291 public:
Accept(mojo::Message * message)292 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE {
293 // Imagine some IPC happened here.
294
295 if (g_dump_message_as_hex) {
296 DumpHex(reinterpret_cast<const uint8_t*>(message->data()),
297 message->data_num_bytes());
298 }
299
300 // In the receiving process, an implementation of ServiceStub is known to
301 // the system. It receives the incoming message.
302 ServiceImpl impl;
303
304 ServiceStub stub;
305 stub.set_sink(&impl);
306 return stub.Accept(message);
307 }
308
AcceptWithResponder(mojo::Message * message,mojo::MessageReceiver * responder)309 virtual bool AcceptWithResponder(mojo::Message* message,
310 mojo::MessageReceiver* responder)
311 MOJO_OVERRIDE {
312 return false;
313 }
314 };
315
316 class BindingsSampleTest : public testing::Test {
317 public:
BindingsSampleTest()318 BindingsSampleTest() {}
~BindingsSampleTest()319 virtual ~BindingsSampleTest() {}
320
321 private:
322 mojo::Environment env_;
323
324 MOJO_DISALLOW_COPY_AND_ASSIGN(BindingsSampleTest);
325 };
326
TEST_F(BindingsSampleTest,Basic)327 TEST_F(BindingsSampleTest, Basic) {
328 SimpleMessageReceiver receiver;
329
330 // User has a proxy to a Service somehow.
331 Service* service = new ServiceProxyImpl(&receiver);
332
333 // User constructs a message to send.
334
335 // Notice that it doesn't matter in what order the structs / arrays are
336 // allocated. Here, the various members of Foo are allocated before Foo is
337 // allocated.
338
339 FooPtr foo = MakeFoo();
340 CheckFoo(*foo);
341
342 PortPtr port;
343 service->Frobinate(foo.Pass(), Service::BAZ_OPTIONS_EXTRA, port.Pass());
344
345 delete service;
346 }
347
TEST_F(BindingsSampleTest,DefaultValues)348 TEST_F(BindingsSampleTest, DefaultValues) {
349 DefaultsTestPtr defaults(DefaultsTest::New());
350 EXPECT_EQ(-12, defaults->a0);
351 EXPECT_EQ(kTwelve, defaults->a1);
352 EXPECT_EQ(1234, defaults->a2);
353 EXPECT_EQ(34567U, defaults->a3);
354 EXPECT_EQ(123456, defaults->a4);
355 EXPECT_EQ(3456789012U, defaults->a5);
356 EXPECT_EQ(-111111111111LL, defaults->a6);
357 EXPECT_EQ(9999999999999999999ULL, defaults->a7);
358 EXPECT_EQ(0x12345, defaults->a8);
359 EXPECT_EQ(-0x12345, defaults->a9);
360 EXPECT_EQ(1234, defaults->a10);
361 EXPECT_TRUE(defaults->a11);
362 EXPECT_FALSE(defaults->a12);
363 EXPECT_FLOAT_EQ(123.25f, defaults->a13);
364 EXPECT_DOUBLE_EQ(1234567890.123, defaults->a14);
365 EXPECT_DOUBLE_EQ(1E10, defaults->a15);
366 EXPECT_DOUBLE_EQ(-1.2E+20, defaults->a16);
367 EXPECT_DOUBLE_EQ(1.23E-20, defaults->a17);
368 EXPECT_TRUE(defaults->a18.is_null());
369 EXPECT_TRUE(defaults->a19.is_null());
370 EXPECT_EQ(Bar::TYPE_BOTH, defaults->a20);
371 EXPECT_TRUE(defaults->a21.is_null());
372 ASSERT_FALSE(defaults->a22.is_null());
373 EXPECT_EQ(imported::SHAPE_RECTANGLE, defaults->a22->shape);
374 EXPECT_EQ(imported::COLOR_BLACK, defaults->a22->color);
375 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, defaults->a23);
376 EXPECT_EQ(0x123456789, defaults->a24);
377 EXPECT_EQ(-0x123456789, defaults->a25);
378 }
379
380 } // namespace
381 } // namespace sample
382