2004-04-14 Olivier Andrieu <oliv__a@users.sourceforge.net>
[platform/upstream/dbus.git] / test / glib / test-dbus-glib.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include "dbus-glib.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 static GMainLoop *loop = NULL;
8 static int n_times_foo_received = 0;
9
10 static gboolean
11 timed_exit (gpointer loop)
12 {
13   g_main_loop_quit (loop);
14   return TRUE;
15 }
16
17 static void
18 foo_signal_handler (DBusGProxy  *proxy,
19                     DBusMessage *signal,
20                     void        *user_data)
21 {
22   double d;
23   DBusError derror;
24   
25   if (!dbus_message_is_signal (signal,
26                                "org.freedesktop.TestSuite",
27                                "Foo"))
28     {
29       g_printerr ("Signal handler received the wrong message\n");
30       exit (1);
31     }
32
33   dbus_error_init (&derror);
34   if (!dbus_message_get_args (signal, &derror, DBUS_TYPE_DOUBLE,
35                               &d, DBUS_TYPE_INVALID))
36     {
37       g_printerr ("failed to get signal args: %s\n", derror.message);
38       dbus_error_free (&derror);
39       exit (1);
40     }
41
42   n_times_foo_received += 1;
43
44   g_main_loop_quit (loop);
45 }
46
47 int
48 main (int argc, char **argv)
49 {
50   DBusConnection *connection;
51   GError *error;
52   DBusGProxy *driver;
53   DBusGProxy *proxy;
54   DBusPendingCall *call;
55   char **service_list;
56   int service_list_len;
57   int i;
58   dbus_uint32_t result;
59   char *str;
60   
61   g_type_init ();
62   
63   loop = g_main_loop_new (NULL, FALSE);
64
65   error = NULL;
66   connection = dbus_bus_get_with_g_main (DBUS_BUS_SESSION,
67                                          &error);
68   if (connection == NULL)
69     {
70       g_printerr ("Failed to open connection to bus: %s\n",
71                   error->message);
72       g_error_free (error);
73       exit (1);
74     }
75
76   /* Create a proxy object for the "bus driver" */
77   
78   driver = dbus_gproxy_new_for_service (connection,
79                                         DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
80                                         DBUS_PATH_ORG_FREEDESKTOP_DBUS,
81                                         DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
82
83   /* Call ListServices method */
84   
85   call = dbus_gproxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);
86
87   error = NULL;
88   if (!dbus_gproxy_end_call (driver, call, &error,
89                              DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
90                              &service_list, &service_list_len,
91                              DBUS_TYPE_INVALID))
92     {
93       g_printerr ("Failed to complete ListServices call: %s\n",
94                   error->message);
95       g_error_free (error);
96       exit (1);
97     }
98
99   g_print ("Services on the message bus:\n");
100   i = 0;
101   while (i < service_list_len)
102     {
103       g_assert (service_list[i] != NULL);
104       g_print ("  %s\n", service_list[i]);
105       ++i;
106     }
107   g_assert (service_list[i] == NULL);
108   
109   dbus_free_string_array (service_list);
110
111   /* Test handling of unknown method */
112   call = dbus_gproxy_begin_call (driver, "ThisMethodDoesNotExist",
113                                  DBUS_TYPE_STRING,
114                                  "blah blah blah blah blah",
115                                  DBUS_TYPE_INT32,
116                                  10,
117                                  DBUS_TYPE_INVALID);
118
119   error = NULL;
120   if (dbus_gproxy_end_call (driver, call, &error,
121                             DBUS_TYPE_INVALID))
122     {
123       g_printerr ("Calling nonexistent method succeeded!\n");
124       exit (1);
125     }
126
127   g_print ("Got EXPECTED error from calling unknown method: %s\n",
128            error->message);
129   g_error_free (error);
130   
131   /* Activate a service */
132   call = dbus_gproxy_begin_call (driver, "ActivateService",
133                                  DBUS_TYPE_STRING,
134                                  "org.freedesktop.DBus.TestSuiteEchoService",
135                                  DBUS_TYPE_UINT32,
136                                  0,
137                                  DBUS_TYPE_INVALID);
138
139   error = NULL;
140   if (!dbus_gproxy_end_call (driver, call, &error,
141                              DBUS_TYPE_UINT32, &result,
142                              DBUS_TYPE_INVALID))
143     {
144       g_printerr ("Failed to complete Activate call: %s\n",
145                   error->message);
146       g_error_free (error);
147       exit (1);
148     }
149
150   g_print ("Activation of echo service = 0x%x\n", result);
151
152   /* Activate a service again */
153   call = dbus_gproxy_begin_call (driver, "ActivateService",
154                                  DBUS_TYPE_STRING,
155                                  "org.freedesktop.DBus.TestSuiteEchoService",
156                                  DBUS_TYPE_UINT32,
157                                  0,
158                                  DBUS_TYPE_INVALID);
159
160   error = NULL;
161   if (!dbus_gproxy_end_call (driver, call, &error,
162                              DBUS_TYPE_UINT32, &result,
163                              DBUS_TYPE_INVALID))
164     {
165       g_printerr ("Failed to complete Activate call: %s\n",
166                   error->message);
167       g_error_free (error);
168       exit (1);
169     }
170
171   g_print ("Duplicate activation of echo service = 0x%x\n", result);
172
173   /* Talk to the new service */
174   
175   proxy = dbus_gproxy_new_for_service (connection,
176                                        "org.freedesktop.DBus.TestSuiteEchoService",
177                                        "/org/freedesktop/TestSuite",
178                                        "org.freedesktop.TestSuite");
179   
180   call = dbus_gproxy_begin_call (proxy, "Echo",
181                                  DBUS_TYPE_STRING,
182                                  "my string hello",
183                                  DBUS_TYPE_INVALID);
184
185   error = NULL;
186   if (!dbus_gproxy_end_call (proxy, call, &error,
187                              DBUS_TYPE_STRING, &str,
188                              DBUS_TYPE_INVALID))
189     {
190       g_printerr ("Failed to complete Echo call: %s\n",
191                   error->message);
192       g_error_free (error);
193       exit (1);
194     }
195
196   g_print ("String echoed = \"%s\"\n", str);
197   dbus_free (str);
198
199   /* Test oneway call and signal handling */
200
201   dbus_gproxy_connect_signal (proxy, "Foo",
202                               foo_signal_handler,
203                               NULL, NULL);
204   
205   dbus_gproxy_call_no_reply (proxy, "EmitFoo",
206                              DBUS_TYPE_INVALID);
207
208   dbus_connection_flush (connection);
209   
210   g_timeout_add (5000, timed_exit, loop);
211
212   g_main_loop_run (loop);
213
214   if (n_times_foo_received != 1)
215     {
216       g_printerr ("Foo signal received %d times, should have been 1\n",
217                   n_times_foo_received);
218       exit (1);
219     }
220   
221   g_object_unref (G_OBJECT (driver));
222   g_object_unref (G_OBJECT (proxy));
223   
224   g_print ("Successfully completed %s\n", argv[0]);
225   
226   return 0;
227 }