pipewire/test/test-context.c

268 lines
7.4 KiB
C
Raw Normal View History

2019-01-08 16:38:34 +00:00
/* PipeWire
*
* Copyright © 2019 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.
*/
2021-06-04 01:15:04 +00:00
#include "pwtest.h"
#include <spa/utils/string.h>
#include <spa/support/dbus.h>
#include <spa/support/cpu.h>
2019-01-08 16:38:34 +00:00
#include <pipewire/pipewire.h>
#include <pipewire/global.h>
2019-01-08 16:38:34 +00:00
#define TEST_FUNC(a,b,func) \
do { \
a.func = b.func; \
2021-06-04 01:15:04 +00:00
pwtest_ptr_eq(SPA_PTRDIFF(&a.func, &a), SPA_PTRDIFF(&b.func, &b)); \
2019-01-08 16:38:34 +00:00
} while(0)
2021-06-04 01:15:04 +00:00
PWTEST(context_abi)
2019-01-08 16:38:34 +00:00
{
struct pw_context_events ev;
2019-01-08 16:38:34 +00:00
struct {
uint32_t version;
void (*destroy) (void *data);
void (*free) (void *data);
2019-12-11 10:21:43 +00:00
void (*check_access) (void *data, struct pw_impl_client *client);
2019-01-08 16:38:34 +00:00
void (*global_added) (void *data, struct pw_global *global);
void (*global_removed) (void *data, struct pw_global *global);
} test = { PW_VERSION_CONTEXT_EVENTS, NULL };
2019-01-08 16:38:34 +00:00
2021-06-04 01:15:04 +00:00
pw_init(0, NULL);
2019-01-08 16:38:34 +00:00
TEST_FUNC(ev, test, destroy);
TEST_FUNC(ev, test, free);
TEST_FUNC(ev, test, check_access);
TEST_FUNC(ev, test, global_added);
TEST_FUNC(ev, test, global_removed);
2021-06-04 01:15:04 +00:00
pwtest_int_eq(PW_VERSION_CONTEXT_EVENTS, 0);
pwtest_int_eq(sizeof(ev), sizeof(test));
return PWTEST_PASS;
2019-01-08 16:38:34 +00:00
}
static void context_destroy_error(void *data)
2019-01-08 16:38:34 +00:00
{
2021-06-04 01:15:04 +00:00
pwtest_fail_if_reached();
2019-01-08 16:38:34 +00:00
}
static void context_free_error(void *data)
2019-01-08 16:38:34 +00:00
{
2021-06-04 01:15:04 +00:00
pwtest_fail_if_reached();
2019-01-08 16:38:34 +00:00
}
2019-12-11 10:21:43 +00:00
static void context_check_access_error(void *data, struct pw_impl_client *client)
2019-01-08 16:38:34 +00:00
{
2021-06-04 01:15:04 +00:00
pwtest_fail_if_reached();
2019-01-08 16:38:34 +00:00
}
static void context_global_added_error(void *data, struct pw_global *global)
2019-01-08 16:38:34 +00:00
{
2021-06-04 01:15:04 +00:00
pwtest_fail_if_reached();
2019-01-08 16:38:34 +00:00
}
static void context_global_removed_error(void *data, struct pw_global *global)
2019-01-08 16:38:34 +00:00
{
2021-06-04 01:15:04 +00:00
pwtest_fail_if_reached();
2019-01-08 16:38:34 +00:00
}
static const struct pw_context_events context_events_error =
2019-01-08 16:38:34 +00:00
{
PW_VERSION_CONTEXT_EVENTS,
.destroy = context_destroy_error,
.free = context_free_error,
.check_access = context_check_access_error,
.global_added = context_global_added_error,
.global_removed = context_global_removed_error,
2019-01-08 16:38:34 +00:00
};
static int destroy_count = 0;
static void context_destroy_count(void *data)
2019-01-08 16:38:34 +00:00
{
destroy_count++;
}
static int free_count = 0;
static void context_free_count(void *data)
2019-01-08 16:38:34 +00:00
{
free_count++;
}
static int global_removed_count = 0;
static void context_global_removed_count(void *data, struct pw_global *global)
2019-01-08 16:38:34 +00:00
{
global_removed_count++;
}
static int context_foreach_count = 0;
static int context_foreach(void *data, struct pw_global *global)
2019-01-08 16:38:34 +00:00
{
context_foreach_count++;
2019-01-08 16:38:34 +00:00
return 0;
}
static int context_foreach_error(void *data, struct pw_global *global)
2019-01-08 16:38:34 +00:00
{
context_foreach_count++;
2019-01-08 16:38:34 +00:00
return -1;
}
2021-06-04 01:15:04 +00:00
PWTEST(context_create)
2019-01-08 16:38:34 +00:00
{
struct pw_main_loop *loop;
struct pw_context *context;
2019-01-08 16:38:34 +00:00
struct spa_hook listener = { NULL, };
struct pw_context_events context_events = context_events_error;
2019-01-08 16:38:34 +00:00
int res;
2021-06-04 01:15:04 +00:00
pw_init(0, NULL);
2019-01-08 16:38:34 +00:00
loop = pw_main_loop_new(NULL);
2021-06-04 01:15:04 +00:00
pwtest_ptr_notnull(loop);
2019-01-08 16:38:34 +00:00
context = pw_context_new(pw_main_loop_get_loop(loop),
2019-12-06 16:12:07 +00:00
pw_properties_new(
PW_KEY_CONFIG_NAME, "null",
2019-12-06 16:12:07 +00:00
NULL), 12);
2021-06-04 01:15:04 +00:00
pwtest_ptr_notnull(context);
pw_context_add_listener(context, &listener, &context_events, context);
2019-01-08 16:38:34 +00:00
/* check main loop */
2021-06-04 01:15:04 +00:00
pwtest_ptr_eq(pw_context_get_main_loop(context), pw_main_loop_get_loop(loop));
2019-01-08 16:38:34 +00:00
/* check user data */
2021-06-04 01:15:04 +00:00
pwtest_ptr_notnull(pw_context_get_user_data(context));
2019-01-08 16:38:34 +00:00
/* iterate globals */
2021-06-04 01:15:04 +00:00
pwtest_int_eq(context_foreach_count, 0);
res = pw_context_for_each_global(context, context_foreach, context);
2021-06-04 01:15:04 +00:00
pwtest_int_eq(res, 0);
pwtest_int_eq(context_foreach_count, 2);
res = pw_context_for_each_global(context, context_foreach_error, context);
2021-06-04 01:15:04 +00:00
pwtest_int_eq(res, -1);
pwtest_int_eq(context_foreach_count, 3);
2019-01-08 16:38:34 +00:00
/* check destroy */
context_events.destroy = context_destroy_count;
context_events.free = context_free_count;
context_events.global_removed = context_global_removed_count;
2019-01-08 16:38:34 +00:00
2021-06-04 01:15:04 +00:00
pwtest_int_eq(destroy_count, 0);
pwtest_int_eq(free_count, 0);
pwtest_int_eq(global_removed_count, 0);
pw_context_destroy(context);
2021-06-04 01:15:04 +00:00
pwtest_int_eq(destroy_count, 1);
pwtest_int_eq(free_count, 1);
pwtest_int_eq(global_removed_count, 2);
2019-01-08 16:38:34 +00:00
pw_main_loop_destroy(loop);
2021-06-04 01:15:04 +00:00
return PWTEST_PASS;
2019-01-08 16:38:34 +00:00
}
2021-06-04 01:15:04 +00:00
PWTEST(context_properties)
2019-01-08 16:38:34 +00:00
{
struct pw_main_loop *loop;
struct pw_context *context;
2019-01-08 16:38:34 +00:00
const struct pw_properties *props;
struct spa_hook listener = { NULL, };
struct pw_context_events context_events = context_events_error;
2019-01-08 16:38:34 +00:00
struct spa_dict_item items[3];
2021-06-04 01:15:04 +00:00
pw_init(0, NULL);
2019-01-08 16:38:34 +00:00
loop = pw_main_loop_new(NULL);
context = pw_context_new(pw_main_loop_get_loop(loop),
2019-01-08 16:38:34 +00:00
pw_properties_new("foo", "bar",
"biz", "fuzz",
NULL),
0);
2021-06-04 01:15:04 +00:00
pwtest_ptr_notnull(context);
pwtest_ptr_null(pw_context_get_user_data(context));
pw_context_add_listener(context, &listener, &context_events, context);
2019-01-08 16:38:34 +00:00
props = pw_context_get_properties(context);
2021-06-04 01:15:04 +00:00
pwtest_ptr_notnull(props);
pwtest_str_eq(pw_properties_get(props, "foo"), "bar");
pwtest_str_eq(pw_properties_get(props, "biz"), "fuzz");
pwtest_str_eq(pw_properties_get(props, "buzz"), NULL);
2019-01-08 16:38:34 +00:00
/* 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_context_update_properties(context, &SPA_DICT_INIT(items, 3));
2019-01-08 16:38:34 +00:00
2021-06-04 01:15:04 +00:00
pwtest_ptr_eq(props, pw_context_get_properties(context));
pwtest_str_eq(pw_properties_get(props, "foo"), NULL);
pwtest_str_eq(pw_properties_get(props, "biz"), "buzz");
pwtest_str_eq(pw_properties_get(props, "buzz"), "frizz");
2019-01-08 16:38:34 +00:00
spa_hook_remove(&listener);
pw_context_destroy(context);
2019-01-08 16:38:34 +00:00
pw_main_loop_destroy(loop);
2021-06-04 01:15:04 +00:00
return PWTEST_PASS;
2019-01-08 16:38:34 +00:00
}
2021-06-04 01:15:04 +00:00
PWTEST(context_support)
2019-01-08 16:38:34 +00:00
{
static const char * const types[] = {
2019-06-06 14:40:15 +00:00
SPA_TYPE_INTERFACE_DataSystem,
2019-01-08 16:38:34 +00:00
SPA_TYPE_INTERFACE_DataLoop,
2019-06-06 14:40:15 +00:00
SPA_TYPE_INTERFACE_System,
SPA_TYPE_INTERFACE_Loop,
2019-01-08 16:38:34 +00:00
SPA_TYPE_INTERFACE_LoopUtils,
SPA_TYPE_INTERFACE_Log,
SPA_TYPE_INTERFACE_DBus,
SPA_TYPE_INTERFACE_CPU
};
struct pw_main_loop *loop;
struct pw_context *context;
const struct spa_support *support;
uint32_t n_support;
2019-01-08 16:38:34 +00:00
size_t i;
2021-06-04 01:15:04 +00:00
pw_init(0, NULL);
2019-01-08 16:38:34 +00:00
loop = pw_main_loop_new(NULL);
context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 0);
2019-01-08 16:38:34 +00:00
support = pw_context_get_support(context, &n_support);
2021-06-04 01:15:04 +00:00
pwtest_ptr_notnull(support);
pwtest_int_gt(n_support, 0U);
2019-01-08 16:38:34 +00:00
for (i = 0; i < SPA_N_ELEMENTS(types); i++) {
2021-06-04 01:15:04 +00:00
pwtest_ptr_notnull(spa_support_find(support, n_support, types[i]));
2019-01-08 16:38:34 +00:00
}
pw_context_destroy(context);
2019-01-08 16:38:34 +00:00
pw_main_loop_destroy(loop);
2021-06-04 01:15:04 +00:00
return PWTEST_PASS;
2019-01-08 16:38:34 +00:00
}
2021-06-04 01:15:04 +00:00
PWTEST_SUITE(context)
2019-01-08 16:38:34 +00:00
{
2021-06-04 01:15:04 +00:00
pwtest_add(context_abi, PWTEST_NOARG);
pwtest_add(context_create, PWTEST_NOARG);
pwtest_add(context_properties, PWTEST_NOARG);
pwtest_add(context_support, PWTEST_NOARG);
2019-01-08 16:38:34 +00:00
2021-06-04 01:15:04 +00:00
return PWTEST_PASS;
2019-01-08 16:38:34 +00:00
}