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