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