2004-06-20 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   char *str;
66   
67   g_type_init ();
68   
69   loop = g_main_loop_new (NULL, FALSE);
70
71   error = NULL;
72   connection = dbus_g_bus_get (DBUS_BUS_SESSION,
73                                &error);
74   if (connection == NULL)
75     {
76       g_printerr ("Failed to open connection to bus: %s\n",
77                   error->message);
78       g_error_free (error);
79       exit (1);
80     }
81
82   /* Create a proxy object for the "bus driver" */
83   
84   driver = dbus_g_proxy_new_for_service (connection,
85                                          DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
86                                          DBUS_PATH_ORG_FREEDESKTOP_DBUS,
87                                          DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
88
89   /* Call ListServices method */
90   
91   call = dbus_g_proxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);
92
93   error = NULL;
94   if (!dbus_g_proxy_end_call (driver, call, &error,
95                               DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
96                               &service_list, &service_list_len,
97                               DBUS_TYPE_INVALID))
98     {
99       g_printerr ("Failed to complete ListServices call: %s\n",
100                   error->message);
101       g_error_free (error);
102       exit (1);
103     }
104
105   g_print ("Services on the message bus:\n");
106   i = 0;
107   while (i < service_list_len)
108     {
109       g_assert (service_list[i] != NULL);
110       g_print ("  %s\n", service_list[i]);
111       ++i;
112     }
113   g_assert (service_list[i] == NULL);
114
115   g_strfreev (service_list);
116
117   /* Test handling of unknown method */
118   call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
119                                  DBUS_TYPE_STRING,
120                                  "blah blah blah blah blah",
121                                  DBUS_TYPE_INT32,
122                                  10,
123                                  DBUS_TYPE_INVALID);
124
125   error = NULL;
126   if (dbus_g_proxy_end_call (driver, call, &error,
127                             DBUS_TYPE_INVALID))
128     {
129       g_printerr ("Calling nonexistent method succeeded!\n");
130       exit (1);
131     }
132
133   g_print ("Got EXPECTED error from calling unknown method: %s\n",
134            error->message);
135   g_error_free (error);
136   
137   /* Activate a service */
138   call = dbus_g_proxy_begin_call (driver, "ActivateService",
139                                  DBUS_TYPE_STRING,
140                                  "org.freedesktop.DBus.TestSuiteEchoService",
141                                  DBUS_TYPE_UINT32,
142                                  0,
143                                  DBUS_TYPE_INVALID);
144
145   error = NULL;
146   if (!dbus_g_proxy_end_call (driver, call, &error,
147                              DBUS_TYPE_UINT32, &result,
148                              DBUS_TYPE_INVALID))
149     {
150       g_printerr ("Failed to complete Activate call: %s\n",
151                   error->message);
152       g_error_free (error);
153       exit (1);
154     }
155
156   g_print ("Activation of echo service = 0x%x\n", result);
157
158   /* Activate a service again */
159   call = dbus_g_proxy_begin_call (driver, "ActivateService",
160                                  DBUS_TYPE_STRING,
161                                  "org.freedesktop.DBus.TestSuiteEchoService",
162                                  DBUS_TYPE_UINT32,
163                                  0,
164                                  DBUS_TYPE_INVALID);
165
166   error = NULL;
167   if (!dbus_g_proxy_end_call (driver, call, &error,
168                              DBUS_TYPE_UINT32, &result,
169                              DBUS_TYPE_INVALID))
170     {
171       g_printerr ("Failed to complete Activate call: %s\n",
172                   error->message);
173       g_error_free (error);
174       exit (1);
175     }
176
177   g_print ("Duplicate activation of echo service = 0x%x\n", result);
178
179   /* Talk to the new service */
180   
181   proxy = dbus_g_proxy_new_for_service_owner (connection,
182                                               "org.freedesktop.DBus.TestSuiteEchoService",
183                                               "/org/freedesktop/TestSuite",
184                                               "org.freedesktop.TestSuite",
185                                               &error);
186   
187   if (proxy == NULL)
188     {
189       g_printerr ("Failed to create proxy for service owner: %s\n",
190                   error->message);
191       g_error_free (error);
192       exit (1);      
193     }
194
195   call = dbus_g_proxy_begin_call (proxy, "Echo",
196                                  DBUS_TYPE_STRING,
197                                  "my string hello",
198                                  DBUS_TYPE_INVALID);
199
200   error = NULL;
201   if (!dbus_g_proxy_end_call (proxy, call, &error,
202                              DBUS_TYPE_STRING, &str,
203                              DBUS_TYPE_INVALID))
204     {
205       g_printerr ("Failed to complete Echo call: %s\n",
206                   error->message);
207       g_error_free (error);
208       exit (1);
209     }
210
211   g_print ("String echoed = \"%s\"\n", str);
212   g_free (str);
213
214   /* Test oneway call and signal handling */
215
216   dbus_g_proxy_connect_signal (proxy, "Foo",
217                                G_CALLBACK (foo_signal_handler),
218                                NULL, NULL);
219   
220   dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
221                               DBUS_TYPE_INVALID);
222   
223   dbus_g_connection_flush (connection);
224   
225   g_timeout_add (5000, timed_exit, loop);
226
227   g_main_loop_run (loop);
228
229   if (n_times_foo_received != 1)
230     {
231       g_printerr ("Foo signal received %d times, should have been 1\n",
232                   n_times_foo_received);
233       exit (1);
234     }
235   
236   g_object_unref (G_OBJECT (driver));
237   g_object_unref (G_OBJECT (proxy));
238   
239   g_print ("Successfully completed %s\n", argv[0]);
240   
241   return 0;
242 }