1 /* Unit tests for GIOModule
2 * Copyright (C) 2013 Red Hat, Inc
3 * Author: Matthias Clasen
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
26 #include <glibconfig.h>
30 #define MODULE_FILENAME(x) "" x ".dll"
32 #define MODULE_FILENAME(x) "lib" x ".dll"
34 #elif defined(__APPLE__)
35 #define MODULE_FILENAME(x) "lib" x ".dylib"
37 #define MODULE_FILENAME(x) "lib" x ".so"
41 test_extension_point (void)
43 GIOExtensionPoint *ep, *ep2;
49 ep = g_io_extension_point_lookup ("test-extension-point");
51 ep = g_io_extension_point_register ("test-extension-point");
52 ep2 = g_io_extension_point_lookup ("test-extension-point");
55 req = g_io_extension_point_get_required_type (ep);
56 g_assert (req == G_TYPE_INVALID);
57 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
58 req = g_io_extension_point_get_required_type (ep);
59 g_assert (req == G_TYPE_OBJECT);
61 list = g_io_extension_point_get_extensions (ep);
64 g_io_extension_point_implement ("test-extension-point",
69 g_io_extension_point_implement ("test-extension-point",
74 list = g_io_extension_point_get_extensions (ep);
75 g_assert_cmpint (g_list_length (list), ==, 2);
78 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension2");
79 g_assert (g_io_extension_get_type (ext) == G_TYPE_OBJECT);
80 g_assert (g_io_extension_get_priority (ext) == 20);
81 class = g_io_extension_ref_class (ext);
82 g_assert (class == g_type_class_peek (G_TYPE_OBJECT));
83 g_type_class_unref (class);
85 ext = list->next->data;
86 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension1");
87 g_assert (g_io_extension_get_type (ext) == G_TYPE_VFS);
88 g_assert (g_io_extension_get_priority (ext) == 10);
91 #define INHERIT_ALL (G_TEST_SUBPROCESS_INHERIT_STDIN | \
92 G_TEST_SUBPROCESS_INHERIT_STDOUT | \
93 G_TEST_SUBPROCESS_INHERIT_STDERR)
96 test_module_scan_all (void)
98 #ifdef GLIB_STATIC_COMPILATION
99 /* The plugin module is statically linked with a separate copy
100 * of GLib so g_io_extension_point_implement won't work. */
101 g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
105 if (g_test_subprocess ())
107 GIOExtensionPoint *ep;
110 ep = g_io_extension_point_register ("test-extension-point");
111 g_io_modules_scan_all_in_directory (g_test_get_filename (G_TEST_BUILT, "modules", NULL));
112 list = g_io_extension_point_get_extensions (ep);
113 g_assert_cmpint (g_list_length (list), ==, 2);
115 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-b");
116 ext = list->next->data;
117 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
120 g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
121 g_test_trap_assert_passed ();
125 test_module_scan_all_with_scope (void)
127 #ifdef GLIB_STATIC_COMPILATION
128 /* Disabled for the same reason as test_module_scan_all. */
129 g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds.");
133 if (g_test_subprocess ())
135 GIOExtensionPoint *ep;
136 GIOModuleScope *scope;
140 ep = g_io_extension_point_register ("test-extension-point");
141 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
142 g_io_module_scope_block (scope, MODULE_FILENAME ("testmoduleb"));
143 g_io_modules_scan_all_in_directory_with_scope (g_test_get_filename (G_TEST_BUILT, "modules", NULL), scope);
144 list = g_io_extension_point_get_extensions (ep);
145 g_assert_cmpint (g_list_length (list), ==, 1);
147 g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a");
148 g_io_module_scope_free (scope);
151 g_test_trap_subprocess (NULL, 0, INHERIT_ALL);
152 g_test_trap_assert_passed ();
156 main (int argc, char *argv[])
158 g_test_init (&argc, &argv, NULL);
160 g_test_add_func ("/giomodule/extension-point", test_extension_point);
161 g_test_add_func ("/giomodule/module-scan-all", test_module_scan_all);
162 g_test_add_func ("/giomodule/module-scan-all-with-scope", test_module_scan_all_with_scope);
164 return g_test_run ();