8af2ca67d10e9498b8ea3115b1c1a434adf22b56
[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_igen_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 (FooiGenBar             *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_igen_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 (FooiGenBar            *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_igen_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 (FooiGenBar            *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                                     const gchar* const    *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_strjoinv (", ", (gchar **) array_of_objpaths);
169   g_string_append_printf (str, "array_of_objpaths: [%s] ", s);
170   g_free (s);
171   s = g_variant_print (array_of_signatures, 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_igen_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 (FooiGenBar             *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_igen_bar_emit_test_signal (object, 43, a_strv, a_bytestring_array, a_variant); /* consumes a_variant */
194       foo_igen_bar_complete_request_signal_emission (object, invocation);
195     }
196   return TRUE;
197 }
198
199 static gboolean
200 on_handle_request_multi_property_mods (FooiGenBar             *object,
201                                        GDBusMethodInvocation  *invocation,
202                                        gpointer                user_data)
203 {
204   foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
205   foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
206   foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
207   foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
208   g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
209   foo_igen_bar_set_y (object, foo_igen_bar_get_y (object) + 1);
210   foo_igen_bar_set_i (object, foo_igen_bar_get_i (object) + 1);
211   foo_igen_bar_complete_request_multi_property_mods (object, invocation);
212   return TRUE;
213 }
214
215 static gboolean
216 on_handle_property_cancellation (FooiGenBar             *object,
217                                  GDBusMethodInvocation  *invocation,
218                                  gpointer                user_data)
219 {
220   guint n;
221   n = foo_igen_bar_get_n (object);
222   /* This queues up a PropertiesChange event */
223   foo_igen_bar_set_n (object, n + 1);
224   /* this modifies the queued up event */
225   foo_igen_bar_set_n (object, n);
226   /* this flushes all PropertiesChanges event (sends the D-Bus message right
227    * away, if any - there should not be any)
228    */
229   g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
230   /* this makes us return the reply D-Bus method */
231   foo_igen_bar_complete_property_cancellation (object, invocation);
232   return TRUE;
233 }
234
235 /* ---------------------------------------------------------------------------------------------------- */
236
237 static gboolean
238 on_handle_force_method (FooiGenBat             *object,
239                         GDBusMethodInvocation  *invocation,
240                         GVariant               *force_in_i,
241                         GVariant               *force_in_s,
242                         GVariant               *force_in_ay,
243                         GVariant               *force_in_struct,
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_igen_bat_emit_force_signal (object,
270                                   ret_i,
271                                   ret_s,
272                                   ret_ay,
273                                   ret_struct);
274
275   foo_igen_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 (FooiGenAuthorize       *object,
372                                 GDBusMethodInvocation  *invocation,
373                                 gpointer                user_data)
374 {
375   foo_igen_authorize_complete_check_not_authorized (object, invocation);
376   return TRUE;
377 }
378
379 static gboolean
380 on_handle_check_authorized (FooiGenAuthorize       *object,
381                             GDBusMethodInvocation  *invocation,
382                             gpointer                user_data)
383 {
384   foo_igen_authorize_complete_check_authorized (object, invocation);
385   return TRUE;
386 }
387
388 static gboolean
389 on_handle_check_not_authorized_from_object (FooiGenAuthorize       *object,
390                                             GDBusMethodInvocation  *invocation,
391                                             gpointer                user_data)
392 {
393   foo_igen_authorize_complete_check_not_authorized_from_object (object, invocation);
394   return TRUE;
395 }
396
397 /* ---------------------------------------------------------------------------------------------------- */
398
399 static gboolean
400 on_handle_get_self (FooiGenMethodThreads   *object,
401                     GDBusMethodInvocation  *invocation,
402                     gpointer                user_data)
403 {
404   gchar *s;
405   s = g_strdup_printf ("%p", g_thread_self ());
406   foo_igen_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 FooiGenBar *exported_bar_object = NULL;
416 static FooiGenBat *exported_bat_object = NULL;
417 static FooiGenAuthorize *exported_authorize_object = NULL;
418 static GDBusObjectSkeleton *authorize_enclosing_object = NULL;
419 static FooiGenMethodThreads *exported_thread_object_1 = NULL;
420 static FooiGenMethodThreads *exported_thread_object_2 = NULL;
421
422 static void
423 unexport_objects (void)
424 {
425   g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_bar_object));
426   g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_bat_object));
427   g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
428   g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1));
429   g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2));
430 }
431
432 static void
433 on_bus_acquired (GDBusConnection *connection,
434                  const gchar     *name,
435                  gpointer         user_data)
436 {
437   GError *error;
438
439   /* Test that we can export an object using the generated
440    * FooiGenBarSkeleton subclass. Notes:
441    *
442    * 1. We handle methods by simply connecting to the appropriate
443    * GObject signal.
444    *
445    * 2. Property storage is taken care of by the class; we can
446    *    use g_object_get()/g_object_set() (and the generated
447    *    C bindings at will)
448    */
449   error = NULL;
450   exported_bar_object = foo_igen_bar_skeleton_new ();
451   foo_igen_bar_set_ay (exported_bar_object, "ABCabc");
452   foo_igen_bar_set_y (exported_bar_object, 42);
453   foo_igen_bar_set_d (exported_bar_object, 43.0);
454   foo_igen_bar_set_finally_normal_name (exported_bar_object, "There aint no place like home");
455   foo_igen_bar_set_writeonly_property (exported_bar_object, "Mr. Burns");
456
457   /* The following works because it's on the Skeleton object - it will
458    * fail (at run-time) on a Proxy (see on_proxy_appeared() below)
459    */
460   foo_igen_bar_set_readonly_property (exported_bar_object, "blah");
461   g_assert_cmpstr (foo_igen_bar_get_writeonly_property (exported_bar_object), ==, "Mr. Burns");
462
463   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bar_object),
464                                     connection,
465                                     "/bar",
466                                     &error);
467   g_assert_no_error (error);
468   g_signal_connect (exported_bar_object,
469                     "handle-hello-world",
470                     G_CALLBACK (on_handle_hello_world),
471                     NULL);
472   g_signal_connect (exported_bar_object,
473                     "handle-test-primitive-types",
474                     G_CALLBACK (on_handle_test_primitive_types),
475                     NULL);
476   g_signal_connect (exported_bar_object,
477                     "handle-test-non-primitive-types",
478                     G_CALLBACK (on_handle_test_non_primitive_types),
479                     NULL);
480   g_signal_connect (exported_bar_object,
481                     "handle-request-signal-emission",
482                     G_CALLBACK (on_handle_request_signal_emission),
483                     NULL);
484   g_signal_connect (exported_bar_object,
485                     "handle-request-multi-property-mods",
486                     G_CALLBACK (on_handle_request_multi_property_mods),
487                     NULL);
488   g_signal_connect (exported_bar_object,
489                     "handle-property-cancellation",
490                     G_CALLBACK (on_handle_property_cancellation),
491                     NULL);
492
493   exported_bat_object = foo_igen_bat_skeleton_new ();
494   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bat_object),
495                                     connection,
496                                     "/bat",
497                                     &error);
498   g_assert_no_error (error);
499   g_signal_connect (exported_bat_object,
500                     "handle-force-method",
501                     G_CALLBACK (on_handle_force_method),
502                     NULL);
503   g_object_set (exported_bat_object,
504                 "force-i", g_variant_new_int32 (43),
505                 "force-s", g_variant_new_string ("prop string"),
506                 "force-ay", g_variant_new_bytestring ("prop bytestring\xff"),
507                 "force-struct", g_variant_new ("(i)", 4300),
508                 NULL);
509
510   authorize_enclosing_object = g_dbus_object_skeleton_new ("/authorize");
511   g_signal_connect (authorize_enclosing_object,
512                     "authorize-method",
513                     G_CALLBACK (my_object_authorize_method_handler),
514                     NULL);
515   exported_authorize_object = foo_igen_authorize_skeleton_new ();
516   g_dbus_object_skeleton_add_interface (authorize_enclosing_object,
517                                         G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
518   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_authorize_object),
519                                     connection,
520                                     "/authorize",
521                                     &error);
522   g_assert_no_error (error);
523   g_signal_connect (exported_authorize_object,
524                     "g-authorize-method",
525                     G_CALLBACK (my_g_authorize_method_handler),
526                     NULL);
527   g_signal_connect (exported_authorize_object,
528                     "handle-check-not-authorized",
529                     G_CALLBACK (on_handle_check_not_authorized),
530                     NULL);
531   g_signal_connect (exported_authorize_object,
532                     "handle-check-authorized",
533                     G_CALLBACK (on_handle_check_authorized),
534                     NULL);
535   g_signal_connect (exported_authorize_object,
536                     "handle-check-not-authorized-from-object",
537                     G_CALLBACK (on_handle_check_not_authorized_from_object),
538                     NULL);
539
540
541   /* only object 1 has the G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD flag set */
542   exported_thread_object_1 = foo_igen_method_threads_skeleton_new ();
543   g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
544                                        G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
545
546   g_assert (!g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1), connection));
547   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
548                                     connection,
549                                     "/method_threads_1",
550                                     &error);
551   g_assert_no_error (error);
552   g_signal_connect (exported_thread_object_1,
553                     "handle-get-self",
554                     G_CALLBACK (on_handle_get_self),
555                     NULL);
556   g_assert_cmpint (g_dbus_interface_skeleton_get_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1)), ==, G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
557
558   exported_thread_object_2 = foo_igen_method_threads_skeleton_new ();
559   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2),
560                                     connection,
561                                     "/method_threads_2",
562                                     &error);
563   g_assert_no_error (error);
564   g_signal_connect (exported_thread_object_2,
565                     "handle-get-self",
566                     G_CALLBACK (on_handle_get_self),
567                     NULL);
568
569   g_assert_cmpint (g_dbus_interface_skeleton_get_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2)), ==, G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
570
571   method_handler_thread = g_thread_self ();
572 }
573
574 static gpointer check_proxies_in_thread (gpointer user_data);
575
576 static void
577 on_name_acquired (GDBusConnection *connection,
578                   const gchar     *name,
579                   gpointer         user_data)
580 {
581   GMainLoop *loop = user_data;
582
583   g_thread_new ("check-proxies",
584                 check_proxies_in_thread,
585                 loop);
586 }
587
588 static void
589 on_name_lost (GDBusConnection *connection,
590               const gchar     *name,
591               gpointer         user_data)
592 {
593   g_assert_not_reached ();
594 }
595
596 /* ---------------------------------------------------------------------------------------------------- */
597
598 typedef struct
599 {
600   GMainLoop *thread_loop;
601   gint initial_y;
602   gint initial_i;
603   guint num_g_properties_changed;
604   gboolean received_test_signal;
605   guint num_notify_u;
606   guint num_notify_n;
607 } ClientData;
608
609 static void
610 on_notify_u (GObject    *object,
611            GParamSpec *pspec,
612            gpointer    user_data)
613 {
614   ClientData *data = user_data;
615   g_assert_cmpstr (pspec->name, ==, "u");
616   data->num_notify_u += 1;
617 }
618
619 static void
620 on_notify_n (GObject    *object,
621              GParamSpec *pspec,
622              gpointer    user_data)
623 {
624   ClientData *data = user_data;
625   g_assert_cmpstr (pspec->name, ==, "n");
626   data->num_notify_n += 1;
627 }
628
629 static void
630 on_g_properties_changed (GDBusProxy          *_proxy,
631                          GVariant            *changed_properties,
632                          const gchar* const  *invalidated_properties,
633                          gpointer             user_data)
634 {
635   ClientData *data = user_data;
636   FooiGenBar *proxy = FOO_IGEN_BAR (_proxy);
637
638   g_assert_cmpint (g_variant_n_children (changed_properties), ==, 2);
639
640   if (data->num_g_properties_changed == 0)
641     {
642       g_assert_cmpint (data->initial_y, ==, foo_igen_bar_get_y (proxy) - 2);
643       g_assert_cmpint (data->initial_i, ==, foo_igen_bar_get_i (proxy) - 2);
644     }
645   else if (data->num_g_properties_changed == 1)
646     {
647       g_assert_cmpint (data->initial_y, ==, foo_igen_bar_get_y (proxy) - 3);
648       g_assert_cmpint (data->initial_i, ==, foo_igen_bar_get_i (proxy) - 3);
649     }
650   else
651     g_assert_not_reached ();
652
653   data->num_g_properties_changed++;
654
655   if (data->num_g_properties_changed == 2)
656     g_main_loop_quit (data->thread_loop);
657 }
658
659 static void
660 on_test_signal (FooiGenBar          *proxy,
661                 gint                 val_int32,
662                 const gchar* const  *array_of_strings,
663                 const gchar* const  *array_of_bytestrings,
664                 GVariant            *dict_s_to_pairs,
665                 gpointer             user_data)
666 {
667   ClientData *data = user_data;
668
669   g_assert_cmpint (val_int32, ==, 43);
670   g_assert_cmpstr (array_of_strings[0], ==, "foo");
671   g_assert_cmpstr (array_of_strings[1], ==, "bar");
672   g_assert (array_of_strings[2] == NULL);
673   g_assert_cmpstr (array_of_bytestrings[0], ==, "foo\xff");
674   g_assert_cmpstr (array_of_bytestrings[1], ==, "bar\xff");
675   g_assert (array_of_bytestrings[2] == NULL);
676
677   data->received_test_signal = TRUE;
678   g_main_loop_quit (data->thread_loop);
679 }
680
681 static void
682 on_property_cancellation_cb (FooiGenBar    *proxy,
683                              GAsyncResult  *res,
684                              gpointer       user_data)
685 {
686   ClientData *data = user_data;
687   gboolean ret;
688   GError *error = NULL;
689
690   error = NULL;
691   ret = foo_igen_bar_call_property_cancellation_finish (proxy, res, &error);
692   g_assert_no_error (error);
693   g_assert (ret);
694
695   g_main_loop_quit (data->thread_loop);
696 }
697
698 static void
699 check_bar_proxy (FooiGenBar *proxy,
700                  GMainLoop  *thread_loop)
701 {
702   guchar ret_val_byte;
703   gboolean ret_val_boolean;
704   gint16 ret_val_int16;
705   guint16 ret_val_uint16;
706   gint ret_val_int32;
707   guint ret_val_uint32;
708   gint64 ret_val_int64;
709   guint64 ret_val_uint64;
710   gdouble ret_val_double;
711   gchar *ret_val_string;
712   gchar *ret_val_objpath;
713   gchar *ret_val_signature;
714   gchar *ret_val_bytestring;
715   gboolean ret;
716   GError *error;
717   ClientData *data;
718   guchar val_y;
719   gboolean val_b;
720   gint val_n;
721   guint val_q;
722   gint val_i;
723   guint val_u;
724   gint64 val_x;
725   guint64 val_t;
726   gdouble val_d;
727   gchar *val_s;
728   gchar *val_o;
729   gchar *val_g;
730   gchar *val_ay;
731   gchar **val_as;
732   gchar **val_ao;
733   GVariant *val_ag;
734   gint32 val_unset_i;
735   gdouble val_unset_d;
736   gchar *val_unset_s;
737   gchar *val_unset_o;
738   gchar *val_unset_g;
739   gchar *val_unset_ay;
740   gchar **val_unset_as;
741   gchar **val_unset_ao;
742   GVariant *val_unset_ag;
743   GVariant *val_unset_struct;
744   gchar *val_finally_normal_name;
745   GVariant *v;
746   gchar *s;
747
748   data = g_new0 (ClientData, 1);
749   data->thread_loop = thread_loop;
750
751   v = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "y");
752   g_assert (v != NULL);
753   g_variant_unref (v);
754
755   /* set empty values to non-empty */
756   val_unset_i = 42;
757   val_unset_d = 42.0;
758   val_unset_s = "42";
759   val_unset_o = "42";
760   val_unset_g = "42";
761   val_unset_ay = NULL;
762   val_unset_as = NULL;
763   val_unset_ao = NULL;
764   val_unset_ag = NULL;
765   val_unset_struct = NULL;
766   /* check properties */
767   g_object_get (proxy,
768                 "y", &val_y,
769                 "b", &val_b,
770                 "n", &val_n,
771                 "q", &val_q,
772                 "i", &val_i,
773                 "u", &val_u,
774                 "x", &val_x,
775                 "t", &val_t,
776                 "d", &val_d,
777                 "s", &val_s,
778                 "o", &val_o,
779                 "g", &val_g,
780                 "ay", &val_ay,
781                 "as", &val_as,
782                 "ao", &val_ao,
783                 "ag", &val_ag,
784                 "unset_i", &val_unset_i,
785                 "unset_d", &val_unset_d,
786                 "unset_s", &val_unset_s,
787                 "unset_o", &val_unset_o,
788                 "unset_g", &val_unset_g,
789                 "unset_ay", &val_unset_ay,
790                 "unset_as", &val_unset_as,
791                 "unset_ao", &val_unset_ao,
792                 "unset_ag", &val_unset_ag,
793                 "unset_struct", &val_unset_struct,
794                 "finally-normal-name", &val_finally_normal_name,
795                 NULL);
796   g_assert_cmpint (val_y, ==, 42);
797   g_assert_cmpstr (val_finally_normal_name, ==, "There aint no place like home");
798   g_free (val_s);
799   g_free (val_o);
800   g_free (val_g);
801   g_assert_cmpstr (val_ay, ==, "ABCabc");
802   g_free (val_ay);
803   g_strfreev (val_as);
804   g_strfreev (val_ao);
805   g_variant_unref (val_ag);
806   g_free (val_finally_normal_name);
807   /* check empty values */
808   g_assert_cmpint (val_unset_i, ==, 0);
809   g_assert_cmpfloat (val_unset_d, ==, 0.0);
810   g_assert_cmpstr (val_unset_s, ==, "");
811   g_assert_cmpstr (val_unset_o, ==, "/");
812   g_assert_cmpstr (val_unset_g, ==, "");
813   g_free (val_unset_s);
814   g_free (val_unset_o);
815   g_free (val_unset_g);
816   g_assert_cmpstr (val_unset_ay, ==, "");
817   g_assert (val_unset_as[0] == NULL);
818   g_assert (val_unset_ao[0] == NULL);
819   g_assert (g_variant_is_of_type (val_unset_ag, G_VARIANT_TYPE ("ag")));
820   g_assert (g_variant_is_of_type (val_unset_struct, G_VARIANT_TYPE ("(idsogayasaoag)")));
821   s = g_variant_print (val_unset_struct, TRUE);
822   g_assert_cmpstr (s, ==, "(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])");
823   g_free (s);
824   g_free (val_unset_ay);
825   g_strfreev (val_unset_as);
826   g_strfreev (val_unset_ao);
827   g_variant_unref (val_unset_ag);
828   g_variant_unref (val_unset_struct);
829
830   /* Try setting a property. This causes the generated glue to invoke
831    * the org.fd.DBus.Properties.Set() method asynchronously. So we
832    * have to wait for properties-changed...
833    */
834   foo_igen_bar_set_finally_normal_name (proxy, "foo!");
835   _g_assert_property_notify (proxy, "finally-normal-name");
836   g_assert_cmpstr (foo_igen_bar_get_finally_normal_name (proxy), ==, "foo!");
837
838   /* Try setting properties that requires memory management. This
839    * is to exercise the paths that frees the references.
840    */
841   const gchar *array_of_strings[3] = {"one", "two", NULL};
842   const gchar *array_of_strings_2[3] = {"one2", "two2", NULL};
843   const gchar *array_of_objpaths[3] = {"/one", "/one/two", NULL};
844   const gchar *array_of_bytestrings[3] = {"one\xff", "two\xff", NULL};
845
846   g_object_set (proxy,
847                 "s", "a string",
848                 "o", "/a/path",
849                 "g", "asig",
850                 "ay", g_variant_new_parsed ("[byte 0x65, 0x67]"),
851                 "as", array_of_strings,
852                 "ao", array_of_objpaths,
853                 "ag", g_variant_new_parsed ("[@g 'ass', 'git']"),
854                 NULL);
855
856   error = NULL;
857   ret = foo_igen_bar_call_test_primitive_types_sync (proxy,
858                                                      10,
859                                                      TRUE,
860                                                      11,
861                                                      12,
862                                                      13,
863                                                      14,
864                                                      15,
865                                                      16,
866                                                      17,
867                                                      "a string",
868                                                      "/a/path",
869                                                      "asig",
870                                                      "bytestring\xff",
871                                                      &ret_val_byte,
872                                                      &ret_val_boolean,
873                                                      &ret_val_int16,
874                                                      &ret_val_uint16,
875                                                      &ret_val_int32,
876                                                      &ret_val_uint32,
877                                                      &ret_val_int64,
878                                                      &ret_val_uint64,
879                                                      &ret_val_double,
880                                                      &ret_val_string,
881                                                      &ret_val_objpath,
882                                                      &ret_val_signature,
883                                                      &ret_val_bytestring,
884                                                      NULL, /* GCancellable */
885                                                      &error);
886   g_assert_no_error (error);
887   g_assert (ret);
888
889   error = NULL;
890   ret = foo_igen_bar_call_test_non_primitive_types_sync (proxy,
891                                                          g_variant_new_parsed ("{'one': 'red',"
892                                                                                " 'two': 'blue'}"),
893                                                          g_variant_new_parsed ("{'first': (42, 42), "
894                                                                                "'second': (43, 43)}"),
895                                                          g_variant_new_parsed ("(42, 'foo', 'bar')"),
896                                                          array_of_strings,
897                                                          array_of_objpaths,
898                                                          g_variant_new_parsed ("[@g 'ass', 'git']"),
899                                                          array_of_bytestrings,
900                                                          &s,
901                                                          NULL, /* GCancellable */
902                                                          &error);
903
904   g_assert_no_error (error);
905   g_assert (ret);
906
907   /* Check that org.freedesktop.DBus.Error.UnknownMethod is returned on
908    * unimplemented methods.
909    */
910   error = NULL;
911   ret = foo_igen_bar_call_unimplemented_method_sync (proxy, NULL /* GCancellable */, &error);
912   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
913   g_error_free (error);
914   error = NULL;
915   g_assert (!ret);
916
917   g_signal_connect (proxy,
918                     "test-signal",
919                     G_CALLBACK (on_test_signal),
920                     data);
921   error = NULL;
922   ret = foo_igen_bar_call_request_signal_emission_sync (proxy, 0, NULL, &error);
923   g_assert_no_error (error);
924   g_assert (ret);
925
926   g_assert (!data->received_test_signal);
927   g_main_loop_run (thread_loop);
928   g_assert (data->received_test_signal);
929
930   /* Try setting a property. This causes the generated glue to invoke
931    * the org.fd.DBus.Properties.Set() method asynchronously. So we
932    * have to wait for properties-changed...
933    */
934   foo_igen_bar_set_finally_normal_name (proxy, "hey back!");
935   _g_assert_property_notify (proxy, "finally-normal-name");
936   g_assert_cmpstr (foo_igen_bar_get_finally_normal_name (proxy), ==, "hey back!");
937
938   /* Check that multiple calls to a strv getter works... and that
939    * updates on them works as well (See comment for "property vfuncs"
940    * in gio/gdbus-codegen/codegen.py for details)
941    */
942   const gchar *const *read_as;
943   const gchar *const *read_as2;
944   const gchar *const *read_as3;
945   read_as = foo_igen_bar_get_as (proxy);
946   read_as2 = foo_igen_bar_get_as (proxy);
947   g_assert_cmpint (g_strv_length ((gchar **) read_as), ==, 2);
948   g_assert_cmpstr (read_as[0], ==, "one");
949   g_assert_cmpstr (read_as[1], ==, "two");
950   g_assert (read_as == read_as2); /* this is more testing an implementation detail */
951   g_object_set (proxy,
952                 "as", array_of_strings_2,
953                 NULL);
954   _g_assert_property_notify (proxy, "as");
955   read_as3 = foo_igen_bar_get_as (proxy);
956   g_assert_cmpint (g_strv_length ((gchar **) read_as3), ==, 2);
957   g_assert_cmpstr (read_as3[0], ==, "one2");
958   g_assert_cmpstr (read_as3[1], ==, "two2");
959
960   /* Check that grouping changes in idle works.
961    *
962    * See on_handle_request_multi_property_mods(). The server should
963    * emit exactly two PropertiesChanged signals each containing two
964    * properties.
965    *
966    * On the first reception, y and i should both be increased by
967    * two. On the second reception, only by one. The signal handler
968    * checks this.
969    *
970    * This also checks that _drain_notify() works.
971    */
972   data->initial_y = foo_igen_bar_get_y (proxy);
973   data->initial_i = foo_igen_bar_get_i (proxy);
974   g_signal_connect (proxy,
975                     "g-properties-changed",
976                     G_CALLBACK (on_g_properties_changed),
977                     data);
978   error = NULL;
979   ret = foo_igen_bar_call_request_multi_property_mods_sync (proxy, NULL, &error);
980   g_assert_no_error (error);
981   g_assert (ret);
982   g_main_loop_run (thread_loop);
983   g_assert_cmpint (data->num_g_properties_changed, ==, 2);
984   g_signal_handlers_disconnect_by_func (proxy,
985                                         G_CALLBACK (on_g_properties_changed),
986                                         data);
987
988   /* Check that we don't emit PropertiesChanged() if the property
989    * didn't change... we actually get two notifies.. one for the
990    * local set (without a value change) and one when receiving
991    * the PropertiesChanged() signal generated from the remote end.
992    */
993   g_assert_cmpint (data->num_notify_u, ==, 0);
994   g_signal_connect (proxy,
995                     "notify::u",
996                     G_CALLBACK (on_notify_u),
997                     data);
998   foo_igen_bar_set_u (proxy, 1042);
999   g_assert_cmpint (data->num_notify_u, ==, 1);
1000   g_assert_cmpint (foo_igen_bar_get_u (proxy), ==, 0);
1001   _g_assert_property_notify (proxy, "u");
1002   g_assert_cmpint (foo_igen_bar_get_u (proxy), ==, 1042);
1003   g_assert_cmpint (data->num_notify_u, ==, 2);
1004
1005   /* Now change u again to the same value.. this will cause a
1006    * local notify:: notify and the usual Properties.Set() call
1007    *
1008    * (Btw, why also the Set() call if the value in the cache is
1009    * the same? Because someone else might have changed it
1010    * in the mean time and we're just waiting to receive the
1011    * PropertiesChanged() signal...)
1012    *
1013    * More tricky - how do we check for the *absence* of the
1014    * notification that u changed? Simple: we change another
1015    * property and wait for that PropertiesChanged() message
1016    * to arrive.
1017    */
1018   foo_igen_bar_set_u (proxy, 1042);
1019   g_assert_cmpint (data->num_notify_u, ==, 3);
1020
1021   g_assert_cmpint (data->num_notify_n, ==, 0);
1022   g_signal_connect (proxy,
1023                     "notify::n",
1024                     G_CALLBACK (on_notify_n),
1025                     data);
1026   foo_igen_bar_set_n (proxy, 10042);
1027   g_assert_cmpint (data->num_notify_n, ==, 1);
1028   g_assert_cmpint (foo_igen_bar_get_n (proxy), ==, 0);
1029   _g_assert_property_notify (proxy, "n");
1030   g_assert_cmpint (foo_igen_bar_get_n (proxy), ==, 10042);
1031   g_assert_cmpint (data->num_notify_n, ==, 2);
1032   /* Checks that u didn't change at all */
1033   g_assert_cmpint (data->num_notify_u, ==, 3);
1034
1035   /* Now we check that if the service does
1036    *
1037    *   guint n = foo_igen_bar_get_n (foo);
1038    *   foo_igen_bar_set_n (foo, n + 1);
1039    *   foo_igen_bar_set_n (foo, n);
1040    *
1041    *  then no PropertiesChanged() signal is emitted!
1042    */
1043   error = NULL;
1044   foo_igen_bar_call_property_cancellation (proxy,
1045                                            NULL, /* GCancellable */
1046                                            (GAsyncReadyCallback) on_property_cancellation_cb,
1047                                            data);
1048   g_main_loop_run (thread_loop);
1049   /* Checks that n didn't change at all */
1050   g_assert_cmpint (data->num_notify_n, ==, 2);
1051
1052   /* cleanup */
1053   g_free (data);
1054 }
1055
1056 /* ---------------------------------------------------------------------------------------------------- */
1057
1058 static void
1059 on_force_signal (FooiGenBat *proxy,
1060                  GVariant   *force_i,
1061                  GVariant   *force_s,
1062                  GVariant   *force_ay,
1063                  GVariant   *force_struct,
1064                  gpointer    user_data)
1065 {
1066   gboolean *signal_received = user_data;
1067   gint val;
1068
1069   g_assert (!(*signal_received));
1070
1071   g_assert_cmpint (g_variant_get_int32 (force_i), ==, 42 + 10);
1072   g_assert_cmpstr (g_variant_get_string (force_s, NULL), ==, "a string_foo");
1073   g_assert_cmpstr (g_variant_get_bytestring (force_ay), ==, "a bytestring\xff_foo\xff");
1074   g_variant_get (force_struct, "(i)", &val);
1075   g_assert_cmpint (val, ==, 4200 + 10);
1076
1077   *signal_received = TRUE;
1078 }
1079
1080 static void
1081 check_bat_proxy (FooiGenBat *proxy,
1082                  GMainLoop  *thread_loop)
1083 {
1084   GError *error;
1085   GVariant *ret_i;
1086   GVariant *ret_s;
1087   GVariant *ret_ay;
1088   GVariant *ret_struct;
1089   gint val;
1090   gboolean force_signal_received;
1091
1092   /* --------------------------------------------------- */
1093   /* Check type-mapping where we force use of a GVariant */
1094   /* --------------------------------------------------- */
1095
1096   /* check properties */
1097   g_object_get (proxy,
1098                 "force-i", &ret_i,
1099                 "force-s", &ret_s,
1100                 "force-ay", &ret_ay,
1101                 "force-struct", &ret_struct,
1102                 NULL);
1103   g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 43);
1104   g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "prop string");
1105   g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "prop bytestring\xff");
1106   g_variant_get (ret_struct, "(i)", &val);
1107   g_assert_cmpint (val, ==, 4300);
1108   g_variant_unref (ret_i);
1109   g_variant_unref (ret_s);
1110   g_variant_unref (ret_ay);
1111   g_variant_unref (ret_struct);
1112
1113   /* check method and signal */
1114   force_signal_received = FALSE;
1115   g_signal_connect (proxy,
1116                     "force-signal",
1117                     G_CALLBACK (on_force_signal),
1118                     &force_signal_received);
1119
1120   error = NULL;
1121   foo_igen_bat_call_force_method_sync (proxy,
1122                                        g_variant_new_int32 (42),
1123                                        g_variant_new_string ("a string"),
1124                                        g_variant_new_bytestring ("a bytestring\xff"),
1125                                        g_variant_new ("(i)", 4200),
1126                                        &ret_i,
1127                                        &ret_s,
1128                                        &ret_ay,
1129                                        &ret_struct,
1130                                        NULL, /* GCancellable* */
1131                                        &error);
1132   g_assert_no_error (error);
1133   g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 42 + 10);
1134   g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "a string_foo");
1135   g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "a bytestring\xff_foo\xff");
1136   g_variant_get (ret_struct, "(i)", &val);
1137   g_assert_cmpint (val, ==, 4200 + 10);
1138   g_variant_unref (ret_i);
1139   g_variant_unref (ret_s);
1140   g_variant_unref (ret_ay);
1141   g_variant_unref (ret_struct);
1142   _g_assert_signal_received (proxy, "force-signal");
1143   g_assert (force_signal_received);
1144 }
1145
1146 /* ---------------------------------------------------------------------------------------------------- */
1147
1148 static void
1149 check_authorize_proxy (FooiGenAuthorize *proxy,
1150                        GMainLoop        *thread_loop)
1151 {
1152   GError *error;
1153   gboolean ret;
1154
1155   /* Check that g-authorize-method works as intended */
1156
1157   error = NULL;
1158   ret = foo_igen_authorize_call_check_not_authorized_sync (proxy, NULL, &error);
1159   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1160   g_error_free (error);
1161   g_assert (!ret);
1162
1163   error = NULL;
1164   ret = foo_igen_authorize_call_check_authorized_sync (proxy, NULL, &error);
1165   g_assert_no_error (error);
1166   g_assert (ret);
1167
1168   error = NULL;
1169   ret = foo_igen_authorize_call_check_not_authorized_from_object_sync (proxy, NULL, &error);
1170   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
1171   g_error_free (error);
1172   g_assert (!ret);
1173 }
1174
1175 /* ---------------------------------------------------------------------------------------------------- */
1176
1177 static GThread *
1178 get_self_via_proxy (FooiGenMethodThreads *proxy_1)
1179 {
1180   GError *error;
1181   gchar *self_str;
1182   gboolean ret;
1183   gpointer self;
1184
1185   error = NULL;
1186   ret = foo_igen_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
1187   g_assert_no_error (error);
1188   g_assert (ret);
1189
1190   g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
1191
1192   g_free (self_str);
1193
1194   return self;
1195 }
1196
1197 static void
1198 check_thread_proxies (FooiGenMethodThreads *proxy_1,
1199                       FooiGenMethodThreads *proxy_2,
1200                       GMainLoop            *thread_loop)
1201 {
1202   /* proxy_1 is indeed using threads so should never get the handler thread */
1203   g_assert (get_self_via_proxy (proxy_1) != method_handler_thread);
1204
1205   /* proxy_2 is not using threads so should get the handler thread */
1206   g_assert (get_self_via_proxy (proxy_2) == method_handler_thread);
1207 }
1208
1209 /* ---------------------------------------------------------------------------------------------------- */
1210
1211 static gpointer
1212 check_proxies_in_thread (gpointer user_data)
1213 {
1214   GMainLoop *loop = user_data;
1215   GMainContext *thread_context;
1216   GMainLoop *thread_loop;
1217   GError *error;
1218   FooiGenBar *bar_proxy;
1219   FooiGenBat *bat_proxy;
1220   FooiGenAuthorize *authorize_proxy;
1221   FooiGenMethodThreads *thread_proxy_1;
1222   FooiGenMethodThreads *thread_proxy_2;
1223
1224   thread_context = g_main_context_new ();
1225   thread_loop = g_main_loop_new (thread_context, FALSE);
1226   g_main_context_push_thread_default (thread_context);
1227
1228   /* Check the object */
1229   error = NULL;
1230   bar_proxy = foo_igen_bar_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1231                                                    G_DBUS_PROXY_FLAGS_NONE,
1232                                                    "org.gtk.GDBus.BindingsTool.Test",
1233                                                    "/bar",
1234                                                    NULL, /* GCancellable* */
1235                                                    &error);
1236   check_bar_proxy (bar_proxy, thread_loop);
1237   g_assert_no_error (error);
1238   g_object_unref (bar_proxy);
1239
1240   error = NULL;
1241   bat_proxy = foo_igen_bat_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1242                                                    G_DBUS_PROXY_FLAGS_NONE,
1243                                                    "org.gtk.GDBus.BindingsTool.Test",
1244                                                    "/bat",
1245                                                    NULL, /* GCancellable* */
1246                                                    &error);
1247   check_bat_proxy (bat_proxy, thread_loop);
1248   g_assert_no_error (error);
1249   g_object_unref (bat_proxy);
1250
1251   error = NULL;
1252   authorize_proxy = foo_igen_authorize_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1253                                                                G_DBUS_PROXY_FLAGS_NONE,
1254                                                                "org.gtk.GDBus.BindingsTool.Test",
1255                                                                "/authorize",
1256                                                                NULL, /* GCancellable* */
1257                                                                &error);
1258   check_authorize_proxy (authorize_proxy, thread_loop);
1259   g_assert_no_error (error);
1260   g_object_unref (authorize_proxy);
1261
1262   error = NULL;
1263   thread_proxy_1 = foo_igen_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1264                                                                    G_DBUS_PROXY_FLAGS_NONE,
1265                                                                    "org.gtk.GDBus.BindingsTool.Test",
1266                                                                    "/method_threads_1",
1267                                                                    NULL, /* GCancellable* */
1268                                                                    &error);
1269   g_assert_no_error (error);
1270   thread_proxy_2 = foo_igen_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1271                                                                    G_DBUS_PROXY_FLAGS_NONE,
1272                                                                    "org.gtk.GDBus.BindingsTool.Test",
1273                                                                    "/method_threads_2",
1274                                                                    NULL, /* GCancellable* */
1275                                                                    &error);
1276   g_assert_no_error (error);
1277   check_thread_proxies (thread_proxy_1, thread_proxy_2, thread_loop);
1278   g_object_unref (thread_proxy_1);
1279   g_object_unref (thread_proxy_2);
1280
1281   g_main_loop_unref (thread_loop);
1282   g_main_context_unref (thread_context);
1283
1284   /* this breaks out of the loop in main() (below) */
1285   g_main_loop_quit (loop);
1286   return NULL;
1287 }
1288
1289 /* ---------------------------------------------------------------------------------------------------- */
1290
1291 typedef struct
1292 {
1293   gchar *xml;
1294   GMainLoop *loop;
1295 } IntrospectData;
1296
1297 static void
1298 introspect_cb (GDBusConnection   *connection,
1299                GAsyncResult      *res,
1300                gpointer           user_data)
1301 {
1302   IntrospectData *data = user_data;
1303   GVariant *result;
1304   GError *error;
1305
1306   error = NULL;
1307   result = g_dbus_connection_call_finish (connection,
1308                                           res,
1309                                           &error);
1310   g_assert_no_error (error);
1311   g_assert (result != NULL);
1312   g_variant_get (result, "(s)", &data->xml);
1313   g_variant_unref (result);
1314
1315   g_main_loop_quit (data->loop);
1316 }
1317
1318 static GDBusNodeInfo *
1319 introspect (GDBusConnection  *connection,
1320             const gchar      *name,
1321             const gchar      *object_path,
1322             GMainLoop        *loop)
1323 {
1324   GError *error;
1325   GDBusNodeInfo *node_info;
1326   IntrospectData *data;
1327
1328   data = g_new0 (IntrospectData, 1);
1329   data->xml = NULL;
1330   data->loop = loop;
1331
1332   /* do this async to avoid deadlocks */
1333   g_dbus_connection_call (connection,
1334                           name,
1335                           object_path,
1336                           "org.freedesktop.DBus.Introspectable",
1337                           "Introspect",
1338                           NULL, /* params */
1339                           G_VARIANT_TYPE ("(s)"),
1340                           G_DBUS_CALL_FLAGS_NONE,
1341                           -1,
1342                           NULL,
1343                           (GAsyncReadyCallback) introspect_cb,
1344                           data);
1345   g_main_loop_run (loop);
1346   g_assert (data->xml != NULL);
1347
1348   error = NULL;
1349   node_info = g_dbus_node_info_new_for_xml (data->xml, &error);
1350   g_assert_no_error (error);
1351   g_assert (node_info != NULL);
1352   g_free (data->xml);
1353   g_free (data);
1354
1355   return node_info;
1356 }
1357
1358 static guint
1359 count_interfaces (GDBusNodeInfo *info)
1360 {
1361   guint n;
1362   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1363     ;
1364   return n;
1365 }
1366
1367 static guint
1368 count_nodes (GDBusNodeInfo *info)
1369 {
1370   guint n;
1371   for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
1372     ;
1373   return n;
1374 }
1375
1376 static guint
1377 has_interface (GDBusNodeInfo *info,
1378                const gchar   *name)
1379 {
1380   guint n;
1381   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1382     {
1383       if (g_strcmp0 (info->interfaces[n]->name, name) == 0)
1384         return TRUE;
1385     }
1386   return FALSE;
1387 }
1388
1389 /* ---------------------------------------------------------------------------------------------------- */
1390
1391 typedef struct {
1392   GMainLoop *loop;
1393   GVariant *result;
1394 } OMGetManagedObjectsData;
1395
1396 static void
1397 om_get_all_cb (GDBusConnection *connection,
1398                GAsyncResult    *res,
1399                gpointer         user_data)
1400 {
1401   OMGetManagedObjectsData *data = user_data;
1402   GError *error;
1403
1404   error = NULL;
1405   data->result = g_dbus_connection_call_finish (connection,
1406                                                 res,
1407                                                 &error);
1408   g_assert_no_error (error);
1409   g_assert (data->result != NULL);
1410   g_main_loop_quit (data->loop);
1411 }
1412
1413 static void
1414 om_check_get_all (GDBusConnection *c,
1415                   GMainLoop       *loop,
1416                   const gchar     *str)
1417 {
1418   OMGetManagedObjectsData data;
1419   gchar *s;
1420
1421   data.loop = loop;
1422   data.result = NULL;
1423
1424   /* do this async to avoid deadlocks */
1425   g_dbus_connection_call (c,
1426                           g_dbus_connection_get_unique_name (c),
1427                           "/managed",
1428                           "org.freedesktop.DBus.ObjectManager",
1429                           "GetManagedObjects",
1430                           NULL, /* params */
1431                           G_VARIANT_TYPE ("(a{oa{sa{sv}}})"),
1432                           G_DBUS_CALL_FLAGS_NONE,
1433                           -1,
1434                           NULL,
1435                           (GAsyncReadyCallback) om_get_all_cb,
1436                           &data);
1437   g_main_loop_run (loop);
1438   g_assert (data.result != NULL);
1439   s = g_variant_print (data.result, TRUE);
1440   g_assert_cmpstr (s, ==, str);
1441   g_free (s);
1442   g_variant_unref (data.result);
1443 }
1444
1445 typedef struct
1446 {
1447   GMainLoop *loop;
1448   guint state;
1449
1450   guint num_object_proxy_added_signals;
1451   guint num_object_proxy_removed_signals;
1452   guint num_interface_added_signals;
1453   guint num_interface_removed_signals;
1454 } OMData;
1455
1456 static gint
1457 my_pstrcmp (const gchar **a, const gchar **b)
1458 {
1459   return g_strcmp0 (*a, *b);
1460 }
1461
1462 static void
1463 om_check_interfaces_added (const gchar *signal_name,
1464                            GVariant *parameters,
1465                            const gchar *object_path,
1466                            const gchar *first_interface_name,
1467                            ...)
1468 {
1469   const gchar *path;
1470   GVariant *array;
1471   guint n;
1472   GPtrArray *interfaces;
1473   GPtrArray *interfaces_in_message;
1474   va_list var_args;
1475   const gchar *str;
1476
1477   interfaces = g_ptr_array_new ();
1478   g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1479   va_start (var_args, first_interface_name);
1480   do
1481     {
1482       str = va_arg (var_args, const gchar *);
1483       if (str == NULL)
1484         break;
1485       g_ptr_array_add (interfaces, (gpointer) str);
1486     }
1487   while (TRUE);
1488   va_end (var_args);
1489
1490   g_variant_get (parameters, "(&o*)", &path, &array);
1491   g_assert_cmpstr (signal_name, ==, "InterfacesAdded");
1492   g_assert_cmpstr (path, ==, object_path);
1493   g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1494   interfaces_in_message = g_ptr_array_new ();
1495   for (n = 0; n < interfaces->len; n++)
1496     {
1497       const gchar *iface_name;
1498       g_variant_get_child (array, n, "{&sa{sv}}", &iface_name, NULL);
1499       g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1500     }
1501   g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1502   g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1503   g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1504   for (n = 0; n < interfaces->len; n++)
1505     g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1506   g_ptr_array_unref (interfaces_in_message);
1507   g_ptr_array_unref (interfaces);
1508   g_variant_unref (array);
1509 }
1510
1511 static void
1512 om_check_interfaces_removed (const gchar *signal_name,
1513                              GVariant *parameters,
1514                              const gchar *object_path,
1515                              const gchar *first_interface_name,
1516                              ...)
1517 {
1518   const gchar *path;
1519   GVariant *array;
1520   guint n;
1521   GPtrArray *interfaces;
1522   GPtrArray *interfaces_in_message;
1523   va_list var_args;
1524   const gchar *str;
1525
1526   interfaces = g_ptr_array_new ();
1527   g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1528   va_start (var_args, first_interface_name);
1529   do
1530     {
1531       str = va_arg (var_args, const gchar *);
1532       if (str == NULL)
1533         break;
1534       g_ptr_array_add (interfaces, (gpointer) str);
1535     }
1536   while (TRUE);
1537   va_end (var_args);
1538
1539   g_variant_get (parameters, "(&o*)", &path, &array);
1540   g_assert_cmpstr (signal_name, ==, "InterfacesRemoved");
1541   g_assert_cmpstr (path, ==, object_path);
1542   g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1543   interfaces_in_message = g_ptr_array_new ();
1544   for (n = 0; n < interfaces->len; n++)
1545     {
1546       const gchar *iface_name;
1547       g_variant_get_child (array, n, "&s", &iface_name, NULL);
1548       g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1549     }
1550   g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1551   g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1552   g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1553   for (n = 0; n < interfaces->len; n++)
1554     g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1555   g_ptr_array_unref (interfaces_in_message);
1556   g_ptr_array_unref (interfaces);
1557   g_variant_unref (array);
1558 }
1559
1560 static void
1561 om_on_signal (GDBusConnection *connection,
1562               const gchar     *sender_name,
1563               const gchar     *object_path,
1564               const gchar     *interface_name,
1565               const gchar     *signal_name,
1566               GVariant        *parameters,
1567               gpointer         user_data)
1568 {
1569   OMData *om_data = user_data;
1570
1571   //g_debug ("foo: %s", g_variant_print (parameters, TRUE));
1572
1573   switch (om_data->state)
1574     {
1575     default:
1576     case 0:
1577       g_print ("failing and om_data->state=%d on signal %s, params=%s\n",
1578                om_data->state,
1579                signal_name,
1580                g_variant_print (parameters, TRUE));
1581       g_assert_not_reached ();
1582       break;
1583
1584     case 1:
1585       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1586                                  "org.project.Bar", NULL);
1587       om_data->state = 2;
1588       g_main_loop_quit (om_data->loop);
1589       break;
1590
1591     case 3:
1592       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1593                                    "org.project.Bar", NULL);
1594       om_data->state = 5;
1595       /* keep running the loop */
1596       break;
1597
1598     case 5:
1599       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1600                                  "org.project.Bar", NULL);
1601       om_data->state = 6;
1602       g_main_loop_quit (om_data->loop);
1603       break;
1604
1605     case 7:
1606       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1607                                    "org.project.Bar", NULL);
1608       om_data->state = 9;
1609       /* keep running the loop */
1610       break;
1611
1612     case 9:
1613       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1614                                  "org.project.Bar", NULL);
1615       om_data->state = 10;
1616       g_main_loop_quit (om_data->loop);
1617       break;
1618
1619     case 11:
1620       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1621                                  "org.project.Bat", NULL);
1622       om_data->state = 12;
1623       g_main_loop_quit (om_data->loop);
1624       break;
1625
1626     case 13:
1627       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1628                                    "org.project.Bar", NULL);
1629       om_data->state = 14;
1630       g_main_loop_quit (om_data->loop);
1631       break;
1632
1633     case 15:
1634       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1635                                    "org.project.Bat", NULL);
1636       om_data->state = 16;
1637       g_main_loop_quit (om_data->loop);
1638       break;
1639
1640     case 17:
1641       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1642                                  "com.acme.Coyote", NULL);
1643       om_data->state = 18;
1644       g_main_loop_quit (om_data->loop);
1645       break;
1646
1647     case 101:
1648       om_check_interfaces_added (signal_name, parameters, "/managed/second",
1649                                  "org.project.Bat", "org.project.Bar", NULL);
1650       om_data->state = 102;
1651       g_main_loop_quit (om_data->loop);
1652       break;
1653
1654     case 103:
1655       om_check_interfaces_removed (signal_name, parameters, "/managed/second",
1656                                    "org.project.Bat", "org.project.Bar", NULL);
1657       om_data->state = 104;
1658       g_main_loop_quit (om_data->loop);
1659       break;
1660
1661     case 200:
1662       om_check_interfaces_added (signal_name, parameters, "/managed/first_1",
1663                                  "com.acme.Coyote", NULL);
1664       om_data->state = 201;
1665       g_main_loop_quit (om_data->loop);
1666       break;
1667     }
1668 }
1669
1670 static GAsyncResult *om_res = NULL;
1671
1672 static void
1673 om_pm_start_cb (FooiGenObjectManagerClient *manager,
1674                 GAsyncResult               *res,
1675                 gpointer                    user_data)
1676 {
1677   GMainLoop *loop = user_data;
1678   om_res = g_object_ref (res);
1679   g_main_loop_quit (loop);
1680 }
1681
1682 static void
1683 on_interface_added (GDBusObject    *object,
1684                     GDBusInterface *interface,
1685                     gpointer        user_data)
1686 {
1687   OMData *om_data = user_data;
1688   om_data->num_interface_added_signals += 1;
1689 }
1690
1691 static void
1692 on_interface_removed (GDBusObject    *object,
1693                       GDBusInterface *interface,
1694                       gpointer        user_data)
1695 {
1696   OMData *om_data = user_data;
1697   om_data->num_interface_removed_signals += 1;
1698 }
1699
1700 static void
1701 on_object_proxy_added (GDBusObjectManagerClient  *manager,
1702                        GDBusObjectProxy   *object_proxy,
1703                        gpointer            user_data)
1704 {
1705   OMData *om_data = user_data;
1706   om_data->num_object_proxy_added_signals += 1;
1707   g_signal_connect (object_proxy,
1708                     "interface-added",
1709                     G_CALLBACK (on_interface_added),
1710                     om_data);
1711   g_signal_connect (object_proxy,
1712                     "interface-removed",
1713                     G_CALLBACK (on_interface_removed),
1714                     om_data);
1715 }
1716
1717 static void
1718 on_object_proxy_removed (GDBusObjectManagerClient  *manager,
1719                          GDBusObjectProxy   *object_proxy,
1720                          gpointer            user_data)
1721 {
1722   OMData *om_data = user_data;
1723   om_data->num_object_proxy_removed_signals += 1;
1724   g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1725                                                          G_CALLBACK (on_interface_added),
1726                                                          om_data), ==, 1);
1727   g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1728                                                          G_CALLBACK (on_interface_removed),
1729                                                          om_data), ==, 1);
1730 }
1731
1732 static void
1733 property_d_changed (GObject    *object,
1734                     GParamSpec *pspec,
1735                     gpointer    user_data)
1736 {
1737   gboolean *changed = user_data;
1738
1739   *changed = TRUE;
1740 }
1741
1742 static void
1743 om_check_property_and_signal_emission (GMainLoop  *loop,
1744                                        FooiGenBar *skeleton,
1745                                        FooiGenBar *proxy)
1746 {
1747   gboolean d_changed = FALSE;
1748   guint handler;
1749
1750   /* First PropertiesChanged */
1751   g_assert_cmpint (foo_igen_bar_get_i (skeleton), ==, 0);
1752   g_assert_cmpint (foo_igen_bar_get_i (proxy), ==, 0);
1753   foo_igen_bar_set_i (skeleton, 1);
1754   _g_assert_property_notify (proxy, "i");
1755   g_assert_cmpint (foo_igen_bar_get_i (skeleton), ==, 1);
1756   g_assert_cmpint (foo_igen_bar_get_i (proxy), ==, 1);
1757
1758   /* Double-check the gdouble case */
1759   g_assert_cmpfloat (foo_igen_bar_get_d (skeleton), ==, 0.0);
1760   g_assert_cmpfloat (foo_igen_bar_get_d (proxy), ==, 0.0);
1761   foo_igen_bar_set_d (skeleton, 1.0);
1762   _g_assert_property_notify (proxy, "d");
1763
1764   /* Verify that re-setting it to the same value doesn't cause a
1765    * notify on the proxy, by taking advantage of the fact that
1766    * notifications are serialized.
1767    */
1768   handler = g_signal_connect (proxy, "notify::d",
1769                               G_CALLBACK (property_d_changed), &d_changed);
1770   foo_igen_bar_set_d (skeleton, 1.0);
1771   foo_igen_bar_set_i (skeleton, 2);
1772   _g_assert_property_notify (proxy, "i");
1773   g_assert (d_changed == FALSE);
1774   g_signal_handler_disconnect (proxy, handler);
1775
1776   /* Then just a regular signal */
1777   foo_igen_bar_emit_another_signal (skeleton, "word");
1778   _g_assert_signal_received (proxy, "another-signal");
1779 }
1780
1781 static void
1782 check_object_manager (void)
1783 {
1784   FooiGenObjectSkeleton *o = NULL;
1785   FooiGenObjectSkeleton *o2 = NULL;
1786   FooiGenObjectSkeleton *o3 = NULL;
1787   GDBusInterfaceSkeleton *i;
1788   GDBusConnection *c;
1789   GDBusObjectManagerServer *manager = NULL;
1790   GDBusNodeInfo *info;
1791   GError *error;
1792   GMainLoop *loop;
1793   OMData *om_data = NULL;
1794   guint om_signal_id = -1;
1795   GDBusObjectManager *pm = NULL;
1796   GList *object_proxies;
1797   GList *proxies;
1798   GDBusObject *op;
1799   GDBusProxy *p;
1800   FooiGenBar *bar_skeleton;
1801   GDBusInterface *iface;
1802   gchar *path, *name, *name_owner;
1803   GDBusConnection *c2;
1804   GDBusObjectManagerClientFlags flags;
1805
1806   loop = g_main_loop_new (NULL, FALSE);
1807
1808   om_data = g_new0 (OMData, 1);
1809   om_data->loop = loop;
1810   om_data->state = 0;
1811
1812   error = NULL;
1813   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1814   g_assert_no_error (error);
1815   g_assert (c != NULL);
1816
1817   om_signal_id = g_dbus_connection_signal_subscribe (c,
1818                                                      NULL, /* sender */
1819                                                      "org.freedesktop.DBus.ObjectManager",
1820                                                      NULL, /* member */
1821                                                      NULL, /* object_path */
1822                                                      NULL, /* arg0 */
1823                                                      G_DBUS_SIGNAL_FLAGS_NONE,
1824                                                      om_on_signal,
1825                                                      om_data,
1826                                                      NULL); /* user_data_free_func */
1827
1828   /* Our GDBusObjectManagerClient tests are simple - we basically just count the
1829    * number of times the various signals have been emitted (we don't check
1830    * that the right objects/interfaces are passed though - that's checked
1831    * in the lower-level tests in om_on_signal()...)
1832    *
1833    * Note that these tests rely on the D-Bus signal handlers used by
1834    * GDBusObjectManagerClient firing before om_on_signal().
1835    */
1836   error = NULL;
1837   pm = foo_igen_object_manager_client_new_sync (c,
1838                                                 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1839                                                 g_dbus_connection_get_unique_name (c),
1840                                                 "/managed",
1841                                                 NULL, /* GCancellable */
1842                                                 &error);
1843   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
1844   g_error_free (error);
1845   g_assert (pm == NULL);
1846
1847   manager = g_dbus_object_manager_server_new ("/managed");
1848
1849   g_assert (g_dbus_object_manager_server_get_connection (manager) == NULL);
1850
1851   g_dbus_object_manager_server_set_connection (manager, c);
1852
1853   g_assert_cmpstr (g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (manager)), ==, "/managed");
1854   g_object_get (manager, "object-path", &path, "connection", &c2, NULL);
1855   g_assert_cmpstr (path, ==, "/managed");
1856   g_assert (c2 == c);
1857   g_free (path);
1858   g_clear_object (&c2);
1859
1860   /* Check that the manager object is visible */
1861   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
1862   g_assert_cmpint (count_interfaces (info), ==, 4); /* ObjectManager + Properties,Introspectable,Peer */
1863   g_assert (has_interface (info, "org.freedesktop.DBus.ObjectManager"));
1864   g_assert_cmpint (count_nodes (info), ==, 0);
1865   g_dbus_node_info_unref (info);
1866
1867   /* Check GetManagedObjects() - should be empty since we have no objects */
1868   om_check_get_all (c, loop,
1869                     "(@a{oa{sa{sv}}} {},)");
1870
1871   /* Now try to create the proxy manager again - this time it should work */
1872   error = NULL;
1873   foo_igen_object_manager_client_new (c,
1874                                       G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1875                                       g_dbus_connection_get_unique_name (c),
1876                                       "/managed",
1877                                       NULL, /* GCancellable */
1878                                       (GAsyncReadyCallback) om_pm_start_cb,
1879                                       loop);
1880   g_main_loop_run (loop);
1881   error = NULL;
1882   pm = foo_igen_object_manager_client_new_finish (om_res, &error);
1883   g_clear_object (&om_res);
1884   g_assert_no_error (error);
1885   g_assert (pm != NULL);
1886   g_signal_connect (pm,
1887                     "object-added",
1888                     G_CALLBACK (on_object_proxy_added),
1889                     om_data);
1890   g_signal_connect (pm,
1891                     "object-removed",
1892                     G_CALLBACK (on_object_proxy_removed),
1893                     om_data);
1894
1895   g_assert_cmpstr (g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (pm)), ==, "/managed");
1896   g_object_get (pm,
1897                 "object-path", &path,
1898                 "connection", &c2,
1899                 "name", &name,
1900                 "name-owner", &name_owner,
1901                 "flags", &flags,
1902                 NULL);
1903   g_assert_cmpstr (path, ==, "/managed");
1904   g_assert_cmpstr (name, ==, g_dbus_connection_get_unique_name (c));
1905   g_assert_cmpstr (name_owner, ==, g_dbus_connection_get_unique_name (c));
1906   g_assert_cmpint (flags, ==, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE);
1907   g_assert (c2 == c);
1908   g_free (path);
1909   g_clear_object (&c2);
1910   g_free (name);
1911   g_free (name_owner);
1912
1913   /* ... check there are no object proxies yet */
1914   object_proxies = g_dbus_object_manager_get_objects (pm);
1915   g_assert (object_proxies == NULL);
1916
1917   /* First, export an object with a single interface (also check that
1918    * g_dbus_interface_get_object() works and that the object isn't reffed)
1919    */
1920   o = foo_igen_object_skeleton_new ("/managed/first");
1921   i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
1922   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1923   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1924   foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1925   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1926   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1927   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1928   foo_igen_object_skeleton_set_bar (o, NULL);
1929   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1930   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1931   foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1932   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1933   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1934
1935   o2 = FOO_IGEN_OBJECT_SKELETON (g_dbus_interface_dup_object (G_DBUS_INTERFACE (i)));
1936   g_assert (G_DBUS_OBJECT (o2) == G_DBUS_OBJECT (o));
1937   g_assert_cmpint (G_OBJECT (o2)->ref_count, ==, 2);
1938   g_clear_object (&o2);
1939
1940   g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o));
1941
1942   /* ... check we get the InterfacesAdded signal */
1943   om_data->state = 1;
1944
1945   g_main_loop_run (om_data->loop);
1946
1947   g_assert_cmpint (om_data->state, ==, 2);
1948   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 1);
1949   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 0);
1950   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1951   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1952   /* ... check there's one non-standard interfaces */
1953   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1954   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1955   g_assert (has_interface (info, "org.project.Bar"));
1956   g_dbus_node_info_unref (info);
1957
1958   /* Also check g_dbus_object_manager_get_interface */
1959   iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (manager), "/managed/first", "org.project.Bar");
1960   g_assert (iface != NULL);
1961   g_clear_object (&iface);
1962   iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (manager), "/managed/first", "org.project.Bat");
1963   g_assert (iface == NULL);
1964   iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (pm), "/managed/first", "org.project.Bar");
1965   g_assert (iface != NULL);
1966   g_clear_object (&iface);
1967   iface = g_dbus_object_manager_get_interface (G_DBUS_OBJECT_MANAGER (pm), "/managed/first", "org.project.Bat");
1968   g_assert (iface == NULL);
1969
1970   /* Now, check adding the same interface replaces the existing one */
1971   foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1972   /* ... check we get the InterfacesRemoved */
1973   om_data->state = 3;
1974   g_main_loop_run (om_data->loop);
1975   /* ... and then check we get the InterfacesAdded */
1976   g_assert_cmpint (om_data->state, ==, 6);
1977   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 2);
1978   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 1);
1979   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1980   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1981   /* ... check introspection data */
1982   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1983   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1984   g_assert (has_interface (info, "org.project.Bar"));
1985   g_dbus_node_info_unref (info);
1986   g_clear_object (&i);
1987
1988   /* check adding an interface of same type (but not same object) replaces the existing one */
1989   i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
1990   foo_igen_object_skeleton_set_bar (o, FOO_IGEN_BAR (i));
1991   /* ... check we get the InterfacesRemoved and then InterfacesAdded */
1992   om_data->state = 7;
1993   g_main_loop_run (om_data->loop);
1994   g_assert_cmpint (om_data->state, ==, 10);
1995   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1996   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1997   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1998   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1999   /* ... check introspection data */
2000   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2001   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
2002   g_assert (has_interface (info, "org.project.Bar"));
2003   g_dbus_node_info_unref (info);
2004   g_clear_object (&i);
2005
2006   /* check adding an interface of another type doesn't replace the existing one */
2007   i = G_DBUS_INTERFACE_SKELETON (foo_igen_bat_skeleton_new ());
2008   foo_igen_object_skeleton_set_bat (o, FOO_IGEN_BAT (i));
2009   g_clear_object (&i);
2010   /* ... check we get the InterfacesAdded */
2011   om_data->state = 11;
2012   g_main_loop_run (om_data->loop);
2013   g_assert_cmpint (om_data->state, ==, 12);
2014   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2015   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
2016   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2017   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
2018   /* ... check introspection data */
2019   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2020   g_assert_cmpint (count_interfaces (info), ==, 5); /* Bar,Bat + Properties,Introspectable,Peer */
2021   g_assert (has_interface (info, "org.project.Bar"));
2022   g_assert (has_interface (info, "org.project.Bat"));
2023   g_dbus_node_info_unref (info);
2024
2025   /* check we can remove an interface */
2026   foo_igen_object_skeleton_set_bar (o, NULL);
2027   /* ... check we get the InterfacesRemoved */
2028   om_data->state = 13;
2029   g_main_loop_run (om_data->loop);
2030   g_assert_cmpint (om_data->state, ==, 14);
2031   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2032   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
2033   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2034   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2035   /* ... check introspection data */
2036   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2037   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
2038   g_assert (has_interface (info, "org.project.Bat"));
2039   g_dbus_node_info_unref (info);
2040   /* also and that the call only has effect if the interface actually exists
2041    *
2042    * (Note: if a signal was emitted we'd assert in the signal handler
2043    * because we're in state 14)
2044    */
2045   foo_igen_object_skeleton_set_bar (o, NULL);
2046   /* ... check introspection data */
2047   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2048   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
2049   g_assert (has_interface (info, "org.project.Bat"));
2050   g_dbus_node_info_unref (info);
2051
2052   /* remove the last interface */
2053   foo_igen_object_skeleton_set_bat (o, NULL);
2054   /* ... check we get the InterfacesRemoved */
2055   om_data->state = 15;
2056   g_main_loop_run (om_data->loop);
2057   g_assert_cmpint (om_data->state, ==, 16);
2058   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
2059   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2060   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2061   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2062   /* ... check introspection data */
2063   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2064   g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
2065   g_dbus_node_info_unref (info);
2066
2067   /* and add an interface again */
2068   i = G_DBUS_INTERFACE_SKELETON (foo_igen_com_acme_coyote_skeleton_new ());
2069   foo_igen_object_skeleton_set_com_acme_coyote (o, FOO_IGEN_COM_ACME_COYOTE (i));
2070   g_clear_object (&i);
2071   /* ... check we get the InterfacesAdded */
2072   om_data->state = 17;
2073   g_main_loop_run (om_data->loop);
2074   g_assert_cmpint (om_data->state, ==, 18);
2075   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 4);
2076   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2077   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2078   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2079   /* ... check introspection data */
2080   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
2081   g_assert_cmpint (count_interfaces (info), ==, 4); /* com.acme.Coyote + Properties,Introspectable,Peer */
2082   g_assert (has_interface (info, "com.acme.Coyote"));
2083   g_dbus_node_info_unref (info);
2084
2085   /* Check GetManagedObjects() - should be just the Coyote */
2086   om_check_get_all (c, loop,
2087                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2088
2089   /* -------------------------------------------------- */
2090
2091   /* create a new object with two interfaces */
2092   o2 = foo_igen_object_skeleton_new ("/managed/second");
2093   i = G_DBUS_INTERFACE_SKELETON (foo_igen_bar_skeleton_new ());
2094   bar_skeleton = FOO_IGEN_BAR (i); /* save for later test */
2095   foo_igen_object_skeleton_set_bar (o2, FOO_IGEN_BAR (i));
2096   g_clear_object (&i);
2097   i = G_DBUS_INTERFACE_SKELETON (foo_igen_bat_skeleton_new ());
2098   foo_igen_object_skeleton_set_bat (o2, FOO_IGEN_BAT (i));
2099   g_clear_object (&i);
2100   /* ... add it */
2101   g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o2));
2102   /* ... check we get the InterfacesAdded with _two_ interfaces */
2103   om_data->state = 101;
2104   g_main_loop_run (om_data->loop);
2105   g_assert_cmpint (om_data->state, ==, 102);
2106   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2107   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
2108   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2109   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2110
2111   /* -------------------------------------------------- */
2112
2113   /* Now that we have a couple of objects with interfaces, check
2114    * that ObjectManager.GetManagedObjects() works
2115    */
2116   om_check_get_all (c, loop,
2117                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/second': {'org.project.Bar': {'y': <byte 0x00>, 'b': <false>, 'n': <int16 0>, 'q': <uint16 0>, 'i': <0>, 'u': <uint32 0>, 'x': <int64 0>, 't': <uint64 0>, 'd': <0.0>, 's': <''>, 'o': <objectpath '/'>, 'g': <signature ''>, 'ay': <b''>, 'as': <@as []>, 'aay': <@aay []>, 'ao': <@ao []>, 'ag': <@ag []>, 'FinallyNormalName': <''>, 'ReadonlyProperty': <''>, 'unset_i': <0>, 'unset_d': <0.0>, 'unset_s': <''>, 'unset_o': <objectpath '/'>, 'unset_g': <signature ''>, 'unset_ay': <b''>, 'unset_as': <@as []>, 'unset_ao': <@ao []>, 'unset_ag': <@ag []>, 'unset_struct': <(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])>}, 'org.project.Bat': {'force_i': <0>, 'force_s': <''>, 'force_ay': <@ay []>, 'force_struct': <(0,)>}}},)");
2118
2119   /* Set connection to NULL, causing everything to be unexported.. verify this.. and
2120    * then set the connection back.. and then check things still work
2121    */
2122   g_dbus_object_manager_server_set_connection (manager, NULL);
2123   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
2124   g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
2125   g_dbus_node_info_unref (info);
2126
2127   g_dbus_object_manager_server_set_connection (manager, c);
2128   om_check_get_all (c, loop,
2129                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/second': {'org.project.Bar': {'y': <byte 0x00>, 'b': <false>, 'n': <int16 0>, 'q': <uint16 0>, 'i': <0>, 'u': <uint32 0>, 'x': <int64 0>, 't': <uint64 0>, 'd': <0.0>, 's': <''>, 'o': <objectpath '/'>, 'g': <signature ''>, 'ay': <b''>, 'as': <@as []>, 'aay': <@aay []>, 'ao': <@ao []>, 'ag': <@ag []>, 'FinallyNormalName': <''>, 'ReadonlyProperty': <''>, 'unset_i': <0>, 'unset_d': <0.0>, 'unset_s': <''>, 'unset_o': <objectpath '/'>, 'unset_g': <signature ''>, 'unset_ay': <b''>, 'unset_as': <@as []>, 'unset_ao': <@ao []>, 'unset_ag': <@ag []>, 'unset_struct': <(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])>}, 'org.project.Bat': {'force_i': <0>, 'force_s': <''>, 'force_ay': <@ay []>, 'force_struct': <(0,)>}}},)");
2130
2131   /* Also check that the ObjectManagerClient returns these objects - and
2132    * that they are of the right GType cf. what was requested via
2133    * the generated ::get-proxy-type signal handler
2134    */
2135   object_proxies = g_dbus_object_manager_get_objects (pm);
2136   g_assert (g_list_length (object_proxies) == 2);
2137   g_list_free_full (object_proxies, g_object_unref);
2138   op = g_dbus_object_manager_get_object (pm, "/managed/first");
2139   g_assert (op != NULL);
2140   g_assert (FOO_IGEN_IS_OBJECT_PROXY (op));
2141   g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/first");
2142   proxies = g_dbus_object_get_interfaces (op);
2143   g_assert (g_list_length (proxies) == 1);
2144   g_list_free_full (proxies, g_object_unref);
2145   p = G_DBUS_PROXY (foo_igen_object_get_com_acme_coyote (FOO_IGEN_OBJECT (op)));
2146   g_assert (p != NULL);
2147   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_COM_ACME_COYOTE_PROXY);
2148   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_COM_ACME_COYOTE));
2149   g_clear_object (&p);
2150   p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2151   g_assert (p == NULL);
2152   g_clear_object (&op);
2153
2154   /* -- */
2155   op = g_dbus_object_manager_get_object (pm, "/managed/second");
2156   g_assert (op != NULL);
2157   g_assert (FOO_IGEN_IS_OBJECT_PROXY (op));
2158   g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/second");
2159   proxies = g_dbus_object_get_interfaces (op);
2160   g_assert (g_list_length (proxies) == 2);
2161   g_list_free_full (proxies, g_object_unref);
2162   p = G_DBUS_PROXY (foo_igen_object_get_bat (FOO_IGEN_OBJECT (op)));
2163   g_assert (p != NULL);
2164   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_BAT_PROXY);
2165   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_BAT));
2166   g_clear_object (&p);
2167   p = G_DBUS_PROXY (foo_igen_object_get_bar (FOO_IGEN_OBJECT (op)));
2168   g_assert (p != NULL);
2169   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_IGEN_TYPE_BAR_PROXY);
2170   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_IGEN_TYPE_BAR));
2171   /* ... now that we have a Bar instance around, also check that we get signals
2172    *     and property changes...
2173    */
2174   om_check_property_and_signal_emission (loop, bar_skeleton, FOO_IGEN_BAR (p));
2175   g_clear_object (&p);
2176   p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2177   g_assert (p == NULL);
2178   g_clear_object (&op);
2179
2180   /* -------------------------------------------------- */
2181
2182   /* Now remove the second object added above */
2183   g_dbus_object_manager_server_unexport (manager, "/managed/second");
2184   /* ... check we get InterfacesRemoved with both interfaces */
2185   om_data->state = 103;
2186   g_main_loop_run (om_data->loop);
2187   g_assert_cmpint (om_data->state, ==, 104);
2188   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2189   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 4);
2190   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2191   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2192   /* ... check introspection data (there should be nothing) */
2193   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/second", loop);
2194   g_assert_cmpint (count_nodes (info), ==, 0);
2195   g_assert_cmpint (count_interfaces (info), ==, 0);
2196   g_dbus_node_info_unref (info);
2197
2198   /* Check GetManagedObjects() again */
2199   om_check_get_all (c, loop,
2200                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2201   /* -------------------------------------------------- */
2202
2203   /* Check that export_uniquely() works */
2204
2205   o3 = foo_igen_object_skeleton_new ("/managed/first");
2206   i = G_DBUS_INTERFACE_SKELETON (foo_igen_com_acme_coyote_skeleton_new ());
2207   foo_igen_com_acme_coyote_set_mood (FOO_IGEN_COM_ACME_COYOTE (i), "indifferent");
2208   foo_igen_object_skeleton_set_com_acme_coyote (o3, FOO_IGEN_COM_ACME_COYOTE (i));
2209   g_clear_object (&i);
2210   g_dbus_object_manager_server_export_uniquely (manager, G_DBUS_OBJECT_SKELETON (o3));
2211   /* ... check we get the InterfacesAdded signal */
2212   om_data->state = 200;
2213   g_main_loop_run (om_data->loop);
2214   g_assert_cmpint (om_data->state, ==, 201);
2215
2216   om_check_get_all (c, loop,
2217                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/first_1': {'com.acme.Coyote': {'Mood': <'indifferent'>}}},)");
2218
2219   //g_main_loop_run (loop); /* TODO: tmp */
2220
2221   /* Clean up objects */
2222   g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/first_1"));
2223   //g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/second"));
2224   g_assert (g_dbus_object_manager_server_unexport (manager, "/managed/first"));
2225   g_assert_cmpint (g_list_length (g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager))), ==, 0);
2226
2227   if (loop != NULL)
2228     g_main_loop_unref (loop);
2229
2230   if (om_signal_id != -1)
2231     g_dbus_connection_signal_unsubscribe (c, om_signal_id);
2232   g_clear_object (&o3);
2233   g_clear_object (&o2);
2234   g_clear_object (&o);
2235   g_clear_object (&manager);
2236   if (pm != NULL)
2237     {
2238       g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2239                                                              G_CALLBACK (on_object_proxy_added),
2240                                                              om_data), ==, 1);
2241       g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2242                                                              G_CALLBACK (on_object_proxy_removed),
2243                                                              om_data), ==, 1);
2244       g_clear_object (&pm);
2245     }
2246   g_clear_object (&c);
2247
2248   g_free (om_data);
2249 }
2250
2251 /* ---------------------------------------------------------------------------------------------------- */
2252
2253 static void
2254 test_object_manager (void)
2255 {
2256   GMainLoop *loop;
2257   guint id;
2258
2259   loop = g_main_loop_new (NULL, FALSE);
2260
2261   id = g_bus_own_name (G_BUS_TYPE_SESSION,
2262                        "org.gtk.GDBus.BindingsTool.Test",
2263                        G_BUS_NAME_OWNER_FLAGS_NONE,
2264                        on_bus_acquired,
2265                        on_name_acquired,
2266                        on_name_lost,
2267                        loop,
2268                        NULL);
2269
2270   g_main_loop_run (loop);
2271
2272   check_object_manager ();
2273
2274   /* uncomment to keep the service around (to e.g. introspect it) */
2275   /* g_main_loop_run (loop); */
2276
2277   unexport_objects ();
2278
2279   g_bus_unown_name (id);
2280   g_main_loop_unref (loop);
2281 }
2282
2283 /* ---------------------------------------------------------------------------------------------------- */
2284 /* This checks that forcing names via org.gtk.GDBus.Name works (see test-codegen.xml) */
2285
2286 extern gpointer name_forcing_1;
2287 extern gpointer name_forcing_2;
2288 extern gpointer name_forcing_3;
2289 extern gpointer name_forcing_4;
2290 extern gpointer name_forcing_5;
2291 extern gpointer name_forcing_6;
2292 extern gpointer name_forcing_7;
2293 gpointer name_forcing_1 = foo_igen_rocket123_get_type;
2294 gpointer name_forcing_2 = foo_igen_rocket123_call_ignite_xyz;
2295 gpointer name_forcing_3 = foo_igen_rocket123_emit_exploded_xyz;
2296 gpointer name_forcing_4 = foo_igen_rocket123_get_speed_xyz;
2297 gpointer name_forcing_5 = foo_igen_test_ugly_case_interface_call_get_iscsi_servers;
2298 gpointer name_forcing_6 = foo_igen_test_ugly_case_interface_emit_servers_updated_now;
2299 gpointer name_forcing_7 = foo_igen_test_ugly_case_interface_get_ugly_name;
2300
2301 /* ---------------------------------------------------------------------------------------------------- */
2302
2303 /* See https://bugzilla.gnome.org/show_bug.cgi?id=647577#c5 for details */
2304
2305 #define CHECK_FIELD(name, v1, v2) g_assert_cmpint (G_STRUCT_OFFSET (FooiGenChangingInterface##v1##Iface, name), ==, G_STRUCT_OFFSET (FooiGenChangingInterface##v2##Iface, name));
2306
2307 static void
2308 test_interface_stability (void)
2309 {
2310   CHECK_FIELD(handle_foo_method, V1, V2);
2311   CHECK_FIELD(handle_bar_method, V1, V2);
2312   CHECK_FIELD(handle_baz_method, V1, V2);
2313   CHECK_FIELD(foo_signal, V1, V2);
2314   CHECK_FIELD(bar_signal, V1, V2);
2315   CHECK_FIELD(baz_signal, V1, V2);
2316   CHECK_FIELD(handle_new_method_in2, V2, V10);
2317   CHECK_FIELD(new_signal_in2, V2, V10);
2318 }
2319
2320 #undef CHECK_FIELD
2321
2322 /* ---------------------------------------------------------------------------------------------------- */
2323
2324 /* property naming
2325  *
2326  * - check that a property with name "Type" is mapped into g-name "type"
2327  *   with C accessors get_type_ (to avoid clashing with the GType accessor)
2328  *   and set_type_ (for symmetri)
2329  *   (see https://bugzilla.gnome.org/show_bug.cgi?id=679473 for details)
2330  *
2331  * - (could add more tests here)
2332  */
2333
2334 static void
2335 test_property_naming (void)
2336 {
2337   gpointer c_getter_name = foo_igen_naming_get_type_;
2338   gpointer c_setter_name = foo_igen_naming_set_type_;
2339   FooiGenNaming *skel;
2340
2341   (void) c_getter_name;
2342   (void) c_setter_name;
2343
2344   skel = foo_igen_naming_skeleton_new ();
2345   g_assert (g_object_class_find_property (G_OBJECT_GET_CLASS (skel), "type") != NULL);
2346   g_object_unref (skel);
2347 }
2348
2349 /* ---------------------------------------------------------------------------------------------------- */
2350
2351 int
2352 main (int   argc,
2353       char *argv[])
2354 {
2355   gint ret;
2356
2357   g_test_init (&argc, &argv, NULL);
2358
2359   session_bus_up ();
2360
2361   g_test_add_func ("/gdbus/codegen/annotations", test_annotations);
2362   g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability);
2363   g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager);
2364   g_test_add_func ("/gdbus/codegen/property-naming", test_property_naming);
2365
2366   ret = g_test_run();
2367
2368   /* tear down bus */
2369   session_bus_down ();
2370
2371   return ret;
2372 }