Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-proxy-well-known-name.c
blob72d23bdf4a4aecd8651c9293ca7d97e52e688090
1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
25 #include "gdbus-tests.h"
27 /* all tests rely on a shared mainloop */
28 static GMainLoop *loop = NULL;
30 /* ---------------------------------------------------------------------------------------------------- */
32 static void
33 proxy_new_cb (GObject *source_object,
34 GAsyncResult *res,
35 gpointer user_data)
37 GDBusProxy **ret = user_data;
38 GError *error;
40 error = NULL;
41 *ret = g_dbus_proxy_new_finish (res, &error);
42 g_assert_no_error (error);
43 g_assert (ret != NULL);
45 g_main_loop_quit (loop);
48 static void
49 test_proxy_well_known_name (void)
51 GDBusProxy *p;
52 GDBusProxy *p2;
53 GDBusProxy *ap;
54 GDBusProxy *ap2;
55 GDBusConnection *c;
56 GError *error;
57 gchar *name_owner;
58 gchar **property_names;
59 GVariant *variant;
60 GVariant *result;
62 session_bus_up ();
64 error = NULL;
65 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
66 g_assert_no_error (error);
67 g_assert (c != NULL);
69 error = NULL;
70 p = g_dbus_proxy_new_sync (c,
71 G_DBUS_PROXY_FLAGS_NONE,
72 NULL, /* GDBusInterfaceInfo* */
73 "com.example.TestService", /* name */
74 "/com/example/TestObject", /* object path */
75 "com.example.Frob", /* interface name */
76 NULL, /* GCancellable */
77 &error);
78 g_assert_no_error (error);
80 /* we shouldn't have a name owner nor any cached properties */
81 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
82 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
84 /* also for async: we shouldn't have a name owner nor any cached properties */
85 g_dbus_proxy_new (c,
86 G_DBUS_PROXY_FLAGS_NONE,
87 NULL, /* GDBusInterfaceInfo* */
88 "com.example.TestService", /* name */
89 "/com/example/TestObject", /* object path */
90 "com.example.Frob", /* interface name */
91 NULL, /* GCancellable */
92 (GAsyncReadyCallback) proxy_new_cb,
93 &ap);
94 g_main_loop_run (loop);
95 g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
96 g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
98 /* this is safe; testserver will exit once the bus goes away */
99 g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
101 /* check that we get the notify::g-name-owner signal */
102 _g_assert_property_notify (p, "g-name-owner");
104 /* Now we should have a name owner as well as properties */
105 name_owner = g_dbus_proxy_get_name_owner (p);
106 property_names = g_dbus_proxy_get_cached_property_names (p);
107 g_assert (g_dbus_is_unique_name (name_owner));
108 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
109 g_free (name_owner);
110 g_strfreev (property_names);
112 /* if we create another proxy with the service being available, check that
113 * it has a name owner and properties
115 error = NULL;
116 p2 = g_dbus_proxy_new_sync (c,
117 G_DBUS_PROXY_FLAGS_NONE,
118 NULL, /* GDBusInterfaceInfo* */
119 "com.example.TestService", /* name */
120 "/com/example/TestObject", /* object path */
121 "com.example.Frob", /* interface name */
122 NULL, /* GCancellable */
123 &error);
124 g_assert_no_error (error);
125 name_owner = g_dbus_proxy_get_name_owner (p2);
126 property_names = g_dbus_proxy_get_cached_property_names (p2);
127 g_assert (g_dbus_is_unique_name (name_owner));
128 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
129 g_free (name_owner);
130 g_strfreev (property_names);
132 /* also for async: we should have a name owner and cached properties */
133 g_dbus_proxy_new (c,
134 G_DBUS_PROXY_FLAGS_NONE,
135 NULL, /* GDBusInterfaceInfo* */
136 "com.example.TestService", /* name */
137 "/com/example/TestObject", /* object path */
138 "com.example.Frob", /* interface name */
139 NULL, /* GCancellable */
140 (GAsyncReadyCallback) proxy_new_cb,
141 &ap2);
142 g_main_loop_run (loop);
143 name_owner = g_dbus_proxy_get_name_owner (ap2);
144 property_names = g_dbus_proxy_get_cached_property_names (ap2);
145 g_assert (g_dbus_is_unique_name (name_owner));
146 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
147 g_free (name_owner);
148 g_strfreev (property_names);
150 /* Check property value is the initial value */
151 variant = g_dbus_proxy_get_cached_property (p, "y");
152 g_assert (variant != NULL);
153 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
154 g_variant_unref (variant);
155 variant = g_dbus_proxy_get_cached_property (p2, "y");
156 g_assert (variant != NULL);
157 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
158 g_variant_unref (variant);
159 variant = g_dbus_proxy_get_cached_property (ap, "y");
160 g_assert (variant != NULL);
161 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
162 g_variant_unref (variant);
163 variant = g_dbus_proxy_get_cached_property (ap2, "y");
164 g_assert (variant != NULL);
165 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
166 g_variant_unref (variant);
168 /* Check that properties are updated on both p and p2 */
169 result = g_dbus_proxy_call_sync (p,
170 "FrobSetProperty",
171 g_variant_new ("(sv)",
172 "y",
173 g_variant_new_byte (42)),
174 G_DBUS_CALL_FLAGS_NONE,
176 NULL,
177 &error);
178 g_assert_no_error (error);
179 g_assert (result != NULL);
180 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
181 g_variant_unref (result);
182 _g_assert_signal_received (p, "g-properties-changed");
183 variant = g_dbus_proxy_get_cached_property (p, "y");
184 g_assert (variant != NULL);
185 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
186 g_variant_unref (variant);
187 variant = g_dbus_proxy_get_cached_property (p2, "y");
188 g_assert (variant != NULL);
189 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
190 g_variant_unref (variant);
191 variant = g_dbus_proxy_get_cached_property (ap, "y");
192 g_assert (variant != NULL);
193 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
194 g_variant_unref (variant);
195 variant = g_dbus_proxy_get_cached_property (ap2, "y");
196 g_assert (variant != NULL);
197 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
198 g_variant_unref (variant);
200 /* Nuke the service and check that we get the signal and then don't
201 * have a name owner nor any cached properties
203 result = g_dbus_proxy_call_sync (p,
204 "Quit",
205 NULL,
206 G_DBUS_CALL_FLAGS_NONE,
208 NULL,
209 &error);
210 g_assert_no_error (error);
211 g_assert (result != NULL);
212 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
213 g_variant_unref (result);
214 /* and wait... */
215 _g_assert_property_notify (p, "g-name-owner");
216 /* now we shouldn't have a name owner nor any cached properties */
217 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
218 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
219 g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
221 /* now bring back the server and wait for the proxy to be updated.. now
222 * the 'y' property should be back at 1...
224 /* this is safe; testserver will exit once the bus goes away */
225 g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
227 /* check that we get the notify::g-name-owner signal */
228 _g_assert_property_notify (p, "g-name-owner");
229 /* Now we should have a name owner as well as properties */
230 name_owner = g_dbus_proxy_get_name_owner (p);
231 property_names = g_dbus_proxy_get_cached_property_names (p);
232 g_assert (g_dbus_is_unique_name (name_owner));
233 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
234 g_free (name_owner);
235 g_strfreev (property_names);
236 /* and finally check the 'y' property */
237 variant = g_dbus_proxy_get_cached_property (p, "y");
238 g_assert (variant != NULL);
239 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
240 g_variant_unref (variant);
242 g_object_unref (p2);
243 g_object_unref (p);
244 g_object_unref (ap2);
245 g_object_unref (ap);
247 g_object_unref (c);
249 /* tear down bus */
250 session_bus_down ();
253 /* ---------------------------------------------------------------------------------------------------- */
256 main (int argc,
257 char *argv[])
259 gint ret;
261 g_test_init (&argc, &argv, NULL);
263 /* all the tests rely on a shared main loop */
264 loop = g_main_loop_new (NULL, FALSE);
266 g_test_dbus_unset ();
268 g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);
270 ret = g_test_run();
271 return ret;