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_serialize (void)
47 /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
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));
58 g_object_unref (icon);
59 g_object_unref (icon2);
60 g_object_unref (location);
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));
71 g_object_unref (icon);
72 g_object_unref (icon2);
73 g_object_unref (location);
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));
84 g_object_unref (icon);
85 g_object_unref (icon2);
86 g_object_unref (location);
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));
98 g_object_unref (icon);
99 g_object_unref (icon2);
100 g_object_unref (location);
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));
110 g_object_unref (icon);
111 g_object_unref (icon2);
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);
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);
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");
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);
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");
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);
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");
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);
178 /* Check that GThemedIcon serialization works */
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));
187 g_object_unref (icon);
188 g_object_unref (icon2);
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));
197 g_object_unref (icon);
198 g_object_unref (icon2);
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));
207 g_object_unref (icon);
208 g_object_unref (icon2);
210 /* Check that GEmblemedIcon serialization works */
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);
237 test_themed_icon (void)
239 GIcon *icon1, *icon2, *icon3;
240 const gchar *const *names;
241 const gchar *names2[] = { "first", "testicon", "last", NULL };
245 icon1 = g_themed_icon_new ("testicon");
247 g_object_get (icon1, "use-default-fallbacks", &fallbacks, NULL);
248 g_assert (!fallbacks);
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");
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);
263 icon2 = g_themed_icon_new_from_names ((gchar**)names2, -1);
264 g_assert (g_icon_equal (icon1, icon2));
266 str = g_icon_to_string (icon2);
267 icon3 = g_icon_new_for_string (str, NULL);
268 g_assert (g_icon_equal (icon2, icon3));
271 g_object_unref (icon1);
272 g_object_unref (icon2);
273 g_object_unref (icon3);
277 test_emblemed_icon (void)
279 GIcon *icon1, *icon2, *icon3, *icon4;
280 GEmblem *emblem, *emblem1, *emblem2;
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);
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);
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);
298 g_assert (!g_icon_equal (icon3, icon4));
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);
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);
308 g_object_unref (icon1);
309 g_object_unref (icon2);
310 g_object_unref (icon3);
311 g_object_unref (icon4);
313 g_object_unref (emblem1);
314 g_object_unref (emblem2);
318 test_file_icon (void)
324 GInputStream *stream;
327 file = g_file_new_for_path (SRCDIR "/g-icon.c");
328 icon = g_file_icon_new (file);
329 g_object_unref (file);
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);
336 g_object_unref (stream);
338 str = g_icon_to_string (icon);
339 icon2 = g_icon_new_for_string (str, NULL);
340 g_assert (g_icon_equal (icon, icon2));
343 g_object_unref (icon);
344 g_object_unref (icon2);
352 g_test_init (&argc, &argv, NULL);
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);