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.
23 #include "gconstructor.h"
24 #include "test_resources2.h"
27 test_resource (GResource *resource)
30 gboolean found, success;
38 found = g_resource_get_info (resource,
40 G_RESOURCE_LOOKUP_FLAGS_NONE,
41 &size, &flags, &error);
43 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
44 g_clear_error (&error);
46 found = g_resource_get_info (resource,
48 G_RESOURCE_LOOKUP_FLAGS_NONE,
49 &size, &flags, &error);
51 g_assert_no_error (error);
52 g_assert_cmpint (size, ==, 6);
53 g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
55 found = g_resource_get_info (resource,
56 "/a_prefix/test2.txt",
57 G_RESOURCE_LOOKUP_FLAGS_NONE,
58 &size, &flags, &error);
60 g_assert_no_error (error);
61 g_assert_cmpint (size, ==, 6);
62 g_assert_cmpuint (flags, ==, 0);
64 found = g_resource_get_info (resource,
65 "/a_prefix/test2-alias.txt",
66 G_RESOURCE_LOOKUP_FLAGS_NONE,
67 &size, &flags, &error);
69 g_assert_no_error (error);
70 g_assert_cmpint (size, ==, 6);
71 g_assert_cmpuint (flags, ==, 0);
73 data = g_resource_lookup_data (resource,
75 G_RESOURCE_LOOKUP_FLAGS_NONE,
77 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
78 g_assert_no_error (error);
81 in = g_resource_open_stream (resource,
83 G_RESOURCE_LOOKUP_FLAGS_NONE,
85 g_assert (in != NULL);
86 g_assert_no_error (error);
88 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
92 g_assert_no_error (error);
93 g_assert_cmpint (size, ==, 6);
95 g_assert_cmpstr (buffer, ==, "test1\n");
97 g_input_stream_close (in, NULL, &error);
98 g_assert_no_error (error);
101 data = g_resource_lookup_data (resource,
102 "/a_prefix/test2.txt",
103 G_RESOURCE_LOOKUP_FLAGS_NONE,
105 g_assert (data != NULL);
106 g_assert_no_error (error);
107 size = g_bytes_get_size (data);
108 g_assert_cmpint (size, ==, 6);
109 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
110 g_bytes_unref (data);
112 data = g_resource_lookup_data (resource,
113 "/a_prefix/test2-alias.txt",
114 G_RESOURCE_LOOKUP_FLAGS_NONE,
116 g_assert (data != NULL);
117 g_assert_no_error (error);
118 size = g_bytes_get_size (data);
119 g_assert_cmpint (size, ==, 6);
120 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
121 g_bytes_unref (data);
123 children = g_resource_enumerate_children (resource,
125 G_RESOURCE_LOOKUP_FLAGS_NONE,
127 g_assert (children == NULL);
128 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
129 g_clear_error (&error);
131 children = g_resource_enumerate_children (resource,
133 G_RESOURCE_LOOKUP_FLAGS_NONE,
135 g_assert (children != NULL);
136 g_assert_no_error (error);
137 g_assert_cmpint (g_strv_length (children), ==, 2);
138 g_strfreev (children);
142 test_resource_file (void)
145 GError *error = NULL;
147 resource = g_resource_load ("not-there", &error);
148 g_assert (resource == NULL);
149 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
150 g_clear_error (&error);
152 resource = g_resource_load ("test.gresource", &error);
153 g_assert (resource != NULL);
154 g_assert_no_error (error);
156 test_resource (resource);
157 g_resource_unref (resource);
161 test_resource_data (void)
164 GError *error = NULL;
165 gboolean loaded_file;
170 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
172 g_assert (loaded_file);
174 data = g_bytes_new_take (content, content_size);
175 resource = g_resource_new_from_data (data, &error);
176 g_bytes_unref (data);
177 g_assert (resource != NULL);
178 g_assert_no_error (error);
180 test_resource (resource);
182 g_resource_unref (resource);
186 test_resource_registered (void)
189 GError *error = NULL;
190 gboolean found, success;
198 resource = g_resource_load ("test.gresource", &error);
199 g_assert (resource != NULL);
200 g_assert_no_error (error);
202 found = g_resources_get_info ("/test1.txt",
203 G_RESOURCE_LOOKUP_FLAGS_NONE,
204 &size, &flags, &error);
206 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
207 g_clear_error (&error);
209 g_resources_register (resource);
211 found = g_resources_get_info ("/test1.txt",
212 G_RESOURCE_LOOKUP_FLAGS_NONE,
213 &size, &flags, &error);
215 g_assert_no_error (error);
216 g_assert_cmpint (size, ==, 6);
217 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
219 found = g_resources_get_info ("/a_prefix/test2.txt",
220 G_RESOURCE_LOOKUP_FLAGS_NONE,
221 &size, &flags, &error);
223 g_assert_no_error (error);
224 g_assert_cmpint (size, ==, 6);
225 g_assert_cmpint (flags, ==, 0);
227 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
228 G_RESOURCE_LOOKUP_FLAGS_NONE,
229 &size, &flags, &error);
231 g_assert_no_error (error);
232 g_assert_cmpint (size, ==, 6);
233 g_assert_cmpuint (flags, ==, 0);
235 data = g_resources_lookup_data ("/test1.txt",
236 G_RESOURCE_LOOKUP_FLAGS_NONE,
238 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
239 g_assert_no_error (error);
240 g_bytes_unref (data);
242 in = g_resources_open_stream ("/test1.txt",
243 G_RESOURCE_LOOKUP_FLAGS_NONE,
245 g_assert (in != NULL);
246 g_assert_no_error (error);
248 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
252 g_assert_no_error (error);
253 g_assert_cmpint (size, ==, 6);
255 g_assert_cmpstr (buffer, ==, "test1\n");
257 g_input_stream_close (in, NULL, &error);
258 g_assert_no_error (error);
259 g_clear_object (&in);
261 data = g_resources_lookup_data ("/a_prefix/test2.txt",
262 G_RESOURCE_LOOKUP_FLAGS_NONE,
264 g_assert (data != NULL);
265 g_assert_no_error (error);
266 size = g_bytes_get_size (data);
267 g_assert_cmpint (size, ==, 6);
268 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
269 g_bytes_unref (data);
271 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
272 G_RESOURCE_LOOKUP_FLAGS_NONE,
274 g_assert (data != NULL);
275 g_assert_no_error (error);
276 size = g_bytes_get_size (data);
277 g_assert_cmpint (size, ==, 6);
278 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
279 g_bytes_unref (data);
281 children = g_resources_enumerate_children ("/not/here",
282 G_RESOURCE_LOOKUP_FLAGS_NONE,
284 g_assert (children == NULL);
285 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
286 g_clear_error (&error);
288 children = g_resources_enumerate_children ("/a_prefix",
289 G_RESOURCE_LOOKUP_FLAGS_NONE,
291 g_assert (children != NULL);
292 g_assert_no_error (error);
293 g_assert_cmpint (g_strv_length (children), ==, 2);
294 g_strfreev (children);
296 g_resources_unregister (resource);
297 g_resource_unref (resource);
299 found = g_resources_get_info ("/test1.txt",
300 G_RESOURCE_LOOKUP_FLAGS_NONE,
301 &size, &flags, &error);
303 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
304 g_clear_error (&error);
308 test_resource_automatic (void)
310 GError *error = NULL;
316 found = g_resources_get_info ("/auto_loaded/test1.txt",
317 G_RESOURCE_LOOKUP_FLAGS_NONE,
318 &size, &flags, &error);
320 g_assert_no_error (error);
321 g_assert_cmpint (size, ==, 6);
322 g_assert_cmpint (flags, ==, 0);
324 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
325 G_RESOURCE_LOOKUP_FLAGS_NONE,
327 g_assert (data != NULL);
328 g_assert_no_error (error);
329 size = g_bytes_get_size (data);
330 g_assert_cmpint (size, ==, 6);
331 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
332 g_bytes_unref (data);
336 test_resource_manual (void)
338 GError *error = NULL;
344 found = g_resources_get_info ("/manual_loaded/test1.txt",
345 G_RESOURCE_LOOKUP_FLAGS_NONE,
346 &size, &flags, &error);
348 g_assert_no_error (error);
349 g_assert_cmpint (size, ==, 6);
350 g_assert_cmpuint (flags, ==, 0);
352 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
353 G_RESOURCE_LOOKUP_FLAGS_NONE,
355 g_assert (data != NULL);
356 g_assert_no_error (error);
357 size = g_bytes_get_size (data);
358 g_assert_cmpint (size, ==, 6);
359 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
360 g_bytes_unref (data);
364 test_resource_manual2 (void)
369 GError *error = NULL;
371 resource = _g_test2_get_resource ();
373 data = g_resource_lookup_data (resource,
374 "/manual_loaded/test1.txt",
375 G_RESOURCE_LOOKUP_FLAGS_NONE,
377 g_assert (data != NULL);
378 g_assert_no_error (error);
379 size = g_bytes_get_size (data);
380 g_assert_cmpint (size, ==, 6);
381 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
382 g_bytes_unref (data);
384 g_resource_unref (resource);
388 test_resource_module (void)
397 if (g_module_supported ())
401 dir = g_get_current_dir ();
403 path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin", NULL);
404 module = g_io_module_new (path);
410 found = g_resources_get_info ("/resourceplugin/test1.txt",
411 G_RESOURCE_LOOKUP_FLAGS_NONE,
412 &size, &flags, &error);
414 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
415 g_clear_error (&error);
417 g_type_module_use (G_TYPE_MODULE (module));
419 found = g_resources_get_info ("/resourceplugin/test1.txt",
420 G_RESOURCE_LOOKUP_FLAGS_NONE,
421 &size, &flags, &error);
423 g_assert_no_error (error);
424 g_assert_cmpint (size, ==, 6);
425 g_assert_cmpuint (flags, ==, 0);
427 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
428 G_RESOURCE_LOOKUP_FLAGS_NONE,
430 g_assert (data != NULL);
431 g_assert_no_error (error);
432 size = g_bytes_get_size (data);
433 g_assert_cmpint (size, ==, 6);
434 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
435 g_bytes_unref (data);
437 g_type_module_unuse (G_TYPE_MODULE (module));
439 found = g_resources_get_info ("/resourceplugin/test1.txt",
440 G_RESOURCE_LOOKUP_FLAGS_NONE,
441 &size, &flags, &error);
443 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
444 g_clear_error (&error);
449 test_uri_query_info (void)
452 GError *error = NULL;
453 gboolean loaded_file;
459 const char *content_type;
461 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
463 g_assert (loaded_file);
465 data = g_bytes_new_take (content, content_size);
466 resource = g_resource_new_from_data (data, &error);
467 g_bytes_unref (data);
468 g_assert (resource != NULL);
469 g_assert_no_error (error);
471 g_resources_register (resource);
473 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
475 info = g_file_query_info (file, "*", 0, NULL, &error);
476 g_assert_no_error (error);
478 content_type = g_file_info_get_content_type (info);
479 g_assert (content_type);
480 g_assert_cmpstr (content_type, ==, "text/plain");
482 g_object_unref (info);
484 g_object_unref (file);
486 g_resources_unregister (resource);
487 g_resource_unref (resource);
494 GError *error = NULL;
495 gboolean loaded_file;
502 GFile *file2, *parent;
503 GFileEnumerator *enumerator;
505 GFileAttributeInfoList *attrs;
506 GInputStream *stream;
511 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
513 g_assert (loaded_file);
515 data = g_bytes_new_take (content, content_size);
516 resource = g_resource_new_from_data (data, &error);
517 g_bytes_unref (data);
518 g_assert (resource != NULL);
519 g_assert_no_error (error);
521 g_resources_register (resource);
523 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
525 g_assert (g_file_get_path (file) == NULL);
527 name = g_file_get_parse_name (file);
528 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
531 name = g_file_get_uri (file);
532 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
535 g_assert (!g_file_is_native (file));
536 g_assert (!g_file_has_uri_scheme (file, "http"));
537 g_assert (g_file_has_uri_scheme (file, "resource"));
538 scheme = g_file_get_uri_scheme (file);
539 g_assert_cmpstr (scheme, ==, "resource");
542 file2 = g_file_dup (file);
543 g_assert (g_file_equal (file, file2));
544 g_object_unref (file2);
546 parent = g_file_get_parent (file);
547 enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
548 g_assert_no_error (error);
550 file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
551 g_assert_no_error (error);
552 g_assert (g_file_equal (file, file2));
553 g_object_unref (file2);
555 info = g_file_enumerator_next_file (enumerator, NULL, &error);
556 g_assert_no_error (error);
557 g_assert (info != NULL);
558 g_object_unref (info);
560 info = g_file_enumerator_next_file (enumerator, NULL, &error);
561 g_assert_no_error (error);
562 g_assert (info != NULL);
563 g_object_unref (info);
565 info = g_file_enumerator_next_file (enumerator, NULL, &error);
566 g_assert_no_error (error);
567 g_assert (info == NULL);
569 g_file_enumerator_close (enumerator, NULL, &error);
570 g_assert_no_error (error);
571 g_object_unref (enumerator);
573 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
574 g_assert (g_file_equal (file, file2));
576 g_assert (g_file_has_prefix (file, parent));
578 name = g_file_get_relative_path (parent, file);
579 g_assert_cmpstr (name, ==, "test2-alias.txt");
582 g_object_unref (parent);
584 attrs = g_file_query_settable_attributes (file, NULL, &error);
585 g_assert_no_error (error);
586 g_file_attribute_info_list_unref (attrs);
588 attrs = g_file_query_writable_namespaces (file, NULL, &error);
589 g_assert_no_error (error);
590 g_file_attribute_info_list_unref (attrs);
592 stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
593 g_assert_no_error (error);
594 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
595 g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
596 ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
598 g_assert_no_error (error);
599 skipped = g_input_stream_skip (stream, 1, NULL, &error);
600 g_assert_cmpint (skipped, ==, 1);
601 g_assert_no_error (error);
603 memset (buf, 0, 1024);
604 ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
606 g_assert_no_error (error);
607 g_assert_cmpstr (buf, ==, "st2\n");
608 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
609 G_FILE_ATTRIBUTE_STANDARD_SIZE,
612 g_assert_no_error (error);
613 g_assert (info != NULL);
614 g_assert_cmpint (g_file_info_get_size (info), ==, 6);
615 g_object_unref (info);
617 ret = g_input_stream_close (stream, NULL, &error);
619 g_assert_no_error (error);
620 g_object_unref (stream);
622 g_object_unref (file);
623 g_object_unref (file2);
625 g_resources_unregister (resource);
626 g_resource_unref (resource);
633 g_test_init (&argc, &argv, NULL);
635 _g_test2_register_resource ();
637 g_test_add_func ("/resource/file", test_resource_file);
638 g_test_add_func ("/resource/data", test_resource_data);
639 g_test_add_func ("/resource/registered", test_resource_registered);
640 g_test_add_func ("/resource/manual", test_resource_manual);
641 g_test_add_func ("/resource/manual2", test_resource_manual2);
642 #ifdef G_HAS_CONSTRUCTORS
643 g_test_add_func ("/resource/automatic", test_resource_automatic);
644 /* This only uses automatic resources too, so it tests the constructors and destructors */
645 g_test_add_func ("/resource/module", test_resource_module);
647 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
648 g_test_add_func ("/resource/uri/file", test_uri_file);