1 | /* Unit tests for GIOModule |
2 | * Copyright (C) 2013 Red Hat, Inc |
3 | * Author: Matthias Clasen |
4 | * |
5 | * This work is provided "as is"; redistribution and modification |
6 | * in whole or in part, in any medium, physical or electronic is |
7 | * permitted without restriction. |
8 | * |
9 | * This work is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12 | * |
13 | * In no event shall the authors or contributors be liable for any |
14 | * direct, indirect, incidental, special, exemplary, or consequential |
15 | * damages (including, but not limited to, procurement of substitute |
16 | * goods or services; loss of use, data, or profits; or business |
17 | * interruption) however caused and on any theory of liability, whether |
18 | * in contract, strict liability, or tort (including negligence or |
19 | * otherwise) arising in any way out of the use of this software, even |
20 | * if advised of the possibility of such damage. |
21 | */ |
22 | |
23 | #include <gio/gio.h> |
24 | #include <glibconfig.h> |
25 | |
26 | #ifdef _MSC_VER |
27 | # define MODULE_FILENAME_PREFIX "" |
28 | #else |
29 | # define MODULE_FILENAME_PREFIX "lib" |
30 | #endif |
31 | |
32 | static void |
33 | test_extension_point (void) |
34 | { |
35 | GIOExtensionPoint *ep, *ep2; |
36 | GIOExtension *ext; |
37 | GList *list; |
38 | GType req; |
39 | GTypeClass *class; |
40 | |
41 | ep = g_io_extension_point_lookup (name: "test-extension-point" ); |
42 | g_assert_null (ep); |
43 | ep = g_io_extension_point_register (name: "test-extension-point" ); |
44 | ep2 = g_io_extension_point_lookup (name: "test-extension-point" ); |
45 | g_assert (ep2 == ep); |
46 | |
47 | req = g_io_extension_point_get_required_type (extension_point: ep); |
48 | g_assert (req == G_TYPE_INVALID); |
49 | g_io_extension_point_set_required_type (extension_point: ep, G_TYPE_OBJECT); |
50 | req = g_io_extension_point_get_required_type (extension_point: ep); |
51 | g_assert (req == G_TYPE_OBJECT); |
52 | |
53 | list = g_io_extension_point_get_extensions (extension_point: ep); |
54 | g_assert_null (list); |
55 | |
56 | g_io_extension_point_implement (extension_point_name: "test-extension-point" , |
57 | G_TYPE_VFS, |
58 | extension_name: "extension1" , |
59 | priority: 10); |
60 | |
61 | g_io_extension_point_implement (extension_point_name: "test-extension-point" , |
62 | G_TYPE_OBJECT, |
63 | extension_name: "extension2" , |
64 | priority: 20); |
65 | |
66 | list = g_io_extension_point_get_extensions (extension_point: ep); |
67 | g_assert_cmpint (g_list_length (list), ==, 2); |
68 | |
69 | ext = list->data; |
70 | g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension2" ); |
71 | g_assert (g_io_extension_get_type (ext) == G_TYPE_OBJECT); |
72 | g_assert (g_io_extension_get_priority (ext) == 20); |
73 | class = g_io_extension_ref_class (extension: ext); |
74 | g_assert (class == g_type_class_peek (G_TYPE_OBJECT)); |
75 | g_type_class_unref (g_class: class); |
76 | |
77 | ext = list->next->data; |
78 | g_assert_cmpstr (g_io_extension_get_name (ext), ==, "extension1" ); |
79 | g_assert (g_io_extension_get_type (ext) == G_TYPE_VFS); |
80 | g_assert (g_io_extension_get_priority (ext) == 10); |
81 | } |
82 | |
83 | static void |
84 | test_module_scan_all (void) |
85 | { |
86 | #ifdef GLIB_STATIC_COMPILATION |
87 | /* The plugin module is statically linked with a separate copy |
88 | * of GLib so g_io_extension_point_implement won't work. */ |
89 | g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds." ); |
90 | return; |
91 | #endif |
92 | |
93 | if (g_test_subprocess ()) |
94 | { |
95 | GIOExtensionPoint *ep; |
96 | GIOExtension *ext; |
97 | GList *list; |
98 | ep = g_io_extension_point_register (name: "test-extension-point" ); |
99 | g_io_modules_scan_all_in_directory (dirname: g_test_get_filename (file_type: G_TEST_BUILT, first_path: "modules" , NULL)); |
100 | list = g_io_extension_point_get_extensions (extension_point: ep); |
101 | g_assert_cmpint (g_list_length (list), ==, 2); |
102 | ext = list->data; |
103 | g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-b" ); |
104 | ext = list->next->data; |
105 | g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a" ); |
106 | return; |
107 | } |
108 | g_test_trap_subprocess (NULL, usec_timeout: 0, test_flags: 7); |
109 | g_test_trap_assert_passed (); |
110 | } |
111 | |
112 | static void |
113 | test_module_scan_all_with_scope (void) |
114 | { |
115 | #ifdef GLIB_STATIC_COMPILATION |
116 | /* Disabled for the same reason as test_module_scan_all. */ |
117 | g_test_skip ("GIOExtensionPoint with dynamic modules isn't supported in static builds." ); |
118 | return; |
119 | #endif |
120 | |
121 | if (g_test_subprocess ()) |
122 | { |
123 | GIOExtensionPoint *ep; |
124 | GIOModuleScope *scope; |
125 | GIOExtension *ext; |
126 | GList *list; |
127 | |
128 | ep = g_io_extension_point_register (name: "test-extension-point" ); |
129 | scope = g_io_module_scope_new (flags: G_IO_MODULE_SCOPE_BLOCK_DUPLICATES); |
130 | g_io_module_scope_block (scope, MODULE_FILENAME_PREFIX "testmoduleb." G_MODULE_SUFFIX); |
131 | g_io_modules_scan_all_in_directory_with_scope (dirname: g_test_get_filename (file_type: G_TEST_BUILT, first_path: "modules" , NULL), scope); |
132 | list = g_io_extension_point_get_extensions (extension_point: ep); |
133 | g_assert_cmpint (g_list_length (list), ==, 1); |
134 | ext = list->data; |
135 | g_assert_cmpstr (g_io_extension_get_name (ext), ==, "test-a" ); |
136 | g_io_module_scope_free (scope); |
137 | return; |
138 | } |
139 | g_test_trap_subprocess (NULL, usec_timeout: 0, test_flags: 7); |
140 | g_test_trap_assert_passed (); |
141 | } |
142 | |
143 | int |
144 | main (int argc, char *argv[]) |
145 | { |
146 | g_test_init (argc: &argc, argv: &argv, NULL); |
147 | |
148 | g_test_add_func (testpath: "/giomodule/extension-point" , test_func: test_extension_point); |
149 | g_test_add_func (testpath: "/giomodule/module-scan-all" , test_func: test_module_scan_all); |
150 | g_test_add_func (testpath: "/giomodule/module-scan-all-with-scope" , test_func: test_module_scan_all_with_scope); |
151 | |
152 | return g_test_run (); |
153 | } |
154 | |