2005-01-15 Havoc Pennington <hp@redhat.com>
[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
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                     void        *user_data)
20 {
21 #if 0
22   double d;
23   
24   /* FIXME - need to fix up dbus_gproxy_signal_connect() to be able to
25    * get signal args
26    */
27   
28   DBusError derror;
29   
30   if (!dbus_message_is_signal (signal,
31                                "org.freedesktop.TestSuite",
32                                "Foo"))
33     {
34       g_printerr ("Signal handler received the wrong message\n");
35       exit (1);
36     }
37
38   dbus_error_init (&derror);
39   if (!dbus_message_get_args (signal, &derror, DBUS_TYPE_DOUBLE,
40                               &d, DBUS_TYPE_INVALID))
41     {
42       g_printerr ("failed to get signal args: %s\n", derror.message);
43       dbus_error_free (&derror);
44       exit (1);
45     }
46 #endif
47
48   n_times_foo_received += 1;
49
50   g_main_loop_quit (loop);
51 }
52
53 int
54 main (int argc, char **argv)
55 {
56   DBusGConnection *connection;
57   GError *error;
58   DBusGProxy *driver;
59   DBusGProxy *proxy;
60   DBusGPendingCall *call;
61   char **service_list;
62   int service_list_len;
63   int i;
64   guint32 result;
65   const char *v_STRING;
66   guint32 v_UINT32;
67     
68   g_type_init ();
69   
70   loop = g_main_loop_new (NULL, FALSE);
71
72   error = NULL;
73   connection = dbus_g_bus_get (DBUS_BUS_SESSION,
74                                &error);
75   if (connection == NULL)
76     {
77       g_printerr ("Failed to open connection to bus: %s\n",
78                   error->message);
79       g_error_free (error);
80       exit (1);
81     }
82
83   /* Create a proxy object for the "bus driver" */
84   
85   driver = dbus_g_proxy_new_for_service (connection,
86                                          DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
87                                          DBUS_PATH_ORG_FREEDESKTOP_DBUS,
88                                          DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
89
90   /* Call ListServices method */
91   
92   call = dbus_g_proxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);
93
94   error = NULL;
95   if (!dbus_g_proxy_end_call (driver, call, &error,
96                               DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
97                               &service_list, &service_list_len,
98                               DBUS_TYPE_INVALID))
99     {
100       g_printerr ("Failed to complete ListServices call: %s\n",
101                   error->message);
102       g_error_free (error);
103       exit (1);
104     }
105
106   g_print ("Services on the message bus:\n");
107   i = 0;
108   while (i < service_list_len)
109     {
110       g_assert (service_list[i] != NULL);
111       g_print ("  %s\n", service_list[i]);
112       ++i;
113     }
114   g_assert (service_list[i] == NULL);
115
116   g_strfreev (service_list);
117
118   /* Test handling of unknown method */
119   v_STRING = "blah blah blah blah blah";
120   v_UINT32 = 10;
121   call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
122                                   DBUS_TYPE_STRING,
123                                   &v_STRING,
124                                   DBUS_TYPE_INT32,
125                                   &v_UINT32,
126                                   DBUS_TYPE_INVALID);
127
128   error = NULL;
129   if (dbus_g_proxy_end_call (driver, call, &error,
130                             DBUS_TYPE_INVALID))
131     {
132       g_printerr ("Calling nonexistent method succeeded!\n");
133       exit (1);
134     }
135
136   g_print ("Got EXPECTED error from calling unknown method: %s\n",
137            error->message);
138   g_error_free (error);
139   
140   /* Activate a service */
141   v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
142   v_UINT32 = 0;
143   call = dbus_g_proxy_begin_call (driver, "ActivateService",
144                                   DBUS_TYPE_STRING,
145                                   &v_STRING,
146                                   DBUS_TYPE_UINT32,
147                                   &v_UINT32,
148                                   DBUS_TYPE_INVALID);
149
150   error = NULL;
151   if (!dbus_g_proxy_end_call (driver, call, &error,
152                               DBUS_TYPE_UINT32, &result,
153                               DBUS_TYPE_INVALID))
154     {
155       g_printerr ("Failed to complete Activate call: %s\n",
156                   error->message);
157       g_error_free (error);
158       exit (1);
159     }
160
161   g_print ("Activation of echo service = 0x%x\n", result);
162
163   /* Activate a service again */
164   v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
165   v_UINT32 = 0;
166   call = dbus_g_proxy_begin_call (driver, "ActivateService",
167                                   DBUS_TYPE_STRING,
168                                   &v_STRING,
169                                   DBUS_TYPE_UINT32,
170                                   &v_UINT32,
171                                   DBUS_TYPE_INVALID);
172
173   error = NULL;
174   if (!dbus_g_proxy_end_call (driver, call, &error,
175                              DBUS_TYPE_UINT32, &result,
176                              DBUS_TYPE_INVALID))
177     {
178       g_printerr ("Failed to complete Activate call: %s\n",
179                   error->message);
180       g_error_free (error);
181       exit (1);
182     }
183
184   g_print ("Duplicate activation of echo service = 0x%x\n", result);
185
186   /* Talk to the new service */
187   
188   proxy = dbus_g_proxy_new_for_service_owner (connection,
189                                               "org.freedesktop.DBus.TestSuiteEchoService",
190                                               "/org/freedesktop/TestSuite",
191                                               "org.freedesktop.TestSuite",
192                                               &error);
193   
194   if (proxy == NULL)
195     {
196       g_printerr ("Failed to create proxy for service owner: %s\n",
197                   error->message);
198       g_error_free (error);
199       exit (1);      
200     }
201
202   v_STRING = "my string hello";
203   call = dbus_g_proxy_begin_call (proxy, "Echo",
204                                   DBUS_TYPE_STRING,
205                                   &v_STRING,
206                                   DBUS_TYPE_INVALID);
207
208   error = NULL;
209   if (!dbus_g_proxy_end_call (proxy, call, &error,
210                               DBUS_TYPE_STRING, &v_STRING,
211                               DBUS_TYPE_INVALID))
212     {
213       g_printerr ("Failed to complete Echo call: %s\n",
214                   error->message);
215       g_error_free (error);
216       exit (1);
217     }
218
219   g_print ("String echoed = \"%s\"\n", v_STRING);
220
221   /* Test oneway call and signal handling */
222
223   dbus_g_proxy_connect_signal (proxy, "Foo",
224                                G_CALLBACK (foo_signal_handler),
225                                NULL, NULL);
226   
227   dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
228                               DBUS_TYPE_INVALID);
229   
230   dbus_g_connection_flush (connection);
231   
232   g_timeout_add (5000, timed_exit, loop);
233
234   g_main_loop_run (loop);
235
236   if (n_times_foo_received != 1)
237     {
238       g_printerr ("Foo signal received %d times, should have been 1\n",
239                   n_times_foo_received);
240       exit (1);
241     }
242   
243   g_object_unref (G_OBJECT (driver));
244   g_object_unref (G_OBJECT (proxy));
245   
246   g_print ("Successfully completed %s\n", argv[0]);
247   
248   return 0;
249 }