| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "../common/base-test-fixture.h" |
| 10 |
#include <spa/param/audio/format-utils.h> |
| 11 |
|
| 12 |
#define DEFAULT_RATE 44100 |
| 13 |
#define DEFAULT_CHANNELS 2 |
| 14 |
|
| 15 |
typedef struct _ScriptRunnerFixture ScriptRunnerFixture; |
| 16 |
static void load_component (ScriptRunnerFixture *f, const gchar *name, |
| 17 |
const gchar *type); |
| 18 |
|
| 19 |
struct _WpScriptTester |
| 20 |
{ |
| 21 |
WpPlugin parent; |
| 22 |
struct pw_stream *stream; |
| 23 |
ScriptRunnerFixture *test_fixture; |
| 24 |
}; |
| 25 |
|
| 26 |
enum { |
| 27 |
ACTION_CREATE_STREAM_NODE, |
| 28 |
N_SIGNALS |
| 29 |
}; |
| 30 |
|
| 31 |
enum { |
| 32 |
PROP_0, |
| 33 |
PROP_TEST_FIXTURE, |
| 34 |
PROP_SUPPORTED_FEATURES, |
| 35 |
}; |
| 36 |
|
| 37 |
static guint signals [N_SIGNALS] = { 0 }; |
| 38 |
|
| 39 |
/* plugin for lua test scripts to trigger stream node creation, after all the |
| 40 |
* device nodes are created and ready. |
| 41 |
*/ |
| 42 |
G_DECLARE_FINAL_TYPE (WpScriptTester, wp_script_tester, |
| 43 |
WP, SCRIPT_TESTER, WpPlugin) |
| 44 |
G_DEFINE_TYPE (WpScriptTester, wp_script_tester, WP_TYPE_PLUGIN) |
| 45 |
|
| 46 |
struct _ScriptRunnerFixture { |
| 47 |
WpBaseTestFixture base; |
| 48 |
WpScriptTester *plugin; |
| 49 |
}; |
| 50 |
|
| 51 |
static void |
| 52 |
wp_script_tester_init (WpScriptTester *self) |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
static G_GNUC_UNUSED void |
| 57 |
dummy_cb (WpObject *object, GAsyncResult *res, WpBaseTestFixture *f) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
wp_script_tester_restart_plugin (WpScriptTester *self, const gchar *name) |
| 63 |
{ |
| 64 |
ScriptRunnerFixture *f = self->test_fixture; |
| 65 |
g_autoptr (WpPlugin) plugin = wp_plugin_find (f->base.core, name); |
| 66 |
|
| 67 |
wp_object_deactivate (WP_OBJECT (plugin), WP_PLUGIN_FEATURE_ENABLED); |
| 68 |
wp_object_activate (WP_OBJECT (plugin), WP_PLUGIN_FEATURE_ENABLED, |
| 69 |
NULL, (GAsyncReadyCallback) dummy_cb, f); |
| 70 |
} |
| 71 |
static void |
| 72 |
wp_script_tester_create_stream (WpScriptTester *self, const gchar *stream_type, |
| 73 |
WpProperties *stream_props) |
| 74 |
{ |
| 75 |
ScriptRunnerFixture *f = self->test_fixture; |
| 76 |
g_autoptr (WpProperties) props = NULL; |
| 77 |
const struct spa_pod *params [1]; |
| 78 |
uint8_t buffer [1024]; |
| 79 |
struct spa_pod_builder b = SPA_POD_BUILDER_INIT (buffer, sizeof (buffer)); |
| 80 |
int direction; |
| 81 |
|
| 82 |
wp_info ("create stream_type(%s) with props(%p)", stream_type, stream_props); |
| 83 |
|
| 84 |
if (g_str_equal (stream_type, "playback")) |
| 85 |
direction = PW_DIRECTION_OUTPUT; |
| 86 |
else |
| 87 |
direction = PW_DIRECTION_INPUT; |
| 88 |
|
| 89 |
props = wp_properties_new ( |
| 90 |
PW_KEY_MEDIA_TYPE, "Audio", |
| 91 |
PW_KEY_NODE_NAME, "stream-node", |
| 92 |
NULL); |
| 93 |
|
| 94 |
if (stream_props) |
| 95 |
wp_properties_add (props, stream_props); |
| 96 |
|
| 97 |
self->stream = pw_stream_new ( |
| 98 |
wp_core_get_pw_core (f->base.client_core), |
| 99 |
"stream-node", wp_properties_unref_and_take_pw_properties (g_steal_pointer (&props))); |
| 100 |
|
| 101 |
params [0] = spa_format_audio_raw_build (&b, SPA_PARAM_EnumFormat, |
| 102 |
&SPA_AUDIO_INFO_RAW_INIT ( |
| 103 |
.format = SPA_AUDIO_FORMAT_F32, |
| 104 |
.channels = DEFAULT_CHANNELS, |
| 105 |
.rate = DEFAULT_RATE)); |
| 106 |
|
| 107 |
pw_stream_connect (self->stream, |
| 108 |
direction, |
| 109 |
PW_ID_ANY, |
| 110 |
PW_STREAM_FLAG_AUTOCONNECT | |
| 111 |
PW_STREAM_FLAG_MAP_BUFFERS, |
| 112 |
params, 1); |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
wp_script_tester_set_property (GObject *object, guint property_id, |
| 117 |
const GValue *value, GParamSpec *pspec) |
| 118 |
{ |
| 119 |
WpScriptTester *self = WP_SCRIPT_TESTER (object); |
| 120 |
|
| 121 |
switch (property_id) { |
| 122 |
case PROP_TEST_FIXTURE: |
| 123 |
self->test_fixture = g_value_get_pointer (value); |
| 124 |
break; |
| 125 |
default: |
| 126 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
wp_script_tester_get_property (GObject *object, guint property_id, GValue *value, |
| 133 |
GParamSpec *pspec) |
| 134 |
{ |
| 135 |
WpScriptTester *self = WP_SCRIPT_TESTER (object); |
| 136 |
|
| 137 |
switch (property_id) { |
| 138 |
case PROP_TEST_FIXTURE: |
| 139 |
g_value_set_pointer (value, self->test_fixture); |
| 140 |
break; |
| 141 |
default: |
| 142 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
wp_script_tester_class_init (WpScriptTesterClass *klass) |
| 149 |
{ |
| 150 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 151 |
object_class->get_property = wp_script_tester_get_property; |
| 152 |
object_class->set_property = wp_script_tester_set_property; |
| 153 |
|
| 154 |
g_object_class_install_property (object_class, PROP_TEST_FIXTURE, |
| 155 |
g_param_spec_pointer ("test-fixture", "test-fixture", "The Test Fixture", |
| 156 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 157 |
|
| 158 |
signals [ACTION_CREATE_STREAM_NODE] = g_signal_new_class_handler ( |
| 159 |
"create-stream", G_TYPE_FROM_CLASS (klass), |
| 160 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 161 |
(GCallback) wp_script_tester_create_stream, |
| 162 |
NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_STRING, WP_TYPE_PROPERTIES); |
| 163 |
|
| 164 |
signals [ACTION_CREATE_STREAM_NODE] = g_signal_new_class_handler ( |
| 165 |
"restart-plugin", G_TYPE_FROM_CLASS (klass), |
| 166 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 167 |
(GCallback) wp_script_tester_restart_plugin, |
| 168 |
NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_STRING); |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
on_plugin_loaded (WpCore * core, GAsyncResult * res, ScriptRunnerFixture *f) |
| 174 |
{ |
| 175 |
gboolean loaded; |
| 176 |
GError *error = NULL; |
| 177 |
|
| 178 |
loaded = wp_core_load_component_finish (core, res, &error); |
| 179 |
g_assert_no_error (error); |
| 180 |
g_assert_true (loaded); |
| 181 |
|
| 182 |
g_main_loop_quit (f->base.loop); |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
load_component (ScriptRunnerFixture *f, const gchar *name, const gchar *type) |
| 187 |
{ |
| 188 |
g_autoptr (WpSpaJson) arguments = NULL; |
| 189 |
|
| 190 |
if (g_str_equal (name, "metadata.lua")) { |
| 191 |
arguments = wp_spa_json_new_object ("metadata.name", "s", "default", |
| 192 |
NULL); |
| 193 |
} |
| 194 |
|
| 195 |
wp_core_load_component (f->base.core, name, type, arguments, NULL, NULL, |
| 196 |
(GAsyncReadyCallback) on_plugin_loaded, f); |
| 197 |
g_main_loop_run (f->base.loop); |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
script_run (ScriptRunnerFixture *f, gconstpointer argv) |
| 202 |
{ |
| 203 |
gchar **args = (gchar **) argv; |
| 204 |
const gchar *test_script = args [2]; |
| 205 |
|
| 206 |
/* load the test script */ |
| 207 |
load_component (f, (const gchar *) test_script, "script/lua"); |
| 208 |
} |
| 209 |
|
| 210 |
static void |
| 211 |
load_components (ScriptRunnerFixture *f, gconstpointer argv) |
| 212 |
{ |
| 213 |
g_autoptr (WpPlugin) plugin = NULL; |
| 214 |
g_autoptr (GError) error = NULL; |
| 215 |
g_autofree gchar *pluginname = NULL; |
| 216 |
gchar **args = (gchar **) argv; |
| 217 |
gchar *test_suite = args [1]; |
| 218 |
/* TODO: we could do some more stuff here to provide the test script with an |
| 219 |
API to deal with the main loop and test asynchronous stuff, if necessary */ |
| 220 |
|
| 221 |
load_component (f, "libwireplumber-module-lua-scripting", "module"); |
| 222 |
|
| 223 |
load_component (f, "libwireplumber-module-settings", "module"); |
| 224 |
load_component (f, "settings-instance", "built-in"); |
| 225 |
|
| 226 |
if (g_str_equal (test_suite, "script-tests")) { |
| 227 |
|
| 228 |
load_component (f, "libwireplumber-module-standard-event-source", "module"); |
| 229 |
|
| 230 |
load_component (f, "libwireplumber-module-si-audio-adapter", "module"); |
| 231 |
load_component (f, "libwireplumber-module-si-standard-link", "module"); |
| 232 |
|
| 233 |
load_component (f, "default-nodes/apply-default-node.lua", "script/lua"); |
| 234 |
load_component (f, "default-nodes/state-default-nodes.lua", "script/lua"); |
| 235 |
load_component (f, "default-nodes/find-best-default-node.lua", "script/lua"); |
| 236 |
load_component (f, "default-nodes/rescan.lua", "script/lua"); |
| 237 |
|
| 238 |
load_component (f, "metadata.lua", "script/lua"); |
| 239 |
load_component (f, "libwireplumber-module-default-nodes-api", "module"); |
| 240 |
|
| 241 |
load_component (f, "node/create-item.lua", "script/lua"); |
| 242 |
|
| 243 |
load_component (f, "linking/find-best-target.lua", "script/lua"); |
| 244 |
load_component (f, "linking/find-default-target.lua", "script/lua"); |
| 245 |
load_component (f, "linking/find-defined-target.lua", "script/lua"); |
| 246 |
load_component (f, "linking/find-filter-target.lua", "script/lua"); |
| 247 |
load_component (f, "linking/find-media-role-target.lua", "script/lua"); |
| 248 |
load_component (f, "linking/get-filter-from-target.lua", "script/lua"); |
| 249 |
load_component (f, "linking/link-target.lua", "script/lua"); |
| 250 |
load_component (f, "linking/prepare-link.lua", "script/lua"); |
| 251 |
load_component (f, "linking/rescan.lua", "script/lua"); |
| 252 |
|
| 253 |
{ |
| 254 |
g_autoptr (WpTestServerLocker) lock = |
| 255 |
wp_test_server_locker_new (&f->base.server); |
| 256 |
|
| 257 |
g_assert_nonnull (pw_context_load_module (f->base.server.context, |
| 258 |
"libpipewire-module-adapter", NULL, NULL)); |
| 259 |
|
| 260 |
g_assert_nonnull (pw_context_load_module (f->base.server.context, |
| 261 |
"libpipewire-module-link-factory", NULL, NULL)); |
| 262 |
|
| 263 |
g_assert_cmpint (pw_context_add_spa_lib (f->base.server.context, |
| 264 |
"audiotestsrc", "audiotestsrc/libspa-audiotestsrc"), == , 0); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
base_tests_setup (ScriptRunnerFixture *f, gconstpointer data) |
| 271 |
{ |
| 272 |
f->base.conf_file = |
| 273 |
g_strdup_printf ("%s/settings/wireplumber.conf", g_getenv ("G_TEST_SRCDIR")); |
| 274 |
|
| 275 |
wp_base_test_fixture_setup (&f->base, WP_BASE_TEST_FLAG_CLIENT_CORE); |
| 276 |
|
| 277 |
load_components (f, data); |
| 278 |
} |
| 279 |
|
| 280 |
static void |
| 281 |
script_tests_setup (ScriptRunnerFixture *f, gconstpointer data) |
| 282 |
{ |
| 283 |
f->base.conf_file = |
| 284 |
g_strdup_printf ("%s/config/wireplumber.conf", g_getenv ("G_TEST_SRCDIR")); |
| 285 |
|
| 286 |
wp_base_test_fixture_setup (&f->base, WP_BASE_TEST_FLAG_CLIENT_CORE); |
| 287 |
|
| 288 |
load_components (f, data); |
| 289 |
|
| 290 |
f->plugin = g_object_new (wp_script_tester_get_type (), |
| 291 |
"name", "script-tester", |
| 292 |
"core", f->base.core, |
| 293 |
"test-fixture", f, |
| 294 |
NULL); |
| 295 |
|
| 296 |
wp_core_register_object (f->base.core, (WpPlugin *)f->plugin); |
| 297 |
} |
| 298 |
|
| 299 |
static void |
| 300 |
base_tests_teardown (ScriptRunnerFixture *f, gconstpointer data) |
| 301 |
{ |
| 302 |
wp_base_test_fixture_teardown (&f->base); |
| 303 |
} |
| 304 |
|
| 305 |
static void |
| 306 |
script_tests_teardown (ScriptRunnerFixture *f, gconstpointer data) |
| 307 |
{ |
| 308 |
wp_base_test_fixture_teardown (&f->base); |
| 309 |
} |
| 310 |
|
| 311 |
gint |
| 312 |
main (gint argc, gchar *argv[]) |
| 313 |
{ |
| 314 |
g_test_init (&argc, &argv, NULL); |
| 315 |
wp_init (WP_INIT_ALL); |
| 316 |
|
| 317 |
g_assert_cmpint (argc, >= , 1); |
| 318 |
gchar **args = (gchar **) argv; |
| 319 |
gchar *test_suite = args [1]; |
| 320 |
|
| 321 |
if (g_str_equal (test_suite, "script-tests")) |
| 322 |
g_test_add ("/lua/linking-tests", ScriptRunnerFixture, argv, |
| 323 |
script_tests_setup, script_run, script_tests_teardown); |
| 324 |
else |
| 325 |
g_test_add ("/lua/wprun/tests", ScriptRunnerFixture, argv, |
| 326 |
base_tests_setup, script_run, base_tests_teardown); |
| 327 |
|
| 328 |
return g_test_run (); |
| 329 |
} |