1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008 Red Hat, Inc.
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
24 * Authors: David Zeuthen <davidz@redhat.com>
27 #include <glib/glib.h>
33 test_g_icon_to_string (void)
52 /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
54 uri = "file:///some/native/path/to/an/icon.png";
55 location = g_file_new_for_uri (uri);
56 icon = g_file_icon_new (location);
58 g_object_get (icon, "file", &file, NULL);
59 g_assert (file == location);
60 g_object_unref (file);
62 data = g_icon_to_string (icon);
63 g_assert_cmpstr (data, ==, G_DIR_SEPARATOR_S "some" G_DIR_SEPARATOR_S "native" G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "an" G_DIR_SEPARATOR_S "icon.png");
64 icon2 = g_icon_new_for_string (data, &error);
65 g_assert_no_error (error);
66 g_assert (g_icon_equal (icon, icon2));
68 g_object_unref (icon);
69 g_object_unref (icon2);
70 g_object_unref (location);
72 uri = "file:///some/native/path/to/an/icon with spaces.png";
73 location = g_file_new_for_uri (uri);
74 icon = g_file_icon_new (location);
75 data = g_icon_to_string (icon);
76 g_assert_cmpstr (data, ==, G_DIR_SEPARATOR_S "some" G_DIR_SEPARATOR_S "native" G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "an" G_DIR_SEPARATOR_S "icon with spaces.png");
77 icon2 = g_icon_new_for_string (data, &error);
78 g_assert_no_error (error);
79 g_assert (g_icon_equal (icon, icon2));
81 g_object_unref (icon);
82 g_object_unref (icon2);
83 g_object_unref (location);
85 uri = "sftp:///some/non-native/path/to/an/icon.png";
86 location = g_file_new_for_uri (uri);
87 icon = g_file_icon_new (location);
88 data = g_icon_to_string (icon);
89 g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon.png");
90 icon2 = g_icon_new_for_string (data, &error);
91 g_assert_no_error (error);
92 g_assert (g_icon_equal (icon, icon2));
94 g_object_unref (icon);
95 g_object_unref (icon2);
96 g_object_unref (location);
99 uri = "sftp:///some/non-native/path/to/an/icon with spaces.png";
100 location = g_file_new_for_uri (uri);
101 icon = g_file_icon_new (location);
102 data = g_icon_to_string (icon);
103 g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon%20with%20spaces.png");
104 icon2 = g_icon_new_for_string (data, &error);
105 g_assert_no_error (error);
106 g_assert (g_icon_equal (icon, icon2));
108 g_object_unref (icon);
109 g_object_unref (icon2);
110 g_object_unref (location);
113 icon = g_themed_icon_new_with_default_fallbacks ("some-icon-symbolic");
114 g_themed_icon_append_name (G_THEMED_ICON (icon), "some-other-icon");
115 data = g_icon_to_string (icon);
116 g_assert_cmpstr (data, ==, ". GThemedIcon "
117 "some-icon-symbolic some-symbolic some-other-icon some-other some "
118 "some-icon some-other-icon-symbolic some-other-symbolic");
120 g_object_unref (icon);
122 icon = g_themed_icon_new ("network-server");
123 data = g_icon_to_string (icon);
124 g_assert_cmpstr (data, ==, "network-server");
125 icon2 = g_icon_new_for_string (data, &error);
126 g_assert_no_error (error);
127 g_assert (g_icon_equal (icon, icon2));
129 g_object_unref (icon);
130 g_object_unref (icon2);
132 icon = g_themed_icon_new_with_default_fallbacks ("network-server");
133 data = g_icon_to_string (icon);
134 g_assert_cmpstr (data, ==, ". GThemedIcon network-server network network-server-symbolic network-symbolic");
135 icon2 = g_icon_new_for_string (data, &error);
136 g_assert_no_error (error);
137 g_assert (g_icon_equal (icon, icon2));
139 g_object_unref (icon);
140 g_object_unref (icon2);
142 /* Check that we can serialize from well-known specified formats */
143 icon = g_icon_new_for_string ("network-server%", &error);
144 g_assert_no_error (error);
145 icon2 = g_themed_icon_new ("network-server%");
146 g_assert (g_icon_equal (icon, icon2));
147 g_object_unref (icon);
148 g_object_unref (icon2);
150 icon = g_icon_new_for_string ("/path/to/somewhere.png", &error);
151 g_assert_no_error (error);
152 location = g_file_new_for_commandline_arg ("/path/to/somewhere.png");
153 icon2 = g_file_icon_new (location);
154 g_assert (g_icon_equal (icon, icon2));
155 g_object_unref (icon);
156 g_object_unref (icon2);
157 g_object_unref (location);
159 icon = g_icon_new_for_string ("/path/to/somewhere with whitespace.png", &error);
160 g_assert_no_error (error);
161 data = g_icon_to_string (icon);
162 g_assert_cmpstr (data, ==, G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "somewhere with whitespace.png");
164 location = g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
165 icon2 = g_file_icon_new (location);
166 g_assert (g_icon_equal (icon, icon2));
167 g_object_unref (location);
168 g_object_unref (icon2);
169 location = g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
170 icon2 = g_file_icon_new (location);
171 g_assert (!g_icon_equal (icon, icon2));
172 g_object_unref (location);
173 g_object_unref (icon2);
174 g_object_unref (icon);
176 icon = g_icon_new_for_string ("sftp:///path/to/somewhere.png", &error);
177 g_assert_no_error (error);
178 data = g_icon_to_string (icon);
179 g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere.png");
181 location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
182 icon2 = g_file_icon_new (location);
183 g_assert (g_icon_equal (icon, icon2));
184 g_object_unref (icon);
185 g_object_unref (icon2);
186 g_object_unref (location);
189 icon = g_icon_new_for_string ("sftp:///path/to/somewhere with whitespace.png", &error);
190 g_assert_no_error (error);
191 data = g_icon_to_string (icon);
192 g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere%20with%20whitespace.png");
194 location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere with whitespace.png");
195 icon2 = g_file_icon_new (location);
196 g_assert (g_icon_equal (icon, icon2));
197 g_object_unref (location);
198 g_object_unref (icon2);
199 location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere%20with%20whitespace.png");
200 icon2 = g_file_icon_new (location);
201 g_assert (g_icon_equal (icon, icon2));
202 g_object_unref (location);
203 g_object_unref (icon2);
204 g_object_unref (icon);
207 /* Check that GThemedIcon serialization works */
209 icon = g_themed_icon_new ("network-server");
210 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
211 data = g_icon_to_string (icon);
212 icon2 = g_icon_new_for_string (data, &error);
213 g_assert_no_error (error);
214 g_assert (g_icon_equal (icon, icon2));
216 g_object_unref (icon);
217 g_object_unref (icon2);
219 icon = g_themed_icon_new ("icon name with whitespace");
220 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
221 data = g_icon_to_string (icon);
222 icon2 = g_icon_new_for_string (data, &error);
223 g_assert_no_error (error);
224 g_assert (g_icon_equal (icon, icon2));
226 g_object_unref (icon);
227 g_object_unref (icon2);
229 icon = g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
230 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
231 data = g_icon_to_string (icon);
232 icon2 = g_icon_new_for_string (data, &error);
233 g_assert_no_error (error);
234 g_assert (g_icon_equal (icon, icon2));
236 g_object_unref (icon);
237 g_object_unref (icon2);
239 /* Check that GEmblemedIcon serialization works */
241 icon = g_themed_icon_new ("face-smirk");
242 icon2 = g_themed_icon_new ("emblem-important");
243 g_themed_icon_append_name (G_THEMED_ICON (icon2), "emblem-shared");
244 location = g_file_new_for_uri ("file:///some/path/somewhere.png");
245 icon3 = g_file_icon_new (location);
246 g_object_unref (location);
247 emblem1 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_DEVICE);
248 emblem2 = g_emblem_new_with_origin (icon3, G_EMBLEM_ORIGIN_LIVEMETADATA);
249 icon4 = g_emblemed_icon_new (icon, emblem1);
250 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
251 data = g_icon_to_string (icon4);
252 icon5 = g_icon_new_for_string (data, &error);
253 g_assert_no_error (error);
254 g_assert (g_icon_equal (icon4, icon5));
256 g_object_get (emblem1, "origin", &origin, "icon", &i, NULL);
257 g_assert (origin == G_EMBLEM_ORIGIN_DEVICE);
258 g_assert (i == icon2);
261 g_object_unref (emblem1);
262 g_object_unref (emblem2);
263 g_object_unref (icon);
264 g_object_unref (icon2);
265 g_object_unref (icon3);
266 g_object_unref (icon4);
267 g_object_unref (icon5);
272 test_g_icon_serialize (void)
286 /* Check that we can deserialize from well-known specified formats */
287 data = g_variant_new_string ("network-server%");
288 icon = g_icon_deserialize (g_variant_ref_sink (data));
289 g_variant_unref (data);
290 icon2 = g_themed_icon_new ("network-server%");
291 g_assert (g_icon_equal (icon, icon2));
292 g_object_unref (icon);
293 g_object_unref (icon2);
295 data = g_variant_new_string ("/path/to/somewhere.png");
296 icon = g_icon_deserialize (g_variant_ref_sink (data));
297 g_variant_unref (data);
298 location = g_file_new_for_commandline_arg ("/path/to/somewhere.png");
299 icon2 = g_file_icon_new (location);
300 g_assert (g_icon_equal (icon, icon2));
301 g_object_unref (icon);
302 g_object_unref (icon2);
303 g_object_unref (location);
305 data = g_variant_new_string ("/path/to/somewhere with whitespace.png");
306 icon = g_icon_deserialize (g_variant_ref_sink (data));
307 g_variant_unref (data);
308 location = g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
309 icon2 = g_file_icon_new (location);
310 g_assert (g_icon_equal (icon, icon2));
311 g_object_unref (location);
312 g_object_unref (icon2);
313 location = g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
314 icon2 = g_file_icon_new (location);
315 g_assert (!g_icon_equal (icon, icon2));
316 g_object_unref (location);
317 g_object_unref (icon2);
318 g_object_unref (icon);
320 data = g_variant_new_string ("sftp:///path/to/somewhere.png");
321 icon = g_icon_deserialize (g_variant_ref_sink (data));
322 g_variant_unref (data);
323 location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
324 icon2 = g_file_icon_new (location);
325 g_assert (g_icon_equal (icon, icon2));
326 g_object_unref (icon);
327 g_object_unref (icon2);
328 g_object_unref (location);
330 /* Check that GThemedIcon serialization works */
332 icon = g_themed_icon_new ("network-server");
333 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
334 data = g_icon_serialize (icon);
335 icon2 = g_icon_deserialize (data);
336 g_assert (g_icon_equal (icon, icon2));
337 g_variant_unref (data);
338 g_object_unref (icon);
339 g_object_unref (icon2);
341 icon = g_themed_icon_new ("icon name with whitespace");
342 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
343 data = g_icon_serialize (icon);
344 icon2 = g_icon_deserialize (data);
345 g_assert (g_icon_equal (icon, icon2));
346 g_variant_unref (data);
347 g_object_unref (icon);
348 g_object_unref (icon2);
350 icon = g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
351 g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
352 data = g_icon_serialize (icon);
353 icon2 = g_icon_deserialize (data);
354 g_assert (g_icon_equal (icon, icon2));
355 g_variant_unref (data);
356 g_object_unref (icon);
357 g_object_unref (icon2);
359 /* Check that GEmblemedIcon serialization works */
361 icon = g_themed_icon_new ("face-smirk");
362 icon2 = g_themed_icon_new ("emblem-important");
363 g_themed_icon_append_name (G_THEMED_ICON (icon2), "emblem-shared");
364 location = g_file_new_for_uri ("file:///some/path/somewhere.png");
365 icon3 = g_file_icon_new (location);
366 g_object_unref (location);
367 emblem1 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_DEVICE);
368 emblem2 = g_emblem_new_with_origin (icon3, G_EMBLEM_ORIGIN_LIVEMETADATA);
369 icon4 = g_emblemed_icon_new (icon, emblem1);
370 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
371 data = g_icon_serialize (icon4);
372 icon5 = g_icon_deserialize (data);
373 g_assert (g_icon_equal (icon4, icon5));
375 g_object_get (emblem1, "origin", &origin, "icon", &i, NULL);
376 g_assert (origin == G_EMBLEM_ORIGIN_DEVICE);
377 g_assert (i == icon2);
380 g_object_unref (emblem1);
381 g_object_unref (emblem2);
382 g_object_unref (icon);
383 g_object_unref (icon2);
384 g_object_unref (icon3);
385 g_object_unref (icon4);
386 g_object_unref (icon5);
387 g_variant_unref (data);
391 test_themed_icon (void)
393 GIcon *icon1, *icon2, *icon3, *icon4;
394 const gchar *const *names;
395 const gchar *names2[] = { "first-symbolic", "testicon", "last", NULL };
400 icon1 = g_themed_icon_new ("testicon");
402 g_object_get (icon1, "use-default-fallbacks", &fallbacks, NULL);
403 g_assert (!fallbacks);
405 names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
406 g_assert_cmpint (g_strv_length ((gchar **)names), ==, 2);
407 g_assert_cmpstr (names[0], ==, "testicon");
408 g_assert_cmpstr (names[1], ==, "testicon-symbolic");
410 g_themed_icon_prepend_name (G_THEMED_ICON (icon1), "first-symbolic");
411 g_themed_icon_append_name (G_THEMED_ICON (icon1), "last");
412 names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
413 g_assert_cmpint (g_strv_length ((gchar **)names), ==, 6);
414 g_assert_cmpstr (names[0], ==, "first-symbolic");
415 g_assert_cmpstr (names[1], ==, "testicon");
416 g_assert_cmpstr (names[2], ==, "last");
417 g_assert_cmpstr (names[3], ==, "first");
418 g_assert_cmpstr (names[4], ==, "testicon-symbolic");
419 g_assert_cmpstr (names[5], ==, "last-symbolic");
420 g_assert_cmpuint (g_icon_hash (icon1), ==, 1812785139);
422 icon2 = g_themed_icon_new_from_names ((gchar**)names2, -1);
423 g_assert (g_icon_equal (icon1, icon2));
425 str = g_icon_to_string (icon2);
426 icon3 = g_icon_new_for_string (str, NULL);
427 g_assert (g_icon_equal (icon2, icon3));
430 variant = g_icon_serialize (icon3);
431 icon4 = g_icon_deserialize (variant);
432 g_assert (g_icon_equal (icon3, icon4));
433 g_assert (g_icon_hash (icon3) == g_icon_hash (icon4));
434 g_variant_unref (variant);
436 g_object_unref (icon1);
437 g_object_unref (icon2);
438 g_object_unref (icon3);
439 g_object_unref (icon4);
443 test_emblemed_icon (void)
446 GIcon *icon1, *icon2, *icon3, *icon4, *icon5;
447 GEmblem *emblem, *emblem1, *emblem2;
451 icon1 = g_themed_icon_new ("testicon");
452 icon2 = g_themed_icon_new ("testemblem");
453 emblem1 = g_emblem_new (icon2);
454 emblem2 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_TAG);
456 icon3 = g_emblemed_icon_new (icon1, emblem1);
457 emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon3));
458 g_assert_cmpint (g_list_length (emblems), ==, 1);
459 g_assert (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon3)) == icon1);
461 icon4 = g_emblemed_icon_new (icon1, emblem1);
462 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
463 emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4));
464 g_assert_cmpint (g_list_length (emblems), ==, 2);
466 g_assert (!g_icon_equal (icon3, icon4));
468 variant = g_icon_serialize (icon4);
469 icon5 = g_icon_deserialize (variant);
470 g_assert (g_icon_equal (icon4, icon5));
471 g_assert (g_icon_hash (icon4) == g_icon_hash (icon5));
472 g_variant_unref (variant);
474 emblem = emblems->data;
475 g_assert (g_emblem_get_icon (emblem) == icon2);
476 g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_UNKNOWN);
478 emblem = emblems->next->data;
479 g_assert (g_emblem_get_icon (emblem) == icon2);
480 g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_TAG);
482 g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (icon4));
483 g_assert (g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4)) == NULL);
485 g_assert (g_icon_hash (icon4) != g_icon_hash (icon2));
486 g_object_get (icon4, "gicon", &icon, NULL);
487 g_assert (icon == icon1);
488 g_object_unref (icon);
490 g_object_unref (icon1);
491 g_object_unref (icon2);
492 g_object_unref (icon3);
493 g_object_unref (icon4);
494 g_object_unref (icon5);
496 g_object_unref (emblem1);
497 g_object_unref (emblem2);
501 load_cb (GObject *source_object,
505 GLoadableIcon *icon = G_LOADABLE_ICON (source_object);
506 GMainLoop *loop = data;
507 GError *error = NULL;
508 GInputStream *stream;
510 stream = g_loadable_icon_load_finish (icon, res, NULL, &error);
511 g_assert_no_error (error);
512 g_assert (G_IS_INPUT_STREAM (stream));
513 g_object_unref (stream);
514 g_main_loop_quit (loop);
518 loadable_icon_tests (GLoadableIcon *icon)
520 GError *error = NULL;
521 GInputStream *stream;
524 stream = g_loadable_icon_load (icon, 20, NULL, NULL, &error);
525 g_assert_no_error (error);
526 g_assert (G_IS_INPUT_STREAM (stream));
527 g_object_unref (stream);
529 loop = g_main_loop_new (NULL, FALSE);
530 g_loadable_icon_load_async (icon, 20, NULL, load_cb, loop);
531 g_main_loop_run (loop);
532 g_main_loop_unref (loop);
536 test_file_icon (void)
546 file = g_file_new_for_path (g_test_get_filename (G_TEST_DIST, "g-icon.c", NULL));
547 icon = g_file_icon_new (file);
548 g_object_unref (file);
550 loadable_icon_tests (G_LOADABLE_ICON (icon));
552 str = g_icon_to_string (icon);
553 icon2 = g_icon_new_for_string (str, NULL);
554 g_assert (g_icon_equal (icon, icon2));
557 file = g_file_new_for_path ("/\1\2\3/\244");
558 icon4 = g_file_icon_new (file);
560 variant = g_icon_serialize (icon4);
561 icon3 = g_icon_deserialize (variant);
562 g_assert (g_icon_equal (icon4, icon3));
563 g_assert (g_icon_hash (icon4) == g_icon_hash (icon3));
564 g_variant_unref (variant);
566 g_object_unref (icon);
567 g_object_unref (icon2);
568 g_object_unref (icon3);
569 g_object_unref (icon4);
570 g_object_unref (file);
574 test_bytes_icon (void)
582 const gchar *data = "1234567890987654321";
584 bytes = g_bytes_new_static (data, strlen (data));
585 icon = g_bytes_icon_new (bytes);
586 icon2 = g_bytes_icon_new (bytes);
588 g_assert (g_bytes_icon_get_bytes (G_BYTES_ICON (icon)) == bytes);
589 g_assert (g_icon_equal (icon, icon2));
590 g_assert (g_icon_hash (icon) == g_icon_hash (icon2));
592 g_object_get (icon, "bytes", &bytes2, NULL);
593 g_assert (bytes == bytes2);
594 g_bytes_unref (bytes2);
596 variant = g_icon_serialize (icon);
597 icon3 = g_icon_deserialize (variant);
598 g_assert (g_icon_equal (icon, icon3));
599 g_assert (g_icon_hash (icon) == g_icon_hash (icon3));
601 loadable_icon_tests (G_LOADABLE_ICON (icon));
603 g_variant_unref (variant);
604 g_object_unref (icon);
605 g_object_unref (icon2);
606 g_object_unref (icon3);
607 g_bytes_unref (bytes);
614 g_test_init (&argc, &argv, NULL);
616 g_test_add_func ("/icons/to-string", test_g_icon_to_string);
617 g_test_add_func ("/icons/serialize", test_g_icon_serialize);
618 g_test_add_func ("/icons/themed", test_themed_icon);
619 g_test_add_func ("/icons/emblemed", test_emblemed_icon);
620 g_test_add_func ("/icons/file", test_file_icon);
621 g_test_add_func ("/icons/bytes", test_bytes_icon);