1 /* GLib testing framework examples and tests
3 * Copyright (C) 2011 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "test_resources2.h"
25 test_resource (GResource *resource)
28 gboolean found, success;
36 found = g_resource_get_info (resource,
38 G_RESOURCE_LOOKUP_FLAGS_NONE,
39 &size, &flags, &error);
41 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
42 g_clear_error (&error);
44 found = g_resource_get_info (resource,
46 G_RESOURCE_LOOKUP_FLAGS_NONE,
47 &size, &flags, &error);
49 g_assert_no_error (error);
51 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
53 found = g_resource_get_info (resource,
54 "/a_prefix/test2.txt",
55 G_RESOURCE_LOOKUP_FLAGS_NONE,
56 &size, &flags, &error);
58 g_assert_no_error (error);
60 g_assert (flags == 0);
62 found = g_resource_get_info (resource,
63 "/a_prefix/test2-alias.txt",
64 G_RESOURCE_LOOKUP_FLAGS_NONE,
65 &size, &flags, &error);
67 g_assert_no_error (error);
69 g_assert (flags == 0);
71 data = g_resource_lookup_data (resource,
73 G_RESOURCE_LOOKUP_FLAGS_NONE,
75 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
76 g_assert_no_error (error);
79 in = g_resource_open_stream (resource,
81 G_RESOURCE_LOOKUP_FLAGS_NONE,
83 g_assert (in != NULL);
84 g_assert_no_error (error);
86 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
90 g_assert_no_error (error);
93 g_assert_cmpstr (buffer, ==, "test1\n");
95 g_input_stream_close (in, NULL, NULL);
97 data = g_resource_lookup_data (resource,
98 "/a_prefix/test2.txt",
99 G_RESOURCE_LOOKUP_FLAGS_NONE,
101 g_assert (data != NULL);
102 g_assert_no_error (error);
103 size = g_bytes_get_size (data);
104 g_assert (size == 6);
105 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
106 g_bytes_unref (data);
108 data = g_resource_lookup_data (resource,
109 "/a_prefix/test2-alias.txt",
110 G_RESOURCE_LOOKUP_FLAGS_NONE,
112 g_assert (data != NULL);
113 g_assert_no_error (error);
114 size = g_bytes_get_size (data);
115 g_assert (size == 6);
116 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
117 g_bytes_unref (data);
119 children = g_resource_enumerate_children (resource,
121 G_RESOURCE_LOOKUP_FLAGS_NONE,
123 g_assert (children == NULL);
124 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
125 g_clear_error (&error);
127 children = g_resource_enumerate_children (resource,
129 G_RESOURCE_LOOKUP_FLAGS_NONE,
131 g_assert (children != NULL);
132 g_assert_no_error (error);
133 g_assert (g_strv_length (children) == 2);
134 g_strfreev (children);
138 test_resource_file (void)
141 GError *error = NULL;
143 resource = g_resource_load ("not-there", &error);
144 g_assert (resource == NULL);
145 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
146 g_clear_error (&error);
148 resource = g_resource_load ("test.gresource", &error);
149 g_assert (resource != NULL);
150 g_assert_no_error (error);
152 test_resource (resource);
153 g_resource_unref (resource);
157 test_resource_data (void)
160 GError *error = NULL;
161 gboolean loaded_file;
166 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
168 g_assert (loaded_file);
170 data = g_bytes_new_take (content, content_size);
171 resource = g_resource_new_from_data (data, &error);
172 g_assert (resource != NULL);
173 g_assert_no_error (error);
175 test_resource (resource);
177 g_resource_unref (resource);
181 test_resource_registred (void)
184 GError *error = NULL;
185 gboolean found, success;
193 resource = g_resource_load ("test.gresource", &error);
194 g_assert (resource != NULL);
195 g_assert_no_error (error);
197 found = g_resources_get_info ("/test1.txt",
198 G_RESOURCE_LOOKUP_FLAGS_NONE,
199 &size, &flags, &error);
201 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
202 g_clear_error (&error);
204 g_resources_register (resource);
206 found = g_resources_get_info ("/test1.txt",
207 G_RESOURCE_LOOKUP_FLAGS_NONE,
208 &size, &flags, &error);
210 g_assert_no_error (error);
211 g_assert (size == 6);
212 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
214 found = g_resources_get_info ("/a_prefix/test2.txt",
215 G_RESOURCE_LOOKUP_FLAGS_NONE,
216 &size, &flags, &error);
218 g_assert_no_error (error);
219 g_assert (size == 6);
220 g_assert (flags == 0);
222 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
223 G_RESOURCE_LOOKUP_FLAGS_NONE,
224 &size, &flags, &error);
226 g_assert_no_error (error);
227 g_assert (size == 6);
228 g_assert (flags == 0);
230 data = g_resources_lookup_data ("/test1.txt",
231 G_RESOURCE_LOOKUP_FLAGS_NONE,
233 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
234 g_assert_no_error (error);
235 g_bytes_unref (data);
237 in = g_resources_open_stream ("/test1.txt",
238 G_RESOURCE_LOOKUP_FLAGS_NONE,
240 g_assert (in != NULL);
241 g_assert_no_error (error);
243 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
247 g_assert_no_error (error);
248 g_assert (size == 6);
250 g_assert_cmpstr (buffer, ==, "test1\n");
252 g_input_stream_close (in, NULL, NULL);
255 data = g_resources_lookup_data ("/a_prefix/test2.txt",
256 G_RESOURCE_LOOKUP_FLAGS_NONE,
258 g_assert (data != NULL);
259 g_assert_no_error (error);
260 size = g_bytes_get_size (data);
261 g_assert (size == 6);
262 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
263 g_bytes_unref (data);
265 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
266 G_RESOURCE_LOOKUP_FLAGS_NONE,
268 g_assert (data != NULL);
269 g_assert_no_error (error);
270 size = g_bytes_get_size (data);
271 g_assert (size == 6);
272 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
273 g_bytes_unref (data);
275 children = g_resources_enumerate_children ("/not/here",
276 G_RESOURCE_LOOKUP_FLAGS_NONE,
278 g_assert (children == NULL);
279 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
280 g_clear_error (&error);
282 children = g_resources_enumerate_children ("/a_prefix",
283 G_RESOURCE_LOOKUP_FLAGS_NONE,
285 g_assert (children != NULL);
286 g_assert_no_error (error);
287 g_assert (g_strv_length (children) == 2);
289 g_resources_unregister (resource);
291 found = g_resources_get_info ("/test1.txt",
292 G_RESOURCE_LOOKUP_FLAGS_NONE,
293 &size, &flags, &error);
295 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
296 g_clear_error (&error);
300 test_resource_automatic (void)
302 GError *error = NULL;
308 found = g_resources_get_info ("/auto_loaded/test1.txt",
309 G_RESOURCE_LOOKUP_FLAGS_NONE,
310 &size, &flags, &error);
312 g_assert_no_error (error);
313 g_assert (size == 6);
314 g_assert (flags == 0);
316 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
317 G_RESOURCE_LOOKUP_FLAGS_NONE,
319 g_assert (data != NULL);
320 g_assert_no_error (error);
321 size = g_bytes_get_size (data);
322 g_assert (size == 6);
323 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
324 g_bytes_unref (data);
328 test_resource_manual (void)
330 GError *error = NULL;
336 found = g_resources_get_info ("/manual_loaded/test1.txt",
337 G_RESOURCE_LOOKUP_FLAGS_NONE,
338 &size, &flags, &error);
340 g_assert_no_error (error);
341 g_assert (size == 6);
342 g_assert (flags == 0);
344 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
345 G_RESOURCE_LOOKUP_FLAGS_NONE,
347 g_assert (data != NULL);
348 g_assert_no_error (error);
349 size = g_bytes_get_size (data);
350 g_assert (size == 6);
351 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
352 g_bytes_unref (data);
356 test_resource_module (void)
365 if (g_module_supported ())
369 dir = g_get_current_dir ();
371 path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin", NULL);
372 module = g_io_module_new (path);
377 found = g_resources_get_info ("/resourceplugin/test1.txt",
378 G_RESOURCE_LOOKUP_FLAGS_NONE,
379 &size, &flags, &error);
381 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
382 g_clear_error (&error);
384 g_type_module_use (G_TYPE_MODULE (module));
386 found = g_resources_get_info ("/resourceplugin/test1.txt",
387 G_RESOURCE_LOOKUP_FLAGS_NONE,
388 &size, &flags, &error);
390 g_assert_no_error (error);
391 g_assert (size == 6);
392 g_assert (flags == 0);
394 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
395 G_RESOURCE_LOOKUP_FLAGS_NONE,
397 g_assert (data != NULL);
398 g_assert_no_error (error);
399 size = g_bytes_get_size (data);
400 g_assert (size == 6);
401 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
402 g_bytes_unref (data);
404 g_type_module_unuse (G_TYPE_MODULE (module));
406 found = g_resources_get_info ("/resourceplugin/test1.txt",
407 G_RESOURCE_LOOKUP_FLAGS_NONE,
408 &size, &flags, &error);
410 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
411 g_clear_error (&error);
416 test_uri_query_info (void)
419 GError *error = NULL;
420 gboolean loaded_file;
426 const char *content_type;
428 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
430 g_assert (loaded_file);
432 data = g_bytes_new_take (content, content_size);
433 resource = g_resource_new_from_data (data, &error);
434 g_assert (resource != NULL);
435 g_assert_no_error (error);
437 g_resources_register (resource);
439 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
441 info = g_file_query_info (file, "*", 0, NULL, &error);
442 g_assert_no_error (error);
443 g_object_unref (file);
445 content_type = g_file_info_get_content_type (info);
446 g_assert (content_type);
447 g_assert_cmpstr (content_type, ==, "text/plain");
449 g_object_unref (info);
451 g_resources_unregister (resource);
452 g_resource_unref (resource);
460 g_test_init (&argc, &argv, NULL);
462 _g_test2_register_resource ();
464 g_test_add_func ("/resource/file", test_resource_file);
465 g_test_add_func ("/resource/data", test_resource_data);
466 g_test_add_func ("/resource/registred", test_resource_registred);
467 g_test_add_func ("/resource/manual", test_resource_manual);
468 #ifdef G_HAS_CONSTRUCTORS
469 g_test_add_func ("/resource/automatic", test_resource_automatic);
470 /* This only uses automatic resources too, so it tests the constructors and destructors */
471 g_test_add_func ("/resource/module", test_resource_module);
473 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);