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

Side by Side Diff: mojo/public/bindings/sample/sample_service_unittests.cc

Issue 134823005: Mojo: re-organize public tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « mojo/public/bindings/sample/sample_service.mojom ('k') | mojo/public/tests/DEPS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <algorithm>
6 #include <ostream>
7 #include <string>
8
9 #include "mojo/public/tests/simple_bindings_support.h"
10 #include "mojom/sample_service.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace mojo {
14
15 template <>
16 class TypeConverter<sample::Bar, int32_t> {
17 public:
18 static int32_t ConvertTo(const sample::Bar& 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
29 // Set this variable to true to print the binary message in hex.
30 bool g_dump_message_as_hex = true;
31
32 // Make a sample |Foo|.
33 Foo MakeFoo() {
34 mojo::String name("foopy");
35
36 Bar::Builder bar;
37 bar.set_alpha(20);
38 bar.set_beta(40);
39 bar.set_gamma(60);
40 bar.set_type(BAR_VERTICAL);
41
42 mojo::Array<Bar>::Builder extra_bars(3);
43 for (size_t i = 0; i < extra_bars.size(); ++i) {
44 BarType type = i % 2 == 0 ? BAR_VERTICAL : BAR_HORIZONTAL;
45 Bar::Builder bar;
46 uint8_t base = static_cast<uint8_t>(i * 100);
47 bar.set_alpha(base);
48 bar.set_beta(base + 20);
49 bar.set_gamma(base + 40);
50 bar.set_type(type);
51 extra_bars[i] = bar.Finish();
52 }
53
54 mojo::Array<uint8_t>::Builder data(10);
55 for (size_t i = 0; i < data.size(); ++i)
56 data[i] = static_cast<uint8_t>(data.size() - i);
57
58 mojo::Array<mojo::DataPipeConsumerHandle>::Builder input_streams(2);
59 mojo::Array<mojo::DataPipeProducerHandle>::Builder output_streams(2);
60 for (size_t i = 0; i < input_streams.size(); ++i) {
61 MojoCreateDataPipeOptions options;
62 options.struct_size = sizeof(MojoCreateDataPipeOptions);
63 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
64 options.element_num_bytes = 1;
65 options.capacity_num_bytes = 1024;
66 mojo::ScopedDataPipeProducerHandle producer;
67 mojo::ScopedDataPipeConsumerHandle consumer;
68 mojo::CreateDataPipe(&options, &producer, &consumer);
69 input_streams[i] = consumer.Pass();
70 output_streams[i] = producer.Pass();
71 }
72
73 mojo::ScopedMessagePipeHandle pipe0, pipe1;
74 mojo::CreateMessagePipe(&pipe0, &pipe1);
75
76 Foo::Builder foo;
77 foo.set_name(name);
78 foo.set_x(1);
79 foo.set_y(2);
80 foo.set_a(false);
81 foo.set_b(true);
82 foo.set_c(false);
83 foo.set_bar(bar.Finish());
84 foo.set_extra_bars(extra_bars.Finish());
85 foo.set_data(data.Finish());
86 foo.set_source(pipe1.Pass());
87 foo.set_input_streams(input_streams.Finish());
88 foo.set_output_streams(output_streams.Finish());
89
90 return foo.Finish();
91 }
92
93 // Check that the given |Foo| is identical to the one made by |MakeFoo()|.
94 void CheckFoo(const Foo& foo) {
95 const std::string kName("foopy");
96 ASSERT_FALSE(foo.name().is_null());
97 EXPECT_EQ(kName.size(), foo.name().size());
98 for (size_t i = 0; i < std::min(kName.size(), foo.name().size()); i++) {
99 // Test both |operator[]| and |at|.
100 EXPECT_EQ(kName[i], foo.name().at(i)) << i;
101 EXPECT_EQ(kName[i], foo.name()[i]) << i;
102 }
103 EXPECT_EQ(kName, foo.name().To<std::string>());
104
105 EXPECT_EQ(1, foo.x());
106 EXPECT_EQ(2, foo.y());
107 EXPECT_FALSE(foo.a());
108 EXPECT_TRUE(foo.b());
109 EXPECT_FALSE(foo.c());
110
111 EXPECT_EQ(20, foo.bar().alpha());
112 EXPECT_EQ(40, foo.bar().beta());
113 EXPECT_EQ(60, foo.bar().gamma());
114 EXPECT_EQ(BAR_VERTICAL, foo.bar().type());
115
116 EXPECT_EQ(3u, foo.extra_bars().size());
117 for (size_t i = 0; i < foo.extra_bars().size(); i++) {
118 uint8_t base = static_cast<uint8_t>(i * 100);
119 BarType type = i % 2 == 0 ? BAR_VERTICAL : BAR_HORIZONTAL;
120 EXPECT_EQ(base, foo.extra_bars()[i].alpha()) << i;
121 EXPECT_EQ(base + 20, foo.extra_bars()[i].beta()) << i;
122 EXPECT_EQ(base + 40, foo.extra_bars()[i].gamma()) << i;
123 EXPECT_EQ(type, foo.extra_bars()[i].type()) << i;
124 }
125
126 EXPECT_EQ(10u, foo.data().size());
127 for (size_t i = 0; i < foo.data().size(); ++i) {
128 EXPECT_EQ(static_cast<uint8_t>(foo.data().size() - i), foo.data()[i]) << i;
129 }
130
131 EXPECT_FALSE(foo.input_streams().is_null());
132 EXPECT_EQ(2u, foo.input_streams().size());
133
134 EXPECT_FALSE(foo.output_streams().is_null());
135 EXPECT_EQ(2u, foo.output_streams().size());
136 }
137
138 static void PrintSpacer(int depth) {
139 for (int i = 0; i < depth; ++i)
140 std::cout << " ";
141 }
142
143 static void Print(int depth, const char* name, bool value) {
144 PrintSpacer(depth);
145 std::cout << name << ": " << (value ? "true" : "false") << std::endl;
146 }
147
148 static void Print(int depth, const char* name, int32_t value) {
149 PrintSpacer(depth);
150 std::cout << name << ": " << value << std::endl;
151 }
152
153 static void Print(int depth, const char* name, uint8_t value) {
154 PrintSpacer(depth);
155 std::cout << name << ": " << uint32_t(value) << std::endl;
156 }
157
158 static void Print(int depth, const char* name, mojo::Handle value) {
159 PrintSpacer(depth);
160 std::cout << name << ": 0x" << std::hex << value.value() << std::endl;
161 }
162
163 static void Print(int depth, const char* name, const mojo::String& str) {
164 std::string s = str.To<std::string>();
165 PrintSpacer(depth);
166 std::cout << name << ": \"" << str.To<std::string>() << "\"" << std::endl;
167 }
168
169 static void Print(int depth, const char* name, const Bar& bar) {
170 PrintSpacer(depth);
171 std::cout << name << ":" << std::endl;
172 if (!bar.is_null()) {
173 ++depth;
174 Print(depth, "alpha", bar.alpha());
175 Print(depth, "beta", bar.beta());
176 Print(depth, "gamma", bar.gamma());
177 Print(depth, "packed", bar.To<int32_t>());
178 --depth;
179 }
180 }
181
182 template <typename T>
183 static void Print(int depth, const char* name,
184 const mojo::Passable<T>& passable) {
185 Print(depth, name, passable.get());
186 }
187
188 template <typename T>
189 static void Print(int depth, const char* name, const mojo::Array<T>& array) {
190 PrintSpacer(depth);
191 std::cout << name << ":" << std::endl;
192 if (!array.is_null()) {
193 ++depth;
194 for (size_t i = 0; i < array.size(); ++i) {
195 std::stringstream buf;
196 buf << i;
197 Print(depth, buf.str().data(), array.at(i));
198 }
199 --depth;
200 }
201 }
202
203 static void Print(int depth, const char* name, const Foo& foo) {
204 PrintSpacer(depth);
205 std::cout << name << ":" << std::endl;
206 if (!foo.is_null()) {
207 ++depth;
208 Print(depth, "name", foo.name());
209 Print(depth, "x", foo.x());
210 Print(depth, "y", foo.y());
211 Print(depth, "a", foo.a());
212 Print(depth, "b", foo.b());
213 Print(depth, "c", foo.c());
214 Print(depth, "bar", foo.bar());
215 Print(depth, "extra_bars", foo.extra_bars());
216 Print(depth, "data", foo.data());
217 Print(depth, "source", foo.source().get());
218 Print(depth, "input_streams", foo.input_streams());
219 Print(depth, "output_streams", foo.output_streams());
220 --depth;
221 }
222 }
223
224 static void DumpHex(const uint8_t* bytes, uint32_t num_bytes) {
225 for (uint32_t i = 0; i < num_bytes; ++i) {
226 std::cout << std::setw(2) << std::setfill('0') << std::hex <<
227 uint32_t(bytes[i]);
228
229 if (i % 16 == 15) {
230 std::cout << std::endl;
231 continue;
232 }
233
234 if (i % 2 == 1)
235 std::cout << " ";
236 if (i % 8 == 7)
237 std::cout << " ";
238 }
239 }
240
241 class ServiceImpl : public Service {
242 public:
243 virtual void Frobinate(const Foo& foo, int32_t baz,
244 mojo::ScopedMessagePipeHandle port)
245 MOJO_OVERRIDE {
246 // Users code goes here to handle the incoming Frobinate message.
247
248 // We mainly check that we're given the expected arguments.
249 CheckFoo(foo);
250 EXPECT_EQ(BAZ_EXTRA, baz);
251
252 // Also dump the Foo structure and all of its members.
253 // TODO(vtl): Make it optional, so that the test spews less?
254 std::cout << "Frobinate:" << std::endl;
255 int depth = 1;
256 Print(depth, "foo", foo);
257 Print(depth, "baz", baz);
258 Print(depth, "port", port.get());
259 }
260 };
261
262 class SimpleMessageReceiver : public mojo::MessageReceiver {
263 public:
264 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE {
265 // Imagine some IPC happened here.
266
267 if (g_dump_message_as_hex) {
268 DumpHex(reinterpret_cast<const uint8_t*>(message->data),
269 message->data->header.num_bytes);
270 }
271
272 // In the receiving process, an implementation of ServiceStub is known to
273 // the system. It receives the incoming message.
274 ServiceImpl impl;
275
276 ServiceStub stub(&impl);
277 return stub.Accept(message);
278 }
279 };
280
281 TEST(BindingsSampleTest, Basic) {
282 mojo::test::SimpleBindingsSupport bindings_support;
283 SimpleMessageReceiver receiver;
284
285 // User has a proxy to a Service somehow.
286 Service* service = new ServiceProxy(&receiver);
287
288 // User constructs a message to send.
289
290 // Notice that it doesn't matter in what order the structs / arrays are
291 // allocated. Here, the various members of Foo are allocated before Foo is
292 // allocated.
293
294 mojo::AllocationScope scope;
295
296 Foo foo = MakeFoo();
297 CheckFoo(foo);
298
299 mojo::ScopedMessagePipeHandle port0, port1;
300 mojo::CreateMessagePipe(&port0, &port1);
301
302 service->Frobinate(foo, Service::BAZ_EXTRA, port0.Pass());
303 }
304
305 } // namespace sample
OLDNEW
« no previous file with comments | « mojo/public/bindings/sample/sample_service.mojom ('k') | mojo/public/tests/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698