Debian Codegraph @

tests/modules/dbus-connection.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2022-2023 Collabora Ltd.
4 * @author Julian Bouzas <[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 } TestFixture;
17
18 static void
19 test_setup (TestFixture *self, gconstpointer user_data)
20 {
21 wp_base_test_fixture_setup (&self->base, 0);
22 self->test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
23 g_test_dbus_up (self->test_dbus);
24 }
25
26 static void
27 test_teardown (TestFixture *self, gconstpointer user_data)
28 {
29 g_test_dbus_down (self->test_dbus);
30 g_clear_object (&self->test_dbus);
31 wp_base_test_fixture_teardown (&self->base);
32 }
33
34 static void
35 on_dbus_state_changed (GObject * dbus, GParamSpec * spec, TestFixture *f)
36 {
37 WpDBusConnectionState state = -1;
38 g_object_get (dbus, "state", &state, NULL);
39 g_assert_cmpint (state, ==, WP_DBUS_CONNECTION_STATE_CLOSED);
40
41 g_object_set_data (G_OBJECT (dbus), "state-closed", GUINT_TO_POINTER (0x1));
42 }
43
44 static void
45 on_plugin_loaded (WpCore * core, GAsyncResult * res, TestFixture *f)
46 {
47 gboolean loaded;
48 GError *error = NULL;
49
50 loaded = wp_core_load_component_finish (core, res, &error);
51 g_assert_no_error (error);
52 g_assert_true (loaded);
53
54 g_main_loop_quit (f->base.loop);
55 }
56
57 static void
58 test_dbus_connection (TestFixture *f, gconstpointer user_data)
59 {
60 g_autoptr (WpPlugin) dbus = NULL;
61 g_autoptr (GDBusConnection) conn = NULL;
62 WpDBusConnectionState state = -1;
63
64 wp_core_load_component (f->base.core,
65 "libwireplumber-module-dbus-connection", "module", NULL, NULL, NULL,
66 (GAsyncReadyCallback) on_plugin_loaded, f);
67 g_main_loop_run (f->base.loop);
68
69 dbus = wp_plugin_find (f->base.core, "dbus-connection");
70 g_assert_nonnull (dbus);
71
72 g_object_get (dbus, "state", &state, NULL);
73 g_assert_cmpint (state, ==, WP_DBUS_CONNECTION_STATE_CONNECTED);
74
75 g_object_get (dbus, "connection", &conn, NULL);
76 g_assert_nonnull (conn);
77 g_clear_object (&conn);
78
79 g_signal_connect (dbus, "notify::state", G_CALLBACK (on_dbus_state_changed),
80 f);
81
82 wp_object_deactivate (WP_OBJECT (dbus), WP_PLUGIN_FEATURE_ENABLED);
83
84 // set by on_dbus_state_changed
85 g_assert_cmphex (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (dbus), "state-closed")), ==, 0x1);
86
87 g_object_get (dbus, "connection", &conn, NULL);
88 g_assert_null (conn);
89 }
90
91 gint
92 main (gint argc, gchar *argv[])
93 {
94 g_test_init (&argc, &argv, NULL);
95 wp_init (WP_INIT_ALL);
96
97 g_test_add ("/wp/dbus/connection", TestFixture, NULL,
98 test_setup, test_dbus_connection, test_teardown);
99
100 return g_test_run ();
101 }