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 "gconstructor.h"
23 #include "test_resources2.h"
26 test_resource (GResource *resource)
29 gboolean found, success;
37 found = g_resource_get_info (resource,
39 G_RESOURCE_LOOKUP_FLAGS_NONE,
40 &size, &flags, &error);
42 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
43 g_clear_error (&error);
45 found = g_resource_get_info (resource,
47 G_RESOURCE_LOOKUP_FLAGS_NONE,
48 &size, &flags, &error);
50 g_assert_no_error (error);
51 g_assert_cmpint (size, ==, 6);
52 g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
54 found = g_resource_get_info (resource,
55 "/a_prefix/test2.txt",
56 G_RESOURCE_LOOKUP_FLAGS_NONE,
57 &size, &flags, &error);
59 g_assert_no_error (error);
60 g_assert_cmpint (size, ==, 6);
61 g_assert_cmpuint (flags, ==, 0);
63 found = g_resource_get_info (resource,
64 "/a_prefix/test2-alias.txt",
65 G_RESOURCE_LOOKUP_FLAGS_NONE,
66 &size, &flags, &error);
68 g_assert_no_error (error);
69 g_assert_cmpint (size, ==, 6);
70 g_assert_cmpuint (flags, ==, 0);
72 data = g_resource_lookup_data (resource,
74 G_RESOURCE_LOOKUP_FLAGS_NONE,
76 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
77 g_assert_no_error (error);
80 in = g_resource_open_stream (resource,
82 G_RESOURCE_LOOKUP_FLAGS_NONE,
84 g_assert (in != NULL);
85 g_assert_no_error (error);
87 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
91 g_assert_no_error (error);
92 g_assert_cmpint (size, ==, 6);
94 g_assert_cmpstr (buffer, ==, "test1\n");
96 g_input_stream_close (in, NULL, &error);
97 g_assert_no_error (error);
100 data = g_resource_lookup_data (resource,
101 "/a_prefix/test2.txt",
102 G_RESOURCE_LOOKUP_FLAGS_NONE,
104 g_assert (data != NULL);
105 g_assert_no_error (error);
106 size = g_bytes_get_size (data);
107 g_assert_cmpint (size, ==, 6);
108 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
109 g_bytes_unref (data);
111 data = g_resource_lookup_data (resource,
112 "/a_prefix/test2-alias.txt",
113 G_RESOURCE_LOOKUP_FLAGS_NONE,
115 g_assert (data != NULL);
116 g_assert_no_error (error);
117 size = g_bytes_get_size (data);
118 g_assert_cmpint (size, ==, 6);
119 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
120 g_bytes_unref (data);
122 children = g_resource_enumerate_children (resource,
124 G_RESOURCE_LOOKUP_FLAGS_NONE,
126 g_assert (children == NULL);
127 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
128 g_clear_error (&error);
130 children = g_resource_enumerate_children (resource,
132 G_RESOURCE_LOOKUP_FLAGS_NONE,
134 g_assert (children != NULL);
135 g_assert_no_error (error);
136 g_assert_cmpint (g_strv_length (children), ==, 2);
137 g_strfreev (children);
141 test_resource_file (void)
144 GError *error = NULL;
146 resource = g_resource_load ("not-there", &error);
147 g_assert (resource == NULL);
148 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
149 g_clear_error (&error);
151 resource = g_resource_load ("test.gresource", &error);
152 g_assert (resource != NULL);
153 g_assert_no_error (error);
155 test_resource (resource);
156 g_resource_unref (resource);
160 test_resource_data (void)
163 GError *error = NULL;
164 gboolean loaded_file;
169 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
171 g_assert (loaded_file);
173 data = g_bytes_new_take (content, content_size);
174 resource = g_resource_new_from_data (data, &error);
175 g_bytes_unref (data);
176 g_assert (resource != NULL);
177 g_assert_no_error (error);
179 test_resource (resource);
181 g_resource_unref (resource);
185 test_resource_registered (void)
188 GError *error = NULL;
189 gboolean found, success;
197 resource = g_resource_load ("test.gresource", &error);
198 g_assert (resource != NULL);
199 g_assert_no_error (error);
201 found = g_resources_get_info ("/test1.txt",
202 G_RESOURCE_LOOKUP_FLAGS_NONE,
203 &size, &flags, &error);
205 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
206 g_clear_error (&error);
208 g_resources_register (resource);
210 found = g_resources_get_info ("/test1.txt",
211 G_RESOURCE_LOOKUP_FLAGS_NONE,
212 &size, &flags, &error);
214 g_assert_no_error (error);
215 g_assert_cmpint (size, ==, 6);
216 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
218 found = g_resources_get_info ("/a_prefix/test2.txt",
219 G_RESOURCE_LOOKUP_FLAGS_NONE,
220 &size, &flags, &error);
222 g_assert_no_error (error);
223 g_assert_cmpint (size, ==, 6);
224 g_assert_cmpint (flags, ==, 0);
226 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
227 G_RESOURCE_LOOKUP_FLAGS_NONE,
228 &size, &flags, &error);
230 g_assert_no_error (error);
231 g_assert_cmpint (size, ==, 6);
232 g_assert_cmpuint (flags, ==, 0);
234 data = g_resources_lookup_data ("/test1.txt",
235 G_RESOURCE_LOOKUP_FLAGS_NONE,
237 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
238 g_assert_no_error (error);
239 g_bytes_unref (data);
241 in = g_resources_open_stream ("/test1.txt",
242 G_RESOURCE_LOOKUP_FLAGS_NONE,
244 g_assert (in != NULL);
245 g_assert_no_error (error);
247 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
251 g_assert_no_error (error);
252 g_assert_cmpint (size, ==, 6);
254 g_assert_cmpstr (buffer, ==, "test1\n");
256 g_input_stream_close (in, NULL, &error);
257 g_assert_no_error (error);
258 g_clear_object (&in);
260 data = g_resources_lookup_data ("/a_prefix/test2.txt",
261 G_RESOURCE_LOOKUP_FLAGS_NONE,
263 g_assert (data != NULL);
264 g_assert_no_error (error);
265 size = g_bytes_get_size (data);
266 g_assert_cmpint (size, ==, 6);
267 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
268 g_bytes_unref (data);
270 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
271 G_RESOURCE_LOOKUP_FLAGS_NONE,
273 g_assert (data != NULL);
274 g_assert_no_error (error);
275 size = g_bytes_get_size (data);
276 g_assert_cmpint (size, ==, 6);
277 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
278 g_bytes_unref (data);
280 children = g_resources_enumerate_children ("/not/here",
281 G_RESOURCE_LOOKUP_FLAGS_NONE,
283 g_assert (children == NULL);
284 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
285 g_clear_error (&error);
287 children = g_resources_enumerate_children ("/a_prefix",
288 G_RESOURCE_LOOKUP_FLAGS_NONE,
290 g_assert (children != NULL);
291 g_assert_no_error (error);
292 g_assert_cmpint (g_strv_length (children), ==, 2);
293 g_strfreev (children);
295 g_resources_unregister (resource);
296 g_resource_unref (resource);
298 found = g_resources_get_info ("/test1.txt",
299 G_RESOURCE_LOOKUP_FLAGS_NONE,
300 &size, &flags, &error);
302 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
303 g_clear_error (&error);
307 test_resource_automatic (void)
309 GError *error = NULL;
315 found = g_resources_get_info ("/auto_loaded/test1.txt",
316 G_RESOURCE_LOOKUP_FLAGS_NONE,
317 &size, &flags, &error);
319 g_assert_no_error (error);
320 g_assert_cmpint (size, ==, 6);
321 g_assert_cmpint (flags, ==, 0);
323 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
324 G_RESOURCE_LOOKUP_FLAGS_NONE,
326 g_assert (data != NULL);
327 g_assert_no_error (error);
328 size = g_bytes_get_size (data);
329 g_assert_cmpint (size, ==, 6);
330 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
331 g_bytes_unref (data);
335 test_resource_manual (void)
337 GError *error = NULL;
343 found = g_resources_get_info ("/manual_loaded/test1.txt",
344 G_RESOURCE_LOOKUP_FLAGS_NONE,
345 &size, &flags, &error);
347 g_assert_no_error (error);
348 g_assert_cmpint (size, ==, 6);
349 g_assert_cmpuint (flags, ==, 0);
351 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
352 G_RESOURCE_LOOKUP_FLAGS_NONE,
354 g_assert (data != NULL);
355 g_assert_no_error (error);
356 size = g_bytes_get_size (data);
357 g_assert_cmpint (size, ==, 6);
358 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
359 g_bytes_unref (data);
363 test_resource_module (void)
372 if (g_module_supported ())
376 dir = g_get_current_dir ();
378 path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin", NULL);
379 module = g_io_module_new (path);
385 found = g_resources_get_info ("/resourceplugin/test1.txt",
386 G_RESOURCE_LOOKUP_FLAGS_NONE,
387 &size, &flags, &error);
389 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
390 g_clear_error (&error);
392 g_type_module_use (G_TYPE_MODULE (module));
394 found = g_resources_get_info ("/resourceplugin/test1.txt",
395 G_RESOURCE_LOOKUP_FLAGS_NONE,
396 &size, &flags, &error);
398 g_assert_no_error (error);
399 g_assert_cmpint (size, ==, 6);
400 g_assert_cmpuint (flags, ==, 0);
402 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
403 G_RESOURCE_LOOKUP_FLAGS_NONE,
405 g_assert (data != NULL);
406 g_assert_no_error (error);
407 size = g_bytes_get_size (data);
408 g_assert_cmpint (size, ==, 6);
409 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
410 g_bytes_unref (data);
412 g_type_module_unuse (G_TYPE_MODULE (module));
414 found = g_resources_get_info ("/resourceplugin/test1.txt",
415 G_RESOURCE_LOOKUP_FLAGS_NONE,
416 &size, &flags, &error);
418 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
419 g_clear_error (&error);
424 test_uri_query_info (void)
427 GError *error = NULL;
428 gboolean loaded_file;
434 const char *content_type;
436 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
438 g_assert (loaded_file);
440 data = g_bytes_new_take (content, content_size);
441 resource = g_resource_new_from_data (data, &error);
442 g_bytes_unref (data);
443 g_assert (resource != NULL);
444 g_assert_no_error (error);
446 g_resources_register (resource);
448 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
450 info = g_file_query_info (file, "*", 0, NULL, &error);
451 g_assert_no_error (error);
453 content_type = g_file_info_get_content_type (info);
454 g_assert (content_type);
455 g_assert_cmpstr (content_type, ==, "text/plain");
457 g_object_unref (info);
459 g_object_unref (file);
461 g_resources_unregister (resource);
462 g_resource_unref (resource);
469 GError *error = NULL;
470 gboolean loaded_file;
477 GFile *file2, *parent;
478 GFileEnumerator *enumerator;
480 GFileAttributeInfoList *attrs;
482 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
484 g_assert (loaded_file);
486 data = g_bytes_new_take (content, content_size);
487 resource = g_resource_new_from_data (data, &error);
488 g_bytes_unref (data);
489 g_assert (resource != NULL);
490 g_assert_no_error (error);
492 g_resources_register (resource);
494 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
496 g_assert (g_file_get_path (file) == NULL);
498 name = g_file_get_parse_name (file);
499 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
502 name = g_file_get_uri (file);
503 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
506 g_assert (!g_file_is_native (file));
507 g_assert (!g_file_has_uri_scheme (file, "http"));
508 g_assert (g_file_has_uri_scheme (file, "resource"));
509 scheme = g_file_get_uri_scheme (file);
510 g_assert_cmpstr (scheme, ==, "resource");
513 file2 = g_file_dup (file);
514 g_assert (g_file_equal (file, file2));
515 g_object_unref (file2);
517 parent = g_file_get_parent (file);
518 enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
519 g_assert_no_error (error);
521 file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
522 g_assert_no_error (error);
523 g_assert (g_file_equal (file, file2));
524 g_object_unref (file2);
526 info = g_file_enumerator_next_file (enumerator, NULL, &error);
527 g_assert_no_error (error);
528 g_assert (info != NULL);
529 g_object_unref (info);
531 info = g_file_enumerator_next_file (enumerator, NULL, &error);
532 g_assert_no_error (error);
533 g_assert (info != NULL);
534 g_object_unref (info);
536 info = g_file_enumerator_next_file (enumerator, NULL, &error);
537 g_assert_no_error (error);
538 g_assert (info == NULL);
540 g_file_enumerator_close (enumerator, NULL, &error);
541 g_assert_no_error (error);
542 g_object_unref (enumerator);
544 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
545 g_assert (g_file_equal (file, file2));
547 g_assert (g_file_has_prefix (file, parent));
549 name = g_file_get_relative_path (parent, file);
550 g_assert_cmpstr (name, ==, "test2-alias.txt");
553 g_object_unref (parent);
555 attrs = g_file_query_settable_attributes (file, NULL, &error);
556 g_assert_no_error (error);
557 g_file_attribute_info_list_unref (attrs);
559 attrs = g_file_query_writable_namespaces (file, NULL, &error);
560 g_assert_no_error (error);
561 g_file_attribute_info_list_unref (attrs);
563 g_object_unref (file);
565 g_resources_unregister (resource);
566 g_resource_unref (resource);
574 g_test_init (&argc, &argv, NULL);
576 _g_test2_register_resource ();
578 g_test_add_func ("/resource/file", test_resource_file);
579 g_test_add_func ("/resource/data", test_resource_data);
580 g_test_add_func ("/resource/registered", test_resource_registered);
581 g_test_add_func ("/resource/manual", test_resource_manual);
582 #ifdef G_HAS_CONSTRUCTORS
583 g_test_add_func ("/resource/automatic", test_resource_automatic);
584 /* This only uses automatic resources too, so it tests the constructors and destructors */
585 g_test_add_func ("/resource/module", test_resource_module);
587 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
588 g_test_add_func ("/resource/uri/file", test_uri_file);