| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <wp/wp.h> |
| 10 |
#include "../../modules/dbus-connection-state.h" |
| 11 |
#include "../common/base-test-fixture.h" |
| 12 |
|
| 13 |
typedef struct { |
| 14 |
WpBaseTestFixture base; |
| 15 |
GTestDBus *test_dbus; |
| 16 |
WpPlugin *dbus_1; |
| 17 |
WpPlugin *dbus_2; |
| 18 |
WpPlugin *rd_plugin_1; |
| 19 |
WpPlugin *rd_plugin_2; |
| 20 |
gint expected_rd1_state; |
| 21 |
gint expected_rd2_state; |
| 22 |
} RdTestFixture; |
| 23 |
|
| 24 |
static void |
| 25 |
on_plugin_loaded (WpCore * core, GAsyncResult * res, RdTestFixture *f) |
| 26 |
{ |
| 27 |
gboolean loaded; |
| 28 |
GError *error = NULL; |
| 29 |
|
| 30 |
loaded = wp_core_load_component_finish (core, res, &error); |
| 31 |
g_assert_no_error (error); |
| 32 |
g_assert_true (loaded); |
| 33 |
|
| 34 |
g_main_loop_quit (f->base.loop); |
| 35 |
} |
| 36 |
|
| 37 |
static void |
| 38 |
test_rd_setup (RdTestFixture *f, gconstpointer data) |
| 39 |
{ |
| 40 |
WpDBusConnectionState state = -1; |
| 41 |
|
| 42 |
wp_base_test_fixture_setup (&f->base, |
| 43 |
WP_BASE_TEST_FLAG_CLIENT_CORE | WP_BASE_TEST_FLAG_DONT_CONNECT); |
| 44 |
|
| 45 |
f->test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE); |
| 46 |
g_test_dbus_up (f->test_dbus); |
| 47 |
|
| 48 |
{ |
| 49 |
wp_core_load_component (f->base.core, |
| 50 |
"libwireplumber-module-dbus-connection", "module", NULL, NULL, NULL, |
| 51 |
(GAsyncReadyCallback) on_plugin_loaded, f); |
| 52 |
g_main_loop_run (f->base.loop); |
| 53 |
} |
| 54 |
{ |
| 55 |
wp_core_load_component (f->base.client_core, |
| 56 |
"libwireplumber-module-dbus-connection", "module", NULL, NULL, NULL, |
| 57 |
(GAsyncReadyCallback) on_plugin_loaded, f); |
| 58 |
g_main_loop_run (f->base.loop); |
| 59 |
} |
| 60 |
{ |
| 61 |
wp_core_load_component (f->base.core, |
| 62 |
"libwireplumber-module-reserve-device", "module", NULL, NULL, NULL, |
| 63 |
(GAsyncReadyCallback) on_plugin_loaded, f); |
| 64 |
g_main_loop_run (f->base.loop); |
| 65 |
} |
| 66 |
{ |
| 67 |
wp_core_load_component (f->base.client_core, |
| 68 |
"libwireplumber-module-reserve-device", "module", NULL, NULL, NULL, |
| 69 |
(GAsyncReadyCallback) on_plugin_loaded, f); |
| 70 |
g_main_loop_run (f->base.loop); |
| 71 |
} |
| 72 |
|
| 73 |
f->dbus_1 = wp_plugin_find (f->base.core, "dbus-connection"); |
| 74 |
g_assert_nonnull (f->dbus_1); |
| 75 |
|
| 76 |
f->dbus_2 = wp_plugin_find (f->base.client_core, "dbus-connection"); |
| 77 |
g_assert_nonnull (f->dbus_2); |
| 78 |
|
| 79 |
f->rd_plugin_1 = wp_plugin_find (f->base.core, "reserve-device"); |
| 80 |
g_assert_nonnull (f->rd_plugin_1); |
| 81 |
|
| 82 |
f->rd_plugin_2 = wp_plugin_find (f->base.client_core, "reserve-device"); |
| 83 |
g_assert_nonnull (f->rd_plugin_2); |
| 84 |
|
| 85 |
g_object_get (f->dbus_1, "state", &state, NULL); |
| 86 |
g_assert_cmpint (state, ==, WP_DBUS_CONNECTION_STATE_CONNECTED); |
| 87 |
|
| 88 |
g_object_get (f->dbus_2, "state", &state, NULL); |
| 89 |
g_assert_cmpint (state, ==, WP_DBUS_CONNECTION_STATE_CONNECTED); |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
test_rd_teardown (RdTestFixture *f, gconstpointer data) |
| 94 |
{ |
| 95 |
g_clear_object (&f->dbus_1); |
| 96 |
g_clear_object (&f->dbus_2); |
| 97 |
g_clear_object (&f->rd_plugin_1); |
| 98 |
g_clear_object (&f->rd_plugin_2); |
| 99 |
g_test_dbus_down (f->test_dbus); |
| 100 |
g_clear_object (&f->test_dbus); |
| 101 |
wp_base_test_fixture_teardown (&f->base); |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
ensure_plugins_stable_state (GObject * obj, GParamSpec * spec, RdTestFixture *f) |
| 106 |
{ |
| 107 |
WpDBusConnectionState state1 = -1, state2 = -1; |
| 108 |
g_object_get (f->dbus_1, "state", &state1, NULL); |
| 109 |
g_object_get (f->dbus_2, "state", &state2, NULL); |
| 110 |
|
| 111 |
if (state1 != WP_DBUS_CONNECTION_STATE_CONNECTING && |
| 112 |
state2 != WP_DBUS_CONNECTION_STATE_CONNECTING && |
| 113 |
state1 == state2) |
| 114 |
g_main_loop_quit (f->base.loop); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
test_rd_plugin (RdTestFixture *f, gconstpointer data) |
| 119 |
{ |
| 120 |
GObject *rd1 = NULL, *rd2 = NULL, *rd_video = NULL, *tmp = NULL; |
| 121 |
gint priority = 0; |
| 122 |
gchar *str; |
| 123 |
|
| 124 |
g_signal_emit_by_name (f->rd_plugin_1, "create-reservation", |
| 125 |
"Audio0", "WirePlumber", "hw:0,0", 10, &rd1); |
| 126 |
g_assert_nonnull (rd1); |
| 127 |
g_signal_emit_by_name (f->rd_plugin_2, "create-reservation", |
| 128 |
"Audio0", "Other Server", "hw:0,0", 15, &rd2); |
| 129 |
g_assert_nonnull (rd2); |
| 130 |
g_signal_emit_by_name (f->rd_plugin_1, "create-reservation", |
| 131 |
"Video0", "WirePlumber", "/dev/video0", 10, &rd_video); |
| 132 |
g_assert_nonnull (rd_video); |
| 133 |
|
| 134 |
g_signal_emit_by_name (f->rd_plugin_1, "get-reservation", "Video1", &tmp); |
| 135 |
g_assert_null (tmp); |
| 136 |
g_signal_emit_by_name (f->rd_plugin_2, "get-reservation", "Video0", &tmp); |
| 137 |
g_assert_null (tmp); |
| 138 |
|
| 139 |
g_signal_emit_by_name (f->rd_plugin_1, "get-reservation", "Audio0", &tmp); |
| 140 |
g_assert_nonnull (tmp); |
| 141 |
g_assert_true (tmp == rd1); |
| 142 |
g_clear_object (&tmp); |
| 143 |
|
| 144 |
g_object_get (rd1, "name", &str, NULL); |
| 145 |
g_assert_cmpstr (str, ==, "Audio0"); |
| 146 |
g_free (str); |
| 147 |
g_object_get (rd2, "name", &str, NULL); |
| 148 |
g_assert_cmpstr (str, ==, "Audio0"); |
| 149 |
g_free (str); |
| 150 |
g_object_get (rd_video, "name", &str, NULL); |
| 151 |
g_assert_cmpstr (str, ==, "Video0"); |
| 152 |
g_free (str); |
| 153 |
g_object_get (rd1, "application-name", &str, NULL); |
| 154 |
g_assert_cmpstr (str, ==, "WirePlumber"); |
| 155 |
g_free (str); |
| 156 |
g_object_get (rd1, "application-device-name", &str, NULL); |
| 157 |
g_assert_cmpstr (str, ==, "hw:0,0"); |
| 158 |
g_free (str); |
| 159 |
g_object_get (rd1, "priority", &priority, NULL); |
| 160 |
g_assert_cmpint (priority, ==, 10); |
| 161 |
g_object_get (rd2, "priority", &priority, NULL); |
| 162 |
g_assert_cmpint (priority, ==, 15); |
| 163 |
|
| 164 |
g_signal_emit_by_name (f->rd_plugin_1, "destroy-reservation", "Audio0"); |
| 165 |
g_signal_emit_by_name (f->rd_plugin_1, "get-reservation", "Audio0", &tmp); |
| 166 |
g_assert_null (tmp); |
| 167 |
g_signal_emit_by_name (f->rd_plugin_2, "get-reservation", "Audio0", &tmp); |
| 168 |
g_assert_nonnull (tmp); |
| 169 |
g_assert_true (tmp == rd2); |
| 170 |
g_clear_object (&tmp); |
| 171 |
g_clear_object (&rd2); |
| 172 |
g_clear_object (&rd1); |
| 173 |
g_clear_object (&rd_video); |
| 174 |
} |
| 175 |
|
| 176 |
static void |
| 177 |
test_rd_conn_closed (RdTestFixture *f, gconstpointer data) |
| 178 |
{ |
| 179 |
GObject *rd1 = NULL; |
| 180 |
WpDBusConnectionState state = -1; |
| 181 |
|
| 182 |
g_signal_connect (f->dbus_1, "notify::state", |
| 183 |
G_CALLBACK (ensure_plugins_stable_state), f); |
| 184 |
g_signal_connect (f->dbus_2, "notify::state", |
| 185 |
G_CALLBACK (ensure_plugins_stable_state), f); |
| 186 |
|
| 187 |
g_signal_emit_by_name (f->rd_plugin_1, "create-reservation", |
| 188 |
"Audio0", "WirePlumber", "hw:0,0", 10, &rd1); |
| 189 |
g_assert_nonnull (rd1); |
| 190 |
g_clear_object (&rd1); |
| 191 |
|
| 192 |
/* stop the bus, expect the connections to close |
| 193 |
and state to go back to CLOSED */ |
| 194 |
g_test_dbus_stop (f->test_dbus); |
| 195 |
g_main_loop_run (f->base.loop); |
| 196 |
|
| 197 |
g_object_get (f->dbus_1, "state", &state, NULL); |
| 198 |
g_assert_cmpint (state, ==, WP_DBUS_CONNECTION_STATE_CLOSED); |
| 199 |
g_object_get (f->dbus_2, "state", &state, NULL); |
| 200 |
g_assert_cmpint (state, ==, WP_DBUS_CONNECTION_STATE_CLOSED); |
| 201 |
|
| 202 |
g_signal_emit_by_name (f->rd_plugin_1, "get-reservation", "Audio0", &rd1); |
| 203 |
g_assert_null (rd1); |
| 204 |
} |
| 205 |
|
| 206 |
static void |
| 207 |
expect_rd1_state (GObject * rd, GParamSpec * spec, RdTestFixture *f) |
| 208 |
{ |
| 209 |
gint state; |
| 210 |
g_object_get (rd, "state", &state, NULL); |
| 211 |
if (state == f->expected_rd1_state) |
| 212 |
g_main_loop_quit (f->base.loop); |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
expect_rd2_state (GObject * rd, GParamSpec * spec, RdTestFixture *f) |
| 217 |
{ |
| 218 |
gint state; |
| 219 |
g_object_get (rd, "state", &state, NULL); |
| 220 |
if (state == f->expected_rd2_state) |
| 221 |
g_main_loop_quit (f->base.loop); |
| 222 |
} |
| 223 |
|
| 224 |
static void |
| 225 |
handle_release_requested (GObject * rd, gboolean forced, RdTestFixture *f) |
| 226 |
{ |
| 227 |
gint state = 0xffff; |
| 228 |
g_signal_emit_by_name (rd, "release"); |
| 229 |
g_object_get (rd, "state", &state, NULL); |
| 230 |
g_assert_cmpint (state, ==, 2); |
| 231 |
g_main_loop_quit (f->base.loop); |
| 232 |
} |
| 233 |
|
| 234 |
static void |
| 235 |
test_rd_acquire_release (RdTestFixture *f, gconstpointer data) |
| 236 |
{ |
| 237 |
GObject *rd1 = NULL, *rd2 = NULL; |
| 238 |
gint state = 0; |
| 239 |
gchar *str = NULL; |
| 240 |
|
| 241 |
g_signal_emit_by_name (f->rd_plugin_1, "create-reservation", |
| 242 |
"Audio0", "WirePlumber", "hw:0,0", 10, &rd1); |
| 243 |
g_assert_nonnull (rd1); |
| 244 |
g_signal_emit_by_name (f->rd_plugin_2, "create-reservation", |
| 245 |
"Audio0", "Other Server", "hw:0,0", 15, &rd2); |
| 246 |
g_assert_nonnull (rd2); |
| 247 |
|
| 248 |
g_signal_connect (rd1, "notify::state", G_CALLBACK (expect_rd1_state), f); |
| 249 |
g_signal_connect (rd2, "notify::state", G_CALLBACK (expect_rd2_state), f); |
| 250 |
|
| 251 |
/* acquire */ |
| 252 |
wp_info ("rd1 acquire"); |
| 253 |
|
| 254 |
f->expected_rd1_state = 3; |
| 255 |
g_signal_emit_by_name (rd1, "acquire"); |
| 256 |
g_main_loop_run (f->base.loop); |
| 257 |
g_object_get (rd1, "state", &state, NULL); |
| 258 |
g_assert_cmpint (state, ==, 3); |
| 259 |
g_object_get (rd1, "owner-application-name", &str, NULL); |
| 260 |
g_assert_cmpstr (str, ==, "WirePlumber"); |
| 261 |
g_free (str); |
| 262 |
|
| 263 |
g_signal_connect (rd1, "release-requested", |
| 264 |
G_CALLBACK (handle_release_requested), f); |
| 265 |
|
| 266 |
/* acquire with higher priority */ |
| 267 |
wp_info ("rd2 acquire, higher prio"); |
| 268 |
g_signal_emit_by_name (rd2, "acquire"); |
| 269 |
|
| 270 |
/* rd1 is now released */ |
| 271 |
g_main_loop_run (f->base.loop); |
| 272 |
g_object_get (rd1, "state", &state, NULL); |
| 273 |
g_assert_cmpint (state, ==, 2); |
| 274 |
|
| 275 |
/* rd2 acquired */ |
| 276 |
f->expected_rd2_state = 3; |
| 277 |
g_main_loop_run (f->base.loop); |
| 278 |
g_object_get (rd2, "state", &state, NULL); |
| 279 |
g_assert_cmpint (state, ==, 3); |
| 280 |
|
| 281 |
/* rd1 busy */ |
| 282 |
g_signal_connect_swapped (rd1, "notify::owner-application-name", |
| 283 |
G_CALLBACK (g_main_loop_quit), f->base.loop); |
| 284 |
g_main_loop_run (f->base.loop); |
| 285 |
g_object_get (rd1, "state", &state, NULL); |
| 286 |
g_assert_cmpint (state, ==, 1); |
| 287 |
g_object_get (rd1, "owner-application-name", &str, NULL); |
| 288 |
g_assert_cmpstr (str, ==, "Other Server"); |
| 289 |
g_free (str); |
| 290 |
g_signal_handlers_disconnect_by_func (rd1, G_CALLBACK (g_main_loop_quit), |
| 291 |
f->base.loop); |
| 292 |
|
| 293 |
/* try to acquire back with lower priority */ |
| 294 |
wp_info ("rd1 acquire, lower prio"); |
| 295 |
g_signal_emit_by_name (rd1, "acquire"); |
| 296 |
|
| 297 |
/* ... expect this to fail */ |
| 298 |
f->expected_rd1_state = 1; |
| 299 |
g_main_loop_run (f->base.loop); |
| 300 |
g_object_get (rd1, "state", &state, NULL); |
| 301 |
g_assert_cmpint (state, ==, 1); |
| 302 |
|
| 303 |
g_object_get (rd1, "owner-application-name", &str, NULL); |
| 304 |
g_assert_cmpstr (str, ==, "Other Server"); |
| 305 |
g_free (str); |
| 306 |
|
| 307 |
/* release */ |
| 308 |
wp_info ("rd2 release"); |
| 309 |
g_signal_emit_by_name (rd2, "release"); |
| 310 |
g_object_get (rd2, "state", &state, NULL); |
| 311 |
g_assert_cmpint (state, ==, 2); |
| 312 |
|
| 313 |
f->expected_rd1_state = 2; |
| 314 |
g_main_loop_run (f->base.loop); |
| 315 |
g_object_get (rd1, "state", &state, NULL); |
| 316 |
g_assert_cmpint (state, ==, 2); |
| 317 |
|
| 318 |
g_object_get (rd1, "owner-application-name", &str, NULL); |
| 319 |
g_assert_null (str); |
| 320 |
|
| 321 |
g_clear_object (&rd1); |
| 322 |
g_clear_object (&rd2); |
| 323 |
} |
| 324 |
|
| 325 |
gint |
| 326 |
main (gint argc, gchar *argv[]) |
| 327 |
{ |
| 328 |
g_test_init (&argc, &argv, NULL); |
| 329 |
wp_init (WP_INIT_ALL); |
| 330 |
|
| 331 |
g_test_add ("/modules/rd/plugin", RdTestFixture, NULL, |
| 332 |
test_rd_setup, test_rd_plugin, test_rd_teardown); |
| 333 |
g_test_add ("/modules/rd/conn_closed", RdTestFixture, NULL, |
| 334 |
test_rd_setup, test_rd_conn_closed, test_rd_teardown); |
| 335 |
g_test_add ("/modules/rd/acquire_release", RdTestFixture, NULL, |
| 336 |
test_rd_setup, test_rd_acquire_release, test_rd_teardown); |
| 337 |
|
| 338 |
return g_test_run (); |
| 339 |
} |