Merge branch 'master' into mgorse
[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 #if !defined TEST_INTROSPECTION_DIRECTORY
17     #error "No introspection XML directory defined"
18 #endif
19
20 #define STRING_ONE "StringOne"
21 #define STRING_TWO "StringTwo"
22
23 #define INT_ONE 0
24 #define INT_TWO 456
25
26 #define NONE_REPLY_STRING "NoneMethod"
27
28 typedef struct _AnObject
29 {
30     gchar *astring;
31     guint *anint;
32 } AnObject;
33
34 static DBusConnection *bus;
35 static GMainLoop      *main_loop;
36 static gboolean       success = TRUE;
37
38 static DBusMessage *
39 impl_null (DBusConnection *bus, DBusMessage *message, void *user_data)
40 {
41     DBusMessage *reply;
42
43     reply = dbus_message_new_method_return (message);
44     return reply;
45 }
46
47 static DBusMessage *
48 impl_getInt (DBusConnection *bus, DBusMessage *message, void *user_data)
49 {
50     AnObject    *object = (AnObject *) user_data;
51     DBusMessage *reply;
52     DBusError    error;
53
54     dbus_error_init (&error);
55
56     reply = dbus_message_new_method_return (message);
57     dbus_message_append_args (reply, DBUS_TYPE_INT32, &(object->anint), DBUS_TYPE_INVALID);
58     return reply;
59 }
60
61 static DBusMessage *
62 impl_setInt (DBusConnection *bus, DBusMessage *message, void *user_data)
63 {
64     AnObject    *object = (AnObject *) user_data;
65     DBusMessage *reply;
66     DBusError    error;
67
68     dbus_error_init (&error);
69
70     dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &(object->anint), DBUS_TYPE_INVALID);
71
72     reply = dbus_message_new_method_return (message);
73     return reply;
74 }
75
76 static DBusMessage *
77 impl_getString (DBusConnection *bus, DBusMessage *message, void *user_data)
78 {
79     AnObject    *object = (AnObject *) user_data;
80     DBusMessage *reply;
81     DBusError    error;
82
83     dbus_error_init (&error);
84
85     reply = dbus_message_new_method_return (message);
86     dbus_message_append_args (reply, DBUS_TYPE_STRING, &(object->astring), DBUS_TYPE_INVALID);
87     return reply;
88 }
89
90 static DBusMessage *
91 impl_setString (DBusConnection *bus, DBusMessage *message, void *user_data)
92 {
93     AnObject    *object = (AnObject *) user_data;
94     DBusMessage *reply;
95     DBusError    error;
96
97     dbus_error_init (&error);
98
99     g_free (object->astring);
100     dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &(object->astring), DBUS_TYPE_INVALID);
101
102     reply = dbus_message_new_method_return (message);
103     return reply;
104 }
105
106 static DBusMessage *
107 impl_getInterfaceOne (DBusConnection *bus, DBusMessage *message, void *user_data)
108 {
109     DBusMessage *reply;
110     DBusError    error;
111     gchar       *itf = TEST_INTERFACE_ONE;
112
113     dbus_error_init (&error);
114
115     reply = dbus_message_new_method_return (message);
116     dbus_message_append_args (reply, DBUS_TYPE_STRING, &itf, DBUS_TYPE_INVALID);
117     return reply;
118 }
119
120 static DBusMessage *
121 impl_getInterfaceTwo (DBusConnection *bus, DBusMessage *message, void *user_data)
122 {
123     DBusMessage *reply;
124     DBusError    error;
125     gchar       *itf = TEST_INTERFACE_TWO;
126
127     dbus_error_init (&error);
128
129     reply = dbus_message_new_method_return (message);
130     dbus_message_append_args (reply, DBUS_TYPE_STRING, &itf, DBUS_TYPE_INVALID);
131     return reply;
132 }
133
134 static DRouteMethod test_methods_one[] = {
135     {impl_null,            "null"},
136     {impl_getInt,          "getInt"},
137     {impl_setInt,          "setInt"},
138     {impl_getString,       "getString"},
139     {impl_setString,       "setString"},
140     {impl_getInterfaceOne, "getInterfaceOne"},
141     {NULL, NULL}
142 };
143
144 static DRouteMethod test_methods_two[] = {
145     {impl_null,            "null"},
146     {impl_getInt,          "getInt"},
147     {impl_setInt,          "setInt"},
148     {impl_getString,       "getString"},
149     {impl_setString,       "setString"},
150     {impl_getInterfaceTwo, "getInterfaceTwo"},
151     {NULL, NULL}
152 };
153
154 static DRouteProperty test_properties[] = {
155     {NULL, NULL, NULL}
156 };
157
158 gboolean
159 do_tests_func (gpointer data)
160 {
161     DBusError    error;
162     const gchar *bus_name;
163
164     gchar     *expected_string;
165     gchar     *result_string;
166
167     dbus_error_init (&error);
168     bus_name = dbus_bus_get_unique_name (bus);
169
170     /* --------------------------------------------------------*/
171
172     dbind_method_call_reentrant (bus,
173                                  bus_name,
174                                  TEST_OBJECT_PATH,
175                                  TEST_INTERFACE_ONE,
176                                  "null",
177                                  NULL,
178                                  "");
179
180     /* --------------------------------------------------------*/
181
182     expected_string = TEST_INTERFACE_ONE;
183     result_string = NULL;
184     dbind_method_call_reentrant (bus,
185                                  bus_name,
186                                  TEST_OBJECT_PATH,
187                                  TEST_INTERFACE_ONE,
188                                  "getInterfaceOne",
189                                  NULL,
190                                  "=>s",
191                                  &result_string);
192     if (g_strcmp0(expected_string, result_string))
193     {
194             g_print ("Failed: reply to getInterfaceOne not as expected\n");
195             goto out;
196     }
197
198     /* --------------------------------------------------------*/
199
200 out:
201     g_main_loop_quit (main_loop);
202     return FALSE;
203 }
204
205
206 int main (int argc, char **argv)
207 {
208     DRouteContext  *cnx;
209     DRoutePath     *path;
210     AnObject       *object;
211     DBusError       error;
212
213     /* Setup some server object */
214
215     object = g_new0(AnObject, 1);
216     object->astring = g_strdup (STRING_ONE);
217     object->anint = INT_ONE;
218
219     dbus_error_init (&error);
220     main_loop = g_main_loop_new(NULL, FALSE);
221     bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
222     dbus_connection_setup_with_g_main(bus, g_main_context_default());
223
224     cnx = droute_new (bus, TEST_INTROSPECTION_DIRECTORY);
225     path = droute_add_one (cnx, TEST_OBJECT_PATH, object);
226
227     droute_path_add_interface (path,
228                                TEST_INTERFACE_ONE,
229                                test_methods_one,
230                                test_properties);
231
232     droute_path_add_interface (path,
233                                TEST_INTERFACE_TWO,
234                                test_methods_two,
235                                test_properties);
236
237     g_idle_add (do_tests_func, NULL);
238     g_main_run(main_loop);
239     if (success)
240             return 0;
241     else
242             return 1;
243 }