gdbus-codegen: Don't send out PropertiesChanged if value ends up not changing
[platform/upstream/glib.git] / gio / tests / gdbus-test-codegen.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2011 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #include "gdbus-tests.h"
29
30 #include "gdbus-test-codegen-generated.h"
31
32 /* ---------------------------------------------------------------------------------------------------- */
33
34 static guint
35 count_annotations (GDBusAnnotationInfo **annotations)
36 {
37   guint ret;
38   ret = 0;
39   while (annotations != NULL && annotations[ret] != NULL)
40     ret++;
41   return ret;
42 }
43
44 /* checks that
45  *
46  *  - non-internal annotations are written out correctly; and
47  *  - injection via --annotation --key --value works
48  */
49 static void
50 test_annotations (void)
51 {
52   GDBusInterfaceInfo *iface;
53   GDBusMethodInfo *method;
54   GDBusSignalInfo *signal;
55   GDBusPropertyInfo *property;
56
57   iface = foo_bar_interface_info ();
58   g_assert (iface != NULL);
59
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");
63
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");
69
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");
75
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");
80
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");
85 }
86
87 /* ---------------------------------------------------------------------------------------------------- */
88
89 static gboolean
90 on_handle_hello_world (FooBar                 *object,
91                        GDBusMethodInvocation  *invocation,
92                        const gchar            *greeting,
93                        gpointer                user_data)
94 {
95   gchar *response;
96   response = g_strdup_printf ("Word! You said `%s'. I'm Skeleton, btw!", greeting);
97   foo_bar_complete_hello_world (object, invocation, response);
98   g_free (response);
99   return TRUE;
100 }
101
102 static gboolean
103 on_handle_test_primitive_types (FooBar                *object,
104                                 GDBusMethodInvocation *invocation,
105                                 guchar                 val_byte,
106                                 gboolean               val_boolean,
107                                 gint16                 val_int16,
108                                 guint16                val_uint16,
109                                 gint                   val_int32,
110                                 guint                  val_uint32,
111                                 gint64                 val_int64,
112                                 guint64                val_uint64,
113                                 gdouble                val_double,
114                                 const gchar           *val_string,
115                                 const gchar           *val_objpath,
116                                 const gchar           *val_signature,
117                                 const gchar           *val_bytestring,
118                                 gpointer               user_data)
119 {
120   gchar *s1;
121   gchar *s2;
122   gchar *s3;
123   s1 = g_strdup_printf ("Word! You said `%s'. Rock'n'roll!", val_string);
124   s2 = g_strdup_printf ("/modified%s", val_objpath);
125   s3 = g_strdup_printf ("assgit%s", val_signature);
126   foo_bar_complete_test_primitive_types (object,
127                                          invocation,
128                                          10 + val_byte,
129                                          !val_boolean,
130                                          100 + val_int16,
131                                          1000 + val_uint16,
132                                          10000 + val_int32,
133                                          100000 + val_uint32,
134                                          1000000 + val_int64,
135                                          10000000 + val_uint64,
136                                          val_double / G_PI,
137                                          s1,
138                                          s2,
139                                          s3,
140                                          "bytestring!\xff");
141   g_free (s1);
142   g_free (s2);
143   g_free (s3);
144   return TRUE;
145 }
146
147 static gboolean
148 on_handle_test_non_primitive_types (FooBar                *object,
149                                     GDBusMethodInvocation *invocation,
150                                     GVariant              *dict_s_to_s,
151                                     GVariant              *dict_s_to_pairs,
152                                     GVariant              *a_struct,
153                                     const gchar* const    *array_of_strings,
154                                     GVariant              *array_of_objpaths,
155                                     GVariant              *array_of_signatures,
156                                     const gchar* const    *array_of_bytestrings,
157                                     gpointer               user_data)
158 {
159   gchar *s;
160   GString *str;
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);
167   g_free (s);
168   s = g_variant_print (array_of_objpaths, TRUE);
169   g_string_append_printf (str, "array_of_objpaths: %s ", s);
170   g_free (s);
171   s = g_variant_print (array_of_objpaths, TRUE);
172   g_string_append_printf (str, "array_of_signatures: %s ", s);
173   g_free (s);
174   s = g_strjoinv (", ", (gchar **) array_of_bytestrings);
175   g_string_append_printf (str, "array_of_bytestrings: [%s] ", s);
176   g_free (s);
177   foo_bar_complete_test_non_primitive_types (object, invocation, str->str);
178   g_string_free (str, TRUE);
179   return TRUE;
180 }
181
182 static gboolean
183 on_handle_request_signal_emission (FooBar                 *object,
184                                    GDBusMethodInvocation  *invocation,
185                                    gint                    which_one,
186                                    gpointer                user_data)
187 {
188   if (which_one == 0)
189     {
190       const gchar *a_strv[] = {"foo", "bar", NULL};
191       const gchar *a_bytestring_array[] = {"foo\xff", "bar\xff", NULL};
192       GVariant *a_variant = g_variant_new_parsed ("{'first': (42, 42), 'second': (43, 43)}");
193       foo_bar_emit_test_signal (object, 43, a_strv, a_bytestring_array, a_variant); /* consumes a_variant */
194       foo_bar_complete_request_signal_emission (object, invocation);
195     }
196   return TRUE;
197 }
198
199 static gboolean
200 on_handle_request_multi_property_mods (FooBar                 *object,
201                                        GDBusMethodInvocation  *invocation,
202                                        gpointer                user_data)
203 {
204   foo_bar_set_y (object, foo_bar_get_y (object) + 1);
205   foo_bar_set_i (object, foo_bar_get_i (object) + 1);
206   foo_bar_set_y (object, foo_bar_get_y (object) + 1);
207   foo_bar_set_i (object, foo_bar_get_i (object) + 1);
208   g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
209   foo_bar_set_y (object, foo_bar_get_y (object) + 1);
210   foo_bar_set_i (object, foo_bar_get_i (object) + 1);
211   foo_bar_complete_request_multi_property_mods (object, invocation);
212   return TRUE;
213 }
214
215 static gboolean
216 on_handle_property_cancellation (FooBar                 *object,
217                                  GDBusMethodInvocation  *invocation,
218                                  gpointer                user_data)
219 {
220   guint n;
221   n = foo_bar_get_n (object);
222   /* This queues up a PropertiesChange event */
223   foo_bar_set_n (object, n + 1);
224   /* this modifies the queued up event */
225   foo_bar_set_n (object, n);
226   /* this flushes all PropertiesChanges event (sends the D-Bus message right
227    * away, if any - there should not be any)
228    */
229   g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
230   /* this makes us return the reply D-Bus method */
231   foo_bar_complete_property_cancellation (object, invocation);
232   return TRUE;
233 }
234
235 /* ---------------------------------------------------------------------------------------------------- */
236
237 static gboolean
238 on_handle_force_method (FooBat                 *object,
239                         GDBusMethodInvocation  *invocation,
240                         GVariant               *force_in_i,
241                         GVariant               *force_in_s,
242                         GVariant               *force_in_ay,
243                         GVariant               *force_in_struct,
244                         gpointer                user_data)
245 {
246   GVariant *ret_i;
247   GVariant *ret_s;
248   GVariant *ret_ay;
249   GVariant *ret_struct;
250   gint32 val;
251   gchar *s;
252
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);
256   g_free (s);
257   s = g_strdup_printf ("%s_foo\xff", g_variant_get_bytestring (force_in_ay));
258   ret_ay = g_variant_new_bytestring (s);
259   g_free (s);
260
261   g_variant_get (force_in_struct, "(i)", &val);
262   ret_struct = g_variant_new ("(i)", val + 10);
263
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);
268
269   foo_bat_emit_force_signal (object,
270                              ret_i,
271                              ret_s,
272                              ret_ay,
273                              ret_struct);
274
275   foo_bat_complete_force_method (object,
276                                  invocation,
277                                  ret_i,
278                                  ret_s,
279                                  ret_ay,
280                                  ret_struct);
281
282   g_variant_unref (ret_i);
283   g_variant_unref (ret_s);
284   g_variant_unref (ret_ay);
285   g_variant_unref (ret_struct);
286
287   return TRUE;
288 }
289
290
291 /* ---------------------------------------------------------------------------------------------------- */
292
293 static gboolean
294 my_g_authorize_method_handler (GDBusInterfaceSkeleton *interface,
295                                GDBusMethodInvocation  *invocation,
296                                gpointer                user_data)
297 {
298   const gchar *method_name;
299   gboolean authorized;
300
301   authorized = FALSE;
302
303   method_name = g_dbus_method_invocation_get_method_name (invocation);
304   if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
305     {
306       authorized = FALSE;
307     }
308   else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
309     {
310       authorized = TRUE;
311     }
312   else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
313     {
314       authorized = TRUE;
315     }
316   else
317     {
318       g_assert_not_reached ();
319     }
320
321   if (!authorized)
322     {
323       g_dbus_method_invocation_return_error (invocation,
324                                              G_IO_ERROR,
325                                              G_IO_ERROR_PERMISSION_DENIED,
326                                              "not authorized...");
327     }
328   return authorized;
329 }
330
331 static gboolean
332 my_object_authorize_method_handler (GDBusObjectSkeleton     *object,
333                                     GDBusInterfaceSkeleton  *interface,
334                                     GDBusMethodInvocation   *invocation,
335                                     gpointer                 user_data)
336 {
337   const gchar *method_name;
338   gboolean authorized;
339
340   authorized = FALSE;
341
342   method_name = g_dbus_method_invocation_get_method_name (invocation);
343   if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
344     {
345       authorized = TRUE;
346     }
347   else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
348     {
349       authorized = TRUE;
350     }
351   else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
352     {
353       authorized = FALSE;
354     }
355   else
356     {
357       g_assert_not_reached ();
358     }
359
360   if (!authorized)
361     {
362       g_dbus_method_invocation_return_error (invocation,
363                                              G_IO_ERROR,
364                                              G_IO_ERROR_PENDING,
365                                              "not authorized (from object)...");
366     }
367   return authorized;
368 }
369
370 static gboolean
371 on_handle_check_not_authorized (FooAuthorize           *object,
372                                 GDBusMethodInvocation  *invocation,
373                                 gpointer                user_data)
374 {
375   foo_authorize_complete_check_not_authorized (object, invocation);
376   return TRUE;
377 }
378
379 static gboolean
380 on_handle_check_authorized (FooAuthorize           *object,
381                             GDBusMethodInvocation  *invocation,
382                             gpointer                user_data)
383 {
384   foo_authorize_complete_check_authorized (object, invocation);
385   return TRUE;
386 }
387
388 static gboolean
389 on_handle_check_not_authorized_from_object (FooAuthorize           *object,
390                                             GDBusMethodInvocation  *invocation,
391                                             gpointer                user_data)
392 {
393   foo_authorize_complete_check_not_authorized_from_object (object, invocation);
394   return TRUE;
395 }
396
397 /* ---------------------------------------------------------------------------------------------------- */
398
399 static gboolean
400 on_handle_get_self (FooMethodThreads       *object,
401                     GDBusMethodInvocation  *invocation,
402                     gpointer                user_data)
403 {
404   gchar *s;
405   s = g_strdup_printf ("%p", g_thread_self ());
406   foo_method_threads_complete_get_self (object, invocation, s);
407   g_free (s);
408   return TRUE;
409 }
410
411 /* ---------------------------------------------------------------------------------------------------- */
412
413 static GThread *method_handler_thread = NULL;
414
415 static FooBar *exported_bar_object = NULL;
416 static FooBat *exported_bat_object = NULL;
417 static FooAuthorize *exported_authorize_object = NULL;
418 static GDBusObjectSkeleton *authorize_enclosing_object = NULL;
419 static FooMethodThreads *exported_thread_object_1 = NULL;
420 static FooMethodThreads *exported_thread_object_2 = NULL;
421
422 static void
423 on_bus_acquired (GDBusConnection *connection,
424                  const gchar     *name,
425                  gpointer         user_data)
426 {
427   GError *error;
428
429   /* Test that we can export an object using the generated
430    * FooBarSkeleton subclass. Notes:
431    *
432    * 1. We handle methods by simply connecting to the appropriate
433    * GObject signal.
434    *
435    * 2. Property storage is taken care of by the class; we can
436    *    use g_object_get()/g_object_set() (and the generated
437    *    C bindings at will)
438    */
439   error = NULL;
440   exported_bar_object = foo_bar_skeleton_new ();
441   foo_bar_set_ay (exported_bar_object, "ABCabc");
442   foo_bar_set_y (exported_bar_object, 42);
443   foo_bar_set_d (exported_bar_object, 43.0);
444   foo_bar_set_finally_normal_name (exported_bar_object, "There aint no place like home");
445   foo_bar_set_writeonly_property (exported_bar_object, "Mr. Burns");
446
447   /* The following works because it's on the Skeleton object - it will
448    * fail (at run-time) on a Proxy (see on_proxy_appeared() below)
449    */
450   foo_bar_set_readonly_property (exported_bar_object, "blah");
451   g_assert_cmpstr (foo_bar_get_writeonly_property (exported_bar_object), ==, "Mr. Burns");
452
453   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bar_object),
454                                     connection,
455                                     "/bar",
456                                     &error);
457   g_assert_no_error (error);
458   g_signal_connect (exported_bar_object,
459                     "handle-hello-world",
460                     G_CALLBACK (on_handle_hello_world),
461                     NULL);
462   g_signal_connect (exported_bar_object,
463                     "handle-test-primitive-types",
464                     G_CALLBACK (on_handle_test_primitive_types),
465                     NULL);
466   g_signal_connect (exported_bar_object,
467                     "handle-test-non-primitive-types",
468                     G_CALLBACK (on_handle_test_non_primitive_types),
469                     NULL);
470   g_signal_connect (exported_bar_object,
471                     "handle-request-signal-emission",
472                     G_CALLBACK (on_handle_request_signal_emission),
473                     NULL);
474   g_signal_connect (exported_bar_object,
475                     "handle-request-multi-property-mods",
476                     G_CALLBACK (on_handle_request_multi_property_mods),
477                     NULL);
478   g_signal_connect (exported_bar_object,
479                     "handle-property-cancellation",
480                     G_CALLBACK (on_handle_property_cancellation),
481                     NULL);
482
483   exported_bat_object = foo_bat_skeleton_new ();
484   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bat_object),
485                                     connection,
486                                     "/bat",
487                                     &error);
488   g_assert_no_error (error);
489   g_signal_connect (exported_bat_object,
490                     "handle-force-method",
491                     G_CALLBACK (on_handle_force_method),
492                     NULL);
493   g_object_set (exported_bat_object,
494                 "force-i", g_variant_new_int32 (43),
495                 "force-s", g_variant_new_string ("prop string"),
496                 "force-ay", g_variant_new_bytestring ("prop bytestring\xff"),
497                 "force-struct", g_variant_new ("(i)", 4300),
498                 NULL);
499
500   authorize_enclosing_object = g_dbus_object_skeleton_new ("/authorize");
501   g_signal_connect (authorize_enclosing_object,
502                     "authorize-method",
503                     G_CALLBACK (my_object_authorize_method_handler),
504                     NULL);
505   exported_authorize_object = foo_authorize_skeleton_new ();
506   g_dbus_object_skeleton_add_interface (authorize_enclosing_object,
507                                         G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
508   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_authorize_object),
509                                     connection,
510                                     "/authorize",
511                                     &error);
512   g_assert_no_error (error);
513   g_signal_connect (exported_authorize_object,
514                     "g-authorize-method",
515                     G_CALLBACK (my_g_authorize_method_handler),
516                     NULL);
517   g_signal_connect (exported_authorize_object,
518                     "handle-check-not-authorized",
519                     G_CALLBACK (on_handle_check_not_authorized),
520                     NULL);
521   g_signal_connect (exported_authorize_object,
522                     "handle-check-authorized",
523                     G_CALLBACK (on_handle_check_authorized),
524                     NULL);
525   g_signal_connect (exported_authorize_object,
526                     "handle-check-not-authorized-from-object",
527                     G_CALLBACK (on_handle_check_not_authorized_from_object),
528                     NULL);
529
530
531   /* only object 1 has the G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD flag set */
532   exported_thread_object_1 = foo_method_threads_skeleton_new ();
533   g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
534                                        G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
535   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
536                                     connection,
537                                     "/method_threads_1",
538                                     &error);
539   g_assert_no_error (error);
540   g_signal_connect (exported_thread_object_1,
541                     "handle-get-self",
542                     G_CALLBACK (on_handle_get_self),
543                     NULL);
544
545   exported_thread_object_2 = foo_method_threads_skeleton_new ();
546   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2),
547                                     connection,
548                                     "/method_threads_2",
549                                     &error);
550   g_assert_no_error (error);
551   g_signal_connect (exported_thread_object_2,
552                     "handle-get-self",
553                     G_CALLBACK (on_handle_get_self),
554                     NULL);
555
556   method_handler_thread = g_thread_self ();
557 }
558
559 static gpointer check_proxies_in_thread (gpointer user_data);
560
561 static void
562 on_name_acquired (GDBusConnection *connection,
563                   const gchar     *name,
564                   gpointer         user_data)
565 {
566   GMainLoop *loop = user_data;
567
568   g_thread_create (check_proxies_in_thread,
569                    loop,
570                    TRUE,
571                    NULL);
572 }
573
574 static void
575 on_name_lost (GDBusConnection *connection,
576               const gchar     *name,
577               gpointer         user_data)
578 {
579   g_assert_not_reached ();
580 }
581
582 /* ---------------------------------------------------------------------------------------------------- */
583
584 typedef struct
585 {
586   GMainLoop *thread_loop;
587   gint initial_y;
588   gint initial_i;
589   guint num_g_properties_changed;
590   gboolean received_test_signal;
591   guint num_notify_u;
592   guint num_notify_n;
593 } ClientData;
594
595 static void
596 on_notify_u (GObject    *object,
597            GParamSpec *pspec,
598            gpointer    user_data)
599 {
600   ClientData *data = user_data;
601   g_assert_cmpstr (pspec->name, ==, "u");
602   data->num_notify_u += 1;
603 }
604
605 static void
606 on_notify_n (GObject    *object,
607              GParamSpec *pspec,
608              gpointer    user_data)
609 {
610   ClientData *data = user_data;
611   g_assert_cmpstr (pspec->name, ==, "n");
612   data->num_notify_n += 1;
613 }
614
615 static void
616 on_g_properties_changed (GDBusProxy          *_proxy,
617                          GVariant            *changed_properties,
618                          const gchar* const  *invalidated_properties,
619                          gpointer             user_data)
620 {
621   ClientData *data = user_data;
622   FooBar *proxy = FOO_BAR (_proxy);
623
624   g_assert_cmpint (g_variant_n_children (changed_properties), ==, 2);
625
626   if (data->num_g_properties_changed == 0)
627     {
628       g_assert_cmpint (data->initial_y, ==, foo_bar_get_y (proxy) - 2);
629       g_assert_cmpint (data->initial_i, ==, foo_bar_get_i (proxy) - 2);
630     }
631   else if (data->num_g_properties_changed == 1)
632     {
633       g_assert_cmpint (data->initial_y, ==, foo_bar_get_y (proxy) - 3);
634       g_assert_cmpint (data->initial_i, ==, foo_bar_get_i (proxy) - 3);
635     }
636   else
637     g_assert_not_reached ();
638
639   data->num_g_properties_changed++;
640
641   if (data->num_g_properties_changed == 2)
642     g_main_loop_quit (data->thread_loop);
643 }
644
645 static void
646 on_test_signal (FooBar              *proxy,
647                 gint                 val_int32,
648                 const gchar* const  *array_of_strings,
649                 const gchar* const  *array_of_bytestrings,
650                 GVariant            *dict_s_to_pairs,
651                 gpointer             user_data)
652 {
653   ClientData *data = user_data;
654
655   g_assert_cmpint (val_int32, ==, 43);
656   g_assert_cmpstr (array_of_strings[0], ==, "foo");
657   g_assert_cmpstr (array_of_strings[1], ==, "bar");
658   g_assert (array_of_strings[2] == NULL);
659   g_assert_cmpstr (array_of_bytestrings[0], ==, "foo\xff");
660   g_assert_cmpstr (array_of_bytestrings[1], ==, "bar\xff");
661   g_assert (array_of_bytestrings[2] == NULL);
662
663   data->received_test_signal = TRUE;
664   g_main_loop_quit (data->thread_loop);
665 }
666
667 static void
668 on_property_cancellation_cb (FooBar        *proxy,
669                              GAsyncResult  *res,
670                              gpointer       user_data)
671 {
672   ClientData *data = user_data;
673   gboolean ret;
674   GError *error = NULL;
675
676   error = NULL;
677   ret = foo_bar_call_property_cancellation_finish (proxy, res, &error);
678   g_assert_no_error (error);
679   g_assert (ret);
680
681   g_main_loop_quit (data->thread_loop);
682 }
683
684 static void
685 check_bar_proxy (FooBar    *proxy,
686                  GMainLoop *thread_loop)
687 {
688   guchar ret_val_byte;
689   gboolean ret_val_boolean;
690   gint16 ret_val_int16;
691   guint16 ret_val_uint16;
692   gint ret_val_int32;
693   guint ret_val_uint32;
694   gint64 ret_val_int64;
695   guint64 ret_val_uint64;
696   gdouble ret_val_double;
697   gchar *ret_val_string;
698   gchar *ret_val_objpath;
699   gchar *ret_val_signature;
700   gchar *ret_val_bytestring;
701   gboolean ret;
702   GError *error;
703   ClientData *data;
704   guchar val_y;
705   gboolean val_b;
706   gint16 val_n;
707   guint16 val_q;
708   gint val_i;
709   guint val_u;
710   gint64 val_x;
711   guint64 val_t;
712   gdouble val_d;
713   gchar *val_s;
714   gchar *val_o;
715   gchar *val_g;
716   gchar *val_ay;
717   gchar **val_as;
718   GVariant *val_ao;
719   GVariant *val_ag;
720   gint32 val_unset_i;
721   gdouble val_unset_d;
722   gchar *val_unset_s;
723   gchar *val_unset_o;
724   gchar *val_unset_g;
725   gchar *val_unset_ay;
726   gchar **val_unset_as;
727   GVariant *val_unset_ao;
728   GVariant *val_unset_ag;
729   GVariant *val_unset_struct;
730   gchar *val_finally_normal_name;
731   GVariant *v;
732   gchar *s;
733
734   data = g_new0 (ClientData, 1);
735   data->thread_loop = thread_loop;
736
737   v = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "y");
738   g_assert (v != NULL);
739   g_variant_unref (v);
740
741   /* set empty values to non-empty */
742   val_unset_i = 42;
743   val_unset_d = 42.0;
744   val_unset_s = "42";
745   val_unset_o = "42";
746   val_unset_g = "42";
747   val_unset_ay = NULL;
748   val_unset_as = NULL;
749   val_unset_ao = NULL;
750   val_unset_ag = NULL;
751   val_unset_struct = NULL;
752   /* check properties */
753   g_object_get (proxy,
754                 "y", &val_y,
755                 "b", &val_b,
756                 "n", &val_n,
757                 "q", &val_q,
758                 "i", &val_i,
759                 "u", &val_u,
760                 "x", &val_x,
761                 "t", &val_t,
762                 "d", &val_d,
763                 "s", &val_s,
764                 "o", &val_o,
765                 "g", &val_g,
766                 "ay", &val_ay,
767                 "as", &val_as,
768                 "ao", &val_ao,
769                 "ag", &val_ag,
770                 "unset_i", &val_unset_i,
771                 "unset_d", &val_unset_d,
772                 "unset_s", &val_unset_s,
773                 "unset_o", &val_unset_o,
774                 "unset_g", &val_unset_g,
775                 "unset_ay", &val_unset_ay,
776                 "unset_as", &val_unset_as,
777                 "unset_ao", &val_unset_ao,
778                 "unset_ag", &val_unset_ag,
779                 "unset_struct", &val_unset_struct,
780                 "finally-normal-name", &val_finally_normal_name,
781                 NULL);
782   g_assert_cmpint (val_y, ==, 42);
783   g_assert_cmpstr (val_finally_normal_name, ==, "There aint no place like home");
784   g_free (val_s);
785   g_free (val_o);
786   g_free (val_g);
787   g_assert_cmpstr (val_ay, ==, "ABCabc");
788   g_free (val_ay);
789   g_strfreev (val_as);
790   g_variant_unref (val_ao);
791   g_variant_unref (val_ag);
792   g_free (val_finally_normal_name);
793   /* check empty values */
794   g_assert_cmpint (val_unset_i, ==, 0);
795   g_assert_cmpfloat (val_unset_d, ==, 0.0);
796   g_assert_cmpstr (val_unset_s, ==, "");
797   g_assert_cmpstr (val_unset_o, ==, "/");
798   g_assert_cmpstr (val_unset_g, ==, "");
799   g_free (val_unset_s);
800   g_free (val_unset_o);
801   g_free (val_unset_g);
802   g_assert_cmpstr (val_unset_ay, ==, "");
803   g_assert (val_unset_as[0] == NULL);
804   g_assert (g_variant_is_of_type (val_unset_ao, G_VARIANT_TYPE ("ao")));
805   g_assert (g_variant_is_of_type (val_unset_ag, G_VARIANT_TYPE ("ag")));
806   g_assert (g_variant_is_of_type (val_unset_struct, G_VARIANT_TYPE ("(idsogayasaoag)")));
807   s = g_variant_print (val_unset_struct, TRUE);
808   g_assert_cmpstr (s, ==, "(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])");
809   g_free (s);
810   g_free (val_unset_ay);
811   g_strfreev (val_unset_as);
812   g_variant_unref (val_unset_ao);
813   g_variant_unref (val_unset_ag);
814   g_variant_unref (val_unset_struct);
815
816   /* Try setting a property. This causes the generated glue to invoke
817    * the org.fd.DBus.Properties.Set() method asynchronously. So we
818    * have to wait for properties-changed...
819    */
820   foo_bar_set_finally_normal_name (proxy, "foo!");
821   _g_assert_property_notify (proxy, "finally-normal-name");
822   g_assert_cmpstr (foo_bar_get_finally_normal_name (proxy), ==, "foo!");
823
824   /* Try setting properties that requires memory management. This
825    * is to exercise the paths that frees the references.
826    */
827   const gchar *array_of_strings[3] = {"one", "two", NULL};
828   const gchar *array_of_bytestrings[3] = {"one\xff", "two\xff", NULL};
829   g_object_set (proxy,
830                 "s", "a string",
831                 "o", "/a/path",
832                 "g", "asig",
833                 "ay", g_variant_new_parsed ("[byte 0x65, 0x67]"),
834                 "as", array_of_strings,
835                 "ao", g_variant_new_parsed ("[@o '/one', '/one/two']"),
836                 "ag", g_variant_new_parsed ("[@g 'ass', 'git']"),
837                 NULL);
838
839   error = NULL;
840   ret = foo_bar_call_test_primitive_types_sync (proxy,
841                                                 10,
842                                                 TRUE,
843                                                 11,
844                                                 12,
845                                                 13,
846                                                 14,
847                                                 15,
848                                                 16,
849                                                 17,
850                                                 "a string",
851                                                 "/a/path",
852                                                 "asig",
853                                                 "bytestring\xff",
854                                                 &ret_val_byte,
855                                                 &ret_val_boolean,
856                                                 &ret_val_int16,
857                                                 &ret_val_uint16,
858                                                 &ret_val_int32,
859                                                 &ret_val_uint32,
860                                                 &ret_val_int64,
861                                                 &ret_val_uint64,
862                                                 &ret_val_double,
863                                                 &ret_val_string,
864                                                 &ret_val_objpath,
865                                                 &ret_val_signature,
866                                                 &ret_val_bytestring,
867                                                 NULL, /* GCancellable */
868                                                 &error);
869   g_assert_no_error (error);
870   g_assert (ret);
871
872   error = NULL;
873   ret = foo_bar_call_test_non_primitive_types_sync (proxy,
874                                                     g_variant_new_parsed ("{'one': 'red',"
875                                                                           " 'two': 'blue'}"),
876                                                     g_variant_new_parsed ("{'first': (42, 42), "
877                                                                           "'second': (43, 43)}"),
878                                                     g_variant_new_parsed ("(42, 'foo', 'bar')"),
879                                                     array_of_strings,
880                                                     g_variant_new_parsed ("[@o '/one', '/one/two']"),
881                                                     g_variant_new_parsed ("[@g 'ass', 'git']"),
882                                                     array_of_bytestrings,
883                                                     &s,
884                                                     NULL, /* GCancellable */
885                                                     &error);
886
887   g_assert_no_error (error);
888   g_assert (ret);
889
890   /* Check that org.freedesktop.DBus.Error.UnknownMethod is returned on
891    * unimplemented methods.
892    */
893   error = NULL;
894   ret = foo_bar_call_unimplemented_method_sync (proxy, NULL /* GCancellable */, &error);
895   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
896   g_error_free (error);
897   error = NULL;
898   g_assert (!ret);
899
900   g_signal_connect (proxy,
901                     "test-signal",
902                     G_CALLBACK (on_test_signal),
903                     data);
904   error = NULL;
905   ret = foo_bar_call_request_signal_emission_sync (proxy, 0, NULL, &error);
906   g_assert_no_error (error);
907   g_assert (ret);
908
909   g_assert (!data->received_test_signal);
910   g_main_loop_run (thread_loop);
911   g_assert (data->received_test_signal);
912
913   /* Try setting a property. This causes the generated glue to invoke
914    * the org.fd.DBus.Properties.Set() method asynchronously. So we
915    * have to wait for properties-changed...
916    */
917   foo_bar_set_finally_normal_name (proxy, "hey back!");
918   _g_assert_property_notify (proxy, "finally-normal-name");
919   g_assert_cmpstr (foo_bar_get_finally_normal_name (proxy), ==, "hey back!");
920
921   /* Check that grouping changes in idle works.
922    *
923    * See on_handle_request_multi_property_mods(). The server should
924    * emit exactly two PropertiesChanged signals each containing two
925    * properties.
926    *
927    * On the first reception, y and i should both be increased by
928    * two. On the the second reception, only by one. The signal handler
929    * checks this.
930    *
931    * This also checks that _drain_notify() works.
932    */
933   data->initial_y = foo_bar_get_y (proxy);
934   data->initial_i = foo_bar_get_i (proxy);
935   g_signal_connect (proxy,
936                     "g-properties-changed",
937                     G_CALLBACK (on_g_properties_changed),
938                     data);
939   error = NULL;
940   ret = foo_bar_call_request_multi_property_mods_sync (proxy, NULL, &error);
941   g_assert_no_error (error);
942   g_assert (ret);
943   g_main_loop_run (thread_loop);
944   g_assert_cmpint (data->num_g_properties_changed, ==, 2);
945   g_signal_handlers_disconnect_by_func (proxy,
946                                         G_CALLBACK (on_g_properties_changed),
947                                         data);
948
949   /* Check that we don't emit PropertiesChanged() if the property
950    * didn't change... we actually get two notifies.. one for the
951    * local set (without a value change) and one when receiving
952    * the PropertiesChanged() signal generated from the remote end.
953    */
954   g_assert_cmpint (data->num_notify_u, ==, 0);
955   g_signal_connect (proxy,
956                     "notify::u",
957                     G_CALLBACK (on_notify_u),
958                     data);
959   foo_bar_set_u (proxy, 1042);
960   g_assert_cmpint (data->num_notify_u, ==, 1);
961   g_assert_cmpint (foo_bar_get_u (proxy), ==, 0);
962   _g_assert_property_notify (proxy, "u");
963   g_assert_cmpint (foo_bar_get_u (proxy), ==, 1042);
964   g_assert_cmpint (data->num_notify_u, ==, 2);
965
966   /* Now change u again to the same value.. this will cause a
967    * local notify:: notify and the usual Properties.Set() call
968    *
969    * (Btw, why also the Set() call if the value in the cache is
970    * the same? Because someone else might have changed it
971    * in the mean time and we're just waiting to receive the
972    * PropertiesChanged() signal...)
973    *
974    * More tricky - how do we check for the *absence* of the
975    * notification that u changed? Simple: we change another
976    * property and wait for that PropertiesChanged() message
977    * to arrive.
978    */
979   foo_bar_set_u (proxy, 1042);
980   g_assert_cmpint (data->num_notify_u, ==, 3);
981
982   g_assert_cmpint (data->num_notify_n, ==, 0);
983   g_signal_connect (proxy,
984                     "notify::n",
985                     G_CALLBACK (on_notify_n),
986                     data);
987   foo_bar_set_n (proxy, 10042);
988   g_assert_cmpint (data->num_notify_n, ==, 1);
989   g_assert_cmpint (foo_bar_get_n (proxy), ==, 0);
990   _g_assert_property_notify (proxy, "n");
991   g_assert_cmpint (foo_bar_get_n (proxy), ==, 10042);
992   g_assert_cmpint (data->num_notify_n, ==, 2);
993   /* Checks that u didn't change at all */
994   g_assert_cmpint (data->num_notify_u, ==, 3);
995
996   /* Now we check that if the service does
997    *
998    *   guint n = foo_bar_get_n (foo);
999    *   foo_bar_set_n (foo, n + 1);
1000    *   foo_bar_set_n (foo, n);
1001    *
1002    *  then no PropertiesChanged() signal is emitted!
1003    */
1004   error = NULL;
1005   foo_bar_call_property_cancellation (proxy,
1006                                       NULL, /* GCancellable */
1007                                       (GAsyncReadyCallback) on_property_cancellation_cb,
1008                                       data);
1009   g_main_loop_run (thread_loop);
1010   /* Checks that n didn't change at all */
1011   g_assert_cmpint (data->num_notify_n, ==, 2);
1012
1013   /* cleanup */
1014   g_free (data);
1015 }
1016
1017 /* ---------------------------------------------------------------------------------------------------- */
1018
1019 static void
1020 on_force_signal (FooBat    *proxy,
1021                  GVariant  *force_i,
1022                  GVariant  *force_s,
1023                  GVariant  *force_ay,
1024                  GVariant  *force_struct,
1025                  gpointer   user_data)
1026 {
1027   gboolean *signal_received = user_data;
1028   gint val;
1029
1030   g_assert (!(*signal_received));
1031
1032   g_assert_cmpint (g_variant_get_int32 (force_i), ==, 42 + 10);
1033   g_assert_cmpstr (g_variant_get_string (force_s, NULL), ==, "a string_foo");
1034   g_assert_cmpstr (g_variant_get_bytestring (force_ay), ==, "a bytestring\xff_foo\xff");
1035   g_variant_get (force_struct, "(i)", &val);
1036   g_assert_cmpint (val, ==, 4200 + 10);
1037
1038   *signal_received = TRUE;
1039 }
1040
1041 static void
1042 check_bat_proxy (FooBat *proxy,
1043                  GMainLoop *thread_loop)
1044 {
1045   GError *error;
1046   GVariant *ret_i;
1047   GVariant *ret_s;
1048   GVariant *ret_ay;
1049   GVariant *ret_struct;
1050   gint val;
1051   gboolean force_signal_received;
1052
1053   /* --------------------------------------------------- */
1054   /* Check type-mapping where we force use of a GVariant */
1055   /* --------------------------------------------------- */
1056
1057   /* check properties */
1058   g_object_get (proxy,
1059                 "force-i", &ret_i,
1060                 "force-s", &ret_s,
1061                 "force-ay", &ret_ay,
1062                 "force-struct", &ret_struct,
1063                 NULL);
1064   g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 43);
1065   g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "prop string");
1066   g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "prop bytestring\xff");
1067   g_variant_get (ret_struct, "(i)", &val);
1068   g_assert_cmpint (val, ==, 4300);
1069   g_variant_unref (ret_i);
1070   g_variant_unref (ret_s);
1071   g_variant_unref (ret_ay);
1072   g_variant_unref (ret_struct);
1073
1074   /* check method and signal */
1075   force_signal_received = FALSE;
1076   g_signal_connect (proxy,
1077                     "force-signal",
1078                     G_CALLBACK (on_force_signal),
1079                     &force_signal_received);
1080
1081   error = NULL;
1082   foo_bat_call_force_method_sync (proxy,
1083                                   g_variant_new_int32 (42),
1084                                   g_variant_new_string ("a string"),
1085                                   g_variant_new_bytestring ("a bytestring\xff"),
1086                                   g_variant_new ("(i)", 4200),
1087                                   &ret_i,
1088                                   &ret_s,
1089                                   &ret_ay,
1090                                   &ret_struct,
1091                                   NULL, /* GCancellable* */
1092                                   &error);
1093   g_assert_no_error (error);
1094   g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 42 + 10);
1095   g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "a string_foo");
1096   g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "a bytestring\xff_foo\xff");
1097   g_variant_get (ret_struct, "(i)", &val);
1098   g_assert_cmpint (val, ==, 4200 + 10);
1099   g_variant_unref (ret_i);
1100   g_variant_unref (ret_s);
1101   g_variant_unref (ret_ay);
1102   g_variant_unref (ret_struct);
1103   _g_assert_signal_received (proxy, "force-signal");
1104   g_assert (force_signal_received);
1105 }
1106
1107 /* ---------------------------------------------------------------------------------------------------- */
1108
1109 static void
1110 check_authorize_proxy (FooAuthorize *proxy,
1111                        GMainLoop *thread_loop)
1112 {
1113   GError *error;
1114   gboolean ret;
1115
1116   /* Check that g-authorize-method works as intended */
1117
1118   error = NULL;
1119   ret = foo_authorize_call_check_not_authorized_sync (proxy, NULL, &error);
1120   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1121   g_error_free (error);
1122   g_assert (!ret);
1123
1124   error = NULL;
1125   ret = foo_authorize_call_check_authorized_sync (proxy, NULL, &error);
1126   g_assert_no_error (error);
1127   g_assert (ret);
1128
1129   error = NULL;
1130   ret = foo_authorize_call_check_not_authorized_from_object_sync (proxy, NULL, &error);
1131   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
1132   g_error_free (error);
1133   g_assert (!ret);
1134 }
1135
1136 /* ---------------------------------------------------------------------------------------------------- */
1137
1138 static GThread *
1139 get_self_via_proxy (FooMethodThreads *proxy_1)
1140 {
1141   GError *error;
1142   gchar *self_str;
1143   gboolean ret;
1144   gpointer self;
1145
1146   error = NULL;
1147   ret = foo_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
1148   g_assert_no_error (error);
1149   g_assert (ret);
1150
1151   g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
1152
1153   g_free (self_str);
1154
1155   return self;
1156 }
1157
1158 static void
1159 check_thread_proxies (FooMethodThreads *proxy_1,
1160                       FooMethodThreads *proxy_2,
1161                       GMainLoop *thread_loop)
1162 {
1163   /* proxy_1 is indeed using threads so should never get the handler thread */
1164   g_assert (get_self_via_proxy (proxy_1) != method_handler_thread);
1165
1166   /* proxy_2 is not using threads so should get the handler thread */
1167   g_assert (get_self_via_proxy (proxy_2) == method_handler_thread);
1168 }
1169
1170 /* ---------------------------------------------------------------------------------------------------- */
1171
1172 static gpointer
1173 check_proxies_in_thread (gpointer user_data)
1174 {
1175   GMainLoop *loop = user_data;
1176   GMainContext *thread_context;
1177   GMainLoop *thread_loop;
1178   GError *error;
1179   FooBar *bar_proxy;
1180   FooBat *bat_proxy;
1181   FooAuthorize *authorize_proxy;
1182   FooMethodThreads *thread_proxy_1;
1183   FooMethodThreads *thread_proxy_2;
1184
1185   thread_context = g_main_context_new ();
1186   thread_loop = g_main_loop_new (thread_context, FALSE);
1187   g_main_context_push_thread_default (thread_context);
1188
1189   /* Check the object */
1190   error = NULL;
1191   bar_proxy = foo_bar_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1192                                               G_DBUS_PROXY_FLAGS_NONE,
1193                                               "org.gtk.GDBus.BindingsTool.Test",
1194                                               "/bar",
1195                                               NULL, /* GCancellable* */
1196                                               &error);
1197   check_bar_proxy (bar_proxy, thread_loop);
1198   g_assert_no_error (error);
1199   g_object_unref (bar_proxy);
1200
1201   error = NULL;
1202   bat_proxy = foo_bat_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1203                                               G_DBUS_PROXY_FLAGS_NONE,
1204                                               "org.gtk.GDBus.BindingsTool.Test",
1205                                               "/bat",
1206                                               NULL, /* GCancellable* */
1207                                               &error);
1208   check_bat_proxy (bat_proxy, thread_loop);
1209   g_assert_no_error (error);
1210   g_object_unref (bat_proxy);
1211
1212   error = NULL;
1213   authorize_proxy = foo_authorize_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1214                                                           G_DBUS_PROXY_FLAGS_NONE,
1215                                                           "org.gtk.GDBus.BindingsTool.Test",
1216                                                           "/authorize",
1217                                                           NULL, /* GCancellable* */
1218                                                           &error);
1219   check_authorize_proxy (authorize_proxy, thread_loop);
1220   g_assert_no_error (error);
1221   g_object_unref (authorize_proxy);
1222
1223   error = NULL;
1224   thread_proxy_1 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1225                                                               G_DBUS_PROXY_FLAGS_NONE,
1226                                                               "org.gtk.GDBus.BindingsTool.Test",
1227                                                               "/method_threads_1",
1228                                                               NULL, /* GCancellable* */
1229                                                               &error);
1230   g_assert_no_error (error);
1231   thread_proxy_2 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1232                                                               G_DBUS_PROXY_FLAGS_NONE,
1233                                                               "org.gtk.GDBus.BindingsTool.Test",
1234                                                               "/method_threads_2",
1235                                                               NULL, /* GCancellable* */
1236                                                               &error);
1237   g_assert_no_error (error);
1238   check_thread_proxies (thread_proxy_1, thread_proxy_2, thread_loop);
1239   g_object_unref (thread_proxy_1);
1240   g_object_unref (thread_proxy_2);
1241
1242   g_main_loop_unref (thread_loop);
1243   g_main_context_unref (thread_context);
1244
1245   /* this breaks out of the loop in main() (below) */
1246   g_main_loop_quit (loop);
1247   return NULL;
1248 }
1249
1250 /* ---------------------------------------------------------------------------------------------------- */
1251
1252 typedef struct
1253 {
1254   gchar *xml;
1255   GMainLoop *loop;
1256 } IntrospectData;
1257
1258 static void
1259 introspect_cb (GDBusConnection   *connection,
1260                GAsyncResult      *res,
1261                gpointer           user_data)
1262 {
1263   IntrospectData *data = user_data;
1264   GVariant *result;
1265   GError *error;
1266
1267   error = NULL;
1268   result = g_dbus_connection_call_finish (connection,
1269                                           res,
1270                                           &error);
1271   g_assert_no_error (error);
1272   g_assert (result != NULL);
1273   g_variant_get (result, "(s)", &data->xml);
1274   g_variant_unref (result);
1275
1276   g_main_loop_quit (data->loop);
1277 }
1278
1279 static GDBusNodeInfo *
1280 introspect (GDBusConnection  *connection,
1281             const gchar      *name,
1282             const gchar      *object_path,
1283             GMainLoop        *loop)
1284 {
1285   GError *error;
1286   GDBusNodeInfo *node_info;
1287   IntrospectData *data;
1288
1289   data = g_new0 (IntrospectData, 1);
1290   data->xml = NULL;
1291   data->loop = loop;
1292
1293   /* do this async to avoid deadlocks */
1294   g_dbus_connection_call (connection,
1295                           name,
1296                           object_path,
1297                           "org.freedesktop.DBus.Introspectable",
1298                           "Introspect",
1299                           NULL, /* params */
1300                           G_VARIANT_TYPE ("(s)"),
1301                           G_DBUS_CALL_FLAGS_NONE,
1302                           -1,
1303                           NULL,
1304                           (GAsyncReadyCallback) introspect_cb,
1305                           data);
1306   g_main_loop_run (loop);
1307   g_assert (data->xml != NULL);
1308
1309   error = NULL;
1310   node_info = g_dbus_node_info_new_for_xml (data->xml, &error);
1311   g_assert_no_error (error);
1312   g_assert (node_info != NULL);
1313   g_free (data->xml);
1314   g_free (data);
1315
1316   return node_info;
1317 }
1318
1319 static guint
1320 count_interfaces (GDBusNodeInfo *info)
1321 {
1322   guint n;
1323   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1324     ;
1325   return n;
1326 }
1327
1328 static guint
1329 count_nodes (GDBusNodeInfo *info)
1330 {
1331   guint n;
1332   for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
1333     ;
1334   return n;
1335 }
1336
1337 static guint
1338 has_interface (GDBusNodeInfo *info,
1339                const gchar   *name)
1340 {
1341   guint n;
1342   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1343     {
1344       if (g_strcmp0 (info->interfaces[n]->name, name) == 0)
1345         return TRUE;
1346     }
1347   return FALSE;
1348 }
1349
1350 /* ---------------------------------------------------------------------------------------------------- */
1351
1352 typedef struct {
1353   GMainLoop *loop;
1354   GVariant *result;
1355 } OMGetManagedObjectsData;
1356
1357 static void
1358 om_get_all_cb (GDBusConnection *connection,
1359                GAsyncResult    *res,
1360                gpointer         user_data)
1361 {
1362   OMGetManagedObjectsData *data = user_data;
1363   GError *error;
1364
1365   error = NULL;
1366   data->result = g_dbus_connection_call_finish (connection,
1367                                                 res,
1368                                                 &error);
1369   g_assert_no_error (error);
1370   g_assert (data->result != NULL);
1371   g_main_loop_quit (data->loop);
1372 }
1373
1374 static void
1375 om_check_get_all (GDBusConnection *c,
1376                   GMainLoop       *loop,
1377                   const gchar     *str)
1378 {
1379   OMGetManagedObjectsData data;
1380   gchar *s;
1381
1382   data.loop = loop;
1383   data.result = NULL;
1384
1385   /* do this async to avoid deadlocks */
1386   g_dbus_connection_call (c,
1387                           g_dbus_connection_get_unique_name (c),
1388                           "/managed",
1389                           "org.freedesktop.DBus.ObjectManager",
1390                           "GetManagedObjects",
1391                           NULL, /* params */
1392                           G_VARIANT_TYPE ("(a{oa{sa{sv}}})"),
1393                           G_DBUS_CALL_FLAGS_NONE,
1394                           -1,
1395                           NULL,
1396                           (GAsyncReadyCallback) om_get_all_cb,
1397                           &data);
1398   g_main_loop_run (loop);
1399   g_assert (data.result != NULL);
1400   s = g_variant_print (data.result, TRUE);
1401   g_assert_cmpstr (s, ==, str);
1402   g_free (s);
1403   g_variant_unref (data.result);
1404 }
1405
1406 typedef struct
1407 {
1408   GMainLoop *loop;
1409   guint state;
1410
1411   guint num_object_proxy_added_signals;
1412   guint num_object_proxy_removed_signals;
1413   guint num_interface_added_signals;
1414   guint num_interface_removed_signals;
1415 } OMData;
1416
1417 static gint
1418 my_pstrcmp (const gchar **a, const gchar **b)
1419 {
1420   return g_strcmp0 (*a, *b);
1421 }
1422
1423 static void
1424 om_check_interfaces_added (const gchar *signal_name,
1425                            GVariant *parameters,
1426                            const gchar *object_path,
1427                            const gchar *first_interface_name,
1428                            ...)
1429 {
1430   const gchar *path;
1431   GVariant *array;
1432   guint n;
1433   GPtrArray *interfaces;
1434   GPtrArray *interfaces_in_message;
1435   va_list var_args;
1436   const gchar *str;
1437
1438   interfaces = g_ptr_array_new ();
1439   g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1440   va_start (var_args, first_interface_name);
1441   do
1442     {
1443       str = va_arg (var_args, const gchar *);
1444       if (str == NULL)
1445         break;
1446       g_ptr_array_add (interfaces, (gpointer) str);
1447     }
1448   while (TRUE);
1449   va_end (var_args);
1450
1451   g_variant_get (parameters, "(&o*)", &path, &array);
1452   g_assert_cmpstr (signal_name, ==, "InterfacesAdded");
1453   g_assert_cmpstr (path, ==, object_path);
1454   g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1455   interfaces_in_message = g_ptr_array_new ();
1456   for (n = 0; n < interfaces->len; n++)
1457     {
1458       const gchar *iface_name;
1459       g_variant_get_child (array, n, "{&sa{sv}}", &iface_name, NULL);
1460       g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1461     }
1462   g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1463   g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1464   g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1465   for (n = 0; n < interfaces->len; n++)
1466     g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1467   g_ptr_array_unref (interfaces_in_message);
1468   g_ptr_array_unref (interfaces);
1469   g_variant_unref (array);
1470 }
1471
1472 static void
1473 om_check_interfaces_removed (const gchar *signal_name,
1474                              GVariant *parameters,
1475                              const gchar *object_path,
1476                              const gchar *first_interface_name,
1477                              ...)
1478 {
1479   const gchar *path;
1480   GVariant *array;
1481   guint n;
1482   GPtrArray *interfaces;
1483   GPtrArray *interfaces_in_message;
1484   va_list var_args;
1485   const gchar *str;
1486
1487   interfaces = g_ptr_array_new ();
1488   g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1489   va_start (var_args, first_interface_name);
1490   do
1491     {
1492       str = va_arg (var_args, const gchar *);
1493       if (str == NULL)
1494         break;
1495       g_ptr_array_add (interfaces, (gpointer) str);
1496     }
1497   while (TRUE);
1498   va_end (var_args);
1499
1500   g_variant_get (parameters, "(&o*)", &path, &array);
1501   g_assert_cmpstr (signal_name, ==, "InterfacesRemoved");
1502   g_assert_cmpstr (path, ==, object_path);
1503   g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1504   interfaces_in_message = g_ptr_array_new ();
1505   for (n = 0; n < interfaces->len; n++)
1506     {
1507       const gchar *iface_name;
1508       g_variant_get_child (array, n, "&s", &iface_name, NULL);
1509       g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1510     }
1511   g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1512   g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1513   g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1514   for (n = 0; n < interfaces->len; n++)
1515     g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1516   g_ptr_array_unref (interfaces_in_message);
1517   g_ptr_array_unref (interfaces);
1518   g_variant_unref (array);
1519 }
1520
1521 static void
1522 om_on_signal (GDBusConnection *connection,
1523               const gchar     *sender_name,
1524               const gchar     *object_path,
1525               const gchar     *interface_name,
1526               const gchar     *signal_name,
1527               GVariant        *parameters,
1528               gpointer         user_data)
1529 {
1530   OMData *om_data = user_data;
1531
1532   //g_debug ("foo: %s", g_variant_print (parameters, TRUE));
1533
1534   switch (om_data->state)
1535     {
1536     default:
1537     case 0:
1538       g_print ("failing and om_data->state=%d on signal %s, params=%s\n",
1539                om_data->state,
1540                signal_name,
1541                g_variant_print (parameters, TRUE));
1542       g_assert_not_reached ();
1543       break;
1544
1545     case 1:
1546       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1547                                  "org.project.Bar", NULL);
1548       om_data->state = 2;
1549       g_main_loop_quit (om_data->loop);
1550       break;
1551
1552     case 3:
1553       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1554                                    "org.project.Bar", NULL);
1555       om_data->state = 5;
1556       /* keep running the loop */
1557       break;
1558
1559     case 5:
1560       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1561                                  "org.project.Bar", NULL);
1562       om_data->state = 6;
1563       g_main_loop_quit (om_data->loop);
1564       break;
1565
1566     case 7:
1567       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1568                                    "org.project.Bar", NULL);
1569       om_data->state = 9;
1570       /* keep running the loop */
1571       break;
1572
1573     case 9:
1574       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1575                                  "org.project.Bar", NULL);
1576       om_data->state = 10;
1577       g_main_loop_quit (om_data->loop);
1578       break;
1579
1580     case 11:
1581       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1582                                  "org.project.Bat", NULL);
1583       om_data->state = 12;
1584       g_main_loop_quit (om_data->loop);
1585       break;
1586
1587     case 13:
1588       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1589                                    "org.project.Bar", NULL);
1590       om_data->state = 14;
1591       g_main_loop_quit (om_data->loop);
1592       break;
1593
1594     case 15:
1595       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1596                                    "org.project.Bat", NULL);
1597       om_data->state = 16;
1598       g_main_loop_quit (om_data->loop);
1599       break;
1600
1601     case 17:
1602       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1603                                  "com.acme.Coyote", NULL);
1604       om_data->state = 18;
1605       g_main_loop_quit (om_data->loop);
1606       break;
1607
1608     case 101:
1609       om_check_interfaces_added (signal_name, parameters, "/managed/second",
1610                                  "org.project.Bat", "org.project.Bar", NULL);
1611       om_data->state = 102;
1612       g_main_loop_quit (om_data->loop);
1613       break;
1614
1615     case 103:
1616       om_check_interfaces_removed (signal_name, parameters, "/managed/second",
1617                                    "org.project.Bat", "org.project.Bar", NULL);
1618       om_data->state = 104;
1619       g_main_loop_quit (om_data->loop);
1620       break;
1621     }
1622 }
1623
1624 static GAsyncResult *om_res = NULL;
1625
1626 static void
1627 om_pm_start_cb (GDBusObjectManagerClient *manager,
1628                 GAsyncResult      *res,
1629                 gpointer           user_data)
1630 {
1631   GMainLoop *loop = user_data;
1632   om_res = g_object_ref (res);
1633   g_main_loop_quit (loop);
1634 }
1635
1636 static GType
1637 get_proxy_type (GDBusObjectManagerClient *manager,
1638                 const gchar       *object_path,
1639                 const gchar       *interface_name,
1640                 gpointer           user_data)
1641 {
1642   GType type;
1643
1644   g_assert_cmpint (GPOINTER_TO_UINT (user_data), ==, 42);
1645
1646   type = G_TYPE_DBUS_PROXY;
1647   /* only map Bar and Bat, not other interface types */
1648   if (g_strcmp0 (interface_name, "org.project.Bar") == 0)
1649     type = FOO_TYPE_BAR_PROXY;
1650   else if (g_strcmp0 (interface_name, "org.project.Bat") == 0)
1651     type = FOO_TYPE_BAT_PROXY;
1652
1653   return type;
1654 }
1655
1656 static void
1657 on_interface_added (GDBusObject    *object,
1658                     GDBusInterface *interface,
1659                     gpointer        user_data)
1660 {
1661   OMData *om_data = user_data;
1662   om_data->num_interface_added_signals += 1;
1663 }
1664
1665 static void
1666 on_interface_removed (GDBusObject    *object,
1667                       GDBusInterface *interface,
1668                       gpointer        user_data)
1669 {
1670   OMData *om_data = user_data;
1671   om_data->num_interface_removed_signals += 1;
1672 }
1673
1674 static void
1675 on_object_proxy_added (GDBusObjectManagerClient  *manager,
1676                        GDBusObjectProxy   *object_proxy,
1677                        gpointer            user_data)
1678 {
1679   OMData *om_data = user_data;
1680   om_data->num_object_proxy_added_signals += 1;
1681   g_signal_connect (object_proxy,
1682                     "interface-added",
1683                     G_CALLBACK (on_interface_added),
1684                     om_data);
1685   g_signal_connect (object_proxy,
1686                     "interface-removed",
1687                     G_CALLBACK (on_interface_removed),
1688                     om_data);
1689 }
1690
1691 static void
1692 on_object_proxy_removed (GDBusObjectManagerClient  *manager,
1693                          GDBusObjectProxy   *object_proxy,
1694                          gpointer            user_data)
1695 {
1696   OMData *om_data = user_data;
1697   om_data->num_object_proxy_removed_signals += 1;
1698   g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1699                                                          G_CALLBACK (on_interface_added),
1700                                                          om_data), ==, 1);
1701   g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1702                                                          G_CALLBACK (on_interface_removed),
1703                                                          om_data), ==, 1);
1704 }
1705
1706 static void
1707 om_check_property_and_signal_emission (GMainLoop  *loop,
1708                                        FooBar     *skeleton,
1709                                        FooBar     *proxy)
1710 {
1711   /* First PropertiesChanged */
1712   g_assert_cmpint (foo_bar_get_i (skeleton), ==, 0);
1713   g_assert_cmpint (foo_bar_get_i (proxy), ==, 0);
1714   foo_bar_set_i (skeleton, 1);
1715   _g_assert_property_notify (proxy, "i");
1716   g_assert_cmpint (foo_bar_get_i (skeleton), ==, 1);
1717   g_assert_cmpint (foo_bar_get_i (proxy), ==, 1);
1718
1719   /* Then just a regular signal */
1720   foo_bar_emit_another_signal (skeleton, "word");
1721   _g_assert_signal_received (proxy, "another-signal");
1722 }
1723
1724 static void
1725 check_object_manager (void)
1726 {
1727   GDBusObjectSkeleton *o;
1728   GDBusObjectSkeleton *o2;
1729   GDBusInterfaceSkeleton *i;
1730   GDBusInterfaceSkeleton *i2;
1731   GDBusConnection *c;
1732   GDBusObjectManagerServer *manager;
1733   GDBusNodeInfo *info;
1734   GError *error;
1735   GMainLoop *loop;
1736   OMData *om_data;
1737   guint om_signal_id;
1738   GDBusObjectManager *pm;
1739   GList *object_proxies;
1740   GList *proxies;
1741   GDBusObject *op;
1742   GDBusProxy *p;
1743   FooBar *bar_skeleton;
1744   FooBar *bar_p;
1745   FooBar *bar_p2;
1746   FooComAcmeCoyote *coyote_p;
1747   guint old_ref_count;
1748
1749   loop = g_main_loop_new (NULL, FALSE);
1750
1751   om_data = g_new0 (OMData, 1);
1752   om_data->loop = loop;
1753   om_data->state = 0;
1754
1755   error = NULL;
1756   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1757   g_assert_no_error (error);
1758   g_assert (c != NULL);
1759
1760   om_signal_id = g_dbus_connection_signal_subscribe (c,
1761                                                      NULL, /* sender */
1762                                                      "org.freedesktop.DBus.ObjectManager",
1763                                                      NULL, /* member */
1764                                                      NULL, /* object_path */
1765                                                      NULL, /* arg0 */
1766                                                      G_DBUS_SIGNAL_FLAGS_NONE,
1767                                                      om_on_signal,
1768                                                      om_data,
1769                                                      NULL); /* user_data_free_func */
1770
1771   /* Our GDBusObjectManagerClient tests are simple - we basically just count the
1772    * number of times the various signals have been emitted (we don't check
1773    * that the right objects/interfaces are passed though - that's checked
1774    * in the lower-level tests in om_on_signal()...)
1775    *
1776    * Note that these tests rely on the D-Bus signal handlers used by
1777    * GDBusObjectManagerClient firing before om_on_signal().
1778    */
1779   error = NULL;
1780   pm = g_dbus_object_manager_client_new_sync (c,
1781                                               G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1782                                               g_dbus_connection_get_unique_name (c),
1783                                               "/managed",
1784                                               get_proxy_type,
1785                                               GUINT_TO_POINTER (42),
1786                                               NULL, /* GCancellable */
1787                                               &error);
1788   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
1789   g_error_free (error);
1790   g_assert (pm == NULL);
1791
1792   manager = g_dbus_object_manager_server_new (c, "/managed");
1793
1794   /* Check that the manager object is visible */
1795   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
1796   g_assert_cmpint (count_interfaces (info), ==, 4); /* ObjectManager + Properties,Introspectable,Peer */
1797   g_assert (has_interface (info, "org.freedesktop.DBus.ObjectManager"));
1798   g_assert_cmpint (count_nodes (info), ==, 0);
1799   g_dbus_node_info_unref (info);
1800
1801   /* Check GetManagedObjects() - should be empty since we have no objects */
1802   om_check_get_all (c, loop,
1803                     "(@a{oa{sa{sv}}} {},)");
1804
1805   /* Now try to create the the proxy manager again - this time it should work */
1806   error = NULL;
1807   g_dbus_object_manager_client_new (c,
1808                             G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1809                             g_dbus_connection_get_unique_name (c),
1810                             "/managed",
1811                             get_proxy_type,
1812                             GUINT_TO_POINTER (42),
1813                             NULL, /* GCancellable */
1814                             (GAsyncReadyCallback) om_pm_start_cb,
1815                             loop);
1816   g_main_loop_run (loop);
1817   error = NULL;
1818   pm = g_dbus_object_manager_client_new_finish (om_res, &error);
1819   g_object_unref (om_res);
1820   g_assert_no_error (error);
1821   g_assert (pm != NULL);
1822   g_signal_connect (pm,
1823                     "object-added",
1824                     G_CALLBACK (on_object_proxy_added),
1825                     om_data);
1826   g_signal_connect (pm,
1827                     "object-removed",
1828                     G_CALLBACK (on_object_proxy_removed),
1829                     om_data);
1830   /* ... check there are no object proxies yet */
1831   object_proxies = g_dbus_object_manager_get_objects (pm);
1832   g_assert (object_proxies == NULL);
1833
1834   /* First, export an object with a single interface (also check that
1835    * g_dbus_interface_get_object() works and that the object isn't reffed)
1836    */
1837   o = g_dbus_object_skeleton_new ("/managed/first");
1838   i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1839   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1840   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1841   g_dbus_object_skeleton_add_interface (o, i);
1842   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1843   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1844   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1845   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1846   g_dbus_object_skeleton_remove_interface (o, i);
1847   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1848   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1849   g_dbus_object_skeleton_add_interface (o, i);
1850   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1851   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1852   g_dbus_object_manager_server_export (manager, o);
1853
1854   /* ... check we get the InterfacesAdded signal */
1855   om_data->state = 1;
1856   g_main_loop_run (om_data->loop);
1857   g_assert_cmpint (om_data->state, ==, 2);
1858   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 1);
1859   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 0);
1860   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1861   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1862   /* ... check there's one non-standard interfaces */
1863   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1864   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1865   g_assert (has_interface (info, "org.project.Bar"));
1866   g_dbus_node_info_unref (info);
1867
1868   /* Now, check adding the same interface replaces the existing one */
1869   g_dbus_object_skeleton_add_interface (o, i);
1870   /* ... check we get the InterfacesRemoved */
1871   om_data->state = 3;
1872   g_main_loop_run (om_data->loop);
1873   /* ... and then check we get the InterfacesAdded */
1874   g_assert_cmpint (om_data->state, ==, 6);
1875   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 2);
1876   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 1);
1877   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1878   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1879   /* ... check introspection data */
1880   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1881   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1882   g_assert (has_interface (info, "org.project.Bar"));
1883   g_dbus_node_info_unref (info);
1884   g_object_unref (i);
1885
1886   /* check adding an interface of same type (but not same object) replaces the existing one */
1887   i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1888   g_dbus_object_skeleton_add_interface (o, i);
1889   /* ... check we get the InterfacesRemoved and then InterfacesAdded */
1890   om_data->state = 7;
1891   g_main_loop_run (om_data->loop);
1892   g_assert_cmpint (om_data->state, ==, 10);
1893   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1894   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1895   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1896   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1897   /* ... check introspection data */
1898   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1899   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1900   g_assert (has_interface (info, "org.project.Bar"));
1901   g_dbus_node_info_unref (info);
1902   g_object_unref (i);
1903
1904   /* check adding an interface of another type doesn't replace the existing one */
1905   i = G_DBUS_INTERFACE_SKELETON (foo_bat_skeleton_new ());
1906   g_dbus_object_skeleton_add_interface (o, i);
1907   g_object_unref (i);
1908   /* ... check we get the InterfacesAdded */
1909   om_data->state = 11;
1910   g_main_loop_run (om_data->loop);
1911   g_assert_cmpint (om_data->state, ==, 12);
1912   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1913   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1914   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1915   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1916   /* ... check introspection data */
1917   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1918   g_assert_cmpint (count_interfaces (info), ==, 5); /* Bar,Bat + Properties,Introspectable,Peer */
1919   g_assert (has_interface (info, "org.project.Bar"));
1920   g_assert (has_interface (info, "org.project.Bat"));
1921   g_dbus_node_info_unref (info);
1922
1923   /* check we can remove an interface */
1924   g_dbus_object_skeleton_remove_interface_by_name (o, "org.project.Bar");
1925   /* ... check we get the InterfacesRemoved */
1926   om_data->state = 13;
1927   g_main_loop_run (om_data->loop);
1928   g_assert_cmpint (om_data->state, ==, 14);
1929   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1930   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1931   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1932   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1933   /* ... check introspection data */
1934   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1935   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
1936   g_assert (has_interface (info, "org.project.Bat"));
1937   g_dbus_node_info_unref (info);
1938   /* also and that the call only has effect if the interface actually exists
1939    *
1940    * (Note: if a signal was emitted we'd assert in the signal handler
1941    * because we're in state 14)
1942    */
1943   g_dbus_object_skeleton_remove_interface_by_name (o, "org.project.Bar");
1944   /* ... check introspection data */
1945   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1946   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
1947   g_assert (has_interface (info, "org.project.Bat"));
1948   g_dbus_node_info_unref (info);
1949
1950   /* remove the last interface */
1951   g_dbus_object_skeleton_remove_interface_by_name (o, "org.project.Bat");
1952   /* ... check we get the InterfacesRemoved */
1953   om_data->state = 15;
1954   g_main_loop_run (om_data->loop);
1955   g_assert_cmpint (om_data->state, ==, 16);
1956   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1957   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1958   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1959   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1960   /* ... check introspection data */
1961   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1962   g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
1963   g_dbus_node_info_unref (info);
1964
1965   /* and add an interface again */
1966   i = G_DBUS_INTERFACE_SKELETON (foo_com_acme_coyote_skeleton_new ());
1967   g_dbus_object_skeleton_add_interface (o, i);
1968   g_object_unref (i);
1969   /* ... check we get the InterfacesAdded */
1970   om_data->state = 17;
1971   g_main_loop_run (om_data->loop);
1972   g_assert_cmpint (om_data->state, ==, 18);
1973   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 4);
1974   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1975   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1976   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1977   /* ... check introspection data */
1978   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1979   g_assert_cmpint (count_interfaces (info), ==, 4); /* com.acme.Coyote + Properties,Introspectable,Peer */
1980   g_assert (has_interface (info, "com.acme.Coyote"));
1981   g_dbus_node_info_unref (info);
1982
1983   /* Check GetManagedObjects() - should be just the Coyote */
1984   om_check_get_all (c, loop,
1985                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
1986
1987   /* -------------------------------------------------- */
1988
1989   /* create a new object with two interfaces */
1990   o2 = g_dbus_object_skeleton_new ("/managed/second");
1991   i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1992   bar_skeleton = FOO_BAR (i); /* save for later test */
1993   g_dbus_object_skeleton_add_interface (o2, i);
1994   g_object_unref (i);
1995   i = G_DBUS_INTERFACE_SKELETON (foo_bat_skeleton_new ());
1996   g_dbus_object_skeleton_add_interface (o2, i);
1997   g_object_unref (i);
1998   /* ... add it */
1999   g_dbus_object_manager_server_export (manager, o2);
2000   /* ... check we get the InterfacesAdded with _two_ interfaces */
2001   om_data->state = 101;
2002   g_main_loop_run (om_data->loop);
2003   g_assert_cmpint (om_data->state, ==, 102);
2004   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2005   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2006   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2007   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2008
2009   /* -------------------------------------------------- */
2010
2011   /* Now that we have a couple of objects with interfaces, check
2012    * that ObjectManager.GetManagedObjects() works
2013    */
2014   om_check_get_all (c, loop,
2015                     "({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,)>}}},)");
2016
2017   /* check that the _GET_ and _PEEK_ macros work on the server side */
2018   i = FOO_PEEK_BAR (o);
2019   g_assert (i == NULL);
2020   i = FOO_PEEK_COM_ACME_COYOTE (o);
2021   g_assert (i != NULL);
2022   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (i), G_TYPE_DBUS_INTERFACE_SKELETON));
2023   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (i), FOO_TYPE_COM_ACME_COYOTE));
2024   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (i), FOO_TYPE_COM_ACME_COYOTE_SKELETON));
2025   /* ... and that PEEK doesn't increase the ref_count but GET does */
2026   g_assert_cmpint (G_OBJECT (i)->ref_count, ==, 2);
2027   i2 = FOO_PEEK_COM_ACME_COYOTE (o);
2028   g_assert (i == i2);
2029   g_assert_cmpint (G_OBJECT (i)->ref_count, ==, 2);
2030   i2 = FOO_GET_COM_ACME_COYOTE (o);
2031   g_assert (i == i2);
2032   g_assert_cmpint (G_OBJECT (i)->ref_count, ==, 3);
2033   g_object_unref (i);
2034
2035   /* Also check that the ObjectManagerClient returns these objects - and
2036    * that they are of the right GType cf. what we requested via
2037    * our ::get-proxy-type signal handler
2038    */
2039   object_proxies = g_dbus_object_manager_get_objects (pm);
2040   g_assert (g_list_length (object_proxies) == 2);
2041   g_list_foreach (object_proxies, (GFunc) g_object_unref, NULL);
2042   g_list_free (object_proxies);
2043   op = g_dbus_object_manager_get_object (pm, "/managed/first");
2044   g_assert (op != NULL);
2045   g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/first");
2046   proxies = g_dbus_object_get_interfaces (op);
2047   g_assert (g_list_length (proxies) == 1);
2048   g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
2049   g_list_free (proxies);
2050   p = G_DBUS_PROXY (g_dbus_object_get_interface (op, "com.acme.Coyote"));
2051   g_assert (p != NULL);
2052   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, G_TYPE_DBUS_PROXY);
2053   g_assert (!g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_COM_ACME_COYOTE));
2054   g_object_unref (p);
2055   p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2056   g_assert (p == NULL);
2057   g_object_unref (op);
2058   /* -- */
2059   op = g_dbus_object_manager_get_object (pm, "/managed/second");
2060   g_assert (op != NULL);
2061   g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/second");
2062   proxies = g_dbus_object_get_interfaces (op);
2063   g_assert (g_list_length (proxies) == 2);
2064   g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
2065   g_list_free (proxies);
2066   p = G_DBUS_PROXY (g_dbus_object_get_interface (op, "org.project.Bat"));
2067   g_assert (p != NULL);
2068   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_BAT_PROXY);
2069   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_BAT));
2070   g_object_unref (p);
2071   p = G_DBUS_PROXY (g_dbus_object_get_interface (op, "org.project.Bar"));
2072   g_assert (p != NULL);
2073   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_BAR_PROXY);
2074   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_BAR));
2075   /* ... now that we have a Bar instance around, also check that we get signals
2076    *     and property changes...
2077    */
2078   om_check_property_and_signal_emission (loop, bar_skeleton, FOO_BAR (p));
2079   g_object_unref (p);
2080   p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2081   g_assert (p == NULL);
2082   g_object_unref (op);
2083
2084   /* -------------------------------------------------- */
2085
2086   /* check that the _GET_ and _PEEK_ macros work on the proxy side */
2087   op = g_dbus_object_manager_get_object (pm, "/managed/second");
2088   bar_p = FOO_GET_BAR (op);
2089   old_ref_count = G_OBJECT (op)->ref_count;
2090   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (bar_p)) == op);
2091   g_assert_cmpint (old_ref_count, ==, G_OBJECT (op)->ref_count);
2092   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (bar_p), FOO_TYPE_BAR));
2093   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (bar_p), G_TYPE_DBUS_PROXY));
2094   g_assert_cmpint (G_OBJECT (bar_p)->ref_count, ==, 2);
2095   g_object_unref (bar_p);
2096   bar_p2 = FOO_PEEK_BAR (op);
2097   g_assert (bar_p2 == bar_p);
2098   g_assert_cmpint (G_OBJECT (bar_p)->ref_count, ==, 1);
2099   coyote_p = FOO_GET_COM_ACME_COYOTE (op);
2100   g_assert (coyote_p == NULL);
2101   coyote_p = FOO_PEEK_COM_ACME_COYOTE (op);
2102   g_assert (coyote_p == NULL);
2103   g_object_unref (op);
2104
2105   /* Also, check that we warn on stderr in case the get_proxy_type() function is
2106    * wrong etc.
2107    */
2108   op = g_dbus_object_manager_get_object (pm, "/managed/first");
2109   bar_p = FOO_GET_BAR (op);
2110   g_assert (bar_p == NULL);
2111   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
2112     {
2113       coyote_p = FOO_GET_COM_ACME_COYOTE (op);
2114     }
2115   g_test_trap_assert_stderr ("*runtime check failed: (G_TYPE_CHECK_INSTANCE_TYPE (ret, type))*");
2116   g_object_unref (op);
2117
2118   /* -------------------------------------------------- */
2119
2120   /* Now remove the second object added above */
2121   g_dbus_object_manager_server_unexport (manager, "/managed/second");
2122   /* ... check we get InterfacesRemoved with both interfaces */
2123   om_data->state = 103;
2124   g_main_loop_run (om_data->loop);
2125   g_assert_cmpint (om_data->state, ==, 104);
2126   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2127   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 4);
2128   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2129   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2130   /* ... check introspection data (there should be nothing) */
2131   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/second", loop);
2132   g_assert_cmpint (count_nodes (info), ==, 0);
2133   g_assert_cmpint (count_interfaces (info), ==, 0);
2134   g_dbus_node_info_unref (info);
2135
2136   /* Check GetManagedObjects() again */
2137   om_check_get_all (c, loop,
2138                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2139
2140   //g_main_loop_run (loop); /* TODO: tmp */
2141
2142   g_main_loop_unref (loop);
2143
2144   g_dbus_connection_signal_unsubscribe (c, om_signal_id);
2145   g_object_unref (o2);
2146   g_object_unref (o);
2147   g_object_unref (manager);
2148   g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2149                                                          G_CALLBACK (on_object_proxy_added),
2150                                                          om_data), ==, 1);
2151   g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2152                                                          G_CALLBACK (on_object_proxy_removed),
2153                                                          om_data), ==, 1);
2154   g_object_unref (pm);
2155   g_object_unref (c);
2156
2157   g_free (om_data);
2158 }
2159
2160 /* ---------------------------------------------------------------------------------------------------- */
2161
2162 static void
2163 test_object_manager (void)
2164 {
2165   GMainLoop *loop;
2166   guint id;
2167
2168   loop = g_main_loop_new (NULL, FALSE);
2169
2170   id = g_bus_own_name (G_BUS_TYPE_SESSION,
2171                        "org.gtk.GDBus.BindingsTool.Test",
2172                        G_BUS_NAME_OWNER_FLAGS_NONE,
2173                        on_bus_acquired,
2174                        on_name_acquired,
2175                        on_name_lost,
2176                        loop,
2177                        NULL);
2178
2179   g_main_loop_run (loop);
2180
2181   check_object_manager ();
2182
2183   /* uncomment to keep the service around (to e.g. introspect it) */
2184   /* g_main_loop_run (loop); */
2185
2186   g_bus_unown_name (id);
2187   g_main_loop_unref (loop);
2188 }
2189
2190 /* ---------------------------------------------------------------------------------------------------- */
2191 /* This checks that forcing names via org.gtk.GDBus.Name works (see test-codegen.xml) */
2192
2193 extern gpointer name_forcing_1;
2194 extern gpointer name_forcing_2;
2195 extern gpointer name_forcing_3;
2196 extern gpointer name_forcing_4;
2197 extern gpointer name_forcing_5;
2198 extern gpointer name_forcing_6;
2199 extern gpointer name_forcing_7;
2200 gpointer name_forcing_1 = foo_rocket123_get_gtype;
2201 gpointer name_forcing_2 = foo_rocket123_call_ignite_xyz;
2202 gpointer name_forcing_3 = foo_rocket123_emit_exploded_xyz;
2203 gpointer name_forcing_4 = foo_rocket123_get_speed_xyz;
2204 gpointer name_forcing_5 = foo_test_ugly_case_interface_call_get_iscsi_servers;
2205 gpointer name_forcing_6 = foo_test_ugly_case_interface_emit_servers_updated_now;
2206 gpointer name_forcing_7 = foo_test_ugly_case_interface_get_ugly_name;
2207
2208 /* ---------------------------------------------------------------------------------------------------- */
2209
2210 /* See https://bugzilla.gnome.org/show_bug.cgi?id=647577#c5 for details */
2211
2212 #define CHECK_FIELD(name, v1, v2) g_assert_cmpint (G_STRUCT_OFFSET (FooChangingInterface##v1##Iface, name), ==, G_STRUCT_OFFSET (FooChangingInterface##v2##Iface, name));
2213
2214 static void
2215 test_interface_stability (void)
2216 {
2217   CHECK_FIELD(handle_foo_method, V1, V2);
2218   CHECK_FIELD(handle_bar_method, V1, V2);
2219   CHECK_FIELD(handle_baz_method, V1, V2);
2220   CHECK_FIELD(foo_signal, V1, V2);
2221   CHECK_FIELD(bar_signal, V1, V2);
2222   CHECK_FIELD(baz_signal, V1, V2);
2223   CHECK_FIELD(handle_new_method_in2, V2, V10);
2224   CHECK_FIELD(new_signal_in2, V2, V10);
2225 }
2226
2227 #undef CHECK_FIELD
2228
2229 /* ---------------------------------------------------------------------------------------------------- */
2230
2231 int
2232 main (int   argc,
2233       char *argv[])
2234 {
2235   gint ret;
2236
2237   g_type_init ();
2238   g_test_init (&argc, &argv, NULL);
2239
2240   /* all the tests use a session bus with a well-known address that we can bring up and down
2241    * using session_bus_up() and session_bus_down().
2242    */
2243   g_unsetenv ("DISPLAY");
2244   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
2245
2246   session_bus_up ();
2247
2248   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
2249    * until one can connect to the bus but that's not how things work right now
2250    */
2251   usleep (500 * 1000);
2252
2253   g_test_add_func ("/gdbus/codegen/annotations", test_annotations);
2254   g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability);
2255   g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager);
2256
2257   ret = g_test_run();
2258
2259   /* tear down bus */
2260   session_bus_down ();
2261
2262   return ret;
2263 }