2005-07-11 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / test / glib / test-dbus-glib.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include <dbus/dbus-glib.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "test-service-glib-bindings.h"
7 #include <glib/dbus-gidl.h>
8 #include <glib/dbus-gparser.h>
9 #include <glib.h>
10 #include <glib-object.h>
11 #include "my-object-marshal.h"
12
13 static GMainLoop *loop = NULL;
14 static const char *await_terminating_service = NULL;
15 static int n_times_foo_received = 0;
16 static int n_times_frobnicate_received = 0;
17 static int n_times_frobnicate_received_2 = 0;
18 static int n_times_sig0_received = 0;
19 static int n_times_sig1_received = 0;
20 static int n_times_sig2_received = 0;
21 static guint exit_timeout = 0;
22 static gboolean proxy_destroyed = FALSE;
23 static gboolean proxy_destroy_and_nameowner = FALSE;
24 static gboolean proxy_destroy_and_nameowner_complete = FALSE;
25
26 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
27 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
28
29 static void
30 unset_and_free_gvalue (gpointer val)
31 {
32   g_value_unset (val);
33   g_free (val);
34 }
35
36 static gboolean
37 timed_exit (gpointer loop)
38 {
39   g_print ("timed exit!\n");
40   g_main_loop_quit (loop);
41   return TRUE;
42 }
43
44 static void
45 proxy_destroyed_cb (DBusGProxy *proxy, gpointer user_data)
46 {
47   proxy_destroyed = TRUE;
48   if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && await_terminating_service == NULL)
49     {
50       g_source_remove (exit_timeout);
51       g_main_loop_quit (loop);
52       proxy_destroy_and_nameowner_complete = TRUE;
53     } 
54 }
55
56 static void
57 name_owner_changed (DBusGProxy *proxy,
58                     const char *name,
59                     const char *prev_owner,
60                     const char *new_owner,
61                     gpointer   user_data)
62 {
63   g_print ("(signal NameOwnerChanged) name owner changed for %s from %s to %s\n",
64            name, prev_owner, new_owner);
65   if (await_terminating_service &&
66       !strcmp (name, await_terminating_service)
67       && !strcmp ("", new_owner))
68     {
69       g_print ("Caught expected ownership loss for %s\n", name); 
70       await_terminating_service = NULL;
71       if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && proxy_destroyed)
72         {
73           g_source_remove (exit_timeout);
74           g_main_loop_quit (loop);
75           proxy_destroy_and_nameowner_complete = TRUE;
76         } 
77       else if (!proxy_destroy_and_nameowner)
78         {
79           g_source_remove (exit_timeout);
80           g_main_loop_quit (loop);
81         }
82     }
83 }
84
85 static void
86 foo_signal_handler (DBusGProxy  *proxy,
87                     double       d,
88                     void        *user_data)
89 {
90   n_times_foo_received += 1;
91
92   g_print ("Got Foo signal\n");
93
94   g_main_loop_quit (loop);
95   g_source_remove (exit_timeout);
96 }
97
98 static void
99 frobnicate_signal_handler (DBusGProxy  *proxy,
100                            int          val,
101                            void        *user_data)
102 {
103   n_times_frobnicate_received += 1;
104
105   g_assert (val == 42);
106   g_print ("Got Frobnicate signal\n");
107
108   g_main_loop_quit (loop);
109   g_source_remove (exit_timeout);
110 }
111
112 static void
113 frobnicate_signal_handler_2 (DBusGProxy  *proxy,
114                              int          val,
115                              void        *user_data)
116 {
117   n_times_frobnicate_received_2 += 1;
118
119   g_assert (val == 42);
120   g_print ("Got Frobnicate signal (again)\n");
121 }
122
123 static void
124 sig0_signal_handler (DBusGProxy  *proxy,
125                      const char  *str0,
126                      int          val,
127                      const char  *str1,
128                      void        *user_data)
129 {
130   n_times_sig0_received += 1;
131
132   g_assert (!strcmp (str0, "foo"));
133
134   g_assert (val == 22);
135
136   g_assert (!strcmp (str1, "moo"));
137
138   g_print ("Got Sig0 signal\n");
139
140   g_main_loop_quit (loop);
141   g_source_remove (exit_timeout);
142 }
143
144 static void
145 sig1_signal_handler (DBusGProxy  *proxy,
146                      const char  *str0,
147                      GValue      *value,
148                      void        *user_data)
149 {
150   n_times_sig1_received += 1;
151
152   g_assert (!strcmp (str0, "baz"));
153
154   g_assert (G_VALUE_HOLDS_STRING (value));
155
156   g_assert (!strcmp (g_value_get_string (value), "bar"));
157
158   g_print ("Got Sig1 signal\n");
159
160   g_main_loop_quit (loop);
161   g_source_remove (exit_timeout);
162 }
163
164 static void
165 sig2_signal_handler (DBusGProxy  *proxy,
166                      GHashTable  *table,
167                      void        *user_data)
168 {
169   n_times_sig2_received += 1;
170
171   g_assert (g_hash_table_size (table) == 2);
172
173   g_assert (g_hash_table_lookup (table, "baz") != NULL);
174   g_assert (!strcmp (g_hash_table_lookup (table, "baz"), "cow"));
175   g_assert (g_hash_table_lookup (table, "bar") != NULL);
176   g_assert (!strcmp (g_hash_table_lookup (table, "bar"), "foo"));
177
178   g_print ("Got Sig2 signal\n");
179
180   g_main_loop_quit (loop);
181   g_source_remove (exit_timeout);
182 }
183
184 static DBusGProxyCall *echo_call;
185 static guint n_times_echo_cb_entered;
186 static void
187 echo_received_cb (DBusGProxy *proxy,
188                   DBusGProxyCall *call,
189                   gpointer data)
190 {
191   GError *error;
192   char *echo_data;
193
194   g_assert (call == echo_call);
195   g_assert (data == NULL);
196
197   error = NULL;
198   echo_data = NULL;
199   n_times_echo_cb_entered++;
200
201   if (!dbus_g_proxy_end_call (proxy, call, &error,
202                               G_TYPE_STRING,
203                               &echo_data,
204                               G_TYPE_INVALID))
205     lose_gerror ("Failed to complete async Echo", error);
206   g_assert (echo_data != NULL);
207   g_print ("Async echo gave \"%s\"\n", echo_data); 
208   g_free (echo_data);
209   g_main_loop_quit (loop);
210   g_source_remove (exit_timeout);
211 }
212
213 static void
214 increment_received_cb (DBusGProxy *proxy,
215                        DBusGProxyCall *call,
216                        gpointer data)
217 {
218   GError *error;
219   char *echo_data;
220   guint val;
221
222   g_assert (!strcmp (data, "moo"));
223
224   error = NULL;
225   echo_data = NULL;
226   if (!dbus_g_proxy_end_call (proxy, call, &error,
227                               G_TYPE_UINT, &val,
228                               G_TYPE_INVALID))
229     lose_gerror ("Failed to complete (async) Increment call", error);
230
231   if (val != 43)
232     lose ("Increment call returned %d, should be 43", val);
233   
234   g_print ("Async increment gave \"%d\"\n", val); 
235   g_main_loop_quit (loop);
236   g_source_remove (exit_timeout);
237 }
238
239 static void
240 increment_async_cb (DBusGProxy *proxy, guint val, GError *error, gpointer data)
241 {
242   if (error)
243     lose_gerror ("Failed to complete (wrapped async) Increment call", error);
244
245   if (data != NULL)
246     lose ("(wrapped async) Increment call gave unexpected data");
247   if (val != 43)
248     lose ("(wrapped async) Increment call returned %d, should be 43", val);
249
250   g_print ("(wrapped async) increment gave \"%d\"\n", val); 
251   g_main_loop_quit (loop);
252   g_source_remove (exit_timeout);
253 }
254
255
256 static void
257 lose (const char *str, ...)
258 {
259   va_list args;
260
261   va_start (args, str);
262
263   vfprintf (stderr, str, args);
264   fputc ('\n', stderr);
265
266   va_end (args);
267
268   exit (1);
269 }
270
271 static void
272 lose_gerror (const char *prefix, GError *error) 
273 {
274   if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
275     lose ("%s (%s): %s", prefix, dbus_g_error_get_name (error),
276           error->message);
277   else
278     lose ("%s: %s", prefix, error->message);
279 }
280
281 static void
282 run_mainloop (void)
283 {
284   GMainContext *ctx;
285
286   ctx = g_main_loop_get_context (loop);
287
288   while (g_main_context_pending (ctx))
289     g_main_context_iteration (ctx, FALSE);
290 }
291
292 int
293 main (int argc, char **argv)
294 {
295   DBusGConnection *connection;
296   GError *error;
297   DBusGProxy *driver;
298   DBusGProxy *proxy;
299   DBusGProxy *proxy2;
300   char **name_list;
301   guint name_list_len;
302   guint i;
303   DBusGProxyCall *call;
304   guint32 result;
305   char *v_STRING_2;
306   guint32 v_UINT32_2;
307   double v_DOUBLE_2;
308     
309   g_type_init ();
310
311   g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
312   
313   loop = g_main_loop_new (NULL, FALSE);
314
315   error = NULL;
316   connection = dbus_g_bus_get (DBUS_BUS_SESSION,
317                                &error);
318   if (connection == NULL)
319     lose_gerror ("Failed to open connection to bus", error);
320
321   /* should always get the same one */
322   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
323   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
324   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
325   
326   /* Create a proxy object for the "bus driver" */
327   
328   driver = dbus_g_proxy_new_for_name (connection,
329                                       DBUS_SERVICE_DBUS,
330                                       DBUS_PATH_DBUS,
331                                       DBUS_INTERFACE_DBUS);
332
333   dbus_g_proxy_add_signal (driver,
334                            "NameOwnerChanged",
335                            G_TYPE_STRING,
336                            G_TYPE_STRING,
337                            G_TYPE_STRING,
338                            G_TYPE_INVALID);
339   
340   dbus_g_proxy_connect_signal (driver,
341                                "NameOwnerChanged", 
342                                G_CALLBACK (name_owner_changed),
343                                NULL,
344                                NULL);
345   /* Call ListNames method */
346   
347   error = NULL;
348   if (!dbus_g_proxy_call (driver, "ListNames", &error,
349                           G_TYPE_INVALID,
350                           G_TYPE_STRV, &name_list,
351                           G_TYPE_INVALID))
352     lose_gerror ("Failed to complete ListNames call", error);
353
354   g_print ("Names on the message bus:\n");
355   i = 0;
356   name_list_len = g_strv_length (name_list);
357   while (i < name_list_len)
358     {
359       g_assert (name_list[i] != NULL);
360       g_print ("  %s\n", name_list[i]);
361       ++i;
362     }
363   g_assert (name_list[i] == NULL);
364
365   g_strfreev (name_list);
366
367   g_print ("calling ThisMethodDoesNotExist\n");
368   /* Test handling of unknown method */
369   if (dbus_g_proxy_call (driver, "ThisMethodDoesNotExist", &error,
370                          G_TYPE_STRING,
371                          "blah blah blah blah blah",
372                          G_TYPE_INT,
373                          10,
374                          G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
375     lose ("Calling nonexistent method succeeded!");
376
377   g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
378   g_clear_error (&error);
379
380   run_mainloop ();
381   
382   /* Activate a service */
383   g_print ("Activating echo service\n");
384   if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
385                           G_TYPE_STRING,
386                           "org.freedesktop.DBus.TestSuiteEchoService",
387                           G_TYPE_UINT, 0,
388                           G_TYPE_INVALID,
389                           G_TYPE_UINT, &result,
390                           G_TYPE_INVALID))
391     lose_gerror ("Failed to complete Activate call", error);
392
393   g_print ("Starting echo service result = 0x%x\n", result);
394
395   /* Activate a service again */
396   g_print ("Activating echo service again\n");
397   if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
398                           G_TYPE_STRING,
399                           "org.freedesktop.DBus.TestSuiteEchoService",
400                           G_TYPE_UINT,
401                           0,
402                           G_TYPE_INVALID,
403                           G_TYPE_UINT, &result,
404                           G_TYPE_INVALID))
405     lose_gerror ("Failed to complete Activate call", error);
406
407   g_print ("Duplicate start of echo service = 0x%x\n", result);
408
409   /* Talk to the new service */
410   
411   g_print ("Creating proxy for echo service\n");
412   proxy = dbus_g_proxy_new_for_name_owner (connection,
413                                            "org.freedesktop.DBus.TestSuiteEchoService",
414                                            "/org/freedesktop/TestSuite",
415                                            "org.freedesktop.TestSuite",
416                                            &error);
417   
418   if (proxy == NULL)
419     lose_gerror ("Failed to create proxy for name owner", error);
420
421   run_mainloop ();
422
423   g_print ("Calling Echo\n");
424   if (!dbus_g_proxy_call (proxy, "Echo", &error,
425                           G_TYPE_STRING, "my string hello",
426                           G_TYPE_INVALID,
427                           G_TYPE_STRING, &v_STRING_2,
428                           G_TYPE_INVALID))
429     lose_gerror ("Failed to complete Echo call", error);
430
431   g_print ("String echoed = \"%s\"\n", v_STRING_2);
432   g_free (v_STRING_2);
433
434   g_print ("Calling Echo (async)\n");
435   echo_call = dbus_g_proxy_begin_call (proxy, "Echo",
436                                        echo_received_cb, NULL, NULL,
437                                        G_TYPE_STRING, "my string hello",
438                                        G_TYPE_INVALID);
439   dbus_g_connection_flush (connection);
440   exit_timeout = g_timeout_add (5000, timed_exit, loop);
441   g_main_loop_run (loop);
442
443   /* Test oneway call and signal handling */
444
445   g_print ("Testing Foo emission\n");
446   dbus_g_proxy_add_signal (proxy, "Foo", G_TYPE_DOUBLE, G_TYPE_INVALID);
447   
448   dbus_g_proxy_connect_signal (proxy, "Foo",
449                                G_CALLBACK (foo_signal_handler),
450                                NULL, NULL);
451   
452   dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
453                               G_TYPE_INVALID);
454   
455   dbus_g_connection_flush (connection);
456   exit_timeout = g_timeout_add (5000, timed_exit, loop);
457   g_main_loop_run (loop);
458
459   if (n_times_foo_received != 1)
460     lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
461   
462   /* Activate test servie */ 
463   g_print ("Activating TestSuiteGLibService\n");
464   error = NULL;
465   if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
466                           G_TYPE_STRING,
467                           "org.freedesktop.DBus.TestSuiteGLibService",
468                           G_TYPE_UINT,
469                           0,
470                           G_TYPE_INVALID,
471                           G_TYPE_UINT, &result,
472                           G_TYPE_INVALID)) {
473     lose_gerror ("Failed to complete Activate call", error);
474   }
475
476   g_print ("TestSuiteGLibService activated\n");
477
478   if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION"))
479     g_usleep (8 * G_USEC_PER_SEC);
480
481   g_object_unref (G_OBJECT (proxy));
482
483   run_mainloop ();
484
485   proxy = dbus_g_proxy_new_for_name_owner (connection,
486                                            "org.freedesktop.DBus.TestSuiteGLibService",
487                                            "/org/freedesktop/DBus/Tests/MyTestObject",
488                                            "org.freedesktop.DBus.Tests.MyObject",
489                                            &error);
490   
491   if (proxy == NULL)
492     lose_gerror ("Failed to create proxy for name owner", error);
493
494   g_print ("Calling DoNothing\n");
495   if (!dbus_g_proxy_call (proxy, "DoNothing", &error,
496                           G_TYPE_INVALID, G_TYPE_INVALID))
497     lose_gerror ("Failed to complete DoNothing call", error);
498
499   g_print ("Calling Increment\n");
500   error = NULL;
501   if (!dbus_g_proxy_call (proxy, "Increment", &error,
502                           G_TYPE_UINT, 42,
503                           G_TYPE_INVALID,
504                           G_TYPE_UINT, &v_UINT32_2,
505                           G_TYPE_INVALID))
506     lose_gerror ("Failed to complete Increment call", error);
507   if (v_UINT32_2 != 43)
508     lose ("Increment call returned %d, should be 43", v_UINT32_2);
509
510   v_UINT32_2 = 0;
511   g_print ("Calling Increment (async)\n");
512   call = dbus_g_proxy_begin_call (proxy, "Increment",
513                                   increment_received_cb, g_strdup ("moo"), g_free,
514                                   G_TYPE_UINT, 42,
515                                   G_TYPE_INVALID);
516   dbus_g_connection_flush (connection);
517   exit_timeout = g_timeout_add (5000, timed_exit, loop);
518   g_main_loop_run (loop);
519
520   g_print ("Calling IncrementRetval\n");
521   error = NULL;
522   v_UINT32_2 = 0;
523   if (!dbus_g_proxy_call (proxy, "IncrementRetval", &error,
524                           G_TYPE_UINT, 42,
525                           G_TYPE_INVALID,
526                           G_TYPE_UINT, &v_UINT32_2,
527                           G_TYPE_INVALID))
528     lose_gerror ("Failed to complete Increment call", error);
529   if (v_UINT32_2 != 43)
530     lose ("IncrementRetval call returned %d, should be 43", v_UINT32_2);
531
532   g_print ("Calling IncrementRetvalError\n");
533   error = NULL;
534   v_UINT32_2 = 0;
535   if (!dbus_g_proxy_call (proxy, "IncrementRetvalError", &error,
536                           G_TYPE_UINT, 5,
537                           G_TYPE_INVALID,
538                           G_TYPE_UINT, &v_UINT32_2,
539                           G_TYPE_INVALID))
540     lose_gerror ("Failed to complete Increment call", error);
541   if (v_UINT32_2 != 6)
542     lose ("IncrementRetval call returned %d, should be 6", v_UINT32_2);
543
544   g_print ("Calling ThrowError\n");
545   if (dbus_g_proxy_call (proxy, "ThrowError", &error,
546                          G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
547     lose ("ThrowError call unexpectedly succeeded!");
548
549   if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
550     lose ("ThrowError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error),
551           error->message);
552
553   g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
554   g_clear_error (&error);
555
556   g_print ("Calling IncrementRetvalError (for error)\n");
557   error = NULL;
558   v_UINT32_2 = 0;
559   if (dbus_g_proxy_call (proxy, "IncrementRetvalError", &error,
560                          G_TYPE_UINT, 20,
561                          G_TYPE_INVALID,
562                          G_TYPE_UINT, &v_UINT32_2,
563                          G_TYPE_INVALID) != FALSE)
564     lose ("IncrementRetvalError call unexpectedly succeeded!");
565   if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
566     lose ("IncrementRetvalError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error), error->message);
567   g_clear_error (&error);
568
569   error = NULL;
570   g_print ("Calling Uppercase\n");
571   if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
572                           G_TYPE_STRING, "foobar",
573                           G_TYPE_INVALID,
574                           G_TYPE_STRING, &v_STRING_2,
575                           G_TYPE_INVALID))
576     lose_gerror ("Failed to complete Uppercase call", error);
577   if (strcmp ("FOOBAR", v_STRING_2) != 0)
578     lose ("Uppercase call returned unexpected string %s", v_STRING_2);
579   g_free (v_STRING_2);
580
581   run_mainloop ();
582
583   g_print ("Calling ManyArgs\n");
584   if (!dbus_g_proxy_call (proxy, "ManyArgs", &error,
585                           G_TYPE_UINT, 26,
586                           G_TYPE_STRING, "bazwhee",
587                           G_TYPE_DOUBLE, G_PI,
588                           G_TYPE_INVALID,
589                           G_TYPE_DOUBLE, &v_DOUBLE_2,
590                           G_TYPE_STRING, &v_STRING_2,
591                           G_TYPE_INVALID))
592     lose_gerror ("Failed to complete ManyArgs call", error);
593   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
594     lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
595   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
596     lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
597   g_free (v_STRING_2);
598
599   g_print ("Calling (wrapped) do_nothing\n");
600   if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
601     lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
602
603   g_print ("Calling (wrapped) increment\n");
604   if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
605     lose_gerror ("Failed to complete (wrapped) Increment call", error);
606
607   if (v_UINT32_2 != 43)
608     lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
609
610   g_print ("Calling (wrapped async) increment\n");
611   if (!org_freedesktop_DBus_Tests_MyObject_increment_async (proxy, 42, increment_async_cb, NULL))
612     lose_gerror ("Failed to complete (wrapped) Increment call", error);
613   dbus_g_connection_flush (connection);
614   exit_timeout = g_timeout_add (5000, timed_exit, loop);
615   g_main_loop_run (loop);
616
617   v_UINT32_2 = 0;
618   if (!org_freedesktop_DBus_Tests_MyObject_async_increment (proxy, 42, &v_UINT32_2, &error))
619     lose_gerror ("Failed to complete (wrapped) AsyncIncrement call", error);
620
621   if (v_UINT32_2 != 43)
622     lose ("(wrapped) async increment call returned %d, should be 43", v_UINT32_2);
623
624   g_print ("Calling (wrapped) throw_error\n");
625   if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
626     lose ("(wrapped) ThrowError call unexpectedly succeeded!");
627
628   g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
629   g_clear_error (&error);
630
631   if (org_freedesktop_DBus_Tests_MyObject_async_throw_error (proxy, &error) != FALSE)
632     lose ("(wrapped) AsyncThrowError call unexpectedly succeeded!");
633
634   g_print ("(wrapped) AsyncThrowError failed (as expected) returned error: %s\n", error->message);
635   g_clear_error (&error);
636
637   g_print ("Calling (wrapped) uppercase\n");
638   if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error)) 
639     lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
640   if (strcmp ("FOOBAR", v_STRING_2) != 0)
641     lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
642   g_free (v_STRING_2);
643
644   g_print ("Calling (wrapped) many_args\n");
645   if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
646                                                       &v_DOUBLE_2, &v_STRING_2, &error))
647     lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
648
649   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
650     
651     lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
652
653   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
654     lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
655   g_free (v_STRING_2);
656
657   {
658     guint32 arg0;
659     char *arg1;
660     gint32 arg2;
661     guint32 arg3;
662     guint32 arg4;
663     char *arg5;
664     
665     g_print ("Calling (wrapped) many_return\n");
666     if (!org_freedesktop_DBus_Tests_MyObject_many_return (proxy, &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &error))
667       lose_gerror ("Failed to complete (wrapped) ManyReturn call", error);
668
669     if (arg0 != 42)
670       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg0);
671
672     if (strcmp ("42", arg1) != 0)
673       lose ("(wrapped) ManyReturn call returned unexpected string %s", arg1);
674     g_free (arg1);
675
676     if (arg2 != -67)
677       lose ("(wrapped) ManyReturn call returned unexpected gint32 value %u", arg2);
678
679     if (arg3 != 2)
680       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg3);
681
682     if (arg4 != 26)
683       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg4);
684
685     if (strcmp ("hello world", arg5))
686       lose ("(wrapped) ManyReturn call returned unexpected string %s", arg5);
687     g_free (arg5);
688   }
689
690   run_mainloop ();
691
692   {
693     GValue value = {0, };
694
695     g_value_init (&value, G_TYPE_STRING);
696     g_value_set_string (&value, "foo");
697
698     g_print ("Calling (wrapped) stringify, with string\n");
699     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
700                                                         &value,
701                                                         &v_STRING_2,
702                                                         &error))
703       lose_gerror ("Failed to complete (wrapped) stringify call", error);
704     if (strcmp ("foo", v_STRING_2) != 0)
705       lose ("(wrapped) stringify call returned unexpected string %s", v_STRING_2);
706     g_free (v_STRING_2);
707
708     g_value_unset (&value);
709     g_value_init (&value, G_TYPE_INT);
710     g_value_set_int (&value, 42);
711
712     g_print ("Calling (wrapped) stringify, with int\n");
713     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
714                                                         &value,
715                                                         &v_STRING_2,
716                                                         &error))
717       lose_gerror ("Failed to complete (wrapped) stringify call 2", error);
718     if (strcmp ("42", v_STRING_2) != 0)
719       lose ("(wrapped) stringify call 2 returned unexpected string %s", v_STRING_2);
720     g_value_unset (&value);
721     g_free (v_STRING_2);
722
723     g_value_init (&value, G_TYPE_INT);
724     g_value_set_int (&value, 88);
725     g_print ("Calling (wrapped) stringify, with another int\n");
726     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
727                                                         &value,
728                                                         NULL,
729                                                         &error))
730       lose_gerror ("Failed to complete (wrapped) stringify call 3", error);
731     g_value_unset (&value);
732
733     g_print ("Calling (wrapped) unstringify, for string\n");
734     if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
735                                                           "foo",
736                                                           &value,
737                                                           &error))
738       lose_gerror ("Failed to complete (wrapped) unstringify call", error);
739     if (!G_VALUE_HOLDS_STRING (&value))
740       lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
741     if (strcmp (g_value_get_string (&value), "foo"))
742       lose ("(wrapped) unstringify call returned unexpected string %s",
743             g_value_get_string (&value));
744         
745     g_value_unset (&value);
746
747     g_print ("Calling (wrapped) unstringify, for int\n");
748     if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
749                                                           "10",
750                                                           &value,
751                                                           &error))
752       lose_gerror ("Failed to complete (wrapped) unstringify call", error);
753     if (!G_VALUE_HOLDS_INT (&value))
754       lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
755     if (g_value_get_int (&value) != 10)
756       lose ("(wrapped) unstringify call returned unexpected integer %d",
757             g_value_get_int (&value));
758
759     g_value_unset (&value);
760   }
761
762   run_mainloop ();
763
764   {
765     GArray *array;
766     guint32 val;
767     guint32 arraylen;
768
769     array = g_array_new (FALSE, TRUE, sizeof (guint32));
770     val = 42;
771     g_array_append_val (array, val);
772     val = 69;
773     g_array_append_val (array, val);
774     val = 88;
775     g_array_append_val (array, val);
776     val = 26;
777     g_array_append_val (array, val);
778     val = 2;
779     g_array_append_val (array, val);
780
781     arraylen = 0;
782     g_print ("Calling (wrapped) recursive1\n");
783     if (!org_freedesktop_DBus_Tests_MyObject_recursive1 (proxy, array,
784                                                          &arraylen, &error))
785       lose_gerror ("Failed to complete (wrapped) recursive1 call", error);
786     if (arraylen != 5)
787       lose ("(wrapped) recursive1 call returned invalid length %u", arraylen);
788   }
789
790   {
791     GArray *array = NULL;
792     guint32 *arrayvals;
793     
794     g_print ("Calling (wrapped) recursive2\n");
795     if (!org_freedesktop_DBus_Tests_MyObject_recursive2 (proxy, 2, &array, &error))
796       lose_gerror ("Failed to complete (wrapped) Recursive2 call", error);
797
798     if (array == NULL)
799       lose ("(wrapped) Recursive2 call returned NULL");
800     if (array->len != 5)
801       lose ("(wrapped) Recursive2 call returned unexpected array length %u", array->len);
802
803     arrayvals = (guint32*) array->data;
804     if (arrayvals[0] != 42)
805       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 0", arrayvals[0]);
806     if (arrayvals[1] != 26)
807       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 1", arrayvals[1]);
808     if (arrayvals[4] != 2)
809       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 4", arrayvals[4]);
810
811     g_array_free (array, TRUE);
812   }
813
814   run_mainloop ();
815
816   {
817     char **strs;
818     char **strs_ret;
819
820     strs = g_new0 (char *, 4);
821     strs[0] = "hello";
822     strs[1] = "HellO";
823     strs[2] = "HELLO";
824     strs[3] = NULL;
825
826     strs_ret = NULL;
827     g_print ("Calling (wrapped) many_uppercase\n");
828     if (!org_freedesktop_DBus_Tests_MyObject_many_uppercase (proxy, strs, &strs_ret, &error)) 
829       lose_gerror ("Failed to complete (wrapped) ManyUppercase call", error);
830     g_assert (strs_ret != NULL);
831     if (strcmp ("HELLO", strs_ret[0]) != 0)
832       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[0]);
833     if (strcmp ("HELLO", strs_ret[1]) != 0)
834       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[1]);
835     if (strcmp ("HELLO", strs_ret[2]) != 0)
836       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[2]);
837
838     g_free (strs);
839     g_strfreev (strs_ret);
840   }
841
842   {
843     GHashTable *table;
844     guint len;
845
846     table = g_hash_table_new (g_str_hash, g_str_equal);
847     g_hash_table_insert (table, "moooo", "b");
848     g_hash_table_insert (table, "xxx", "cow!");
849
850     len = 0;
851     g_print ("Calling (wrapped) str_hash_len\n");
852     if (!org_freedesktop_DBus_Tests_MyObject_str_hash_len (proxy, table, &len, &error))
853       lose_gerror ("(wrapped) StrHashLen call failed", error);
854     if (len != 13) 
855       lose ("(wrapped) StrHashLen returned unexpected length %u", len);
856     g_hash_table_destroy (table);
857   }
858
859   {
860     GHashTable *table;
861     const char *val;
862
863     g_print ("Calling (wrapped) get_hash\n");
864     if (!org_freedesktop_DBus_Tests_MyObject_get_hash (proxy, &table, &error))
865       lose_gerror ("(wrapped) GetHash call failed", error);
866     val = g_hash_table_lookup (table, "foo");
867     if (val == NULL || strcmp ("bar", val))
868       lose ("(wrapped) StrHashLen returned invalid value %s for key \"foo\"",
869             val ? val : "(null)");
870     val = g_hash_table_lookup (table, "baz");
871     if (val == NULL || strcmp ("whee", val))
872       lose ("(wrapped) StrHashLen returned invalid value %s for key \"whee\"",
873             val ? val : "(null)");
874     val = g_hash_table_lookup (table, "cow");
875     if (val == NULL || strcmp ("crack", val))
876       lose ("(wrapped) StrHashLen returned invalid value %s for key \"cow\"",
877             val ? val : "(null)");
878     if (g_hash_table_size (table) != 3)
879       lose ("(wrapped) StrHashLen returned unexpected hash size %u",
880             g_hash_table_size (table));
881
882     g_hash_table_destroy (table);
883   }
884
885   run_mainloop ();
886
887   {
888     GValueArray *vals;
889     GValueArray *vals_ret;
890     GValue *val;
891
892     vals = g_value_array_new (3);
893
894     g_value_array_append (vals, NULL);
895     g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_STRING);
896     g_value_set_string (g_value_array_get_nth (vals, 0), "foo");
897
898     g_value_array_append (vals, NULL);
899     g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_UINT);
900     g_value_set_uint (g_value_array_get_nth (vals, vals->n_values - 1), 42);
901
902     g_value_array_append (vals, NULL);
903     g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_VALUE);
904     val = g_new0 (GValue, 1);
905     g_value_init (val, G_TYPE_UCHAR);
906     g_value_set_uchar (val, '!');
907     g_value_set_boxed (g_value_array_get_nth (vals, vals->n_values - 1), val);
908
909     vals_ret = NULL;
910     g_print ("Calling SendCar\n");
911     if (!dbus_g_proxy_call (proxy, "SendCar", &error,
912                             G_TYPE_VALUE_ARRAY, vals,
913                             G_TYPE_INVALID,
914                             G_TYPE_VALUE_ARRAY, &vals_ret,
915                             G_TYPE_INVALID))
916       lose_gerror ("Failed to complete SendCar call", error);
917
918     g_assert (vals_ret != NULL);
919     g_assert (vals_ret->n_values == 2);
920
921     g_assert (G_VALUE_HOLDS_UINT (g_value_array_get_nth (vals_ret, 0)));
922     g_assert (g_value_get_uint (g_value_array_get_nth (vals_ret, 0)) == 43);
923     
924     g_assert (G_VALUE_TYPE (g_value_array_get_nth (vals_ret, 1)) == DBUS_TYPE_G_OBJECT_PATH);
925     g_assert (!strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
926                        g_value_get_boxed (g_value_array_get_nth (vals_ret, 1))));
927
928     g_value_array_free (vals);
929     g_value_array_free (vals_ret);
930   }
931
932   {
933     GValue *val;
934     GHashTable *table;
935     GHashTable *ret_table;
936
937     table = g_hash_table_new_full (g_str_hash, g_str_equal,
938                                    g_free, unset_and_free_gvalue);
939     
940     val = g_new0 (GValue, 1);
941     g_value_init (val, G_TYPE_UINT);
942     g_value_set_uint (val, 42);
943     g_hash_table_insert (table, g_strdup ("foo"), val);
944
945     val = g_new0 (GValue, 1);
946     g_value_init (val, G_TYPE_STRING);
947     g_value_set_string (val, "hello");
948     g_hash_table_insert (table, g_strdup ("bar"), val);
949
950     ret_table = NULL;
951     g_print ("Calling ManyStringify\n");
952     if (!dbus_g_proxy_call (proxy, "ManyStringify", &error,
953                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), table,
954                             G_TYPE_INVALID,
955                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &ret_table,
956                             G_TYPE_INVALID))
957       lose_gerror ("Failed to complete ManyStringify call", error);
958
959     g_assert (ret_table != NULL);
960     g_assert (g_hash_table_size (ret_table) == 2);
961
962     val = g_hash_table_lookup (ret_table, "foo");
963     g_assert (val != NULL);
964     g_assert (G_VALUE_HOLDS_STRING (val));
965     g_assert (!strcmp ("42", g_value_get_string (val)));
966
967     val = g_hash_table_lookup (ret_table, "bar");
968     g_assert (val != NULL);
969     g_assert (G_VALUE_HOLDS_STRING (val));
970     g_assert (!strcmp ("hello", g_value_get_string (val)));
971
972     g_hash_table_destroy (table);
973     g_hash_table_destroy (ret_table);
974   }
975
976   {
977     guint val;
978     char *ret_path;
979     DBusGProxy *ret_proxy;
980
981     g_print ("Calling (wrapped) objpath\n");
982     if (!dbus_g_proxy_call (proxy, "Objpath", &error,
983                             DBUS_TYPE_G_PROXY, proxy, G_TYPE_INVALID,
984                             DBUS_TYPE_G_PROXY, &ret_proxy, G_TYPE_INVALID))
985       lose_gerror ("Failed to complete Objpath call", error);
986     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
987                 dbus_g_proxy_get_path (ret_proxy)) != 0)
988       lose ("(wrapped) objpath call returned unexpected proxy %s",
989             dbus_g_proxy_get_path (ret_proxy));
990
991     g_print ("Doing get/increment val tests\n");
992     val = 1;
993     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
994       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
995     if (val != 0)
996       lose ("(wrapped) GetVal returned invalid value %d", val);
997
998     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
999       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1000
1001     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
1002       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1003
1004     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
1005       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1006
1007     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
1008       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1009     if (val != 3)
1010       lose ("(wrapped) GetVal returned invalid value %d", val);
1011
1012     if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
1013       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1014     if (val != 0)
1015       lose ("(wrapped) GetVal returned invalid value %d", val);
1016
1017     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (proxy, &error))
1018       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1019
1020     if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
1021       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1022     if (val != 1)
1023       lose ("(wrapped) GetVal returned invalid value %d", val);
1024
1025     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
1026       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1027     if (val != 3)
1028       lose ("(wrapped) GetVal returned invalid value %d", val);
1029
1030     g_object_unref (G_OBJECT (ret_proxy));
1031
1032     g_print ("Calling objpath again\n");
1033     ret_proxy = NULL;
1034
1035     if (!dbus_g_proxy_call (proxy, "Objpath", &error,
1036                             DBUS_TYPE_G_OBJECT_PATH,
1037                             dbus_g_proxy_get_path (proxy),
1038                             G_TYPE_INVALID,
1039                             DBUS_TYPE_G_OBJECT_PATH,
1040                             &ret_path,
1041                             G_TYPE_INVALID))
1042       lose_gerror ("Failed to complete Objpath call 2", error);
1043     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2", ret_path) != 0)
1044       lose ("Objpath call 2 returned unexpected path %s",
1045             ret_path);
1046
1047     ret_proxy = dbus_g_proxy_new_for_name_owner (connection,
1048                                                  "org.freedesktop.DBus.TestSuiteGLibService",
1049                                                  ret_path,
1050                                                  "org.freedesktop.DBus.Tests.FooObject",
1051                                                  &error);
1052     g_free (ret_path);
1053     
1054     val = 0;
1055     if (!org_freedesktop_DBus_Tests_FooObject_get_value (ret_proxy, &val, &error))
1056       lose_gerror ("Failed to complete (wrapped) GetValue call", error);
1057     if (val != 3)
1058       lose ("(wrapped) GetValue returned invalid value %d", val);
1059   }
1060
1061   run_mainloop ();
1062
1063   {
1064     GPtrArray *objs;
1065     guint i;
1066
1067     g_print ("Calling GetObjs\n");
1068
1069     if (!dbus_g_proxy_call (proxy, "GetObjs", &error, G_TYPE_INVALID,
1070                             dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH),
1071                             &objs,
1072                             G_TYPE_INVALID))
1073       lose_gerror ("Failed to complete GetObjs call", error);
1074     if (objs->len != 2)
1075       lose ("GetObjs call returned unexpected number of objects %d, expected 2",
1076             objs->len);
1077
1078     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject",
1079                 g_ptr_array_index (objs, 0)) != 0)
1080       lose ("GetObjs call returned unexpected path \"%s\" in position 0; expected /org/freedesktop/DBus/Tests/MyTestObject", (char*) g_ptr_array_index (objs, 0));
1081
1082     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
1083                 g_ptr_array_index (objs, 1)) != 0)
1084       lose ("GetObjs call returned unexpected path \"%s\" in position 1; expected /org/freedesktop/DBus/Tests/MyTestObject2", (char*) g_ptr_array_index (objs, 1));
1085
1086     for (i = 0; i < objs->len; i++)
1087       g_free (g_ptr_array_index (objs, i));
1088     g_ptr_array_free (objs, TRUE);
1089   }
1090
1091   /* Signal handling tests */
1092   
1093   g_print ("Testing signal handling\n");
1094   dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1095   
1096   dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1097                                G_CALLBACK (frobnicate_signal_handler),
1098                                NULL, NULL);
1099   
1100   g_print ("Calling EmitFrobnicate\n");
1101   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1102                           G_TYPE_INVALID, G_TYPE_INVALID))
1103     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1104
1105   
1106   dbus_g_connection_flush (connection);
1107   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1108   g_main_loop_run (loop);
1109
1110   if (n_times_frobnicate_received != 1)
1111     lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1112
1113   g_print ("Calling EmitFrobnicate again\n");
1114   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1115                           G_TYPE_INVALID, G_TYPE_INVALID))
1116     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1117   
1118   dbus_g_connection_flush (connection);
1119   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1120   g_main_loop_run (loop);
1121
1122   if (n_times_frobnicate_received != 2)
1123     lose ("Frobnicate signal received %d times, should have been 2", n_times_frobnicate_received);
1124
1125   g_object_unref (G_OBJECT (proxy));
1126
1127   run_mainloop ();
1128
1129   g_print ("Creating proxy for FooObject interface\n");
1130   proxy = dbus_g_proxy_new_for_name_owner (connection,
1131                                            "org.freedesktop.DBus.TestSuiteGLibService",
1132                                            "/org/freedesktop/DBus/Tests/MyTestObject",
1133                                            "org.freedesktop.DBus.Tests.FooObject",
1134                                            &error);
1135   
1136   if (proxy == NULL)
1137     lose_gerror ("Failed to create proxy for name owner", error);
1138
1139   dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_INT_STRING, 
1140                                      G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
1141
1142   dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_BOXED, 
1143                                      G_TYPE_NONE, G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
1144
1145   dbus_g_proxy_add_signal (proxy, "Sig0", G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
1146   dbus_g_proxy_add_signal (proxy, "Sig1", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
1147   dbus_g_proxy_add_signal (proxy, "Sig2", DBUS_TYPE_G_STRING_STRING_HASHTABLE, G_TYPE_INVALID);
1148   
1149   dbus_g_proxy_connect_signal (proxy, "Sig0",
1150                                G_CALLBACK (sig0_signal_handler),
1151                                NULL, NULL);
1152   dbus_g_proxy_connect_signal (proxy, "Sig1",
1153                                G_CALLBACK (sig1_signal_handler),
1154                                NULL, NULL);
1155   dbus_g_proxy_connect_signal (proxy, "Sig2",
1156                                G_CALLBACK (sig2_signal_handler),
1157                                NULL, NULL);
1158
1159   g_print ("Calling FooObject EmitSignals\n");
1160   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1161
1162   dbus_g_connection_flush (connection);
1163   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1164   g_main_loop_run (loop);
1165   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1166   g_main_loop_run (loop);
1167
1168   if (n_times_sig0_received != 1)
1169     lose ("Sig0 signal received %d times, should have been 1", n_times_sig0_received);
1170   if (n_times_sig1_received != 1)
1171     lose ("Sig1 signal received %d times, should have been 1", n_times_sig1_received);
1172
1173   g_print ("Calling FooObject EmitSignals and EmitSignal2\n");
1174   dbus_g_proxy_call_no_reply (proxy, "EmitSignal2", G_TYPE_INVALID);
1175   dbus_g_connection_flush (connection);
1176
1177   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1178   g_main_loop_run (loop);
1179
1180   if (n_times_sig2_received != 1)
1181     lose ("Sig2 signal received %d times, should have been 1", n_times_sig2_received);
1182
1183   g_print ("Calling FooObject EmitSignals two more times\n");
1184   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1185   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1186
1187   dbus_g_connection_flush (connection);
1188   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1189   g_main_loop_run (loop);
1190   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1191   g_main_loop_run (loop);
1192   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1193   g_main_loop_run (loop);
1194   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1195   g_main_loop_run (loop);
1196
1197   if (n_times_sig0_received != 3)
1198     lose ("Sig0 signal received %d times, should have been 3", n_times_sig0_received);
1199   if (n_times_sig1_received != 3)
1200     lose ("Sig1 signal received %d times, should have been 3", n_times_sig1_received);
1201
1202   /* Terminate again */
1203   g_print ("Terminating service\n");
1204   await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1205   dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1206
1207   proxy_destroyed = FALSE;
1208   proxy_destroy_and_nameowner = TRUE;
1209   proxy_destroy_and_nameowner_complete = FALSE;
1210
1211   g_signal_connect (G_OBJECT (proxy),
1212                     "destroy",
1213                     G_CALLBACK (proxy_destroyed_cb),
1214                     NULL);
1215
1216   dbus_g_connection_flush (connection);
1217   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1218   g_main_loop_run (loop);
1219
1220   if (await_terminating_service != NULL)
1221     lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1222   if (!proxy_destroyed)
1223     lose ("Didn't get proxy_destroyed");
1224   g_print ("Proxy destroyed successfully\n");
1225
1226   /* Don't need to unref, proxy was destroyed */
1227
1228   run_mainloop ();
1229
1230   /* Create a new proxy for the name; should not be associated */
1231   proxy = dbus_g_proxy_new_for_name (connection,
1232                                      "org.freedesktop.DBus.TestSuiteGLibService",
1233                                      "/org/freedesktop/DBus/Tests/MyTestObject",
1234                                      "org.freedesktop.DBus.Tests.MyObject");
1235   g_assert (proxy != NULL);
1236
1237   proxy_destroyed = FALSE;
1238   proxy_destroy_and_nameowner = FALSE;
1239   proxy_destroy_and_nameowner_complete = FALSE;
1240
1241   g_signal_connect (G_OBJECT (proxy),
1242                     "destroy",
1243                     G_CALLBACK (proxy_destroyed_cb),
1244                     NULL);
1245   
1246   if (!dbus_g_proxy_call (driver, "GetNameOwner", &error,
1247                           G_TYPE_STRING,
1248                           "org.freedesktop.DBus.TestSuiteGLibService",
1249                           G_TYPE_INVALID,
1250                           G_TYPE_STRING,
1251                           &v_STRING_2,
1252                           G_TYPE_INVALID)) {
1253     if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
1254       g_print ("Got expected error \"org.freedesktop.DBus.Error.NameHasNoOwner\"\n");
1255     else
1256       lose_gerror ("Unexpected error from GetNameOwner", error);
1257   } else
1258     lose ("GetNameOwner unexpectedly succeeded!");
1259   g_clear_error (&error);
1260
1261   /* This will have the side-effect of activating the service, thus
1262    * causing a NameOwnerChanged, which should let our name proxy
1263    * get signals
1264    */
1265   g_print ("Calling Uppercase for name proxy\n");
1266   if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
1267                           G_TYPE_STRING, "bazwhee",
1268                           G_TYPE_INVALID,
1269                           G_TYPE_STRING, &v_STRING_2,
1270                           G_TYPE_INVALID))
1271     lose_gerror ("Failed to complete Uppercase call", error);
1272   g_free (v_STRING_2);
1273
1274   if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION1"))
1275     g_usleep (8 * G_USEC_PER_SEC);
1276
1277   dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1278   
1279   dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1280                                G_CALLBACK (frobnicate_signal_handler),
1281                                NULL, NULL);
1282   
1283   g_print ("Calling EmitFrobnicate\n");
1284   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1285                           G_TYPE_INVALID, G_TYPE_INVALID))
1286     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1287
1288   n_times_frobnicate_received = 0;
1289
1290   dbus_g_connection_flush (connection);
1291   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1292   g_main_loop_run (loop);
1293
1294   if (n_times_frobnicate_received != 1)
1295     lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1296
1297   /* Now terminate the service, then start it again (implicitly) and wait for signals */
1298   g_print ("Terminating service (2)\n");
1299   await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1300   dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1301   dbus_g_connection_flush (connection);
1302   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1303   g_main_loop_run (loop);
1304   if (await_terminating_service != NULL)
1305     lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1306
1307   if (proxy_destroyed)
1308     lose ("Unexpectedly got proxy_destroyed!");
1309
1310   n_times_frobnicate_received = 0;
1311
1312   g_print ("Calling EmitFrobnicate (2)\n");
1313   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1314                           G_TYPE_INVALID, G_TYPE_INVALID))
1315     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1316
1317   if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION2"))
1318     g_usleep (8 * G_USEC_PER_SEC);
1319
1320   dbus_g_connection_flush (connection);
1321   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1322   g_main_loop_run (loop);
1323
1324   if (n_times_frobnicate_received != 1)
1325     lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1326
1327   if (proxy_destroyed)
1328     lose ("Unexpectedly got proxy_destroyed!");
1329   
1330   /* Create another proxy for the name; should be associated immediately */
1331   proxy2 = dbus_g_proxy_new_for_name (connection,
1332                                      "org.freedesktop.DBus.TestSuiteGLibService",
1333                                      "/org/freedesktop/DBus/Tests/MyTestObject",
1334                                      "org.freedesktop.DBus.Tests.MyObject");
1335   g_assert (proxy2 != NULL);
1336
1337   dbus_g_proxy_add_signal (proxy2, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1338   
1339   dbus_g_proxy_connect_signal (proxy2, "Frobnicate",
1340                                G_CALLBACK (frobnicate_signal_handler_2),
1341                                NULL, NULL);
1342
1343   g_print ("Calling EmitFrobnicate (3)\n");
1344   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1345                           G_TYPE_INVALID, G_TYPE_INVALID))
1346     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1347
1348   dbus_g_connection_flush (connection);
1349   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1350   g_main_loop_run (loop);
1351
1352   if (n_times_frobnicate_received != 2)
1353     lose ("Frobnicate signal received %d times for 1st proxy, should have been 2", n_times_frobnicate_received);
1354   if (n_times_frobnicate_received_2 != 1)
1355     lose ("Frobnicate signal received %d times for 2nd proxy, should have been 1", n_times_frobnicate_received_2);
1356
1357   g_object_unref (G_OBJECT (proxy));
1358   g_object_unref (G_OBJECT (proxy2));
1359
1360   run_mainloop ();
1361
1362   /* Test introspection */
1363   proxy = dbus_g_proxy_new_for_name_owner (connection,
1364                                            "org.freedesktop.DBus.TestSuiteGLibService",
1365                                            "/org/freedesktop/DBus/Tests/MyTestObject",
1366                                            "org.freedesktop.DBus.Introspectable",
1367                                            &error);
1368   if (proxy == NULL)
1369     lose_gerror ("Failed to create proxy for name owner", error);
1370
1371   g_print ("Testing introspect\n");
1372   if (!dbus_g_proxy_call (proxy, "Introspect", &error,
1373                           G_TYPE_INVALID,
1374                           G_TYPE_STRING, &v_STRING_2,
1375                           G_TYPE_INVALID))
1376     lose_gerror ("Failed to complete Introspect call", error);
1377
1378   /* Could just do strcmp(), but that seems more fragile */
1379   {
1380     NodeInfo *node;
1381     GSList *elt;
1382     gboolean found_introspectable;
1383     gboolean found_properties;
1384     gboolean found_myobject;
1385     gboolean found_fooobject;
1386
1387     node = description_load_from_string (v_STRING_2, strlen (v_STRING_2), &error);
1388     if (!node)
1389       lose_gerror ("Failed to parse introspection data: %s", error);
1390
1391     found_introspectable = FALSE;
1392     found_properties = FALSE;
1393     found_myobject = FALSE;
1394     found_fooobject = FALSE;
1395     for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
1396       {
1397         InterfaceInfo *iface = elt->data;
1398
1399         if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
1400           found_introspectable = TRUE;
1401         else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
1402           found_properties = TRUE;
1403         else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
1404           {
1405             GSList *elt;
1406             gboolean found_manyargs;
1407             
1408             found_myobject = TRUE;
1409             
1410             found_manyargs = FALSE;
1411             for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
1412               {
1413                 MethodInfo *method;
1414
1415                 method = elt->data;
1416                 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
1417                   {
1418                     found_manyargs = TRUE;
1419                     break;
1420                   }
1421               }
1422             if (!found_manyargs)
1423               lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
1424           }
1425         else if (!found_fooobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.FooObject") == 0)
1426           found_fooobject = TRUE;
1427         else
1428           lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
1429       }
1430
1431     if (!(found_introspectable && found_myobject && found_properties))
1432       lose ("Missing interface"); 
1433   }
1434   g_free (v_STRING_2);
1435   
1436   g_object_unref (G_OBJECT (driver));
1437
1438   g_print ("Successfully completed %s\n", argv[0]);
1439   
1440   return 0;
1441 }