1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008 Red Hat, Inc.
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
22 * Authors: David Zeuthen <davidz@redhat.com>
25 #include <glib/glib.h>
31 test_g_icon_to_string (void)
50 /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
52 uri
= "file:///some/native/path/to/an/icon.png";
53 location
= g_file_new_for_uri (uri
);
54 icon
= g_file_icon_new (location
);
56 g_object_get (icon
, "file", &file
, NULL
);
57 g_assert (file
== location
);
58 g_object_unref (file
);
60 data
= g_icon_to_string (icon
);
61 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");
62 icon2
= g_icon_new_for_string (data
, &error
);
63 g_assert_no_error (error
);
64 g_assert (g_icon_equal (icon
, icon2
));
66 g_object_unref (icon
);
67 g_object_unref (icon2
);
68 g_object_unref (location
);
70 uri
= "file:///some/native/path/to/an/icon with spaces.png";
71 location
= g_file_new_for_uri (uri
);
72 icon
= g_file_icon_new (location
);
73 data
= g_icon_to_string (icon
);
74 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");
75 icon2
= g_icon_new_for_string (data
, &error
);
76 g_assert_no_error (error
);
77 g_assert (g_icon_equal (icon
, icon2
));
79 g_object_unref (icon
);
80 g_object_unref (icon2
);
81 g_object_unref (location
);
83 uri
= "sftp:///some/non-native/path/to/an/icon.png";
84 location
= g_file_new_for_uri (uri
);
85 icon
= g_file_icon_new (location
);
86 data
= g_icon_to_string (icon
);
87 g_assert_cmpstr (data
, ==, "sftp:///some/non-native/path/to/an/icon.png");
88 icon2
= g_icon_new_for_string (data
, &error
);
89 g_assert_no_error (error
);
90 g_assert (g_icon_equal (icon
, icon2
));
92 g_object_unref (icon
);
93 g_object_unref (icon2
);
94 g_object_unref (location
);
97 uri
= "sftp:///some/non-native/path/to/an/icon with spaces.png";
98 location
= g_file_new_for_uri (uri
);
99 icon
= g_file_icon_new (location
);
100 data
= g_icon_to_string (icon
);
101 g_assert_cmpstr (data
, ==, "sftp:///some/non-native/path/to/an/icon%20with%20spaces.png");
102 icon2
= g_icon_new_for_string (data
, &error
);
103 g_assert_no_error (error
);
104 g_assert (g_icon_equal (icon
, icon2
));
106 g_object_unref (icon
);
107 g_object_unref (icon2
);
108 g_object_unref (location
);
111 icon
= g_themed_icon_new ("network-server");
112 data
= g_icon_to_string (icon
);
113 g_assert_cmpstr (data
, ==, "network-server");
114 icon2
= g_icon_new_for_string (data
, &error
);
115 g_assert_no_error (error
);
116 g_assert (g_icon_equal (icon
, icon2
));
118 g_object_unref (icon
);
119 g_object_unref (icon2
);
121 /* Check that we can serialize from well-known specified formats */
122 icon
= g_icon_new_for_string ("network-server%", &error
);
123 g_assert_no_error (error
);
124 icon2
= g_themed_icon_new ("network-server%");
125 g_assert (g_icon_equal (icon
, icon2
));
126 g_object_unref (icon
);
127 g_object_unref (icon2
);
129 icon
= g_icon_new_for_string ("/path/to/somewhere.png", &error
);
130 g_assert_no_error (error
);
131 location
= g_file_new_for_commandline_arg ("/path/to/somewhere.png");
132 icon2
= g_file_icon_new (location
);
133 g_assert (g_icon_equal (icon
, icon2
));
134 g_object_unref (icon
);
135 g_object_unref (icon2
);
136 g_object_unref (location
);
138 icon
= g_icon_new_for_string ("/path/to/somewhere with whitespace.png", &error
);
139 g_assert_no_error (error
);
140 data
= g_icon_to_string (icon
);
141 g_assert_cmpstr (data
, ==, G_DIR_SEPARATOR_S
"path" G_DIR_SEPARATOR_S
"to" G_DIR_SEPARATOR_S
"somewhere with whitespace.png");
143 location
= g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
144 icon2
= g_file_icon_new (location
);
145 g_assert (g_icon_equal (icon
, icon2
));
146 g_object_unref (location
);
147 g_object_unref (icon2
);
148 location
= g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
149 icon2
= g_file_icon_new (location
);
150 g_assert (!g_icon_equal (icon
, icon2
));
151 g_object_unref (location
);
152 g_object_unref (icon2
);
153 g_object_unref (icon
);
155 icon
= g_icon_new_for_string ("sftp:///path/to/somewhere.png", &error
);
156 g_assert_no_error (error
);
157 data
= g_icon_to_string (icon
);
158 g_assert_cmpstr (data
, ==, "sftp:///path/to/somewhere.png");
160 location
= g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
161 icon2
= g_file_icon_new (location
);
162 g_assert (g_icon_equal (icon
, icon2
));
163 g_object_unref (icon
);
164 g_object_unref (icon2
);
165 g_object_unref (location
);
168 icon
= g_icon_new_for_string ("sftp:///path/to/somewhere with whitespace.png", &error
);
169 g_assert_no_error (error
);
170 data
= g_icon_to_string (icon
);
171 g_assert_cmpstr (data
, ==, "sftp:///path/to/somewhere%20with%20whitespace.png");
173 location
= g_file_new_for_commandline_arg ("sftp:///path/to/somewhere with whitespace.png");
174 icon2
= g_file_icon_new (location
);
175 g_assert (g_icon_equal (icon
, icon2
));
176 g_object_unref (location
);
177 g_object_unref (icon2
);
178 location
= g_file_new_for_commandline_arg ("sftp:///path/to/somewhere%20with%20whitespace.png");
179 icon2
= g_file_icon_new (location
);
180 g_assert (g_icon_equal (icon
, icon2
));
181 g_object_unref (location
);
182 g_object_unref (icon2
);
183 g_object_unref (icon
);
186 /* Check that GThemedIcon serialization works */
188 icon
= g_themed_icon_new ("network-server");
189 g_themed_icon_append_name (G_THEMED_ICON (icon
), "computer");
190 data
= g_icon_to_string (icon
);
191 icon2
= g_icon_new_for_string (data
, &error
);
192 g_assert_no_error (error
);
193 g_assert (g_icon_equal (icon
, icon2
));
195 g_object_unref (icon
);
196 g_object_unref (icon2
);
198 icon
= g_themed_icon_new ("icon name with whitespace");
199 g_themed_icon_append_name (G_THEMED_ICON (icon
), "computer");
200 data
= g_icon_to_string (icon
);
201 icon2
= g_icon_new_for_string (data
, &error
);
202 g_assert_no_error (error
);
203 g_assert (g_icon_equal (icon
, icon2
));
205 g_object_unref (icon
);
206 g_object_unref (icon2
);
208 icon
= g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
209 g_themed_icon_append_name (G_THEMED_ICON (icon
), "computer");
210 data
= g_icon_to_string (icon
);
211 icon2
= g_icon_new_for_string (data
, &error
);
212 g_assert_no_error (error
);
213 g_assert (g_icon_equal (icon
, icon2
));
215 g_object_unref (icon
);
216 g_object_unref (icon2
);
218 /* Check that GEmblemedIcon serialization works */
220 icon
= g_themed_icon_new ("face-smirk");
221 icon2
= g_themed_icon_new ("emblem-important");
222 g_themed_icon_append_name (G_THEMED_ICON (icon2
), "emblem-shared");
223 location
= g_file_new_for_uri ("file:///some/path/somewhere.png");
224 icon3
= g_file_icon_new (location
);
225 g_object_unref (location
);
226 emblem1
= g_emblem_new_with_origin (icon2
, G_EMBLEM_ORIGIN_DEVICE
);
227 emblem2
= g_emblem_new_with_origin (icon3
, G_EMBLEM_ORIGIN_LIVEMETADATA
);
228 icon4
= g_emblemed_icon_new (icon
, emblem1
);
229 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4
), emblem2
);
230 data
= g_icon_to_string (icon4
);
231 icon5
= g_icon_new_for_string (data
, &error
);
232 g_assert_no_error (error
);
233 g_assert (g_icon_equal (icon4
, icon5
));
235 g_object_get (emblem1
, "origin", &origin
, "icon", &i
, NULL
);
236 g_assert (origin
== G_EMBLEM_ORIGIN_DEVICE
);
237 g_assert (i
== icon2
);
240 g_object_unref (emblem1
);
241 g_object_unref (emblem2
);
242 g_object_unref (icon
);
243 g_object_unref (icon2
);
244 g_object_unref (icon3
);
245 g_object_unref (icon4
);
246 g_object_unref (icon5
);
251 test_g_icon_serialize (void)
265 /* Check that we can deserialize from well-known specified formats */
266 data
= g_variant_new_string ("network-server%");
267 icon
= g_icon_deserialize (g_variant_ref_sink (data
));
268 g_variant_unref (data
);
269 icon2
= g_themed_icon_new ("network-server%");
270 g_assert (g_icon_equal (icon
, icon2
));
271 g_object_unref (icon
);
272 g_object_unref (icon2
);
274 data
= g_variant_new_string ("/path/to/somewhere.png");
275 icon
= g_icon_deserialize (g_variant_ref_sink (data
));
276 g_variant_unref (data
);
277 location
= g_file_new_for_commandline_arg ("/path/to/somewhere.png");
278 icon2
= g_file_icon_new (location
);
279 g_assert (g_icon_equal (icon
, icon2
));
280 g_object_unref (icon
);
281 g_object_unref (icon2
);
282 g_object_unref (location
);
284 data
= g_variant_new_string ("/path/to/somewhere with whitespace.png");
285 icon
= g_icon_deserialize (g_variant_ref_sink (data
));
286 g_variant_unref (data
);
287 location
= g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
288 icon2
= g_file_icon_new (location
);
289 g_assert (g_icon_equal (icon
, icon2
));
290 g_object_unref (location
);
291 g_object_unref (icon2
);
292 location
= g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
293 icon2
= g_file_icon_new (location
);
294 g_assert (!g_icon_equal (icon
, icon2
));
295 g_object_unref (location
);
296 g_object_unref (icon2
);
297 g_object_unref (icon
);
299 data
= g_variant_new_string ("sftp:///path/to/somewhere.png");
300 icon
= g_icon_deserialize (g_variant_ref_sink (data
));
301 g_variant_unref (data
);
302 location
= g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
303 icon2
= g_file_icon_new (location
);
304 g_assert (g_icon_equal (icon
, icon2
));
305 g_object_unref (icon
);
306 g_object_unref (icon2
);
307 g_object_unref (location
);
309 /* Check that GThemedIcon serialization works */
311 icon
= g_themed_icon_new ("network-server");
312 g_themed_icon_append_name (G_THEMED_ICON (icon
), "computer");
313 data
= g_icon_serialize (icon
);
314 icon2
= g_icon_deserialize (data
);
315 g_assert (g_icon_equal (icon
, icon2
));
316 g_variant_unref (data
);
317 g_object_unref (icon
);
318 g_object_unref (icon2
);
320 icon
= g_themed_icon_new ("icon name with whitespace");
321 g_themed_icon_append_name (G_THEMED_ICON (icon
), "computer");
322 data
= g_icon_serialize (icon
);
323 icon2
= g_icon_deserialize (data
);
324 g_assert (g_icon_equal (icon
, icon2
));
325 g_variant_unref (data
);
326 g_object_unref (icon
);
327 g_object_unref (icon2
);
329 icon
= g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
330 g_themed_icon_append_name (G_THEMED_ICON (icon
), "computer");
331 data
= g_icon_serialize (icon
);
332 icon2
= g_icon_deserialize (data
);
333 g_assert (g_icon_equal (icon
, icon2
));
334 g_variant_unref (data
);
335 g_object_unref (icon
);
336 g_object_unref (icon2
);
338 /* Check that GEmblemedIcon serialization works */
340 icon
= g_themed_icon_new ("face-smirk");
341 icon2
= g_themed_icon_new ("emblem-important");
342 g_themed_icon_append_name (G_THEMED_ICON (icon2
), "emblem-shared");
343 location
= g_file_new_for_uri ("file:///some/path/somewhere.png");
344 icon3
= g_file_icon_new (location
);
345 g_object_unref (location
);
346 emblem1
= g_emblem_new_with_origin (icon2
, G_EMBLEM_ORIGIN_DEVICE
);
347 emblem2
= g_emblem_new_with_origin (icon3
, G_EMBLEM_ORIGIN_LIVEMETADATA
);
348 icon4
= g_emblemed_icon_new (icon
, emblem1
);
349 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4
), emblem2
);
350 data
= g_icon_serialize (icon4
);
351 icon5
= g_icon_deserialize (data
);
352 g_assert (g_icon_equal (icon4
, icon5
));
354 g_object_get (emblem1
, "origin", &origin
, "icon", &i
, NULL
);
355 g_assert (origin
== G_EMBLEM_ORIGIN_DEVICE
);
356 g_assert (i
== icon2
);
359 g_object_unref (emblem1
);
360 g_object_unref (emblem2
);
361 g_object_unref (icon
);
362 g_object_unref (icon2
);
363 g_object_unref (icon3
);
364 g_object_unref (icon4
);
365 g_object_unref (icon5
);
366 g_variant_unref (data
);
370 test_themed_icon (void)
372 GIcon
*icon1
, *icon2
, *icon3
, *icon4
;
373 const gchar
*const *names
;
374 const gchar
*names2
[] = { "first", "testicon", "last", NULL
};
379 icon1
= g_themed_icon_new ("testicon");
381 g_object_get (icon1
, "use-default-fallbacks", &fallbacks
, NULL
);
382 g_assert (!fallbacks
);
384 names
= g_themed_icon_get_names (G_THEMED_ICON (icon1
));
385 g_assert_cmpint (g_strv_length ((gchar
**)names
), ==, 1);
386 g_assert_cmpstr (names
[0], ==, "testicon");
388 g_themed_icon_prepend_name (G_THEMED_ICON (icon1
), "first");
389 g_themed_icon_append_name (G_THEMED_ICON (icon1
), "last");
390 names
= g_themed_icon_get_names (G_THEMED_ICON (icon1
));
391 g_assert_cmpint (g_strv_length ((gchar
**)names
), ==, 3);
392 g_assert_cmpstr (names
[0], ==, "first");
393 g_assert_cmpstr (names
[1], ==, "testicon");
394 g_assert_cmpstr (names
[2], ==, "last");
395 g_assert_cmpuint (g_icon_hash (icon1
), ==, 2400773466U);
397 icon2
= g_themed_icon_new_from_names ((gchar
**)names2
, -1);
398 g_assert (g_icon_equal (icon1
, icon2
));
400 str
= g_icon_to_string (icon2
);
401 icon3
= g_icon_new_for_string (str
, NULL
);
402 g_assert (g_icon_equal (icon2
, icon3
));
405 variant
= g_icon_serialize (icon3
);
406 icon4
= g_icon_deserialize (variant
);
407 g_assert (g_icon_equal (icon3
, icon4
));
408 g_assert (g_icon_hash (icon3
) == g_icon_hash (icon4
));
409 g_variant_unref (variant
);
411 g_object_unref (icon1
);
412 g_object_unref (icon2
);
413 g_object_unref (icon3
);
414 g_object_unref (icon4
);
418 test_emblemed_icon (void)
421 GIcon
*icon1
, *icon2
, *icon3
, *icon4
, *icon5
;
422 GEmblem
*emblem
, *emblem1
, *emblem2
;
426 icon1
= g_themed_icon_new ("testicon");
427 icon2
= g_themed_icon_new ("testemblem");
428 emblem1
= g_emblem_new (icon2
);
429 emblem2
= g_emblem_new_with_origin (icon2
, G_EMBLEM_ORIGIN_TAG
);
431 icon3
= g_emblemed_icon_new (icon1
, emblem1
);
432 emblems
= g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon3
));
433 g_assert_cmpint (g_list_length (emblems
), ==, 1);
434 g_assert (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon3
)) == icon1
);
436 icon4
= g_emblemed_icon_new (icon1
, emblem1
);
437 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4
), emblem2
);
438 emblems
= g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4
));
439 g_assert_cmpint (g_list_length (emblems
), ==, 2);
441 g_assert (!g_icon_equal (icon3
, icon4
));
443 variant
= g_icon_serialize (icon4
);
444 icon5
= g_icon_deserialize (variant
);
445 g_assert (g_icon_equal (icon4
, icon5
));
446 g_assert (g_icon_hash (icon4
) == g_icon_hash (icon5
));
447 g_variant_unref (variant
);
449 emblem
= emblems
->data
;
450 g_assert (g_emblem_get_icon (emblem
) == icon2
);
451 g_assert (g_emblem_get_origin (emblem
) == G_EMBLEM_ORIGIN_TAG
);
453 emblem
= emblems
->next
->data
;
454 g_assert (g_emblem_get_icon (emblem
) == icon2
);
455 g_assert (g_emblem_get_origin (emblem
) == G_EMBLEM_ORIGIN_UNKNOWN
);
457 g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (icon4
));
458 g_assert (g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4
)) == NULL
);
460 g_assert (g_icon_hash (icon4
) != g_icon_hash (icon2
));
461 g_object_get (icon4
, "gicon", &icon
, NULL
);
462 g_assert (icon
== icon1
);
463 g_object_unref (icon
);
465 g_object_unref (icon1
);
466 g_object_unref (icon2
);
467 g_object_unref (icon3
);
468 g_object_unref (icon4
);
469 g_object_unref (icon5
);
471 g_object_unref (emblem1
);
472 g_object_unref (emblem2
);
476 load_cb (GObject
*source_object
,
480 GLoadableIcon
*icon
= G_LOADABLE_ICON (source_object
);
481 GMainLoop
*loop
= data
;
482 GError
*error
= NULL
;
483 GInputStream
*stream
;
485 stream
= g_loadable_icon_load_finish (icon
, res
, NULL
, &error
);
486 g_assert_no_error (error
);
487 g_assert (G_IS_INPUT_STREAM (stream
));
488 g_object_unref (stream
);
489 g_main_loop_quit (loop
);
493 loadable_icon_tests (GLoadableIcon
*icon
)
495 GError
*error
= NULL
;
496 GInputStream
*stream
;
499 stream
= g_loadable_icon_load (icon
, 20, NULL
, NULL
, &error
);
500 g_assert_no_error (error
);
501 g_assert (G_IS_INPUT_STREAM (stream
));
502 g_object_unref (stream
);
504 loop
= g_main_loop_new (NULL
, FALSE
);
505 g_loadable_icon_load_async (icon
, 20, NULL
, load_cb
, loop
);
506 g_main_loop_run (loop
);
507 g_main_loop_unref (loop
);
511 test_file_icon (void)
521 file
= g_file_new_for_path (g_test_get_filename (G_TEST_DIST
, "g-icon.c", NULL
));
522 icon
= g_file_icon_new (file
);
523 g_object_unref (file
);
525 loadable_icon_tests (G_LOADABLE_ICON (icon
));
527 str
= g_icon_to_string (icon
);
528 icon2
= g_icon_new_for_string (str
, NULL
);
529 g_assert (g_icon_equal (icon
, icon2
));
532 file
= g_file_new_for_path ("/\1\2\3/\244");
533 icon4
= g_file_icon_new (file
);
535 variant
= g_icon_serialize (icon4
);
536 icon3
= g_icon_deserialize (variant
);
537 g_assert (g_icon_equal (icon4
, icon3
));
538 g_assert (g_icon_hash (icon4
) == g_icon_hash (icon3
));
539 g_variant_unref (variant
);
541 g_object_unref (icon
);
542 g_object_unref (icon2
);
543 g_object_unref (icon3
);
544 g_object_unref (icon4
);
545 g_object_unref (file
);
549 test_bytes_icon (void)
557 const gchar
*data
= "1234567890987654321";
559 bytes
= g_bytes_new_static (data
, strlen (data
));
560 icon
= g_bytes_icon_new (bytes
);
561 icon2
= g_bytes_icon_new (bytes
);
563 g_assert (g_bytes_icon_get_bytes (G_BYTES_ICON (icon
)) == bytes
);
564 g_assert (g_icon_equal (icon
, icon2
));
565 g_assert (g_icon_hash (icon
) == g_icon_hash (icon2
));
567 g_object_get (icon
, "bytes", &bytes2
, NULL
);
568 g_assert (bytes
== bytes2
);
569 g_bytes_unref (bytes2
);
571 variant
= g_icon_serialize (icon
);
572 icon3
= g_icon_deserialize (variant
);
573 g_assert (g_icon_equal (icon
, icon3
));
574 g_assert (g_icon_hash (icon
) == g_icon_hash (icon3
));
576 loadable_icon_tests (G_LOADABLE_ICON (icon
));
578 g_variant_unref (variant
);
579 g_object_unref (icon
);
580 g_object_unref (icon2
);
581 g_object_unref (icon3
);
582 g_bytes_unref (bytes
);
589 g_test_init (&argc
, &argv
, NULL
);
591 g_test_add_func ("/icons/to-string", test_g_icon_to_string
);
592 g_test_add_func ("/icons/serialize", test_g_icon_serialize
);
593 g_test_add_func ("/icons/themed", test_themed_icon
);
594 g_test_add_func ("/icons/emblemed", test_emblemed_icon
);
595 g_test_add_func ("/icons/file", test_file_icon
);
596 g_test_add_func ("/icons/bytes", test_bytes_icon
);