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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
26 #include "gdbus-tests.h"
28 #include "gdbus-test-codegen-generated.h"
30 /* ---------------------------------------------------------------------------------------------------- */
33 count_annotations (GDBusAnnotationInfo **annotations)
37 while (annotations != NULL && annotations[ret] != NULL)
44 * - non-internal annotations are written out correctly; and
45 * - injection via --annotation --key --value works
48 test_annotations (void)
50 GDBusInterfaceInfo *iface;
51 GDBusMethodInfo *method;
52 GDBusSignalInfo *signal;
53 GDBusPropertyInfo *property;
55 iface = foo_igen_bar_interface_info ();
56 g_assert (iface != NULL);
58 /* see Makefile.am for where these annotations are injected */
59 g_assert_cmpint (count_annotations (iface->annotations), ==, 1);
60 g_assert_cmpstr (g_dbus_annotation_info_lookup (iface->annotations, "Key1"), ==, "Value1");
62 method = g_dbus_interface_info_lookup_method (iface, "HelloWorld");
63 g_assert (method != NULL);
64 g_assert_cmpint (count_annotations (method->annotations), ==, 2);
65 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->annotations, "ExistingAnnotation"), ==, "blah");
66 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->annotations, "Key3"), ==, "Value3");
68 signal = g_dbus_interface_info_lookup_signal (iface, "TestSignal");
69 g_assert (signal != NULL);
70 g_assert_cmpint (count_annotations (signal->annotations), ==, 1);
71 g_assert_cmpstr (g_dbus_annotation_info_lookup (signal->annotations, "Key4"), ==, "Value4");
72 g_assert_cmpstr (g_dbus_annotation_info_lookup (signal->args[1]->annotations, "Key8"), ==, "Value8");
74 property = g_dbus_interface_info_lookup_property (iface, "ay");
75 g_assert (property != NULL);
76 g_assert_cmpint (count_annotations (property->annotations), ==, 1);
77 g_assert_cmpstr (g_dbus_annotation_info_lookup (property->annotations, "Key5"), ==, "Value5");
79 method = g_dbus_interface_info_lookup_method (iface, "TestPrimitiveTypes");
80 g_assert (method != NULL);
81 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->in_args[4]->annotations, "Key6"), ==, "Value6");
82 g_assert_cmpstr (g_dbus_annotation_info_lookup (method->out_args[5]->annotations, "Key7"), ==, "Value7");
85 /* ---------------------------------------------------------------------------------------------------- */
88 on_handle_hello_world (FooiGenBar *object,
89 GDBusMethodInvocation *invocation,
90 const gchar *greeting,
94 response = g_strdup_printf ("Word! You said '%s'. I'm Skeleton, btw!", greeting);
95 foo_igen_bar_complete_hello_world (object, invocation, response);
101 on_handle_test_primitive_types (FooiGenBar *object,
102 GDBusMethodInvocation *invocation,
104 gboolean val_boolean,
112 const gchar *val_string,
113 const gchar *val_objpath,
114 const gchar *val_signature,
115 const gchar *val_bytestring,
121 s1 = g_strdup_printf ("Word! You said '%s'. Rock'n'roll!", val_string);
122 s2 = g_strdup_printf ("/modified%s", val_objpath);
123 s3 = g_strdup_printf ("assgit%s", val_signature);
124 foo_igen_bar_complete_test_primitive_types (object,
133 10000000 + val_uint64,
146 on_handle_test_non_primitive_types (FooiGenBar *object,
147 GDBusMethodInvocation *invocation,
148 GVariant *dict_s_to_s,
149 GVariant *dict_s_to_pairs,
151 const gchar* const *array_of_strings,
152 const gchar* const *array_of_objpaths,
153 GVariant *array_of_signatures,
154 const gchar* const *array_of_bytestrings,
159 str = g_string_new (NULL);
160 s = g_variant_print (dict_s_to_s, TRUE); g_string_append (str, s); g_free (s);
161 s = g_variant_print (dict_s_to_pairs, TRUE); g_string_append (str, s); g_free (s);
162 s = g_variant_print (a_struct, TRUE); g_string_append (str, s); g_free (s);
163 s = g_strjoinv (", ", (gchar **) array_of_strings);
164 g_string_append_printf (str, "array_of_strings: [%s] ", s);
166 s = g_strjoinv (", ", (gchar **) array_of_objpaths);
167 g_string_append_printf (str, "array_of_objpaths: [%s] ", s);
169 s = g_variant_print (array_of_signatures, TRUE);
170 g_string_append_printf (str, "array_of_signatures: %s ", s);
172 s = g_strjoinv (", ", (gchar **) array_of_bytestrings);
173 g_string_append_printf (str, "array_of_bytestrings: [%s] ", s);
175 foo_igen_bar_complete_test_non_primitive_types (object, invocation, str->str);
176 g_string_free (str, TRUE);
181 on_handle_request_signal_emission (FooiGenBar *object,
182 GDBusMethodInvocation *invocation,
188 const gchar *a_strv[] = {"foo", "bar", NULL};
189 const gchar *a_bytestring_array[] = {"foo\xff", "bar\xff", NULL};
190 GVariant *a_variant = g_variant_new_parsed ("{'first': (42, 42), 'second': (43, 43)}");
191 foo_igen_bar_emit_test_signal (object, 43, a_strv, a_bytestring_array, a_variant); /* consumes a_variant */
192 foo_igen_bar_complete_request_signal_emission (object, invocation);
198 on_handle_request_multi_property_mods (FooiGenBar *object,
199 GDBusMethodInvocation *invocation,
202 foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
203 foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
204 foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
205 foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
206 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
207 foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
208 foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
209 foo_igen_bar_complete_request_multi_property_mods (object, invocation);
214 on_handle_property_cancellation (FooiGenBar *object,
215 GDBusMethodInvocation *invocation,
219 n = foo_igen_bar_get_n (object);
220 /* This queues up a PropertiesChange event */
221 foo_igen_bar_set_n (object, n + 1);
222 /* this modifies the queued up event */
223 foo_igen_bar_set_n (object, n);
224 /* this flushes all PropertiesChanges event (sends the D-Bus message right
225 * away, if any - there should not be any)
227 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
228 /* this makes us return the reply D-Bus method */
229 foo_igen_bar_complete_property_cancellation (object, invocation);
233 /* ---------------------------------------------------------------------------------------------------- */
236 on_handle_force_method (FooiGenBat *object,
237 GDBusMethodInvocation *invocation,
238 GVariant *force_in_i,
239 GVariant *force_in_s,
240 GVariant *force_in_ay,
241 GVariant *force_in_struct,
247 GVariant *ret_struct;
251 ret_i = g_variant_new_int32 (g_variant_get_int32 (force_in_i) + 10);
252 s = g_strdup_printf ("%s_foo", g_variant_get_string (force_in_s, NULL));
253 ret_s = g_variant_new_string (s);
255 s = g_strdup_printf ("%s_foo\xff", g_variant_get_bytestring (force_in_ay));
256 ret_ay = g_variant_new_bytestring (s);
259 g_variant_get (force_in_struct, "(i)", &val);
260 ret_struct = g_variant_new ("(i)", val + 10);
262 g_variant_ref_sink (ret_i);
263 g_variant_ref_sink (ret_s);
264 g_variant_ref_sink (ret_ay);
265 g_variant_ref_sink (ret_struct);
267 foo_igen_bat_emit_force_signal (object,
273 foo_igen_bat_complete_force_method (object,
280 g_variant_unref (ret_i);
281 g_variant_unref (ret_s);
282 g_variant_unref (ret_ay);
283 g_variant_unref (ret_struct);
289 /* ---------------------------------------------------------------------------------------------------- */
292 my_g_authorize_method_handler (GDBusInterfaceSkeleton *interface,
293 GDBusMethodInvocation *invocation,
296 const gchar *method_name;
301 method_name = g_dbus_method_invocation_get_method_name (invocation);
302 if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
306 else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
310 else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
316 g_assert_not_reached ();
321 g_dbus_method_invocation_return_error (invocation,
323 G_IO_ERROR_PERMISSION_DENIED,
324 "not authorized...");
330 my_object_authorize_method_handler (GDBusObjectSkeleton *object,
331 GDBusInterfaceSkeleton *interface,
332 GDBusMethodInvocation *invocation,
335 const gchar *method_name;
340 method_name = g_dbus_method_invocation_get_method_name (invocation);
341 if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
345 else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
349 else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
355 g_assert_not_reached ();
360 g_dbus_method_invocation_return_error (invocation,
363 "not authorized (from object)...");
369 on_handle_check_not_authorized (FooiGenAuthorize *object,
370 GDBusMethodInvocation *invocation,
373 foo_igen_authorize_complete_check_not_authorized (object, invocation);
378 on_handle_check_authorized (FooiGenAuthorize *object,
379 GDBusMethodInvocation *invocation,
382 foo_igen_authorize_complete_check_authorized (object, invocation);
387 on_handle_check_not_authorized_from_object (FooiGenAuthorize *object,
388 GDBusMethodInvocation *invocation,
391 foo_igen_authorize_complete_check_not_authorized_from_object (object, invocation);
395 /* ---------------------------------------------------------------------------------------------------- */
398 on_handle_get_self (FooiGenMethodThreads *object,
399 GDBusMethodInvocation *invocation,
403 s = g_strdup_printf ("%p", g_thread_self ());
404 foo_igen_method_threads_complete_get_self (object, invocation, s);
409 /* ---------------------------------------------------------------------------------------------------- */
411 static GThread *method_handler_thread = NULL;
413 static FooiGenBar *exported_bar_object = NULL;
414 static FooiGenBat *exported_bat_object = NULL;
415 static FooiGenAuthorize *exported_authorize_object = NULL;
416 static GDBusObjectSkeleton *authorize_enclosing_object = NULL;
417 static FooiGenMethodThreads *exported_thread_object_1 = NULL;
418 static FooiGenMethodThreads *exported_thread_object_2 = NULL;
421 unexport_objects (void)
423 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_bar_object));
424 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_bat_object));
425 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
426 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1));
427 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2));
431 on_bus_acquired (GDBusConnection *connection,
437 /* Test that we can export an object using the generated
438 * FooiGenBarSkeleton subclass. Notes:
440 * 1. We handle methods by simply connecting to the appropriate
443 * 2. Property storage is taken care of by the class; we can
444 * use g_object_get()/g_object_set() (and the generated
445 * C bindings at will)
448 exported_bar_object = foo_igen_bar_skeleton_new ();
449 foo_igen_bar_set_ay (exported_bar_object, "ABCabc");
450 foo_igen_bar_set_y (exported_bar_object, 42);
451 foo_igen_bar_set_d (exported_bar_object, 43.0);
452 foo_igen_bar_set_finally_normal_name (exported_bar_object, "There aint no place like home");
453 foo_igen_bar_set_writeonly_property (exported_bar_object, "Mr. Burns");
455 /* The following works because it's on the Skeleton object - it will
456 * fail (at run-time) on a Proxy (see on_proxy_appeared() below)
458 foo_igen_bar_set_readonly_property (exported_bar_object, "blah");
459 g_assert_cmpstr (foo_igen_bar_get_writeonly_property (exported_bar_object), ==, "Mr. Burns");
461 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bar_object),
465 g_assert_no_error (error);
466 g_signal_connect (exported_bar_object,
467 "handle-hello-world",
468 G_CALLBACK (on_handle_hello_world),
470 g_signal_connect (exported_bar_object,
471 "handle-test-primitive-types",
472 G_CALLBACK (on_handle_test_primitive_types),
474 g_signal_connect (exported_bar_object,
475 "handle-test-non-primitive-types",
476 G_CALLBACK (on_handle_test_non_primitive_types),
478 g_signal_connect (exported_bar_object,
479 "handle-request-signal-emission",
480 G_CALLBACK (on_handle_request_signal_emission),
482 g_signal_connect (exported_bar_object,
483 "handle-request-multi-property-mods",
484 G_CALLBACK (on_handle_request_multi_property_mods),
486 g_signal_connect (exported_bar_object,
487 "handle-property-cancellation",
488 G_CALLBACK (on_handle_property_cancellation),
491 exported_bat_object = foo_igen_bat_skeleton_new ();
492 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bat_object),
496 g_assert_no_error (error);
497 g_signal_connect (exported_bat_object,
498 "handle-force-method",
499 G_CALLBACK (on_handle_force_method),
501 g_object_set (exported_bat_object,
502 "force-i", g_variant_new_int32 (43),
503 "force-s", g_variant_new_string ("prop string"),
504 "force-ay", g_variant_new_bytestring ("prop bytestring\xff"),
505 "force-struct", g_variant_new ("(i)", 4300),
508 authorize_enclosing_object = g_dbus_object_skeleton_new ("/authorize");
509 g_signal_connect (authorize_enclosing_object,
511 G_CALLBACK (my_object_authorize_method_handler),
513 exported_authorize_object = foo_igen_authorize_skeleton_new ();
514 g_dbus_object_skeleton_add_interface (authorize_enclosing_object,
515 G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
516 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_authorize_object),
520 g_assert_no_error (error);
521 g_signal_connect (exported_authorize_object,
522 "g-authorize-method",
523 G_CALLBACK (my_g_authorize_method_handler),
525 g_signal_connect (exported_authorize_object,
526 "handle-check-not-authorized",
527 G_CALLBACK (on_handle_check_not_authorized),
529 g_signal_connect (exported_authorize_object,
530 "handle-check-authorized",
531 G_CALLBACK (on_handle_check_authorized),
533 g_signal_connect (exported_authorize_object,
534 "handle-check-not-authorized-from-object",
535 G_CALLBACK (on_handle_check_not_authorized_from_object),
539 /* only object 1 has the G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD flag set */
540 exported_thread_object_1 = foo_igen_method_threads_skeleton_new ();
541 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
542 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
544 g_assert (!g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1), connection));
545 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
549 g_assert_no_error (error);
550 g_signal_connect (exported_thread_object_1,
552 G_CALLBACK (on_handle_get_self),
554 g_assert_cmpint (g_dbus_interface_skeleton_get_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1)), ==, G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
556 exported_thread_object_2 = foo_igen_method_threads_skeleton_new ();
557 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2),
561 g_assert_no_error (error);
562 g_signal_connect (exported_thread_object_2,
564 G_CALLBACK (on_handle_get_self),
567 g_assert_cmpint (g_dbus_interface_skeleton_get_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2)), ==, G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
569 method_handler_thread = g_thread_self ();
572 static gpointer check_proxies_in_thread (gpointer user_data);
575 on_name_acquired (GDBusConnection *connection,
579 GMainLoop *loop = user_data;
581 g_thread_new ("check-proxies",
582 check_proxies_in_thread,
587 on_name_lost (GDBusConnection *connection,
591 g_assert_not_reached ();
594 /* ---------------------------------------------------------------------------------------------------- */
598 GMainLoop *thread_loop;
601 guint num_g_properties_changed;
602 gboolean received_test_signal;
608 on_notify_u (GObject *object,
612 ClientData *data = user_data;
613 g_assert_cmpstr (pspec->name, ==, "u");
614 data->num_notify_u += 1;
618 on_notify_n (GObject *object,
622 ClientData *data = user_data;
623 g_assert_cmpstr (pspec->name, ==, "n");
624 data->num_notify_n += 1;
628 on_g_properties_changed (GDBusProxy *_proxy,
629 GVariant *changed_properties,
630 const gchar* const *invalidated_properties,
633 ClientData *data = user_data;
634 FooiGenBar *proxy = FOO_IGEN_BAR (_proxy);
636 g_assert_cmpint (g_variant_n_children (changed_properties), ==, 2);
638 if (data->num_g_properties_changed == 0)
640 g_assert_cmpint (data->initial_y, ==, foo_igen_bar_get_y (proxy) - 2);
641 g_assert_cmpint (data->initial_i, ==, foo_igen_bar_get_i (proxy) - 2);
643 else if (data->num_g_properties_changed == 1)
645 g_assert_cmpint (data->initial_y, ==, foo_igen_bar_get_y (proxy) - 3);
646 g_assert_cmpint (data->initial_i, ==, foo_igen_bar_get_i (proxy) - 3);
649 g_assert_not_reached ();
651 data->num_g_properties_changed++;
653 if (data->num_g_properties_changed == 2)
654 g_main_loop_quit (data->thread_loop);
658 on_test_signal (FooiGenBar *proxy,
660 const gchar* const *array_of_strings,
661 const gchar* const *array_of_bytestrings,
662 GVariant *dict_s_to_pairs,
665 ClientData *data = user_data;
667 g_assert_cmpint (val_int32, ==, 43);
668 g_assert_cmpstr (array_of_strings[0], ==, "foo");
669 g_assert_cmpstr (array_of_strings[1], ==, "bar");
670 g_assert (array_of_strings[2] == NULL);
671 g_assert_cmpstr (array_of_bytestrings[0], ==, "foo\xff");
672 g_assert_cmpstr (array_of_bytestrings[1], ==, "bar\xff");
673 g_assert (array_of_bytestrings[2] == NULL);
675 data->received_test_signal = TRUE;
676 g_main_loop_quit (data->thread_loop);
680 on_property_cancellation_cb (FooiGenBar *proxy,
684 ClientData *data = user_data;
686 GError *error = NULL;
689 ret = foo_igen_bar_call_property_cancellation_finish (proxy, res, &error);
690 g_assert_no_error (error);
693 g_main_loop_quit (data->thread_loop);
697 check_bar_proxy (FooiGenBar *proxy,
698 GMainLoop *thread_loop)
700 const gchar *array_of_strings[3] = {"one", "two", NULL};
701 const gchar *array_of_strings_2[3] = {"one2", "two2", NULL};
702 const gchar *array_of_objpaths[3] = {"/one", "/one/two", NULL};
703 const gchar *array_of_bytestrings[3] = {"one\xff", "two\xff", NULL};
705 gboolean ret_val_boolean;
706 gint16 ret_val_int16;
707 guint16 ret_val_uint16;
709 guint ret_val_uint32;
710 gint64 ret_val_int64;
711 guint64 ret_val_uint64;
712 gdouble ret_val_double;
713 gchar *ret_val_string;
714 gchar *ret_val_objpath;
715 gchar *ret_val_signature;
716 gchar *ret_val_bytestring;
742 gchar **val_unset_as;
743 gchar **val_unset_ao;
744 GVariant *val_unset_ag;
745 GVariant *val_unset_struct;
746 gchar *val_finally_normal_name;
749 const gchar *const *read_as;
750 const gchar *const *read_as2;
751 const gchar *const *read_as3;
753 data = g_new0 (ClientData, 1);
754 data->thread_loop = thread_loop;
756 v = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "y");
757 g_assert (v != NULL);
760 /* set empty values to non-empty */
770 val_unset_struct = NULL;
771 /* check properties */
789 "unset_i", &val_unset_i,
790 "unset_d", &val_unset_d,
791 "unset_s", &val_unset_s,
792 "unset_o", &val_unset_o,
793 "unset_g", &val_unset_g,
794 "unset_ay", &val_unset_ay,
795 "unset_as", &val_unset_as,
796 "unset_ao", &val_unset_ao,
797 "unset_ag", &val_unset_ag,
798 "unset_struct", &val_unset_struct,
799 "finally-normal-name", &val_finally_normal_name,
801 g_assert_cmpint (val_y, ==, 42);
802 g_assert_cmpstr (val_finally_normal_name, ==, "There aint no place like home");
806 g_assert_cmpstr (val_ay, ==, "ABCabc");
810 g_variant_unref (val_ag);
811 g_free (val_finally_normal_name);
812 /* check empty values */
813 g_assert_cmpint (val_unset_i, ==, 0);
814 g_assert_cmpfloat (val_unset_d, ==, 0.0);
815 g_assert_cmpstr (val_unset_s, ==, "");
816 g_assert_cmpstr (val_unset_o, ==, "/");
817 g_assert_cmpstr (val_unset_g, ==, "");
818 g_free (val_unset_s);
819 g_free (val_unset_o);
820 g_free (val_unset_g);
821 g_assert_cmpstr (val_unset_ay, ==, "");
822 g_assert (val_unset_as[0] == NULL);
823 g_assert (val_unset_ao[0] == NULL);
824 g_assert (g_variant_is_of_type (val_unset_ag, G_VARIANT_TYPE ("ag")));
825 g_assert (g_variant_is_of_type (val_unset_struct, G_VARIANT_TYPE ("(idsogayasaoag)")));
826 s = g_variant_print (val_unset_struct, TRUE);
827 g_assert_cmpstr (s, ==, "(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])");
829 g_free (val_unset_ay);
830 g_strfreev (val_unset_as);
831 g_strfreev (val_unset_ao);
832 g_variant_unref (val_unset_ag);
833 g_variant_unref (val_unset_struct);
835 /* Try setting a property. This causes the generated glue to invoke
836 * the org.fd.DBus.Properties.Set() method asynchronously. So we
837 * have to wait for properties-changed...
839 foo_igen_bar_set_finally_normal_name (proxy, "foo!");
840 _g_assert_property_notify (proxy, "finally-normal-name");
841 g_assert_cmpstr (foo_igen_bar_get_finally_normal_name (proxy), ==, "foo!");
843 /* Try setting properties that requires memory management. This
844 * is to exercise the paths that frees the references.
851 "ay", g_variant_new_parsed ("[byte 0x65, 0x67]"),
852 "as", array_of_strings,
853 "ao", array_of_objpaths,
854 "ag", g_variant_new_parsed ("[@g 'ass', 'git']"),
858 ret = foo_igen_bar_call_test_primitive_types_sync (proxy,
885 NULL, /* GCancellable */
887 g_assert_no_error (error);
891 ret = foo_igen_bar_call_test_non_primitive_types_sync (proxy,
892 g_variant_new_parsed ("{'one': 'red',"
894 g_variant_new_parsed ("{'first': (42, 42), "
895 "'second': (43, 43)}"),
896 g_variant_new_parsed ("(42, 'foo', 'bar')"),
899 g_variant_new_parsed ("[@g 'ass', 'git']"),
900 array_of_bytestrings,
902 NULL, /* GCancellable */
905 g_assert_no_error (error);
908 /* Check that org.freedesktop.DBus.Error.UnknownMethod is returned on
909 * unimplemented methods.
912 ret = foo_igen_bar_call_unimplemented_method_sync (proxy, NULL /* GCancellable */, &error);
913 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
914 g_error_free (error);
918 g_signal_connect (proxy,
920 G_CALLBACK (on_test_signal),
923 ret = foo_igen_bar_call_request_signal_emission_sync (proxy, 0, NULL, &error);
924 g_assert_no_error (error);
927 g_assert (!data->received_test_signal);
928 g_main_loop_run (thread_loop);
929 g_assert (data->received_test_signal);
931 /* Try setting a property. This causes the generated glue to invoke
932 * the org.fd.DBus.Properties.Set() method asynchronously. So we
933 * have to wait for properties-changed...
935 foo_igen_bar_set_finally_normal_name (proxy, "hey back!");
936 _g_assert_property_notify (proxy, "finally-normal-name");
937 g_assert_cmpstr (foo_igen_bar_get_finally_normal_name (proxy), ==, "hey back!");
939 /* Check that multiple calls to a strv getter works... and that
940 * updates on them works as well (See comment for "property vfuncs"
941 * in gio/gdbus-codegen/codegen.py for details)
943 read_as = foo_igen_bar_get_as (proxy);
944 read_as2 = foo_igen_bar_get_as (proxy);
945 g_assert_cmpint (g_strv_length ((gchar **) read_as), ==, 2);
946 g_assert_cmpstr (read_as[0], ==, "one");
947 g_assert_cmpstr (read_as[1], ==, "two");
948 g_assert (read_as == read_as2); /* this is more testing an implementation detail */
950 "as", array_of_strings_2,
952 _g_assert_property_notify (proxy, "as");
953 read_as3 = foo_igen_bar_get_as (proxy);
954 g_assert_cmpint (g_strv_length ((gchar **) read_as3), ==, 2);
955 g_assert_cmpstr (read_as3[0], ==, "one2");
956 g_assert_cmpstr (read_as3[1], ==, "two2");
958 /* Check that grouping changes in idle works.
960 * See on_handle_request_multi_property_mods(). The server should
961 * emit exactly two PropertiesChanged signals each containing two
964 * On the first reception, y and i should both be increased by
965 * two. On the second reception, only by one. The signal handler
968 * This also checks that _drain_notify() works.
970 data->initial_y = foo_igen_bar_get_y (proxy);
971 data->initial_i = foo_igen_bar_get_i (proxy);
972 g_signal_connect (proxy,
973 "g-properties-changed",
974 G_CALLBACK (on_g_properties_changed),
977 ret = foo_igen_bar_call_request_multi_property_mods_sync (proxy, NULL, &error);
978 g_assert_no_error (error);
980 g_main_loop_run (thread_loop);
981 g_assert_cmpint (data->num_g_properties_changed, ==, 2);
982 g_signal_handlers_disconnect_by_func (proxy,
983 G_CALLBACK (on_g_properties_changed),
986 /* Check that we don't emit PropertiesChanged() if the property
987 * didn't change... we actually get two notifies.. one for the
988 * local set (without a value change) and one when receiving
989 * the PropertiesChanged() signal generated from the remote end.
991 g_assert_cmpint (data->num_notify_u, ==, 0);
992 g_signal_connect (proxy,
994 G_CALLBACK (on_notify_u),
996 foo_igen_bar_set_u (proxy, 1042);
997 g_assert_cmpint (data->num_notify_u, ==, 1);
998 g_assert_cmpint (foo_igen_bar_get_u (proxy), ==, 0);
999 _g_assert_property_notify (proxy, "u");
1000 g_assert_cmpint (foo_igen_bar_get_u (proxy), ==, 1042);
1001 g_assert_cmpint (data->num_notify_u, ==, 2);
1003 /* Now change u again to the same value.. this will cause a
1004 * local notify:: notify and the usual Properties.Set() call
1006 * (Btw, why also the Set() call if the value in the cache is
1007 * the same? Because someone else might have changed it
1008 * in the mean time and we're just waiting to receive the
1009 * PropertiesChanged() signal...)
1011 * More tricky - how do we check for the *absence* of the
1012 * notification that u changed? Simple: we change another
1013 * property and wait for that PropertiesChanged() message
1016 foo_igen_bar_set_u (proxy, 1042);
1017 g_assert_cmpint (data->num_notify_u, ==, 3);
1019 g_assert_cmpint (data->num_notify_n, ==, 0);
1020 g_signal_connect (proxy,
1022 G_CALLBACK (on_notify_n),
1024 foo_igen_bar_set_n (proxy, 10042);
1025 g_assert_cmpint (data->num_notify_n, ==, 1);
1026 g_assert_cmpint (foo_igen_bar_get_n (proxy), ==, 0);
1027 _g_assert_property_notify (proxy, "n");
1028 g_assert_cmpint (foo_igen_bar_get_n (proxy), ==, 10042);
1029 g_assert_cmpint (data->num_notify_n, ==, 2);
1030 /* Checks that u didn't change at all */
1031 g_assert_cmpint (data->num_notify_u, ==, 3);
1033 /* Now we check that if the service does
1035 * guint n = foo_igen_bar_get_n (foo);
1036 * foo_igen_bar_set_n (foo, n + 1);
1037 * foo_igen_bar_set_n (foo, n);
1039 * then no PropertiesChanged() signal is emitted!
1042 foo_igen_bar_call_property_cancellation (proxy,
1043 NULL, /* GCancellable */
1044 (GAsyncReadyCallback) on_property_cancellation_cb,
1046 g_main_loop_run (thread_loop);
1047 /* Checks that n didn't change at all */
1048 g_assert_cmpint (data->num_notify_n, ==, 2);
1054 /* ---------------------------------------------------------------------------------------------------- */
1057 on_force_signal (FooiGenBat *proxy,
1061 GVariant *force_struct,
1064 gboolean *signal_received = user_data;
1067 g_assert (!(*signal_received));
1069 g_assert_cmpint (g_variant_get_int32 (force_i), ==, 42 + 10);
1070 g_assert_cmpstr (g_variant_get_string (force_s, NULL), ==, "a string_foo");
1071 g_assert_cmpstr (g_variant_get_bytestring (force_ay), ==, "a bytestring\xff_foo\xff");
1072 g_variant_get (force_struct, "(i)", &val);
1073 g_assert_cmpint (val, ==, 4200 + 10);
1075 *signal_received = TRUE;
1079 check_bat_proxy (FooiGenBat *proxy,
1080 GMainLoop *thread_loop)
1086 GVariant *ret_struct;
1088 gboolean force_signal_received;
1090 /* --------------------------------------------------- */
1091 /* Check type-mapping where we force use of a GVariant */
1092 /* --------------------------------------------------- */
1094 /* check properties */
1095 g_object_get (proxy,
1098 "force-ay", &ret_ay,
1099 "force-struct", &ret_struct,
1101 g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 43);
1102 g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "prop string");
1103 g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "prop bytestring\xff");
1104 g_variant_get (ret_struct, "(i)", &val);
1105 g_assert_cmpint (val, ==, 4300);
1106 g_variant_unref (ret_i);
1107 g_variant_unref (ret_s);
1108 g_variant_unref (ret_ay);
1109 g_variant_unref (ret_struct);
1111 /* check method and signal */
1112 force_signal_received = FALSE;
1113 g_signal_connect (proxy,
1115 G_CALLBACK (on_force_signal),
1116 &force_signal_received);
1119 foo_igen_bat_call_force_method_sync (proxy,
1120 g_variant_new_int32 (42),
1121 g_variant_new_string ("a string"),
1122 g_variant_new_bytestring ("a bytestring\xff"),
1123 g_variant_new ("(i)", 4200),
1128 NULL, /* GCancellable* */
1130 g_assert_no_error (error);
1131 g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 42 + 10);
1132 g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "a string_foo");
1133 g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "a bytestring\xff_foo\xff");
1134 g_variant_get (ret_struct, "(i)", &val);
1135 g_assert_cmpint (val, ==, 4200 + 10);
1136 g_variant_unref (ret_i);
1137 g_variant_unref (ret_s);
1138 g_variant_unref (ret_ay);
1139 g_variant_unref (ret_struct);
1140 _g_assert_signal_received (proxy, "force-signal");
1141 g_assert (force_signal_received);
1144 /* ---------------------------------------------------------------------------------------------------- */
1147 check_authorize_proxy (FooiGenAuthorize *proxy,
1148 GMainLoop *thread_loop)
1153 /* Check that g-authorize-method works as intended */
1156 ret = foo_igen_authorize_call_check_not_authorized_sync (proxy, NULL, &error);
1157 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1158 g_error_free (error);
1162 ret = foo_igen_authorize_call_check_authorized_sync (proxy, NULL, &error);
1163 g_assert_no_error (error);
1167 ret = foo_igen_authorize_call_check_not_authorized_from_object_sync (proxy, NULL, &error);
1168 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
1169 g_error_free (error);
1173 /* ---------------------------------------------------------------------------------------------------- */
1176 get_self_via_proxy (FooiGenMethodThreads *proxy_1)
1184 ret = foo_igen_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
1185 g_assert_no_error (error);
1188 g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
1196 check_thread_proxies (FooiGenMethodThreads *proxy_1,
1197 FooiGenMethodThreads *proxy_2,
1198 GMainLoop *thread_loop)
1200 /* proxy_1 is indeed using threads so should never get the handler thread */
1201 g_assert (get_self_via_proxy (proxy_1) != method_handler_thread);
1203 /* proxy_2 is not using threads so should get the handler thread */
1204 g_assert (get_self_via_proxy (proxy_2) == method_handler_thread);
1207 /* ---------------------------------------------------------------------------------------------------- */
1210 check_proxies_in_thread (gpointer user_data)
1212 GMainLoop *loop = user_data;
1213 GMainContext *thread_context;
1214 GMainLoop *thread_loop;
1216 FooiGenBar *bar_proxy;
1217 FooiGenBat *bat_proxy;
1218 FooiGenAuthorize *authorize_proxy;
1219 FooiGenMethodThreads *thread_proxy_1;
1220 FooiGenMethodThreads *thread_proxy_2;
1222 thread_context = g_main_context_new ();
1223 thread_loop = g_main_loop_new (thread_context, FALSE);
1224 g_main_context_push_thread_default (thread_context);
1226 /* Check the object */
1228 bar_proxy = foo_igen_bar_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1229 G_DBUS_PROXY_FLAGS_NONE,
1230 "org.gtk.GDBus.BindingsTool.Test",
1232 NULL, /* GCancellable* */
1234 check_bar_proxy (bar_proxy, thread_loop);
1235 g_assert_no_error (error);
1236 g_object_unref (bar_proxy);
1239 bat_proxy = foo_igen_bat_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1240 G_DBUS_PROXY_FLAGS_NONE,
1241 "org.gtk.GDBus.BindingsTool.Test",
1243 NULL, /* GCancellable* */
1245 check_bat_proxy (bat_proxy, thread_loop);
1246 g_assert_no_error (error);
1247 g_object_unref (bat_proxy);
1250 authorize_proxy = foo_igen_authorize_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1251 G_DBUS_PROXY_FLAGS_NONE,
1252 "org.gtk.GDBus.BindingsTool.Test",
1254 NULL, /* GCancellable* */
1256 check_authorize_proxy (authorize_proxy, thread_loop);
1257 g_assert_no_error (error);
1258 g_object_unref (authorize_proxy);
1261 thread_proxy_1 = foo_igen_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1262 G_DBUS_PROXY_FLAGS_NONE,
1263 "org.gtk.GDBus.BindingsTool.Test",
1264 "/method_threads_1",
1265 NULL, /* GCancellable* */
1267 g_assert_no_error (error);
1268 thread_proxy_2 = foo_igen_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1269 G_DBUS_PROXY_FLAGS_NONE,
1270 "org.gtk.GDBus.BindingsTool.Test",
1271 "/method_threads_2",
1272 NULL, /* GCancellable* */
1274 g_assert_no_error (error);
1275 check_thread_proxies (thread_proxy_1, thread_proxy_2, thread_loop);
1276 g_object_unref (thread_proxy_1);
1277 g_object_unref (thread_proxy_2);
1279 g_main_loop_unref (thread_loop);
1280 g_main_context_unref (thread_context);
1282 /* this breaks out of the loop in main() (below) */
1283 g_main_loop_quit (loop);
1287 /* ---------------------------------------------------------------------------------------------------- */
1296 introspect_cb (GDBusConnection *connection,
1300 IntrospectData *data = user_data;
1305 result = g_dbus_connection_call_finish (connection,
1308 g_assert_no_error (error);
1309 g_assert (result != NULL);
1310 g_variant_get (result, "(s)", &data->xml);
1311 g_variant_unref (result);
1313 g_main_loop_quit (data->loop);
1316 static GDBusNodeInfo *
1317 introspect (GDBusConnection *connection,
1319 const gchar *object_path,
1323 GDBusNodeInfo *node_info;
1324 IntrospectData *data;
1326 data = g_new0 (IntrospectData, 1);
1330 /* do this async to avoid deadlocks */
1331 g_dbus_connection_call (connection,
1334 "org.freedesktop.DBus.Introspectable",
1337 G_VARIANT_TYPE ("(s)"),
1338 G_DBUS_CALL_FLAGS_NONE,
1341 (GAsyncReadyCallback) introspect_cb,
1343 g_main_loop_run (loop);
1344 g_assert (data->xml != NULL);
1347 node_info = g_dbus_node_info_new_for_xml (data->xml, &error);
1348 g_assert_no_error (error);
1349 g_assert (node_info != NULL);
1357 count_interfaces (GDBusNodeInfo *info)
1360 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1366 count_nodes (GDBusNodeInfo *info)
1369 for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
1375 has_interface (GDBusNodeInfo *info,
1379 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1381 if (g_strcmp0 (info->interfaces[n]->name, name) == 0)
1387 /* ---------------------------------------------------------------------------------------------------- */
1392 } OMGetManagedObjectsData;
1395 om_get_all_cb (GDBusConnection *connection,
1399 OMGetManagedObjectsData *data = user_data;
1403 data->result = g_dbus_connection_call_finish (connection,
1406 g_assert_no_error (error);
1407 g_assert (data->result != NULL);
1408 g_main_loop_quit (data->loop);
1412 om_check_get_all (GDBusConnection *c,
1416 OMGetManagedObjectsData data;
1422 /* do this async to avoid deadlocks */
1423 g_dbus_connection_call (c,
1424 g_dbus_connection_get_unique_name (c),
1426 "org.freedesktop.DBus.ObjectManager",
1427 "GetManagedObjects",
1429 G_VARIANT_TYPE ("(a{oa{sa{sv}}})"),
1430 G_DBUS_CALL_FLAGS_NONE,
1433 (GAsyncReadyCallback) om_get_all_cb,
1435 g_main_loop_run (loop);
1436 g_assert (data.result != NULL);
1437 s = g_variant_print (data.result, TRUE);
1438 g_assert_cmpstr (s, ==, str);
1440 g_variant_unref (data.result);
1448 guint num_object_proxy_added_signals;
1449 guint num_object_proxy_removed_signals;
1450 guint num_interface_added_signals;
1451 guint num_interface_removed_signals;
1455 my_pstrcmp (const gchar **a, const gchar **b)
1457 return g_strcmp0 (*a, *b);
1461 om_check_interfaces_added (const gchar *signal_name,
1462 GVariant *parameters,
1463 const gchar *object_path,
1464 const gchar *first_interface_name,
1470 GPtrArray *interfaces;
1471 GPtrArray *interfaces_in_message;
1475 interfaces = g_ptr_array_new ();
1476 g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1477 va_start (var_args, first_interface_name);
1480 str = va_arg (var_args, const gchar *);
1483 g_ptr_array_add (interfaces, (gpointer) str);
1488 g_variant_get (parameters, "(&o*)", &path, &array);
1489 g_assert_cmpstr (signal_name, ==, "InterfacesAdded");
1490 g_assert_cmpstr (path, ==, object_path);
1491 g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1492 interfaces_in_message = g_ptr_array_new ();
1493 for (n = 0; n < interfaces->len; n++)
1495 const gchar *iface_name;
1496 g_variant_get_child (array, n, "{&sa{sv}}", &iface_name, NULL);
1497 g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1499 g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1500 g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1501 g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1502 for (n = 0; n < interfaces->len; n++)
1503 g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1504 g_ptr_array_unref (interfaces_in_message);
1505 g_ptr_array_unref (interfaces);
1506 g_variant_unref (array);
1510 om_check_interfaces_removed (const gchar *signal_name,
1511 GVariant *parameters,
1512 const gchar *object_path,
1513 const gchar *first_interface_name,
1519 GPtrArray *interfaces;
1520 GPtrArray *interfaces_in_message;
1524 interfaces = g_ptr_array_new ();
1525 g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1526 va_start (var_args, first_interface_name);
1529 str = va_arg (var_args, const gchar *);
1532 g_ptr_array_add (interfaces, (gpointer) str);
1537 g_variant_get (parameters, "(&o*)", &path, &array);
1538 g_assert_cmpstr (signal_name, ==, "InterfacesRemoved");
1539 g_assert_cmpstr (path, ==, object_path);
1540 g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1541 interfaces_in_message = g_ptr_array_new ();
1542 for (n = 0; n < interfaces->len; n++)
1544 const gchar *iface_name;
1545 g_variant_get_child (array, n, "&s", &iface_name, NULL);
1546 g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1548 g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1549 g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1550 g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1551 for (n = 0; n < interfaces->len; n++)
1552 g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1553 g_ptr_array_unref (interfaces_in_message);
1554 g_ptr_array_unref (interfaces);
1555 g_variant_unref (array);
1559 om_on_signal (GDBusConnection *connection,
1560 const gchar *sender_name,
1561 const gchar *object_path,
1562 const gchar *interface_name,
1563 const gchar *signal_name,
1564 GVariant *parameters,
1567 OMData *om_data = user_data;
1569 //g_debug ("foo: %s", g_variant_print (parameters, TRUE));
1571 switch (om_data->state)
1575 g_print ("failing and om_data->state=%d on signal %s, params=%s\n",
1578 g_variant_print (parameters, TRUE));
1579 g_assert_not_reached ();
1583 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1584 "org.project.Bar", NULL);
1586 g_main_loop_quit (om_data->loop);
1590 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1591 "org.project.Bar", NULL);
1593 /* keep running the loop */
1597 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1598 "org.project.Bar", NULL);
1600 g_main_loop_quit (om_data->loop);
1604 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1605 "org.project.Bar", NULL);
1607 /* keep running the loop */
1611 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1612 "org.project.Bar", NULL);
1613 om_data->state = 10;
1614 g_main_loop_quit (om_data->loop);
1618 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1619 "org.project.Bat", NULL);
1620 om_data->state = 12;
1621 g_main_loop_quit (om_data->loop);
1625 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1626 "org.project.Bar", NULL);
1627 om_data->state = 14;
1628 g_main_loop_quit (om_data->loop);
1632 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1633 "org.project.Bat", NULL);
1634 om_data->state = 16;
1635 g_main_loop_quit (om_data->loop);
1639 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1640 "com.acme.Coyote", NULL);
1641 om_data->state = 18;
1642 g_main_loop_quit (om_data->loop);
1646 om_check_interfaces_added (signal_name, parameters, "/managed/second",
1647 "org.project.Bat", "org.project.Bar", NULL);
1648 om_data->state = 102;
1649 g_main_loop_quit (om_data->loop);
1653 om_check_interfaces_removed (signal_name, parameters, "/managed/second",
1654 "org.project.Bat", "org.project.Bar", NULL);
1655 om_data->state = 104;
1656 g_main_loop_quit (om_data->loop);
1660 om_check_interfaces_added (signal_name, parameters, "/managed/first_1",
1661 "com.acme.Coyote", NULL);
1662 om_data->state = 201;
1663 g_main_loop_quit (om_data->loop);
1668 static GAsyncResult *om_res = NULL;
1671 om_pm_start_cb (FooiGenObjectManagerClient *manager,
1675 GMainLoop *loop = user_data;
1676 om_res = g_object_ref (res);
1677 g_main_loop_quit (loop);
1681 on_interface_added (GDBusObject *object,
1682 GDBusInterface *interface,
1685 OMData *om_data = user_data;
1686 om_data->num_interface_added_signals += 1;
1690 on_interface_removed (GDBusObject *object,
1691 GDBusInterface *interface,
1694 OMData *om_data = user_data;
1695 om_data->num_interface_removed_signals += 1;
1699 on_object_proxy_added (GDBusObjectManagerClient *manager,
1700 GDBusObjectProxy *object_proxy,
1703 OMData *om_data = user_data;
1704 om_data->num_object_proxy_added_signals += 1;
1705 g_signal_connect (object_proxy,
1707 G_CALLBACK (on_interface_added),
1709 g_signal_connect (object_proxy,
1710 "interface-removed",
1711 G_CALLBACK (on_interface_removed),
1716 on_object_proxy_removed (GDBusObjectManagerClient *manager,
1717 GDBusObjectProxy *object_proxy,
1720 OMData *om_data = user_data;
1721 om_data->num_object_proxy_removed_signals += 1;
1722 g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1723 G_CALLBACK (on_interface_added),
1725 g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1726 G_CALLBACK (on_interface_removed),
1731 property_d_changed (GObject *object,
1735 gboolean *changed = user_data;
1741 om_check_property_and_signal_emission (GMainLoop *loop,
1742 FooiGenBar *skeleton,
1745 gboolean d_changed = FALSE;
1748 /* First PropertiesChanged */
1749 g_assert_cmpint (foo_igen_bar_get_i (skeleton), ==, 0);
1750 g_assert_cmpint (foo_igen_bar_get_i (proxy), ==, 0);
1751 foo_igen_bar_set_i (skeleton, 1);
1752 _g_assert_property_notify (proxy, "i");
1753 g_assert_cmpint (foo_igen_bar_get_i (skeleton), ==, 1);
1754 g_assert_cmpint (foo_igen_bar_get_i (proxy), ==, 1);
1756 /* Double-check the gdouble case */
1757 g_assert_cmpfloat (foo_igen_bar_get_d (skeleton), ==, 0.0);
1758 g_assert_cmpfloat (foo_igen_bar_get_d (proxy), ==, 0.0);
1759 foo_igen_bar_set_d (skeleton, 1.0);
1760 _g_assert_property_notify (proxy, "d");
1762 /* Verify that re-setting it to the same value doesn't cause a
1763 * notify on the proxy, by taking advantage of the fact that
1764 * notifications are serialized.
1766 handler = g_signal_connect (proxy, "notify::d",
1767 G_CALLBACK (property_d_changed), &d_changed);
1768 foo_igen_bar_set_d (skeleton, 1.0);
1769 foo_igen_bar_set_i (skeleton, 2);
1770 _g_assert_property_notify (proxy, "i");
1771 g_assert (d_changed == FALSE);
1772 g_signal_handler_disconnect (proxy, handler);
1774 /* Then just a regular signal */
1775 foo_igen_bar_emit_another_signal (skeleton, "word");
1776 _g_assert_signal_received (proxy, "another-signal");
1780 check_object_manager (void)
1782 FooiGenObjectSkeleton *o = NULL;
1783 FooiGenObjectSkeleton *o2 = NULL;
1784 FooiGenObjectSkeleton *o3 = NULL;
1785 GDBusInterfaceSkeleton *i;
1787 GDBusObjectManagerServer *manager = NULL;
1788 GDBusNodeInfo *info;
1791 OMData *om_data = NULL;
1792 guint om_signal_id = -1;
1793 GDBusObjectManager *pm = NULL;
1794 GList *object_proxies;
1798 FooiGenBar *bar_skeleton;
1799 GDBusInterface *iface;
1800 gchar *path, *name, *name_owner;
1801 GDBusConnection *c2;
1802 GDBusObjectManagerClientFlags flags;
1804 loop = g_main_loop_new (NULL, FALSE);
1806 om_data = g_new0 (OMData, 1);
1807 om_data->loop = loop;
1811 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1812 g_assert_no_error (error);
1813 g_assert (c != NULL);
1815 om_signal_id = g_dbus_connection_signal_subscribe (c,
1817 "org.freedesktop.DBus.ObjectManager",
1819 NULL, /* object_path */
1821 G_DBUS_SIGNAL_FLAGS_NONE,
1824 NULL); /* user_data_free_func */
1826 /* Our GDBusObjectManagerClient tests are simple - we basically just count the
1827 * number of times the various signals have been emitted (we don't check
1828 * that the right objects/interfaces are passed though - that's checked
1829 * in the lower-level tests in om_on_signal()...)
1831 * Note that these tests rely on the D-Bus signal handlers used by
1832 * GDBusObjectManagerClient firing before om_on_signal().
1835 pm = foo_igen_object_manager_client_new_sync (c,
1836 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1837 g_dbus_connection_get_unique_name (c),
1839 NULL, /* GCancellable */
1841 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
1842 g_error_free (error);
1843 g_assert (pm == NULL);
1845 manager = g_dbus_object_manager_server_new ("/managed");
1847 g_assert (g_dbus_object_manager_server_get_connection (manager) == NULL);
1849 g_dbus_object_manager_server_set_connection (manager, c);
1851 g_assert_cmpstr (g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (manager)), ==, "/managed");
1852 g_object_get (manager, "object-path", &path, "connection", &c2, NULL);
1853 g_assert_cmpstr (path, ==, "/managed");
1856 g_clear_object (&c2);
1858 /* Check that the manager object is visible */
1859 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
1860 g_assert_cmpint (count_interfaces (info), ==, 4); /* ObjectManager + Properties,Introspectable,Peer */
1861 g_assert (has_interface (info, "org.freedesktop.DBus.ObjectManager"));
1862 g_assert_cmpint (count_nodes (info), ==, 0);
1863 g_dbus_node_info_unref (info);
1865 /* Check GetManagedObjects() - should be empty since we have no objects */
1866 om_check_get_all (c, loop,
1867 "(@a{oa{sa{sv}}} {},)");
1869 /* Now try to create the proxy manager again - this time it should work */
1871 foo_igen_object_manager_client_new (c,
1872 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1873 g_dbus_connection_get_unique_name (c),
1875 NULL, /* GCancellable */
1876 (GAsyncReadyCallback) om_pm_start_cb,
1878 g_main_loop_run (loop);
1880 pm = foo_igen_object_manager_client_new_finish (om_res, &error);
1881 g_clear_object (&om_res);
1882 g_assert_no_error (error);
1883 g_assert (pm != NULL);
1884 g_signal_connect (pm,
1886 G_CALLBACK (on_object_proxy_added),
1888 g_signal_connect (pm,
1890 G_CALLBACK (on_object_proxy_removed),
1893 g_assert_cmpstr (g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (pm)), ==, "/managed");
1895 "object-path", &path,
1898 "name-owner", &name_owner,
1901 g_assert_cmpstr (path, ==, "/managed");
1902 g_assert_cmpstr (name, ==, g_dbus_connection_get_unique_name (c));
1903 g_assert_cmpstr (name_owner, ==, g_dbus_connection_get_unique_name (c));
1904 g_assert_cmpint (flags, ==, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE);
1907 g_clear_object (&c2);
1909 g_free (name_owner);
1911 /* ... check there are no object proxies yet */
1912 object_proxies = g_dbus_object_manager_get_objects (pm);
1913 g_assert (object_proxies == NULL);
1915 /* First, export an object with a single interface (also check that
1916 * g_dbus_interface_get_object() works and that the object isn't reffed)
1918 o = foo_igen_object_skeleton_new ("/managed/first");
1919 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
1920 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1921 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1922 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1923 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1924 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1925 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1926 foo_igen_object_skeleton_set_bar (o, NULL);
1927 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1928 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1929 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1930 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1931 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1933 o2 = FOO_IGEN_OBJECT_SKELETON (g_dbus_interface_dup_object (G_DBUS_INTERFACE (i)));
1934 g_assert (G_DBUS_OBJECT (o2) == G_DBUS_OBJECT (o));
1935 g_assert_cmpint (G_OBJECT (o2)->ref_count, ==, 2);
1936 g_clear_object (&o2);
1938 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o));
1940 /* ... check we get the InterfacesAdded signal */
1943 g_main_loop_run (om_data->loop);
1945 g_assert_cmpint (om_data->state, ==, 2);
1946 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 1);
1947 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 0);
1948 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1949 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1950 /* ... check there's one non-standard interfaces */
1951 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1952 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1953 g_assert (has_interface (info, "org.project.Bar"));
1954 g_dbus_node_info_unref (info);
1956 /* Also check g_dbus_object_manager_get_interface */
1957 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (manager), "/managed/first", "org.project.Bar");
1958 g_assert (iface != NULL);
1959 g_clear_object (&iface);
1960 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (manager), "/managed/first", "org.project.Bat");
1961 g_assert (iface == NULL);
1962 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (pm), "/managed/first", "org.project.Bar");
1963 g_assert (iface != NULL);
1964 g_clear_object (&iface);
1965 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (pm), "/managed/first", "org.project.Bat");
1966 g_assert (iface == NULL);
1968 /* Now, check adding the same interface replaces the existing one */
1969 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1970 /* ... check we get the InterfacesRemoved */
1972 g_main_loop_run (om_data->loop);
1973 /* ... and then check we get the InterfacesAdded */
1974 g_assert_cmpint (om_data->state, ==, 6);
1975 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 2);
1976 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 1);
1977 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1978 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1979 /* ... check introspection data */
1980 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1981 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1982 g_assert (has_interface (info, "org.project.Bar"));
1983 g_dbus_node_info_unref (info);
1984 g_clear_object (&i);
1986 /* check adding an interface of same type (but not same object) replaces the existing one */
1987 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
1988 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1989 /* ... check we get the InterfacesRemoved and then InterfacesAdded */
1991 g_main_loop_run (om_data->loop);
1992 g_assert_cmpint (om_data->state, ==, 10);
1993 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1994 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1995 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1996 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1997 /* ... check introspection data */
1998 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1999 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
2000 g_assert (has_interface (info, "org.project.Bar"));
2001 g_dbus_node_info_unref (info);
2002 g_clear_object (&i);
2004 /* check adding an interface of another type doesn't replace the existing one */
2005 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bat_skeleton_new ());
2006 foo_igen_object_skeleton_set_bat (o, FOO_IGEN_BAT (i));
2007 g_clear_object (&i);
2008 /* ... check we get the InterfacesAdded */
2009 om_data->state = 11;
2010 g_main_loop_run (om_data->loop);
2011 g_assert_cmpint (om_data->state, ==, 12);
2012 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2013 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
2014 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2015 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
2016 /* ... check introspection data */
2017 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2018 g_assert_cmpint (count_interfaces (info), ==, 5); /* Bar,Bat + Properties,Introspectable,Peer */
2019 g_assert (has_interface (info, "org.project.Bar"));
2020 g_assert (has_interface (info, "org.project.Bat"));
2021 g_dbus_node_info_unref (info);
2023 /* check we can remove an interface */
2024 foo_igen_object_skeleton_set_bar (o, NULL);
2025 /* ... check we get the InterfacesRemoved */
2026 om_data->state = 13;
2027 g_main_loop_run (om_data->loop);
2028 g_assert_cmpint (om_data->state, ==, 14);
2029 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2030 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
2031 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2032 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2033 /* ... check introspection data */
2034 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2035 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
2036 g_assert (has_interface (info, "org.project.Bat"));
2037 g_dbus_node_info_unref (info);
2038 /* also and that the call only has effect if the interface actually exists
2040 * (Note: if a signal was emitted we'd assert in the signal handler
2041 * because we're in state 14)
2043 foo_igen_object_skeleton_set_bar (o, NULL);
2044 /* ... check introspection data */
2045 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2046 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
2047 g_assert (has_interface (info, "org.project.Bat"));
2048 g_dbus_node_info_unref (info);
2050 /* remove the last interface */
2051 foo_igen_object_skeleton_set_bat (o, NULL);
2052 /* ... check we get the InterfacesRemoved */
2053 om_data->state = 15;
2054 g_main_loop_run (om_data->loop);
2055 g_assert_cmpint (om_data->state, ==, 16);
2056 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2057 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2058 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2059 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2060 /* ... check introspection data */
2061 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2062 g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
2063 g_dbus_node_info_unref (info);
2065 /* and add an interface again */
2066 i = G_DBUS_INTERFACE_SKELETON (foo_igen_com_acme_coyote_skeleton_new ());
2067 foo_igen_object_skeleton_set_com_acme_coyote (o, FOO_IGEN_COM_ACME_COYOTE (i));
2068 g_clear_object (&i);
2069 /* ... check we get the InterfacesAdded */
2070 om_data->state = 17;
2071 g_main_loop_run (om_data->loop);
2072 g_assert_cmpint (om_data->state, ==, 18);
2073 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 4);
2074 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2075 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2076 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2077 /* ... check introspection data */
2078 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2079 g_assert_cmpint (count_interfaces (info), ==, 4); /* com.acme.Coyote + Properties,Introspectable,Peer */
2080 g_assert (has_interface (info, "com.acme.Coyote"));
2081 g_dbus_node_info_unref (info);
2083 /* Check GetManagedObjects() - should be just the Coyote */
2084 om_check_get_all (c, loop,
2085 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2087 /* -------------------------------------------------- */
2089 /* create a new object with two interfaces */
2090 o2 = foo_igen_object_skeleton_new ("/managed/second");
2091 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
2092 bar_skeleton = FOO_IGEN_BAR (i); /* save for later test */
2093 foo_igen_object_skeleton_set_bar (o2, FOO_IGEN_BAR (i));
2094 g_clear_object (&i);
2095 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bat_skeleton_new ());
2096 foo_igen_object_skeleton_set_bat (o2, FOO_IGEN_BAT (i));
2097 g_clear_object (&i);
2099 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o2));
2100 /* ... check we get the InterfacesAdded with _two_ interfaces */
2101 om_data->state = 101;
2102 g_main_loop_run (om_data->loop);
2103 g_assert_cmpint (om_data->state, ==, 102);
2104 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2105 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2106 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2107 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2109 /* -------------------------------------------------- */
2111 /* Now that we have a couple of objects with interfaces, check
2112 * that ObjectManager.GetManagedObjects() works
2114 om_check_get_all (c, loop,
2115 "({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,)>}}},)");
2117 /* Set connection to NULL, causing everything to be unexported.. verify this.. and
2118 * then set the connection back.. and then check things still work
2120 g_dbus_object_manager_server_set_connection (manager, NULL);
2121 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
2122 g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
2123 g_dbus_node_info_unref (info);
2125 g_dbus_object_manager_server_set_connection (manager, c);
2126 om_check_get_all (c, loop,
2127 "({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,)>}}},)");
2129 /* Also check that the ObjectManagerClient returns these objects - and
2130 * that they are of the right GType cf. what was requested via
2131 * the generated ::get-proxy-type signal handler
2133 object_proxies = g_dbus_object_manager_get_objects (pm);
2134 g_assert (g_list_length (object_proxies) == 2);
2135 g_list_free_full (object_proxies, g_object_unref);
2136 op = g_dbus_object_manager_get_object (pm, "/managed/first");
2137 g_assert (op != NULL);
2138 g_assert (FOO_IGEN_IS_OBJECT_PROXY (op));
2139 g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/first");
2140 proxies = g_dbus_object_get_interfaces (op);
2141 g_assert (g_list_length (proxies) == 1);
2142 g_list_free_full (proxies, g_object_unref);
2143 p = G_DBUS_PROXY (foo_igen_object_get_com_acme_coyote (FOO_IGEN_OBJECT (op)));
2144 g_assert (p != NULL);
2145 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_COM_ACME_COYOTE_PROXY);
2146 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_COM_ACME_COYOTE));
2147 g_clear_object (&p);
2148 p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2149 g_assert (p == NULL);
2150 g_clear_object (&op);
2153 op = g_dbus_object_manager_get_object (pm, "/managed/second");
2154 g_assert (op != NULL);
2155 g_assert (FOO_IGEN_IS_OBJECT_PROXY (op));
2156 g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/second");
2157 proxies = g_dbus_object_get_interfaces (op);
2158 g_assert (g_list_length (proxies) == 2);
2159 g_list_free_full (proxies, g_object_unref);
2160 p = G_DBUS_PROXY (foo_igen_object_get_bat (FOO_IGEN_OBJECT (op)));
2161 g_assert (p != NULL);
2162 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_BAT_PROXY);
2163 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_BAT));
2164 g_clear_object (&p);
2165 p = G_DBUS_PROXY (foo_igen_object_get_bar (FOO_IGEN_OBJECT (op)));
2166 g_assert (p != NULL);
2167 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_BAR_PROXY);
2168 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_BAR));
2169 /* ... now that we have a Bar instance around, also check that we get signals
2170 * and property changes...
2172 om_check_property_and_signal_emission (loop, bar_skeleton, FOO_IGEN_BAR (p));
2173 g_clear_object (&p);
2174 p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2175 g_assert (p == NULL);
2176 g_clear_object (&op);
2178 /* -------------------------------------------------- */
2180 /* Now remove the second object added above */
2181 g_dbus_object_manager_server_unexport (manager, "/managed/second");
2182 /* ... check we get InterfacesRemoved with both interfaces */
2183 om_data->state = 103;
2184 g_main_loop_run (om_data->loop);
2185 g_assert_cmpint (om_data->state, ==, 104);
2186 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2187 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 4);
2188 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2189 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2190 /* ... check introspection data (there should be nothing) */
2191 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/second", loop);
2192 g_assert_cmpint (count_nodes (info), ==, 0);
2193 g_assert_cmpint (count_interfaces (info), ==, 0);
2194 g_dbus_node_info_unref (info);
2196 /* Check GetManagedObjects() again */
2197 om_check_get_all (c, loop,
2198 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2199 /* -------------------------------------------------- */
2201 /* Check that export_uniquely() works */
2203 o3 = foo_igen_object_skeleton_new ("/managed/first");
2204 i = G_DBUS_INTERFACE_SKELETON (foo_igen_com_acme_coyote_skeleton_new ());
2205 foo_igen_com_acme_coyote_set_mood (FOO_IGEN_COM_ACME_COYOTE (i), "indifferent");
2206 foo_igen_object_skeleton_set_com_acme_coyote (o3, FOO_IGEN_COM_ACME_COYOTE (i));
2207 g_clear_object (&i);
2208 g_dbus_object_manager_server_export_uniquely (manager, G_DBUS_OBJECT_SKELETON (o3));
2209 /* ... check we get the InterfacesAdded signal */
2210 om_data->state = 200;
2211 g_main_loop_run (om_data->loop);
2212 g_assert_cmpint (om_data->state, ==, 201);
2214 om_check_get_all (c, loop,
2215 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/first_1': {'com.acme.Coyote': {'Mood': <'indifferent'>}}},)");
2217 //g_main_loop_run (loop); /* TODO: tmp */
2219 /* Clean up objects */
2220 g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/first_1"));
2221 //g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/second"));
2222 g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/first"));
2223 g_assert_cmpint (g_list_length (g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager))), ==, 0);
2226 g_main_loop_unref (loop);
2228 if (om_signal_id != -1)
2229 g_dbus_connection_signal_unsubscribe (c, om_signal_id);
2230 g_clear_object (&o3);
2231 g_clear_object (&o2);
2232 g_clear_object (&o);
2233 g_clear_object (&manager);
2236 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2237 G_CALLBACK (on_object_proxy_added),
2239 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2240 G_CALLBACK (on_object_proxy_removed),
2242 g_clear_object (&pm);
2244 g_clear_object (&c);
2249 /* ---------------------------------------------------------------------------------------------------- */
2252 test_object_manager (void)
2257 loop = g_main_loop_new (NULL, FALSE);
2259 id = g_bus_own_name (G_BUS_TYPE_SESSION,
2260 "org.gtk.GDBus.BindingsTool.Test",
2261 G_BUS_NAME_OWNER_FLAGS_NONE,
2268 g_main_loop_run (loop);
2270 check_object_manager ();
2272 /* uncomment to keep the service around (to e.g. introspect it) */
2273 /* g_main_loop_run (loop); */
2275 unexport_objects ();
2277 g_bus_unown_name (id);
2278 g_main_loop_unref (loop);
2281 /* ---------------------------------------------------------------------------------------------------- */
2282 /* This checks that forcing names via org.gtk.GDBus.Name works (see test-codegen.xml) */
2284 extern gpointer name_forcing_1;
2285 extern gpointer name_forcing_2;
2286 extern gpointer name_forcing_3;
2287 extern gpointer name_forcing_4;
2288 extern gpointer name_forcing_5;
2289 extern gpointer name_forcing_6;
2290 extern gpointer name_forcing_7;
2291 gpointer name_forcing_1 = foo_igen_rocket123_get_type;
2292 gpointer name_forcing_2 = foo_igen_rocket123_call_ignite_xyz;
2293 gpointer name_forcing_3 = foo_igen_rocket123_emit_exploded_xyz;
2294 gpointer name_forcing_4 = foo_igen_rocket123_get_speed_xyz;
2295 gpointer name_forcing_5 = foo_igen_test_ugly_case_interface_call_get_iscsi_servers;
2296 gpointer name_forcing_6 = foo_igen_test_ugly_case_interface_emit_servers_updated_now;
2297 gpointer name_forcing_7 = foo_igen_test_ugly_case_interface_get_ugly_name;
2299 /* ---------------------------------------------------------------------------------------------------- */
2301 /* See https://bugzilla.gnome.org/show_bug.cgi?id=647577#c5 for details */
2303 #define CHECK_FIELD(name, v1, v2) g_assert_cmpint (G_STRUCT_OFFSET (FooiGenChangingInterface##v1##Iface, name), ==, G_STRUCT_OFFSET (FooiGenChangingInterface##v2##Iface, name));
2306 test_interface_stability (void)
2308 CHECK_FIELD(handle_foo_method, V1, V2);
2309 CHECK_FIELD(handle_bar_method, V1, V2);
2310 CHECK_FIELD(handle_baz_method, V1, V2);
2311 CHECK_FIELD(foo_signal, V1, V2);
2312 CHECK_FIELD(bar_signal, V1, V2);
2313 CHECK_FIELD(baz_signal, V1, V2);
2314 CHECK_FIELD(handle_new_method_in2, V2, V10);
2315 CHECK_FIELD(new_signal_in2, V2, V10);
2320 /* ---------------------------------------------------------------------------------------------------- */
2324 * - check that a property with name "Type" is mapped into g-name "type"
2325 * with C accessors get_type_ (to avoid clashing with the GType accessor)
2326 * and set_type_ (for symmetri)
2327 * (see https://bugzilla.gnome.org/show_bug.cgi?id=679473 for details)
2329 * - (could add more tests here)
2333 test_property_naming (void)
2335 gpointer c_getter_name = foo_igen_naming_get_type_;
2336 gpointer c_setter_name = foo_igen_naming_set_type_;
2337 FooiGenNaming *skel;
2339 (void) c_getter_name;
2340 (void) c_setter_name;
2342 skel = foo_igen_naming_skeleton_new ();
2343 g_assert (g_object_class_find_property (G_OBJECT_GET_CLASS (skel), "type") != NULL);
2344 g_object_unref (skel);
2347 /* ---------------------------------------------------------------------------------------------------- */
2353 g_test_init (&argc, &argv, NULL);
2355 g_test_add_func ("/gdbus/codegen/annotations", test_annotations);
2356 g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability);
2357 g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager);
2358 g_test_add_func ("/gdbus/codegen/property-naming", test_property_naming);
2360 return session_bus_run ();