policy tests: facilitate Lua src tests in script-tester.c

This commit is contained in:
Ashok Sidipotu
2022-12-22 06:04:43 +05:30
committed by Julian Bouzas
parent a1e79cc84b
commit a8aeb1010e

View File

@@ -7,54 +7,44 @@
*/
#include "../common/base-test-fixture.h"
#include <spa/param/audio/format-utils.h>
#define DEFAULT_RATE 44100
#define DEFAULT_CHANNELS 2
typedef struct {
WpBaseTestFixture base;
struct pw_stream *stream;
} ScriptRunnerFixture;
#define TEST_METADATA_NAME "test-settings"
#define DEFAULT_METADATA_NAME "sm-settings"
static void
script_runner_setup (ScriptRunnerFixture *f, gconstpointer data)
{
f->base.conf_file =
g_strdup_printf ("%s/settings.conf", g_getenv ("G_TEST_SRCDIR"));
wp_base_test_fixture_setup (&f->base, 0);
}
static void
script_runner_teardown (ScriptRunnerFixture *f, gconstpointer data)
{
wp_base_test_fixture_teardown (&f->base);
}
static void
load_component (ScriptRunnerFixture *f, const gchar *name, const gchar *type,
GVariant *args)
load_component (ScriptRunnerFixture *f, const gchar *name, const gchar *type)
{
g_autofree gchar *component_name = NULL;
g_autofree gchar *plugin_name = NULL;
g_autoptr (WpPlugin) plugin = NULL;
g_autoptr (GError) error = NULL;
g_autoptr (WpSiFactory) si = NULL;
if (g_str_equal (type, "script/lua")) {
if ((g_str_equal (type, "script/lua")) &&
(g_file_test (name, G_FILE_TEST_EXISTS))) {
g_autofree gchar *filename = g_path_get_basename (name);
plugin_name = g_strdup_printf ("script:%s", (const gchar *) filename);
component_name = g_strdup (name);
} else if (g_str_equal (type, "script/lua")) {
component_name = g_strdup (name);
plugin_name = g_strdup_printf ("script:%s", (const gchar *) name);
}
else {
} else {
component_name = g_strdup_printf ("libwireplumber-module-%s", name);
plugin_name = g_strdup (name);
}
wp_core_load_component (f->base.core, component_name, type, args, &error);
wp_core_load_component (f->base.core, component_name, type, NULL, &error);
g_assert_no_error (error);
plugin = wp_plugin_find (f->base.core, plugin_name);
if (!g_str_has_prefix (name, "si")) {
g_autoptr (WpPlugin) plugin = wp_plugin_find (f->base.core, plugin_name);
wp_object_activate (WP_OBJECT (plugin), WP_PLUGIN_FEATURE_ENABLED,
NULL, (GAsyncReadyCallback) test_object_activate_finish_cb, f);
@@ -64,36 +54,118 @@ load_component (ScriptRunnerFixture *f, const gchar *name, const gchar *type,
static void
script_run (ScriptRunnerFixture *f, gconstpointer argv)
{
gchar **args = (gchar **) argv;
const gchar *test_script = args [2];
/* load the test script */
load_component (f, (const gchar *) test_script, "script/lua");
}
static void
load_components (ScriptRunnerFixture *f, gconstpointer argv)
{
g_autoptr (WpPlugin) plugin = NULL;
g_autoptr (GError) error = NULL;
g_autofree gchar *pluginname = NULL;
gchar **args = (gchar **) argv;
gchar *test_type = args [1];
gchar *test_script = args [2];
GVariant *metadata_name = NULL;
gchar *test_suite = args [1];
/* TODO: we could do some more stuff here to provide the test script with an
API to deal with the main loop and test asynchronous stuff, if necessary */
load_component (f, "lua-scripting", "module", NULL);
load_component (f, "lua-scripting", "module");
if (g_str_equal (test_type, "policy-tests")) {
metadata_name = g_variant_new_string (DEFAULT_METADATA_NAME);
load_component (f, "settings", "module", metadata_name);
load_component (f, "settings", "module");
load_component (f, "si-node", "module", NULL);
if (g_str_equal (test_suite, "script-tests")) {
load_component (f, "si-audio-adapter", "module", NULL);
load_component (f, "standard-event-source", "module");
load_component (f, "si-standard-link", "module", NULL);
load_component (f, "si-audio-adapter", "module");
load_component (f, "si-standard-link", "module");
load_component (f, "create-item.lua", "script/lua");
load_component (f, "policy-hooks.lua", "script/lua");
g_assert_nonnull (pw_context_load_module (f->base.server.context,
"libpipewire-module-adapter", NULL, NULL));
g_assert_nonnull (pw_context_load_module (f->base.server.context,
"libpipewire-module-link-factory", NULL, NULL));
g_assert_cmpint (pw_context_add_spa_lib (f->base.server.context,
"audiotestsrc", "audiotestsrc/libspa-audiotestsrc"), == , 0);
load_component (f, "policy-hooks.lua", "script/lua", NULL);
} else {
metadata_name = g_variant_new_string (TEST_METADATA_NAME);
load_component (f, "settings", "module", metadata_name);
}
/* load the test script */
load_component (f, (const gchar *) test_script, "script/lua", NULL);
}
static void
base_tests_setup (ScriptRunnerFixture *f, gconstpointer data)
{
f->base.conf_file =
g_strdup_printf ("%s/settings.conf", g_getenv ("G_TEST_SRCDIR"));
wp_base_test_fixture_setup (&f->base, WP_BASE_TEST_FLAG_CLIENT_CORE);
load_components (f, data);
}
static void
create_stream_node (ScriptRunnerFixture *f, gconstpointer argv)
{
gchar **args = (gchar **) argv;
gchar *test_case = args [2];
struct pw_properties *props;
const struct spa_pod *params [1];
uint8_t buffer [1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT (buffer, sizeof (buffer));
int direction = PW_DIRECTION_OUTPUT;
if (g_str_has_suffix (test_case, "capture.lua"))
direction = PW_DIRECTION_INPUT;
props = pw_properties_new (
PW_KEY_MEDIA_TYPE, "Audio",
PW_KEY_NODE_NAME, "stream-node",
NULL);
f->stream = pw_stream_new (
wp_core_get_pw_core (f->base.client_core),
"stream-node", props);
params [0] = spa_format_audio_raw_build (&b, SPA_PARAM_EnumFormat,
&SPA_AUDIO_INFO_RAW_INIT (
.format = SPA_AUDIO_FORMAT_F32,
.channels = DEFAULT_CHANNELS,
.rate = DEFAULT_RATE));
pw_stream_connect (f->stream,
direction,
PW_ID_ANY,
PW_STREAM_FLAG_AUTOCONNECT |
PW_STREAM_FLAG_MAP_BUFFERS,
params, 1);
}
static void
script_tests_setup (ScriptRunnerFixture *f, gconstpointer data)
{
base_tests_setup (f, data);
create_stream_node (f, data);
}
static void
base_tests_teardown (ScriptRunnerFixture *f, gconstpointer data)
{
wp_base_test_fixture_teardown (&f->base);
}
static void
script_tests_teardown (ScriptRunnerFixture *f, gconstpointer data)
{
base_tests_teardown (f, data);
}
gint
@@ -102,10 +174,16 @@ main (gint argc, gchar *argv[])
g_test_init (&argc, &argv, NULL);
wp_init (WP_INIT_ALL);
g_assert_cmpint (argc, >=, 1);
g_assert_cmpint (argc, >= , 1);
gchar **args = (gchar **) argv;
gchar *test_suite = args [1];
g_test_add ("/lua/script/run", ScriptRunnerFixture, argv,
script_runner_setup, script_run, script_runner_teardown);
if (g_str_equal (test_suite, "script-tests"))
g_test_add ("/lua/policy-tests", ScriptRunnerFixture, argv,
script_tests_setup, script_run, script_tests_teardown);
else
g_test_add ("/lua/wprun/tests", ScriptRunnerFixture, argv,
base_tests_setup, script_run, base_tests_teardown);
return g_test_run ();
}