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.
9 #include "mojo/public/bindings/allocation_scope.h"
10 #include "mojo/public/environment/environment.h"
11 #include "mojom/sample_service.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 class TypeConverter<sample::Bar, int32_t> {
19 static int32_t ConvertTo(const sample::Bar& bar) {
20 return static_cast<int32_t>(bar.alpha()) << 16 |
21 static_cast<int32_t>(bar.beta()) << 8 |
22 static_cast<int32_t>(bar.gamma());
31 // Set this variable to true to print the message in hex.
32 bool g_dump_message_as_hex = false;
34 // Set this variable to true to print the message in human readable form.
35 bool g_dump_message_as_text = false;
37 // Make a sample |Foo|.
39 mojo::String name("foopy");
45 bar.set_type(BAR_VERTICAL);
47 mojo::Array<Bar>::Builder extra_bars(3);
48 for (size_t i = 0; i < extra_bars.size(); ++i) {
49 BarType type = i % 2 == 0 ? BAR_VERTICAL : BAR_HORIZONTAL;
51 uint8_t base = static_cast<uint8_t>(i * 100);
53 bar.set_beta(base + 20);
54 bar.set_gamma(base + 40);
56 extra_bars[i] = bar.Finish();
59 mojo::Array<uint8_t>::Builder data(10);
60 for (size_t i = 0; i < data.size(); ++i)
61 data[i] = static_cast<uint8_t>(data.size() - i);
63 mojo::Array<mojo::DataPipeConsumerHandle>::Builder input_streams(2);
64 mojo::Array<mojo::DataPipeProducerHandle>::Builder output_streams(2);
65 for (size_t i = 0; i < input_streams.size(); ++i) {
66 MojoCreateDataPipeOptions options;
67 options.struct_size = sizeof(MojoCreateDataPipeOptions);
68 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
69 options.element_num_bytes = 1;
70 options.capacity_num_bytes = 1024;
71 mojo::ScopedDataPipeProducerHandle producer;
72 mojo::ScopedDataPipeConsumerHandle consumer;
73 mojo::CreateDataPipe(&options, &producer, &consumer);
74 input_streams[i] = consumer.Pass();
75 output_streams[i] = producer.Pass();
78 mojo::ScopedMessagePipeHandle pipe0, pipe1;
79 mojo::CreateMessagePipe(&pipe0, &pipe1);
88 foo.set_bar(bar.Finish());
89 foo.set_extra_bars(extra_bars.Finish());
90 foo.set_data(data.Finish());
91 foo.set_source(pipe1.Pass());
92 foo.set_input_streams(input_streams.Finish());
93 foo.set_output_streams(output_streams.Finish());
98 // Check that the given |Foo| is identical to the one made by |MakeFoo()|.
99 void CheckFoo(const Foo& foo) {
100 const std::string kName("foopy");
101 ASSERT_FALSE(foo.name().is_null());
102 EXPECT_EQ(kName.size(), foo.name().size());
103 for (size_t i = 0; i < std::min(kName.size(), foo.name().size()); i++) {
104 // Test both |operator[]| and |at|.
105 EXPECT_EQ(kName[i], foo.name().at(i)) << i;
106 EXPECT_EQ(kName[i], foo.name()[i]) << i;
108 EXPECT_EQ(kName, foo.name().To<std::string>());
110 EXPECT_EQ(1, foo.x());
111 EXPECT_EQ(2, foo.y());
112 EXPECT_FALSE(foo.a());
113 EXPECT_TRUE(foo.b());
114 EXPECT_FALSE(foo.c());
116 EXPECT_EQ(20, foo.bar().alpha());
117 EXPECT_EQ(40, foo.bar().beta());
118 EXPECT_EQ(60, foo.bar().gamma());
119 EXPECT_EQ(BAR_VERTICAL, foo.bar().type());
121 EXPECT_EQ(3u, foo.extra_bars().size());
122 for (size_t i = 0; i < foo.extra_bars().size(); i++) {
123 uint8_t base = static_cast<uint8_t>(i * 100);
124 BarType type = i % 2 == 0 ? BAR_VERTICAL : BAR_HORIZONTAL;
125 EXPECT_EQ(base, foo.extra_bars()[i].alpha()) << i;
126 EXPECT_EQ(base + 20, foo.extra_bars()[i].beta()) << i;
127 EXPECT_EQ(base + 40, foo.extra_bars()[i].gamma()) << i;
128 EXPECT_EQ(type, foo.extra_bars()[i].type()) << i;
131 EXPECT_EQ(10u, foo.data().size());
132 for (size_t i = 0; i < foo.data().size(); ++i) {
133 EXPECT_EQ(static_cast<uint8_t>(foo.data().size() - i), foo.data()[i]) << i;
136 EXPECT_FALSE(foo.input_streams().is_null());
137 EXPECT_EQ(2u, foo.input_streams().size());
139 EXPECT_FALSE(foo.output_streams().is_null());
140 EXPECT_EQ(2u, foo.output_streams().size());
143 void PrintSpacer(int depth) {
144 for (int i = 0; i < depth; ++i)
148 void Print(int depth, const char* name, bool value) {
150 std::cout << name << ": " << (value ? "true" : "false") << std::endl;
153 void Print(int depth, const char* name, int32_t value) {
155 std::cout << name << ": " << value << std::endl;
158 void Print(int depth, const char* name, uint8_t value) {
160 std::cout << name << ": " << uint32_t(value) << std::endl;
163 void Print(int depth, const char* name, mojo::Handle value) {
165 std::cout << name << ": 0x" << std::hex << value.value() << std::endl;
168 void Print(int depth, const char* name, const mojo::String& str) {
169 std::string s = str.To<std::string>();
171 std::cout << name << ": \"" << str.To<std::string>() << "\"" << std::endl;
174 void Print(int depth, const char* name, const Bar& bar) {
176 std::cout << name << ":" << std::endl;
177 if (!bar.is_null()) {
179 Print(depth, "alpha", bar.alpha());
180 Print(depth, "beta", bar.beta());
181 Print(depth, "gamma", bar.gamma());
182 Print(depth, "packed", bar.To<int32_t>());
187 template <typename T>
188 void Print(int depth, const char* name,
189 const mojo::Passable<T>& passable) {
190 Print(depth, name, passable.get());
193 template <typename T>
194 void Print(int depth, const char* name, const mojo::Array<T>& array) {
196 std::cout << name << ":" << std::endl;
197 if (!array.is_null()) {
199 for (size_t i = 0; i < array.size(); ++i) {
200 std::stringstream buf;
202 Print(depth, buf.str().data(), array.at(i));
208 void Print(int depth, const char* name, const Foo& foo) {
210 std::cout << name << ":" << std::endl;
211 if (!foo.is_null()) {
213 Print(depth, "name", foo.name());
214 Print(depth, "x", foo.x());
215 Print(depth, "y", foo.y());
216 Print(depth, "a", foo.a());
217 Print(depth, "b", foo.b());
218 Print(depth, "c", foo.c());
219 Print(depth, "bar", foo.bar());
220 Print(depth, "extra_bars", foo.extra_bars());
221 Print(depth, "data", foo.data());
222 Print(depth, "source", foo.source().get());
223 Print(depth, "input_streams", foo.input_streams());
224 Print(depth, "output_streams", foo.output_streams());
229 void DumpHex(const uint8_t* bytes, uint32_t num_bytes) {
230 for (uint32_t i = 0; i < num_bytes; ++i) {
231 std::cout << std::setw(2) << std::setfill('0') << std::hex <<
235 std::cout << std::endl;
246 class ServiceImpl : public Service {
248 virtual void Frobinate(const Foo& foo, int32_t baz, ScopedPortHandle port)
250 // Users code goes here to handle the incoming Frobinate message.
252 // We mainly check that we're given the expected arguments.
254 EXPECT_EQ(BAZ_EXTRA, baz);
256 if (g_dump_message_as_text) {
257 // Also dump the Foo structure and all of its members.
258 std::cout << "Frobinate:" << std::endl;
260 Print(depth, "foo", foo);
261 Print(depth, "baz", baz);
262 Print(depth, "port", port.get());
267 class SimpleMessageReceiver : public mojo::MessageReceiver {
269 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE {
270 // Imagine some IPC happened here.
272 if (g_dump_message_as_hex) {
273 DumpHex(reinterpret_cast<const uint8_t*>(message->data),
274 message->data->header.num_bytes);
277 // In the receiving process, an implementation of ServiceStub is known to
278 // the system. It receives the incoming message.
281 ServiceStub stub(&impl);
282 return stub.Accept(message);
288 TEST(BindingsSampleTest, Basic) {
289 mojo::Environment env;
290 SimpleMessageReceiver receiver;
292 // User has a proxy to a Service somehow.
293 Service* service = new ServiceProxy(&receiver);
295 // User constructs a message to send.
297 // Notice that it doesn't matter in what order the structs / arrays are
298 // allocated. Here, the various members of Foo are allocated before Foo is
301 mojo::AllocationScope scope;
306 mojo::InterfacePipe<Port, mojo::AnyInterface> pipe;
307 service->Frobinate(foo, Service::BAZ_EXTRA, pipe.handle_to_self.Pass());
310 TEST(BindingsSampleTest, DefaultValues) {
311 mojo::Environment env;
312 SimpleMessageReceiver receiver;
313 mojo::AllocationScope scope;
315 Bar bar = Bar::Builder().Finish();
316 EXPECT_EQ(255, bar.alpha());
318 Foo foo = Foo::Builder().Finish();
319 ASSERT_FALSE(foo.name().is_null());
320 EXPECT_EQ("Fooby", foo.name().To<std::string>());
321 EXPECT_TRUE(foo.a());
322 EXPECT_EQ(3u, foo.data().size());
323 EXPECT_EQ(1, foo.data()[0]);
324 EXPECT_EQ(2, foo.data()[1]);
325 EXPECT_EQ(3, foo.data()[2]);
327 DefaultsTestInner inner = DefaultsTestInner::Builder().Finish();
328 EXPECT_EQ(1u, inner.names().size());
329 EXPECT_EQ("Jim", inner.names()[0].To<std::string>());
330 EXPECT_EQ(6*12, inner.height());
332 DefaultsTest full = DefaultsTest::Builder().Finish();
333 EXPECT_EQ(1u, full.people().size());
334 EXPECT_EQ(32, full.people()[0].age());
335 EXPECT_EQ(2u, full.people()[0].names().size());
336 EXPECT_EQ("Bob", full.people()[0].names()[0].To<std::string>());
337 EXPECT_EQ("Bobby", full.people()[0].names()[1].To<std::string>());
338 EXPECT_EQ(6*12, full.people()[0].height());
340 EXPECT_EQ(7, full.point().x());
341 EXPECT_EQ(15, full.point().y());
343 EXPECT_EQ(1u, full.shape_masks().size());
344 EXPECT_EQ(1 << imported::SHAPE_RECTANGLE, full.shape_masks()[0]);
346 EXPECT_EQ(imported::SHAPE_CIRCLE, full.thing().shape());
347 EXPECT_EQ(imported::COLOR_BLACK, full.thing().color());
350 } // namespace sample