2e34d82284f5d91a5df5ea20b7ae14ce3d1f32ad
[platform/upstream/glib.git] / gio / tests / g-icon.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008 Red Hat, Inc.
4  *
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.
8  *
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.
12  *
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.
21  *
22  * Authors: David Zeuthen <davidz@redhat.com>
23  */
24
25 #include <glib/glib.h>
26 #include <gio/gio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 static void
31 test_g_icon_to_string (void)
32 {
33   GIcon *icon;
34   GIcon *icon2;
35   GIcon *icon3;
36   GIcon *icon4;
37   GIcon *icon5;
38   GEmblem *emblem1;
39   GEmblem *emblem2;
40   const char *uri;
41   GFile *location;
42   char *data;
43   GError *error;
44   gint origin;
45   GIcon *i;
46   GFile *file;
47
48   error = NULL;
49
50   /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
51
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);
55
56   g_object_get (icon, "file", &file, NULL);
57   g_assert (file == location);
58   g_object_unref (file);
59
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));
65   g_free (data);
66   g_object_unref (icon);
67   g_object_unref (icon2);
68   g_object_unref (location);
69
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));
78   g_free (data);
79   g_object_unref (icon);
80   g_object_unref (icon2);
81   g_object_unref (location);
82
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));
91   g_free (data);
92   g_object_unref (icon);
93   g_object_unref (icon2);
94   g_object_unref (location);
95
96 #if 0
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));
105   g_free (data);
106   g_object_unref (icon);
107   g_object_unref (icon2);
108   g_object_unref (location);
109 #endif
110
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));
117   g_free (data);
118   g_object_unref (icon);
119   g_object_unref (icon2);
120
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);
128
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);
137
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");
142   g_free (data);
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);
154
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");
159   g_free (data);
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);
166
167 #if 0
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");
172   g_free (data);
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);
184 #endif
185
186   /* Check that GThemedIcon serialization works */
187
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));
194   g_free (data);
195   g_object_unref (icon);
196   g_object_unref (icon2);
197
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));
204   g_free (data);
205   g_object_unref (icon);
206   g_object_unref (icon2);
207
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));
214   g_free (data);
215   g_object_unref (icon);
216   g_object_unref (icon2);
217
218   /* Check that GEmblemedIcon serialization works */
219
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));
234
235   g_object_get (emblem1, "origin", &origin, "icon", &i, NULL);
236   g_assert (origin == G_EMBLEM_ORIGIN_DEVICE);
237   g_assert (i == icon2);
238   g_object_unref (i);
239
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);
247   g_free (data);
248 }
249
250 static void
251 test_g_icon_serialize (void)
252 {
253   GIcon *icon;
254   GIcon *icon2;
255   GIcon *icon3;
256   GIcon *icon4;
257   GIcon *icon5;
258   GEmblem *emblem1;
259   GEmblem *emblem2;
260   GFile *location;
261   GVariant *data;
262   gint origin;
263   GIcon *i;
264
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);
273
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);
283
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);
298
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);
308
309   /* Check that GThemedIcon serialization works */
310
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);
319
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);
328
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);
337
338   /* Check that GEmblemedIcon serialization works */
339
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));
353
354   g_object_get (emblem1, "origin", &origin, "icon", &i, NULL);
355   g_assert (origin == G_EMBLEM_ORIGIN_DEVICE);
356   g_assert (i == icon2);
357   g_object_unref (i);
358
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);
367 }
368
369 static void
370 test_themed_icon (void)
371 {
372   GIcon *icon1, *icon2, *icon3;
373   const gchar *const *names;
374   const gchar *names2[] = { "first", "testicon", "last", NULL };
375   gchar *str;
376   gboolean fallbacks;
377
378   icon1 = g_themed_icon_new ("testicon");
379
380   g_object_get (icon1, "use-default-fallbacks", &fallbacks, NULL);
381   g_assert (!fallbacks);
382
383   names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
384   g_assert_cmpint (g_strv_length ((gchar **)names), ==, 1);
385   g_assert_cmpstr (names[0], ==, "testicon");
386
387   g_themed_icon_prepend_name (G_THEMED_ICON (icon1), "first");
388   g_themed_icon_append_name (G_THEMED_ICON (icon1), "last");
389   names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
390   g_assert_cmpint (g_strv_length ((gchar **)names), ==, 3);
391   g_assert_cmpstr (names[0], ==, "first");
392   g_assert_cmpstr (names[1], ==, "testicon");
393   g_assert_cmpstr (names[2], ==, "last");
394   g_assert_cmpuint (g_icon_hash (icon1), ==, 2400773466U);
395
396   icon2 = g_themed_icon_new_from_names ((gchar**)names2, -1);
397   g_assert (g_icon_equal (icon1, icon2));
398
399   str = g_icon_to_string (icon2);
400   icon3 = g_icon_new_for_string (str, NULL);
401   g_assert (g_icon_equal (icon2, icon3));
402   g_free (str);
403
404   g_object_unref (icon1);
405   g_object_unref (icon2);
406   g_object_unref (icon3);
407 }
408
409 static void
410 test_emblemed_icon (void)
411 {
412   GIcon *icon1, *icon2, *icon3, *icon4;
413   GEmblem *emblem, *emblem1, *emblem2;
414   GList *emblems;
415   GIcon *icon;
416
417   icon1 = g_themed_icon_new ("testicon");
418   icon2 = g_themed_icon_new ("testemblem");
419   emblem1 = g_emblem_new (icon2);
420   emblem2 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_TAG);
421
422   icon3 = g_emblemed_icon_new (icon1, emblem1);
423   emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon3));
424   g_assert_cmpint (g_list_length (emblems), ==, 1);
425   g_assert (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon3)) == icon1);
426
427   icon4 = g_emblemed_icon_new (icon1, emblem1);
428   g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
429   emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4));
430   g_assert_cmpint (g_list_length (emblems), ==, 2);
431
432   g_assert (!g_icon_equal (icon3, icon4));
433
434   emblem = emblems->data;
435   g_assert (g_emblem_get_icon (emblem) == icon2);
436   g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_TAG);
437
438   emblem = emblems->next->data;
439   g_assert (g_emblem_get_icon (emblem) == icon2);
440   g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_UNKNOWN);
441
442   g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (icon4));
443   g_assert (g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4)) == NULL);
444
445   g_assert (g_icon_hash (icon4) != g_icon_hash (icon2));
446   g_object_get (icon4, "gicon", &icon, NULL);
447   g_assert (icon == icon1);
448   g_object_unref (icon);
449
450   g_object_unref (icon1);
451   g_object_unref (icon2);
452   g_object_unref (icon3);
453   g_object_unref (icon4);
454
455   g_object_unref (emblem1);
456   g_object_unref (emblem2);
457 }
458
459 static void
460 test_file_icon (void)
461 {
462   GFile *file;
463   GIcon *icon;
464   GIcon *icon2;
465   GError *error;
466   GInputStream *stream;
467   gchar *str;
468
469   file = g_file_new_for_path (g_test_get_filename (G_TEST_DISTED, "g-icon.c", NULL));
470   icon = g_file_icon_new (file);
471   g_object_unref (file);
472
473   error = NULL;
474   stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), 20, NULL, NULL, &error);
475   g_assert (stream != NULL);
476   g_assert_no_error (error);
477
478   g_object_unref (stream);
479
480   str = g_icon_to_string (icon);
481   icon2 = g_icon_new_for_string (str, NULL);
482   g_assert (g_icon_equal (icon, icon2));
483   g_free (str);
484
485   g_object_unref (icon);
486   g_object_unref (icon2);
487 }
488
489 int
490 main (int   argc,
491       char *argv[])
492 {
493   g_test_init (&argc, &argv, NULL);
494
495   g_test_add_func ("/icons/to-string", test_g_icon_to_string);
496   g_test_add_func ("/icons/serialize", test_g_icon_serialize);
497   g_test_add_func ("/icons/themed", test_themed_icon);
498   g_test_add_func ("/icons/emblemed", test_emblemed_icon);
499   g_test_add_func ("/icons/file", test_file_icon);
500
501   return g_test_run();
502 }