Debian Codegraph @

tests/wp/session-item.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <stdio.h>
10
11 #include <wp/wp.h>
12 #include "../common/base-test-fixture.h"
13
14 G_DEFINE_QUARK (test-domain, test_domain)
15
16 typedef struct {
17 WpBaseTestFixture base;
18 WpObjectManager *si_om;
19 gint n_items;
20 } TestSessionItemFixture;
21
22 static void
23 test_session_item_setup (TestSessionItemFixture *self, gconstpointer user_data)
24 {
25 wp_base_test_fixture_setup (&self->base, WP_BASE_TEST_FLAG_CLIENT_CORE);
26 self->si_om = wp_object_manager_new ();
27 self->n_items = 0;
28 }
29
30 static void
31 test_session_item_teardown (TestSessionItemFixture *self, gconstpointer user_data)
32 {
33 g_clear_object (&self->si_om);
34 wp_base_test_fixture_teardown (&self->base);
35 }
36
37 struct _TestSiDummy
38 {
39 WpSessionItem parent;
40 const gchar *name;
41 gboolean fail;
42 gboolean activate_done;
43 gboolean export_done;
44 };
45
46 G_DECLARE_FINAL_TYPE (TestSiDummy, si_dummy, TEST, SI_DUMMY, WpSessionItem)
47 G_DEFINE_TYPE (TestSiDummy, si_dummy, WP_TYPE_SESSION_ITEM)
48
49 static void
50 si_dummy_init (TestSiDummy * self)
51 {
52 }
53
54 static void
55 si_dummy_reset (WpSessionItem * item)
56 {
57 TestSiDummy *self = TEST_SI_DUMMY (item);
58
59 /* deactivate first */
60 wp_object_deactivate (WP_OBJECT (self),
61 WP_SESSION_ITEM_FEATURE_ACTIVE | WP_SESSION_ITEM_FEATURE_EXPORTED);
62
63 /* reset */
64 self->fail = FALSE;
65
66 WP_SESSION_ITEM_CLASS (si_dummy_parent_class)->reset (item);
67 }
68
69 static gboolean
70 si_dummy_configure (WpSessionItem * item, WpProperties * props)
71 {
72 TestSiDummy *self = TEST_SI_DUMMY (item);
73 const gchar *str = NULL;
74
75 /* reset previous config */
76 si_dummy_reset (item);
77
78 str = wp_properties_get (props, "fail");
79 if (!str || sscanf(str, "%u", &self->fail) != 1)
80 return FALSE;
81
82 wp_session_item_set_properties (WP_SESSION_ITEM (self), props);
83 return TRUE;
84 }
85
86 static gpointer
87 si_dummy_get_associated_proxy (WpSessionItem * item, GType proxy_type)
88 {
89 return NULL;
90 }
91
92 static void
93 si_dummy_disable_active (WpSessionItem *si)
94 {
95 TestSiDummy *self = TEST_SI_DUMMY (si);
96
97 self->activate_done = FALSE;
98 wp_object_update_features (WP_OBJECT (self), 0,
99 WP_SESSION_ITEM_FEATURE_ACTIVE);
100 }
101
102 static void
103 si_dummy_disable_exported (WpSessionItem *si)
104 {
105 TestSiDummy *self = TEST_SI_DUMMY (si);
106
107 self->export_done = FALSE;
108 wp_object_update_features (WP_OBJECT (self), 0,
109 WP_SESSION_ITEM_FEATURE_EXPORTED);
110 }
111
112 static gboolean
113 si_dummy_step_activate (gpointer data)
114 {
115 WpTransition *transition = data;
116 g_assert_true (WP_IS_TRANSITION (transition));
117
118 TestSiDummy *self = wp_transition_get_source_object (transition);
119 g_assert_true (TEST_IS_SI_DUMMY (self));
120
121 if (self->fail) {
122 wp_transition_return_error (transition,
123 g_error_new (test_domain_quark (), 0, "error"));
124 } else {
125 self->activate_done = TRUE;
126 wp_object_update_features (WP_OBJECT (self),
127 WP_SESSION_ITEM_FEATURE_ACTIVE, 0);
128 }
129
130 return G_SOURCE_REMOVE;
131 }
132
133 static void
134 si_dummy_enable_active (WpSessionItem *si, WpTransition *transition)
135 {
136 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (si));
137 wp_core_idle_add (core, NULL, si_dummy_step_activate, transition, NULL);
138 }
139
140 static gboolean
141 si_dummy_step_export (gpointer data)
142 {
143 WpTransition *transition = data;
144 g_assert_true (WP_IS_TRANSITION (transition));
145
146 TestSiDummy *self = wp_transition_get_source_object (transition);
147 g_assert_true (TEST_IS_SI_DUMMY (self));
148
149 if (self->fail) {
150 wp_transition_return_error (transition,
151 g_error_new (test_domain_quark (), 0, "error"));
152 } else {
153 self->export_done = TRUE;
154 wp_object_update_features (WP_OBJECT (self),
155 WP_SESSION_ITEM_FEATURE_EXPORTED, 0);
156 }
157
158 return G_SOURCE_REMOVE;
159 }
160
161 static void
162 si_dummy_enable_exported (WpSessionItem *si, WpTransition *transition)
163 {
164 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (si));
165 wp_core_idle_add (core, NULL, si_dummy_step_export, transition, NULL);
166 }
167
168 static void
169 si_dummy_class_init (TestSiDummyClass * klass)
170 {
171 WpSessionItemClass *si_class = (WpSessionItemClass *) klass;
172
173 si_class->reset = si_dummy_reset;
174 si_class->configure = si_dummy_configure;
175 si_class->get_associated_proxy = si_dummy_get_associated_proxy;
176 si_class->disable_active = si_dummy_disable_active;
177 si_class->disable_exported = si_dummy_disable_exported;
178 si_class->enable_active = si_dummy_enable_active;
179 si_class->enable_exported = si_dummy_enable_exported;
180 }
181
182 static void
183 expect_activate_success (WpObject * object, GAsyncResult * res, gpointer data)
184 {
185 GMainLoop *loop = data;
186 g_autoptr (GError) error = NULL;
187 g_assert_true (TEST_IS_SI_DUMMY (object));
188 g_assert_true (wp_object_activate_finish (object, res, &error));
189 g_assert_no_error (error);
190 g_main_loop_quit (loop);
191 }
192
193 static void
194 expect_activate_failure (WpObject * object, GAsyncResult * res, gpointer data)
195 {
196 GMainLoop *loop = data;
197 g_autoptr (GError) error = NULL;
198 g_assert_true (TEST_IS_SI_DUMMY (object));
199 g_assert_false (wp_object_activate_finish (object, res, &error));
200 g_assert_error (error, test_domain_quark (), 0);
201 g_main_loop_quit (loop);
202 }
203
204 static void
205 test_configuration (TestSessionItemFixture *fixture, gconstpointer data)
206 {
207 g_autoptr (WpSessionItem) item = NULL;
208 TestSiDummy *dummy;
209
210 item = g_object_new (si_dummy_get_type (), "core", fixture->base.core, NULL);
211 dummy = TEST_SI_DUMMY (item);
212
213 {
214 g_autoptr (WpProperties) p = wp_properties_new_empty ();
215 wp_properties_setf (p, "fail", "%u", TRUE);
216 g_assert_true (wp_session_item_configure (item, g_steal_pointer (&p)));
217 g_assert_true (wp_session_item_is_configured (item));
218 g_assert_true (dummy->fail);
219 }
220
221 {
222 g_autoptr (WpProperties) p = wp_session_item_get_properties (item);
223 g_assert_nonnull (p);
224 const gchar * str = wp_properties_get (p, "fail");
225 gboolean fail = FALSE;
226 g_assert_nonnull (str);
227 g_assert_true (sscanf(str, "%u", &fail) == 1);
228 g_assert_true (fail);
229 }
230 }
231
232 static void
233 test_activation (TestSessionItemFixture *fixture, gconstpointer data)
234 {
235 g_autoptr (WpSessionItem) item = NULL;
236 TestSiDummy *dummy;
237
238 item = g_object_new (si_dummy_get_type (), "core", fixture->base.core, NULL);
239 dummy = TEST_SI_DUMMY (item);
240
241 {
242 g_autoptr (WpProperties) p = wp_properties_new_empty ();
243 wp_properties_setf (p, "fail", "%u", FALSE);
244 g_assert_true (wp_session_item_configure (item, g_steal_pointer (&p)));
245 g_assert_true (wp_session_item_is_configured (item));
246 }
247
248 wp_object_activate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_ACTIVE,
249 NULL, (GAsyncReadyCallback) expect_activate_success, fixture->base.loop);
250 g_main_loop_run (fixture->base.loop);
251 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==,
252 WP_SESSION_ITEM_FEATURE_ACTIVE);
253 g_assert_true (dummy->activate_done);
254
255 wp_object_deactivate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_ACTIVE);
256 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==, 0);
257 g_assert_false (dummy->activate_done);
258 }
259
260 static void
261 test_activation_error (TestSessionItemFixture *fixture, gconstpointer data)
262 {
263 g_autoptr (WpSessionItem) item = NULL;
264 TestSiDummy *dummy;
265
266 item = g_object_new (si_dummy_get_type (), "core", fixture->base.core, NULL);
267 dummy = TEST_SI_DUMMY (item);
268
269 {
270 g_autoptr (WpProperties) p = wp_properties_new_empty ();
271 wp_properties_setf (p, "fail", "%u", TRUE);
272 g_assert_true (wp_session_item_configure (item, g_steal_pointer (&p)));
273 g_assert_true (wp_session_item_is_configured (item));
274 }
275
276 wp_object_activate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_ACTIVE,
277 NULL, (GAsyncReadyCallback) expect_activate_failure, fixture->base.loop);
278 g_main_loop_run (fixture->base.loop);
279 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==, 0);
280 g_assert_false (dummy->activate_done);
281 g_assert_true (dummy->fail);
282
283 wp_object_deactivate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_ACTIVE);
284 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==, 0);
285 g_assert_true (dummy->fail);
286 g_assert_false (dummy->activate_done);
287
288 wp_session_item_reset (item);
289 g_assert_false (dummy->fail);
290 g_assert_false (dummy->activate_done);
291 g_assert_false (wp_session_item_is_configured (item));
292 }
293
294 static void
295 test_export (TestSessionItemFixture *fixture, gconstpointer data)
296 {
297 g_autoptr (WpSessionItem) item = NULL;
298 TestSiDummy *dummy;
299
300 item = g_object_new (si_dummy_get_type (), "core", fixture->base.core, NULL);
301 dummy = TEST_SI_DUMMY (item);
302
303 {
304 g_autoptr (WpProperties) p = wp_properties_new_empty ();
305 wp_properties_setf (p, "fail", "%u", FALSE);
306 g_assert_true (wp_session_item_configure (item, g_steal_pointer (&p)));
307 g_assert_true (wp_session_item_is_configured (item));
308 }
309
310 wp_object_activate (WP_OBJECT (item),
311 WP_SESSION_ITEM_FEATURE_ACTIVE | WP_SESSION_ITEM_FEATURE_EXPORTED,
312 NULL, (GAsyncReadyCallback) expect_activate_success, fixture->base.loop);
313 g_main_loop_run (fixture->base.loop);
314 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==,
315 WP_SESSION_ITEM_FEATURE_ACTIVE | WP_SESSION_ITEM_FEATURE_EXPORTED);
316 g_assert_true (dummy->activate_done);
317 g_assert_true (dummy->export_done);
318
319 wp_object_deactivate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_EXPORTED);
320 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==,
321 WP_SESSION_ITEM_FEATURE_ACTIVE);
322 g_assert_true (dummy->activate_done);
323 g_assert_false (dummy->export_done);
324
325 wp_session_item_reset (item);
326 g_assert_false (dummy->activate_done);
327 g_assert_false (wp_session_item_is_configured (item));
328 }
329
330 static void
331 test_export_error (TestSessionItemFixture *fixture, gconstpointer data)
332 {
333 g_autoptr (WpSessionItem) item = NULL;
334 TestSiDummy *dummy;
335
336 item = g_object_new (si_dummy_get_type (), "core", fixture->base.core, NULL);
337 dummy = TEST_SI_DUMMY (item);
338
339 {
340 g_autoptr (WpProperties) p = wp_properties_new_empty ();
341 wp_properties_setf (p, "fail", "%u", TRUE);
342 g_assert_true (wp_session_item_configure (item, g_steal_pointer (&p)));
343 g_assert_true (wp_session_item_is_configured (item));
344 }
345
346 wp_object_activate (WP_OBJECT (item),
347 WP_SESSION_ITEM_FEATURE_ACTIVE | WP_SESSION_ITEM_FEATURE_EXPORTED,
348 NULL, (GAsyncReadyCallback) expect_activate_failure, fixture->base.loop);
349 g_main_loop_run (fixture->base.loop);
350 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==, 0);
351 g_assert_false (dummy->activate_done);
352 g_assert_false (dummy->export_done);
353
354 wp_object_deactivate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_EXPORTED);
355 g_assert_cmpint (wp_object_get_active_features (WP_OBJECT (item)), ==, 0);
356 g_assert_true (dummy->fail);
357 g_assert_false (dummy->activate_done);
358 g_assert_false (dummy->export_done);
359
360 wp_session_item_reset (item);
361 g_assert_false (dummy->fail);
362 g_assert_false (dummy->activate_done);
363 g_assert_false (dummy->export_done);
364 g_assert_false (wp_session_item_is_configured (item));
365 }
366
367 static void
368 test_session_item_added (WpObjectManager *om, WpSessionItem *item,
369 TestSessionItemFixture *fixture)
370 {
371 g_assert_true (WP_IS_SESSION_ITEM (item));
372 fixture->n_items++;
373 g_main_loop_quit (fixture->base.loop);
374 }
375
376 static void
377 test_session_item_removed (WpObjectManager *om, WpSessionItem *item,
378 TestSessionItemFixture *fixture)
379 {
380 g_assert_true (WP_IS_SESSION_ITEM (item));
381 fixture->n_items--;
382 g_main_loop_quit (fixture->base.loop);
383 }
384
385 static gboolean
386 idle_register (gpointer data)
387 {
388 WpSessionItem *item = data;
389 wp_session_item_register (g_object_ref (item));
390 return G_SOURCE_REMOVE;
391 }
392
393 static gboolean
394 idle_remove (gpointer data)
395 {
396 WpSessionItem *item = data;
397 wp_session_item_remove (item);
398 return G_SOURCE_REMOVE;
399 }
400
401 static void
402 test_registration (TestSessionItemFixture *fixture, gconstpointer data)
403 {
404 g_autoptr (WpSessionItem) item = NULL;
405 g_autoptr (WpSessionItem) item2 = NULL;
406
407 g_signal_connect (fixture->si_om, "object-added",
408 (GCallback) test_session_item_added, fixture);
409 g_signal_connect (fixture->si_om, "object-removed",
410 (GCallback) test_session_item_removed, fixture);
411 wp_object_manager_add_interest (fixture->si_om, WP_TYPE_SESSION_ITEM, NULL);
412 wp_core_install_object_manager (fixture->base.core, fixture->si_om);
413
414 /* create, configure and export first session item */
415 item = g_object_new (si_dummy_get_type (), "core", fixture->base.core, NULL);
416 g_assert_nonnull (item);
417 {
418 g_autoptr (WpProperties) p = wp_properties_new_empty ();
419 wp_properties_setf (p, "fail", "%u", FALSE);
420 g_assert_true (wp_session_item_configure (item, g_steal_pointer (&p)));
421 g_assert_true (wp_session_item_is_configured (item));
422 }
423
424 /* create and configure second session item */
425 item2 = g_object_new (si_dummy_get_type (), "core", fixture->base.core, NULL);
426 g_assert_nonnull (item2);
427 {
428 g_autoptr (WpProperties) p = wp_properties_new_empty ();
429 wp_properties_setf (p, "fail", "%u", TRUE);
430 g_assert_true (wp_session_item_configure (item2, g_steal_pointer (&p)));
431 g_assert_true (wp_session_item_is_configured (item2));
432 }
433
434 /* register item */
435 wp_core_idle_add (fixture->base.core, NULL, idle_register, item, NULL);
436 g_main_loop_run (fixture->base.loop);
437 g_assert_cmpint (fixture->n_items, ==, 1);
438
439 /* register item2 */
440 wp_core_idle_add (fixture->base.core, NULL, idle_register, item2, NULL);
441 g_main_loop_run (fixture->base.loop);
442 g_assert_cmpint (fixture->n_items, ==, 2);
443
444 /* lookup item, which has fail set to false */
445 {
446 g_autoptr (WpSessionItem) si = wp_object_manager_lookup (
447 fixture->si_om, WP_TYPE_SESSION_ITEM,
448 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "fail", "=b", FALSE, NULL);
449 g_assert_nonnull (si);
450 g_assert_true (si == item);
451 }
452
453 /* lookup item2, which has fail set to true */
454 {
455 g_autoptr (WpSessionItem) si = wp_object_manager_lookup (
456 fixture->si_om, WP_TYPE_SESSION_ITEM,
457 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "fail", "=b", TRUE, NULL);
458 g_assert_nonnull (si);
459 g_assert_true (si == item2);
460 }
461
462 /* remove item */
463 wp_core_idle_add (fixture->base.core, NULL, idle_remove, item, NULL);
464 g_main_loop_run (fixture->base.loop);
465 g_assert_cmpint (fixture->n_items, ==, 1);
466
467 /* make sure item cannot be found */
468 {
469 g_autoptr (WpSessionItem) si = wp_object_manager_lookup (
470 fixture->si_om, WP_TYPE_SESSION_ITEM,
471 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "fail", "=b", FALSE, NULL);
472 g_assert_null (si);
473 }
474
475 /* make sure item2 is still there */
476 {
477 g_autoptr (WpSessionItem) si = wp_object_manager_lookup (
478 fixture->si_om, WP_TYPE_SESSION_ITEM,
479 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "fail", "=b", TRUE, NULL);
480 g_assert_nonnull (si);
481 g_assert_true (si == item2);
482 }
483
484 /* remove item2 */
485 wp_core_idle_add (fixture->base.core, NULL, idle_remove, item2, NULL);
486 g_main_loop_run (fixture->base.loop);
487 g_assert_cmpint (fixture->n_items, ==, 0);
488
489 /* make sure item cannot be found */
490 {
491 g_autoptr (WpSessionItem) si = wp_object_manager_lookup (
492 fixture->si_om, WP_TYPE_SESSION_ITEM,
493 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "fail", "=b", TRUE, NULL);
494 g_assert_null (si);
495 }
496 }
497
498 gint
499 main (gint argc, gchar *argv[])
500 {
501 g_test_init (&argc, &argv, NULL);
502 wp_init (WP_INIT_ALL);
503
504 g_test_add ("/wp/session-item/configuration", TestSessionItemFixture, NULL,
505 test_session_item_setup, test_configuration, test_session_item_teardown);
506 g_test_add ("/wp/session-item/activation", TestSessionItemFixture, NULL,
507 test_session_item_setup, test_activation, test_session_item_teardown);
508 g_test_add ("/wp/session-item/activation-error", TestSessionItemFixture, NULL,
509 test_session_item_setup, test_activation_error, test_session_item_teardown);
510 g_test_add ("/wp/session-item/export", TestSessionItemFixture, NULL,
511 test_session_item_setup, test_export, test_session_item_teardown);
512 g_test_add ("/wp/session-item/export-error", TestSessionItemFixture, NULL,
513 test_session_item_setup, test_export_error, test_session_item_teardown);
514 g_test_add ("/wp/session-item/registration", TestSessionItemFixture, NULL,
515 test_session_item_setup, test_registration, test_session_item_teardown);
516
517 return g_test_run ();
518 }