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_assert (resource != NULL);
176 g_assert_no_error (error);
178 test_resource (resource);
180 g_resource_unref (resource);
184 test_resource_registred (void)
187 GError *error = NULL;
188 gboolean found, success;
196 resource = g_resource_load ("test.gresource", &error);
197 g_assert (resource != NULL);
198 g_assert_no_error (error);
200 found = g_resources_get_info ("/test1.txt",
201 G_RESOURCE_LOOKUP_FLAGS_NONE,
202 &size, &flags, &error);
204 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
205 g_clear_error (&error);
207 g_resources_register (resource);
209 found = g_resources_get_info ("/test1.txt",
210 G_RESOURCE_LOOKUP_FLAGS_NONE,
211 &size, &flags, &error);
213 g_assert_no_error (error);
214 g_assert_cmpint (size, ==, 6);
215 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
217 found = g_resources_get_info ("/a_prefix/test2.txt",
218 G_RESOURCE_LOOKUP_FLAGS_NONE,
219 &size, &flags, &error);
221 g_assert_no_error (error);
222 g_assert_cmpint (size, ==, 6);
223 g_assert_cmpint (flags, ==, 0);
225 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
226 G_RESOURCE_LOOKUP_FLAGS_NONE,
227 &size, &flags, &error);
229 g_assert_no_error (error);
230 g_assert_cmpint (size, ==, 6);
231 g_assert_cmpuint (flags, ==, 0);
233 data = g_resources_lookup_data ("/test1.txt",
234 G_RESOURCE_LOOKUP_FLAGS_NONE,
236 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
237 g_assert_no_error (error);
238 g_bytes_unref (data);
240 in = g_resources_open_stream ("/test1.txt",
241 G_RESOURCE_LOOKUP_FLAGS_NONE,
243 g_assert (in != NULL);
244 g_assert_no_error (error);
246 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
250 g_assert_no_error (error);
251 g_assert_cmpint (size, ==, 6);
253 g_assert_cmpstr (buffer, ==, "test1\n");
255 g_input_stream_close (in, NULL, &error);
256 g_assert_no_error (error);
257 g_clear_object (&in);
259 data = g_resources_lookup_data ("/a_prefix/test2.txt",
260 G_RESOURCE_LOOKUP_FLAGS_NONE,
262 g_assert (data != NULL);
263 g_assert_no_error (error);
264 size = g_bytes_get_size (data);
265 g_assert_cmpint (size, ==, 6);
266 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
267 g_bytes_unref (data);
269 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
270 G_RESOURCE_LOOKUP_FLAGS_NONE,
272 g_assert (data != NULL);
273 g_assert_no_error (error);
274 size = g_bytes_get_size (data);
275 g_assert_cmpint (size, ==, 6);
276 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
277 g_bytes_unref (data);
279 children = g_resources_enumerate_children ("/not/here",
280 G_RESOURCE_LOOKUP_FLAGS_NONE,
282 g_assert (children == NULL);
283 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
284 g_clear_error (&error);
286 children = g_resources_enumerate_children ("/a_prefix",
287 G_RESOURCE_LOOKUP_FLAGS_NONE,
289 g_assert (children != NULL);
290 g_assert_no_error (error);
291 g_assert_cmpint (g_strv_length (children), ==, 2);
292 g_strfreev (children);
294 g_resources_unregister (resource);
296 found = g_resources_get_info ("/test1.txt",
297 G_RESOURCE_LOOKUP_FLAGS_NONE,
298 &size, &flags, &error);
300 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
301 g_clear_error (&error);
305 test_resource_automatic (void)
307 GError *error = NULL;
313 found = g_resources_get_info ("/auto_loaded/test1.txt",
314 G_RESOURCE_LOOKUP_FLAGS_NONE,
315 &size, &flags, &error);
317 g_assert_no_error (error);
318 g_assert_cmpint (size, ==, 6);
319 g_assert_cmpint (flags, ==, 0);
321 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
322 G_RESOURCE_LOOKUP_FLAGS_NONE,
324 g_assert (data != NULL);
325 g_assert_no_error (error);
326 size = g_bytes_get_size (data);
327 g_assert_cmpint (size, ==, 6);
328 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
329 g_bytes_unref (data);
333 test_resource_manual (void)
335 GError *error = NULL;
341 found = g_resources_get_info ("/manual_loaded/test1.txt",
342 G_RESOURCE_LOOKUP_FLAGS_NONE,
343 &size, &flags, &error);
345 g_assert_no_error (error);
346 g_assert_cmpint (size, ==, 6);
347 g_assert_cmpuint (flags, ==, 0);
349 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
350 G_RESOURCE_LOOKUP_FLAGS_NONE,
352 g_assert (data != NULL);
353 g_assert_no_error (error);
354 size = g_bytes_get_size (data);
355 g_assert_cmpint (size, ==, 6);
356 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
357 g_bytes_unref (data);
361 test_resource_module (void)
370 if (g_module_supported ())
374 dir = g_get_current_dir ();
376 path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin", NULL);
377 module = g_io_module_new (path);
383 found = g_resources_get_info ("/resourceplugin/test1.txt",
384 G_RESOURCE_LOOKUP_FLAGS_NONE,
385 &size, &flags, &error);
387 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
388 g_clear_error (&error);
390 g_type_module_use (G_TYPE_MODULE (module));
392 found = g_resources_get_info ("/resourceplugin/test1.txt",
393 G_RESOURCE_LOOKUP_FLAGS_NONE,
394 &size, &flags, &error);
396 g_assert_no_error (error);
397 g_assert_cmpint (size, ==, 6);
398 g_assert_cmpuint (flags, ==, 0);
400 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
401 G_RESOURCE_LOOKUP_FLAGS_NONE,
403 g_assert (data != NULL);
404 g_assert_no_error (error);
405 size = g_bytes_get_size (data);
406 g_assert_cmpint (size, ==, 6);
407 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
408 g_bytes_unref (data);
410 g_type_module_unuse (G_TYPE_MODULE (module));
412 found = g_resources_get_info ("/resourceplugin/test1.txt",
413 G_RESOURCE_LOOKUP_FLAGS_NONE,
414 &size, &flags, &error);
416 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
417 g_clear_error (&error);
422 test_uri_query_info (void)
425 GError *error = NULL;
426 gboolean loaded_file;
432 const char *content_type;
434 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
436 g_assert (loaded_file);
438 data = g_bytes_new_take (content, content_size);
439 resource = g_resource_new_from_data (data, &error);
440 g_bytes_unref (data);
441 g_assert (resource != NULL);
442 g_assert_no_error (error);
444 g_resources_register (resource);
446 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
448 info = g_file_query_info (file, "*", 0, NULL, &error);
449 g_assert_no_error (error);
450 g_object_unref (file);
452 content_type = g_file_info_get_content_type (info);
453 g_assert (content_type);
454 g_assert_cmpstr (content_type, ==, "text/plain");
456 g_object_unref (info);
458 g_resources_unregister (resource);
459 g_resource_unref (resource);
467 g_test_init (&argc, &argv, NULL);
469 _g_test2_register_resource ();
471 g_test_add_func ("/resource/file", test_resource_file);
472 g_test_add_func ("/resource/data", test_resource_data);
473 g_test_add_func ("/resource/registred", test_resource_registred);
474 g_test_add_func ("/resource/manual", test_resource_manual);
475 #ifdef G_HAS_CONSTRUCTORS
476 g_test_add_func ("/resource/automatic", test_resource_automatic);
477 /* This only uses automatic resources too, so it tests the constructors and destructors */
478 g_test_add_func ("/resource/module", test_resource_module);
480 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);