summaryrefslogtreecommitdiffstats
path: root/test/test-context.c
blob: 3625d35a0cf3a5aa53e96efc1ef515736a202653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* 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.
 */

#include "pwtest.h"

#include <spa/utils/string.h>
#include <spa/support/dbus.h>
#include <spa/support/cpu.h>

#include <pipewire/pipewire.h>
#include <pipewire/global.h>

#define TEST_FUNC(a,b,func)	\
do {				\
	a.func = b.func;	\
	pwtest_ptr_eq(SPA_PTRDIFF(&a.func, &a), SPA_PTRDIFF(&b.func, &b)); \
} while(0)

PWTEST(context_abi)
{
	struct pw_context_events ev;
	struct {
		uint32_t version;
		void (*destroy) (void *data);
		void (*free) (void *data);
		void (*check_access) (void *data, struct pw_impl_client *client);
		void (*global_added) (void *data, struct pw_global *global);
		void (*global_removed) (void *data, struct pw_global *global);
	} test = { PW_VERSION_CONTEXT_EVENTS, NULL };

	pw_init(0, NULL);

	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);

	pwtest_int_eq(PW_VERSION_CONTEXT_EVENTS, 0);
	pwtest_int_eq(sizeof(ev), sizeof(test));

	pw_deinit();

	return PWTEST_PASS;
}

static void context_destroy_error(void *data)
{
	pwtest_fail_if_reached();
}
static void context_free_error(void *data)
{
	pwtest_fail_if_reached();
}
static void context_check_access_error(void *data, struct pw_impl_client *client)
{
	pwtest_fail_if_reached();
}
static void context_global_added_error(void *data, struct pw_global *global)
{
	pwtest_fail_if_reached();
}
static void context_global_removed_error(void *data, struct pw_global *global)
{
	pwtest_fail_if_reached();
}

static const struct pw_context_events context_events_error =
{
	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,
};

static int destroy_count = 0;
static void context_destroy_count(void *data)
{
	destroy_count++;
}
static int free_count = 0;
static void context_free_count(void *data)
{
	free_count++;
}
static int global_removed_count = 0;
static void context_global_removed_count(void *data, struct pw_global *global)
{
	global_removed_count++;
}
static int context_foreach_count = 0;
static int context_foreach(void *data, struct pw_global *global)
{
	context_foreach_count++;
	return 0;
}
static int context_foreach_error(void *data, struct pw_global *global)
{
	context_foreach_count++;
	return -1;
}
PWTEST(context_create)
{
	struct pw_main_loop *loop;
	struct pw_context *context;
	struct spa_hook listener = { { NULL }, };
	struct pw_context_events context_events = context_events_error;
	int res;

	pw_init(0, NULL);

	loop = pw_main_loop_new(NULL);
	pwtest_ptr_notnull(loop);

	context = pw_context_new(pw_main_loop_get_loop(loop),
			pw_properties_new(
				PW_KEY_CONFIG_NAME, "null",
				NULL), 12);
	pwtest_ptr_notnull(context);
	pw_context_add_listener(context, &listener, &context_events, context);

	/* check main loop */
	pwtest_ptr_eq(pw_context_get_main_loop(context), pw_main_loop_get_loop(loop));
	/* check user data */
	pwtest_ptr_notnull(pw_context_get_user_data(context));

	/* iterate globals */
	pwtest_int_eq(context_foreach_count, 0);
	res = pw_context_for_each_global(context, context_foreach, context);
	pwtest_int_eq(res, 0);
	pwtest_int_eq(context_foreach_count, 2);
	res = pw_context_for_each_global(context, context_foreach_error, context);
	pwtest_int_eq(res, -1);
	pwtest_int_eq(context_foreach_count, 3);

	/* check destroy */
	context_events.destroy = context_destroy_count;
	context_events.free = context_free_count;
	context_events.global_removed = context_global_removed_count;

	pwtest_int_eq(destroy_count, 0);
	pwtest_int_eq(free_count, 0);
	pwtest_int_eq(global_removed_count, 0);
	pw_context_destroy(context);
	pwtest_int_eq(destroy_count, 1);
	pwtest_int_eq(free_count, 1);
	pwtest_int_eq(global_removed_count, 2);
	pw_main_loop_destroy(loop);

	pw_deinit();

	return PWTEST_PASS;
}

PWTEST(context_properties)
{
	struct pw_main_loop *loop;
	struct pw_context *context;
	const struct pw_properties *props;
	struct spa_hook listener = { { NULL }, };
	struct pw_context_events context_events = context_events_error;
	struct spa_dict_item items[3];

	pw_init(0, NULL);

	loop = pw_main_loop_new(NULL);
	context = pw_context_new(pw_main_loop_get_loop(loop),
			pw_properties_new("foo", "bar",
					  "biz", "fuzz",
					  NULL),
			0);
	pwtest_ptr_notnull(context);
	pwtest_ptr_null(pw_context_get_user_data(context));
	pw_context_add_listener(context, &listener, &context_events, context);

	props = pw_context_get_properties(context);
	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);

	/* 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));

	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");

	spa_hook_remove(&listener);
	pw_context_destroy(context);
	pw_main_loop_destroy(loop);

	pw_deinit();

	return PWTEST_PASS;
}

PWTEST(context_support)
{
	static const char * const types[] = {
		SPA_TYPE_INTERFACE_DataSystem,
		SPA_TYPE_INTERFACE_DataLoop,
		SPA_TYPE_INTERFACE_System,
		SPA_TYPE_INTERFACE_Loop,
		SPA_TYPE_INTERFACE_LoopUtils,
		SPA_TYPE_INTERFACE_Log,
#ifdef HAVE_DBUS
		SPA_TYPE_INTERFACE_DBus,
#endif
		SPA_TYPE_INTERFACE_CPU
	};

	struct pw_main_loop *loop;
	struct pw_context *context;
	const struct spa_support *support;
	uint32_t n_support;
	size_t i;

	pw_init(0, NULL);

	loop = pw_main_loop_new(NULL);
	context = pw_context_new(pw_main_loop_get_loop(loop), NULL, 0);

	support = pw_context_get_support(context, &n_support);
	pwtest_ptr_notnull(support);
	pwtest_int_gt(n_support, 0U);

	for (i = 0; i < SPA_N_ELEMENTS(types); i++) {
		pwtest_ptr_notnull(spa_support_find(support, n_support, types[i]));
	}

	pw_context_destroy(context);
	pw_main_loop_destroy(loop);

	pw_deinit();

	return PWTEST_PASS;
}

PWTEST_SUITE(context)
{
	pwtest_add(context_abi, PWTEST_NOARG);
	pwtest_add(context_create, PWTEST_NOARG);
	pwtest_add(context_properties, PWTEST_NOARG);
	pwtest_add(context_support, PWTEST_NOARG);

	return PWTEST_PASS;
}