Debian Codegraph @

tests/modules/si-node.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 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 } TestFixture;
14
15 typedef struct {
16 const gchar *factory;
17 const gchar *name;
18 const gchar *media_class;
19 const gchar *expected_media_class;
20 WpDirection expected_direction;
21 } TestData;
22
23 static void
24 on_plugin_loaded (WpCore * core, GAsyncResult * res, TestFixture *f)
25 {
26 gboolean loaded;
27 GError *error = NULL;
28
29 loaded = wp_core_load_component_finish (core, res, &error);
30 g_assert_no_error (error);
31 g_assert_true (loaded);
32
33 g_main_loop_quit (f->base.loop);
34 }
35
36 static void
37 test_si_node_setup (TestFixture * f, gconstpointer user_data)
38 {
39 wp_base_test_fixture_setup (&f->base, 0);
40
41 /* load modules */
42 {
43 g_autoptr (WpTestServerLocker) lock =
44 wp_test_server_locker_new (&f->base.server);
45
46 g_assert_cmpint (pw_context_add_spa_lib (f->base.server.context,
47 "audiotestsrc", "audiotestsrc/libspa-audiotestsrc"), ==, 0);
48 if (!test_is_spa_lib_installed (&f->base, "audiotestsrc")) {
49 g_test_skip ("The pipewire audiotestsrc factory was not found");
50 return;
51 }
52 g_assert_nonnull (pw_context_load_module (f->base.server.context,
53 "libpipewire-module-spa-node-factory", NULL, NULL));
54 }
55 {
56 wp_core_load_component (f->base.core,
57 "libwireplumber-module-si-node", "module", NULL, NULL, NULL,
58 (GAsyncReadyCallback) on_plugin_loaded, f);
59 g_main_loop_run (f->base.loop);
60 }
61 }
62
63 static void
64 test_si_node_teardown (TestFixture * f, gconstpointer user_data)
65 {
66 wp_base_test_fixture_teardown (&f->base);
67 }
68
69 static void
70 test_si_node_configure_activate (TestFixture * f, gconstpointer user_data)
71 {
72 const TestData *data = user_data;
73 g_autoptr (WpNode) node = NULL;
74 g_autoptr (WpSessionItem) item = NULL;
75
76 /* skip test if spa factory is not installed */
77 if (!test_is_spa_lib_installed (&f->base, data->factory)) {
78 g_autofree gchar *msg = NULL;
79 msg = g_strdup_printf ("The pipewire %s factory was not found",
80 data->factory);
81 g_test_skip (msg);
82 return;
83 }
84
85 /* create item */
86
87 item = wp_session_item_make (f->base.core, "si-node");
88 g_assert_nonnull (item);
89 g_assert_true (WP_IS_SI_LINKABLE (item));
90
91 node = wp_node_new_from_factory (f->base.core,
92 "spa-node-factory",
93 wp_properties_new (
94 "factory.name", data->factory,
95 "node.name", data->name,
96 NULL));
97 g_assert_nonnull (node);
98
99 wp_object_activate (WP_OBJECT (node), WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL,
100 NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
101 g_main_loop_run (f->base.loop);
102
103 /* configure */
104
105 {
106 WpProperties *props = wp_properties_new_empty ();
107 wp_properties_setf (props, "item.node", "%p", node);
108 wp_properties_set (props, "media.class", data->media_class);
109 g_assert_true (wp_session_item_configure (item, props));
110 g_assert_true (wp_session_item_is_configured (item));
111 }
112
113 {
114 const gchar *str = NULL;
115 g_autoptr (WpProperties) props = wp_session_item_get_properties (item);
116 g_assert_nonnull (props);
117 str = wp_properties_get (props, "media.class");
118 g_assert_nonnull (str);
119 g_assert_cmpstr (data->expected_media_class, ==, str);
120 str = wp_properties_get (props, "item.factory.name");
121 g_assert_nonnull (str);
122 g_assert_cmpstr ("si-node", ==, str);
123 }
124
125 /* activate */
126
127 wp_object_activate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_ACTIVE,
128 NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
129 g_main_loop_run (f->base.loop);
130 g_assert_cmphex (wp_object_get_active_features (WP_OBJECT (item)), ==,
131 WP_SESSION_ITEM_FEATURE_ACTIVE);
132 g_assert_cmphex (wp_object_get_active_features (WP_OBJECT (node)), ==,
133 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL | WP_NODE_FEATURE_PORTS);
134
135 if (data->expected_direction == WP_DIRECTION_INPUT)
136 g_assert_cmpuint (wp_node_get_n_input_ports (node, NULL), ==, 1);
137 else
138 g_assert_cmpuint (wp_node_get_n_output_ports (node, NULL), ==, 1);
139 g_assert_cmpuint (wp_node_get_n_ports (node), ==, 1);
140
141 {
142 guint32 node_id, port_id, channel;
143 g_autoptr (GVariant) v =
144 wp_si_linkable_get_ports (WP_SI_LINKABLE (item),
145 (data->expected_direction == WP_DIRECTION_INPUT) ? "input" : "output");
146
147 g_assert_true (g_variant_is_of_type (v, G_VARIANT_TYPE ("a(uuu)")));
148 g_assert_cmpint (g_variant_n_children (v), ==, 1);
149 g_variant_get_child (v, 0, "(uuu)", &node_id, &port_id, &channel);
150 g_assert_cmpuint (node_id, ==, wp_proxy_get_bound_id (WP_PROXY (node)));
151 g_assert_cmpuint (channel, ==, 0);
152
153 {
154 g_autoptr (WpIterator) it = wp_node_new_ports_iterator (node);
155 g_auto (GValue) val = G_VALUE_INIT;
156 WpProxy *port;
157
158 g_assert_true (wp_iterator_next (it, &val));
159 port = g_value_get_object (&val);
160 g_assert_nonnull (port);
161 g_assert_cmpuint (port_id, ==, wp_proxy_get_bound_id (port));
162 }
163 }
164
165 /* deactivate - configuration should not be altered */
166
167 wp_object_deactivate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_ACTIVE);
168
169 g_assert_cmphex (wp_object_get_active_features (WP_OBJECT (item)), ==, 0);
170 g_assert_true (wp_session_item_is_configured (item));
171 g_assert_cmphex (wp_object_get_active_features (WP_OBJECT (node)), ==,
172 WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL | WP_NODE_FEATURE_PORTS);
173
174 {
175 const gchar *str = NULL;
176 g_autoptr (WpProperties) props = wp_session_item_get_properties (item);
177 g_assert_nonnull (props);
178 str = wp_properties_get (props, "media.class");
179 g_assert_nonnull (str);
180 g_assert_cmpstr (data->expected_media_class, ==, str);
181 str = wp_properties_get (props, "item.factory.name");
182 g_assert_nonnull (str);
183 g_assert_cmpstr ("si-node", ==, str);
184 }
185
186 /* reset - configuration resets */
187
188 wp_session_item_reset (item);
189 g_assert_false (wp_session_item_is_configured (item));
190
191 {
192 g_autoptr (WpProperties) props =
193 wp_session_item_get_properties (item);
194 g_assert_null (props);
195 }
196 }
197
198 gint
199 main (gint argc, gchar *argv[])
200 {
201 g_test_init (&argc, &argv, NULL);
202 wp_init (WP_INIT_ALL);
203
204 /* data */
205
206 const TestData nullsink_data = {
207 "support.null-audio-sink", "nullsink0", "Fake/Sink", "Fake/Sink", WP_DIRECTION_INPUT
208 };
209 const TestData audiotestsrc_data = {
210 "audiotestsrc", "audiotestsrc0", "Audio/Source", "Audio/Source", WP_DIRECTION_OUTPUT
211 };
212
213 /* configure-activate */
214
215 g_test_add ("/modules/si-node/configure-activate/nullsink",
216 TestFixture, &nullsink_data,
217 test_si_node_setup,
218 test_si_node_configure_activate,
219 test_si_node_teardown);
220
221 g_test_add ("/modules/si-node/configure-activate/audiotestsrc",
222 TestFixture, &audiotestsrc_data,
223 test_si_node_setup,
224 test_si_node_configure_activate,
225 test_si_node_teardown);
226
227 return g_test_run ();
228 }