1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2011 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbus-tests.h"
30 #include "gdbus-test-codegen-generated.h"
32 /* ---------------------------------------------------------------------------------------------------- */
35 count_annotations (GDBusAnnotationInfo **annotations)
39 while (annotations != NULL && annotations[ret] != NULL)
46 * - non-internal annotations are written out correctly; and
47 * - injection via --annotation --key --value works
50 test_annotations (void)
52 GDBusInterfaceInfo *iface;
53 GDBusMethodInfo *method;
54 GDBusSignalInfo *signal;
55 GDBusPropertyInfo *property;
57 iface = foo_bar_interface_info ();
58 g_assert (iface != NULL);
60 /* see Makefile.am for where these annotations are injected */
61 g_assert_cmpint (count_annotations (iface->annotations), ==, 1);
62 g_assert_cmpstr (g_dbus_annotation_info_lookup (iface->annotations, "Key1"), ==, "Value1");
64 method = g_dbus_interface_info_lookup_method (iface, "HelloWorld");
65 g_assert (method != NULL);
66 g_assert_cmpint (count_annotations (method->annotations), ==, 2);
67 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->annotations, "ExistingAnnotation"), ==, "blah");
68 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->annotations, "Key3"), ==, "Value3");
70 signal = g_dbus_interface_info_lookup_signal (iface, "TestSignal");
71 g_assert (signal != NULL);
72 g_assert_cmpint (count_annotations (signal->annotations), ==, 1);
73 g_assert_cmpstr (g_dbus_annotation_info_lookup (signal->annotations, "Key4"), ==, "Value4");
74 g_assert_cmpstr (g_dbus_annotation_info_lookup (signal->args[1]->annotations, "Key8"), ==, "Value8");
76 property = g_dbus_interface_info_lookup_property (iface, "ay");
77 g_assert (property != NULL);
78 g_assert_cmpint (count_annotations (property->annotations), ==, 1);
79 g_assert_cmpstr (g_dbus_annotation_info_lookup (property->annotations, "Key5"), ==, "Value5");
81 method = g_dbus_interface_info_lookup_method (iface, "TestPrimitiveTypes");
82 g_assert (method != NULL);
83 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->in_args[4]->annotations, "Key6"), ==, "Value6");
84 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->out_args[5]->annotations, "Key7"), ==, "Value7");
87 /* ---------------------------------------------------------------------------------------------------- */
90 on_handle_hello_world (FooBar *object,
91 GDBusMethodInvocation *invocation,
92 const gchar *greeting,
96 response = g_strdup_printf ("Word! You said `%s'. I'm Skeleton, btw!", greeting);
97 foo_bar_complete_hello_world (object, invocation, response);
103 on_handle_test_primitive_types (FooBar *object,
104 GDBusMethodInvocation *invocation,
106 gboolean val_boolean,
114 const gchar *val_string,
115 const gchar *val_objpath,
116 const gchar *val_signature,
117 const gchar *val_bytestring,
123 s1 = g_strdup_printf ("Word! You said `%s'. Rock'n'roll!", val_string);
124 s2 = g_strdup_printf ("/modified%s", val_objpath);
125 s3 = g_strdup_printf ("assgit%s", val_signature);
126 foo_bar_complete_test_primitive_types (object,
135 10000000 + val_uint64,
148 on_handle_test_non_primitive_types (FooBar *object,
149 GDBusMethodInvocation *invocation,
150 GVariant *dict_s_to_s,
151 GVariant *dict_s_to_pairs,
153 const gchar* const *array_of_strings,
154 GVariant *array_of_objpaths,
155 GVariant *array_of_signatures,
156 const gchar* const *array_of_bytestrings,
161 str = g_string_new (NULL);
162 s = g_variant_print (dict_s_to_s, TRUE); g_string_append (str, s); g_free (s);
163 s = g_variant_print (dict_s_to_pairs, TRUE); g_string_append (str, s); g_free (s);
164 s = g_variant_print (a_struct, TRUE); g_string_append (str, s); g_free (s);
165 s = g_strjoinv (", ", (gchar **) array_of_strings);
166 g_string_append_printf (str, "array_of_strings: [%s] ", s);
168 s = g_variant_print (array_of_objpaths, TRUE);
169 g_string_append_printf (str, "array_of_objpaths: %s ", s);
171 s = g_variant_print (array_of_objpaths, TRUE);
172 g_string_append_printf (str, "array_of_signatures: %s ", s);
174 s = g_strjoinv (", ", (gchar **) array_of_bytestrings);
175 g_string_append_printf (str, "array_of_bytestrings: [%s] ", s);
177 foo_bar_complete_test_non_primitive_types (object, invocation, str->str);
178 g_string_free (str, TRUE);
183 on_handle_request_signal_emission (FooBar *object,
184 GDBusMethodInvocation *invocation,
190 const gchar *a_strv[] = {"foo", "bar", NULL};
191 const gchar *a_bytestring_array[] = {"foo\xff", "bar\xff", NULL};
192 GVariant *a_variant = g_variant_new_parsed ("{'first': (42, 42), 'second': (43, 43)}");
193 foo_bar_emit_test_signal (object, 43, a_strv, a_bytestring_array, a_variant); /* consumes a_variant */
194 foo_bar_complete_request_signal_emission (object, invocation);
200 on_handle_request_multi_property_mods (FooBar *object,
201 GDBusMethodInvocation *invocation,
204 foo_bar_set_y (object, foo_bar_get_y (object) + 1);
205 foo_bar_set_i (object, foo_bar_get_i (object) + 1);
206 foo_bar_set_y (object, foo_bar_get_y (object) + 1);
207 foo_bar_set_i (object, foo_bar_get_i (object) + 1);
208 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
209 foo_bar_set_y (object, foo_bar_get_y (object) + 1);
210 foo_bar_set_i (object, foo_bar_get_i (object) + 1);
211 foo_bar_complete_request_multi_property_mods (object, invocation);
216 on_handle_property_cancellation (FooBar *object,
217 GDBusMethodInvocation *invocation,
221 n = foo_bar_get_n (object);
222 /* This queues up a PropertiesChange event */
223 foo_bar_set_n (object, n + 1);
224 /* this modifies the queued up event */
225 foo_bar_set_n (object, n);
226 /* this flushes all PropertiesChanges event (sends the D-Bus message right
227 * away, if any - there should not be any)
229 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
230 /* this makes us return the reply D-Bus method */
231 foo_bar_complete_property_cancellation (object, invocation);
235 /* ---------------------------------------------------------------------------------------------------- */
238 on_handle_force_method (FooBat *object,
239 GDBusMethodInvocation *invocation,
240 GVariant *force_in_i,
241 GVariant *force_in_s,
242 GVariant *force_in_ay,
243 GVariant *force_in_struct,
249 GVariant *ret_struct;
253 ret_i = g_variant_new_int32 (g_variant_get_int32 (force_in_i) + 10);
254 s = g_strdup_printf ("%s_foo", g_variant_get_string (force_in_s, NULL));
255 ret_s = g_variant_new_string (s);
257 s = g_strdup_printf ("%s_foo\xff", g_variant_get_bytestring (force_in_ay));
258 ret_ay = g_variant_new_bytestring (s);
261 g_variant_get (force_in_struct, "(i)", &val);
262 ret_struct = g_variant_new ("(i)", val + 10);
264 g_variant_ref_sink (ret_i);
265 g_variant_ref_sink (ret_s);
266 g_variant_ref_sink (ret_ay);
267 g_variant_ref_sink (ret_struct);
269 foo_bat_emit_force_signal (object,
275 foo_bat_complete_force_method (object,
282 g_variant_unref (ret_i);
283 g_variant_unref (ret_s);
284 g_variant_unref (ret_ay);
285 g_variant_unref (ret_struct);
291 /* ---------------------------------------------------------------------------------------------------- */
294 my_g_authorize_method_handler (GDBusInterfaceSkeleton *interface,
295 GDBusMethodInvocation *invocation,
298 const gchar *method_name;
303 method_name = g_dbus_method_invocation_get_method_name (invocation);
304 if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
308 else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
312 else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
318 g_assert_not_reached ();
323 g_dbus_method_invocation_return_error (invocation,
325 G_IO_ERROR_PERMISSION_DENIED,
326 "not authorized...");
332 my_object_authorize_method_handler (GDBusObjectSkeleton *object,
333 GDBusInterfaceSkeleton *interface,
334 GDBusMethodInvocation *invocation,
337 const gchar *method_name;
342 method_name = g_dbus_method_invocation_get_method_name (invocation);
343 if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
347 else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
351 else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
357 g_assert_not_reached ();
362 g_dbus_method_invocation_return_error (invocation,
365 "not authorized (from object)...");
371 on_handle_check_not_authorized (FooAuthorize *object,
372 GDBusMethodInvocation *invocation,
375 foo_authorize_complete_check_not_authorized (object, invocation);
380 on_handle_check_authorized (FooAuthorize *object,
381 GDBusMethodInvocation *invocation,
384 foo_authorize_complete_check_authorized (object, invocation);
389 on_handle_check_not_authorized_from_object (FooAuthorize *object,
390 GDBusMethodInvocation *invocation,
393 foo_authorize_complete_check_not_authorized_from_object (object, invocation);
397 /* ---------------------------------------------------------------------------------------------------- */
400 on_handle_get_self (FooMethodThreads *object,
401 GDBusMethodInvocation *invocation,
405 s = g_strdup_printf ("%p", g_thread_self ());
406 foo_method_threads_complete_get_self (object, invocation, s);
411 /* ---------------------------------------------------------------------------------------------------- */
413 static GThread *method_handler_thread = NULL;
415 static FooBar *exported_bar_object = NULL;
416 static FooBat *exported_bat_object = NULL;
417 static FooAuthorize *exported_authorize_object = NULL;
418 static GDBusObjectSkeleton *authorize_enclosing_object = NULL;
419 static FooMethodThreads *exported_thread_object_1 = NULL;
420 static FooMethodThreads *exported_thread_object_2 = NULL;
423 on_bus_acquired (GDBusConnection *connection,
429 /* Test that we can export an object using the generated
430 * FooBarSkeleton subclass. Notes:
432 * 1. We handle methods by simply connecting to the appropriate
435 * 2. Property storage is taken care of by the class; we can
436 * use g_object_get()/g_object_set() (and the generated
437 * C bindings at will)
440 exported_bar_object = foo_bar_skeleton_new ();
441 foo_bar_set_ay (exported_bar_object, "ABCabc");
442 foo_bar_set_y (exported_bar_object, 42);
443 foo_bar_set_d (exported_bar_object, 43.0);
444 foo_bar_set_finally_normal_name (exported_bar_object, "There aint no place like home");
445 foo_bar_set_writeonly_property (exported_bar_object, "Mr. Burns");
447 /* The following works because it's on the Skeleton object - it will
448 * fail (at run-time) on a Proxy (see on_proxy_appeared() below)
450 foo_bar_set_readonly_property (exported_bar_object, "blah");
451 g_assert_cmpstr (foo_bar_get_writeonly_property (exported_bar_object), ==, "Mr. Burns");
453 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bar_object),
457 g_assert_no_error (error);
458 g_signal_connect (exported_bar_object,
459 "handle-hello-world",
460 G_CALLBACK (on_handle_hello_world),
462 g_signal_connect (exported_bar_object,
463 "handle-test-primitive-types",
464 G_CALLBACK (on_handle_test_primitive_types),
466 g_signal_connect (exported_bar_object,
467 "handle-test-non-primitive-types",
468 G_CALLBACK (on_handle_test_non_primitive_types),
470 g_signal_connect (exported_bar_object,
471 "handle-request-signal-emission",
472 G_CALLBACK (on_handle_request_signal_emission),
474 g_signal_connect (exported_bar_object,
475 "handle-request-multi-property-mods",
476 G_CALLBACK (on_handle_request_multi_property_mods),
478 g_signal_connect (exported_bar_object,
479 "handle-property-cancellation",
480 G_CALLBACK (on_handle_property_cancellation),
483 exported_bat_object = foo_bat_skeleton_new ();
484 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bat_object),
488 g_assert_no_error (error);
489 g_signal_connect (exported_bat_object,
490 "handle-force-method",
491 G_CALLBACK (on_handle_force_method),
493 g_object_set (exported_bat_object,
494 "force-i", g_variant_new_int32 (43),
495 "force-s", g_variant_new_string ("prop string"),
496 "force-ay", g_variant_new_bytestring ("prop bytestring\xff"),
497 "force-struct", g_variant_new ("(i)", 4300),
500 authorize_enclosing_object = g_dbus_object_skeleton_new ("/authorize");
501 g_signal_connect (authorize_enclosing_object,
503 G_CALLBACK (my_object_authorize_method_handler),
505 exported_authorize_object = foo_authorize_skeleton_new ();
506 g_dbus_object_skeleton_add_interface (authorize_enclosing_object,
507 G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
508 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_authorize_object),
512 g_assert_no_error (error);
513 g_signal_connect (exported_authorize_object,
514 "g-authorize-method",
515 G_CALLBACK (my_g_authorize_method_handler),
517 g_signal_connect (exported_authorize_object,
518 "handle-check-not-authorized",
519 G_CALLBACK (on_handle_check_not_authorized),
521 g_signal_connect (exported_authorize_object,
522 "handle-check-authorized",
523 G_CALLBACK (on_handle_check_authorized),
525 g_signal_connect (exported_authorize_object,
526 "handle-check-not-authorized-from-object",
527 G_CALLBACK (on_handle_check_not_authorized_from_object),
531 /* only object 1 has the G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD flag set */
532 exported_thread_object_1 = foo_method_threads_skeleton_new ();
533 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
534 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
535 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
539 g_assert_no_error (error);
540 g_signal_connect (exported_thread_object_1,
542 G_CALLBACK (on_handle_get_self),
545 exported_thread_object_2 = foo_method_threads_skeleton_new ();
546 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2),
550 g_assert_no_error (error);
551 g_signal_connect (exported_thread_object_2,
553 G_CALLBACK (on_handle_get_self),
556 method_handler_thread = g_thread_self ();
559 static gpointer check_proxies_in_thread (gpointer user_data);
562 on_name_acquired (GDBusConnection *connection,
566 GMainLoop *loop = user_data;
568 g_thread_create (check_proxies_in_thread,
575 on_name_lost (GDBusConnection *connection,
579 g_assert_not_reached ();
582 /* ---------------------------------------------------------------------------------------------------- */
586 GMainLoop *thread_loop;
589 guint num_g_properties_changed;
590 gboolean received_test_signal;
596 on_notify_u (GObject *object,
600 ClientData *data = user_data;
601 g_assert_cmpstr (pspec->name, ==, "u");
602 data->num_notify_u += 1;
606 on_notify_n (GObject *object,
610 ClientData *data = user_data;
611 g_assert_cmpstr (pspec->name, ==, "n");
612 data->num_notify_n += 1;
616 on_g_properties_changed (GDBusProxy *_proxy,
617 GVariant *changed_properties,
618 const gchar* const *invalidated_properties,
621 ClientData *data = user_data;
622 FooBar *proxy = FOO_BAR (_proxy);
624 g_assert_cmpint (g_variant_n_children (changed_properties), ==, 2);
626 if (data->num_g_properties_changed == 0)
628 g_assert_cmpint (data->initial_y, ==, foo_bar_get_y (proxy) - 2);
629 g_assert_cmpint (data->initial_i, ==, foo_bar_get_i (proxy) - 2);
631 else if (data->num_g_properties_changed == 1)
633 g_assert_cmpint (data->initial_y, ==, foo_bar_get_y (proxy) - 3);
634 g_assert_cmpint (data->initial_i, ==, foo_bar_get_i (proxy) - 3);
637 g_assert_not_reached ();
639 data->num_g_properties_changed++;
641 if (data->num_g_properties_changed == 2)
642 g_main_loop_quit (data->thread_loop);
646 on_test_signal (FooBar *proxy,
648 const gchar* const *array_of_strings,
649 const gchar* const *array_of_bytestrings,
650 GVariant *dict_s_to_pairs,
653 ClientData *data = user_data;
655 g_assert_cmpint (val_int32, ==, 43);
656 g_assert_cmpstr (array_of_strings[0], ==, "foo");
657 g_assert_cmpstr (array_of_strings[1], ==, "bar");
658 g_assert (array_of_strings[2] == NULL);
659 g_assert_cmpstr (array_of_bytestrings[0], ==, "foo\xff");
660 g_assert_cmpstr (array_of_bytestrings[1], ==, "bar\xff");
661 g_assert (array_of_bytestrings[2] == NULL);
663 data->received_test_signal = TRUE;
664 g_main_loop_quit (data->thread_loop);
668 on_property_cancellation_cb (FooBar *proxy,
672 ClientData *data = user_data;
674 GError *error = NULL;
677 ret = foo_bar_call_property_cancellation_finish (proxy, res, &error);
678 g_assert_no_error (error);
681 g_main_loop_quit (data->thread_loop);
685 check_bar_proxy (FooBar *proxy,
686 GMainLoop *thread_loop)
689 gboolean ret_val_boolean;
690 gint16 ret_val_int16;
691 guint16 ret_val_uint16;
693 guint ret_val_uint32;
694 gint64 ret_val_int64;
695 guint64 ret_val_uint64;
696 gdouble ret_val_double;
697 gchar *ret_val_string;
698 gchar *ret_val_objpath;
699 gchar *ret_val_signature;
700 gchar *ret_val_bytestring;
726 gchar **val_unset_as;
727 GVariant *val_unset_ao;
728 GVariant *val_unset_ag;
729 GVariant *val_unset_struct;
730 gchar *val_finally_normal_name;
734 data = g_new0 (ClientData, 1);
735 data->thread_loop = thread_loop;
737 v = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "y");
738 g_assert (v != NULL);
741 /* set empty values to non-empty */
751 val_unset_struct = NULL;
752 /* check properties */
770 "unset_i", &val_unset_i,
771 "unset_d", &val_unset_d,
772 "unset_s", &val_unset_s,
773 "unset_o", &val_unset_o,
774 "unset_g", &val_unset_g,
775 "unset_ay", &val_unset_ay,
776 "unset_as", &val_unset_as,
777 "unset_ao", &val_unset_ao,
778 "unset_ag", &val_unset_ag,
779 "unset_struct", &val_unset_struct,
780 "finally-normal-name", &val_finally_normal_name,
782 g_assert_cmpint (val_y, ==, 42);
783 g_assert_cmpstr (val_finally_normal_name, ==, "There aint no place like home");
787 g_assert_cmpstr (val_ay, ==, "ABCabc");
790 g_variant_unref (val_ao);
791 g_variant_unref (val_ag);
792 g_free (val_finally_normal_name);
793 /* check empty values */
794 g_assert_cmpint (val_unset_i, ==, 0);
795 g_assert_cmpfloat (val_unset_d, ==, 0.0);
796 g_assert_cmpstr (val_unset_s, ==, "");
797 g_assert_cmpstr (val_unset_o, ==, "/");
798 g_assert_cmpstr (val_unset_g, ==, "");
799 g_free (val_unset_s);
800 g_free (val_unset_o);
801 g_free (val_unset_g);
802 g_assert_cmpstr (val_unset_ay, ==, "");
803 g_assert (val_unset_as[0] == NULL);
804 g_assert (g_variant_is_of_type (val_unset_ao, G_VARIANT_TYPE ("ao")));
805 g_assert (g_variant_is_of_type (val_unset_ag, G_VARIANT_TYPE ("ag")));
806 g_assert (g_variant_is_of_type (val_unset_struct, G_VARIANT_TYPE ("(idsogayasaoag)")));
807 s = g_variant_print (val_unset_struct, TRUE);
808 g_assert_cmpstr (s, ==, "(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])");
810 g_free (val_unset_ay);
811 g_strfreev (val_unset_as);
812 g_variant_unref (val_unset_ao);
813 g_variant_unref (val_unset_ag);
814 g_variant_unref (val_unset_struct);
816 /* Try setting a property. This causes the generated glue to invoke
817 * the org.fd.DBus.Properties.Set() method asynchronously. So we
818 * have to wait for properties-changed...
820 foo_bar_set_finally_normal_name (proxy, "foo!");
821 _g_assert_property_notify (proxy, "finally-normal-name");
822 g_assert_cmpstr (foo_bar_get_finally_normal_name (proxy), ==, "foo!");
824 /* Try setting properties that requires memory management. This
825 * is to exercise the paths that frees the references.
827 const gchar *array_of_strings[3] = {"one", "two", NULL};
828 const gchar *array_of_bytestrings[3] = {"one\xff", "two\xff", NULL};
833 "ay", g_variant_new_parsed ("[byte 0x65, 0x67]"),
834 "as", array_of_strings,
835 "ao", g_variant_new_parsed ("[@o '/one', '/one/two']"),
836 "ag", g_variant_new_parsed ("[@g 'ass', 'git']"),
840 ret = foo_bar_call_test_primitive_types_sync (proxy,
867 NULL, /* GCancellable */
869 g_assert_no_error (error);
873 ret = foo_bar_call_test_non_primitive_types_sync (proxy,
874 g_variant_new_parsed ("{'one': 'red',"
876 g_variant_new_parsed ("{'first': (42, 42), "
877 "'second': (43, 43)}"),
878 g_variant_new_parsed ("(42, 'foo', 'bar')"),
880 g_variant_new_parsed ("[@o '/one', '/one/two']"),
881 g_variant_new_parsed ("[@g 'ass', 'git']"),
882 array_of_bytestrings,
884 NULL, /* GCancellable */
887 g_assert_no_error (error);
890 /* Check that org.freedesktop.DBus.Error.UnknownMethod is returned on
891 * unimplemented methods.
894 ret = foo_bar_call_unimplemented_method_sync (proxy, NULL /* GCancellable */, &error);
895 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
896 g_error_free (error);
900 g_signal_connect (proxy,
902 G_CALLBACK (on_test_signal),
905 ret = foo_bar_call_request_signal_emission_sync (proxy, 0, NULL, &error);
906 g_assert_no_error (error);
909 g_assert (!data->received_test_signal);
910 g_main_loop_run (thread_loop);
911 g_assert (data->received_test_signal);
913 /* Try setting a property. This causes the generated glue to invoke
914 * the org.fd.DBus.Properties.Set() method asynchronously. So we
915 * have to wait for properties-changed...
917 foo_bar_set_finally_normal_name (proxy, "hey back!");
918 _g_assert_property_notify (proxy, "finally-normal-name");
919 g_assert_cmpstr (foo_bar_get_finally_normal_name (proxy), ==, "hey back!");
921 /* Check that grouping changes in idle works.
923 * See on_handle_request_multi_property_mods(). The server should
924 * emit exactly two PropertiesChanged signals each containing two
927 * On the first reception, y and i should both be increased by
928 * two. On the the second reception, only by one. The signal handler
931 * This also checks that _drain_notify() works.
933 data->initial_y = foo_bar_get_y (proxy);
934 data->initial_i = foo_bar_get_i (proxy);
935 g_signal_connect (proxy,
936 "g-properties-changed",
937 G_CALLBACK (on_g_properties_changed),
940 ret = foo_bar_call_request_multi_property_mods_sync (proxy, NULL, &error);
941 g_assert_no_error (error);
943 g_main_loop_run (thread_loop);
944 g_assert_cmpint (data->num_g_properties_changed, ==, 2);
945 g_signal_handlers_disconnect_by_func (proxy,
946 G_CALLBACK (on_g_properties_changed),
949 /* Check that we don't emit PropertiesChanged() if the property
950 * didn't change... we actually get two notifies.. one for the
951 * local set (without a value change) and one when receiving
952 * the PropertiesChanged() signal generated from the remote end.
954 g_assert_cmpint (data->num_notify_u, ==, 0);
955 g_signal_connect (proxy,
957 G_CALLBACK (on_notify_u),
959 foo_bar_set_u (proxy, 1042);
960 g_assert_cmpint (data->num_notify_u, ==, 1);
961 g_assert_cmpint (foo_bar_get_u (proxy), ==, 0);
962 _g_assert_property_notify (proxy, "u");
963 g_assert_cmpint (foo_bar_get_u (proxy), ==, 1042);
964 g_assert_cmpint (data->num_notify_u, ==, 2);
966 /* Now change u again to the same value.. this will cause a
967 * local notify:: notify and the usual Properties.Set() call
969 * (Btw, why also the Set() call if the value in the cache is
970 * the same? Because someone else might have changed it
971 * in the mean time and we're just waiting to receive the
972 * PropertiesChanged() signal...)
974 * More tricky - how do we check for the *absence* of the
975 * notification that u changed? Simple: we change another
976 * property and wait for that PropertiesChanged() message
979 foo_bar_set_u (proxy, 1042);
980 g_assert_cmpint (data->num_notify_u, ==, 3);
982 g_assert_cmpint (data->num_notify_n, ==, 0);
983 g_signal_connect (proxy,
985 G_CALLBACK (on_notify_n),
987 foo_bar_set_n (proxy, 10042);
988 g_assert_cmpint (data->num_notify_n, ==, 1);
989 g_assert_cmpint (foo_bar_get_n (proxy), ==, 0);
990 _g_assert_property_notify (proxy, "n");
991 g_assert_cmpint (foo_bar_get_n (proxy), ==, 10042);
992 g_assert_cmpint (data->num_notify_n, ==, 2);
993 /* Checks that u didn't change at all */
994 g_assert_cmpint (data->num_notify_u, ==, 3);
996 /* Now we check that if the service does
998 * guint n = foo_bar_get_n (foo);
999 * foo_bar_set_n (foo, n + 1);
1000 * foo_bar_set_n (foo, n);
1002 * then no PropertiesChanged() signal is emitted!
1005 foo_bar_call_property_cancellation (proxy,
1006 NULL, /* GCancellable */
1007 (GAsyncReadyCallback) on_property_cancellation_cb,
1009 g_main_loop_run (thread_loop);
1010 /* Checks that n didn't change at all */
1011 g_assert_cmpint (data->num_notify_n, ==, 2);
1017 /* ---------------------------------------------------------------------------------------------------- */
1020 on_force_signal (FooBat *proxy,
1024 GVariant *force_struct,
1027 gboolean *signal_received = user_data;
1030 g_assert (!(*signal_received));
1032 g_assert_cmpint (g_variant_get_int32 (force_i), ==, 42 + 10);
1033 g_assert_cmpstr (g_variant_get_string (force_s, NULL), ==, "a string_foo");
1034 g_assert_cmpstr (g_variant_get_bytestring (force_ay), ==, "a bytestring\xff_foo\xff");
1035 g_variant_get (force_struct, "(i)", &val);
1036 g_assert_cmpint (val, ==, 4200 + 10);
1038 *signal_received = TRUE;
1042 check_bat_proxy (FooBat *proxy,
1043 GMainLoop *thread_loop)
1049 GVariant *ret_struct;
1051 gboolean force_signal_received;
1053 /* --------------------------------------------------- */
1054 /* Check type-mapping where we force use of a GVariant */
1055 /* --------------------------------------------------- */
1057 /* check properties */
1058 g_object_get (proxy,
1061 "force-ay", &ret_ay,
1062 "force-struct", &ret_struct,
1064 g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 43);
1065 g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "prop string");
1066 g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "prop bytestring\xff");
1067 g_variant_get (ret_struct, "(i)", &val);
1068 g_assert_cmpint (val, ==, 4300);
1069 g_variant_unref (ret_i);
1070 g_variant_unref (ret_s);
1071 g_variant_unref (ret_ay);
1072 g_variant_unref (ret_struct);
1074 /* check method and signal */
1075 force_signal_received = FALSE;
1076 g_signal_connect (proxy,
1078 G_CALLBACK (on_force_signal),
1079 &force_signal_received);
1082 foo_bat_call_force_method_sync (proxy,
1083 g_variant_new_int32 (42),
1084 g_variant_new_string ("a string"),
1085 g_variant_new_bytestring ("a bytestring\xff"),
1086 g_variant_new ("(i)", 4200),
1091 NULL, /* GCancellable* */
1093 g_assert_no_error (error);
1094 g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 42 + 10);
1095 g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "a string_foo");
1096 g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "a bytestring\xff_foo\xff");
1097 g_variant_get (ret_struct, "(i)", &val);
1098 g_assert_cmpint (val, ==, 4200 + 10);
1099 g_variant_unref (ret_i);
1100 g_variant_unref (ret_s);
1101 g_variant_unref (ret_ay);
1102 g_variant_unref (ret_struct);
1103 _g_assert_signal_received (proxy, "force-signal");
1104 g_assert (force_signal_received);
1107 /* ---------------------------------------------------------------------------------------------------- */
1110 check_authorize_proxy (FooAuthorize *proxy,
1111 GMainLoop *thread_loop)
1116 /* Check that g-authorize-method works as intended */
1119 ret = foo_authorize_call_check_not_authorized_sync (proxy, NULL, &error);
1120 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1121 g_error_free (error);
1125 ret = foo_authorize_call_check_authorized_sync (proxy, NULL, &error);
1126 g_assert_no_error (error);
1130 ret = foo_authorize_call_check_not_authorized_from_object_sync (proxy, NULL, &error);
1131 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
1132 g_error_free (error);
1136 /* ---------------------------------------------------------------------------------------------------- */
1139 get_self_via_proxy (FooMethodThreads *proxy_1)
1147 ret = foo_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
1148 g_assert_no_error (error);
1151 g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
1159 check_thread_proxies (FooMethodThreads *proxy_1,
1160 FooMethodThreads *proxy_2,
1161 GMainLoop *thread_loop)
1163 /* proxy_1 is indeed using threads so should never get the handler thread */
1164 g_assert (get_self_via_proxy (proxy_1) != method_handler_thread);
1166 /* proxy_2 is not using threads so should get the handler thread */
1167 g_assert (get_self_via_proxy (proxy_2) == method_handler_thread);
1170 /* ---------------------------------------------------------------------------------------------------- */
1173 check_proxies_in_thread (gpointer user_data)
1175 GMainLoop *loop = user_data;
1176 GMainContext *thread_context;
1177 GMainLoop *thread_loop;
1181 FooAuthorize *authorize_proxy;
1182 FooMethodThreads *thread_proxy_1;
1183 FooMethodThreads *thread_proxy_2;
1185 thread_context = g_main_context_new ();
1186 thread_loop = g_main_loop_new (thread_context, FALSE);
1187 g_main_context_push_thread_default (thread_context);
1189 /* Check the object */
1191 bar_proxy = foo_bar_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1192 G_DBUS_PROXY_FLAGS_NONE,
1193 "org.gtk.GDBus.BindingsTool.Test",
1195 NULL, /* GCancellable* */
1197 check_bar_proxy (bar_proxy, thread_loop);
1198 g_assert_no_error (error);
1199 g_object_unref (bar_proxy);
1202 bat_proxy = foo_bat_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1203 G_DBUS_PROXY_FLAGS_NONE,
1204 "org.gtk.GDBus.BindingsTool.Test",
1206 NULL, /* GCancellable* */
1208 check_bat_proxy (bat_proxy, thread_loop);
1209 g_assert_no_error (error);
1210 g_object_unref (bat_proxy);
1213 authorize_proxy = foo_authorize_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1214 G_DBUS_PROXY_FLAGS_NONE,
1215 "org.gtk.GDBus.BindingsTool.Test",
1217 NULL, /* GCancellable* */
1219 check_authorize_proxy (authorize_proxy, thread_loop);
1220 g_assert_no_error (error);
1221 g_object_unref (authorize_proxy);
1224 thread_proxy_1 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1225 G_DBUS_PROXY_FLAGS_NONE,
1226 "org.gtk.GDBus.BindingsTool.Test",
1227 "/method_threads_1",
1228 NULL, /* GCancellable* */
1230 g_assert_no_error (error);
1231 thread_proxy_2 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1232 G_DBUS_PROXY_FLAGS_NONE,
1233 "org.gtk.GDBus.BindingsTool.Test",
1234 "/method_threads_2",
1235 NULL, /* GCancellable* */
1237 g_assert_no_error (error);
1238 check_thread_proxies (thread_proxy_1, thread_proxy_2, thread_loop);
1239 g_object_unref (thread_proxy_1);
1240 g_object_unref (thread_proxy_2);
1242 g_main_loop_unref (thread_loop);
1243 g_main_context_unref (thread_context);
1245 /* this breaks out of the loop in main() (below) */
1246 g_main_loop_quit (loop);
1250 /* ---------------------------------------------------------------------------------------------------- */
1259 introspect_cb (GDBusConnection *connection,
1263 IntrospectData *data = user_data;
1268 result = g_dbus_connection_call_finish (connection,
1271 g_assert_no_error (error);
1272 g_assert (result != NULL);
1273 g_variant_get (result, "(s)", &data->xml);
1274 g_variant_unref (result);
1276 g_main_loop_quit (data->loop);
1279 static GDBusNodeInfo *
1280 introspect (GDBusConnection *connection,
1282 const gchar *object_path,
1286 GDBusNodeInfo *node_info;
1287 IntrospectData *data;
1289 data = g_new0 (IntrospectData, 1);
1293 /* do this async to avoid deadlocks */
1294 g_dbus_connection_call (connection,
1297 "org.freedesktop.DBus.Introspectable",
1300 G_VARIANT_TYPE ("(s)"),
1301 G_DBUS_CALL_FLAGS_NONE,
1304 (GAsyncReadyCallback) introspect_cb,
1306 g_main_loop_run (loop);
1307 g_assert (data->xml != NULL);
1310 node_info = g_dbus_node_info_new_for_xml (data->xml, &error);
1311 g_assert_no_error (error);
1312 g_assert (node_info != NULL);
1320 count_interfaces (GDBusNodeInfo *info)
1323 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1329 count_nodes (GDBusNodeInfo *info)
1332 for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
1338 has_interface (GDBusNodeInfo *info,
1342 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1344 if (g_strcmp0 (info->interfaces[n]->name, name) == 0)
1350 /* ---------------------------------------------------------------------------------------------------- */
1355 } OMGetManagedObjectsData;
1358 om_get_all_cb (GDBusConnection *connection,
1362 OMGetManagedObjectsData *data = user_data;
1366 data->result = g_dbus_connection_call_finish (connection,
1369 g_assert_no_error (error);
1370 g_assert (data->result != NULL);
1371 g_main_loop_quit (data->loop);
1375 om_check_get_all (GDBusConnection *c,
1379 OMGetManagedObjectsData data;
1385 /* do this async to avoid deadlocks */
1386 g_dbus_connection_call (c,
1387 g_dbus_connection_get_unique_name (c),
1389 "org.freedesktop.DBus.ObjectManager",
1390 "GetManagedObjects",
1392 G_VARIANT_TYPE ("(a{oa{sa{sv}}})"),
1393 G_DBUS_CALL_FLAGS_NONE,
1396 (GAsyncReadyCallback) om_get_all_cb,
1398 g_main_loop_run (loop);
1399 g_assert (data.result != NULL);
1400 s = g_variant_print (data.result, TRUE);
1401 g_assert_cmpstr (s, ==, str);
1403 g_variant_unref (data.result);
1411 guint num_object_proxy_added_signals;
1412 guint num_object_proxy_removed_signals;
1413 guint num_interface_added_signals;
1414 guint num_interface_removed_signals;
1418 my_pstrcmp (const gchar **a, const gchar **b)
1420 return g_strcmp0 (*a, *b);
1424 om_check_interfaces_added (const gchar *signal_name,
1425 GVariant *parameters,
1426 const gchar *object_path,
1427 const gchar *first_interface_name,
1433 GPtrArray *interfaces;
1434 GPtrArray *interfaces_in_message;
1438 interfaces = g_ptr_array_new ();
1439 g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1440 va_start (var_args, first_interface_name);
1443 str = va_arg (var_args, const gchar *);
1446 g_ptr_array_add (interfaces, (gpointer) str);
1451 g_variant_get (parameters, "(&o*)", &path, &array);
1452 g_assert_cmpstr (signal_name, ==, "InterfacesAdded");
1453 g_assert_cmpstr (path, ==, object_path);
1454 g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1455 interfaces_in_message = g_ptr_array_new ();
1456 for (n = 0; n < interfaces->len; n++)
1458 const gchar *iface_name;
1459 g_variant_get_child (array, n, "{&sa{sv}}", &iface_name, NULL);
1460 g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1462 g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1463 g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1464 g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1465 for (n = 0; n < interfaces->len; n++)
1466 g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1467 g_ptr_array_unref (interfaces_in_message);
1468 g_ptr_array_unref (interfaces);
1469 g_variant_unref (array);
1473 om_check_interfaces_removed (const gchar *signal_name,
1474 GVariant *parameters,
1475 const gchar *object_path,
1476 const gchar *first_interface_name,
1482 GPtrArray *interfaces;
1483 GPtrArray *interfaces_in_message;
1487 interfaces = g_ptr_array_new ();
1488 g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1489 va_start (var_args, first_interface_name);
1492 str = va_arg (var_args, const gchar *);
1495 g_ptr_array_add (interfaces, (gpointer) str);
1500 g_variant_get (parameters, "(&o*)", &path, &array);
1501 g_assert_cmpstr (signal_name, ==, "InterfacesRemoved");
1502 g_assert_cmpstr (path, ==, object_path);
1503 g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1504 interfaces_in_message = g_ptr_array_new ();
1505 for (n = 0; n < interfaces->len; n++)
1507 const gchar *iface_name;
1508 g_variant_get_child (array, n, "&s", &iface_name, NULL);
1509 g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1511 g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1512 g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1513 g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1514 for (n = 0; n < interfaces->len; n++)
1515 g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1516 g_ptr_array_unref (interfaces_in_message);
1517 g_ptr_array_unref (interfaces);
1518 g_variant_unref (array);
1522 om_on_signal (GDBusConnection *connection,
1523 const gchar *sender_name,
1524 const gchar *object_path,
1525 const gchar *interface_name,
1526 const gchar *signal_name,
1527 GVariant *parameters,
1530 OMData *om_data = user_data;
1532 //g_debug ("foo: %s", g_variant_print (parameters, TRUE));
1534 switch (om_data->state)
1538 g_print ("failing and om_data->state=%d on signal %s, params=%s\n",
1541 g_variant_print (parameters, TRUE));
1542 g_assert_not_reached ();
1546 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1547 "org.project.Bar", NULL);
1549 g_main_loop_quit (om_data->loop);
1553 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1554 "org.project.Bar", NULL);
1556 /* keep running the loop */
1560 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1561 "org.project.Bar", NULL);
1563 g_main_loop_quit (om_data->loop);
1567 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1568 "org.project.Bar", NULL);
1570 /* keep running the loop */
1574 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1575 "org.project.Bar", NULL);
1576 om_data->state = 10;
1577 g_main_loop_quit (om_data->loop);
1581 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1582 "org.project.Bat", NULL);
1583 om_data->state = 12;
1584 g_main_loop_quit (om_data->loop);
1588 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1589 "org.project.Bar", NULL);
1590 om_data->state = 14;
1591 g_main_loop_quit (om_data->loop);
1595 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1596 "org.project.Bat", NULL);
1597 om_data->state = 16;
1598 g_main_loop_quit (om_data->loop);
1602 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1603 "com.acme.Coyote", NULL);
1604 om_data->state = 18;
1605 g_main_loop_quit (om_data->loop);
1609 om_check_interfaces_added (signal_name, parameters, "/managed/second",
1610 "org.project.Bat", "org.project.Bar", NULL);
1611 om_data->state = 102;
1612 g_main_loop_quit (om_data->loop);
1616 om_check_interfaces_removed (signal_name, parameters, "/managed/second",
1617 "org.project.Bat", "org.project.Bar", NULL);
1618 om_data->state = 104;
1619 g_main_loop_quit (om_data->loop);
1624 static GAsyncResult *om_res = NULL;
1627 om_pm_start_cb (FooObjectManagerClient *manager,
1631 GMainLoop *loop = user_data;
1632 om_res = g_object_ref (res);
1633 g_main_loop_quit (loop);
1637 on_interface_added (GDBusObject *object,
1638 GDBusInterface *interface,
1641 OMData *om_data = user_data;
1642 om_data->num_interface_added_signals += 1;
1646 on_interface_removed (GDBusObject *object,
1647 GDBusInterface *interface,
1650 OMData *om_data = user_data;
1651 om_data->num_interface_removed_signals += 1;
1655 on_object_proxy_added (GDBusObjectManagerClient *manager,
1656 GDBusObjectProxy *object_proxy,
1659 OMData *om_data = user_data;
1660 om_data->num_object_proxy_added_signals += 1;
1661 g_signal_connect (object_proxy,
1663 G_CALLBACK (on_interface_added),
1665 g_signal_connect (object_proxy,
1666 "interface-removed",
1667 G_CALLBACK (on_interface_removed),
1672 on_object_proxy_removed (GDBusObjectManagerClient *manager,
1673 GDBusObjectProxy *object_proxy,
1676 OMData *om_data = user_data;
1677 om_data->num_object_proxy_removed_signals += 1;
1678 g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1679 G_CALLBACK (on_interface_added),
1681 g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1682 G_CALLBACK (on_interface_removed),
1687 om_check_property_and_signal_emission (GMainLoop *loop,
1691 /* First PropertiesChanged */
1692 g_assert_cmpint (foo_bar_get_i (skeleton), ==, 0);
1693 g_assert_cmpint (foo_bar_get_i (proxy), ==, 0);
1694 foo_bar_set_i (skeleton, 1);
1695 _g_assert_property_notify (proxy, "i");
1696 g_assert_cmpint (foo_bar_get_i (skeleton), ==, 1);
1697 g_assert_cmpint (foo_bar_get_i (proxy), ==, 1);
1699 /* Then just a regular signal */
1700 foo_bar_emit_another_signal (skeleton, "word");
1701 _g_assert_signal_received (proxy, "another-signal");
1705 check_object_manager (void)
1707 FooObjectSkeleton *o;
1708 FooObjectSkeleton *o2;
1709 GDBusInterfaceSkeleton *i;
1711 GDBusObjectManagerServer *manager;
1712 GDBusNodeInfo *info;
1717 GDBusObjectManager *pm;
1718 GList *object_proxies;
1722 FooBar *bar_skeleton;
1724 loop = g_main_loop_new (NULL, FALSE);
1726 om_data = g_new0 (OMData, 1);
1727 om_data->loop = loop;
1731 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1732 g_assert_no_error (error);
1733 g_assert (c != NULL);
1735 om_signal_id = g_dbus_connection_signal_subscribe (c,
1737 "org.freedesktop.DBus.ObjectManager",
1739 NULL, /* object_path */
1741 G_DBUS_SIGNAL_FLAGS_NONE,
1744 NULL); /* user_data_free_func */
1746 /* Our GDBusObjectManagerClient tests are simple - we basically just count the
1747 * number of times the various signals have been emitted (we don't check
1748 * that the right objects/interfaces are passed though - that's checked
1749 * in the lower-level tests in om_on_signal()...)
1751 * Note that these tests rely on the D-Bus signal handlers used by
1752 * GDBusObjectManagerClient firing before om_on_signal().
1755 pm = foo_object_manager_client_new_sync (c,
1756 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1757 g_dbus_connection_get_unique_name (c),
1759 NULL, /* GCancellable */
1761 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
1762 g_error_free (error);
1763 g_assert (pm == NULL);
1765 manager = g_dbus_object_manager_server_new (c, "/managed");
1767 /* Check that the manager object is visible */
1768 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
1769 g_assert_cmpint (count_interfaces (info), ==, 4); /* ObjectManager + Properties,Introspectable,Peer */
1770 g_assert (has_interface (info, "org.freedesktop.DBus.ObjectManager"));
1771 g_assert_cmpint (count_nodes (info), ==, 0);
1772 g_dbus_node_info_unref (info);
1774 /* Check GetManagedObjects() - should be empty since we have no objects */
1775 om_check_get_all (c, loop,
1776 "(@a{oa{sa{sv}}} {},)");
1778 /* Now try to create the the proxy manager again - this time it should work */
1780 foo_object_manager_client_new (c,
1781 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1782 g_dbus_connection_get_unique_name (c),
1784 NULL, /* GCancellable */
1785 (GAsyncReadyCallback) om_pm_start_cb,
1787 g_main_loop_run (loop);
1789 pm = foo_object_manager_client_new_finish (om_res, &error);
1790 g_object_unref (om_res);
1791 g_assert_no_error (error);
1792 g_assert (pm != NULL);
1793 g_signal_connect (pm,
1795 G_CALLBACK (on_object_proxy_added),
1797 g_signal_connect (pm,
1799 G_CALLBACK (on_object_proxy_removed),
1801 /* ... check there are no object proxies yet */
1802 object_proxies = g_dbus_object_manager_get_objects (pm);
1803 g_assert (object_proxies == NULL);
1805 /* First, export an object with a single interface (also check that
1806 * g_dbus_interface_get_object() works and that the object isn't reffed)
1808 o = foo_object_skeleton_new ("/managed/first");
1809 i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1810 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1811 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1812 foo_object_skeleton_set_bar (o, FOO_BAR (i));
1813 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1814 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1815 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1816 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1817 foo_object_skeleton_set_bar (o, NULL);
1818 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1819 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1820 foo_object_skeleton_set_bar (o, FOO_BAR (i));
1821 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1822 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1823 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o));
1825 /* ... check we get the InterfacesAdded signal */
1827 g_main_loop_run (om_data->loop);
1828 g_assert_cmpint (om_data->state, ==, 2);
1829 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 1);
1830 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 0);
1831 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1832 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1833 /* ... check there's one non-standard interfaces */
1834 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1835 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1836 g_assert (has_interface (info, "org.project.Bar"));
1837 g_dbus_node_info_unref (info);
1839 /* Now, check adding the same interface replaces the existing one */
1840 foo_object_skeleton_set_bar (o, FOO_BAR (i));
1841 /* ... check we get the InterfacesRemoved */
1843 g_main_loop_run (om_data->loop);
1844 /* ... and then check we get the InterfacesAdded */
1845 g_assert_cmpint (om_data->state, ==, 6);
1846 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 2);
1847 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 1);
1848 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1849 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1850 /* ... check introspection data */
1851 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1852 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1853 g_assert (has_interface (info, "org.project.Bar"));
1854 g_dbus_node_info_unref (info);
1857 /* check adding an interface of same type (but not same object) replaces the existing one */
1858 i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1859 foo_object_skeleton_set_bar (o, FOO_BAR (i));
1860 /* ... check we get the InterfacesRemoved and then InterfacesAdded */
1862 g_main_loop_run (om_data->loop);
1863 g_assert_cmpint (om_data->state, ==, 10);
1864 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1865 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1866 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1867 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1868 /* ... check introspection data */
1869 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1870 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1871 g_assert (has_interface (info, "org.project.Bar"));
1872 g_dbus_node_info_unref (info);
1875 /* check adding an interface of another type doesn't replace the existing one */
1876 i = G_DBUS_INTERFACE_SKELETON (foo_bat_skeleton_new ());
1877 foo_object_skeleton_set_bat (o, FOO_BAT (i));
1879 /* ... check we get the InterfacesAdded */
1880 om_data->state = 11;
1881 g_main_loop_run (om_data->loop);
1882 g_assert_cmpint (om_data->state, ==, 12);
1883 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1884 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1885 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1886 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1887 /* ... check introspection data */
1888 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1889 g_assert_cmpint (count_interfaces (info), ==, 5); /* Bar,Bat + Properties,Introspectable,Peer */
1890 g_assert (has_interface (info, "org.project.Bar"));
1891 g_assert (has_interface (info, "org.project.Bat"));
1892 g_dbus_node_info_unref (info);
1894 /* check we can remove an interface */
1895 foo_object_skeleton_set_bar (o, NULL);
1896 /* ... check we get the InterfacesRemoved */
1897 om_data->state = 13;
1898 g_main_loop_run (om_data->loop);
1899 g_assert_cmpint (om_data->state, ==, 14);
1900 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1901 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1902 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1903 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1904 /* ... check introspection data */
1905 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1906 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
1907 g_assert (has_interface (info, "org.project.Bat"));
1908 g_dbus_node_info_unref (info);
1909 /* also and that the call only has effect if the interface actually exists
1911 * (Note: if a signal was emitted we'd assert in the signal handler
1912 * because we're in state 14)
1914 foo_object_skeleton_set_bar (o, NULL);
1915 /* ... check introspection data */
1916 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1917 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
1918 g_assert (has_interface (info, "org.project.Bat"));
1919 g_dbus_node_info_unref (info);
1921 /* remove the last interface */
1922 foo_object_skeleton_set_bat (o, NULL);
1923 /* ... check we get the InterfacesRemoved */
1924 om_data->state = 15;
1925 g_main_loop_run (om_data->loop);
1926 g_assert_cmpint (om_data->state, ==, 16);
1927 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1928 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1929 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1930 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1931 /* ... check introspection data */
1932 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1933 g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
1934 g_dbus_node_info_unref (info);
1936 /* and add an interface again */
1937 i = G_DBUS_INTERFACE_SKELETON (foo_com_acme_coyote_skeleton_new ());
1938 foo_object_skeleton_set_com_acme_coyote (o, FOO_COM_ACME_COYOTE (i));
1940 /* ... check we get the InterfacesAdded */
1941 om_data->state = 17;
1942 g_main_loop_run (om_data->loop);
1943 g_assert_cmpint (om_data->state, ==, 18);
1944 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 4);
1945 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1946 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1947 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1948 /* ... check introspection data */
1949 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1950 g_assert_cmpint (count_interfaces (info), ==, 4); /* com.acme.Coyote + Properties,Introspectable,Peer */
1951 g_assert (has_interface (info, "com.acme.Coyote"));
1952 g_dbus_node_info_unref (info);
1954 /* Check GetManagedObjects() - should be just the Coyote */
1955 om_check_get_all (c, loop,
1956 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
1958 /* -------------------------------------------------- */
1960 /* create a new object with two interfaces */
1961 o2 = foo_object_skeleton_new ("/managed/second");
1962 i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1963 bar_skeleton = FOO_BAR (i); /* save for later test */
1964 foo_object_skeleton_set_bar (o2, FOO_BAR (i));
1966 i = G_DBUS_INTERFACE_SKELETON (foo_bat_skeleton_new ());
1967 foo_object_skeleton_set_bat (o2, FOO_BAT (i));
1970 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o2));
1971 /* ... check we get the InterfacesAdded with _two_ interfaces */
1972 om_data->state = 101;
1973 g_main_loop_run (om_data->loop);
1974 g_assert_cmpint (om_data->state, ==, 102);
1975 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
1976 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1977 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1978 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1980 /* -------------------------------------------------- */
1982 /* Now that we have a couple of objects with interfaces, check
1983 * that ObjectManager.GetManagedObjects() works
1985 om_check_get_all (c, loop,
1986 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/second': {'org.project.Bar': {'y': <byte 0x00>, 'b': <false>, 'n': <int16 0>, 'q': <uint16 0>, 'i': <0>, 'u': <uint32 0>, 'x': <int64 0>, 't': <uint64 0>, 'd': <0.0>, 's': <''>, 'o': <objectpath '/'>, 'g': <signature ''>, 'ay': <b''>, 'as': <@as []>, 'aay': <@aay []>, 'ao': <@ao []>, 'ag': <@ag []>, 'FinallyNormalName': <''>, 'ReadonlyProperty': <''>, 'unset_i': <0>, 'unset_d': <0.0>, 'unset_s': <''>, 'unset_o': <objectpath '/'>, 'unset_g': <signature ''>, 'unset_ay': <b''>, 'unset_as': <@as []>, 'unset_ao': <@ao []>, 'unset_ag': <@ag []>, 'unset_struct': <(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])>}, 'org.project.Bat': {'force_i': <0>, 'force_s': <''>, 'force_ay': <@ay []>, 'force_struct': <(0,)>}}},)");
1988 /* Also check that the ObjectManagerClient returns these objects - and
1989 * that they are of the right GType cf. what was requested via
1990 * the generated ::get-proxy-type signal handler
1992 object_proxies = g_dbus_object_manager_get_objects (pm);
1993 g_assert (g_list_length (object_proxies) == 2);
1994 g_list_foreach (object_proxies, (GFunc) g_object_unref, NULL);
1995 g_list_free (object_proxies);
1996 op = g_dbus_object_manager_get_object (pm, "/managed/first");
1997 g_assert (op != NULL);
1998 g_assert (FOO_IS_OBJECT_PROXY (op));
1999 g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/first");
2000 proxies = g_dbus_object_get_interfaces (op);
2001 g_assert (g_list_length (proxies) == 1);
2002 g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
2003 g_list_free (proxies);
2004 p = G_DBUS_PROXY (foo_object_get_com_acme_coyote (FOO_OBJECT (op)));
2005 g_assert (p != NULL);
2006 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_COM_ACME_COYOTE_PROXY);
2007 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_COM_ACME_COYOTE));
2009 p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2010 g_assert (p == NULL);
2011 g_object_unref (op);
2013 op = g_dbus_object_manager_get_object (pm, "/managed/second");
2014 g_assert (op != NULL);
2015 g_assert (FOO_IS_OBJECT_PROXY (op));
2016 g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/second");
2017 proxies = g_dbus_object_get_interfaces (op);
2018 g_assert (g_list_length (proxies) == 2);
2019 g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
2020 g_list_free (proxies);
2021 p = G_DBUS_PROXY (foo_object_get_bat (FOO_OBJECT (op)));
2022 g_assert (p != NULL);
2023 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_BAT_PROXY);
2024 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_BAT));
2026 p = G_DBUS_PROXY (foo_object_get_bar (FOO_OBJECT (op)));
2027 g_assert (p != NULL);
2028 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_BAR_PROXY);
2029 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_BAR));
2030 /* ... now that we have a Bar instance around, also check that we get signals
2031 * and property changes...
2033 om_check_property_and_signal_emission (loop, bar_skeleton, FOO_BAR (p));
2035 p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2036 g_assert (p == NULL);
2037 g_object_unref (op);
2039 /* -------------------------------------------------- */
2041 /* Now remove the second object added above */
2042 g_dbus_object_manager_server_unexport (manager, "/managed/second");
2043 /* ... check we get InterfacesRemoved with both interfaces */
2044 om_data->state = 103;
2045 g_main_loop_run (om_data->loop);
2046 g_assert_cmpint (om_data->state, ==, 104);
2047 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2048 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 4);
2049 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2050 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2051 /* ... check introspection data (there should be nothing) */
2052 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/second", loop);
2053 g_assert_cmpint (count_nodes (info), ==, 0);
2054 g_assert_cmpint (count_interfaces (info), ==, 0);
2055 g_dbus_node_info_unref (info);
2057 /* Check GetManagedObjects() again */
2058 om_check_get_all (c, loop,
2059 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2061 //g_main_loop_run (loop); /* TODO: tmp */
2063 g_main_loop_unref (loop);
2065 g_dbus_connection_signal_unsubscribe (c, om_signal_id);
2066 g_object_unref (o2);
2068 g_object_unref (manager);
2069 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2070 G_CALLBACK (on_object_proxy_added),
2072 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2073 G_CALLBACK (on_object_proxy_removed),
2075 g_object_unref (pm);
2081 /* ---------------------------------------------------------------------------------------------------- */
2084 test_object_manager (void)
2089 loop = g_main_loop_new (NULL, FALSE);
2091 id = g_bus_own_name (G_BUS_TYPE_SESSION,
2092 "org.gtk.GDBus.BindingsTool.Test",
2093 G_BUS_NAME_OWNER_FLAGS_NONE,
2100 g_main_loop_run (loop);
2102 check_object_manager ();
2104 /* uncomment to keep the service around (to e.g. introspect it) */
2105 /* g_main_loop_run (loop); */
2107 g_bus_unown_name (id);
2108 g_main_loop_unref (loop);
2111 /* ---------------------------------------------------------------------------------------------------- */
2112 /* This checks that forcing names via org.gtk.GDBus.Name works (see test-codegen.xml) */
2114 extern gpointer name_forcing_1;
2115 extern gpointer name_forcing_2;
2116 extern gpointer name_forcing_3;
2117 extern gpointer name_forcing_4;
2118 extern gpointer name_forcing_5;
2119 extern gpointer name_forcing_6;
2120 extern gpointer name_forcing_7;
2121 gpointer name_forcing_1 = foo_rocket123_get_type;
2122 gpointer name_forcing_2 = foo_rocket123_call_ignite_xyz;
2123 gpointer name_forcing_3 = foo_rocket123_emit_exploded_xyz;
2124 gpointer name_forcing_4 = foo_rocket123_get_speed_xyz;
2125 gpointer name_forcing_5 = foo_test_ugly_case_interface_call_get_iscsi_servers;
2126 gpointer name_forcing_6 = foo_test_ugly_case_interface_emit_servers_updated_now;
2127 gpointer name_forcing_7 = foo_test_ugly_case_interface_get_ugly_name;
2129 /* ---------------------------------------------------------------------------------------------------- */
2131 /* See https://bugzilla.gnome.org/show_bug.cgi?id=647577#c5 for details */
2133 #define CHECK_FIELD(name, v1, v2) g_assert_cmpint (G_STRUCT_OFFSET (FooChangingInterface##v1##Iface, name), ==, G_STRUCT_OFFSET (FooChangingInterface##v2##Iface, name));
2136 test_interface_stability (void)
2138 CHECK_FIELD(handle_foo_method, V1, V2);
2139 CHECK_FIELD(handle_bar_method, V1, V2);
2140 CHECK_FIELD(handle_baz_method, V1, V2);
2141 CHECK_FIELD(foo_signal, V1, V2);
2142 CHECK_FIELD(bar_signal, V1, V2);
2143 CHECK_FIELD(baz_signal, V1, V2);
2144 CHECK_FIELD(handle_new_method_in2, V2, V10);
2145 CHECK_FIELD(new_signal_in2, V2, V10);
2150 /* ---------------------------------------------------------------------------------------------------- */
2159 g_test_init (&argc, &argv, NULL);
2161 /* all the tests use a session bus with a well-known address that we can bring up and down
2162 * using session_bus_up() and session_bus_down().
2164 g_unsetenv ("DISPLAY");
2165 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
2169 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
2170 * until one can connect to the bus but that's not how things work right now
2172 usleep (500 * 1000);
2174 g_test_add_func ("/gdbus/codegen/annotations", test_annotations);
2175 g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability);
2176 g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager);
2181 session_bus_down ();