1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include <dbus/dbus-glib.h>
6 #include "test-service-glib-bindings.h"
8 static GMainLoop *loop = NULL;
9 static int n_times_foo_received = 0;
12 timed_exit (gpointer loop)
14 g_main_loop_quit (loop);
19 foo_signal_handler (DBusGProxy *proxy,
23 n_times_foo_received += 1;
25 g_main_loop_quit (loop);
29 main (int argc, char **argv)
31 DBusGConnection *connection;
35 DBusGPendingCall *call;
49 loop = g_main_loop_new (NULL, FALSE);
52 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
54 if (connection == NULL)
56 g_printerr ("Failed to open connection to bus: %s\n",
62 /* should always get the same one */
63 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
64 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
65 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
67 /* Create a proxy object for the "bus driver" */
69 driver = dbus_g_proxy_new_for_name (connection,
74 /* Call ListNames method */
76 call = dbus_g_proxy_begin_call (driver, "ListNames", DBUS_TYPE_INVALID);
79 if (!dbus_g_proxy_end_call (driver, call, &error,
80 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
81 &name_list, &name_list_len,
84 g_printerr ("Failed to complete ListNames call: %s\n",
90 g_print ("Names on the message bus:\n");
92 while (i < name_list_len)
94 g_assert (name_list[i] != NULL);
95 g_print (" %s\n", name_list[i]);
98 g_assert (name_list[i] == NULL);
100 g_strfreev (name_list);
102 /* Test handling of unknown method */
103 v_STRING = "blah blah blah blah blah";
105 call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
113 if (dbus_g_proxy_end_call (driver, call, &error,
116 g_printerr ("Calling nonexistent method succeeded!\n");
120 g_print ("Got EXPECTED error from calling unknown method: %s\n",
122 g_error_free (error);
124 /* Activate a service */
125 v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
127 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
135 if (!dbus_g_proxy_end_call (driver, call, &error,
136 DBUS_TYPE_UINT32, &result,
139 g_printerr ("Failed to complete Activate call: %s\n",
141 g_error_free (error);
145 g_print ("Starting echo service result = 0x%x\n", result);
147 /* Activate a service again */
148 v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
150 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
158 if (!dbus_g_proxy_end_call (driver, call, &error,
159 DBUS_TYPE_UINT32, &result,
162 g_printerr ("Failed to complete Activate call: %s\n",
164 g_error_free (error);
168 g_print ("Duplicate start of echo service = 0x%x\n", result);
170 /* Talk to the new service */
172 proxy = dbus_g_proxy_new_for_name_owner (connection,
173 "org.freedesktop.DBus.TestSuiteEchoService",
174 "/org/freedesktop/TestSuite",
175 "org.freedesktop.TestSuite",
180 g_printerr ("Failed to create proxy for name owner: %s\n",
182 g_error_free (error);
186 v_STRING = "my string hello";
187 call = dbus_g_proxy_begin_call (proxy, "Echo",
193 if (!dbus_g_proxy_end_call (proxy, call, &error,
194 DBUS_TYPE_STRING, &v_STRING,
197 g_printerr ("Failed to complete Echo call: %s\n",
199 g_error_free (error);
203 g_print ("String echoed = \"%s\"\n", v_STRING);
205 /* Test oneway call and signal handling */
207 dbus_g_proxy_add_signal (proxy, "Foo", DBUS_TYPE_DOUBLE_AS_STRING);
209 dbus_g_proxy_connect_signal (proxy, "Foo",
210 G_CALLBACK (foo_signal_handler),
213 dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
216 dbus_g_connection_flush (connection);
218 g_timeout_add (5000, timed_exit, loop);
220 g_main_loop_run (loop);
222 if (n_times_foo_received != 1)
224 g_printerr ("Foo signal received %d times, should have been 1\n",
225 n_times_foo_received);
229 /* Activate test servie */
230 g_print ("Activating TestSuiteGLibService\n");
231 v_STRING = "org.freedesktop.DBus.TestSuiteGLibService";
233 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
241 if (!dbus_g_proxy_end_call (driver, call, &error,
242 DBUS_TYPE_UINT32, &result,
245 g_printerr ("Failed to complete Activate call: %s\n",
247 g_error_free (error);
251 g_object_unref (G_OBJECT (proxy));
253 proxy = dbus_g_proxy_new_for_name_owner (connection,
254 "org.freedesktop.DBus.TestSuiteGLibService",
255 "/org/freedesktop/DBus/Tests/MyTestObject",
256 "org.freedesktop.DBus.Tests.MyObject",
261 g_printerr ("Failed to create proxy for name owner: %s\n",
263 g_error_free (error);
267 call = dbus_g_proxy_begin_call (proxy, "DoNothing",
270 if (!dbus_g_proxy_end_call (proxy, call, &error, DBUS_TYPE_INVALID))
272 g_printerr ("Failed to complete DoNothing call: %s\n",
274 g_error_free (error);
279 call = dbus_g_proxy_begin_call (proxy, "Increment",
280 DBUS_TYPE_UINT32, &v_UINT32,
283 if (!dbus_g_proxy_end_call (proxy, call, &error,
284 DBUS_TYPE_UINT32, &v_UINT32_2,
287 g_printerr ("Failed to complete Increment call: %s\n",
289 g_error_free (error);
293 if (v_UINT32_2 != v_UINT32 + 1)
295 g_printerr ("Increment call returned %d, should be 43\n", v_UINT32_2);
299 call = dbus_g_proxy_begin_call (proxy, "ThrowError", DBUS_TYPE_INVALID);
301 if (dbus_g_proxy_end_call (proxy, call, &error, DBUS_TYPE_INVALID) != FALSE)
303 g_printerr ("ThrowError call unexpectedly succeeded!\n");
307 g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
308 g_error_free (error);
311 call = dbus_g_proxy_begin_call (proxy, "Uppercase",
312 DBUS_TYPE_STRING, &v_STRING,
315 if (!dbus_g_proxy_end_call (proxy, call, &error,
316 DBUS_TYPE_STRING, &v_STRING_2,
319 g_printerr ("Failed to complete Uppercase call: %s\n",
321 g_error_free (error);
324 if (strcmp ("FOOBAR", v_STRING_2) != 0)
326 g_printerr ("Uppercase call returned unexpected string %s\n", v_STRING_2);
330 v_STRING = "bazwhee";
333 call = dbus_g_proxy_begin_call (proxy, "ManyArgs",
334 DBUS_TYPE_UINT32, &v_UINT32,
335 DBUS_TYPE_STRING, &v_STRING,
336 DBUS_TYPE_DOUBLE, &v_DOUBLE,
339 if (!dbus_g_proxy_end_call (proxy, call, &error,
340 DBUS_TYPE_DOUBLE, &v_DOUBLE_2,
341 DBUS_TYPE_STRING, &v_STRING_2,
344 g_printerr ("Failed to complete ManyArgs call: %s\n",
346 g_error_free (error);
349 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
351 g_printerr ("ManyArgs call returned unexpected double value %f\n", v_DOUBLE_2);
354 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
356 g_printerr ("ManyArgs call returned unexpected string %s\n", v_STRING_2);
360 if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
362 g_printerr ("Failed to complete (wrapped) DoNothing call: %s\n",
364 g_error_free (error);
368 if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
370 g_printerr ("Failed to complete (wrapped) Increment call: %s\n",
372 g_error_free (error);
376 if (v_UINT32_2 != 43)
378 g_printerr ("(wrapped) increment call returned %d, should be 43\n", v_UINT32_2);
382 if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
384 g_printerr ("(wrapped) ThrowError call unexpectedly succeeded!\n");
388 g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
389 g_error_free (error);
391 if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error))
393 g_printerr ("Failed to complete (wrapped) Uppercase call: %s\n",
395 g_error_free (error);
398 if (strcmp ("FOOBAR", v_STRING_2) != 0)
400 g_printerr ("(wrapped) Uppercase call returned unexpected string %s\n", v_STRING_2);
404 if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
405 &v_DOUBLE_2, &v_STRING_2, &error))
407 g_printerr ("Failed to complete (wrapped) ManyArgs call: %s\n",
409 g_error_free (error);
412 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
414 g_printerr ("(wrapped) ManyArgs call returned unexpected double value %f\n", v_DOUBLE_2);
417 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
419 g_printerr ("(wrapped) ManyArgs call returned unexpected string %s\n", v_STRING_2);
423 g_object_unref (G_OBJECT (proxy));
424 g_object_unref (G_OBJECT (driver));
426 g_print ("Successfully completed %s\n", argv[0]);