- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / public / bindings / sample / sample_test.cc
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 <stdio.h>
6
7 #include <string>
8
9 #include "mojo/public/bindings/sample/generated/sample_bar.h"
10 #include "mojo/public/bindings/sample/generated/sample_foo.h"
11 #include "mojo/public/bindings/sample/generated/sample_service.h"
12 #include "mojo/public/bindings/sample/generated/sample_service_proxy.h"
13 #include "mojo/public/bindings/sample/generated/sample_service_stub.h"
14
15 namespace sample {
16
17 static void PrintSpacer(int depth) {
18   for (int i = 0; i < depth; ++i)
19     printf("   ");
20 }
21
22 static void Print(int depth, const char* name, bool value) {
23   PrintSpacer(depth);
24   printf("%s: %s\n", name, value ? "true" : "false");
25 }
26
27 static void Print(int depth, const char* name, int32_t value) {
28   PrintSpacer(depth);
29   printf("%s: %d\n", name, value);
30 }
31
32 static void Print(int depth, const char* name, uint8_t value) {
33   PrintSpacer(depth);
34   printf("%s: %u\n", name, value);
35 }
36
37 static void Print(int depth, const char* name, mojo::Handle value) {
38   PrintSpacer(depth);
39   printf("%s: 0x%x\n", name, value.value);
40 }
41
42 static void Print(int depth, const char* name, const mojo::String* str) {
43   PrintSpacer(depth);
44   printf("%s: \"%*s\"\n", name, static_cast<int>(str->size()), &str->at(0));
45 }
46
47 static void Print(int depth, const char* name, const Bar* bar) {
48   PrintSpacer(depth);
49   printf("%s: %p\n", name, bar);
50   if (bar) {
51     ++depth;
52     Print(depth, "alpha", bar->alpha());
53     Print(depth, "beta", bar->beta());
54     Print(depth, "gamma", bar->gamma());
55     --depth;
56   }
57 }
58
59 template <typename T>
60 static void Print(int depth, const char* name, const mojo::Array<T>* array) {
61   PrintSpacer(depth);
62   printf("%s: %p\n", name, array);
63   if (array) {
64     ++depth;
65     for (size_t i = 0; i < array->size(); ++i) {
66       char buf[32];
67       sprintf(buf, "%lu", static_cast<unsigned long>(i));
68       Print(depth, buf, array->at(i));
69     }
70     --depth;
71   }
72 }
73
74 static void Print(int depth, const char* name, const Foo* foo) {
75   PrintSpacer(depth);
76   printf("%s: %p\n", name, foo);
77   if (foo) {
78     ++depth;
79     Print(depth, "name", foo->name());
80     Print(depth, "x", foo->x());
81     Print(depth, "y", foo->y());
82     Print(depth, "a", foo->a());
83     Print(depth, "b", foo->b());
84     Print(depth, "c", foo->c());
85     Print(depth, "bar", foo->bar());
86     Print(depth, "extra_bars", foo->extra_bars());
87     Print(depth, "data", foo->data());
88     Print(depth, "files", foo->files());
89     --depth;
90   }
91 }
92
93 class ServiceImpl : public ServiceStub {
94  public:
95   virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port)
96       MOJO_OVERRIDE {
97     // Users code goes here to handle the incoming Frobinate message.
98     // We'll just dump the Foo structure and all of its members.
99
100     printf("Frobinate:\n");
101
102     int depth = 1;
103     Print(depth, "foo", foo);
104     Print(depth, "baz", baz);
105     Print(depth, "port", port);
106   }
107 };
108
109 class SimpleMessageReceiver : public mojo::MessageReceiver {
110  public:
111   virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE {
112     // Imagine some IPC happened here.
113
114     // In the receiving process, an implementation of ServiceStub is known to
115     // the system. It receives the incoming message.
116     ServiceImpl impl;
117
118     ServiceStub* stub = &impl;
119     return stub->Accept(message);
120   }
121 };
122
123 void Exercise() {
124   SimpleMessageReceiver receiver;
125
126   // User has a proxy to a Service somehow.
127   Service* service = new ServiceProxy(&receiver);
128
129   // User constructs a message to send.
130
131   // Notice that it doesn't matter in what order the structs / arrays are
132   // allocated. Here, the various members of Foo are allocated before Foo is
133   // allocated.
134
135   mojo::ScratchBuffer buf;
136
137   Bar* bar = Bar::New(&buf);
138   bar->set_alpha(20);
139   bar->set_beta(40);
140   bar->set_gamma(60);
141
142   const char kName[] = "foopy";
143   mojo::String* name = mojo::String::NewCopyOf(&buf, std::string(kName));
144
145   mojo::Array<Bar*>* extra_bars = mojo::Array<Bar*>::New(&buf, 3);
146   for (size_t i = 0; i < extra_bars->size(); ++i) {
147     Bar* bar = Bar::New(&buf);
148     uint8_t base = static_cast<uint8_t>(i * 100);
149     bar->set_alpha(base);
150     bar->set_beta(base + 20);
151     bar->set_gamma(base + 40);
152     (*extra_bars)[i] = bar;
153   }
154
155   mojo::Array<uint8_t>* data = mojo::Array<uint8_t>::New(&buf, 10);
156   for (size_t i = 0; i < data->size(); ++i)
157     (*data)[i] = static_cast<uint8_t>(data->size() - i);
158
159   mojo::Array<mojo::Handle>* files = mojo::Array<mojo::Handle>::New(&buf, 4);
160   for (size_t i = 0; i < files->size(); ++i)
161     (*files)[i].value = static_cast<MojoHandle>(0xFFFF - i);
162
163   Foo* foo = Foo::New(&buf);
164   foo->set_name(name);
165   foo->set_x(1);
166   foo->set_y(2);
167   foo->set_a(false);
168   foo->set_b(true);
169   foo->set_c(false);
170   foo->set_bar(bar);
171   foo->set_extra_bars(extra_bars);
172   foo->set_data(data);
173   foo->set_files(files);
174
175   mojo::Handle port = { 10 };
176
177   service->Frobinate(foo, true, port);
178 }
179
180 }  // namespace sample
181
182 int main() {
183   sample::Exercise();
184   return 0;
185 }