Debian Codegraph @

tests/wp/proxy.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2019 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/base-test-fixture.h"
10
11 typedef struct {
12 WpBaseTestFixture base;
13 /* the object manager that listens for proxies */
14 WpObjectManager *om;
15 } TestFixture;
16
17 static void
18 test_proxy_setup (TestFixture *self, gconstpointer user_data)
19 {
20 wp_base_test_fixture_setup (&self->base, 0);
21 self->om = wp_object_manager_new ();
22 }
23
24 static void
25 test_proxy_teardown (TestFixture *self, gconstpointer user_data)
26 {
27 g_clear_object (&self->om);
28 wp_base_test_fixture_teardown (&self->base);
29 }
30
31 static void
32 test_proxy_basic_activated (WpObject *proxy, GAsyncResult *res,
33 TestFixture *fixture)
34 {
35 g_autoptr (GError) error = NULL;
36 g_assert_true (wp_object_activate_finish (proxy, res, &error));
37 g_assert_no_error (error);
38
39 g_assert_true (wp_object_get_active_features (proxy) & WP_PROXY_FEATURE_BOUND);
40 g_assert_nonnull (wp_proxy_get_pw_proxy (WP_PROXY (proxy)));
41
42 g_main_loop_quit (fixture->base.loop);
43 }
44
45 static void
46 test_proxy_basic_object_added (WpObjectManager *om, WpGlobalProxy *proxy,
47 TestFixture *fixture)
48 {
49 g_assert_nonnull (proxy);
50 {
51 g_autoptr (WpCore) pcore = NULL;
52 g_autoptr (WpCore) omcore = NULL;
53 g_object_get (proxy, "core", &pcore, NULL);
54 g_object_get (om, "core", &omcore, NULL);
55 g_assert_nonnull (pcore);
56 g_assert_nonnull (omcore);
57 g_assert_true (pcore == omcore);
58 }
59 g_assert_cmphex (wp_global_proxy_get_permissions (proxy), ==, PW_PERM_ALL);
60 g_assert_true (WP_IS_CLIENT (proxy));
61
62 g_assert_cmphex (wp_object_get_active_features (WP_OBJECT (proxy)), ==, 0);
63 g_assert_null (wp_proxy_get_pw_proxy (WP_PROXY (proxy)));
64
65 {
66 g_autoptr (WpProperties) props =
67 wp_global_proxy_get_global_properties (proxy);
68 g_assert_nonnull (props);
69 g_assert_cmpstr (wp_properties_get (props, PW_KEY_PROTOCOL), ==,
70 "protocol-native");
71 }
72
73 wp_object_activate (WP_OBJECT (proxy), WP_PROXY_FEATURE_BOUND, NULL,
74 (GAsyncReadyCallback) test_proxy_basic_activated, fixture);
75 }
76
77 static void
78 test_proxy_basic (TestFixture *fixture, gconstpointer data)
79 {
80 /* our test server should advertise exactly one
81 * client: our core; use this to test WpProxy */
82 g_signal_connect (fixture->om, "object-added",
83 (GCallback) test_proxy_basic_object_added, fixture);
84
85 wp_object_manager_add_interest (fixture->om, WP_TYPE_CLIENT, NULL);
86 wp_core_install_object_manager (fixture->base.core, fixture->om);
87
88 g_main_loop_run (fixture->base.loop);
89 }
90
91 static void
92 test_node_enum_params_done (WpPipewireObject *node, GAsyncResult *res,
93 TestFixture *f)
94 {
95 g_autoptr (WpIterator) params = NULL;
96 g_autoptr (GError) error = NULL;
97 g_auto (GValue) item = G_VALUE_INIT;
98 guint n_params = 0;
99
100 params = wp_pipewire_object_enum_params_finish (node, res, &error);
101 g_assert_no_error (error);
102 g_assert_nonnull (params);
103
104 for (; wp_iterator_next (params, &item); g_value_unset (&item)) {
105 WpSpaPod *pod = NULL;
106 g_assert_cmpuint (G_VALUE_TYPE (&item), ==, WP_TYPE_SPA_POD);
107 pod = g_value_get_boxed (&item);
108 g_assert_nonnull (pod);
109 g_assert_true (wp_spa_pod_is_object (pod));
110 g_assert_cmpuint (wp_spa_type_from_name ("Spa:Pod:Object:Param:PropInfo"),
111 ==, wp_spa_pod_get_spa_type (pod));
112 n_params++;
113 }
114 g_assert_cmpint (n_params, >, 0);
115
116 g_main_loop_quit (f->base.loop);
117 }
118
119 static void
120 test_node (TestFixture *f, gconstpointer data)
121 {
122 g_autoptr (WpPipewireObject) proxy = NULL;
123 const struct pw_node_info *info;
124
125 /* load audiotestsrc on the server side */
126 {
127 g_autoptr (WpTestServerLocker) lock =
128 wp_test_server_locker_new (&f->base.server);
129
130 g_assert_cmpint (pw_context_add_spa_lib (f->base.server.context,
131 "audiotestsrc", "audiotestsrc/libspa-audiotestsrc"), ==, 0);
132 if (!test_is_spa_lib_installed (&f->base, "audiotestsrc")) {
133 g_test_skip ("The pipewire audiotestsrc factory was not found");
134 return;
135 }
136
137 g_assert_nonnull (pw_context_load_module (f->base.server.context,
138 "libpipewire-module-adapter", NULL, NULL));
139 }
140
141 proxy = WP_PIPEWIRE_OBJECT (wp_node_new_from_factory (f->base.core,
142 "adapter",
143 wp_properties_new (
144 "factory.name", "audiotestsrc",
145 "node.name", "audiotestsrc.adapter",
146 NULL)));
147 g_assert_nonnull (proxy);
148 wp_object_activate (WP_OBJECT (proxy), WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL,
149 NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
150 g_main_loop_run (f->base.loop);
151
152 /* basic tests */
153 g_assert_cmphex (wp_object_get_active_features (WP_OBJECT (proxy)), ==,
154 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
155 g_assert_nonnull (wp_proxy_get_pw_proxy (WP_PROXY (proxy)));
156 g_assert_true (WP_IS_NODE (proxy));
157
158 /* info */
159 {
160 info = wp_pipewire_object_get_native_info (proxy);
161 g_assert_nonnull (info);
162 g_assert_cmpint (wp_proxy_get_bound_id (WP_PROXY (proxy)), ==, info->id);
163 }
164
165 /* properties */
166 {
167 const gchar *id = wp_pipewire_object_get_property (proxy, PW_KEY_OBJECT_ID);
168 g_assert_nonnull (id);
169 g_assert_cmpint (info->id, ==, atoi(id));
170 }
171 {
172 const char *id;
173 g_autoptr (WpProperties) props = NULL;
174
175 props = wp_pipewire_object_get_properties (proxy);
176 g_assert_nonnull (props);
177 id = wp_properties_get (props, PW_KEY_OBJECT_ID);
178 g_assert_nonnull (id);
179 g_assert_cmpint (info->id, ==, atoi(id));
180 }
181
182 /* param info */
183 {
184 const gchar *flags_str;
185 g_autoptr (GVariant) param_info = wp_pipewire_object_get_param_info (proxy);
186
187 g_assert_nonnull (param_info);
188 g_assert_true (g_variant_is_of_type (param_info, G_VARIANT_TYPE ("a{ss}")));
189 g_assert_cmpuint (g_variant_n_children (param_info), ==, info->n_params);
190 g_assert_true (g_variant_lookup (param_info, "PropInfo", "&s", &flags_str));
191 g_assert_cmpstr (flags_str, ==, "r");
192 g_assert_true (g_variant_lookup (param_info, "Props", "&s", &flags_str));
193 g_assert_cmpstr (flags_str, ==, "rw");
194 }
195
196 /* enum params */
197 wp_pipewire_object_enum_params (proxy, "PropInfo", NULL, NULL,
198 (GAsyncReadyCallback) test_node_enum_params_done, f);
199 g_main_loop_run (f->base.loop);
200 }
201
202 static void
203 activate_error_cb (WpObject * object, GAsyncResult * res,
204 WpBaseTestFixture * f)
205 {
206 g_autoptr (GError) error = NULL;
207 gboolean augment_ret = wp_object_activate_finish (object, res, &error);
208 g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED);
209 g_assert_false (augment_ret);
210
211 g_main_loop_quit (f->loop);
212 }
213
214 static void
215 test_link_error (TestFixture *f, gconstpointer data)
216 {
217 g_autoptr (WpPipewireObject) proxy = NULL;
218
219 /* load audiotestsrc on the server side */
220 {
221 g_autoptr (WpTestServerLocker) lock =
222 wp_test_server_locker_new (&f->base.server);
223
224 g_assert_nonnull (pw_context_load_module (f->base.server.context,
225 "libpipewire-module-link-factory", NULL, NULL));
226 }
227
228 proxy = WP_PIPEWIRE_OBJECT (wp_link_new_from_factory (f->base.core,
229 "link-factory",
230 wp_properties_new (
231 "link.output.node", "invalid",
232 "link.input.node", "invalid",
233 NULL)));
234 g_assert_nonnull (proxy);
235 wp_object_activate (WP_OBJECT (proxy), WP_OBJECT_FEATURES_ALL,
236 NULL, (GAsyncReadyCallback) activate_error_cb, f);
237 g_main_loop_run (f->base.loop);
238 }
239
240 static void
241 enum_params_error_cb (WpPipewireObject * object, GAsyncResult * res,
242 WpBaseTestFixture * f)
243 {
244 g_autoptr (GError) error = NULL;
245 g_autoptr (WpIterator) it =
246 wp_pipewire_object_enum_params_finish (object, res, &error);
247 g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED);
248 g_assert_null (it);
249
250 g_main_loop_quit (f->loop);
251 }
252
253 static void
254 test_enum_params_error (TestFixture *f, gconstpointer data)
255 {
256 g_autoptr (WpNode) node = NULL;
257
258 /* load audiotestsrc on the server side */
259 {
260 g_autoptr (WpTestServerLocker) lock =
261 wp_test_server_locker_new (&f->base.server);
262
263 g_assert_cmpint (pw_context_add_spa_lib (f->base.server.context,
264 "audiotestsrc", "audiotestsrc/libspa-audiotestsrc"), ==, 0);
265 if (!test_is_spa_lib_installed (&f->base, "audiotestsrc")) {
266 g_test_skip ("The pipewire audiotestsrc factory was not found");
267 return;
268 }
269 g_assert_nonnull (pw_context_load_module (f->base.server.context,
270 "libpipewire-module-adapter", NULL, NULL));
271 }
272
273 node = wp_node_new_from_factory (f->base.core,
274 "adapter",
275 wp_properties_new (
276 "factory.name", "audiotestsrc",
277 "node.name", "audiotestsrc",
278 NULL));
279 g_assert_nonnull (node);
280
281 wp_object_activate (WP_OBJECT (node), WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL,
282 NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
283 g_main_loop_run (f->base.loop);
284
285 /* EnumRoute doesn't exist on audiotestsrc, obviously */
286 wp_pipewire_object_enum_params (WP_PIPEWIRE_OBJECT (node), "EnumRoute", NULL,
287 NULL, (GAsyncReadyCallback) enum_params_error_cb, f);
288 g_main_loop_run (f->base.loop);
289 }
290
291 gint
292 main (gint argc, gchar *argv[])
293 {
294 g_test_init (&argc, &argv, NULL);
295 wp_init (WP_INIT_ALL);
296
297 g_test_add ("/wp/proxy/basic", TestFixture, NULL,
298 test_proxy_setup, test_proxy_basic, test_proxy_teardown);
299 g_test_add ("/wp/proxy/node", TestFixture, NULL,
300 test_proxy_setup, test_node, test_proxy_teardown);
301 g_test_add ("/wp/proxy/link_error", TestFixture, NULL,
302 test_proxy_setup, test_link_error, test_proxy_teardown);
303 g_test_add ("/wp/proxy/enum_params_error", TestFixture, NULL,
304 test_proxy_setup, test_enum_params_error, test_proxy_teardown);
305
306 return g_test_run ();
307 }