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 #include <glib-object.h>
11 #include "my-object-marshal.h"
13 static GMainLoop *loop = NULL;
14 static const char *await_terminating_service = NULL;
15 static int n_times_foo_received = 0;
16 static int n_times_frobnicate_received = 0;
17 static int n_times_frobnicate_received_2 = 0;
18 static int n_times_sig0_received = 0;
19 static int n_times_sig1_received = 0;
20 static int n_times_sig2_received = 0;
21 static guint exit_timeout = 0;
22 static gboolean proxy_destroyed = FALSE;
23 static gboolean proxy_destroy_and_nameowner = FALSE;
24 static gboolean proxy_destroy_and_nameowner_complete = FALSE;
27 timed_exit (gpointer loop)
29 g_print ("timed exit!\n");
30 g_main_loop_quit (loop);
35 proxy_destroyed_cb (DBusGProxy *proxy, gpointer user_data)
37 proxy_destroyed = TRUE;
38 if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && await_terminating_service == NULL)
40 g_main_loop_quit (loop);
41 proxy_destroy_and_nameowner_complete = TRUE;
46 name_owner_changed (DBusGProxy *proxy,
48 const char *prev_owner,
49 const char *new_owner,
52 g_print ("(signal NameOwnerChanged) name owner changed for %s from %s to %s\n",
53 name, prev_owner, new_owner);
54 if (await_terminating_service &&
55 !strcmp (name, await_terminating_service)
56 && !strcmp ("", new_owner))
58 g_print ("Caught expected ownership loss for %s\n", name);
59 await_terminating_service = NULL;
60 if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && proxy_destroyed)
62 g_main_loop_quit (loop);
63 proxy_destroy_and_nameowner_complete = TRUE;
65 else if (!proxy_destroy_and_nameowner)
67 g_main_loop_quit (loop);
73 foo_signal_handler (DBusGProxy *proxy,
77 n_times_foo_received += 1;
79 g_print ("Got Foo signal\n");
81 g_main_loop_quit (loop);
82 g_source_remove (exit_timeout);
86 frobnicate_signal_handler (DBusGProxy *proxy,
90 n_times_frobnicate_received += 1;
93 g_print ("Got Frobnicate signal\n");
95 g_main_loop_quit (loop);
96 g_source_remove (exit_timeout);
100 frobnicate_signal_handler_2 (DBusGProxy *proxy,
104 n_times_frobnicate_received_2 += 1;
106 g_assert (val == 42);
107 g_print ("Got Frobnicate signal (again)\n");
111 sig0_signal_handler (DBusGProxy *proxy,
117 n_times_sig0_received += 1;
119 g_assert (!strcmp (str0, "foo"));
121 g_assert (val == 22);
123 g_assert (!strcmp (str1, "moo"));
125 g_print ("Got Sig0 signal\n");
127 g_main_loop_quit (loop);
128 g_source_remove (exit_timeout);
132 sig1_signal_handler (DBusGProxy *proxy,
137 n_times_sig1_received += 1;
139 g_assert (!strcmp (str0, "baz"));
141 g_assert (G_VALUE_HOLDS_STRING (value));
143 g_assert (!strcmp (g_value_get_string (value), "bar"));
145 g_print ("Got Sig1 signal\n");
147 g_main_loop_quit (loop);
148 g_source_remove (exit_timeout);
152 sig2_signal_handler (DBusGProxy *proxy,
156 n_times_sig2_received += 1;
158 g_assert (g_hash_table_size (table) == 2);
160 g_assert (g_hash_table_lookup (table, "baz") != NULL);
161 g_assert (!strcmp (g_hash_table_lookup (table, "baz"), "cow"));
162 g_assert (g_hash_table_lookup (table, "bar") != NULL);
163 g_assert (!strcmp (g_hash_table_lookup (table, "bar"), "foo"));
165 g_print ("Got Sig2 signal\n");
167 g_main_loop_quit (loop);
168 g_source_remove (exit_timeout);
171 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
172 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
175 lose (const char *str, ...)
179 va_start (args, str);
181 vfprintf (stderr, str, args);
182 fputc ('\n', stderr);
190 lose_gerror (const char *prefix, GError *error)
192 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
193 lose ("%s (%s): %s", prefix, dbus_g_error_get_name (error),
196 lose ("%s: %s", prefix, error->message);
204 ctx = g_main_loop_get_context (loop);
206 while (g_main_context_pending (ctx))
207 g_main_context_iteration (ctx, FALSE);
211 main (int argc, char **argv)
213 DBusGConnection *connection;
218 DBusGPendingCall *call;
229 g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
231 loop = g_main_loop_new (NULL, FALSE);
234 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
236 if (connection == NULL)
237 lose_gerror ("Failed to open connection to bus", error);
239 /* should always get the same one */
240 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
241 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
242 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
244 /* Create a proxy object for the "bus driver" */
246 driver = dbus_g_proxy_new_for_name (connection,
249 DBUS_INTERFACE_DBUS);
251 dbus_g_proxy_add_signal (driver,
258 dbus_g_proxy_connect_signal (driver,
260 G_CALLBACK (name_owner_changed),
263 /* Call ListNames method */
265 call = dbus_g_proxy_begin_call (driver, "ListNames", G_TYPE_INVALID);
268 if (!dbus_g_proxy_end_call (driver, call, &error,
269 G_TYPE_STRV, &name_list,
271 lose_gerror ("Failed to complete ListNames call", error);
273 g_print ("Names on the message bus:\n");
275 name_list_len = g_strv_length (name_list);
276 while (i < name_list_len)
278 g_assert (name_list[i] != NULL);
279 g_print (" %s\n", name_list[i]);
282 g_assert (name_list[i] == NULL);
284 g_strfreev (name_list);
286 g_print ("calling ThisMethodDoesNotExist\n");
287 /* Test handling of unknown method */
288 call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
290 "blah blah blah blah blah",
296 if (dbus_g_proxy_end_call (driver, call, &error,
298 lose ("Calling nonexistent method succeeded!");
300 g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
301 g_error_free (error);
305 /* Activate a service */
306 g_print ("Activating echo service\n");
307 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
309 "org.freedesktop.DBus.TestSuiteEchoService",
315 if (!dbus_g_proxy_end_call (driver, call, &error,
316 G_TYPE_UINT, &result,
318 lose_gerror ("Failed to complete Activate call", error);
320 g_print ("Starting echo service result = 0x%x\n", result);
322 /* Activate a service again */
323 g_print ("Activating echo service again\n");
324 call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
326 "org.freedesktop.DBus.TestSuiteEchoService",
332 if (!dbus_g_proxy_end_call (driver, call, &error,
333 G_TYPE_UINT, &result,
335 lose_gerror ("Failed to complete Activate call", error);
337 g_print ("Duplicate start of echo service = 0x%x\n", result);
339 /* Talk to the new service */
341 g_print ("Creating proxy for echo service\n");
342 proxy = dbus_g_proxy_new_for_name_owner (connection,
343 "org.freedesktop.DBus.TestSuiteEchoService",
344 "/org/freedesktop/TestSuite",
345 "org.freedesktop.TestSuite",
349 lose_gerror ("Failed to create proxy for name owner", error);
353 g_print ("Calling Echo\n");
354 call = dbus_g_proxy_begin_call (proxy, "Echo",
360 if (!dbus_g_proxy_end_call (proxy, call, &error,
361 G_TYPE_STRING, &v_STRING_2,
363 lose_gerror ("Failed to complete Echo call", error);
365 g_print ("String echoed = \"%s\"\n", v_STRING_2);
368 /* Test oneway call and signal handling */
370 g_print ("Testing Foo emission\n");
371 dbus_g_proxy_add_signal (proxy, "Foo", G_TYPE_DOUBLE, G_TYPE_INVALID);
373 dbus_g_proxy_connect_signal (proxy, "Foo",
374 G_CALLBACK (foo_signal_handler),
377 dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
380 dbus_g_connection_flush (connection);
381 exit_timeout = g_timeout_add (5000, timed_exit, loop);
382 g_main_loop_run (loop);
384 if (n_times_foo_received != 1)
385 lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
387 /* Activate test servie */
388 g_print ("Activating TestSuiteGLibService\n");
390 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
392 "org.freedesktop.DBus.TestSuiteGLibService",
396 G_TYPE_UINT, &result,
398 lose_gerror ("Failed to complete Activate call", error);
401 g_print ("TestSuiteGLibService activated\n");
403 if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION"))
404 g_usleep (8 * G_USEC_PER_SEC);
406 g_object_unref (G_OBJECT (proxy));
410 proxy = dbus_g_proxy_new_for_name_owner (connection,
411 "org.freedesktop.DBus.TestSuiteGLibService",
412 "/org/freedesktop/DBus/Tests/MyTestObject",
413 "org.freedesktop.DBus.Tests.MyObject",
417 lose_gerror ("Failed to create proxy for name owner", error);
419 g_print ("Calling DoNothing\n");
420 call = dbus_g_proxy_begin_call (proxy, "DoNothing",
423 if (!dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID))
424 lose_gerror ("Failed to complete DoNothing call", error);
426 g_print ("Calling Increment\n");
428 if (!dbus_g_proxy_call (proxy, "Increment", &error,
431 G_TYPE_UINT, &v_UINT32_2,
433 lose_gerror ("Failed to complete Increment call", error);
435 if (v_UINT32_2 != 43)
436 lose ("Increment call returned %d, should be 43", v_UINT32_2);
438 g_print ("Calling ThrowError\n");
439 call = dbus_g_proxy_begin_call (proxy, "ThrowError", G_TYPE_INVALID);
441 if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID) != FALSE)
442 lose ("ThrowError call unexpectedly succeeded!");
443 if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
444 lose ("ThrowError call returned unexpected error %s", dbus_g_error_get_name (error));
446 g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
447 g_clear_error (&error);
449 g_print ("Calling Uppercase\n");
450 call = dbus_g_proxy_begin_call (proxy, "Uppercase",
451 G_TYPE_STRING, "foobar",
454 if (!dbus_g_proxy_end_call (proxy, call, &error,
455 G_TYPE_STRING, &v_STRING_2,
457 lose_gerror ("Failed to complete Uppercase call", error);
458 if (strcmp ("FOOBAR", v_STRING_2) != 0)
459 lose ("Uppercase call returned unexpected string %s", v_STRING_2);
464 g_print ("Calling ManyArgs\n");
465 call = dbus_g_proxy_begin_call (proxy, "ManyArgs",
467 G_TYPE_STRING, "bazwhee",
471 if (!dbus_g_proxy_end_call (proxy, call, &error,
472 G_TYPE_DOUBLE, &v_DOUBLE_2,
473 G_TYPE_STRING, &v_STRING_2,
475 lose_gerror ("Failed to complete ManyArgs call", error);
476 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
477 lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
478 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
479 lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
482 g_print ("Calling (wrapped) do_nothing\n");
483 if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
484 lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
486 g_print ("Calling (wrapped) increment\n");
487 if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
488 lose_gerror ("Failed to complete (wrapped) Increment call", error);
490 if (v_UINT32_2 != 43)
491 lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
493 g_print ("Calling (wrapped) throw_error\n");
494 if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
495 lose ("(wrapped) ThrowError call unexpectedly succeeded!");
497 g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
498 g_clear_error (&error);
500 g_print ("Calling (wrapped) uppercase\n");
501 if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error))
502 lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
503 if (strcmp ("FOOBAR", v_STRING_2) != 0)
504 lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
507 g_print ("Calling (wrapped) many_args\n");
508 if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
509 &v_DOUBLE_2, &v_STRING_2, &error))
510 lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
512 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
514 lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
516 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
517 lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
528 g_print ("Calling (wrapped) many_return\n");
529 if (!org_freedesktop_DBus_Tests_MyObject_many_return (proxy, &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &error))
530 lose_gerror ("Failed to complete (wrapped) ManyReturn call", error);
533 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg0);
535 if (strcmp ("42", arg1) != 0)
536 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg1);
540 lose ("(wrapped) ManyReturn call returned unexpected gint32 value %u", arg2);
543 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg3);
546 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg4);
548 if (strcmp ("hello world", arg5))
549 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg5);
556 GValue value = {0, };
558 g_value_init (&value, G_TYPE_STRING);
559 g_value_set_string (&value, "foo");
561 g_print ("Calling (wrapped) stringify, with string\n");
562 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
566 lose_gerror ("Failed to complete (wrapped) stringify call", error);
567 if (strcmp ("foo", v_STRING_2) != 0)
568 lose ("(wrapped) stringify call returned unexpected string %s", v_STRING_2);
571 g_value_unset (&value);
572 g_value_init (&value, G_TYPE_INT);
573 g_value_set_int (&value, 42);
575 g_print ("Calling (wrapped) stringify, with int\n");
576 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
580 lose_gerror ("Failed to complete (wrapped) stringify call 2", error);
581 if (strcmp ("42", v_STRING_2) != 0)
582 lose ("(wrapped) stringify call 2 returned unexpected string %s", v_STRING_2);
583 g_value_unset (&value);
586 g_value_init (&value, G_TYPE_INT);
587 g_value_set_int (&value, 88);
588 g_print ("Calling (wrapped) stringify, with another int\n");
589 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
593 lose_gerror ("Failed to complete (wrapped) stringify call 3", error);
594 g_value_unset (&value);
596 g_print ("Calling (wrapped) unstringify, for string\n");
597 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
601 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
602 if (!G_VALUE_HOLDS_STRING (&value))
603 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
604 if (strcmp (g_value_get_string (&value), "foo"))
605 lose ("(wrapped) unstringify call returned unexpected string %s",
606 g_value_get_string (&value));
608 g_value_unset (&value);
610 g_print ("Calling (wrapped) unstringify, for int\n");
611 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
615 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
616 if (!G_VALUE_HOLDS_INT (&value))
617 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
618 if (g_value_get_int (&value) != 10)
619 lose ("(wrapped) unstringify call returned unexpected integer %d",
620 g_value_get_int (&value));
622 g_value_unset (&value);
632 array = g_array_new (FALSE, TRUE, sizeof (guint32));
634 g_array_append_val (array, val);
636 g_array_append_val (array, val);
638 g_array_append_val (array, val);
640 g_array_append_val (array, val);
642 g_array_append_val (array, val);
645 g_print ("Calling (wrapped) recursive1\n");
646 if (!org_freedesktop_DBus_Tests_MyObject_recursive1 (proxy, array,
648 lose_gerror ("Failed to complete (wrapped) recursive1 call", error);
650 lose ("(wrapped) recursive1 call returned invalid length %u", arraylen);
654 GArray *array = NULL;
657 g_print ("Calling (wrapped) recursive2\n");
658 if (!org_freedesktop_DBus_Tests_MyObject_recursive2 (proxy, 2, &array, &error))
659 lose_gerror ("Failed to complete (wrapped) Recursive2 call", error);
662 lose ("(wrapped) Recursive2 call returned NULL");
664 lose ("(wrapped) Recursive2 call returned unexpected array length %u", array->len);
666 arrayvals = (guint32*) array->data;
667 if (arrayvals[0] != 42)
668 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 0", arrayvals[0]);
669 if (arrayvals[1] != 26)
670 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 1", arrayvals[1]);
671 if (arrayvals[4] != 2)
672 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 4", arrayvals[4]);
674 g_array_free (array, TRUE);
683 strs = g_new0 (char *, 4);
690 g_print ("Calling (wrapped) many_uppercase\n");
691 if (!org_freedesktop_DBus_Tests_MyObject_many_uppercase (proxy, strs, &strs_ret, &error))
692 lose_gerror ("Failed to complete (wrapped) ManyUppercase call", error);
693 g_assert (strs_ret != NULL);
694 if (strcmp ("HELLO", strs_ret[0]) != 0)
695 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[0]);
696 if (strcmp ("HELLO", strs_ret[1]) != 0)
697 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[1]);
698 if (strcmp ("HELLO", strs_ret[2]) != 0)
699 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[2]);
702 g_strfreev (strs_ret);
709 table = g_hash_table_new (g_str_hash, g_str_equal);
710 g_hash_table_insert (table, "moooo", "b");
711 g_hash_table_insert (table, "xxx", "cow!");
714 g_print ("Calling (wrapped) str_hash_len\n");
715 if (!org_freedesktop_DBus_Tests_MyObject_str_hash_len (proxy, table, &len, &error))
716 lose_gerror ("(wrapped) StrHashLen call failed", error);
718 lose ("(wrapped) StrHashLen returned unexpected length %u", len);
719 g_hash_table_destroy (table);
726 g_print ("Calling (wrapped) get_hash\n");
727 if (!org_freedesktop_DBus_Tests_MyObject_get_hash (proxy, &table, &error))
728 lose_gerror ("(wrapped) GetHash call failed", error);
729 val = g_hash_table_lookup (table, "foo");
730 if (val == NULL || strcmp ("bar", val))
731 lose ("(wrapped) StrHashLen returned invalid value %s for key \"foo\"",
732 val ? val : "(null)");
733 val = g_hash_table_lookup (table, "baz");
734 if (val == NULL || strcmp ("whee", val))
735 lose ("(wrapped) StrHashLen returned invalid value %s for key \"whee\"",
736 val ? val : "(null)");
737 val = g_hash_table_lookup (table, "cow");
738 if (val == NULL || strcmp ("crack", val))
739 lose ("(wrapped) StrHashLen returned invalid value %s for key \"cow\"",
740 val ? val : "(null)");
741 if (g_hash_table_size (table) != 3)
742 lose ("(wrapped) StrHashLen returned unexpected hash size %u",
743 g_hash_table_size (table));
745 g_hash_table_destroy (table);
753 DBusGProxy *ret_proxy;
755 g_print ("Calling (wrapped) objpath\n");
756 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
757 DBUS_TYPE_G_PROXY, proxy, G_TYPE_INVALID,
758 DBUS_TYPE_G_PROXY, &ret_proxy, G_TYPE_INVALID))
759 lose_gerror ("Failed to complete Objpath call", error);
760 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
761 dbus_g_proxy_get_path (ret_proxy)) != 0)
762 lose ("(wrapped) objpath call returned unexpected proxy %s",
763 dbus_g_proxy_get_path (ret_proxy));
765 g_print ("Doing get/increment val tests\n");
767 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
768 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
770 lose ("(wrapped) GetVal returned invalid value %d", val);
772 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
773 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
775 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
776 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
778 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
779 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
781 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
782 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
784 lose ("(wrapped) GetVal returned invalid value %d", val);
786 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
787 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
789 lose ("(wrapped) GetVal returned invalid value %d", val);
791 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (proxy, &error))
792 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
794 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
795 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
797 lose ("(wrapped) GetVal returned invalid value %d", val);
799 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
800 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
802 lose ("(wrapped) GetVal returned invalid value %d", val);
804 g_object_unref (G_OBJECT (ret_proxy));
806 g_print ("Calling objpath again\n");
809 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
810 DBUS_TYPE_G_OBJECT_PATH,
811 dbus_g_proxy_get_path (proxy),
813 DBUS_TYPE_G_OBJECT_PATH,
816 lose_gerror ("Failed to complete (wrapped) Objpath call 2", error);
817 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2", ret_path) != 0)
818 lose ("(wrapped) objpath call 2 returned unexpected path %s",
821 ret_proxy = dbus_g_proxy_new_for_name_owner (connection,
822 "org.freedesktop.DBus.TestSuiteGLibService",
824 "org.freedesktop.DBus.Tests.FooObject",
828 if (!org_freedesktop_DBus_Tests_FooObject_get_value (ret_proxy, &val, &error))
829 lose_gerror ("Failed to complete (wrapped) GetValue call", error);
831 lose ("(wrapped) GetValue returned invalid value %d", val);
836 /* Signal handling tests */
838 g_print ("Testing signal handling\n");
839 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
841 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
842 G_CALLBACK (frobnicate_signal_handler),
845 g_print ("Calling EmitFrobnicate\n");
846 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
847 G_TYPE_INVALID, G_TYPE_INVALID))
848 lose_gerror ("Failed to complete EmitFrobnicate call", error);
851 dbus_g_connection_flush (connection);
852 exit_timeout = g_timeout_add (5000, timed_exit, loop);
853 g_main_loop_run (loop);
855 if (n_times_frobnicate_received != 1)
856 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
858 g_print ("Calling EmitFrobnicate again\n");
859 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
860 G_TYPE_INVALID, G_TYPE_INVALID))
861 lose_gerror ("Failed to complete EmitFrobnicate call", error);
863 dbus_g_connection_flush (connection);
864 exit_timeout = g_timeout_add (5000, timed_exit, loop);
865 g_main_loop_run (loop);
867 if (n_times_frobnicate_received != 2)
868 lose ("Frobnicate signal received %d times, should have been 2", n_times_frobnicate_received);
870 g_object_unref (G_OBJECT (proxy));
874 g_print ("Creating proxy for FooObject interface\n");
875 proxy = dbus_g_proxy_new_for_name_owner (connection,
876 "org.freedesktop.DBus.TestSuiteGLibService",
877 "/org/freedesktop/DBus/Tests/MyTestObject",
878 "org.freedesktop.DBus.Tests.FooObject",
882 lose_gerror ("Failed to create proxy for name owner", error);
884 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_INT_STRING,
885 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
887 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_BOXED,
888 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
890 dbus_g_proxy_add_signal (proxy, "Sig0", G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
891 dbus_g_proxy_add_signal (proxy, "Sig1", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
892 dbus_g_proxy_add_signal (proxy, "Sig2", DBUS_TYPE_G_STRING_STRING_HASHTABLE, G_TYPE_INVALID);
894 dbus_g_proxy_connect_signal (proxy, "Sig0",
895 G_CALLBACK (sig0_signal_handler),
897 dbus_g_proxy_connect_signal (proxy, "Sig1",
898 G_CALLBACK (sig1_signal_handler),
900 dbus_g_proxy_connect_signal (proxy, "Sig2",
901 G_CALLBACK (sig2_signal_handler),
904 g_print ("Calling FooObject EmitSignals\n");
905 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
907 dbus_g_connection_flush (connection);
908 exit_timeout = g_timeout_add (5000, timed_exit, loop);
909 g_main_loop_run (loop);
910 exit_timeout = g_timeout_add (5000, timed_exit, loop);
911 g_main_loop_run (loop);
913 if (n_times_sig0_received != 1)
914 lose ("Sig0 signal received %d times, should have been 1", n_times_sig0_received);
915 if (n_times_sig1_received != 1)
916 lose ("Sig1 signal received %d times, should have been 1", n_times_sig1_received);
918 g_print ("Calling FooObject EmitSignals and EmitSignal2\n");
919 dbus_g_proxy_call_no_reply (proxy, "EmitSignal2", G_TYPE_INVALID);
920 dbus_g_connection_flush (connection);
922 exit_timeout = g_timeout_add (5000, timed_exit, loop);
923 g_main_loop_run (loop);
925 if (n_times_sig2_received != 1)
926 lose ("Sig2 signal received %d times, should have been 1", n_times_sig2_received);
928 g_print ("Calling FooObject EmitSignals two more times\n");
929 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
930 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
932 dbus_g_connection_flush (connection);
933 exit_timeout = g_timeout_add (5000, timed_exit, loop);
934 g_main_loop_run (loop);
935 exit_timeout = g_timeout_add (5000, timed_exit, loop);
936 g_main_loop_run (loop);
937 exit_timeout = g_timeout_add (5000, timed_exit, loop);
938 g_main_loop_run (loop);
939 exit_timeout = g_timeout_add (5000, timed_exit, loop);
940 g_main_loop_run (loop);
942 if (n_times_sig0_received != 3)
943 lose ("Sig0 signal received %d times, should have been 3", n_times_sig0_received);
944 if (n_times_sig1_received != 3)
945 lose ("Sig1 signal received %d times, should have been 3", n_times_sig1_received);
947 /* Terminate again */
948 g_print ("Terminating service\n");
949 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
950 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
952 proxy_destroyed = FALSE;
953 proxy_destroy_and_nameowner = TRUE;
954 proxy_destroy_and_nameowner_complete = FALSE;
956 g_signal_connect (G_OBJECT (proxy),
958 G_CALLBACK (proxy_destroyed_cb),
961 dbus_g_connection_flush (connection);
962 exit_timeout = g_timeout_add (5000, timed_exit, loop);
963 g_main_loop_run (loop);
965 if (await_terminating_service != NULL)
966 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
967 if (!proxy_destroyed)
968 lose ("Didn't get proxy_destroyed");
969 g_print ("Proxy destroyed successfully\n");
971 g_object_unref (G_OBJECT (proxy));
975 /* Create a new proxy for the name; should not be associated */
976 proxy = dbus_g_proxy_new_for_name (connection,
977 "org.freedesktop.DBus.TestSuiteGLibService",
978 "/org/freedesktop/DBus/Tests/MyTestObject",
979 "org.freedesktop.DBus.Tests.MyObject");
980 g_assert (proxy != NULL);
982 proxy_destroyed = FALSE;
983 proxy_destroy_and_nameowner = FALSE;
984 proxy_destroy_and_nameowner_complete = FALSE;
986 g_signal_connect (G_OBJECT (proxy),
988 G_CALLBACK (proxy_destroyed_cb),
991 if (!dbus_g_proxy_call (driver, "GetNameOwner", &error,
993 "org.freedesktop.DBus.TestSuiteGLibService",
998 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
999 g_print ("Got expected error \"org.freedesktop.DBus.Error.NameHasNoOwner\"\n");
1001 lose_gerror ("Unexpected error from GetNameOwner", error);
1003 lose ("GetNameOwner unexpectedly succeeded!");
1004 g_clear_error (&error);
1006 /* This will have the side-effect of activating the service, thus
1007 * causing a NameOwnerChanged, which should let our name proxy
1010 g_print ("Calling Uppercase for name proxy\n");
1011 if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
1012 G_TYPE_STRING, "bazwhee",
1014 G_TYPE_STRING, &v_STRING_2,
1016 lose_gerror ("Failed to complete Uppercase call", error);
1017 g_free (v_STRING_2);
1019 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1021 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1022 G_CALLBACK (frobnicate_signal_handler),
1025 g_print ("Calling EmitFrobnicate\n");
1026 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1027 G_TYPE_INVALID, G_TYPE_INVALID))
1028 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1030 n_times_frobnicate_received = 0;
1032 dbus_g_connection_flush (connection);
1033 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1034 g_main_loop_run (loop);
1036 if (n_times_frobnicate_received != 1)
1037 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1039 /* Now terminate the service, then start it again (implicitly) and wait for signals */
1040 g_print ("Terminating service (2)\n");
1041 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1042 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1043 dbus_g_connection_flush (connection);
1044 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1045 g_main_loop_run (loop);
1046 if (await_terminating_service != NULL)
1047 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1049 if (proxy_destroyed)
1050 lose ("Unexpectedly got proxy_destroyed!");
1052 n_times_frobnicate_received = 0;
1054 g_print ("Calling EmitFrobnicate (2)\n");
1055 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1056 G_TYPE_INVALID, G_TYPE_INVALID))
1057 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1059 dbus_g_connection_flush (connection);
1060 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1061 g_main_loop_run (loop);
1063 if (n_times_frobnicate_received != 1)
1064 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1066 if (proxy_destroyed)
1067 lose ("Unexpectedly got proxy_destroyed!");
1069 /* Create another proxy for the name; should be associated immediately */
1070 proxy2 = dbus_g_proxy_new_for_name (connection,
1071 "org.freedesktop.DBus.TestSuiteGLibService",
1072 "/org/freedesktop/DBus/Tests/MyTestObject",
1073 "org.freedesktop.DBus.Tests.MyObject");
1074 g_assert (proxy2 != NULL);
1076 dbus_g_proxy_add_signal (proxy2, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1078 dbus_g_proxy_connect_signal (proxy2, "Frobnicate",
1079 G_CALLBACK (frobnicate_signal_handler_2),
1082 g_print ("Calling EmitFrobnicate (3)\n");
1083 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1084 G_TYPE_INVALID, G_TYPE_INVALID))
1085 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1087 dbus_g_connection_flush (connection);
1088 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1089 g_main_loop_run (loop);
1091 if (n_times_frobnicate_received != 2)
1092 lose ("Frobnicate signal received %d times for 1st proxy, should have been 2", n_times_frobnicate_received);
1093 if (n_times_frobnicate_received_2 != 1)
1094 lose ("Frobnicate signal received %d times for 2nd proxy, should have been 1", n_times_frobnicate_received_2);
1096 g_object_unref (G_OBJECT (proxy));
1097 g_object_unref (G_OBJECT (proxy2));
1101 /* Test introspection */
1102 proxy = dbus_g_proxy_new_for_name_owner (connection,
1103 "org.freedesktop.DBus.TestSuiteGLibService",
1104 "/org/freedesktop/DBus/Tests/MyTestObject",
1105 "org.freedesktop.DBus.Introspectable",
1108 lose_gerror ("Failed to create proxy for name owner", error);
1110 g_print ("Testing introspect\n");
1111 call = dbus_g_proxy_begin_call (proxy, "Introspect",
1114 if (!dbus_g_proxy_end_call (proxy, call, &error,
1115 G_TYPE_STRING, &v_STRING_2,
1117 lose_gerror ("Failed to complete Introspect call", error);
1119 /* Could just do strcmp(), but that seems more fragile */
1123 gboolean found_introspectable;
1124 gboolean found_properties;
1125 gboolean found_myobject;
1126 gboolean found_fooobject;
1128 node = description_load_from_string (v_STRING_2, strlen (v_STRING_2), &error);
1130 lose_gerror ("Failed to parse introspection data: %s", error);
1132 found_introspectable = FALSE;
1133 found_properties = FALSE;
1134 found_myobject = FALSE;
1135 found_fooobject = FALSE;
1136 for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
1138 InterfaceInfo *iface = elt->data;
1140 if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
1141 found_introspectable = TRUE;
1142 else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
1143 found_properties = TRUE;
1144 else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
1147 gboolean found_manyargs;
1149 found_myobject = TRUE;
1151 found_manyargs = FALSE;
1152 for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
1157 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
1159 found_manyargs = TRUE;
1163 if (!found_manyargs)
1164 lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
1166 else if (!found_fooobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.FooObject") == 0)
1167 found_fooobject = TRUE;
1169 lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
1172 if (!(found_introspectable && found_myobject && found_properties))
1173 lose ("Missing interface");
1175 g_free (v_STRING_2);
1177 g_object_unref (G_OBJECT (driver));
1179 g_print ("Successfully completed %s\n", argv[0]);