1 /* -*- mode: C; c-file-style: "gnu" -*- */
7 static GMainLoop *loop = NULL;
8 static int n_times_foo_received = 0;
11 foo_signal_handler (DBusGProxy *proxy,
18 if (!dbus_message_is_signal (signal,
19 "org.freedesktop.TestSuite",
22 g_printerr ("Signal handler received the wrong message\n");
26 dbus_error_init (&derror);
27 if (!dbus_message_get_args (signal, &derror, DBUS_TYPE_DOUBLE,
28 &d, DBUS_TYPE_INVALID))
30 g_printerr ("failed to get signal args: %s\n", derror.message);
31 dbus_error_free (&derror);
35 n_times_foo_received += 1;
37 g_main_loop_quit (loop);
41 main (int argc, char **argv)
43 DBusConnection *connection;
47 DBusPendingCall *call;
56 loop = g_main_loop_new (NULL, FALSE);
59 connection = dbus_bus_get_with_g_main (DBUS_BUS_SESSION,
61 if (connection == NULL)
63 g_printerr ("Failed to open connection to bus: %s\n",
69 /* Create a proxy object for the "bus driver" */
71 driver = dbus_gproxy_new_for_service (connection,
72 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
73 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
74 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
76 /* Call ListServices method */
78 call = dbus_gproxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);
81 if (!dbus_gproxy_end_call (driver, call, &error,
82 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
83 &service_list, &service_list_len,
86 g_printerr ("Failed to complete ListServices call: %s\n",
92 g_print ("Services on the message bus:\n");
94 while (i < service_list_len)
96 g_assert (service_list[i] != NULL);
97 g_print (" %s\n", service_list[i]);
100 g_assert (service_list[i] == NULL);
102 dbus_free_string_array (service_list);
104 /* Test handling of unknown method */
105 call = dbus_gproxy_begin_call (driver, "ThisMethodDoesNotExist",
107 "blah blah blah blah blah",
113 if (dbus_gproxy_end_call (driver, call, &error,
116 g_printerr ("Calling nonexistent method succeeded!\n");
120 g_print ("Got EXPECTED error from calling unknown method: %s\n",
122 g_error_free (error);
124 /* Activate a service */
125 call = dbus_gproxy_begin_call (driver, "ActivateService",
127 "org.freedesktop.DBus.TestSuiteEchoService",
133 if (!dbus_gproxy_end_call (driver, call, &error,
134 DBUS_TYPE_UINT32, &result,
137 g_printerr ("Failed to complete Activate call: %s\n",
139 g_error_free (error);
143 g_print ("Activation of echo service = 0x%x\n", result);
145 /* Activate a service again */
146 call = dbus_gproxy_begin_call (driver, "ActivateService",
148 "org.freedesktop.DBus.TestSuiteEchoService",
154 if (!dbus_gproxy_end_call (driver, call, &error,
155 DBUS_TYPE_UINT32, &result,
158 g_printerr ("Failed to complete Activate call: %s\n",
160 g_error_free (error);
164 g_print ("Duplicate activation of echo service = 0x%x\n", result);
166 /* Talk to the new service */
168 proxy = dbus_gproxy_new_for_service (connection,
169 "org.freedesktop.DBus.TestSuiteEchoService",
170 "/org/freedesktop/TestSuite",
171 "org.freedesktop.TestSuite");
173 call = dbus_gproxy_begin_call (proxy, "Echo",
179 if (!dbus_gproxy_end_call (proxy, call, &error,
180 DBUS_TYPE_STRING, &str,
183 g_printerr ("Failed to complete Echo call: %s\n",
185 g_error_free (error);
189 g_print ("String echoed = \"%s\"\n", str);
192 /* Test oneway call and signal handling */
194 dbus_gproxy_connect_signal (proxy, "Foo",
198 dbus_gproxy_call_no_reply (proxy, "EmitFoo",
201 dbus_connection_flush (connection);
203 g_main_loop_run (loop);
205 if (n_times_foo_received != 1)
207 g_printerr ("Foo signal received %d times, should have been 1\n",
208 n_times_foo_received);
212 g_object_unref (G_OBJECT (driver));
213 g_object_unref (G_OBJECT (proxy));
215 g_print ("Successfully completed %s\n", argv[0]);