wireplumber/tests/wp/collection.c
Julian Bouzas 59f98e38e4 lib: Add collection proxy API
This proxy uses the metadata interface to represent collections.
2026-05-05 16:00:15 -04:00

281 lines
9.5 KiB
C

/* WirePlumber
*
* Copyright © 2026 Collabora Ltd.
* @author Julian Bouzas <julian.bouzas@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
#include <pipewire/extensions/session-manager/keys.h>
#include "../common/base-test-fixture.h"
typedef struct {
WpBaseTestFixture base;
WpObjectManager *om;
WpCollection *collection;
guint32 last_collected_id;
guint32 last_dropped_id;
} TestFixture;
static void
test_collection_setup (TestFixture *self, gconstpointer user_data)
{
wp_base_test_fixture_setup (&self->base, WP_BASE_TEST_FLAG_CLIENT_CORE);
self->om = wp_object_manager_new ();
}
static void
test_collection_teardown (TestFixture *self, gconstpointer user_data)
{
g_clear_object (&self->om);
wp_base_test_fixture_teardown (&self->base);
}
static void
on_collection_added (WpObjectManager *om, WpCollection *collection,
TestFixture *fixture)
{
g_assert_true (WP_IS_COLLECTION (collection));
g_assert_null (fixture->collection);
fixture->collection = WP_COLLECTION (collection);
g_main_loop_quit (fixture->base.loop);
}
static void
on_collection_removed (WpObjectManager *om, WpCollection *collection,
TestFixture *fixture)
{
g_assert_true (WP_IS_COLLECTION (collection));
g_assert_nonnull (fixture->collection);
fixture->collection = NULL;
g_main_loop_quit (fixture->base.loop);
}
static void
on_global_collected (WpCollection *om, guint32 global_id, TestFixture *fixture)
{
fixture->last_collected_id = global_id;
g_main_loop_quit (fixture->base.loop);
}
static void
on_global_dropped (WpCollection *om, guint32 global_id, TestFixture *fixture)
{
fixture->last_dropped_id = global_id;
g_main_loop_quit (fixture->base.loop);
}
static void
test_impl_collection_activated (WpObject * impl_collection, GAsyncResult * res,
TestFixture *fixture)
{
g_autoptr (GError) error = NULL;
g_assert_true (wp_object_activate_finish (impl_collection, res, &error));
g_assert_no_error (error);
g_assert_true (WP_IS_IMPL_COLLECTION (impl_collection));
g_main_loop_quit (fixture->base.loop);
}
static void
test_collection_basic (TestFixture *fixture, gconstpointer data)
{
g_autoptr (WpImplCollection) impl_collection = NULL;
/* Install object manager on the client side */
g_signal_connect (fixture->om, "object-added",
(GCallback) on_collection_added, fixture);
g_signal_connect (fixture->om, "object-removed",
(GCallback) on_collection_removed, fixture);
wp_object_manager_add_interest (fixture->om, WP_TYPE_COLLECTION, NULL);
wp_object_manager_request_object_features (fixture->om, WP_TYPE_COLLECTION,
WP_OBJECT_FEATURES_ALL);
wp_core_install_object_manager (fixture->base.client_core, fixture->om);
/* Create the collection */
impl_collection = wp_impl_collection_new (fixture->base.core, "my-collection",
NULL);
g_assert_nonnull (impl_collection);
/* Export the collection */
wp_object_activate (WP_OBJECT (impl_collection), WP_OBJECT_FEATURES_ALL,
NULL, (GAsyncReadyCallback) test_impl_collection_activated, fixture);
g_main_loop_run (fixture->base.loop);
/* Run again so the collection is added in the client object manager */
g_assert_null (fixture->collection);
g_main_loop_run (fixture->base.loop);
g_assert_cmpuint (wp_object_manager_get_n_objects (fixture->om), ==, 1);
g_assert_nonnull (fixture->collection);
/* Check name */
{
const gchar *name = wp_collection_get_name (fixture->collection);
g_assert_cmpstr (name, ==, "my-collection");
}
/* Check properties */
{
g_autoptr (WpProperties) props = NULL;
props = wp_global_proxy_get_global_properties (
WP_GLOBAL_PROXY (fixture->collection));
const gchar *str = wp_properties_get (props, "wireplumber.collection");
g_assert_cmpstr (str, ==, "true");
}
/* Handle collection signals */
g_signal_connect (fixture->collection, "global-collected",
(GCallback) on_global_collected, fixture);
g_signal_connect (fixture->collection, "global-dropped",
(GCallback) on_global_dropped, fixture);
/* Make sure collection does not have any globals */
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 0);
/* Collect the first global */
fixture->last_collected_id = 0;
wp_collection_collect_global (fixture->collection, 42);
g_main_loop_run (fixture->base.loop);
g_assert_cmpuint (fixture->last_collected_id, ==, 42);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 1);
g_assert_true (wp_collection_contains_global (fixture->collection, 42));
g_assert_cmpuint (wp_impl_collection_get_size (impl_collection), ==, 1);
g_assert_true (wp_impl_collection_contains_global (impl_collection, 42));
/* Collect the second global */
fixture->last_collected_id = 0;
wp_collection_collect_global (fixture->collection, 99);
g_main_loop_run (fixture->base.loop);
g_assert_cmpuint (fixture->last_collected_id, ==, 99);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 2);
g_assert_true (wp_collection_contains_global (fixture->collection, 99));
g_assert_cmpuint (wp_impl_collection_get_size (impl_collection), ==, 2);
g_assert_true (wp_impl_collection_contains_global (impl_collection, 99));
/* Iterate the globals from the client side */
{
g_autoptr (WpIterator) iter = NULL;
g_auto (GValue) val = G_VALUE_INIT;
gboolean has_first = FALSE;
gboolean has_second = FALSE;
guint count = 0;
iter = wp_collection_new_iterator (fixture->collection);
g_assert_nonnull (iter);
while (wp_iterator_next (iter, &val)) {
guint32 global_id = g_value_get_uint (&val);
if (global_id == 42)
has_first = TRUE;
if (global_id == 99)
has_second = TRUE;
count++;
g_value_unset (&val);
}
g_assert_true (has_first);
g_assert_true (has_second);
g_assert_cmpuint (count, ==, 2);
}
/* Iterate the globals from the impl side */
{
g_autoptr (WpIterator) iter = NULL;
g_auto (GValue) val = G_VALUE_INIT;
gboolean has_first = FALSE;
gboolean has_second = FALSE;
guint count = 0;
iter = wp_impl_collection_new_iterator (impl_collection);
g_assert_nonnull (iter);
while (wp_iterator_next (iter, &val)) {
guint32 global_id = g_value_get_uint (&val);
if (global_id == 42)
has_first = TRUE;
if (global_id == 99)
has_second = TRUE;
count++;
g_value_unset (&val);
}
g_assert_true (has_first);
g_assert_true (has_second);
g_assert_cmpuint (count, ==, 2);
}
/* Drop the first global */
fixture->last_dropped_id = 0;
wp_collection_drop_global (fixture->collection, 42);
g_main_loop_run (fixture->base.loop);
g_assert_cmpuint (fixture->last_dropped_id, ==, 42);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 1);
g_assert_false (wp_collection_contains_global (fixture->collection, 42));
g_assert_cmpuint (wp_impl_collection_get_size (impl_collection), ==, 1);
g_assert_false (wp_impl_collection_contains_global (impl_collection, 42));
/* Collect an existing global and make sure nothing happens */
fixture->last_collected_id = 0;
wp_collection_collect_global (fixture->collection, 99);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 1);
g_assert_true (wp_collection_contains_global (fixture->collection, 99));
g_assert_true (wp_impl_collection_contains_global (impl_collection, 99));
/* Drop an unexisting global and make sure nothing happens */
fixture->last_dropped_id = 0;
wp_collection_drop_global (fixture->collection, 42);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 1);
g_assert_false (wp_collection_contains_global (fixture->collection, 42));
g_assert_cmpuint (wp_impl_collection_get_size (impl_collection), ==, 1);
g_assert_false (wp_impl_collection_contains_global (impl_collection, 42));
/* Drop the second global */
fixture->last_dropped_id = 0;
wp_collection_drop_global (fixture->collection, 99);
g_main_loop_run (fixture->base.loop);
g_assert_cmpuint (fixture->last_dropped_id, ==, 99);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 0);
g_assert_false (wp_collection_contains_global (fixture->collection, 99));
g_assert_false (wp_impl_collection_contains_global (impl_collection, 99));
/* Collect a global from the impl side */
fixture->last_collected_id = 0;
wp_impl_collection_collect_global (impl_collection, 36);
g_main_loop_run (fixture->base.loop);
g_assert_cmpuint (fixture->last_collected_id, ==, 36);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 1);
g_assert_true (wp_collection_contains_global (fixture->collection, 36));
g_assert_cmpuint (wp_impl_collection_get_size (impl_collection), ==, 1);
g_assert_true (wp_impl_collection_contains_global (impl_collection, 36));
/* Drop the global from the impl size */
fixture->last_dropped_id = 0;
wp_impl_collection_drop_global (impl_collection, 36);
g_main_loop_run (fixture->base.loop);
g_assert_cmpuint (fixture->last_dropped_id, ==, 36);
g_assert_cmpuint (wp_collection_get_size (fixture->collection), ==, 0);
g_assert_false (wp_collection_contains_global (fixture->collection, 36));
g_assert_cmpuint (wp_impl_collection_get_size (impl_collection), ==, 0);
g_assert_false (wp_impl_collection_contains_global (impl_collection, 36));
}
gint
main (gint argc, gchar *argv[])
{
g_test_init (&argc, &argv, NULL);
wp_init (WP_INIT_ALL);
g_test_add ("/wp/collection/basic", TestFixture, NULL,
test_collection_setup, test_collection_basic, test_collection_teardown);
return g_test_run ();
}