Modify the droute tests to use the new introspection scheme.
[platform/core/uifw/at-spi2-atk.git] / droute / droute-test.c
1 #include <stdio.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <droute/droute.h>
5 #include <dbind/dbind.h>
6
7 #include "dbus/dbus-glib-lowlevel.h"
8
9 #define TEST_OBJECT_PATH    "/test/object"
10 #define TEST_INTERFACE_ONE  "test.interface.One"
11 #define TEST_INTERFACE_TWO  "test.interface.Two"
12
13 #define OBJECT_ONE "ObjectOne";
14 #define OBJECT_TWO "ObjectTwo";
15
16 #define STRING_ONE "StringOne"
17 #define STRING_TWO "StringTwo"
18
19 #define INT_ONE 0
20 #define INT_TWO 456
21
22 #define NONE_REPLY_STRING "NoneMethod"
23
24 const gchar *test_interface_One = \
25 "<interface name=\"test.interface.One\">"
26 "  <method name=\"null\"/>"
27 "  <method name=\"getInt\">"
28 "    <arg direction=\"out\" type=\"o\"/>"
29 "  </method>"
30 "  <method name=\"setInt\">"
31 "    <arg direction=\"in\" type=\"o\"/>"
32 "  </method>"
33 "  <method name=\"getString\">"
34 "    <arg direction=\"out\" type=\"s\"/>"
35 "  </method>"
36 "  <method name=\"setString\">"
37 "    <arg direction=\"in\" type=\"s\"/>"
38 "  </method>"
39 "</interface>";
40
41 const gchar *test_interface_Two = \
42 "<interface name=\"test.interface.One\">"
43 "  <method name=\"null\"/>"
44 "  <method name=\"getInt\">"
45 "    <arg direction=\"out\" type=\"o\"/>"
46 "  </method>"
47 "  <method name=\"setInt\">"
48 "    <arg direction=\"in\" type=\"o\"/>"
49 "  </method>"
50 "  <method name=\"getString\">"
51 "    <arg direction=\"out\" type=\"s\"/>"
52 "  </method>"
53 "  <method name=\"setString\">"
54 "    <arg direction=\"in\" type=\"s\"/>"
55 "  </method>"
56 "</interface>";
57
58 typedef struct _AnObject
59 {
60     gchar *astring;
61     guint *anint;
62 } AnObject;
63
64 static DBusConnection *bus;
65 static GMainLoop      *main_loop;
66 static gboolean       success = TRUE;
67
68 static DBusMessage *
69 impl_null (DBusConnection *bus, DBusMessage *message, void *user_data)
70 {
71     DBusMessage *reply;
72
73     reply = dbus_message_new_method_return (message);
74     return reply;
75 }
76
77 static DBusMessage *
78 impl_getInt (DBusConnection *bus, DBusMessage *message, void *user_data)
79 {
80     AnObject    *object = (AnObject *) user_data;
81     DBusMessage *reply;
82     DBusError    error;
83
84     dbus_error_init (&error);
85
86     reply = dbus_message_new_method_return (message);
87     dbus_message_append_args (reply, DBUS_TYPE_INT32, &(object->anint), DBUS_TYPE_INVALID);
88     return reply;
89 }
90
91 static DBusMessage *
92 impl_setInt (DBusConnection *bus, DBusMessage *message, void *user_data)
93 {
94     AnObject    *object = (AnObject *) user_data;
95     DBusMessage *reply;
96     DBusError    error;
97
98     dbus_error_init (&error);
99
100     dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &(object->anint), DBUS_TYPE_INVALID);
101
102     reply = dbus_message_new_method_return (message);
103     return reply;
104 }
105
106 static DBusMessage *
107 impl_getString (DBusConnection *bus, DBusMessage *message, void *user_data)
108 {
109     AnObject    *object = (AnObject *) user_data;
110     DBusMessage *reply;
111     DBusError    error;
112
113     dbus_error_init (&error);
114
115     reply = dbus_message_new_method_return (message);
116     dbus_message_append_args (reply, DBUS_TYPE_STRING, &(object->astring), DBUS_TYPE_INVALID);
117     return reply;
118 }
119
120 static DBusMessage *
121 impl_setString (DBusConnection *bus, DBusMessage *message, void *user_data)
122 {
123     AnObject    *object = (AnObject *) user_data;
124     DBusMessage *reply;
125     DBusError    error;
126
127     dbus_error_init (&error);
128
129     g_free (object->astring);
130     dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &(object->astring), DBUS_TYPE_INVALID);
131
132     reply = dbus_message_new_method_return (message);
133     return reply;
134 }
135
136 static DBusMessage *
137 impl_getInterfaceOne (DBusConnection *bus, DBusMessage *message, void *user_data)
138 {
139     DBusMessage *reply;
140     DBusError    error;
141     gchar       *itf = TEST_INTERFACE_ONE;
142
143     dbus_error_init (&error);
144
145     reply = dbus_message_new_method_return (message);
146     dbus_message_append_args (reply, DBUS_TYPE_STRING, &itf, DBUS_TYPE_INVALID);
147     return reply;
148 }
149
150 static DBusMessage *
151 impl_getInterfaceTwo (DBusConnection *bus, DBusMessage *message, void *user_data)
152 {
153     DBusMessage *reply;
154     DBusError    error;
155     gchar       *itf = TEST_INTERFACE_TWO;
156
157     dbus_error_init (&error);
158
159     reply = dbus_message_new_method_return (message);
160     dbus_message_append_args (reply, DBUS_TYPE_STRING, &itf, DBUS_TYPE_INVALID);
161     return reply;
162 }
163
164 static DRouteMethod test_methods_one[] = {
165     {impl_null,            "null"},
166     {impl_getInt,          "getInt"},
167     {impl_setInt,          "setInt"},
168     {impl_getString,       "getString"},
169     {impl_setString,       "setString"},
170     {impl_getInterfaceOne, "getInterfaceOne"},
171     {NULL, NULL}
172 };
173
174 static DRouteMethod test_methods_two[] = {
175     {impl_null,            "null"},
176     {impl_getInt,          "getInt"},
177     {impl_setInt,          "setInt"},
178     {impl_getString,       "getString"},
179     {impl_setString,       "setString"},
180     {impl_getInterfaceTwo, "getInterfaceTwo"},
181     {NULL, NULL}
182 };
183
184 static DRouteProperty test_properties[] = {
185     {NULL, NULL, NULL}
186 };
187
188 gboolean
189 do_tests_func (gpointer data)
190 {
191     DBusError    error;
192     const gchar *bus_name;
193
194     gchar     *expected_string;
195     gchar     *result_string;
196
197     dbus_error_init (&error);
198     bus_name = dbus_bus_get_unique_name (bus);
199
200     /* --------------------------------------------------------*/
201
202     dbind_method_call_reentrant (bus,
203                                  bus_name,
204                                  TEST_OBJECT_PATH,
205                                  TEST_INTERFACE_ONE,
206                                  "null",
207                                  NULL,
208                                  "");
209
210     /* --------------------------------------------------------*/
211
212     expected_string = TEST_INTERFACE_ONE;
213     result_string = NULL;
214     dbind_method_call_reentrant (bus,
215                                  bus_name,
216                                  TEST_OBJECT_PATH,
217                                  TEST_INTERFACE_ONE,
218                                  "getInterfaceOne",
219                                  NULL,
220                                  "=>s",
221                                  &result_string);
222     if (g_strcmp0(expected_string, result_string))
223     {
224             g_print ("Failed: reply to getInterfaceOne not as expected\n");
225             goto out;
226     }
227
228     /* --------------------------------------------------------*/
229
230 out:
231     g_main_loop_quit (main_loop);
232     return FALSE;
233 }
234
235
236 int main (int argc, char **argv)
237 {
238     DRouteContext  *cnx;
239     DRoutePath     *path;
240     AnObject       *object;
241     DBusError       error;
242
243     /* Setup some server object */
244
245     object = g_new0(AnObject, 1);
246     object->astring = g_strdup (STRING_ONE);
247     object->anint = INT_ONE;
248
249     dbus_error_init (&error);
250     main_loop = g_main_loop_new(NULL, FALSE);
251     bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
252     dbus_connection_setup_with_g_main(bus, g_main_context_default());
253
254     cnx = droute_new (bus);
255     path = droute_add_one (cnx, TEST_OBJECT_PATH, object);
256
257     droute_path_add_interface (path,
258                                TEST_INTERFACE_ONE,
259                                test_interface_One,
260                                test_methods_one,
261                                test_properties);
262
263     droute_path_add_interface (path,
264                                TEST_INTERFACE_TWO,
265                                test_interface_Two,
266                                test_methods_two,
267                                test_properties);
268
269     g_idle_add (do_tests_func, NULL);
270     g_main_run(main_loop);
271     if (success)
272             return 0;
273     else
274             return 1;
275 }