summaryrefslogtreecommitdiff
path: root/src/tests/test-context.c
blob: ef1732ff431341554ec1f3f306c42155a4c24549 (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
278
279
280
281
/* 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 <pipewire/pipewire.h>
#include <pipewire/global.h>

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

static void test_abi(void)
{
	struct pw_context_events ev;
	struct {
		uint32_t version;
		void (*destroy) (void *data);
		void (*free) (void *data);
		void (*info_changed) (void *data, const struct pw_core_info *info);
		void (*check_access) (void *data, struct pw_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 };

	TEST_FUNC(ev, test, destroy);
	TEST_FUNC(ev, test, free);
	TEST_FUNC(ev, test, info_changed);
	TEST_FUNC(ev, test, check_access);
	TEST_FUNC(ev, test, global_added);
	TEST_FUNC(ev, test, global_removed);

	spa_assert(PW_VERSION_CONTEXT_EVENTS == 0);
	spa_assert(sizeof(ev) == sizeof(test));
}

static void context_destroy_error(void *data)
{
	spa_assert_not_reached();
}
static void context_free_error(void *data)
{
	spa_assert_not_reached();
}
static void context_info_changed_error(void *data, const struct pw_core_info *info)
{
	spa_assert_not_reached();
}
static void context_check_access_error(void *data, struct pw_client *client)
{
	spa_assert_not_reached();
}
static void context_global_added_error(void *data, struct pw_global *global)
{
	spa_assert_not_reached();
}
static void context_global_removed_error(void *data, struct pw_global *global)
{
	spa_assert_not_reached();
}

static const struct pw_context_events context_events_error =
{
	PW_VERSION_CONTEXT_EVENTS,
	.destroy = context_destroy_error,
	.free = context_free_error,
	.info_changed = context_info_changed_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;
}
static void test_create(void)
{
	struct pw_main_loop *loop;
	struct pw_context *context;
	struct spa_hook listener = { NULL, };
	struct pw_context_events context_events = context_events_error;
	struct pw_global *global;
	int res;

	loop = pw_main_loop_new(NULL);
	spa_assert(loop != NULL);

	context = pw_context_new(pw_main_loop_get_loop(loop),
			pw_properties_new(
				PW_KEY_CORE_PROFILE_MODULES, "none",
				NULL), 12);
	spa_assert(context != NULL);
	pw_context_add_listener(context, &listener, &context_events, context);

	/* check main loop */
	spa_assert(pw_context_get_main_loop(context) == pw_main_loop_get_loop(loop));
	/* check user data */
	spa_assert(pw_context_get_user_data(context) != NULL);

	/* check info */
	spa_assert(pw_context_get_info(context) != NULL);

	/* check global */
	global = pw_context_get_global(context);
	spa_assert(global != NULL);
	spa_assert(pw_context_find_global(context, 0) == global);
	spa_assert(pw_global_get_context(global) == context);
	spa_assert(pw_global_get_type(global) == PW_TYPE_INTERFACE_Core);
	spa_assert(pw_global_get_version(global) == PW_VERSION_CORE);
	spa_assert(pw_global_get_id(global) == 0);
	spa_assert(pw_global_get_object(global) == (void*)context);

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

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

	spa_assert(destroy_count == 0);
	spa_assert(free_count == 0);
	spa_assert(global_removed_count == 0);
	pw_context_destroy(context);
	spa_assert(destroy_count == 1);
	spa_assert(free_count == 1);
	spa_assert(global_removed_count == 1);
	pw_main_loop_destroy(loop);
}

static int info_changed_count = 0;
static void context_info_changed_count(void *data, const struct pw_core_info *info)
{
	spa_assert(spa_dict_lookup(info->props, "foo") == NULL);
	spa_assert(!strcmp(spa_dict_lookup(info->props, "biz"), "buzz"));
	spa_assert(!strcmp(spa_dict_lookup(info->props, "buzz"), "frizz"));
	info_changed_count++;
}

static void test_properties(void)
{
	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];

	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);
	spa_assert(context != NULL);
	spa_assert(pw_context_get_user_data(context) == NULL);
	pw_context_add_listener(context, &listener, &context_events, context);

	context_events.info_changed = context_info_changed_count;
	spa_assert(info_changed_count == 0);

	props = pw_context_get_properties(context);
	spa_assert(props != NULL);
	spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar"));
	spa_assert(!strcmp(pw_properties_get(props, "biz"), "fuzz"));
	spa_assert(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));

	spa_assert(info_changed_count == 1);

	spa_assert(props == pw_context_get_properties(context));
	spa_assert(pw_properties_get(props, "foo") == NULL);
	spa_assert(!strcmp(pw_properties_get(props, "biz"), "buzz"));
	spa_assert(!strcmp(pw_properties_get(props, "buzz"), "frizz"));

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

static void test_support(void)
{
	struct pw_main_loop *loop;
	struct pw_context *context;
	const struct spa_support *support;
	uint32_t n_support;
	uint32_t 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,
		SPA_TYPE_INTERFACE_DBus,
		SPA_TYPE_INTERFACE_CPU
	};
	size_t i;

	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);
	spa_assert(support != NULL);
	spa_assert(n_support > 0);

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

	pw_context_destroy(context);
	pw_main_loop_destroy(loop);
}

int main(int argc, char *argv[])
{
	pw_init(&argc, &argv);

	test_abi();
	test_create();
	test_properties();
	test_support();

	return 0;
}