Debian Codegraph @

tests/wp/component-loader.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2023 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8 #include "../common/base-test-fixture.h"
9
10 struct _WpTestPlugin
11 {
12 WpPlugin parent;
13 gboolean enabled;
14 };
15
16 #define WP_TYPE_TEST_PLUGIN (wp_test_plugin_get_type ())
17 G_DECLARE_FINAL_TYPE (WpTestPlugin, wp_test_plugin, WP, TEST_PLUGIN, WpPlugin)
18 G_DEFINE_TYPE (WpTestPlugin, wp_test_plugin, WP_TYPE_PLUGIN)
19
20 static void
21 wp_test_plugin_init (WpTestPlugin * self)
22 {
23 }
24
25 static void
26 wp_test_plugin_enable (WpPlugin * self, WpTransition * transition)
27 {
28 WP_TEST_PLUGIN (self)->enabled = TRUE;
29
30 if (g_str_equal (wp_plugin_get_name (self), "fail")) {
31 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
32 WP_LIBRARY_ERROR_INVALID_ARGUMENT, "fail"));
33 } else {
34 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
35 }
36 }
37
38 static void
39 wp_test_plugin_class_init (WpTestPluginClass * klass)
40 {
41 WpPluginClass *pclass = (WpPluginClass *) klass;
42 pclass->enable = wp_test_plugin_enable;
43 }
44
45
46 struct _WpTestCompLoader
47 {
48 GObject parent;
49 GPtrArray *history;
50 };
51
52 static void wp_test_comp_loader_iface_init (WpComponentLoaderInterface * iface);
53
54 #define WP_TYPE_TEST_COMP_LOADER (wp_test_comp_loader_get_type ())
55 G_DECLARE_FINAL_TYPE (WpTestCompLoader, wp_test_comp_loader,
56 WP, TEST_COMP_LOADER, GObject)
57 G_DEFINE_TYPE_WITH_CODE (WpTestCompLoader, wp_test_comp_loader,
58 G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (
59 WP_TYPE_COMPONENT_LOADER,
60 wp_test_comp_loader_iface_init))
61
62 static void
63 wp_test_comp_loader_init (WpTestCompLoader * self)
64 {
65 self->history = g_ptr_array_new_with_free_func (g_free);
66 }
67
68 static void
69 wp_test_comp_loader_finalize (GObject * self)
70 {
71 g_clear_pointer (&WP_TEST_COMP_LOADER (self)->history, g_ptr_array_unref);
72 G_OBJECT_CLASS (wp_test_comp_loader_parent_class)->finalize (self);
73 }
74
75 static void
76 wp_test_comp_loader_class_init (WpTestCompLoaderClass * klass)
77 {
78 GObjectClass *oclass = (GObjectClass *) klass;
79 oclass->finalize = wp_test_comp_loader_finalize;
80 }
81
82 static gboolean
83 wp_test_comp_loader_supports_type (WpComponentLoader * cl, const gchar * type)
84 {
85 return g_str_equal (type, "test");
86 }
87
88 static void
89 wp_test_comp_loader_load (WpComponentLoader * self, WpCore * core,
90 const gchar * component, const gchar * type, WpSpaJson * args,
91 GCancellable * cancellable, GAsyncReadyCallback callback, gpointer data)
92 {
93 g_autoptr (GTask) task = g_task_new (self, cancellable, callback, data);
94 GObject *plugin = g_object_new (WP_TYPE_TEST_PLUGIN,
95 "name", component,
96 "core", core,
97 NULL);
98 g_ptr_array_add (WP_TEST_COMP_LOADER (self)->history, g_strdup (component));
99 g_task_return_pointer (task, plugin, g_object_unref);
100 }
101
102 static GObject *
103 wp_test_comp_loader_load_finish (WpComponentLoader * self,
104 GAsyncResult * res, GError ** error)
105 {
106 return g_task_propagate_pointer (G_TASK (res), error);
107 }
108
109 static void
110 wp_test_comp_loader_iface_init (WpComponentLoaderInterface * iface)
111 {
112 iface->supports_type = wp_test_comp_loader_supports_type;
113 iface->load = wp_test_comp_loader_load;
114 iface->load_finish = wp_test_comp_loader_load_finish;
115 }
116
117
118 typedef struct {
119 WpBaseTestFixture base;
120 WpTestCompLoader *loader;
121 } TestFixture;
122
123 static void
124 test_setup (TestFixture *self, gconstpointer user_data)
125 {
126 wp_base_test_fixture_setup (&self->base, 0);
127 self->loader = g_object_new (WP_TYPE_TEST_COMP_LOADER, NULL);
128 wp_core_register_object (self->base.core, self->loader);
129 }
130
131 static void
132 test_teardown (TestFixture *self, gconstpointer user_data)
133 {
134 wp_base_test_fixture_teardown (&self->base);
135 }
136
137 static void
138 on_component_loaded (WpCore * core, GAsyncResult * res, TestFixture *f)
139 {
140 gboolean loaded;
141 GError *error = NULL;
142
143 loaded = wp_core_load_component_finish (core, res, &error);
144 g_assert_no_error (error);
145 g_assert_true (loaded);
146
147 g_main_loop_quit (f->base.loop);
148 }
149
150 static void
151 test_load (TestFixture *f, gconstpointer data)
152 {
153 wp_core_load_component (f->base.core, "name123", "test", NULL,
154 "feature.name123", NULL, (GAsyncReadyCallback) on_component_loaded, f);
155 g_main_loop_run (f->base.loop);
156
157 g_autoptr (WpPlugin) plugin = wp_plugin_find (f->base.core, "name123");
158 g_assert_nonnull (plugin);
159 g_assert_true (WP_IS_TEST_PLUGIN (plugin));
160 g_assert_true (WP_TEST_PLUGIN (plugin)->enabled);
161 g_assert_true (wp_core_test_feature (f->base.core, "feature.name123"));
162 }
163
164 static void
165 on_component_failed (WpCore * core, GAsyncResult * res, TestFixture *f)
166 {
167 gboolean loaded;
168 g_autoptr (GError) error = NULL;
169
170 loaded = wp_core_load_component_finish (core, res, &error);
171 g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT);
172 g_assert_false (loaded);
173
174 g_main_loop_quit (f->base.loop);
175 }
176
177 static void
178 test_load_failure (TestFixture *f, gconstpointer data)
179 {
180 wp_core_load_component (f->base.core, "fail", "test", NULL,
181 "feature.fail", NULL, (GAsyncReadyCallback) on_component_failed, f);
182 g_main_loop_run (f->base.loop);
183
184 g_assert_cmpuint (f->loader->history->len, ==, 1);
185 g_assert_cmpstr (f->loader->history->pdata[0], ==, "fail");
186
187 g_autoptr (WpPlugin) plugin = wp_plugin_find (f->base.core, "fail");
188 g_assert_null (plugin);
189 g_assert_false (wp_core_test_feature (f->base.core, "feature.fail"));
190 }
191
192 static void
193 test_dependencies_setup (TestFixture *f, gconstpointer data)
194 {
195 f->base.conf_file =
196 g_strdup_printf ("%s/component-loader.conf", g_getenv ("G_TEST_SRCDIR"));
197 test_setup (f, data);
198 }
199
200 static void
201 test_dependencies (TestFixture *f, gconstpointer data)
202 {
203 wp_core_load_component (f->base.core, "test", "profile", NULL,
204 NULL, NULL, (GAsyncReadyCallback) on_component_loaded, f);
205 g_main_loop_run (f->base.loop);
206
207 // NULL-terminate the array
208 g_ptr_array_add (f->loader->history, NULL);
209
210 /* verify the order of loading the plugins was as expected */
211 const gchar *expected[] = {
212 "five", "one", "seven", "ten", "eleven", "six", "two", "three", "four", "nine", NULL };
213 g_assert_cmpstrv (f->loader->history->pdata, expected);
214
215 g_assert_true (wp_core_test_feature (f->base.core, "support.one"));
216 g_assert_true (wp_core_test_feature (f->base.core, "support.two"));
217 g_assert_true (wp_core_test_feature (f->base.core, "support.three"));
218 g_assert_true (wp_core_test_feature (f->base.core, "support.four"));
219 g_assert_true (wp_core_test_feature (f->base.core, "virtual.four"));
220 g_assert_true (wp_core_test_feature (f->base.core, "support.five"));
221 g_assert_true (wp_core_test_feature (f->base.core, "support.six"));
222 g_assert_false (wp_core_test_feature (f->base.core, "support.seven"));
223 g_assert_false (wp_core_test_feature (f->base.core, "support.eight"));
224 g_assert_true (wp_core_test_feature (f->base.core, "support.nine"));
225 g_assert_true (wp_core_test_feature (f->base.core, "support.ten"));
226 g_assert_true (wp_core_test_feature (f->base.core, "support.eleven"));
227 }
228
229 gint
230 main (gint argc, gchar *argv[])
231 {
232 g_test_init (&argc, &argv, NULL);
233 wp_init (WP_INIT_ALL);
234
235 g_test_add ("/wp/comploader/load", TestFixture, NULL,
236 test_setup, test_load, test_teardown);
237 g_test_add ("/wp/comploader/load_failure", TestFixture, NULL,
238 test_setup, test_load_failure, test_teardown);
239 g_test_add ("/wp/comploader/dependencies", TestFixture, NULL,
240 test_dependencies_setup, test_dependencies, test_teardown);
241
242 return g_test_run ();
243 }