1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include <dbus/dbus-glib.h>
6 #include "test-service-glib-bindings.h"
7 #include <glib/dbus-gidl.h>
8 #include <glib/dbus-gparser.h>
10 static GMainLoop *loop = NULL;
11 static int n_times_foo_received = 0;
14 timed_exit (gpointer loop)
16 g_main_loop_quit (loop);
21 foo_signal_handler (DBusGProxy *proxy,
25 n_times_foo_received += 1;
27 g_main_loop_quit (loop);
30 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
31 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
34 lose (const char *str, ...)
40 vfprintf (stderr, str, args);
49 lose_gerror (const char *prefix, GError *error)
51 lose ("%s: %s", prefix, error->message);
55 main (int argc, char **argv)
57 DBusGConnection *connection;
61 DBusGPendingCall *call;
75 loop = g_main_loop_new (NULL, FALSE);
78 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
80 if (connection == NULL)
81 lose_gerror ("Failed to open connection to bus", error);
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));
88 /* Create a proxy object for the "bus driver" */
90 driver = dbus_g_proxy_new_for_name (connection,
95 /* Call ListNames method */
97 call = dbus_g_proxy_begin_call (driver, "ListNames", DBUS_TYPE_INVALID);
100 if (!dbus_g_proxy_end_call (driver, call, &error,
101 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
102 &name_list, &name_list_len,
104 lose_gerror ("Failed to complete ListNames call", error);
106 g_print ("Names on the message bus:\n");
108 while (i < name_list_len)
110 g_assert (name_list[i] != NULL);
111 g_print (" %s\n", name_list[i]);
114 g_assert (name_list[i] == NULL);
116 g_strfreev (name_list);
118 /* Test handling of unknown method */
119 v_STRING = "blah blah blah blah blah";
121 call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
129 if (dbus_g_proxy_end_call (driver, call, &error,
131 lose ("Calling nonexistent method succeeded!");
133 g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
134 g_error_free (error);
136 /* Activate a service */
137 v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
139 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
147 if (!dbus_g_proxy_end_call (driver, call, &error,
148 DBUS_TYPE_UINT32, &result,
150 lose_gerror ("Failed to complete Activate call", error);
152 g_print ("Starting echo service result = 0x%x\n", result);
154 /* Activate a service again */
155 v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
157 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
165 if (!dbus_g_proxy_end_call (driver, call, &error,
166 DBUS_TYPE_UINT32, &result,
168 lose_gerror ("Failed to complete Activate call", error);
170 g_print ("Duplicate start of echo service = 0x%x\n", result);
172 /* Talk to the new service */
174 proxy = dbus_g_proxy_new_for_name_owner (connection,
175 "org.freedesktop.DBus.TestSuiteEchoService",
176 "/org/freedesktop/TestSuite",
177 "org.freedesktop.TestSuite",
181 lose_gerror ("Failed to create proxy for name owner", error);
183 v_STRING = "my string hello";
184 call = dbus_g_proxy_begin_call (proxy, "Echo",
190 if (!dbus_g_proxy_end_call (proxy, call, &error,
191 DBUS_TYPE_STRING, &v_STRING,
193 lose_gerror ("Failed to complete Echo call", error);
195 g_print ("String echoed = \"%s\"\n", v_STRING);
197 /* Test oneway call and signal handling */
199 dbus_g_proxy_add_signal (proxy, "Foo", DBUS_TYPE_DOUBLE_AS_STRING);
201 dbus_g_proxy_connect_signal (proxy, "Foo",
202 G_CALLBACK (foo_signal_handler),
205 dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
208 dbus_g_connection_flush (connection);
210 g_timeout_add (5000, timed_exit, loop);
212 g_main_loop_run (loop);
214 if (n_times_foo_received != 1)
215 lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
217 /* Activate test servie */
218 g_print ("Activating TestSuiteGLibService\n");
219 v_STRING = "org.freedesktop.DBus.TestSuiteGLibService";
221 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
229 if (!dbus_g_proxy_end_call (driver, call, &error,
230 DBUS_TYPE_UINT32, &result,
232 lose_gerror ("Failed to complete Activate call", error);
234 g_object_unref (G_OBJECT (proxy));
236 proxy = dbus_g_proxy_new_for_name_owner (connection,
237 "org.freedesktop.DBus.TestSuiteGLibService",
238 "/org/freedesktop/DBus/Tests/MyTestObject",
239 "org.freedesktop.DBus.Tests.MyObject",
243 lose_gerror ("Failed to create proxy for name owner", error);
245 call = dbus_g_proxy_begin_call (proxy, "DoNothing",
248 if (!dbus_g_proxy_end_call (proxy, call, &error, DBUS_TYPE_INVALID))
249 lose_gerror ("Failed to complete DoNothing call", error);
252 call = dbus_g_proxy_begin_call (proxy, "Increment",
253 DBUS_TYPE_UINT32, &v_UINT32,
256 if (!dbus_g_proxy_end_call (proxy, call, &error,
257 DBUS_TYPE_UINT32, &v_UINT32_2,
259 lose_gerror ("Failed to complete Increment call", error);
261 if (v_UINT32_2 != v_UINT32 + 1)
262 lose ("Increment call returned %d, should be 43", v_UINT32_2);
264 call = dbus_g_proxy_begin_call (proxy, "ThrowError", DBUS_TYPE_INVALID);
266 if (dbus_g_proxy_end_call (proxy, call, &error, DBUS_TYPE_INVALID) != FALSE)
267 lose ("ThrowError call unexpectedly succeeded!");
269 g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
270 g_error_free (error);
273 call = dbus_g_proxy_begin_call (proxy, "Uppercase",
274 DBUS_TYPE_STRING, &v_STRING,
277 if (!dbus_g_proxy_end_call (proxy, call, &error,
278 DBUS_TYPE_STRING, &v_STRING_2,
280 lose_gerror ("Failed to complete Uppercase call", error);
281 if (strcmp ("FOOBAR", v_STRING_2) != 0)
282 lose ("Uppercase call returned unexpected string %s", v_STRING_2);
284 v_STRING = "bazwhee";
287 call = dbus_g_proxy_begin_call (proxy, "ManyArgs",
288 DBUS_TYPE_UINT32, &v_UINT32,
289 DBUS_TYPE_STRING, &v_STRING,
290 DBUS_TYPE_DOUBLE, &v_DOUBLE,
293 if (!dbus_g_proxy_end_call (proxy, call, &error,
294 DBUS_TYPE_DOUBLE, &v_DOUBLE_2,
295 DBUS_TYPE_STRING, &v_STRING_2,
297 lose_gerror ("Failed to complete ManyArgs call", error);
298 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
299 lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
300 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
301 lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
303 if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
304 lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
306 if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
307 lose_gerror ("Failed to complete (wrapped) Increment call", error);
309 if (v_UINT32_2 != 43)
310 lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
312 if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
313 lose ("(wrapped) ThrowError call unexpectedly succeeded!");
315 g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
316 g_error_free (error);
318 if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error))
319 lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
320 if (strcmp ("FOOBAR", v_STRING_2) != 0)
321 lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
323 if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
324 &v_DOUBLE_2, &v_STRING_2, &error))
325 lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
327 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
329 lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
331 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
332 lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
334 g_object_unref (G_OBJECT (proxy));
336 proxy = dbus_g_proxy_new_for_name_owner (connection,
337 "org.freedesktop.DBus.TestSuiteGLibService",
338 "/org/freedesktop/DBus/Tests/MyTestObject",
339 "org.freedesktop.DBus.Introspectable",
343 lose_gerror ("Failed to create proxy for name owner", error);
345 call = dbus_g_proxy_begin_call (proxy, "Introspect",
348 if (!dbus_g_proxy_end_call (proxy, call, &error,
349 DBUS_TYPE_STRING, &v_STRING,
351 lose_gerror ("Failed to complete Introspect call", error);
353 /* Could just do strcmp(), but that seems more fragile */
357 gboolean found_introspectable;
358 gboolean found_properties;
359 gboolean found_myobject;
360 gboolean found_gtk_myobject;
362 node = description_load_from_string (v_STRING, strlen (v_STRING), &error);
364 lose_gerror ("Failed to parse introspection data: %s", error);
366 found_introspectable = FALSE;
367 found_properties = FALSE;
368 found_gtk_myobject = FALSE;
369 found_myobject = FALSE;
370 for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
372 InterfaceInfo *iface = elt->data;
374 if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
375 found_introspectable = TRUE;
376 else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
377 found_properties = TRUE;
378 else if (!found_gtk_myobject && strcmp (interface_info_get_name (iface), "org.gtk.objects.MyObject") == 0)
379 found_gtk_myobject = TRUE;
380 else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
383 gboolean found_manyargs;
385 found_myobject = TRUE;
387 found_manyargs = FALSE;
388 for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
393 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
395 found_manyargs = TRUE;
400 lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
403 lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
406 if (!(found_introspectable && found_gtk_myobject && found_myobject && found_properties))
407 lose ("Missing interface");
410 g_object_unref (G_OBJECT (driver));
412 g_print ("Successfully completed %s\n", argv[0]);