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, NULL);
98 data = g_resource_lookup_data (resource,
99 "/a_prefix/test2.txt",
100 G_RESOURCE_LOOKUP_FLAGS_NONE,
102 g_assert (data != NULL);
103 g_assert_no_error (error);
104 size = g_bytes_get_size (data);
105 g_assert_cmpint (size, ==, 6);
106 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
107 g_bytes_unref (data);
109 data = g_resource_lookup_data (resource,
110 "/a_prefix/test2-alias.txt",
111 G_RESOURCE_LOOKUP_FLAGS_NONE,
113 g_assert (data != NULL);
114 g_assert_no_error (error);
115 size = g_bytes_get_size (data);
116 g_assert_cmpint (size, ==, 6);
117 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
118 g_bytes_unref (data);
120 children = g_resource_enumerate_children (resource,
122 G_RESOURCE_LOOKUP_FLAGS_NONE,
124 g_assert (children == NULL);
125 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
126 g_clear_error (&error);
128 children = g_resource_enumerate_children (resource,
130 G_RESOURCE_LOOKUP_FLAGS_NONE,
132 g_assert (children != NULL);
133 g_assert_no_error (error);
134 g_assert_cmpint (g_strv_length (children), ==, 2);
135 g_strfreev (children);
139 test_resource_file (void)
142 GError *error = NULL;
144 resource = g_resource_load ("not-there", &error);
145 g_assert (resource == NULL);
146 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
147 g_clear_error (&error);
149 resource = g_resource_load ("test.gresource", &error);
150 g_assert (resource != NULL);
151 g_assert_no_error (error);
153 test_resource (resource);
154 g_resource_unref (resource);
158 test_resource_data (void)
161 GError *error = NULL;
162 gboolean loaded_file;
167 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
169 g_assert (loaded_file);
171 data = g_bytes_new_take (content, content_size);
172 resource = g_resource_new_from_data (data, &error);
173 g_assert (resource != NULL);
174 g_assert_no_error (error);
176 test_resource (resource);
178 g_resource_unref (resource);
182 test_resource_registred (void)
185 GError *error = NULL;
186 gboolean found, success;
194 resource = g_resource_load ("test.gresource", &error);
195 g_assert (resource != NULL);
196 g_assert_no_error (error);
198 found = g_resources_get_info ("/test1.txt",
199 G_RESOURCE_LOOKUP_FLAGS_NONE,
200 &size, &flags, &error);
202 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
203 g_clear_error (&error);
205 g_resources_register (resource);
207 found = g_resources_get_info ("/test1.txt",
208 G_RESOURCE_LOOKUP_FLAGS_NONE,
209 &size, &flags, &error);
211 g_assert_no_error (error);
212 g_assert_cmpint (size, ==, 6);
213 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
215 found = g_resources_get_info ("/a_prefix/test2.txt",
216 G_RESOURCE_LOOKUP_FLAGS_NONE,
217 &size, &flags, &error);
219 g_assert_no_error (error);
220 g_assert_cmpint (size, ==, 6);
221 g_assert_cmpint (flags, ==, 0);
223 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
224 G_RESOURCE_LOOKUP_FLAGS_NONE,
225 &size, &flags, &error);
227 g_assert_no_error (error);
228 g_assert_cmpint (size, ==, 6);
229 g_assert_cmpuint (flags, ==, 0);
231 data = g_resources_lookup_data ("/test1.txt",
232 G_RESOURCE_LOOKUP_FLAGS_NONE,
234 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
235 g_assert_no_error (error);
236 g_bytes_unref (data);
238 in = g_resources_open_stream ("/test1.txt",
239 G_RESOURCE_LOOKUP_FLAGS_NONE,
241 g_assert (in != NULL);
242 g_assert_no_error (error);
244 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
248 g_assert_no_error (error);
249 g_assert_cmpint (size, ==, 6);
251 g_assert_cmpstr (buffer, ==, "test1\n");
253 g_input_stream_close (in, NULL, NULL);
256 data = g_resources_lookup_data ("/a_prefix/test2.txt",
257 G_RESOURCE_LOOKUP_FLAGS_NONE,
259 g_assert (data != NULL);
260 g_assert_no_error (error);
261 size = g_bytes_get_size (data);
262 g_assert_cmpint (size, ==, 6);
263 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
264 g_bytes_unref (data);
266 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
267 G_RESOURCE_LOOKUP_FLAGS_NONE,
269 g_assert (data != NULL);
270 g_assert_no_error (error);
271 size = g_bytes_get_size (data);
272 g_assert_cmpint (size, ==, 6);
273 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
274 g_bytes_unref (data);
276 children = g_resources_enumerate_children ("/not/here",
277 G_RESOURCE_LOOKUP_FLAGS_NONE,
279 g_assert (children == NULL);
280 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
281 g_clear_error (&error);
283 children = g_resources_enumerate_children ("/a_prefix",
284 G_RESOURCE_LOOKUP_FLAGS_NONE,
286 g_assert (children != NULL);
287 g_assert_no_error (error);
288 g_assert_cmpint (g_strv_length (children), ==, 2);
289 g_strfreev (children);
291 g_resources_unregister (resource);
293 found = g_resources_get_info ("/test1.txt",
294 G_RESOURCE_LOOKUP_FLAGS_NONE,
295 &size, &flags, &error);
297 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
298 g_clear_error (&error);
302 test_resource_automatic (void)
304 GError *error = NULL;
310 found = g_resources_get_info ("/auto_loaded/test1.txt",
311 G_RESOURCE_LOOKUP_FLAGS_NONE,
312 &size, &flags, &error);
314 g_assert_no_error (error);
315 g_assert_cmpint (size, ==, 6);
316 g_assert_cmpint (flags, ==, 0);
318 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
319 G_RESOURCE_LOOKUP_FLAGS_NONE,
321 g_assert (data != NULL);
322 g_assert_no_error (error);
323 size = g_bytes_get_size (data);
324 g_assert_cmpint (size, ==, 6);
325 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
326 g_bytes_unref (data);
330 test_resource_manual (void)
332 GError *error = NULL;
338 found = g_resources_get_info ("/manual_loaded/test1.txt",
339 G_RESOURCE_LOOKUP_FLAGS_NONE,
340 &size, &flags, &error);
342 g_assert_no_error (error);
343 g_assert_cmpint (size, ==, 6);
344 g_assert_cmpuint (flags, ==, 0);
346 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
347 G_RESOURCE_LOOKUP_FLAGS_NONE,
349 g_assert (data != NULL);
350 g_assert_no_error (error);
351 size = g_bytes_get_size (data);
352 g_assert_cmpint (size, ==, 6);
353 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
354 g_bytes_unref (data);
358 test_resource_module (void)
367 if (g_module_supported ())
371 dir = g_get_current_dir ();
373 path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin", NULL);
374 module = g_io_module_new (path);
380 found = g_resources_get_info ("/resourceplugin/test1.txt",
381 G_RESOURCE_LOOKUP_FLAGS_NONE,
382 &size, &flags, &error);
384 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
385 g_clear_error (&error);
387 g_type_module_use (G_TYPE_MODULE (module));
389 found = g_resources_get_info ("/resourceplugin/test1.txt",
390 G_RESOURCE_LOOKUP_FLAGS_NONE,
391 &size, &flags, &error);
393 g_assert_no_error (error);
394 g_assert_cmpint (size, ==, 6);
395 g_assert_cmpuint (flags, ==, 0);
397 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
398 G_RESOURCE_LOOKUP_FLAGS_NONE,
400 g_assert (data != NULL);
401 g_assert_no_error (error);
402 size = g_bytes_get_size (data);
403 g_assert_cmpint (size, ==, 6);
404 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
405 g_bytes_unref (data);
407 g_type_module_unuse (G_TYPE_MODULE (module));
409 found = g_resources_get_info ("/resourceplugin/test1.txt",
410 G_RESOURCE_LOOKUP_FLAGS_NONE,
411 &size, &flags, &error);
413 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
414 g_clear_error (&error);
419 test_uri_query_info (void)
422 GError *error = NULL;
423 gboolean loaded_file;
429 const char *content_type;
431 loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
433 g_assert (loaded_file);
435 data = g_bytes_new_take (content, content_size);
436 resource = g_resource_new_from_data (data, &error);
437 g_assert (resource != NULL);
438 g_assert_no_error (error);
440 g_resources_register (resource);
442 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
444 info = g_file_query_info (file, "*", 0, NULL, &error);
445 g_assert_no_error (error);
446 g_object_unref (file);
448 content_type = g_file_info_get_content_type (info);
449 g_assert (content_type);
450 g_assert_cmpstr (content_type, ==, "text/plain");
452 g_object_unref (info);
454 g_resources_unregister (resource);
455 g_resource_unref (resource);
463 g_test_init (&argc, &argv, NULL);
465 _g_test2_register_resource ();
467 g_test_add_func ("/resource/file", test_resource_file);
468 g_test_add_func ("/resource/data", test_resource_data);
469 g_test_add_func ("/resource/registred", test_resource_registred);
470 g_test_add_func ("/resource/manual", test_resource_manual);
471 #ifdef G_HAS_CONSTRUCTORS
472 g_test_add_func ("/resource/automatic", test_resource_automatic);
473 /* This only uses automatic resources too, so it tests the constructors and destructors */
474 g_test_add_func ("/resource/module", test_resource_module);
476 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);