Plug a mem leak in g-icon test
[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
45   error = NULL;
46
47   /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
48
49   uri = "file:///some/native/path/to/an/icon.png";
50   location = g_file_new_for_uri (uri);
51   icon = g_file_icon_new (location);
52   data = g_icon_to_string (icon);
53   g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon.png");
54   icon2 = g_icon_new_for_string (data, &error);
55   g_assert_no_error (error);
56   g_assert (g_icon_equal (icon, icon2));
57   g_free (data);
58   g_object_unref (icon);
59   g_object_unref (icon2);
60   g_object_unref (location);
61
62   uri = "file:///some/native/path/to/an/icon with spaces.png";
63   location = g_file_new_for_uri (uri);
64   icon = g_file_icon_new (location);
65   data = g_icon_to_string (icon);
66   g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon with spaces.png");
67   icon2 = g_icon_new_for_string (data, &error);
68   g_assert_no_error (error);
69   g_assert (g_icon_equal (icon, icon2));
70   g_free (data);
71   g_object_unref (icon);
72   g_object_unref (icon2);
73   g_object_unref (location);
74
75   uri = "sftp:///some/non-native/path/to/an/icon.png";
76   location = g_file_new_for_uri (uri);
77   icon = g_file_icon_new (location);
78   data = g_icon_to_string (icon);
79   g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon.png");
80   icon2 = g_icon_new_for_string (data, &error);
81   g_assert_no_error (error);
82   g_assert (g_icon_equal (icon, icon2));
83   g_free (data);
84   g_object_unref (icon);
85   g_object_unref (icon2);
86   g_object_unref (location);
87
88 #if 0
89   uri = "sftp:///some/non-native/path/to/an/icon with spaces.png";
90   location = g_file_new_for_uri (uri);
91   icon = g_file_icon_new (location);
92   data = g_icon_to_string (icon);
93   g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon%20with%20spaces.png");
94   icon2 = g_icon_new_for_string (data, &error);
95   g_assert_no_error (error);
96   g_assert (g_icon_equal (icon, icon2));
97   g_free (data);
98   g_object_unref (icon);
99   g_object_unref (icon2);
100   g_object_unref (location);
101 #endif
102
103   icon = g_themed_icon_new ("network-server");
104   data = g_icon_to_string (icon);
105   g_assert_cmpstr (data, ==, "network-server");
106   icon2 = g_icon_new_for_string (data, &error);
107   g_assert_no_error (error);
108   g_assert (g_icon_equal (icon, icon2));
109   g_free (data);
110   g_object_unref (icon);
111   g_object_unref (icon2);
112
113   /* Check that we can serialize from well-known specified formats */
114   icon = g_icon_new_for_string ("network-server%", &error);
115   g_assert_no_error (error);
116   icon2 = g_themed_icon_new ("network-server%");
117   g_assert (g_icon_equal (icon, icon2));
118   g_object_unref (icon);
119   g_object_unref (icon2);
120
121   icon = g_icon_new_for_string ("/path/to/somewhere.png", &error);
122   g_assert_no_error (error);
123   location = g_file_new_for_commandline_arg ("/path/to/somewhere.png");
124   icon2 = g_file_icon_new (location);
125   g_assert (g_icon_equal (icon, icon2));
126   g_object_unref (icon);
127   g_object_unref (icon2);
128   g_object_unref (location);
129
130   icon = g_icon_new_for_string ("/path/to/somewhere with whitespace.png", &error);
131   g_assert_no_error (error);
132   data = g_icon_to_string (icon);
133   g_assert_cmpstr (data, ==, "/path/to/somewhere with whitespace.png");
134   g_free (data);
135   location = g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
136   icon2 = g_file_icon_new (location);
137   g_assert (g_icon_equal (icon, icon2));
138   g_object_unref (location);
139   g_object_unref (icon2);
140   location = g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
141   icon2 = g_file_icon_new (location);
142   g_assert (!g_icon_equal (icon, icon2));
143   g_object_unref (location);
144   g_object_unref (icon2);
145   g_object_unref (icon);
146
147   icon = g_icon_new_for_string ("sftp:///path/to/somewhere.png", &error);
148   g_assert_no_error (error);
149   data = g_icon_to_string (icon);
150   g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere.png");
151   g_free (data);
152   location = g_file_new_for_commandline_arg ("sftp:///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);
158
159 #if 0
160   icon = g_icon_new_for_string ("sftp:///path/to/somewhere with whitespace.png", &error);
161   g_assert_no_error (error);
162   data = g_icon_to_string (icon);
163   g_assert_cmpstr (data, ==, "sftp:///path/to/somewhere%20with%20whitespace.png");
164   g_free (data);
165   location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere with whitespace.png");
166   icon2 = g_file_icon_new (location);
167   g_assert (g_icon_equal (icon, icon2));
168   g_object_unref (location);
169   g_object_unref (icon2);
170   location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere%20with%20whitespace.png");
171   icon2 = g_file_icon_new (location);
172   g_assert (g_icon_equal (icon, icon2));
173   g_object_unref (location);
174   g_object_unref (icon2);
175   g_object_unref (icon);
176 #endif
177
178   /* Check that GThemedIcon serialization works */
179
180   icon = g_themed_icon_new ("network-server");
181   g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
182   data = g_icon_to_string (icon);
183   icon2 = g_icon_new_for_string (data, &error);
184   g_assert_no_error (error);
185   g_assert (g_icon_equal (icon, icon2));
186   g_free (data);
187   g_object_unref (icon);
188   g_object_unref (icon2);
189
190   icon = g_themed_icon_new ("icon name with whitespace");
191   g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
192   data = g_icon_to_string (icon);
193   icon2 = g_icon_new_for_string (data, &error);
194   g_assert_no_error (error);
195   g_assert (g_icon_equal (icon, icon2));
196   g_free (data);
197   g_object_unref (icon);
198   g_object_unref (icon2);
199
200   icon = g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
201   g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
202   data = g_icon_to_string (icon);
203   icon2 = g_icon_new_for_string (data, &error);
204   g_assert_no_error (error);
205   g_assert (g_icon_equal (icon, icon2));
206   g_free (data);
207   g_object_unref (icon);
208   g_object_unref (icon2);
209
210   /* Check that GEmblemedIcon serialization works */
211
212   icon = g_themed_icon_new ("face-smirk");
213   icon2 = g_themed_icon_new ("emblem-important");
214   g_themed_icon_append_name (G_THEMED_ICON (icon2), "emblem-shared");
215   location = g_file_new_for_uri ("file:///some/path/somewhere.png");
216   icon3 = g_file_icon_new (location);
217   g_object_unref (location);
218   emblem1 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_DEVICE);
219   emblem2 = g_emblem_new_with_origin (icon3, G_EMBLEM_ORIGIN_LIVEMETADATA);
220   icon4 = g_emblemed_icon_new (icon, emblem1);
221   g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
222   data = g_icon_to_string (icon4);
223   icon5 = g_icon_new_for_string (data, &error);
224   g_assert_no_error (error);
225   g_assert (g_icon_equal (icon4, icon5));
226   g_object_unref (emblem1);
227   g_object_unref (emblem2);
228   g_object_unref (icon);
229   g_object_unref (icon2);
230   g_object_unref (icon3);
231   g_object_unref (icon4);
232   g_object_unref (icon5);
233   g_free (data);
234 }
235
236 static void
237 test_themed_icon (void)
238 {
239   GIcon *icon1, *icon2, *icon3;
240   const gchar *const *names;
241   const gchar *names2[] = { "first", "testicon", "last", NULL };
242   gchar *str;
243   gboolean fallbacks;
244
245   icon1 = g_themed_icon_new ("testicon");
246
247   g_object_get (icon1, "use-default-fallbacks", &fallbacks, NULL);
248   g_assert (!fallbacks);
249
250   names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
251   g_assert_cmpint (g_strv_length ((gchar **)names), ==, 1);
252   g_assert_cmpstr (names[0], ==, "testicon");
253
254   g_themed_icon_prepend_name (G_THEMED_ICON (icon1), "first");
255   g_themed_icon_append_name (G_THEMED_ICON (icon1), "last");
256   names = g_themed_icon_get_names (G_THEMED_ICON (icon1));
257   g_assert_cmpint (g_strv_length ((gchar **)names), ==, 3);
258   g_assert_cmpstr (names[0], ==, "first");
259   g_assert_cmpstr (names[1], ==, "testicon");
260   g_assert_cmpstr (names[2], ==, "last");
261   g_assert_cmpuint (g_icon_hash (icon1), ==, 3193088045U);
262
263   icon2 = g_themed_icon_new_from_names ((gchar**)names2, -1);
264   g_assert (g_icon_equal (icon1, icon2));
265
266   str = g_icon_to_string (icon2);
267   icon3 = g_icon_new_for_string (str, NULL);
268   g_assert (g_icon_equal (icon2, icon3));
269   g_free (str);
270
271   g_object_unref (icon1);
272   g_object_unref (icon2);
273   g_object_unref (icon3);
274 }
275
276 static void
277 test_emblemed_icon (void)
278 {
279   GIcon *icon1, *icon2, *icon3, *icon4;
280   GEmblem *emblem, *emblem1, *emblem2;
281   GList *emblems;
282
283   icon1 = g_themed_icon_new ("testicon");
284   icon2 = g_themed_icon_new ("testemblem");
285   emblem1 = g_emblem_new (icon2);
286   emblem2 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_TAG);
287
288   icon3 = g_emblemed_icon_new (icon1, emblem1);
289   emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon3));
290   g_assert_cmpint (g_list_length (emblems), ==, 1);
291   g_assert (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon3)) == icon1);
292
293   icon4 = g_emblemed_icon_new (icon1, emblem1);
294   g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
295   emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon4));
296   g_assert_cmpint (g_list_length (emblems), ==, 2);
297
298   g_assert (!g_icon_equal (icon3, icon4));
299
300   emblem = emblems->data;
301   g_assert (g_emblem_get_icon (emblem) == icon2);
302   g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_UNKNOWN);
303
304   emblem = emblems->next->data;
305   g_assert (g_emblem_get_icon (emblem) == icon2);
306   g_assert (g_emblem_get_origin (emblem) == G_EMBLEM_ORIGIN_TAG);
307
308   g_object_unref (icon1);
309   g_object_unref (icon2);
310   g_object_unref (icon3);
311   g_object_unref (icon4);
312
313   g_object_unref (emblem1);
314   g_object_unref (emblem2);
315 }
316
317 static void
318 test_file_icon (void)
319 {
320   GFile *file;
321   GIcon *icon;
322   GIcon *icon2;
323   GError *error;
324   GInputStream *stream;
325   gchar *str;
326
327   file = g_file_new_for_path (SRCDIR "/g-icon.c");
328   icon = g_file_icon_new (file);
329   g_object_unref (file);
330
331   error = NULL;
332   stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), 20, NULL, NULL, &error);
333   g_assert (stream != NULL);
334   g_assert_no_error (error);
335
336   g_object_unref (stream);
337
338   str = g_icon_to_string (icon);
339   icon2 = g_icon_new_for_string (str, NULL);
340   g_assert (g_icon_equal (icon, icon2));
341   g_free (str);
342
343   g_object_unref (icon);
344   g_object_unref (icon2);
345 }
346
347 int
348 main (int   argc,
349       char *argv[])
350 {
351   g_type_init ();
352   g_test_init (&argc, &argv, NULL);
353
354   g_test_add_func ("/icons/serialize", test_g_icon_serialize);
355   g_test_add_func ("/icons/themed", test_themed_icon);
356   g_test_add_func ("/icons/emblemed", test_emblemed_icon);
357   g_test_add_func ("/icons/file", test_file_icon);
358
359   return g_test_run();
360 }