2005-02-17 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / test / glib / test-dbus-glib.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include <dbus/dbus-glib.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "test-service-glib-bindings.h"
7
8 static GMainLoop *loop = NULL;
9 static int n_times_foo_received = 0;
10
11 static gboolean
12 timed_exit (gpointer loop)
13 {
14   g_main_loop_quit (loop);
15   return TRUE;
16 }
17
18 static void
19 foo_signal_handler (DBusGProxy  *proxy,
20                     double       d,
21                     void        *user_data)
22 {
23   n_times_foo_received += 1;
24
25   g_main_loop_quit (loop);
26 }
27
28 int
29 main (int argc, char **argv)
30 {
31   DBusGConnection *connection;
32   GError *error;
33   DBusGProxy *driver;
34   DBusGProxy *proxy;
35   DBusGPendingCall *call;
36   char **name_list;
37   int name_list_len;
38   int i;
39   guint32 result;
40   const char *v_STRING;
41   char *v_STRING_2;
42   guint32 v_UINT32;
43   guint32 v_UINT32_2;
44   double v_DOUBLE;
45   double v_DOUBLE_2;
46     
47   g_type_init ();
48   
49   loop = g_main_loop_new (NULL, FALSE);
50
51   error = NULL;
52   connection = dbus_g_bus_get (DBUS_BUS_SESSION,
53                                &error);
54   if (connection == NULL)
55     {
56       g_printerr ("Failed to open connection to bus: %s\n",
57                   error->message);
58       g_error_free (error);
59       exit (1);
60     }
61
62   /* should always get the same one */
63   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
64   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
65   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
66   
67   /* Create a proxy object for the "bus driver" */
68   
69   driver = dbus_g_proxy_new_for_name (connection,
70                                       DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
71                                       DBUS_PATH_ORG_FREEDESKTOP_DBUS,
72                                       DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
73
74   /* Call ListNames method */
75   
76   call = dbus_g_proxy_begin_call (driver, "ListNames", DBUS_TYPE_INVALID);
77
78   error = NULL;
79   if (!dbus_g_proxy_end_call (driver, call, &error,
80                               DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
81                               &name_list, &name_list_len,
82                               DBUS_TYPE_INVALID))
83     {
84       g_printerr ("Failed to complete ListNames call: %s\n",
85                   error->message);
86       g_error_free (error);
87       exit (1);
88     }
89
90   g_print ("Names on the message bus:\n");
91   i = 0;
92   while (i < name_list_len)
93     {
94       g_assert (name_list[i] != NULL);
95       g_print ("  %s\n", name_list[i]);
96       ++i;
97     }
98   g_assert (name_list[i] == NULL);
99
100   g_strfreev (name_list);
101
102   /* Test handling of unknown method */
103   v_STRING = "blah blah blah blah blah";
104   v_UINT32 = 10;
105   call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
106                                   DBUS_TYPE_STRING,
107                                   &v_STRING,
108                                   DBUS_TYPE_INT32,
109                                   &v_UINT32,
110                                   DBUS_TYPE_INVALID);
111
112   error = NULL;
113   if (dbus_g_proxy_end_call (driver, call, &error,
114                             DBUS_TYPE_INVALID))
115     {
116       g_printerr ("Calling nonexistent method succeeded!\n");
117       exit (1);
118     }
119
120   g_print ("Got EXPECTED error from calling unknown method: %s\n",
121            error->message);
122   g_error_free (error);
123   
124   /* Activate a service */
125   v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
126   v_UINT32 = 0;
127   call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
128                                   DBUS_TYPE_STRING,
129                                   &v_STRING,
130                                   DBUS_TYPE_UINT32,
131                                   &v_UINT32,
132                                   DBUS_TYPE_INVALID);
133
134   error = NULL;
135   if (!dbus_g_proxy_end_call (driver, call, &error,
136                               DBUS_TYPE_UINT32, &result,
137                               DBUS_TYPE_INVALID))
138     {
139       g_printerr ("Failed to complete Activate call: %s\n",
140                   error->message);
141       g_error_free (error);
142       exit (1);
143     }
144
145   g_print ("Starting echo service result = 0x%x\n", result);
146
147   /* Activate a service again */
148   v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
149   v_UINT32 = 0;
150   call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
151                                   DBUS_TYPE_STRING,
152                                   &v_STRING,
153                                   DBUS_TYPE_UINT32,
154                                   &v_UINT32,
155                                   DBUS_TYPE_INVALID);
156
157   error = NULL;
158   if (!dbus_g_proxy_end_call (driver, call, &error,
159                              DBUS_TYPE_UINT32, &result,
160                              DBUS_TYPE_INVALID))
161     {
162       g_printerr ("Failed to complete Activate call: %s\n",
163                   error->message);
164       g_error_free (error);
165       exit (1);
166     }
167
168   g_print ("Duplicate start of echo service = 0x%x\n", result);
169
170   /* Talk to the new service */
171   
172   proxy = dbus_g_proxy_new_for_name_owner (connection,
173                                            "org.freedesktop.DBus.TestSuiteEchoService",
174                                            "/org/freedesktop/TestSuite",
175                                            "org.freedesktop.TestSuite",
176                                            &error);
177   
178   if (proxy == NULL)
179     {
180       g_printerr ("Failed to create proxy for name owner: %s\n",
181                   error->message);
182       g_error_free (error);
183       exit (1);      
184     }
185
186   v_STRING = "my string hello";
187   call = dbus_g_proxy_begin_call (proxy, "Echo",
188                                   DBUS_TYPE_STRING,
189                                   &v_STRING,
190                                   DBUS_TYPE_INVALID);
191
192   error = NULL;
193   if (!dbus_g_proxy_end_call (proxy, call, &error,
194                               DBUS_TYPE_STRING, &v_STRING,
195                               DBUS_TYPE_INVALID))
196     {
197       g_printerr ("Failed to complete Echo call: %s\n",
198                   error->message);
199       g_error_free (error);
200       exit (1);
201     }
202
203   g_print ("String echoed = \"%s\"\n", v_STRING);
204
205   /* Test oneway call and signal handling */
206
207   dbus_g_proxy_add_signal (proxy, "Foo", DBUS_TYPE_DOUBLE_AS_STRING);
208   
209   dbus_g_proxy_connect_signal (proxy, "Foo",
210                                G_CALLBACK (foo_signal_handler),
211                                NULL, NULL);
212   
213   dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
214                               DBUS_TYPE_INVALID);
215   
216   dbus_g_connection_flush (connection);
217   
218   g_timeout_add (5000, timed_exit, loop);
219
220   g_main_loop_run (loop);
221
222   if (n_times_foo_received != 1)
223     {
224       g_printerr ("Foo signal received %d times, should have been 1\n",
225                   n_times_foo_received);
226       exit (1);
227     }
228
229   /* Activate test servie */ 
230   g_print ("Activating TestSuiteGLibService\n");
231   v_STRING = "org.freedesktop.DBus.TestSuiteGLibService";
232   v_UINT32 = 0;
233   call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
234                                   DBUS_TYPE_STRING,
235                                   &v_STRING,
236                                   DBUS_TYPE_UINT32,
237                                   &v_UINT32,
238                                   DBUS_TYPE_INVALID);
239
240   error = NULL;
241   if (!dbus_g_proxy_end_call (driver, call, &error,
242                              DBUS_TYPE_UINT32, &result,
243                              DBUS_TYPE_INVALID))
244     {
245       g_printerr ("Failed to complete Activate call: %s\n",
246                   error->message);
247       g_error_free (error);
248       exit (1);
249     }
250
251   g_object_unref (G_OBJECT (proxy));
252
253   proxy = dbus_g_proxy_new_for_name_owner (connection,
254                                            "org.freedesktop.DBus.TestSuiteGLibService",
255                                            "/org/freedesktop/DBus/Tests/MyTestObject",
256                                            "org.freedesktop.DBus.Tests.MyObject",
257                                            &error);
258   
259   if (proxy == NULL)
260     {
261       g_printerr ("Failed to create proxy for name owner: %s\n",
262                   error->message);
263       g_error_free (error);
264       exit (1);      
265     }
266
267   call = dbus_g_proxy_begin_call (proxy, "DoNothing",
268                                   DBUS_TYPE_INVALID);
269   error = NULL;
270   if (!dbus_g_proxy_end_call (proxy, call, &error, DBUS_TYPE_INVALID))
271     {
272       g_printerr ("Failed to complete DoNothing call: %s\n",
273                   error->message);
274       g_error_free (error);
275       exit (1);
276     }
277
278   v_UINT32 = 42;
279   call = dbus_g_proxy_begin_call (proxy, "Increment",
280                                   DBUS_TYPE_UINT32, &v_UINT32,
281                                   DBUS_TYPE_INVALID);
282   error = NULL;
283   if (!dbus_g_proxy_end_call (proxy, call, &error,
284                               DBUS_TYPE_UINT32, &v_UINT32_2,
285                               DBUS_TYPE_INVALID))
286     {
287       g_printerr ("Failed to complete Increment call: %s\n",
288                   error->message);
289       g_error_free (error);
290       exit (1);
291     }
292
293   if (v_UINT32_2 != v_UINT32 + 1)
294     {
295       g_printerr ("Increment call returned %d, should be 43\n", v_UINT32_2);
296       exit (1);
297     }
298
299   call = dbus_g_proxy_begin_call (proxy, "ThrowError", DBUS_TYPE_INVALID);
300   error = NULL;
301   if (dbus_g_proxy_end_call (proxy, call, &error, DBUS_TYPE_INVALID) != FALSE)
302     {
303       g_printerr ("ThrowError call unexpectedly succeeded!\n");
304       exit (1);
305     }
306
307   g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
308   g_error_free (error);
309
310   v_STRING = "foobar";
311   call = dbus_g_proxy_begin_call (proxy, "Uppercase",
312                                   DBUS_TYPE_STRING, &v_STRING,
313                                   DBUS_TYPE_INVALID);
314   error = NULL;
315   if (!dbus_g_proxy_end_call (proxy, call, &error,
316                               DBUS_TYPE_STRING, &v_STRING_2,
317                               DBUS_TYPE_INVALID))
318     {
319       g_printerr ("Failed to complete Uppercase call: %s\n",
320                   error->message);
321       g_error_free (error);
322       exit (1);
323     }
324   if (strcmp ("FOOBAR", v_STRING_2) != 0)
325     {
326       g_printerr ("Uppercase call returned unexpected string %s\n", v_STRING_2);
327       exit (1);
328     }
329
330   v_STRING = "bazwhee";
331   v_UINT32 = 26;
332   v_DOUBLE = G_PI;
333   call = dbus_g_proxy_begin_call (proxy, "ManyArgs",
334                                   DBUS_TYPE_UINT32, &v_UINT32,
335                                   DBUS_TYPE_STRING, &v_STRING,
336                                   DBUS_TYPE_DOUBLE, &v_DOUBLE,
337                                   DBUS_TYPE_INVALID);
338   error = NULL;
339   if (!dbus_g_proxy_end_call (proxy, call, &error,
340                               DBUS_TYPE_DOUBLE, &v_DOUBLE_2,
341                               DBUS_TYPE_STRING, &v_STRING_2,
342                               DBUS_TYPE_INVALID))
343     {
344       g_printerr ("Failed to complete ManyArgs call: %s\n",
345                   error->message);
346       g_error_free (error);
347       exit (1);
348     }
349   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
350     {
351       g_printerr ("ManyArgs call returned unexpected double value %f\n", v_DOUBLE_2);
352       exit (1);
353     }
354   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
355     {
356       g_printerr ("ManyArgs call returned unexpected string %s\n", v_STRING_2);
357       exit (1);
358     }
359
360   if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
361     {
362       g_printerr ("Failed to complete (wrapped) DoNothing call: %s\n",
363                   error->message);
364       g_error_free (error);
365       exit (1);
366     }
367
368   if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
369     {
370       g_printerr ("Failed to complete (wrapped) Increment call: %s\n",
371                   error->message);
372       g_error_free (error);
373       exit (1);
374     }
375
376   if (v_UINT32_2 != 43)
377     {
378       g_printerr ("(wrapped) increment call returned %d, should be 43\n", v_UINT32_2);
379       exit (1);
380     }
381
382   if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
383     {
384       g_printerr ("(wrapped) ThrowError call unexpectedly succeeded!\n");
385       exit (1);
386     }
387
388   g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
389   g_error_free (error);
390
391   if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error)) 
392     {
393       g_printerr ("Failed to complete (wrapped) Uppercase call: %s\n",
394                   error->message);
395       g_error_free (error);
396       exit (1);
397     }
398   if (strcmp ("FOOBAR", v_STRING_2) != 0)
399     {
400       g_printerr ("(wrapped) Uppercase call returned unexpected string %s\n", v_STRING_2);
401       exit (1);
402     }
403
404   if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
405                                                       &v_DOUBLE_2, &v_STRING_2, &error))
406     {
407       g_printerr ("Failed to complete (wrapped) ManyArgs call: %s\n",
408                   error->message);
409       g_error_free (error);
410       exit (1);
411     }
412   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
413     {
414       g_printerr ("(wrapped) ManyArgs call returned unexpected double value %f\n", v_DOUBLE_2);
415       exit (1);
416     }
417   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
418     {
419       g_printerr ("(wrapped) ManyArgs call returned unexpected string %s\n", v_STRING_2);
420       exit (1);
421     }
422
423   g_object_unref (G_OBJECT (proxy));
424   g_object_unref (G_OBJECT (driver));
425
426   g_print ("Successfully completed %s\n", argv[0]);
427   
428   return 0;
429 }