mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-01 22:58:50 -04:00
tests: add filter unit test
This commit is contained in:
parent
4c3f5a7655
commit
e92e90bd3f
3 changed files with 236 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ test_apps = [
|
|||
'test-interfaces',
|
||||
# 'test-remote',
|
||||
'test-stream',
|
||||
'test-filter',
|
||||
]
|
||||
|
||||
foreach a : test_apps
|
||||
|
|
|
|||
233
src/tests/test-filter.c
Normal file
233
src/tests/test-filter.c
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
/* PipeWire
|
||||
*
|
||||
* Copyright © 2022 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pipewire/pipewire.h>
|
||||
#include <pipewire/main-loop.h>
|
||||
#include <pipewire/filter.h>
|
||||
|
||||
#include <spa/utils/string.h>
|
||||
|
||||
#define TEST_FUNC(a,b,func) \
|
||||
do { \
|
||||
a.func = b.func; \
|
||||
spa_assert_se(SPA_PTRDIFF(&a.func, &a) == SPA_PTRDIFF(&b.func, &b)); \
|
||||
} while(0)
|
||||
|
||||
static void test_abi(void)
|
||||
{
|
||||
static const struct {
|
||||
uint32_t version;
|
||||
void (*destroy) (void *data);
|
||||
void (*state_changed) (void *data, enum pw_filter_state old,
|
||||
enum pw_filter_state state, const char *error);
|
||||
void (*io_changed) (void *data, void *port_data, uint32_t id, void *area, uint32_t size);
|
||||
void (*param_changed) (void *data, void *port_data, uint32_t id, const struct spa_pod *param);
|
||||
void (*add_buffer) (void *data, void *port_data, struct pw_buffer *buffer);
|
||||
void (*remove_buffer) (void *data, void *port_data, struct pw_buffer *buffer);
|
||||
void (*process) (void *data, struct spa_io_position *position);
|
||||
void (*drained) (void *data);
|
||||
void (*command) (void *data, const struct spa_command *command);
|
||||
} test = { PW_VERSION_FILTER_EVENTS, NULL };
|
||||
|
||||
struct pw_filter_events ev;
|
||||
|
||||
TEST_FUNC(ev, test, destroy);
|
||||
TEST_FUNC(ev, test, state_changed);
|
||||
TEST_FUNC(ev, test, io_changed);
|
||||
TEST_FUNC(ev, test, param_changed);
|
||||
TEST_FUNC(ev, test, add_buffer);
|
||||
TEST_FUNC(ev, test, remove_buffer);
|
||||
TEST_FUNC(ev, test, process);
|
||||
TEST_FUNC(ev, test, drained);
|
||||
TEST_FUNC(ev, test, command);
|
||||
|
||||
spa_assert_se(PW_VERSION_FILTER_EVENTS == 1);
|
||||
spa_assert_se(sizeof(ev) == sizeof(test));
|
||||
|
||||
spa_assert_se(PW_FILTER_STATE_ERROR == -1);
|
||||
spa_assert_se(PW_FILTER_STATE_UNCONNECTED == 0);
|
||||
spa_assert_se(PW_FILTER_STATE_CONNECTING == 1);
|
||||
spa_assert_se(PW_FILTER_STATE_PAUSED == 2);
|
||||
spa_assert_se(PW_FILTER_STATE_STREAMING == 3);
|
||||
|
||||
spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_ERROR) != NULL);
|
||||
spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_UNCONNECTED) != NULL);
|
||||
spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_CONNECTING) != NULL);
|
||||
spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_PAUSED) != NULL);
|
||||
spa_assert_se(pw_filter_state_as_string(PW_FILTER_STATE_STREAMING) != NULL);
|
||||
}
|
||||
|
||||
static void filter_destroy_error(void *data)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
static void filter_state_changed_error(void *data, enum pw_filter_state old,
|
||||
enum pw_filter_state state, const char *error)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
static void filter_io_changed_error(void *data, void *port_data, uint32_t id, void *area, uint32_t size)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
static void filter_param_changed_error(void *data, void *port_data, uint32_t id, const struct spa_pod *format)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
static void filter_add_buffer_error(void *data, void *port_data, struct pw_buffer *buffer)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
static void filter_remove_buffer_error(void *data, void *port_data, struct pw_buffer *buffer)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
static void filter_process_error(void *data, struct spa_io_position *position)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
static void filter_drained_error(void *data)
|
||||
{
|
||||
spa_assert_not_reached();
|
||||
}
|
||||
|
||||
static const struct pw_filter_events filter_events_error =
|
||||
{
|
||||
PW_VERSION_FILTER_EVENTS,
|
||||
.destroy = filter_destroy_error,
|
||||
.state_changed = filter_state_changed_error,
|
||||
.io_changed = filter_io_changed_error,
|
||||
.param_changed = filter_param_changed_error,
|
||||
.add_buffer = filter_add_buffer_error,
|
||||
.remove_buffer = filter_remove_buffer_error,
|
||||
.process = filter_process_error,
|
||||
.drained = filter_drained_error
|
||||
};
|
||||
|
||||
static int destroy_count = 0;
|
||||
static void filter_destroy_count(void *data)
|
||||
{
|
||||
destroy_count++;
|
||||
}
|
||||
static void test_create(void)
|
||||
{
|
||||
struct pw_main_loop *loop;
|
||||
struct pw_context *context;
|
||||
struct pw_core *core;
|
||||
struct pw_filter *filter;
|
||||
struct pw_filter_events filter_events = filter_events_error;
|
||||
struct spa_hook listener = { 0, };
|
||||
const char *error = NULL;
|
||||
|
||||
loop = pw_main_loop_new(NULL);
|
||||
context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 12);
|
||||
spa_assert_se(context != NULL);
|
||||
core = pw_context_connect_self(context, NULL, 0);
|
||||
spa_assert_se(core != NULL);
|
||||
filter = pw_filter_new(core, "test", NULL);
|
||||
spa_assert_se(filter != NULL);
|
||||
pw_filter_add_listener(filter, &listener, &filter_events, filter);
|
||||
|
||||
/* check state */
|
||||
spa_assert_se(pw_filter_get_state(filter, &error) == PW_FILTER_STATE_UNCONNECTED);
|
||||
spa_assert_se(error == NULL);
|
||||
/* check name */
|
||||
spa_assert_se(spa_streq(pw_filter_get_name(filter), "test"));
|
||||
|
||||
/* check id, only when connected */
|
||||
spa_assert_se(pw_filter_get_node_id(filter) == SPA_ID_INVALID);
|
||||
|
||||
/* check destroy */
|
||||
destroy_count = 0;
|
||||
filter_events.destroy = filter_destroy_count;
|
||||
pw_filter_destroy(filter);
|
||||
spa_assert_se(destroy_count == 1);
|
||||
|
||||
pw_context_destroy(context);
|
||||
pw_main_loop_destroy(loop);
|
||||
}
|
||||
|
||||
static void test_properties(void)
|
||||
{
|
||||
struct pw_main_loop *loop;
|
||||
struct pw_context *context;
|
||||
struct pw_core *core;
|
||||
const struct pw_properties *props;
|
||||
struct pw_filter *filter;
|
||||
struct pw_filter_events filter_events = filter_events_error;
|
||||
struct spa_hook listener = { { NULL }, };
|
||||
struct spa_dict_item items[3];
|
||||
|
||||
loop = pw_main_loop_new(NULL);
|
||||
context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 12);
|
||||
spa_assert_se(context != NULL);
|
||||
core = pw_context_connect_self(context, NULL, 0);
|
||||
spa_assert_se(core != NULL);
|
||||
filter = pw_filter_new(core, "test",
|
||||
pw_properties_new("foo", "bar",
|
||||
"biz", "fuzz",
|
||||
NULL));
|
||||
spa_assert_se(filter != NULL);
|
||||
pw_filter_add_listener(filter, &listener, &filter_events, filter);
|
||||
|
||||
props = pw_filter_get_properties(filter, NULL);
|
||||
spa_assert_se(props != NULL);
|
||||
spa_assert_se(spa_streq(pw_properties_get(props, "foo"), "bar"));
|
||||
spa_assert_se(spa_streq(pw_properties_get(props, "biz"), "fuzz"));
|
||||
spa_assert_se(pw_properties_get(props, "buzz") == NULL);
|
||||
|
||||
/* remove foo */
|
||||
items[0] = SPA_DICT_ITEM_INIT("foo", NULL);
|
||||
/* change biz */
|
||||
items[1] = SPA_DICT_ITEM_INIT("biz", "buzz");
|
||||
/* add buzz */
|
||||
items[2] = SPA_DICT_ITEM_INIT("buzz", "frizz");
|
||||
pw_filter_update_properties(filter, NULL, &SPA_DICT_INIT(items, 3));
|
||||
|
||||
spa_assert_se(props == pw_filter_get_properties(filter, NULL));
|
||||
spa_assert_se(pw_properties_get(props, "foo") == NULL);
|
||||
spa_assert_se(spa_streq(pw_properties_get(props, "biz"), "buzz"));
|
||||
spa_assert_se(spa_streq(pw_properties_get(props, "buzz"), "frizz"));
|
||||
|
||||
/* check destroy */
|
||||
destroy_count = 0;
|
||||
filter_events.destroy = filter_destroy_count;
|
||||
pw_context_destroy(context);
|
||||
spa_assert_se(destroy_count == 1);
|
||||
|
||||
pw_main_loop_destroy(loop);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pw_init(&argc, &argv);
|
||||
|
||||
test_abi();
|
||||
test_create();
|
||||
test_properties();
|
||||
|
||||
pw_deinit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -251,5 +251,7 @@ int main(int argc, char *argv[])
|
|||
test_create();
|
||||
test_properties();
|
||||
|
||||
pw_deinit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue