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_igen_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 (FooiGenBar *object,
91 GDBusMethodInvocation *invocation,
92 const gchar *greeting,
96 response = g_strdup_printf ("Word! You said '%s'. I'm Skeleton, btw!", greeting);
97 foo_igen_bar_complete_hello_world (object, invocation, response);
103 on_handle_test_primitive_types (FooiGenBar *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_igen_bar_complete_test_primitive_types (object,
135 10000000 + val_uint64,
148 on_handle_test_non_primitive_types (FooiGenBar *object,
149 GDBusMethodInvocation *invocation,
150 GVariant *dict_s_to_s,
151 GVariant *dict_s_to_pairs,
153 const gchar* const *array_of_strings,
154 const gchar* const *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_strjoinv (", ", (gchar **) array_of_objpaths);
169 g_string_append_printf (str, "array_of_objpaths: [%s] ", s);
171 s = g_variant_print (array_of_signatures, 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_igen_bar_complete_test_non_primitive_types (object, invocation, str->str);
178 g_string_free (str, TRUE);
183 on_handle_request_signal_emission (FooiGenBar *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_igen_bar_emit_test_signal (object, 43, a_strv, a_bytestring_array, a_variant); /* consumes a_variant */
194 foo_igen_bar_complete_request_signal_emission (object, invocation);
200 on_handle_request_multi_property_mods (FooiGenBar *object,
201 GDBusMethodInvocation *invocation,
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 foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
207 foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
208 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
209 foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
210 foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
211 foo_igen_bar_complete_request_multi_property_mods (object, invocation);
216 on_handle_property_cancellation (FooiGenBar *object,
217 GDBusMethodInvocation *invocation,
221 n = foo_igen_bar_get_n (object);
222 /* This queues up a PropertiesChange event */
223 foo_igen_bar_set_n (object, n + 1);
224 /* this modifies the queued up event */
225 foo_igen_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_igen_bar_complete_property_cancellation (object, invocation);
235 /* ---------------------------------------------------------------------------------------------------- */
238 on_handle_force_method (FooiGenBat *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_igen_bat_emit_force_signal (object,
275 foo_igen_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 (FooiGenAuthorize *object,
372 GDBusMethodInvocation *invocation,
375 foo_igen_authorize_complete_check_not_authorized (object, invocation);
380 on_handle_check_authorized (FooiGenAuthorize *object,
381 GDBusMethodInvocation *invocation,
384 foo_igen_authorize_complete_check_authorized (object, invocation);
389 on_handle_check_not_authorized_from_object (FooiGenAuthorize *object,
390 GDBusMethodInvocation *invocation,
393 foo_igen_authorize_complete_check_not_authorized_from_object (object, invocation);
397 /* ---------------------------------------------------------------------------------------------------- */
400 on_handle_get_self (FooiGenMethodThreads *object,
401 GDBusMethodInvocation *invocation,
405 s = g_strdup_printf ("%p", g_thread_self ());
406 foo_igen_method_threads_complete_get_self (object, invocation, s);
411 /* ---------------------------------------------------------------------------------------------------- */
413 static GThread *method_handler_thread = NULL;
415 static FooiGenBar *exported_bar_object = NULL;
416 static FooiGenBat *exported_bat_object = NULL;
417 static FooiGenAuthorize *exported_authorize_object = NULL;
418 static GDBusObjectSkeleton *authorize_enclosing_object = NULL;
419 static FooiGenMethodThreads *exported_thread_object_1 = NULL;
420 static FooiGenMethodThreads *exported_thread_object_2 = NULL;
423 unexport_objects (void)
425 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_bar_object));
426 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_bat_object));
427 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
428 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1));
429 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2));
433 on_bus_acquired (GDBusConnection *connection,
439 /* Test that we can export an object using the generated
440 * FooiGenBarSkeleton subclass. Notes:
442 * 1. We handle methods by simply connecting to the appropriate
445 * 2. Property storage is taken care of by the class; we can
446 * use g_object_get()/g_object_set() (and the generated
447 * C bindings at will)
450 exported_bar_object = foo_igen_bar_skeleton_new ();
451 foo_igen_bar_set_ay (exported_bar_object, "ABCabc");
452 foo_igen_bar_set_y (exported_bar_object, 42);
453 foo_igen_bar_set_d (exported_bar_object, 43.0);
454 foo_igen_bar_set_finally_normal_name (exported_bar_object, "There aint no place like home");
455 foo_igen_bar_set_writeonly_property (exported_bar_object, "Mr. Burns");
457 /* The following works because it's on the Skeleton object - it will
458 * fail (at run-time) on a Proxy (see on_proxy_appeared() below)
460 foo_igen_bar_set_readonly_property (exported_bar_object, "blah");
461 g_assert_cmpstr (foo_igen_bar_get_writeonly_property (exported_bar_object), ==, "Mr. Burns");
463 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bar_object),
467 g_assert_no_error (error);
468 g_signal_connect (exported_bar_object,
469 "handle-hello-world",
470 G_CALLBACK (on_handle_hello_world),
472 g_signal_connect (exported_bar_object,
473 "handle-test-primitive-types",
474 G_CALLBACK (on_handle_test_primitive_types),
476 g_signal_connect (exported_bar_object,
477 "handle-test-non-primitive-types",
478 G_CALLBACK (on_handle_test_non_primitive_types),
480 g_signal_connect (exported_bar_object,
481 "handle-request-signal-emission",
482 G_CALLBACK (on_handle_request_signal_emission),
484 g_signal_connect (exported_bar_object,
485 "handle-request-multi-property-mods",
486 G_CALLBACK (on_handle_request_multi_property_mods),
488 g_signal_connect (exported_bar_object,
489 "handle-property-cancellation",
490 G_CALLBACK (on_handle_property_cancellation),
493 exported_bat_object = foo_igen_bat_skeleton_new ();
494 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bat_object),
498 g_assert_no_error (error);
499 g_signal_connect (exported_bat_object,
500 "handle-force-method",
501 G_CALLBACK (on_handle_force_method),
503 g_object_set (exported_bat_object,
504 "force-i", g_variant_new_int32 (43),
505 "force-s", g_variant_new_string ("prop string"),
506 "force-ay", g_variant_new_bytestring ("prop bytestring\xff"),
507 "force-struct", g_variant_new ("(i)", 4300),
510 authorize_enclosing_object = g_dbus_object_skeleton_new ("/authorize");
511 g_signal_connect (authorize_enclosing_object,
513 G_CALLBACK (my_object_authorize_method_handler),
515 exported_authorize_object = foo_igen_authorize_skeleton_new ();
516 g_dbus_object_skeleton_add_interface (authorize_enclosing_object,
517 G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
518 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_authorize_object),
522 g_assert_no_error (error);
523 g_signal_connect (exported_authorize_object,
524 "g-authorize-method",
525 G_CALLBACK (my_g_authorize_method_handler),
527 g_signal_connect (exported_authorize_object,
528 "handle-check-not-authorized",
529 G_CALLBACK (on_handle_check_not_authorized),
531 g_signal_connect (exported_authorize_object,
532 "handle-check-authorized",
533 G_CALLBACK (on_handle_check_authorized),
535 g_signal_connect (exported_authorize_object,
536 "handle-check-not-authorized-from-object",
537 G_CALLBACK (on_handle_check_not_authorized_from_object),
541 /* only object 1 has the G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD flag set */
542 exported_thread_object_1 = foo_igen_method_threads_skeleton_new ();
543 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
544 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
546 g_assert (!g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1), connection));
547 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
551 g_assert_no_error (error);
552 g_signal_connect (exported_thread_object_1,
554 G_CALLBACK (on_handle_get_self),
556 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);
558 exported_thread_object_2 = foo_igen_method_threads_skeleton_new ();
559 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2),
563 g_assert_no_error (error);
564 g_signal_connect (exported_thread_object_2,
566 G_CALLBACK (on_handle_get_self),
569 g_assert_cmpint (g_dbus_interface_skeleton_get_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2)), ==, G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
571 method_handler_thread = g_thread_self ();
574 static gpointer check_proxies_in_thread (gpointer user_data);
577 on_name_acquired (GDBusConnection *connection,
581 GMainLoop *loop = user_data;
583 g_thread_new ("check-proxies",
584 check_proxies_in_thread,
589 on_name_lost (GDBusConnection *connection,
593 g_assert_not_reached ();
596 /* ---------------------------------------------------------------------------------------------------- */
600 GMainLoop *thread_loop;
603 guint num_g_properties_changed;
604 gboolean received_test_signal;
610 on_notify_u (GObject *object,
614 ClientData *data = user_data;
615 g_assert_cmpstr (pspec->name, ==, "u");
616 data->num_notify_u += 1;
620 on_notify_n (GObject *object,
624 ClientData *data = user_data;
625 g_assert_cmpstr (pspec->name, ==, "n");
626 data->num_notify_n += 1;
630 on_g_properties_changed (GDBusProxy *_proxy,
631 GVariant *changed_properties,
632 const gchar* const *invalidated_properties,
635 ClientData *data = user_data;
636 FooiGenBar *proxy = FOO_IGEN_BAR (_proxy);
638 g_assert_cmpint (g_variant_n_children (changed_properties), ==, 2);
640 if (data->num_g_properties_changed == 0)
642 g_assert_cmpint (data->initial_y, ==, foo_igen_bar_get_y (proxy) - 2);
643 g_assert_cmpint (data->initial_i, ==, foo_igen_bar_get_i (proxy) - 2);
645 else if (data->num_g_properties_changed == 1)
647 g_assert_cmpint (data->initial_y, ==, foo_igen_bar_get_y (proxy) - 3);
648 g_assert_cmpint (data->initial_i, ==, foo_igen_bar_get_i (proxy) - 3);
651 g_assert_not_reached ();
653 data->num_g_properties_changed++;
655 if (data->num_g_properties_changed == 2)
656 g_main_loop_quit (data->thread_loop);
660 on_test_signal (FooiGenBar *proxy,
662 const gchar* const *array_of_strings,
663 const gchar* const *array_of_bytestrings,
664 GVariant *dict_s_to_pairs,
667 ClientData *data = user_data;
669 g_assert_cmpint (val_int32, ==, 43);
670 g_assert_cmpstr (array_of_strings[0], ==, "foo");
671 g_assert_cmpstr (array_of_strings[1], ==, "bar");
672 g_assert (array_of_strings[2] == NULL);
673 g_assert_cmpstr (array_of_bytestrings[0], ==, "foo\xff");
674 g_assert_cmpstr (array_of_bytestrings[1], ==, "bar\xff");
675 g_assert (array_of_bytestrings[2] == NULL);
677 data->received_test_signal = TRUE;
678 g_main_loop_quit (data->thread_loop);
682 on_property_cancellation_cb (FooiGenBar *proxy,
686 ClientData *data = user_data;
688 GError *error = NULL;
691 ret = foo_igen_bar_call_property_cancellation_finish (proxy, res, &error);
692 g_assert_no_error (error);
695 g_main_loop_quit (data->thread_loop);
699 check_bar_proxy (FooiGenBar *proxy,
700 GMainLoop *thread_loop)
702 const gchar *array_of_strings[3] = {"one", "two", NULL};
703 const gchar *array_of_strings_2[3] = {"one2", "two2", NULL};
704 const gchar *array_of_objpaths[3] = {"/one", "/one/two", NULL};
705 const gchar *array_of_bytestrings[3] = {"one\xff", "two\xff", NULL};
707 gboolean ret_val_boolean;
708 gint16 ret_val_int16;
709 guint16 ret_val_uint16;
711 guint ret_val_uint32;
712 gint64 ret_val_int64;
713 guint64 ret_val_uint64;
714 gdouble ret_val_double;
715 gchar *ret_val_string;
716 gchar *ret_val_objpath;
717 gchar *ret_val_signature;
718 gchar *ret_val_bytestring;
744 gchar **val_unset_as;
745 gchar **val_unset_ao;
746 GVariant *val_unset_ag;
747 GVariant *val_unset_struct;
748 gchar *val_finally_normal_name;
751 const gchar *const *read_as;
752 const gchar *const *read_as2;
753 const gchar *const *read_as3;
755 data = g_new0 (ClientData, 1);
756 data->thread_loop = thread_loop;
758 v = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "y");
759 g_assert (v != NULL);
762 /* set empty values to non-empty */
772 val_unset_struct = NULL;
773 /* check properties */
791 "unset_i", &val_unset_i,
792 "unset_d", &val_unset_d,
793 "unset_s", &val_unset_s,
794 "unset_o", &val_unset_o,
795 "unset_g", &val_unset_g,
796 "unset_ay", &val_unset_ay,
797 "unset_as", &val_unset_as,
798 "unset_ao", &val_unset_ao,
799 "unset_ag", &val_unset_ag,
800 "unset_struct", &val_unset_struct,
801 "finally-normal-name", &val_finally_normal_name,
803 g_assert_cmpint (val_y, ==, 42);
804 g_assert_cmpstr (val_finally_normal_name, ==, "There aint no place like home");
808 g_assert_cmpstr (val_ay, ==, "ABCabc");
812 g_variant_unref (val_ag);
813 g_free (val_finally_normal_name);
814 /* check empty values */
815 g_assert_cmpint (val_unset_i, ==, 0);
816 g_assert_cmpfloat (val_unset_d, ==, 0.0);
817 g_assert_cmpstr (val_unset_s, ==, "");
818 g_assert_cmpstr (val_unset_o, ==, "/");
819 g_assert_cmpstr (val_unset_g, ==, "");
820 g_free (val_unset_s);
821 g_free (val_unset_o);
822 g_free (val_unset_g);
823 g_assert_cmpstr (val_unset_ay, ==, "");
824 g_assert (val_unset_as[0] == NULL);
825 g_assert (val_unset_ao[0] == NULL);
826 g_assert (g_variant_is_of_type (val_unset_ag, G_VARIANT_TYPE ("ag")));
827 g_assert (g_variant_is_of_type (val_unset_struct, G_VARIANT_TYPE ("(idsogayasaoag)")));
828 s = g_variant_print (val_unset_struct, TRUE);
829 g_assert_cmpstr (s, ==, "(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])");
831 g_free (val_unset_ay);
832 g_strfreev (val_unset_as);
833 g_strfreev (val_unset_ao);
834 g_variant_unref (val_unset_ag);
835 g_variant_unref (val_unset_struct);
837 /* Try setting a property. This causes the generated glue to invoke
838 * the org.fd.DBus.Properties.Set() method asynchronously. So we
839 * have to wait for properties-changed...
841 foo_igen_bar_set_finally_normal_name (proxy, "foo!");
842 _g_assert_property_notify (proxy, "finally-normal-name");
843 g_assert_cmpstr (foo_igen_bar_get_finally_normal_name (proxy), ==, "foo!");
845 /* Try setting properties that requires memory management. This
846 * is to exercise the paths that frees the references.
853 "ay", g_variant_new_parsed ("[byte 0x65, 0x67]"),
854 "as", array_of_strings,
855 "ao", array_of_objpaths,
856 "ag", g_variant_new_parsed ("[@g 'ass', 'git']"),
860 ret = foo_igen_bar_call_test_primitive_types_sync (proxy,
887 NULL, /* GCancellable */
889 g_assert_no_error (error);
893 ret = foo_igen_bar_call_test_non_primitive_types_sync (proxy,
894 g_variant_new_parsed ("{'one': 'red',"
896 g_variant_new_parsed ("{'first': (42, 42), "
897 "'second': (43, 43)}"),
898 g_variant_new_parsed ("(42, 'foo', 'bar')"),
901 g_variant_new_parsed ("[@g 'ass', 'git']"),
902 array_of_bytestrings,
904 NULL, /* GCancellable */
907 g_assert_no_error (error);
910 /* Check that org.freedesktop.DBus.Error.UnknownMethod is returned on
911 * unimplemented methods.
914 ret = foo_igen_bar_call_unimplemented_method_sync (proxy, NULL /* GCancellable */, &error);
915 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
916 g_error_free (error);
920 g_signal_connect (proxy,
922 G_CALLBACK (on_test_signal),
925 ret = foo_igen_bar_call_request_signal_emission_sync (proxy, 0, NULL, &error);
926 g_assert_no_error (error);
929 g_assert (!data->received_test_signal);
930 g_main_loop_run (thread_loop);
931 g_assert (data->received_test_signal);
933 /* Try setting a property. This causes the generated glue to invoke
934 * the org.fd.DBus.Properties.Set() method asynchronously. So we
935 * have to wait for properties-changed...
937 foo_igen_bar_set_finally_normal_name (proxy, "hey back!");
938 _g_assert_property_notify (proxy, "finally-normal-name");
939 g_assert_cmpstr (foo_igen_bar_get_finally_normal_name (proxy), ==, "hey back!");
941 /* Check that multiple calls to a strv getter works... and that
942 * updates on them works as well (See comment for "property vfuncs"
943 * in gio/gdbus-codegen/codegen.py for details)
945 read_as = foo_igen_bar_get_as (proxy);
946 read_as2 = foo_igen_bar_get_as (proxy);
947 g_assert_cmpint (g_strv_length ((gchar **) read_as), ==, 2);
948 g_assert_cmpstr (read_as[0], ==, "one");
949 g_assert_cmpstr (read_as[1], ==, "two");
950 g_assert (read_as == read_as2); /* this is more testing an implementation detail */
952 "as", array_of_strings_2,
954 _g_assert_property_notify (proxy, "as");
955 read_as3 = foo_igen_bar_get_as (proxy);
956 g_assert_cmpint (g_strv_length ((gchar **) read_as3), ==, 2);
957 g_assert_cmpstr (read_as3[0], ==, "one2");
958 g_assert_cmpstr (read_as3[1], ==, "two2");
960 /* Check that grouping changes in idle works.
962 * See on_handle_request_multi_property_mods(). The server should
963 * emit exactly two PropertiesChanged signals each containing two
966 * On the first reception, y and i should both be increased by
967 * two. On the second reception, only by one. The signal handler
970 * This also checks that _drain_notify() works.
972 data->initial_y = foo_igen_bar_get_y (proxy);
973 data->initial_i = foo_igen_bar_get_i (proxy);
974 g_signal_connect (proxy,
975 "g-properties-changed",
976 G_CALLBACK (on_g_properties_changed),
979 ret = foo_igen_bar_call_request_multi_property_mods_sync (proxy, NULL, &error);
980 g_assert_no_error (error);
982 g_main_loop_run (thread_loop);
983 g_assert_cmpint (data->num_g_properties_changed, ==, 2);
984 g_signal_handlers_disconnect_by_func (proxy,
985 G_CALLBACK (on_g_properties_changed),
988 /* Check that we don't emit PropertiesChanged() if the property
989 * didn't change... we actually get two notifies.. one for the
990 * local set (without a value change) and one when receiving
991 * the PropertiesChanged() signal generated from the remote end.
993 g_assert_cmpint (data->num_notify_u, ==, 0);
994 g_signal_connect (proxy,
996 G_CALLBACK (on_notify_u),
998 foo_igen_bar_set_u (proxy, 1042);
999 g_assert_cmpint (data->num_notify_u, ==, 1);
1000 g_assert_cmpint (foo_igen_bar_get_u (proxy), ==, 0);
1001 _g_assert_property_notify (proxy, "u");
1002 g_assert_cmpint (foo_igen_bar_get_u (proxy), ==, 1042);
1003 g_assert_cmpint (data->num_notify_u, ==, 2);
1005 /* Now change u again to the same value.. this will cause a
1006 * local notify:: notify and the usual Properties.Set() call
1008 * (Btw, why also the Set() call if the value in the cache is
1009 * the same? Because someone else might have changed it
1010 * in the mean time and we're just waiting to receive the
1011 * PropertiesChanged() signal...)
1013 * More tricky - how do we check for the *absence* of the
1014 * notification that u changed? Simple: we change another
1015 * property and wait for that PropertiesChanged() message
1018 foo_igen_bar_set_u (proxy, 1042);
1019 g_assert_cmpint (data->num_notify_u, ==, 3);
1021 g_assert_cmpint (data->num_notify_n, ==, 0);
1022 g_signal_connect (proxy,
1024 G_CALLBACK (on_notify_n),
1026 foo_igen_bar_set_n (proxy, 10042);
1027 g_assert_cmpint (data->num_notify_n, ==, 1);
1028 g_assert_cmpint (foo_igen_bar_get_n (proxy), ==, 0);
1029 _g_assert_property_notify (proxy, "n");
1030 g_assert_cmpint (foo_igen_bar_get_n (proxy), ==, 10042);
1031 g_assert_cmpint (data->num_notify_n, ==, 2);
1032 /* Checks that u didn't change at all */
1033 g_assert_cmpint (data->num_notify_u, ==, 3);
1035 /* Now we check that if the service does
1037 * guint n = foo_igen_bar_get_n (foo);
1038 * foo_igen_bar_set_n (foo, n + 1);
1039 * foo_igen_bar_set_n (foo, n);
1041 * then no PropertiesChanged() signal is emitted!
1044 foo_igen_bar_call_property_cancellation (proxy,
1045 NULL, /* GCancellable */
1046 (GAsyncReadyCallback) on_property_cancellation_cb,
1048 g_main_loop_run (thread_loop);
1049 /* Checks that n didn't change at all */
1050 g_assert_cmpint (data->num_notify_n, ==, 2);
1056 /* ---------------------------------------------------------------------------------------------------- */
1059 on_force_signal (FooiGenBat *proxy,
1063 GVariant *force_struct,
1066 gboolean *signal_received = user_data;
1069 g_assert (!(*signal_received));
1071 g_assert_cmpint (g_variant_get_int32 (force_i), ==, 42 + 10);
1072 g_assert_cmpstr (g_variant_get_string (force_s, NULL), ==, "a string_foo");
1073 g_assert_cmpstr (g_variant_get_bytestring (force_ay), ==, "a bytestring\xff_foo\xff");
1074 g_variant_get (force_struct, "(i)", &val);
1075 g_assert_cmpint (val, ==, 4200 + 10);
1077 *signal_received = TRUE;
1081 check_bat_proxy (FooiGenBat *proxy,
1082 GMainLoop *thread_loop)
1088 GVariant *ret_struct;
1090 gboolean force_signal_received;
1092 /* --------------------------------------------------- */
1093 /* Check type-mapping where we force use of a GVariant */
1094 /* --------------------------------------------------- */
1096 /* check properties */
1097 g_object_get (proxy,
1100 "force-ay", &ret_ay,
1101 "force-struct", &ret_struct,
1103 g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 43);
1104 g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "prop string");
1105 g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "prop bytestring\xff");
1106 g_variant_get (ret_struct, "(i)", &val);
1107 g_assert_cmpint (val, ==, 4300);
1108 g_variant_unref (ret_i);
1109 g_variant_unref (ret_s);
1110 g_variant_unref (ret_ay);
1111 g_variant_unref (ret_struct);
1113 /* check method and signal */
1114 force_signal_received = FALSE;
1115 g_signal_connect (proxy,
1117 G_CALLBACK (on_force_signal),
1118 &force_signal_received);
1121 foo_igen_bat_call_force_method_sync (proxy,
1122 g_variant_new_int32 (42),
1123 g_variant_new_string ("a string"),
1124 g_variant_new_bytestring ("a bytestring\xff"),
1125 g_variant_new ("(i)", 4200),
1130 NULL, /* GCancellable* */
1132 g_assert_no_error (error);
1133 g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 42 + 10);
1134 g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "a string_foo");
1135 g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "a bytestring\xff_foo\xff");
1136 g_variant_get (ret_struct, "(i)", &val);
1137 g_assert_cmpint (val, ==, 4200 + 10);
1138 g_variant_unref (ret_i);
1139 g_variant_unref (ret_s);
1140 g_variant_unref (ret_ay);
1141 g_variant_unref (ret_struct);
1142 _g_assert_signal_received (proxy, "force-signal");
1143 g_assert (force_signal_received);
1146 /* ---------------------------------------------------------------------------------------------------- */
1149 check_authorize_proxy (FooiGenAuthorize *proxy,
1150 GMainLoop *thread_loop)
1155 /* Check that g-authorize-method works as intended */
1158 ret = foo_igen_authorize_call_check_not_authorized_sync (proxy, NULL, &error);
1159 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1160 g_error_free (error);
1164 ret = foo_igen_authorize_call_check_authorized_sync (proxy, NULL, &error);
1165 g_assert_no_error (error);
1169 ret = foo_igen_authorize_call_check_not_authorized_from_object_sync (proxy, NULL, &error);
1170 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
1171 g_error_free (error);
1175 /* ---------------------------------------------------------------------------------------------------- */
1178 get_self_via_proxy (FooiGenMethodThreads *proxy_1)
1186 ret = foo_igen_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
1187 g_assert_no_error (error);
1190 g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
1198 check_thread_proxies (FooiGenMethodThreads *proxy_1,
1199 FooiGenMethodThreads *proxy_2,
1200 GMainLoop *thread_loop)
1202 /* proxy_1 is indeed using threads so should never get the handler thread */
1203 g_assert (get_self_via_proxy (proxy_1) != method_handler_thread);
1205 /* proxy_2 is not using threads so should get the handler thread */
1206 g_assert (get_self_via_proxy (proxy_2) == method_handler_thread);
1209 /* ---------------------------------------------------------------------------------------------------- */
1212 check_proxies_in_thread (gpointer user_data)
1214 GMainLoop *loop = user_data;
1215 GMainContext *thread_context;
1216 GMainLoop *thread_loop;
1218 FooiGenBar *bar_proxy;
1219 FooiGenBat *bat_proxy;
1220 FooiGenAuthorize *authorize_proxy;
1221 FooiGenMethodThreads *thread_proxy_1;
1222 FooiGenMethodThreads *thread_proxy_2;
1224 thread_context = g_main_context_new ();
1225 thread_loop = g_main_loop_new (thread_context, FALSE);
1226 g_main_context_push_thread_default (thread_context);
1228 /* Check the object */
1230 bar_proxy = foo_igen_bar_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1231 G_DBUS_PROXY_FLAGS_NONE,
1232 "org.gtk.GDBus.BindingsTool.Test",
1234 NULL, /* GCancellable* */
1236 check_bar_proxy (bar_proxy, thread_loop);
1237 g_assert_no_error (error);
1238 g_object_unref (bar_proxy);
1241 bat_proxy = foo_igen_bat_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1242 G_DBUS_PROXY_FLAGS_NONE,
1243 "org.gtk.GDBus.BindingsTool.Test",
1245 NULL, /* GCancellable* */
1247 check_bat_proxy (bat_proxy, thread_loop);
1248 g_assert_no_error (error);
1249 g_object_unref (bat_proxy);
1252 authorize_proxy = foo_igen_authorize_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1253 G_DBUS_PROXY_FLAGS_NONE,
1254 "org.gtk.GDBus.BindingsTool.Test",
1256 NULL, /* GCancellable* */
1258 check_authorize_proxy (authorize_proxy, thread_loop);
1259 g_assert_no_error (error);
1260 g_object_unref (authorize_proxy);
1263 thread_proxy_1 = foo_igen_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1264 G_DBUS_PROXY_FLAGS_NONE,
1265 "org.gtk.GDBus.BindingsTool.Test",
1266 "/method_threads_1",
1267 NULL, /* GCancellable* */
1269 g_assert_no_error (error);
1270 thread_proxy_2 = foo_igen_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1271 G_DBUS_PROXY_FLAGS_NONE,
1272 "org.gtk.GDBus.BindingsTool.Test",
1273 "/method_threads_2",
1274 NULL, /* GCancellable* */
1276 g_assert_no_error (error);
1277 check_thread_proxies (thread_proxy_1, thread_proxy_2, thread_loop);
1278 g_object_unref (thread_proxy_1);
1279 g_object_unref (thread_proxy_2);
1281 g_main_loop_unref (thread_loop);
1282 g_main_context_unref (thread_context);
1284 /* this breaks out of the loop in main() (below) */
1285 g_main_loop_quit (loop);
1289 /* ---------------------------------------------------------------------------------------------------- */
1298 introspect_cb (GDBusConnection *connection,
1302 IntrospectData *data = user_data;
1307 result = g_dbus_connection_call_finish (connection,
1310 g_assert_no_error (error);
1311 g_assert (result != NULL);
1312 g_variant_get (result, "(s)", &data->xml);
1313 g_variant_unref (result);
1315 g_main_loop_quit (data->loop);
1318 static GDBusNodeInfo *
1319 introspect (GDBusConnection *connection,
1321 const gchar *object_path,
1325 GDBusNodeInfo *node_info;
1326 IntrospectData *data;
1328 data = g_new0 (IntrospectData, 1);
1332 /* do this async to avoid deadlocks */
1333 g_dbus_connection_call (connection,
1336 "org.freedesktop.DBus.Introspectable",
1339 G_VARIANT_TYPE ("(s)"),
1340 G_DBUS_CALL_FLAGS_NONE,
1343 (GAsyncReadyCallback) introspect_cb,
1345 g_main_loop_run (loop);
1346 g_assert (data->xml != NULL);
1349 node_info = g_dbus_node_info_new_for_xml (data->xml, &error);
1350 g_assert_no_error (error);
1351 g_assert (node_info != NULL);
1359 count_interfaces (GDBusNodeInfo *info)
1362 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1368 count_nodes (GDBusNodeInfo *info)
1371 for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
1377 has_interface (GDBusNodeInfo *info,
1381 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1383 if (g_strcmp0 (info->interfaces[n]->name, name) == 0)
1389 /* ---------------------------------------------------------------------------------------------------- */
1394 } OMGetManagedObjectsData;
1397 om_get_all_cb (GDBusConnection *connection,
1401 OMGetManagedObjectsData *data = user_data;
1405 data->result = g_dbus_connection_call_finish (connection,
1408 g_assert_no_error (error);
1409 g_assert (data->result != NULL);
1410 g_main_loop_quit (data->loop);
1414 om_check_get_all (GDBusConnection *c,
1418 OMGetManagedObjectsData data;
1424 /* do this async to avoid deadlocks */
1425 g_dbus_connection_call (c,
1426 g_dbus_connection_get_unique_name (c),
1428 "org.freedesktop.DBus.ObjectManager",
1429 "GetManagedObjects",
1431 G_VARIANT_TYPE ("(a{oa{sa{sv}}})"),
1432 G_DBUS_CALL_FLAGS_NONE,
1435 (GAsyncReadyCallback) om_get_all_cb,
1437 g_main_loop_run (loop);
1438 g_assert (data.result != NULL);
1439 s = g_variant_print (data.result, TRUE);
1440 g_assert_cmpstr (s, ==, str);
1442 g_variant_unref (data.result);
1450 guint num_object_proxy_added_signals;
1451 guint num_object_proxy_removed_signals;
1452 guint num_interface_added_signals;
1453 guint num_interface_removed_signals;
1457 my_pstrcmp (const gchar **a, const gchar **b)
1459 return g_strcmp0 (*a, *b);
1463 om_check_interfaces_added (const gchar *signal_name,
1464 GVariant *parameters,
1465 const gchar *object_path,
1466 const gchar *first_interface_name,
1472 GPtrArray *interfaces;
1473 GPtrArray *interfaces_in_message;
1477 interfaces = g_ptr_array_new ();
1478 g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1479 va_start (var_args, first_interface_name);
1482 str = va_arg (var_args, const gchar *);
1485 g_ptr_array_add (interfaces, (gpointer) str);
1490 g_variant_get (parameters, "(&o*)", &path, &array);
1491 g_assert_cmpstr (signal_name, ==, "InterfacesAdded");
1492 g_assert_cmpstr (path, ==, object_path);
1493 g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1494 interfaces_in_message = g_ptr_array_new ();
1495 for (n = 0; n < interfaces->len; n++)
1497 const gchar *iface_name;
1498 g_variant_get_child (array, n, "{&sa{sv}}", &iface_name, NULL);
1499 g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1501 g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1502 g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1503 g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1504 for (n = 0; n < interfaces->len; n++)
1505 g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1506 g_ptr_array_unref (interfaces_in_message);
1507 g_ptr_array_unref (interfaces);
1508 g_variant_unref (array);
1512 om_check_interfaces_removed (const gchar *signal_name,
1513 GVariant *parameters,
1514 const gchar *object_path,
1515 const gchar *first_interface_name,
1521 GPtrArray *interfaces;
1522 GPtrArray *interfaces_in_message;
1526 interfaces = g_ptr_array_new ();
1527 g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1528 va_start (var_args, first_interface_name);
1531 str = va_arg (var_args, const gchar *);
1534 g_ptr_array_add (interfaces, (gpointer) str);
1539 g_variant_get (parameters, "(&o*)", &path, &array);
1540 g_assert_cmpstr (signal_name, ==, "InterfacesRemoved");
1541 g_assert_cmpstr (path, ==, object_path);
1542 g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1543 interfaces_in_message = g_ptr_array_new ();
1544 for (n = 0; n < interfaces->len; n++)
1546 const gchar *iface_name;
1547 g_variant_get_child (array, n, "&s", &iface_name, NULL);
1548 g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1550 g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1551 g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1552 g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1553 for (n = 0; n < interfaces->len; n++)
1554 g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1555 g_ptr_array_unref (interfaces_in_message);
1556 g_ptr_array_unref (interfaces);
1557 g_variant_unref (array);
1561 om_on_signal (GDBusConnection *connection,
1562 const gchar *sender_name,
1563 const gchar *object_path,
1564 const gchar *interface_name,
1565 const gchar *signal_name,
1566 GVariant *parameters,
1569 OMData *om_data = user_data;
1571 //g_debug ("foo: %s", g_variant_print (parameters, TRUE));
1573 switch (om_data->state)
1577 g_print ("failing and om_data->state=%d on signal %s, params=%s\n",
1580 g_variant_print (parameters, TRUE));
1581 g_assert_not_reached ();
1585 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1586 "org.project.Bar", NULL);
1588 g_main_loop_quit (om_data->loop);
1592 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1593 "org.project.Bar", NULL);
1595 /* keep running the loop */
1599 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1600 "org.project.Bar", NULL);
1602 g_main_loop_quit (om_data->loop);
1606 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1607 "org.project.Bar", NULL);
1609 /* keep running the loop */
1613 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1614 "org.project.Bar", NULL);
1615 om_data->state = 10;
1616 g_main_loop_quit (om_data->loop);
1620 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1621 "org.project.Bat", NULL);
1622 om_data->state = 12;
1623 g_main_loop_quit (om_data->loop);
1627 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1628 "org.project.Bar", NULL);
1629 om_data->state = 14;
1630 g_main_loop_quit (om_data->loop);
1634 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1635 "org.project.Bat", NULL);
1636 om_data->state = 16;
1637 g_main_loop_quit (om_data->loop);
1641 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1642 "com.acme.Coyote", NULL);
1643 om_data->state = 18;
1644 g_main_loop_quit (om_data->loop);
1648 om_check_interfaces_added (signal_name, parameters, "/managed/second",
1649 "org.project.Bat", "org.project.Bar", NULL);
1650 om_data->state = 102;
1651 g_main_loop_quit (om_data->loop);
1655 om_check_interfaces_removed (signal_name, parameters, "/managed/second",
1656 "org.project.Bat", "org.project.Bar", NULL);
1657 om_data->state = 104;
1658 g_main_loop_quit (om_data->loop);
1662 om_check_interfaces_added (signal_name, parameters, "/managed/first_1",
1663 "com.acme.Coyote", NULL);
1664 om_data->state = 201;
1665 g_main_loop_quit (om_data->loop);
1670 static GAsyncResult *om_res = NULL;
1673 om_pm_start_cb (FooiGenObjectManagerClient *manager,
1677 GMainLoop *loop = user_data;
1678 om_res = g_object_ref (res);
1679 g_main_loop_quit (loop);
1683 on_interface_added (GDBusObject *object,
1684 GDBusInterface *interface,
1687 OMData *om_data = user_data;
1688 om_data->num_interface_added_signals += 1;
1692 on_interface_removed (GDBusObject *object,
1693 GDBusInterface *interface,
1696 OMData *om_data = user_data;
1697 om_data->num_interface_removed_signals += 1;
1701 on_object_proxy_added (GDBusObjectManagerClient *manager,
1702 GDBusObjectProxy *object_proxy,
1705 OMData *om_data = user_data;
1706 om_data->num_object_proxy_added_signals += 1;
1707 g_signal_connect (object_proxy,
1709 G_CALLBACK (on_interface_added),
1711 g_signal_connect (object_proxy,
1712 "interface-removed",
1713 G_CALLBACK (on_interface_removed),
1718 on_object_proxy_removed (GDBusObjectManagerClient *manager,
1719 GDBusObjectProxy *object_proxy,
1722 OMData *om_data = user_data;
1723 om_data->num_object_proxy_removed_signals += 1;
1724 g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1725 G_CALLBACK (on_interface_added),
1727 g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1728 G_CALLBACK (on_interface_removed),
1733 property_d_changed (GObject *object,
1737 gboolean *changed = user_data;
1743 om_check_property_and_signal_emission (GMainLoop *loop,
1744 FooiGenBar *skeleton,
1747 gboolean d_changed = FALSE;
1750 /* First PropertiesChanged */
1751 g_assert_cmpint (foo_igen_bar_get_i (skeleton), ==, 0);
1752 g_assert_cmpint (foo_igen_bar_get_i (proxy), ==, 0);
1753 foo_igen_bar_set_i (skeleton, 1);
1754 _g_assert_property_notify (proxy, "i");
1755 g_assert_cmpint (foo_igen_bar_get_i (skeleton), ==, 1);
1756 g_assert_cmpint (foo_igen_bar_get_i (proxy), ==, 1);
1758 /* Double-check the gdouble case */
1759 g_assert_cmpfloat (foo_igen_bar_get_d (skeleton), ==, 0.0);
1760 g_assert_cmpfloat (foo_igen_bar_get_d (proxy), ==, 0.0);
1761 foo_igen_bar_set_d (skeleton, 1.0);
1762 _g_assert_property_notify (proxy, "d");
1764 /* Verify that re-setting it to the same value doesn't cause a
1765 * notify on the proxy, by taking advantage of the fact that
1766 * notifications are serialized.
1768 handler = g_signal_connect (proxy, "notify::d",
1769 G_CALLBACK (property_d_changed), &d_changed);
1770 foo_igen_bar_set_d (skeleton, 1.0);
1771 foo_igen_bar_set_i (skeleton, 2);
1772 _g_assert_property_notify (proxy, "i");
1773 g_assert (d_changed == FALSE);
1774 g_signal_handler_disconnect (proxy, handler);
1776 /* Then just a regular signal */
1777 foo_igen_bar_emit_another_signal (skeleton, "word");
1778 _g_assert_signal_received (proxy, "another-signal");
1782 check_object_manager (void)
1784 FooiGenObjectSkeleton *o = NULL;
1785 FooiGenObjectSkeleton *o2 = NULL;
1786 FooiGenObjectSkeleton *o3 = NULL;
1787 GDBusInterfaceSkeleton *i;
1789 GDBusObjectManagerServer *manager = NULL;
1790 GDBusNodeInfo *info;
1793 OMData *om_data = NULL;
1794 guint om_signal_id = -1;
1795 GDBusObjectManager *pm = NULL;
1796 GList *object_proxies;
1800 FooiGenBar *bar_skeleton;
1801 GDBusInterface *iface;
1802 gchar *path, *name, *name_owner;
1803 GDBusConnection *c2;
1804 GDBusObjectManagerClientFlags flags;
1806 loop = g_main_loop_new (NULL, FALSE);
1808 om_data = g_new0 (OMData, 1);
1809 om_data->loop = loop;
1813 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1814 g_assert_no_error (error);
1815 g_assert (c != NULL);
1817 om_signal_id = g_dbus_connection_signal_subscribe (c,
1819 "org.freedesktop.DBus.ObjectManager",
1821 NULL, /* object_path */
1823 G_DBUS_SIGNAL_FLAGS_NONE,
1826 NULL); /* user_data_free_func */
1828 /* Our GDBusObjectManagerClient tests are simple - we basically just count the
1829 * number of times the various signals have been emitted (we don't check
1830 * that the right objects/interfaces are passed though - that's checked
1831 * in the lower-level tests in om_on_signal()...)
1833 * Note that these tests rely on the D-Bus signal handlers used by
1834 * GDBusObjectManagerClient firing before om_on_signal().
1837 pm = foo_igen_object_manager_client_new_sync (c,
1838 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1839 g_dbus_connection_get_unique_name (c),
1841 NULL, /* GCancellable */
1843 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
1844 g_error_free (error);
1845 g_assert (pm == NULL);
1847 manager = g_dbus_object_manager_server_new ("/managed");
1849 g_assert (g_dbus_object_manager_server_get_connection (manager) == NULL);
1851 g_dbus_object_manager_server_set_connection (manager, c);
1853 g_assert_cmpstr (g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (manager)), ==, "/managed");
1854 g_object_get (manager, "object-path", &path, "connection", &c2, NULL);
1855 g_assert_cmpstr (path, ==, "/managed");
1858 g_clear_object (&c2);
1860 /* Check that the manager object is visible */
1861 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
1862 g_assert_cmpint (count_interfaces (info), ==, 4); /* ObjectManager + Properties,Introspectable,Peer */
1863 g_assert (has_interface (info, "org.freedesktop.DBus.ObjectManager"));
1864 g_assert_cmpint (count_nodes (info), ==, 0);
1865 g_dbus_node_info_unref (info);
1867 /* Check GetManagedObjects() - should be empty since we have no objects */
1868 om_check_get_all (c, loop,
1869 "(@a{oa{sa{sv}}} {},)");
1871 /* Now try to create the proxy manager again - this time it should work */
1873 foo_igen_object_manager_client_new (c,
1874 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1875 g_dbus_connection_get_unique_name (c),
1877 NULL, /* GCancellable */
1878 (GAsyncReadyCallback) om_pm_start_cb,
1880 g_main_loop_run (loop);
1882 pm = foo_igen_object_manager_client_new_finish (om_res, &error);
1883 g_clear_object (&om_res);
1884 g_assert_no_error (error);
1885 g_assert (pm != NULL);
1886 g_signal_connect (pm,
1888 G_CALLBACK (on_object_proxy_added),
1890 g_signal_connect (pm,
1892 G_CALLBACK (on_object_proxy_removed),
1895 g_assert_cmpstr (g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (pm)), ==, "/managed");
1897 "object-path", &path,
1900 "name-owner", &name_owner,
1903 g_assert_cmpstr (path, ==, "/managed");
1904 g_assert_cmpstr (name, ==, g_dbus_connection_get_unique_name (c));
1905 g_assert_cmpstr (name_owner, ==, g_dbus_connection_get_unique_name (c));
1906 g_assert_cmpint (flags, ==, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE);
1909 g_clear_object (&c2);
1911 g_free (name_owner);
1913 /* ... check there are no object proxies yet */
1914 object_proxies = g_dbus_object_manager_get_objects (pm);
1915 g_assert (object_proxies == NULL);
1917 /* First, export an object with a single interface (also check that
1918 * g_dbus_interface_get_object() works and that the object isn't reffed)
1920 o = foo_igen_object_skeleton_new ("/managed/first");
1921 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
1922 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1923 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1924 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1925 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1926 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1927 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1928 foo_igen_object_skeleton_set_bar (o, NULL);
1929 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1930 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1931 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1932 g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1933 g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1935 o2 = FOO_IGEN_OBJECT_SKELETON (g_dbus_interface_dup_object (G_DBUS_INTERFACE (i)));
1936 g_assert (G_DBUS_OBJECT (o2) == G_DBUS_OBJECT (o));
1937 g_assert_cmpint (G_OBJECT (o2)->ref_count, ==, 2);
1938 g_clear_object (&o2);
1940 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o));
1942 /* ... check we get the InterfacesAdded signal */
1945 g_main_loop_run (om_data->loop);
1947 g_assert_cmpint (om_data->state, ==, 2);
1948 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 1);
1949 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 0);
1950 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1951 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1952 /* ... check there's one non-standard interfaces */
1953 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1954 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1955 g_assert (has_interface (info, "org.project.Bar"));
1956 g_dbus_node_info_unref (info);
1958 /* Also check g_dbus_object_manager_get_interface */
1959 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (manager), "/managed/first", "org.project.Bar");
1960 g_assert (iface != NULL);
1961 g_clear_object (&iface);
1962 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (manager), "/managed/first", "org.project.Bat");
1963 g_assert (iface == NULL);
1964 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (pm), "/managed/first", "org.project.Bar");
1965 g_assert (iface != NULL);
1966 g_clear_object (&iface);
1967 iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (pm), "/managed/first", "org.project.Bat");
1968 g_assert (iface == NULL);
1970 /* Now, check adding the same interface replaces the existing one */
1971 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1972 /* ... check we get the InterfacesRemoved */
1974 g_main_loop_run (om_data->loop);
1975 /* ... and then check we get the InterfacesAdded */
1976 g_assert_cmpint (om_data->state, ==, 6);
1977 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 2);
1978 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 1);
1979 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1980 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1981 /* ... check introspection data */
1982 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1983 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1984 g_assert (has_interface (info, "org.project.Bar"));
1985 g_dbus_node_info_unref (info);
1986 g_clear_object (&i);
1988 /* check adding an interface of same type (but not same object) replaces the existing one */
1989 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
1990 foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1991 /* ... check we get the InterfacesRemoved and then InterfacesAdded */
1993 g_main_loop_run (om_data->loop);
1994 g_assert_cmpint (om_data->state, ==, 10);
1995 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1996 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1997 g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1998 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1999 /* ... check introspection data */
2000 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2001 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
2002 g_assert (has_interface (info, "org.project.Bar"));
2003 g_dbus_node_info_unref (info);
2004 g_clear_object (&i);
2006 /* check adding an interface of another type doesn't replace the existing one */
2007 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bat_skeleton_new ());
2008 foo_igen_object_skeleton_set_bat (o, FOO_IGEN_BAT (i));
2009 g_clear_object (&i);
2010 /* ... check we get the InterfacesAdded */
2011 om_data->state = 11;
2012 g_main_loop_run (om_data->loop);
2013 g_assert_cmpint (om_data->state, ==, 12);
2014 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2015 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
2016 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2017 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
2018 /* ... check introspection data */
2019 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2020 g_assert_cmpint (count_interfaces (info), ==, 5); /* Bar,Bat + Properties,Introspectable,Peer */
2021 g_assert (has_interface (info, "org.project.Bar"));
2022 g_assert (has_interface (info, "org.project.Bat"));
2023 g_dbus_node_info_unref (info);
2025 /* check we can remove an interface */
2026 foo_igen_object_skeleton_set_bar (o, NULL);
2027 /* ... check we get the InterfacesRemoved */
2028 om_data->state = 13;
2029 g_main_loop_run (om_data->loop);
2030 g_assert_cmpint (om_data->state, ==, 14);
2031 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2032 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
2033 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2034 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2035 /* ... check introspection data */
2036 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2037 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
2038 g_assert (has_interface (info, "org.project.Bat"));
2039 g_dbus_node_info_unref (info);
2040 /* also and that the call only has effect if the interface actually exists
2042 * (Note: if a signal was emitted we'd assert in the signal handler
2043 * because we're in state 14)
2045 foo_igen_object_skeleton_set_bar (o, NULL);
2046 /* ... check introspection data */
2047 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2048 g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
2049 g_assert (has_interface (info, "org.project.Bat"));
2050 g_dbus_node_info_unref (info);
2052 /* remove the last interface */
2053 foo_igen_object_skeleton_set_bat (o, NULL);
2054 /* ... check we get the InterfacesRemoved */
2055 om_data->state = 15;
2056 g_main_loop_run (om_data->loop);
2057 g_assert_cmpint (om_data->state, ==, 16);
2058 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2059 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2060 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2061 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2062 /* ... check introspection data */
2063 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2064 g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
2065 g_dbus_node_info_unref (info);
2067 /* and add an interface again */
2068 i = G_DBUS_INTERFACE_SKELETON (foo_igen_com_acme_coyote_skeleton_new ());
2069 foo_igen_object_skeleton_set_com_acme_coyote (o, FOO_IGEN_COM_ACME_COYOTE (i));
2070 g_clear_object (&i);
2071 /* ... check we get the InterfacesAdded */
2072 om_data->state = 17;
2073 g_main_loop_run (om_data->loop);
2074 g_assert_cmpint (om_data->state, ==, 18);
2075 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 4);
2076 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2077 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2078 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2079 /* ... check introspection data */
2080 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2081 g_assert_cmpint (count_interfaces (info), ==, 4); /* com.acme.Coyote + Properties,Introspectable,Peer */
2082 g_assert (has_interface (info, "com.acme.Coyote"));
2083 g_dbus_node_info_unref (info);
2085 /* Check GetManagedObjects() - should be just the Coyote */
2086 om_check_get_all (c, loop,
2087 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2089 /* -------------------------------------------------- */
2091 /* create a new object with two interfaces */
2092 o2 = foo_igen_object_skeleton_new ("/managed/second");
2093 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
2094 bar_skeleton = FOO_IGEN_BAR (i); /* save for later test */
2095 foo_igen_object_skeleton_set_bar (o2, FOO_IGEN_BAR (i));
2096 g_clear_object (&i);
2097 i = G_DBUS_INTERFACE_SKELETON (foo_igen_bat_skeleton_new ());
2098 foo_igen_object_skeleton_set_bat (o2, FOO_IGEN_BAT (i));
2099 g_clear_object (&i);
2101 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o2));
2102 /* ... check we get the InterfacesAdded with _two_ interfaces */
2103 om_data->state = 101;
2104 g_main_loop_run (om_data->loop);
2105 g_assert_cmpint (om_data->state, ==, 102);
2106 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2107 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2108 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2109 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2111 /* -------------------------------------------------- */
2113 /* Now that we have a couple of objects with interfaces, check
2114 * that ObjectManager.GetManagedObjects() works
2116 om_check_get_all (c, loop,
2117 "({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,)>}}},)");
2119 /* Set connection to NULL, causing everything to be unexported.. verify this.. and
2120 * then set the connection back.. and then check things still work
2122 g_dbus_object_manager_server_set_connection (manager, NULL);
2123 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
2124 g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
2125 g_dbus_node_info_unref (info);
2127 g_dbus_object_manager_server_set_connection (manager, c);
2128 om_check_get_all (c, loop,
2129 "({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,)>}}},)");
2131 /* Also check that the ObjectManagerClient returns these objects - and
2132 * that they are of the right GType cf. what was requested via
2133 * the generated ::get-proxy-type signal handler
2135 object_proxies = g_dbus_object_manager_get_objects (pm);
2136 g_assert (g_list_length (object_proxies) == 2);
2137 g_list_free_full (object_proxies, g_object_unref);
2138 op = g_dbus_object_manager_get_object (pm, "/managed/first");
2139 g_assert (op != NULL);
2140 g_assert (FOO_IGEN_IS_OBJECT_PROXY (op));
2141 g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/first");
2142 proxies = g_dbus_object_get_interfaces (op);
2143 g_assert (g_list_length (proxies) == 1);
2144 g_list_free_full (proxies, g_object_unref);
2145 p = G_DBUS_PROXY (foo_igen_object_get_com_acme_coyote (FOO_IGEN_OBJECT (op)));
2146 g_assert (p != NULL);
2147 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_COM_ACME_COYOTE_PROXY);
2148 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_COM_ACME_COYOTE));
2149 g_clear_object (&p);
2150 p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2151 g_assert (p == NULL);
2152 g_clear_object (&op);
2155 op = g_dbus_object_manager_get_object (pm, "/managed/second");
2156 g_assert (op != NULL);
2157 g_assert (FOO_IGEN_IS_OBJECT_PROXY (op));
2158 g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/second");
2159 proxies = g_dbus_object_get_interfaces (op);
2160 g_assert (g_list_length (proxies) == 2);
2161 g_list_free_full (proxies, g_object_unref);
2162 p = G_DBUS_PROXY (foo_igen_object_get_bat (FOO_IGEN_OBJECT (op)));
2163 g_assert (p != NULL);
2164 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_BAT_PROXY);
2165 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_BAT));
2166 g_clear_object (&p);
2167 p = G_DBUS_PROXY (foo_igen_object_get_bar (FOO_IGEN_OBJECT (op)));
2168 g_assert (p != NULL);
2169 g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_BAR_PROXY);
2170 g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_BAR));
2171 /* ... now that we have a Bar instance around, also check that we get signals
2172 * and property changes...
2174 om_check_property_and_signal_emission (loop, bar_skeleton, FOO_IGEN_BAR (p));
2175 g_clear_object (&p);
2176 p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2177 g_assert (p == NULL);
2178 g_clear_object (&op);
2180 /* -------------------------------------------------- */
2182 /* Now remove the second object added above */
2183 g_dbus_object_manager_server_unexport (manager, "/managed/second");
2184 /* ... check we get InterfacesRemoved with both interfaces */
2185 om_data->state = 103;
2186 g_main_loop_run (om_data->loop);
2187 g_assert_cmpint (om_data->state, ==, 104);
2188 g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2189 g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 4);
2190 g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2191 g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2192 /* ... check introspection data (there should be nothing) */
2193 info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/second", loop);
2194 g_assert_cmpint (count_nodes (info), ==, 0);
2195 g_assert_cmpint (count_interfaces (info), ==, 0);
2196 g_dbus_node_info_unref (info);
2198 /* Check GetManagedObjects() again */
2199 om_check_get_all (c, loop,
2200 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2201 /* -------------------------------------------------- */
2203 /* Check that export_uniquely() works */
2205 o3 = foo_igen_object_skeleton_new ("/managed/first");
2206 i = G_DBUS_INTERFACE_SKELETON (foo_igen_com_acme_coyote_skeleton_new ());
2207 foo_igen_com_acme_coyote_set_mood (FOO_IGEN_COM_ACME_COYOTE (i), "indifferent");
2208 foo_igen_object_skeleton_set_com_acme_coyote (o3, FOO_IGEN_COM_ACME_COYOTE (i));
2209 g_clear_object (&i);
2210 g_dbus_object_manager_server_export_uniquely (manager, G_DBUS_OBJECT_SKELETON (o3));
2211 /* ... check we get the InterfacesAdded signal */
2212 om_data->state = 200;
2213 g_main_loop_run (om_data->loop);
2214 g_assert_cmpint (om_data->state, ==, 201);
2216 om_check_get_all (c, loop,
2217 "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/first_1': {'com.acme.Coyote': {'Mood': <'indifferent'>}}},)");
2219 //g_main_loop_run (loop); /* TODO: tmp */
2221 /* Clean up objects */
2222 g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/first_1"));
2223 //g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/second"));
2224 g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/first"));
2225 g_assert_cmpint (g_list_length (g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager))), ==, 0);
2228 g_main_loop_unref (loop);
2230 if (om_signal_id != -1)
2231 g_dbus_connection_signal_unsubscribe (c, om_signal_id);
2232 g_clear_object (&o3);
2233 g_clear_object (&o2);
2234 g_clear_object (&o);
2235 g_clear_object (&manager);
2238 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2239 G_CALLBACK (on_object_proxy_added),
2241 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2242 G_CALLBACK (on_object_proxy_removed),
2244 g_clear_object (&pm);
2246 g_clear_object (&c);
2251 /* ---------------------------------------------------------------------------------------------------- */
2254 test_object_manager (void)
2259 loop = g_main_loop_new (NULL, FALSE);
2261 id = g_bus_own_name (G_BUS_TYPE_SESSION,
2262 "org.gtk.GDBus.BindingsTool.Test",
2263 G_BUS_NAME_OWNER_FLAGS_NONE,
2270 g_main_loop_run (loop);
2272 check_object_manager ();
2274 /* uncomment to keep the service around (to e.g. introspect it) */
2275 /* g_main_loop_run (loop); */
2277 unexport_objects ();
2279 g_bus_unown_name (id);
2280 g_main_loop_unref (loop);
2283 /* ---------------------------------------------------------------------------------------------------- */
2284 /* This checks that forcing names via org.gtk.GDBus.Name works (see test-codegen.xml) */
2286 extern gpointer name_forcing_1;
2287 extern gpointer name_forcing_2;
2288 extern gpointer name_forcing_3;
2289 extern gpointer name_forcing_4;
2290 extern gpointer name_forcing_5;
2291 extern gpointer name_forcing_6;
2292 extern gpointer name_forcing_7;
2293 gpointer name_forcing_1 = foo_igen_rocket123_get_type;
2294 gpointer name_forcing_2 = foo_igen_rocket123_call_ignite_xyz;
2295 gpointer name_forcing_3 = foo_igen_rocket123_emit_exploded_xyz;
2296 gpointer name_forcing_4 = foo_igen_rocket123_get_speed_xyz;
2297 gpointer name_forcing_5 = foo_igen_test_ugly_case_interface_call_get_iscsi_servers;
2298 gpointer name_forcing_6 = foo_igen_test_ugly_case_interface_emit_servers_updated_now;
2299 gpointer name_forcing_7 = foo_igen_test_ugly_case_interface_get_ugly_name;
2301 /* ---------------------------------------------------------------------------------------------------- */
2303 /* See https://bugzilla.gnome.org/show_bug.cgi?id=647577#c5 for details */
2305 #define CHECK_FIELD(name, v1, v2) g_assert_cmpint (G_STRUCT_OFFSET (FooiGenChangingInterface##v1##Iface, name), ==, G_STRUCT_OFFSET (FooiGenChangingInterface##v2##Iface, name));
2308 test_interface_stability (void)
2310 CHECK_FIELD(handle_foo_method, V1, V2);
2311 CHECK_FIELD(handle_bar_method, V1, V2);
2312 CHECK_FIELD(handle_baz_method, V1, V2);
2313 CHECK_FIELD(foo_signal, V1, V2);
2314 CHECK_FIELD(bar_signal, V1, V2);
2315 CHECK_FIELD(baz_signal, V1, V2);
2316 CHECK_FIELD(handle_new_method_in2, V2, V10);
2317 CHECK_FIELD(new_signal_in2, V2, V10);
2322 /* ---------------------------------------------------------------------------------------------------- */
2326 * - check that a property with name "Type" is mapped into g-name "type"
2327 * with C accessors get_type_ (to avoid clashing with the GType accessor)
2328 * and set_type_ (for symmetri)
2329 * (see https://bugzilla.gnome.org/show_bug.cgi?id=679473 for details)
2331 * - (could add more tests here)
2335 test_property_naming (void)
2337 gpointer c_getter_name = foo_igen_naming_get_type_;
2338 gpointer c_setter_name = foo_igen_naming_set_type_;
2339 FooiGenNaming *skel;
2341 (void) c_getter_name;
2342 (void) c_setter_name;
2344 skel = foo_igen_naming_skeleton_new ();
2345 g_assert (g_object_class_find_property (G_OBJECT_GET_CLASS (skel), "type") != NULL);
2346 g_object_unref (skel);
2349 /* ---------------------------------------------------------------------------------------------------- */
2355 g_test_init (&argc, &argv, NULL);
2357 g_test_add_func ("/gdbus/codegen/annotations", test_annotations);
2358 g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability);
2359 g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager);
2360 g_test_add_func ("/gdbus/codegen/property-naming", test_property_naming);
2362 return session_bus_run ();