Update German translation
[glib.git] / gio / tests / gdbus-test-codegen.c
blob4abe5a1eb942b53805dd965cbce854fb749a53fb
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>
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdio.h>
26 #include "gdbus-tests.h"
28 #include "gdbus-test-codegen-generated.h"
30 /* ---------------------------------------------------------------------------------------------------- */
32 static guint
33 count_annotations (GDBusAnnotationInfo **annotations)
35 guint ret;
36 ret = 0;
37 while (annotations != NULL && annotations[ret] != NULL)
38 ret++;
39 return ret;
42 /* checks that
44 * - non-internal annotations are written out correctly; and
45 * - injection via --annotation --key --value works
47 static void
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 /* ---------------------------------------------------------------------------------------------------- */
87 static gboolean
88 on_handle_hello_world (FooiGenBar *object,
89 GDBusMethodInvocation *invocation,
90 const gchar *greeting,
91 gpointer user_data)
93 gchar *response;
94 response = g_strdup_printf ("Word! You said '%s'. I'm Skeleton, btw!", greeting);
95 foo_igen_bar_complete_hello_world (object, invocation, response);
96 g_free (response);
97 return TRUE;
100 static gboolean
101 on_handle_test_primitive_types (FooiGenBar *object,
102 GDBusMethodInvocation *invocation,
103 guchar val_byte,
104 gboolean val_boolean,
105 gint16 val_int16,
106 guint16 val_uint16,
107 gint val_int32,
108 guint val_uint32,
109 gint64 val_int64,
110 guint64 val_uint64,
111 gdouble val_double,
112 const gchar *val_string,
113 const gchar *val_objpath,
114 const gchar *val_signature,
115 const gchar *val_bytestring,
116 gpointer user_data)
118 gchar *s1;
119 gchar *s2;
120 gchar *s3;
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,
125 invocation,
126 10 + val_byte,
127 !val_boolean,
128 100 + val_int16,
129 1000 + val_uint16,
130 10000 + val_int32,
131 100000 + val_uint32,
132 1000000 + val_int64,
133 10000000 + val_uint64,
134 val_double / G_PI,
138 "bytestring!\xff");
139 g_free (s1);
140 g_free (s2);
141 g_free (s3);
142 return TRUE;
145 static gboolean
146 on_handle_test_non_primitive_types (FooiGenBar *object,
147 GDBusMethodInvocation *invocation,
148 GVariant *dict_s_to_s,
149 GVariant *dict_s_to_pairs,
150 GVariant *a_struct,
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,
155 gpointer user_data)
157 gchar *s;
158 GString *str;
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);
165 g_free (s);
166 s = g_strjoinv (", ", (gchar **) array_of_objpaths);
167 g_string_append_printf (str, "array_of_objpaths: [%s] ", s);
168 g_free (s);
169 s = g_variant_print (array_of_signatures, TRUE);
170 g_string_append_printf (str, "array_of_signatures: %s ", s);
171 g_free (s);
172 s = g_strjoinv (", ", (gchar **) array_of_bytestrings);
173 g_string_append_printf (str, "array_of_bytestrings: [%s] ", s);
174 g_free (s);
175 foo_igen_bar_complete_test_non_primitive_types (object, invocation, str->str);
176 g_string_free (str, TRUE);
177 return TRUE;
180 static gboolean
181 on_handle_request_signal_emission (FooiGenBar *object,
182 GDBusMethodInvocation *invocation,
183 gint which_one,
184 gpointer user_data)
186 if (which_one == 0)
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);
194 return TRUE;
197 static gboolean
198 on_handle_request_multi_property_mods (FooiGenBar *object,
199 GDBusMethodInvocation *invocation,
200 gpointer user_data)
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);
210 return TRUE;
213 static gboolean
214 on_handle_property_cancellation (FooiGenBar *object,
215 GDBusMethodInvocation *invocation,
216 gpointer user_data)
218 guint n;
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);
230 return TRUE;
233 /* ---------------------------------------------------------------------------------------------------- */
235 static gboolean
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,
242 gpointer user_data)
244 GVariant *ret_i;
245 GVariant *ret_s;
246 GVariant *ret_ay;
247 GVariant *ret_struct;
248 gint32 val;
249 gchar *s;
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);
254 g_free (s);
255 s = g_strdup_printf ("%s_foo\xff", g_variant_get_bytestring (force_in_ay));
256 ret_ay = g_variant_new_bytestring (s);
257 g_free (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,
268 ret_i,
269 ret_s,
270 ret_ay,
271 ret_struct);
273 foo_igen_bat_complete_force_method (object,
274 invocation,
275 ret_i,
276 ret_s,
277 ret_ay,
278 ret_struct);
280 g_variant_unref (ret_i);
281 g_variant_unref (ret_s);
282 g_variant_unref (ret_ay);
283 g_variant_unref (ret_struct);
285 return TRUE;
289 /* ---------------------------------------------------------------------------------------------------- */
291 static gboolean
292 my_g_authorize_method_handler (GDBusInterfaceSkeleton *interface,
293 GDBusMethodInvocation *invocation,
294 gpointer user_data)
296 const gchar *method_name;
297 gboolean authorized;
299 authorized = FALSE;
301 method_name = g_dbus_method_invocation_get_method_name (invocation);
302 if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
304 authorized = FALSE;
306 else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
308 authorized = TRUE;
310 else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
312 authorized = TRUE;
314 else
316 g_assert_not_reached ();
319 if (!authorized)
321 g_dbus_method_invocation_return_error (invocation,
322 G_IO_ERROR,
323 G_IO_ERROR_PERMISSION_DENIED,
324 "not authorized...");
326 return authorized;
329 static gboolean
330 my_object_authorize_method_handler (GDBusObjectSkeleton *object,
331 GDBusInterfaceSkeleton *interface,
332 GDBusMethodInvocation *invocation,
333 gpointer user_data)
335 const gchar *method_name;
336 gboolean authorized;
338 authorized = FALSE;
340 method_name = g_dbus_method_invocation_get_method_name (invocation);
341 if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
343 authorized = TRUE;
345 else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
347 authorized = TRUE;
349 else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
351 authorized = FALSE;
353 else
355 g_assert_not_reached ();
358 if (!authorized)
360 g_dbus_method_invocation_return_error (invocation,
361 G_IO_ERROR,
362 G_IO_ERROR_PENDING,
363 "not authorized (from object)...");
365 return authorized;
368 static gboolean
369 on_handle_check_not_authorized (FooiGenAuthorize *object,
370 GDBusMethodInvocation *invocation,
371 gpointer user_data)
373 foo_igen_authorize_complete_check_not_authorized (object, invocation);
374 return TRUE;
377 static gboolean
378 on_handle_check_authorized (FooiGenAuthorize *object,
379 GDBusMethodInvocation *invocation,
380 gpointer user_data)
382 foo_igen_authorize_complete_check_authorized (object, invocation);
383 return TRUE;
386 static gboolean
387 on_handle_check_not_authorized_from_object (FooiGenAuthorize *object,
388 GDBusMethodInvocation *invocation,
389 gpointer user_data)
391 foo_igen_authorize_complete_check_not_authorized_from_object (object, invocation);
392 return TRUE;
395 /* ---------------------------------------------------------------------------------------------------- */
397 static gboolean
398 on_handle_get_self (FooiGenMethodThreads *object,
399 GDBusMethodInvocation *invocation,
400 gpointer user_data)
402 gchar *s;
403 s = g_strdup_printf ("%p", g_thread_self ());
404 foo_igen_method_threads_complete_get_self (object, invocation, s);
405 g_free (s);
406 return TRUE;
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;
420 static void
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));
430 static void
431 on_bus_acquired (GDBusConnection *connection,
432 const gchar *name,
433 gpointer user_data)
435 GError *error;
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
441 * GObject signal.
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)
447 error = NULL;
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),
462 connection,
463 "/bar",
464 &error);
465 g_assert_no_error (error);
466 g_signal_connect (exported_bar_object,
467 "handle-hello-world",
468 G_CALLBACK (on_handle_hello_world),
469 NULL);
470 g_signal_connect (exported_bar_object,
471 "handle-test-primitive-types",
472 G_CALLBACK (on_handle_test_primitive_types),
473 NULL);
474 g_signal_connect (exported_bar_object,
475 "handle-test-non-primitive-types",
476 G_CALLBACK (on_handle_test_non_primitive_types),
477 NULL);
478 g_signal_connect (exported_bar_object,
479 "handle-request-signal-emission",
480 G_CALLBACK (on_handle_request_signal_emission),
481 NULL);
482 g_signal_connect (exported_bar_object,
483 "handle-request-multi-property-mods",
484 G_CALLBACK (on_handle_request_multi_property_mods),
485 NULL);
486 g_signal_connect (exported_bar_object,
487 "handle-property-cancellation",
488 G_CALLBACK (on_handle_property_cancellation),
489 NULL);
491 exported_bat_object = foo_igen_bat_skeleton_new ();
492 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bat_object),
493 connection,
494 "/bat",
495 &error);
496 g_assert_no_error (error);
497 g_signal_connect (exported_bat_object,
498 "handle-force-method",
499 G_CALLBACK (on_handle_force_method),
500 NULL);
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),
506 NULL);
508 authorize_enclosing_object = g_dbus_object_skeleton_new ("/authorize");
509 g_signal_connect (authorize_enclosing_object,
510 "authorize-method",
511 G_CALLBACK (my_object_authorize_method_handler),
512 NULL);
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),
517 connection,
518 "/authorize",
519 &error);
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),
524 NULL);
525 g_signal_connect (exported_authorize_object,
526 "handle-check-not-authorized",
527 G_CALLBACK (on_handle_check_not_authorized),
528 NULL);
529 g_signal_connect (exported_authorize_object,
530 "handle-check-authorized",
531 G_CALLBACK (on_handle_check_authorized),
532 NULL);
533 g_signal_connect (exported_authorize_object,
534 "handle-check-not-authorized-from-object",
535 G_CALLBACK (on_handle_check_not_authorized_from_object),
536 NULL);
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),
546 connection,
547 "/method_threads_1",
548 &error);
549 g_assert_no_error (error);
550 g_signal_connect (exported_thread_object_1,
551 "handle-get-self",
552 G_CALLBACK (on_handle_get_self),
553 NULL);
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),
558 connection,
559 "/method_threads_2",
560 &error);
561 g_assert_no_error (error);
562 g_signal_connect (exported_thread_object_2,
563 "handle-get-self",
564 G_CALLBACK (on_handle_get_self),
565 NULL);
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);
574 static void
575 on_name_acquired (GDBusConnection *connection,
576 const gchar *name,
577 gpointer user_data)
579 GMainLoop *loop = user_data;
581 g_thread_new ("check-proxies",
582 check_proxies_in_thread,
583 loop);
586 static void
587 on_name_lost (GDBusConnection *connection,
588 const gchar *name,
589 gpointer user_data)
591 g_assert_not_reached ();
594 /* ---------------------------------------------------------------------------------------------------- */
596 typedef struct
598 GMainLoop *thread_loop;
599 gint initial_y;
600 gint initial_i;
601 guint num_g_properties_changed;
602 gboolean received_test_signal;
603 guint num_notify_u;
604 guint num_notify_n;
605 } ClientData;
607 static void
608 on_notify_u (GObject *object,
609 GParamSpec *pspec,
610 gpointer user_data)
612 ClientData *data = user_data;
613 g_assert_cmpstr (pspec->name, ==, "u");
614 data->num_notify_u += 1;
617 static void
618 on_notify_n (GObject *object,
619 GParamSpec *pspec,
620 gpointer user_data)
622 ClientData *data = user_data;
623 g_assert_cmpstr (pspec->name, ==, "n");
624 data->num_notify_n += 1;
627 static void
628 on_g_properties_changed (GDBusProxy *_proxy,
629 GVariant *changed_properties,
630 const gchar* const *invalidated_properties,
631 gpointer user_data)
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);
648 else
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);
657 static void
658 on_test_signal (FooiGenBar *proxy,
659 gint val_int32,
660 const gchar* const *array_of_strings,
661 const gchar* const *array_of_bytestrings,
662 GVariant *dict_s_to_pairs,
663 gpointer user_data)
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);
679 static void
680 on_property_cancellation_cb (FooiGenBar *proxy,
681 GAsyncResult *res,
682 gpointer user_data)
684 ClientData *data = user_data;
685 gboolean ret;
686 GError *error = NULL;
688 error = NULL;
689 ret = foo_igen_bar_call_property_cancellation_finish (proxy, res, &error);
690 g_assert_no_error (error);
691 g_assert (ret);
693 g_main_loop_quit (data->thread_loop);
696 static void
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};
704 guchar ret_val_byte;
705 gboolean ret_val_boolean;
706 gint16 ret_val_int16;
707 guint16 ret_val_uint16;
708 gint ret_val_int32;
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;
717 gboolean ret;
718 GError *error;
719 ClientData *data;
720 guchar val_y;
721 gboolean val_b;
722 gint val_n;
723 guint val_q;
724 gint val_i;
725 guint val_u;
726 gint64 val_x;
727 guint64 val_t;
728 gdouble val_d;
729 gchar *val_s;
730 gchar *val_o;
731 gchar *val_g;
732 gchar *val_ay;
733 gchar **val_as;
734 gchar **val_ao;
735 GVariant *val_ag;
736 gint32 val_unset_i;
737 gdouble val_unset_d;
738 gchar *val_unset_s;
739 gchar *val_unset_o;
740 gchar *val_unset_g;
741 gchar *val_unset_ay;
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;
747 GVariant *v;
748 gchar *s;
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);
758 g_variant_unref (v);
760 /* set empty values to non-empty */
761 val_unset_i = 42;
762 val_unset_d = 42.0;
763 val_unset_s = "42";
764 val_unset_o = "42";
765 val_unset_g = "42";
766 val_unset_ay = NULL;
767 val_unset_as = NULL;
768 val_unset_ao = NULL;
769 val_unset_ag = NULL;
770 val_unset_struct = NULL;
771 /* check properties */
772 g_object_get (proxy,
773 "y", &val_y,
774 "b", &val_b,
775 "n", &val_n,
776 "q", &val_q,
777 "i", &val_i,
778 "u", &val_u,
779 "x", &val_x,
780 "t", &val_t,
781 "d", &val_d,
782 "s", &val_s,
783 "o", &val_o,
784 "g", &val_g,
785 "ay", &val_ay,
786 "as", &val_as,
787 "ao", &val_ao,
788 "ag", &val_ag,
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,
800 NULL);
801 g_assert_cmpint (val_y, ==, 42);
802 g_assert_cmpstr (val_finally_normal_name, ==, "There aint no place like home");
803 g_free (val_s);
804 g_free (val_o);
805 g_free (val_g);
806 g_assert_cmpstr (val_ay, ==, "ABCabc");
807 g_free (val_ay);
808 g_strfreev (val_as);
809 g_strfreev (val_ao);
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 [])");
828 g_free (s);
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.
847 g_object_set (proxy,
848 "s", "a string",
849 "o", "/a/path",
850 "g", "asig",
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']"),
855 NULL);
857 error = NULL;
858 ret = foo_igen_bar_call_test_primitive_types_sync (proxy,
860 TRUE,
868 "a string",
869 "/a/path",
870 "asig",
871 "bytestring\xff",
872 &ret_val_byte,
873 &ret_val_boolean,
874 &ret_val_int16,
875 &ret_val_uint16,
876 &ret_val_int32,
877 &ret_val_uint32,
878 &ret_val_int64,
879 &ret_val_uint64,
880 &ret_val_double,
881 &ret_val_string,
882 &ret_val_objpath,
883 &ret_val_signature,
884 &ret_val_bytestring,
885 NULL, /* GCancellable */
886 &error);
887 g_assert_no_error (error);
888 g_assert (ret);
890 error = NULL;
891 ret = foo_igen_bar_call_test_non_primitive_types_sync (proxy,
892 g_variant_new_parsed ("{'one': 'red',"
893 " 'two': 'blue'}"),
894 g_variant_new_parsed ("{'first': (42, 42), "
895 "'second': (43, 43)}"),
896 g_variant_new_parsed ("(42, 'foo', 'bar')"),
897 array_of_strings,
898 array_of_objpaths,
899 g_variant_new_parsed ("[@g 'ass', 'git']"),
900 array_of_bytestrings,
902 NULL, /* GCancellable */
903 &error);
905 g_assert_no_error (error);
906 g_assert (ret);
908 /* Check that org.freedesktop.DBus.Error.UnknownMethod is returned on
909 * unimplemented methods.
911 error = NULL;
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);
915 error = NULL;
916 g_assert (!ret);
918 g_signal_connect (proxy,
919 "test-signal",
920 G_CALLBACK (on_test_signal),
921 data);
922 error = NULL;
923 ret = foo_igen_bar_call_request_signal_emission_sync (proxy, 0, NULL, &error);
924 g_assert_no_error (error);
925 g_assert (ret);
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 */
949 g_object_set (proxy,
950 "as", array_of_strings_2,
951 NULL);
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
962 * properties.
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
966 * checks this.
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),
975 data);
976 error = NULL;
977 ret = foo_igen_bar_call_request_multi_property_mods_sync (proxy, NULL, &error);
978 g_assert_no_error (error);
979 g_assert (ret);
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),
984 data);
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,
993 "notify::u",
994 G_CALLBACK (on_notify_u),
995 data);
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
1014 * to arrive.
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,
1021 "notify::n",
1022 G_CALLBACK (on_notify_n),
1023 data);
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!
1041 error = NULL;
1042 foo_igen_bar_call_property_cancellation (proxy,
1043 NULL, /* GCancellable */
1044 (GAsyncReadyCallback) on_property_cancellation_cb,
1045 data);
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);
1050 /* cleanup */
1051 g_free (data);
1054 /* ---------------------------------------------------------------------------------------------------- */
1056 static void
1057 on_force_signal (FooiGenBat *proxy,
1058 GVariant *force_i,
1059 GVariant *force_s,
1060 GVariant *force_ay,
1061 GVariant *force_struct,
1062 gpointer user_data)
1064 gboolean *signal_received = user_data;
1065 gint val;
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;
1078 static void
1079 check_bat_proxy (FooiGenBat *proxy,
1080 GMainLoop *thread_loop)
1082 GError *error;
1083 GVariant *ret_i;
1084 GVariant *ret_s;
1085 GVariant *ret_ay;
1086 GVariant *ret_struct;
1087 gint val;
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,
1096 "force-i", &ret_i,
1097 "force-s", &ret_s,
1098 "force-ay", &ret_ay,
1099 "force-struct", &ret_struct,
1100 NULL);
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,
1114 "force-signal",
1115 G_CALLBACK (on_force_signal),
1116 &force_signal_received);
1118 error = NULL;
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),
1124 &ret_i,
1125 &ret_s,
1126 &ret_ay,
1127 &ret_struct,
1128 NULL, /* GCancellable* */
1129 &error);
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 /* ---------------------------------------------------------------------------------------------------- */
1146 static void
1147 check_authorize_proxy (FooiGenAuthorize *proxy,
1148 GMainLoop *thread_loop)
1150 GError *error;
1151 gboolean ret;
1153 /* Check that g-authorize-method works as intended */
1155 error = NULL;
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);
1159 g_assert (!ret);
1161 error = NULL;
1162 ret = foo_igen_authorize_call_check_authorized_sync (proxy, NULL, &error);
1163 g_assert_no_error (error);
1164 g_assert (ret);
1166 error = NULL;
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);
1170 g_assert (!ret);
1173 /* ---------------------------------------------------------------------------------------------------- */
1175 static GThread *
1176 get_self_via_proxy (FooiGenMethodThreads *proxy_1)
1178 GError *error;
1179 gchar *self_str;
1180 gboolean ret;
1181 gpointer self;
1183 error = NULL;
1184 ret = foo_igen_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
1185 g_assert_no_error (error);
1186 g_assert (ret);
1188 g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
1190 g_free (self_str);
1192 return self;
1195 static void
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 /* ---------------------------------------------------------------------------------------------------- */
1209 static gpointer
1210 check_proxies_in_thread (gpointer user_data)
1212 GMainLoop *loop = user_data;
1213 GMainContext *thread_context;
1214 GMainLoop *thread_loop;
1215 GError *error;
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 */
1227 error = NULL;
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",
1231 "/bar",
1232 NULL, /* GCancellable* */
1233 &error);
1234 check_bar_proxy (bar_proxy, thread_loop);
1235 g_assert_no_error (error);
1236 g_object_unref (bar_proxy);
1238 error = NULL;
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",
1242 "/bat",
1243 NULL, /* GCancellable* */
1244 &error);
1245 check_bat_proxy (bat_proxy, thread_loop);
1246 g_assert_no_error (error);
1247 g_object_unref (bat_proxy);
1249 error = NULL;
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",
1253 "/authorize",
1254 NULL, /* GCancellable* */
1255 &error);
1256 check_authorize_proxy (authorize_proxy, thread_loop);
1257 g_assert_no_error (error);
1258 g_object_unref (authorize_proxy);
1260 error = NULL;
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* */
1266 &error);
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* */
1273 &error);
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);
1284 return NULL;
1287 /* ---------------------------------------------------------------------------------------------------- */
1289 typedef struct
1291 gchar *xml;
1292 GMainLoop *loop;
1293 } IntrospectData;
1295 static void
1296 introspect_cb (GDBusConnection *connection,
1297 GAsyncResult *res,
1298 gpointer user_data)
1300 IntrospectData *data = user_data;
1301 GVariant *result;
1302 GError *error;
1304 error = NULL;
1305 result = g_dbus_connection_call_finish (connection,
1306 res,
1307 &error);
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,
1318 const gchar *name,
1319 const gchar *object_path,
1320 GMainLoop *loop)
1322 GError *error;
1323 GDBusNodeInfo *node_info;
1324 IntrospectData *data;
1326 data = g_new0 (IntrospectData, 1);
1327 data->xml = NULL;
1328 data->loop = loop;
1330 /* do this async to avoid deadlocks */
1331 g_dbus_connection_call (connection,
1332 name,
1333 object_path,
1334 "org.freedesktop.DBus.Introspectable",
1335 "Introspect",
1336 NULL, /* params */
1337 G_VARIANT_TYPE ("(s)"),
1338 G_DBUS_CALL_FLAGS_NONE,
1340 NULL,
1341 (GAsyncReadyCallback) introspect_cb,
1342 data);
1343 g_main_loop_run (loop);
1344 g_assert (data->xml != NULL);
1346 error = 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);
1350 g_free (data->xml);
1351 g_free (data);
1353 return node_info;
1356 static guint
1357 count_interfaces (GDBusNodeInfo *info)
1359 guint n;
1360 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1362 return n;
1365 static guint
1366 count_nodes (GDBusNodeInfo *info)
1368 guint n;
1369 for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
1371 return n;
1374 static guint
1375 has_interface (GDBusNodeInfo *info,
1376 const gchar *name)
1378 guint n;
1379 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1381 if (g_strcmp0 (info->interfaces[n]->name, name) == 0)
1382 return TRUE;
1384 return FALSE;
1387 /* ---------------------------------------------------------------------------------------------------- */
1389 typedef struct {
1390 GMainLoop *loop;
1391 GVariant *result;
1392 } OMGetManagedObjectsData;
1394 static void
1395 om_get_all_cb (GDBusConnection *connection,
1396 GAsyncResult *res,
1397 gpointer user_data)
1399 OMGetManagedObjectsData *data = user_data;
1400 GError *error;
1402 error = NULL;
1403 data->result = g_dbus_connection_call_finish (connection,
1404 res,
1405 &error);
1406 g_assert_no_error (error);
1407 g_assert (data->result != NULL);
1408 g_main_loop_quit (data->loop);
1411 static void
1412 om_check_get_all (GDBusConnection *c,
1413 GMainLoop *loop,
1414 const gchar *str)
1416 OMGetManagedObjectsData data;
1417 gchar *s;
1419 data.loop = loop;
1420 data.result = NULL;
1422 /* do this async to avoid deadlocks */
1423 g_dbus_connection_call (c,
1424 g_dbus_connection_get_unique_name (c),
1425 "/managed",
1426 "org.freedesktop.DBus.ObjectManager",
1427 "GetManagedObjects",
1428 NULL, /* params */
1429 G_VARIANT_TYPE ("(a{oa{sa{sv}}})"),
1430 G_DBUS_CALL_FLAGS_NONE,
1432 NULL,
1433 (GAsyncReadyCallback) om_get_all_cb,
1434 &data);
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);
1439 g_free (s);
1440 g_variant_unref (data.result);
1443 typedef struct
1445 GMainLoop *loop;
1446 guint state;
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;
1452 } OMData;
1454 static gint
1455 my_pstrcmp (const gchar **a, const gchar **b)
1457 return g_strcmp0 (*a, *b);
1460 static void
1461 om_check_interfaces_added (const gchar *signal_name,
1462 GVariant *parameters,
1463 const gchar *object_path,
1464 const gchar *first_interface_name,
1465 ...)
1467 const gchar *path;
1468 GVariant *array;
1469 guint n;
1470 GPtrArray *interfaces;
1471 GPtrArray *interfaces_in_message;
1472 va_list var_args;
1473 const gchar *str;
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 *);
1481 if (str == NULL)
1482 break;
1483 g_ptr_array_add (interfaces, (gpointer) str);
1485 while (TRUE);
1486 va_end (var_args);
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);
1509 static void
1510 om_check_interfaces_removed (const gchar *signal_name,
1511 GVariant *parameters,
1512 const gchar *object_path,
1513 const gchar *first_interface_name,
1514 ...)
1516 const gchar *path;
1517 GVariant *array;
1518 guint n;
1519 GPtrArray *interfaces;
1520 GPtrArray *interfaces_in_message;
1521 va_list var_args;
1522 const gchar *str;
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 *);
1530 if (str == NULL)
1531 break;
1532 g_ptr_array_add (interfaces, (gpointer) str);
1534 while (TRUE);
1535 va_end (var_args);
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);
1558 static void
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,
1565 gpointer user_data)
1567 OMData *om_data = user_data;
1569 //g_debug ("foo: %s", g_variant_print (parameters, TRUE));
1571 switch (om_data->state)
1573 default:
1574 case 0:
1575 g_printerr ("failing and om_data->state=%d on signal %s, params=%s\n",
1576 om_data->state,
1577 signal_name,
1578 g_variant_print (parameters, TRUE));
1579 g_assert_not_reached ();
1580 break;
1582 case 1:
1583 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1584 "org.project.Bar", NULL);
1585 om_data->state = 2;
1586 g_main_loop_quit (om_data->loop);
1587 break;
1589 case 3:
1590 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1591 "org.project.Bar", NULL);
1592 om_data->state = 5;
1593 /* keep running the loop */
1594 break;
1596 case 5:
1597 om_check_interfaces_added (signal_name, parameters, "/managed/first",
1598 "org.project.Bar", NULL);
1599 om_data->state = 6;
1600 g_main_loop_quit (om_data->loop);
1601 break;
1603 case 7:
1604 om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1605 "org.project.Bar", NULL);
1606 om_data->state = 9;
1607 /* keep running the loop */
1608 break;
1610 case 9:
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);
1615 break;
1617 case 11:
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);
1622 break;
1624 case 13:
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);
1629 break;
1631 case 15:
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);
1636 break;
1638 case 17:
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);
1643 break;
1645 case 101:
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);
1650 break;
1652 case 103:
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);
1657 break;
1659 case 200:
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);
1664 break;
1668 static GAsyncResult *om_res = NULL;
1670 static void
1671 om_pm_start_cb (FooiGenObjectManagerClient *manager,
1672 GAsyncResult *res,
1673 gpointer user_data)
1675 GMainLoop *loop = user_data;
1676 om_res = g_object_ref (res);
1677 g_main_loop_quit (loop);
1680 static void
1681 on_interface_added (GDBusObject *object,
1682 GDBusInterface *interface,
1683 gpointer user_data)
1685 OMData *om_data = user_data;
1686 om_data->num_interface_added_signals += 1;
1689 static void
1690 on_interface_removed (GDBusObject *object,
1691 GDBusInterface *interface,
1692 gpointer user_data)
1694 OMData *om_data = user_data;
1695 om_data->num_interface_removed_signals += 1;
1698 static void
1699 on_object_proxy_added (GDBusObjectManagerClient *manager,
1700 GDBusObjectProxy *object_proxy,
1701 gpointer user_data)
1703 OMData *om_data = user_data;
1704 om_data->num_object_proxy_added_signals += 1;
1705 g_signal_connect (object_proxy,
1706 "interface-added",
1707 G_CALLBACK (on_interface_added),
1708 om_data);
1709 g_signal_connect (object_proxy,
1710 "interface-removed",
1711 G_CALLBACK (on_interface_removed),
1712 om_data);
1715 static void
1716 on_object_proxy_removed (GDBusObjectManagerClient *manager,
1717 GDBusObjectProxy *object_proxy,
1718 gpointer user_data)
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),
1724 om_data), ==, 1);
1725 g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1726 G_CALLBACK (on_interface_removed),
1727 om_data), ==, 1);
1730 static void
1731 property_d_changed (GObject *object,
1732 GParamSpec *pspec,
1733 gpointer user_data)
1735 gboolean *changed = user_data;
1737 *changed = TRUE;
1740 static void
1741 om_check_property_and_signal_emission (GMainLoop *loop,
1742 FooiGenBar *skeleton,
1743 FooiGenBar *proxy)
1745 gboolean d_changed = FALSE;
1746 guint handler;
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");
1779 static void
1780 check_object_manager (void)
1782 FooiGenObjectSkeleton *o = NULL;
1783 FooiGenObjectSkeleton *o2 = NULL;
1784 FooiGenObjectSkeleton *o3 = NULL;
1785 GDBusInterfaceSkeleton *i;
1786 GDBusConnection *c;
1787 GDBusObjectManagerServer *manager = NULL;
1788 GDBusNodeInfo *info;
1789 GError *error;
1790 GMainLoop *loop;
1791 OMData *om_data = NULL;
1792 guint om_signal_id = -1;
1793 GDBusObjectManager *pm = NULL;
1794 GList *object_proxies;
1795 GList *proxies;
1796 GDBusObject *op;
1797 GDBusProxy *p;
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;
1808 om_data->state = 0;
1810 error = NULL;
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,
1816 NULL, /* sender */
1817 "org.freedesktop.DBus.ObjectManager",
1818 NULL, /* member */
1819 NULL, /* object_path */
1820 NULL, /* arg0 */
1821 G_DBUS_SIGNAL_FLAGS_NONE,
1822 om_on_signal,
1823 om_data,
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().
1834 error = NULL;
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),
1838 "/managed",
1839 NULL, /* GCancellable */
1840 &error);
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");
1854 g_assert (c2 == c);
1855 g_free (path);
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 */
1870 error = NULL;
1871 foo_igen_object_manager_client_new (c,
1872 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1873 g_dbus_connection_get_unique_name (c),
1874 "/managed",
1875 NULL, /* GCancellable */
1876 (GAsyncReadyCallback) om_pm_start_cb,
1877 loop);
1878 g_main_loop_run (loop);
1879 error = NULL;
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,
1885 "object-added",
1886 G_CALLBACK (on_object_proxy_added),
1887 om_data);
1888 g_signal_connect (pm,
1889 "object-removed",
1890 G_CALLBACK (on_object_proxy_removed),
1891 om_data);
1893 g_assert_cmpstr (g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (pm)), ==, "/managed");
1894 g_object_get (pm,
1895 "object-path", &path,
1896 "connection", &c2,
1897 "name", &name,
1898 "name-owner", &name_owner,
1899 "flags", &flags,
1900 NULL);
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);
1905 g_assert (c2 == c);
1906 g_free (path);
1907 g_clear_object (&c2);
1908 g_free (name);
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 */
1941 om_data->state = 1;
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 */
1971 om_data->state = 3;
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 */
1990 om_data->state = 7;
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);
2098 /* ... add it */
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);
2152 /* -- */
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);
2225 if (loop != NULL)
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);
2234 if (pm != NULL)
2236 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2237 G_CALLBACK (on_object_proxy_added),
2238 om_data), ==, 1);
2239 g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2240 G_CALLBACK (on_object_proxy_removed),
2241 om_data), ==, 1);
2242 g_clear_object (&pm);
2244 g_clear_object (&c);
2246 g_free (om_data);
2249 /* ---------------------------------------------------------------------------------------------------- */
2251 static void
2252 test_object_manager (void)
2254 GMainLoop *loop;
2255 guint id;
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,
2262 on_bus_acquired,
2263 on_name_acquired,
2264 on_name_lost,
2265 loop,
2266 NULL);
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));
2305 static void
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);
2318 #undef CHECK_FIELD
2320 /* ---------------------------------------------------------------------------------------------------- */
2322 /* property naming
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)
2332 static void
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 /* ---------------------------------------------------------------------------------------------------- */
2350 main (int argc,
2351 char *argv[])
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 ();