e38ee3409863b060e5d3d82b935ac7ab19d01f57
[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_serialize (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_themed_icon (void)
252 {
253   GIcon *icon1, *icon2, *icon3;
254   const gchar *const *names;
255   const gchar *names2[] = { "first", "testicon", "last", NULL };
256   gchar *str;
257   gboolean fallbacks;
258
259   icon1 = g_themed_icon_new ("testicon");
260
261   g_object_get (icon1, "use-default-fallbacks", &fallbacks, NULL);
262   g_assert (!fallbacks);
263
264   names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
265   g_assert_cmpint (g_strv_length ((gchar **)names), ==, 1);
266   g_assert_cmpstr (names[0], ==, "testicon");
267
268   g_themed_icon_prepend_name (G_THEMED_ICON (icon1), "first");
269   g_themed_icon_append_name (G_THEMED_ICON (icon1), "last");
270   names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
271   g_assert_cmpint (g_strv_length ((gchar **)names), ==, 3);
272   g_assert_cmpstr (names[0], ==, "first");
273   g_assert_cmpstr (names[1], ==, "testicon");
274   g_assert_cmpstr (names[2], ==, "last");
275   g_assert_cmpuint (g_icon_hash (icon1), ==, 2400773466U);
276
277   icon2 = g_themed_icon_new_from_names ((gchar**)names2, -1);
278   g_assert (g_icon_equal (icon1, icon2));
279
280   str = g_icon_to_string (icon2);
281   icon3 = g_icon_new_for_string (str, NULL);
282   g_assert (g_icon_equal (icon2, icon3));
283   g_free (str);
284
285   g_object_unref (icon1);
286   g_object_unref (icon2);
287   g_object_unref (icon3);
288 }
289
290 static void
291 test_emblemed_icon (void)
292 {
293   GIcon *icon1, *icon2, *icon3, *icon4;
294   GEmblem *emblem, *emblem1, *emblem2;
295   GList *emblems;
296   GIcon *icon;
297
298   icon1 = g_themed_icon_new ("testicon");
299   icon2 = g_themed_icon_new ("testemblem");
300   emblem1 = g_emblem_new (icon2);
301   emblem2 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_TAG);
302
303   icon3 = g_emblemed_icon_new (icon1, emblem1);
304   emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon3));
305   g_assert_cmpint (g_list_length (emblems), ==, 1);
306   g_assert (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon3)) == icon1);
307
308   icon4 = g_emblemed_icon_new (icon1, emblem1);
309   g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
310   emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4));
311   g_assert_cmpint (g_list_length (emblems), ==, 2);
312
313   g_assert (!g_icon_equal (icon3, icon4));
314
315   emblem = emblems->data;
316   g_assert (g_emblem_get_icon (emblem) == icon2);
317   g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_TAG);
318
319   emblem = emblems->next->data;
320   g_assert (g_emblem_get_icon (emblem) == icon2);
321   g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_UNKNOWN);
322
323   g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (icon4));
324   g_assert (g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4)) == NULL);
325
326   g_assert (g_icon_hash (icon4) != g_icon_hash (icon2));
327   g_object_get (icon4, "gicon", &icon, NULL);
328   g_assert (icon == icon1);
329   g_object_unref (icon);
330
331   g_object_unref (icon1);
332   g_object_unref (icon2);
333   g_object_unref (icon3);
334   g_object_unref (icon4);
335
336   g_object_unref (emblem1);
337   g_object_unref (emblem2);
338 }
339
340 static void
341 test_file_icon (void)
342 {
343   GFile *file;
344   GIcon *icon;
345   GIcon *icon2;
346   GError *error;
347   GInputStream *stream;
348   gchar *str;
349
350   file = g_file_new_for_path (SRCDIR "/g-icon.c");
351   icon = g_file_icon_new (file);
352   g_object_unref (file);
353
354   error = NULL;
355   stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), 20, NULL, NULL, &error);
356   g_assert (stream != NULL);
357   g_assert_no_error (error);
358
359   g_object_unref (stream);
360
361   str = g_icon_to_string (icon);
362   icon2 = g_icon_new_for_string (str, NULL);
363   g_assert (g_icon_equal (icon, icon2));
364   g_free (str);
365
366   g_object_unref (icon);
367   g_object_unref (icon2);
368 }
369
370 int
371 main (int   argc,
372       char *argv[])
373 {
374   g_type_init ();
375   g_test_init (&argc, &argv, NULL);
376
377   g_test_add_func ("/icons/serialize", test_g_icon_serialize);
378   g_test_add_func ("/icons/themed", test_themed_icon);
379   g_test_add_func ("/icons/emblemed", test_emblemed_icon);
380   g_test_add_func ("/icons/file", test_file_icon);
381
382   return g_test_run();
383 }