1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include "gdbus-tests.h"
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
32 /* ---------------------------------------------------------------------------------------------------- */
35 proxy_new_cb (GObject *source_object,
39 GDBusProxy **ret = user_data;
43 *ret = g_dbus_proxy_new_finish (res, &error);
44 g_assert_no_error (error);
45 g_assert (ret != NULL);
47 g_main_loop_quit (loop);
51 test_proxy_well_known_name (void)
60 gchar **property_names;
67 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
68 g_assert_no_error (error);
72 p = g_dbus_proxy_new_sync (c,
73 G_DBUS_PROXY_FLAGS_NONE,
74 NULL, /* GDBusInterfaceInfo* */
75 "com.example.TestService", /* name */
76 "/com/example/TestObject", /* object path */
77 "com.example.Frob", /* interface name */
78 NULL, /* GCancellable */
80 g_assert_no_error (error);
82 /* we shouldn't have a name owner nor any cached properties */
83 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
84 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
86 /* also for async: we shouldn't have a name owner nor any cached properties */
88 G_DBUS_PROXY_FLAGS_NONE,
89 NULL, /* GDBusInterfaceInfo* */
90 "com.example.TestService", /* name */
91 "/com/example/TestObject", /* object path */
92 "com.example.Frob", /* interface name */
93 NULL, /* GCancellable */
94 (GAsyncReadyCallback) proxy_new_cb,
96 g_main_loop_run (loop);
97 g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
98 g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
100 /* this is safe; testserver will exit once the bus goes away */
101 g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
103 /* check that we get the notify::g-name-owner signal */
104 _g_assert_property_notify (p, "g-name-owner");
106 /* Now we should have a name owner as well as properties */
107 name_owner = g_dbus_proxy_get_name_owner (p);
108 property_names = g_dbus_proxy_get_cached_property_names (p);
109 g_assert (g_dbus_is_unique_name (name_owner));
110 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
112 g_strfreev (property_names);
114 /* if we create another proxy with the service being available, check that
115 * it has a name owner and properties
118 p2 = g_dbus_proxy_new_sync (c,
119 G_DBUS_PROXY_FLAGS_NONE,
120 NULL, /* GDBusInterfaceInfo* */
121 "com.example.TestService", /* name */
122 "/com/example/TestObject", /* object path */
123 "com.example.Frob", /* interface name */
124 NULL, /* GCancellable */
126 g_assert_no_error (error);
127 name_owner = g_dbus_proxy_get_name_owner (p2);
128 property_names = g_dbus_proxy_get_cached_property_names (p2);
129 g_assert (g_dbus_is_unique_name (name_owner));
130 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
132 g_strfreev (property_names);
134 /* also for async: we should have a name owner and cached properties */
136 G_DBUS_PROXY_FLAGS_NONE,
137 NULL, /* GDBusInterfaceInfo* */
138 "com.example.TestService", /* name */
139 "/com/example/TestObject", /* object path */
140 "com.example.Frob", /* interface name */
141 NULL, /* GCancellable */
142 (GAsyncReadyCallback) proxy_new_cb,
144 g_main_loop_run (loop);
145 name_owner = g_dbus_proxy_get_name_owner (ap2);
146 property_names = g_dbus_proxy_get_cached_property_names (ap2);
147 g_assert (g_dbus_is_unique_name (name_owner));
148 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
150 g_strfreev (property_names);
152 /* Check property value is the initial value */
153 variant = g_dbus_proxy_get_cached_property (p, "y");
154 g_assert (variant != NULL);
155 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
156 g_variant_unref (variant);
157 variant = g_dbus_proxy_get_cached_property (p2, "y");
158 g_assert (variant != NULL);
159 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
160 g_variant_unref (variant);
161 variant = g_dbus_proxy_get_cached_property (ap, "y");
162 g_assert (variant != NULL);
163 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
164 g_variant_unref (variant);
165 variant = g_dbus_proxy_get_cached_property (ap2, "y");
166 g_assert (variant != NULL);
167 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
168 g_variant_unref (variant);
170 /* Check that properties are updated on both p and p2 */
171 result = g_dbus_proxy_call_sync (p,
173 g_variant_new ("(sv)",
175 g_variant_new_byte (42)),
176 G_DBUS_CALL_FLAGS_NONE,
180 g_assert_no_error (error);
181 g_assert (result != NULL);
182 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
183 g_variant_unref (result);
184 _g_assert_signal_received (p, "g-properties-changed");
185 variant = g_dbus_proxy_get_cached_property (p, "y");
186 g_assert (variant != NULL);
187 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
188 g_variant_unref (variant);
189 variant = g_dbus_proxy_get_cached_property (p2, "y");
190 g_assert (variant != NULL);
191 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
192 g_variant_unref (variant);
193 variant = g_dbus_proxy_get_cached_property (ap, "y");
194 g_assert (variant != NULL);
195 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
196 g_variant_unref (variant);
197 variant = g_dbus_proxy_get_cached_property (ap2, "y");
198 g_assert (variant != NULL);
199 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
200 g_variant_unref (variant);
202 /* Nuke the service and check that we get the signal and then don't
203 * have a name owner nor any cached properties
205 result = g_dbus_proxy_call_sync (p,
208 G_DBUS_CALL_FLAGS_NONE,
212 g_assert_no_error (error);
213 g_assert (result != NULL);
214 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
215 g_variant_unref (result);
217 _g_assert_property_notify (p, "g-name-owner");
218 /* now we shouldn't have a name owner nor any cached properties */
219 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
220 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
221 g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
223 /* now bring back the server and wait for the proxy to be updated.. now
224 * the 'y' property should be back at 1...
226 /* this is safe; testserver will exit once the bus goes away */
227 g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
229 /* check that we get the notify::g-name-owner signal */
230 _g_assert_property_notify (p, "g-name-owner");
231 /* Now we should have a name owner as well as properties */
232 name_owner = g_dbus_proxy_get_name_owner (p);
233 property_names = g_dbus_proxy_get_cached_property_names (p);
234 g_assert (g_dbus_is_unique_name (name_owner));
235 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
237 g_strfreev (property_names);
238 /* and finally check the 'y' property */
239 variant = g_dbus_proxy_get_cached_property (p, "y");
240 g_assert (variant != NULL);
241 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
242 g_variant_unref (variant);
246 g_object_unref (ap2);
255 /* ---------------------------------------------------------------------------------------------------- */
263 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
265 /* all the tests rely on a shared main loop */
266 loop = g_main_loop_new (NULL, FALSE);
268 g_test_dbus_unset ();
270 g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);