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