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