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