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